ROS2 wait for message python
Hello, I am using ros2 foxy with ubuntu 20.04 and i was wondering what was the way to retrieve a message of type odometry only once ?
I have created a node which provide a service, and I want this service to save the odomoetry of my robot in a .txt file when I call it.
I read that there was a function called waitForMessage with rospy but if I understand correctly, this is for ros1. Is there an alternative for ros2 or am i just missing an easy way to do that ?
class OdomService(Node):
def __init__(self):
super().__init__('odom_service')
self.srv = self.create_service(
Trigger, 'set_charging_station', self.trigger_callback)
def trigger_callback(self, request, response):
result = self.save_odom()
if result == 0:
response.success = True
response.message = "odom saved"
else:
self.get_logger().info(
"An error occured while saving odom, please try again.\n")
response.success = False
response.message = "An error occured"
return response
def save_odom(self):
odom = # not sure what to use here
self.get_logger().info('Odom saved :'+str(odom))
return 0
def main(args=None):
rclpy.init(args=args)
odom_service = OdomService()
rclpy.spin(odom_service)
rclpy.shutdown()
if __name__ == '__main__':
main()
Thank you for taking the time to read my question !