Skip to content

Commit 023744b

Browse files
Change in futures handles
1 parent 0efc956 commit 023744b

File tree

1 file changed

+68
-16
lines changed

1 file changed

+68
-16
lines changed

doc/how_to_guides/pilz_industrial_motion_planner/src/pilz_sequence.cpp

+68-16
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ int main(int argc, char** argv)
151151
service_request->request.items.push_back(item2);
152152

153153
// 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);
155155

156156
// Function to draw the trajectory
157157
auto const draw_trajectory_tool_path =
@@ -162,17 +162,37 @@ int main(int argc, char** argv)
162162
}
163163
};
164164

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) {
167184
RCLCPP_INFO(LOGGER, "Planning successful");
168185

169186
// Access the planned trajectory
170-
auto trajectory = response->response.planned_trajectories;
187+
auto trajectory = service_response->response.planned_trajectories;
171188
draw_trajectory_tool_path(trajectory);
172189
moveit_visual_tools.trigger();
173190

174191
} 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;
176196
}
177197

178198
moveit_visual_tools.prompt("Press 'next' in the RvizVisualToolsGui window to continue the demo");
@@ -184,10 +204,10 @@ int main(int argc, char** argv)
184204

185205
// MoveGroupSequence action client
186206
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");
188208

189209
// 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))) {
191211
RCLCPP_ERROR(LOGGER, "Error waiting for action server /sequence_move_group");
192212
return -1;
193213
}
@@ -241,14 +261,30 @@ int main(int argc, char** argv)
241261
};
242262

243263
// 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);
245265

246266
// 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());
248268

249269
// 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();
252288
RCLCPP_INFO(LOGGER, "Action completed. Result: %d", static_cast<int>(result.code));
253289
} else {
254290
RCLCPP_ERROR(LOGGER, "Action couldn't be completed.");
@@ -261,13 +297,29 @@ int main(int argc, char** argv)
261297
// [ --------------------------------------------------------------- ]
262298

263299
// 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);
265301
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);
267320

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();
271323

272324
if (!cancel_response->return_code) {
273325
RCLCPP_INFO(LOGGER, "The action has been canceled by the action server.");

0 commit comments

Comments
 (0)