Robotics StackExchange | Archived questions

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

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.

Asked by Mohumohu Usagi on 2023-01-27 07:36:34 UTC

Comments

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?

Asked by Mohumohu Usagi on 2023-01-31 04:00:20 UTC

Answers

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.

Asked by billy on 2023-01-28 14:46:01 UTC

Comments

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.

Asked by adityatandon on 2023-01-28 23:20:36 UTC

Comments