Ros2 in 5 days python: get subscriber update within action callback & publish to unknown topic /total_distance

  1. I am working on the quiz in unit 5: actions, currently I’m attempting to obtain the coordinates using /odom whilst the bot is moving and hence calculate the distance. But the action callback method is unable to access the subscriber coordinate updates during the 20 seconds request, is there any way I could access the subscriber information within action_callback?

  2. The quiz also specified to ’ Test also that the Action publishes the current distance traveled value to the topic /total_distance', but I checked this is not a preset topic, do I need to create another interface for it? What does it mean?

Any help would be much appreciated. Thanks!

Hi @zcccccc ,

Yes, you can. Define your odom_callback as usual. Then inside the action’s execute callback function, use rclpy.spin_once(self) in the loop that you use to calculate the 20 seconds. Make sure your odom_callback updates variables that can be used inside the execute_callback function.

No, you do not create another “interface”. But you create a new publisher to write the total distance travelled by the robot in the 20 seconds of action goal, incrementing the value every second.


I understand the first part and it is running successfully, thank you.

Regarding to the new publisher that I need to create, what is the msg_type? I searched and could not find any interface that is related or topic named /total_distance. What module do I import? I am confused because I am already “publishing” in the form of feedback.

Hi @zcccccc ,

I am glad you got that working!

Well, you are publishing total distance moved by the robot at every second, so you must use float. Lookup ros2 interface list to see the available options.

I am not sure where you searched for interfaces. You do know that interface is just a fancy name for “message type” in ROS2 right?!
“interfaces” are already existing with ROS2. You just need to pick the right one for your use. There are no special interfaces for every publisher that you create.

Yes, you will create another publisher which publishes “float” data into “/total_distance” topic. Although the feedback also contains the same message, this is another topic that contains the same data.


Nevermind I found that it is std_msgs. Thank you!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.