@@ -151,7 +151,7 @@ int main(int argc, char** argv)
151
151
service_request->request .items .push_back (item2);
152
152
153
153
// Call the service and process the result
154
- auto future = service_client->async_send_request (service_request);
154
+ auto service_future = service_client->async_send_request (service_request);
155
155
156
156
// Function to draw the trajectory
157
157
auto const draw_trajectory_tool_path =
@@ -162,17 +162,37 @@ int main(int argc, char** argv)
162
162
}
163
163
};
164
164
165
- auto response = future.get ();
166
- if (response->response .error_code .val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS) {
165
+ // Wait for the result
166
+ std::future_status service_status;
167
+ do {
168
+ switch (service_status = service_future.wait_for (std::chrono::seconds (1 )); service_status) {
169
+ case std::future_status::deferred:
170
+ RCLCPP_ERROR (LOGGER, " Deferred" );
171
+ break ;
172
+ case std::future_status::timeout:
173
+ RCLCPP_INFO (LOGGER, " Waiting for trajectory plan..." );
174
+ break ;
175
+ case std::future_status::ready:
176
+ RCLCPP_INFO (LOGGER, " Service ready!" );
177
+ break ;
178
+ }
179
+ }
180
+ while (service_status != std::future_status::ready);
181
+
182
+ auto service_response = service_future.get ();
183
+ if (service_response->response .error_code .val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS) {
167
184
RCLCPP_INFO (LOGGER, " Planning successful" );
168
185
169
186
// Access the planned trajectory
170
- auto trajectory = response ->response .planned_trajectories ;
187
+ auto trajectory = service_response ->response .planned_trajectories ;
171
188
draw_trajectory_tool_path (trajectory);
172
189
moveit_visual_tools.trigger ();
173
190
174
191
} else {
175
- RCLCPP_ERROR (LOGGER, " Planning failed with error code: %d" , response->response .error_code .val );
192
+ RCLCPP_ERROR (LOGGER, " Planning failed with error code: %d" , service_response->response .error_code .val );
193
+
194
+ rclcpp::shutdown ();
195
+ return 0 ;
176
196
}
177
197
178
198
moveit_visual_tools.prompt (" Press 'next' in the RvizVisualToolsGui window to continue the demo" );
@@ -184,10 +204,10 @@ int main(int argc, char** argv)
184
204
185
205
// MoveGroupSequence action client
186
206
using MoveGroupSequence = moveit_msgs::action::MoveGroupSequence;
187
- auto client = rclcpp_action::create_client<MoveGroupSequence>(node, " /sequence_move_group" );
207
+ auto action_client = rclcpp_action::create_client<MoveGroupSequence>(node, " /sequence_move_group" );
188
208
189
209
// Verify that the action server is up and running
190
- if (!client ->wait_for_action_server (std::chrono::seconds (10 ))) {
210
+ if (!action_client ->wait_for_action_server (std::chrono::seconds (10 ))) {
191
211
RCLCPP_ERROR (LOGGER, " Error waiting for action server /sequence_move_group" );
192
212
return -1 ;
193
213
}
@@ -241,14 +261,30 @@ int main(int argc, char** argv)
241
261
};
242
262
243
263
// Send the action goal
244
- auto goal_handle_future = client ->async_send_goal (goal_msg, send_goal_options);
264
+ auto goal_handle_future = action_client ->async_send_goal (goal_msg, send_goal_options);
245
265
246
266
// Get result
247
- auto future_result = client ->async_get_result (goal_handle_future.get ());
267
+ auto action_result_future = action_client ->async_get_result (goal_handle_future.get ());
248
268
249
269
// Wait for the result
250
- if (future_result.valid ()) {
251
- auto result = future_result.get (); // Blocks the program execution until it gets a response
270
+ std::future_status action_status;
271
+ do {
272
+ switch (action_status = action_result_future.wait_for (std::chrono::seconds (1 )); action_status) {
273
+ case std::future_status::deferred:
274
+ RCLCPP_ERROR (LOGGER, " Deferred" );
275
+ break ;
276
+ case std::future_status::timeout:
277
+ RCLCPP_INFO (LOGGER, " Executing trajectory..." );
278
+ break ;
279
+ case std::future_status::ready:
280
+ RCLCPP_INFO (LOGGER, " Action ready!" );
281
+ break ;
282
+ }
283
+ }
284
+ while (action_status != std::future_status::ready);
285
+
286
+ if (action_result_future.valid ()) {
287
+ auto result = action_result_future.get ();
252
288
RCLCPP_INFO (LOGGER, " Action completed. Result: %d" , static_cast <int >(result.code ));
253
289
} else {
254
290
RCLCPP_ERROR (LOGGER, " Action couldn't be completed." );
@@ -261,13 +297,29 @@ int main(int argc, char** argv)
261
297
// [ --------------------------------------------------------------- ]
262
298
263
299
// Repeats the motion but after 2 seconds cancels the action
264
- auto goal_handle_future_new = client ->async_send_goal (goal_msg, send_goal_options);
300
+ auto cancel_goal_handle_future = action_client ->async_send_goal (goal_msg, send_goal_options);
265
301
sleep (2 );
266
- auto future_cancel_motion = client->async_cancel_goal (goal_handle_future_new.get ());
302
+ auto cancel_action_result_future = action_client->async_cancel_goal (cancel_goal_handle_future.get ());
303
+
304
+ // Wait for the result
305
+ std::future_status action_cancel_status;
306
+ do {
307
+ switch (action_cancel_status = cancel_action_result_future.wait_for (std::chrono::seconds (1 )); action_cancel_status) {
308
+ case std::future_status::deferred:
309
+ RCLCPP_ERROR (LOGGER, " Deferred" );
310
+ break ;
311
+ case std::future_status::timeout:
312
+ RCLCPP_INFO (LOGGER, " Waiting for trajectory stop..." );
313
+ break ;
314
+ case std::future_status::ready:
315
+ RCLCPP_INFO (LOGGER, " Action cancel!" );
316
+ break ;
317
+ }
318
+ }
319
+ while (action_cancel_status != std::future_status::ready);
267
320
268
- // Wait until action cancel
269
- if (future_cancel_motion.valid ()) {
270
- auto cancel_response = future_cancel_motion.get (); // Blocks the program execution until it gets a response
321
+ if (cancel_action_result_future.valid ()) {
322
+ auto cancel_response = cancel_action_result_future.get ();
271
323
272
324
if (!cancel_response->return_code ) {
273
325
RCLCPP_INFO (LOGGER, " The action has been canceled by the action server." );
0 commit comments