如何创建一个将实际挂钟时间发送给订阅者的 ROS 发布者? [英] How to create a ROS publisher that sends the actual wall clock time to the subscriber?

查看:24
本文介绍了如何创建一个将实际挂钟时间发送给订阅者的 ROS 发布者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何编写一个发布者节点 (ROS Python) 时遇到了一个问题,该节点将向订阅者发送我需要做的作业的实际时间(即:12:46).我需要发布实际时间,以便订阅者可以使用该时间以及通过服务请求的时间差来计算世界其他地方的时间.(即:Node1 发布纽约时间,Node2 订阅并请求当前位置-纽约-伦敦的时差.Node1 将时差信息发送给Node2.Node2 将当前时间和时差添加到查找伦敦当前时间)

我在谷歌上搜索了这个问题,但找不到任何有用的信息.我发现了一些令人困惑的来源,其中说明了如何以秒为单位获得模拟时间,以及(也许)以秒为单位的时钟时间,但我并没有真正理解他们所说的内容足以使用这些信息.

到目前为止我拥有的代码:对不起,IDK 如何在本网站上正确格式化

<预><代码>#!/usr/bin/env python从 beginer_tutorials.srv 导入 TimeDiff, TimeDifResponse从 std_msgs.msg 导入时间进口玫瑰花pub = rospy.Publisher('currentTime', Time, queue_size=10)当前时间=本地时间()def setupPublisher():rospy.init_node('talker', 匿名=真)rate = rospy.Rate(5) # 5hz虽然不是 rospy.is_shutdown():全球当前时间当前时间=本地时间()pub.publish(当前时间)率.sleep()如果 __ 名称 __ == __ main __":设置发布者()

我真的没有任何代码可以分享,因为我不知道如何合并时间.到目前为止,这是我所拥有的,但我认为这是不对的

它应该向订阅者发布时间,但我不知道它在做什么,或者我应该使用什么变量,或者任何与此相关的东西.我们在课堂上根本没有了解这一点.

解决方案

您应该熟悉所有 ROS 预定义的消息类型(尽管有些比其他的使用得更多),包括 std_msgs std_msgs/Header 消息.它包括 std_msgs/Time 消息和一个 frame_id (String) 术语,您可以为其存储时间的位置.

此外,ROS time 类型(为此有一个 std_msg Time 包装它),需要从适当的 ROS (rospy) 方法中获取,而不是 localtime()(尽管您可以根据需要从 localtime() 制作 time ).有关更多time/Time 参考,请参阅概述客户端时间库python/rospy 细节.要点是三个(完全等效的)函数:

rospy.Time.now() #获取时间作为rospy.Time实例rospy.get_rostime() #获取时间作为rospy.Time实例,同.now()rospy.get_time() #获取时间为浮点秒数

请记住,当前时间"的标准是从 1970 UTC/GMT(时区 +-0)算起的秒数.rospy.Time 持有对此,因为它使用 python 的 time.time() 表示当前的 ROS 时间(它只是从秒开始的秒数和从第二秒开始的额外纳秒的元组).这将允许您使用其余的 python 工具根据需要对其进行格式化.正如您所看到的,ROS 还允许使用 Clock 进行模拟时间",但这是一个有趣的模拟功能,当您使用 Time 时,默认情况下这不是您获得的功能>(无需担心).

I am having an issue figuring out how to write a publisher node (ROS Python) that will send the subscriber the actual time (IE: 12:46) which I need to do for my homework. I need to publish the actual time, so that the subscriber can use that time, and a difference in time requested through a service to calculate the time somewhere else in the world. (Ie: Node1 publishes time in New York, Node2 subscribes and requests the time difference between current location-New York- and London. Node1 sends the information on the time difference to Node2. Node2 takes current time and time difference and adds them to find out the current time in London)

I have googled the issue and I cannot find any helpful information. I have found some confusing sources that say how to get the simulated time in seconds, and (maybe) the clock time in seconds, but I did not really understand what they were saying enough to use the information.

code that I have so far: Sorry, IDK how to format it right on this website


   #!/usr/bin/env python

   from beginner_tutorials.srv import TimeDiff, TimeDifResponse

   from std_msgs.msg import Time

   import rospy

   pub = rospy.Publisher('currentTime', Time, queue_size=10)
   CurrentTime= localtime()

   def setupPublisher():
    
       rospy.init_node('talker', anonymous=True)
       rate = rospy.Rate(5) # 5hz

       while not rospy.is_shutdown():

           global CurrentTime
           CurrentTime= localtime()
           pub.publish(CurrentTime)
           rate.sleep()

   if __ name __ == "__ main __":
       setupPublisher()

I don't really have any code to share because I don't know how to incorporate the time. This is what I have so far, but I don't think it's right

It should publish the time to the subscriber, but I have no idea what it is doing, or what variables I should be using, or anything for that matter. We didn't learn about this in class at all.

解决方案

You should be familiar with all the ROS pre-defined message types (although some are significantly more used than others), including the std_msgs std_msgs/Header message. It includes the std_msgs/Time message and a frame_id (String) term, for which you could store the location of the time.

Also, the ROS time type (for which there is a std_msg Time wrapping it), needs to be acquired from the appropriate ROS (rospy) method, not localtime() (although you could make the time from the localtime() if you wanted). For more time/Time references, here is the overview, client time libraries, and python / rospy specifics. The gist is the three (completely equivalent) functions:

rospy.Time.now() #get time as rospy.Time instance
rospy.get_rostime() #get time as rospy.Time instance, same as .now()
rospy.get_time() #get time as float secs

Remember the standard for the "current time" is seconds from 1970 UTC/GMT (timezone +-0). rospy.Time holds to this as it uses python's time.time() for the current ROS time (which is just a tuple of the seconds since and extra nanoseconds from the second). This will allow you to use the rest of the python tools to format it as you wish. ROS also allows a "simulated time" with the Clock, as you saw, but that is a fun feature for simulation that isn't what you get by default when you use Time (no need to worry).

这篇关于如何创建一个将实际挂钟时间发送给订阅者的 ROS 发布者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆