0

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.

Community
  • 1
  • 1
Cait_L
  • 51
  • 1
  • 5

1 Answers1

0

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).

JWCS
  • 1,120
  • 1
  • 7
  • 17
  • Other than the using `CurrentTime = localtime()` instead of rospy.Time, your code for writing a `Time` publisher is correct. If you have more questions on services or multiple nodes interaction/design, make a new question specifically addressing your problems (as this one only specified publishing and understanding time usage in ROS). – JWCS Sep 30 '19 at 16:09