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

How to design a program which should be executed only one time

asked 2023-01-27 06:36:34 -0600

Mohumohu Usagi gravatar image

I'm now learning ROS 2 and I have understood topic and publisher-subscription model. In tutorial (I use ROS 2 humble), I have learned that I create a class (succeeded from rclcpp::Node) and use interrupts (callback) when I want to communicate via topic. However, I don't know how to design a program which should be executed only one time. I have already programmed sensor node using callbacks and it's OK, because sensors should publish topic all of the time. On the other hand, when I execute some tasks, I need to execute one time, not in endless loop. I first think of using constructor of Node class, but it did not work well.

Could someone tell me how to design such a program? Moreover, I could not find manuals of functions in ROS 2, of course I know tutorial, but I need more information. Is there any document except for reading the raw C++ source code on GItHub?

I have no colleague who is familiar with ROS 2, so please tell me.

edit retag flag offensive close merge delete


I only want to control a robot while getting sensor data from other nodes. However, if I prepare two callbacks in a Node class (the one is for subscribing sensor topic, and the other is for executing main process), the sensor callback will not be called, because two callback functions cannot be executed at the same time. By the way, I often use microcomputers such as PIC or mbed, and I use an interrupt fuction for sensor callback, which has higher priority than main function.

Ultimately, I need a method which receives only the latest topic data. I use Python 3 and C++, so I'd appreciate it if you tell me Python 3 approach.

In ROS 2 style, how can I program such a program using topic data?

Mohumohu Usagi gravatar image Mohumohu Usagi  ( 2023-01-31 03:00:20 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2023-01-28 22:20:36 -0600

adityatandon gravatar image

When you want a certain part of your program to execute only once (or when it is called for execution), you can build that piece of code out as a ROS service instead of a ROS node. I would recommend you read up on services if you haven't already. A good place to do so is here.

edit flag offensive delete link more

answered 2023-01-28 13:46:01 -0600

billy gravatar image

This might be considered a general programming question, but I get the feeling from the question you know enough about programming you can figure out how to force exit a program.

So I'll assume you're looking for a ROS specific solution for some reason. Look up spinning, and more specifically spin_once.

There is also a oneshot timer class that might be useful.

Most simply just stick a shutdown() at the end of your callback. I think but am not sure that will also exit main.

edit flag offensive delete link more

Question Tools


Asked: 2023-01-27 06:36:34 -0600

Seen: 140 times

Last updated: Jan 28