ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS2 wait for message python

asked 2022-06-15 09:03:31 -0500

Bastian2909 gravatar image

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 !

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-15 09:27:10 -0500

ljaniec gravatar image

I think you can use a timer with the callback, that will store the last odometry message in the _last_odom_msg class field, and then you can just retrieve that value when the service is called.

edit flag offensive delete link more

Comments

Thank you, that was what i needed !

Bastian2909 gravatar image Bastian2909  ( 2022-06-29 05:28:07 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-06-15 09:03:31 -0500

Seen: 543 times

Last updated: Jun 15 '22