how to call services periodically using a wall_timer

asked 2022-11-01 09:47:31 -0500

ros_newb gravatar image

Dear Community,

I'm an inexperienced coder and new to ROS, please understand...

I want to figure out why I cannot call a service (by sending a request and waiting for a response) from a node's wall_timer. I've attached a text file outlining two simple methods that yielded different, undesirable results. I hope they sufficiently describe the error I'm having. If not please let me know. I am not necessarily looking for a solution, rather for an explanation of what could be causing this issue from occurring. My knowledge of ROS and C++ is limited so maybe it is really obvious to you...

Thanks to anyone taking the time to help me out!

The issue:

Calling a service periodically, using a rclcpp::wall_timer:

Wall Timer Callback Function:

void connection::timer_callback() { // 1. create a request auto test_request = network_library::create_request(1,1,1,1);

// 2. print a confirmation and the contents of the request to cmd line
network_library::print_string(" 2. sending a request asynchronously");

// 3. send the request asynchronously, create a shared_future to store response
auto result = test_client->async_send_request(test_request);

//4. print a confirmation that the request has been sent
network_library::print_string(" 4. the request has been sent...");

// 5.1 use the shared_future::get() function to "get" the resulting response from server
//network_library::print_message(result.get()); // 6 print result

// 5.2 use the rclcpp::spin_until_future_complete() function to receive the result
if (rclcpp::spin_until_future_complete(shared_from_this(), result) == rclcpp::executor::FutureReturnCode::SUCCESS) {
    network_library::print_message(result); // 6 print result


Generates the following results at runtime (copied from command line):

Results from CLIENT:

Result using method 5.1:

ostfalia@ros:~/dev_ws/src$ ros2 run client main //NOTE: nothing else happens. No other processes continue to function on the client node, no errors are returned.

Result using method 5.2:

ostfalia@ros:~/dev_ws/src$ ros2 run client main terminate called after throwing an instance of 'std::runtime_error' what(): Node has already been added to an executor.

Results from the SERVER:

Result using method 5.1/5.2 (both are identical):

ostfalia@ros:~/dev_ws/src$ ros2 run server main ^C[INFO] [1667311554.420049898] [rclcpp]: signal_handler(signal_value=2) ostfalia@ros:~/dev_ws/src$ ros2 run server main Essentially it shows that calling the spin_until_future_complete() function (5.2) results in a runtime error and in the other configuration (5.1) the .get() never returns and the client node doesn't get anywhere.

edit retag flag offensive close merge delete


how to call services periodically using a wall_timer

This may help:

ravijoshi gravatar image ravijoshi  ( 2022-11-02 07:41:40 -0500 )edit