How to make a server read the latest msg from a topic?
Greetings,
All I want to do is for my server to take a the latest msg from a topic and return it. The service server would take no inputs and return one output. Eg: Return robot position. Or Return a map.
I made a service server node. In the callback of the service I had the node subscribe to the topic I want. In the line were the service node subscribes to the topic, I expect the latest msg to be obtained right then and there from the topic because I made the topic latched by setting the latch argument to True. But it doesn't do as expected. As you can see from the code below I am forced to include the wait_for_message or else the server node never reads a msg.
The topic I am subscribed to doesn't publish frequently and waiting for a message is not practical, I just want the service to return the latest msg on the topic without waiting for the next one, I thought having the topic latched would take care of that. But it didn't.
Thank you
globvar = 0
def callback(msg_arg): # subscription callback
global globvar
print "in sub cb ",globvar
globvar = int(float(msg_arg.data)) #because you cant int(float_string)
def service_callback(req): # service callback
print "in server cb "
sb = rospy.Subscriber('chatter', String, callback)
rospy.wait_for_message("chatter", String)
global globvar
print (globvar)
sb.unregister()
return globvar