Skip to content

Commit e3b3daa

Browse files
Adding maximum retry counter in gpio controller (Multiarm part 3) - v2 (#672) (#696)
* Add waitForAsyncCommand method in gpio controller which introduces a timeout while waiting for the async command success Co-authored-by: Lennart Nachtigall <[email protected]> (cherry picked from commit fb5972d) Co-authored-by: Lennart Nachtigall <[email protected]>
1 parent 90d289d commit e3b3daa

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

ur_controllers/include/ur_controllers/gpio_controller.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ class GPIOController : public controller_interface::ControllerInterface
184184
// TODO(anyone) publishers to add: tcp_pose_pub_
185185
// TODO(anyone) subscribers to add: script_command_sub_
186186
// TODO(anyone) service servers to add: resend_robot_program_srv_, deactivate_srv_, set_payload_srv_, tare_sensor_srv_
187+
188+
/**
189+
* @brief wait until a command interface isn't in state ASYNC_WAITING anymore or until the parameter maximum_retries
190+
* have been reached
191+
*/
192+
bool waitForAsyncCommand(std::function<double(void)> get_value);
187193
};
188194
} // namespace ur_controllers
189195

ur_controllers/src/gpio_controller.cpp

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ bool GPIOController::setIO(ur_msgs::srv::SetIO::Request::SharedPtr req, ur_msgs:
347347

348348
RCLCPP_INFO(get_node()->get_logger(), "Setting digital output '%d' to state: '%1.0f'.", req->pin, req->state);
349349

350-
while (command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
351-
// Asynchronous wait until the hardware interface has set the io value
352-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
350+
if (!waitForAsyncCommand([&]() { return command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value(); })) {
351+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that io was set. (This might happen when using the "
352+
"mocked interface)");
353353
}
354354

355355
resp->success = static_cast<bool>(command_interfaces_[IO_ASYNC_SUCCESS].get_value());
@@ -361,9 +361,9 @@ bool GPIOController::setIO(ur_msgs::srv::SetIO::Request::SharedPtr req, ur_msgs:
361361

362362
RCLCPP_INFO(get_node()->get_logger(), "Setting analog output '%d' to state: '%1.0f'.", req->pin, req->state);
363363

364-
while (command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
365-
// Asynchronous wait until the hardware interface has set the io value
366-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
364+
if (!waitForAsyncCommand([&]() { return command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value(); })) {
365+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that io was set. (This might happen when using the "
366+
"mocked interface)");
367367
}
368368

369369
resp->success = static_cast<bool>(command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value());
@@ -374,9 +374,9 @@ bool GPIOController::setIO(ur_msgs::srv::SetIO::Request::SharedPtr req, ur_msgs:
374374

375375
RCLCPP_INFO(get_node()->get_logger(), "Setting tool voltage to: '%1.0f'.", req->state);
376376

377-
while (command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
378-
// Asynchronous wait until the hardware interface has set the io value
379-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
377+
if (!waitForAsyncCommand([&]() { return command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value(); })) {
378+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that io was set. (This might happen when using the "
379+
"mocked interface)");
380380
}
381381

382382
resp->success = static_cast<bool>(command_interfaces_[CommandInterfaces::IO_ASYNC_SUCCESS].get_value());
@@ -398,9 +398,11 @@ bool GPIOController::setSpeedSlider(ur_msgs::srv::SetSpeedSliderFraction::Reques
398398
command_interfaces_[CommandInterfaces::TARGET_SPEED_FRACTION_CMD].set_value(
399399
static_cast<double>(req->speed_slider_fraction));
400400

401-
while (command_interfaces_[CommandInterfaces::TARGET_SPEED_FRACTION_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
402-
// Asynchronouse wait until the hardware interface has set the slider value
403-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
401+
if (!waitForAsyncCommand([&]() {
402+
return command_interfaces_[CommandInterfaces::TARGET_SPEED_FRACTION_ASYNC_SUCCESS].get_value();
403+
})) {
404+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that target speed fraction was set. (This might happen "
405+
"when using the mocked interface)");
404406
}
405407
resp->success =
406408
static_cast<bool>(command_interfaces_[CommandInterfaces::TARGET_SPEED_FRACTION_ASYNC_SUCCESS].get_value());
@@ -421,9 +423,10 @@ bool GPIOController::resendRobotProgram(std_srvs::srv::Trigger::Request::SharedP
421423
// call the service in the hardware
422424
command_interfaces_[CommandInterfaces::RESEND_ROBOT_PROGRAM_CMD].set_value(1.0);
423425

424-
while (command_interfaces_[CommandInterfaces::RESEND_ROBOT_PROGRAM_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
425-
// Asynchronous wait until the hardware interface has set the slider value
426-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
426+
if (!waitForAsyncCommand(
427+
[&]() { return command_interfaces_[CommandInterfaces::RESEND_ROBOT_PROGRAM_ASYNC_SUCCESS].get_value(); })) {
428+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that program was sent. (This might happen when using the "
429+
"mocked interface)");
427430
}
428431
resp->success =
429432
static_cast<bool>(command_interfaces_[CommandInterfaces::RESEND_ROBOT_PROGRAM_ASYNC_SUCCESS].get_value());
@@ -446,9 +449,10 @@ bool GPIOController::handBackControl(std_srvs::srv::Trigger::Request::SharedPtr
446449
// call the service in the hardware
447450
command_interfaces_[CommandInterfaces::HAND_BACK_CONTROL_CMD].set_value(1.0);
448451

449-
while (command_interfaces_[CommandInterfaces::HAND_BACK_CONTROL_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
450-
// Asynchronous wait until the command has been executed
451-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
452+
if (!waitForAsyncCommand(
453+
[&]() { return command_interfaces_[CommandInterfaces::HAND_BACK_CONTROL_ASYNC_SUCCESS].get_value(); })) {
454+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that hand_back_control was correctly triggered. (This "
455+
"might happen when using the mocked interface)");
452456
}
453457
resp->success =
454458
static_cast<bool>(command_interfaces_[CommandInterfaces::HAND_BACK_CONTROL_ASYNC_SUCCESS].get_value());
@@ -474,9 +478,10 @@ bool GPIOController::setPayload(const ur_msgs::srv::SetPayload::Request::SharedP
474478
command_interfaces_[CommandInterfaces::PAYLOAD_COG_Y].set_value(req->center_of_gravity.y);
475479
command_interfaces_[CommandInterfaces::PAYLOAD_COG_Z].set_value(req->center_of_gravity.z);
476480

477-
while (command_interfaces_[CommandInterfaces::PAYLOAD_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
478-
// Asynchronous wait until the hardware interface has set the payload
479-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
481+
if (!waitForAsyncCommand(
482+
[&]() { return command_interfaces_[CommandInterfaces::PAYLOAD_ASYNC_SUCCESS].get_value(); })) {
483+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that payload was set. (This might happen when using the "
484+
"mocked interface)");
480485
}
481486

482487
resp->success = static_cast<bool>(command_interfaces_[CommandInterfaces::PAYLOAD_ASYNC_SUCCESS].get_value());
@@ -499,9 +504,10 @@ bool GPIOController::zeroFTSensor(std_srvs::srv::Trigger::Request::SharedPtr /*r
499504
// call the service in the hardware
500505
command_interfaces_[CommandInterfaces::ZERO_FTSENSOR_CMD].set_value(1.0);
501506

502-
while (command_interfaces_[CommandInterfaces::ZERO_FTSENSOR_ASYNC_SUCCESS].get_value() == ASYNC_WAITING) {
503-
// Asynchronous wait until the hardware interface has set the slider value
504-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
507+
if (!waitForAsyncCommand(
508+
[&]() { return command_interfaces_[CommandInterfaces::ZERO_FTSENSOR_ASYNC_SUCCESS].get_value(); })) {
509+
RCLCPP_WARN(get_node()->get_logger(), "Could not verify that FTS was zeroed. (This might happen when using the "
510+
"mocked interface)");
505511
}
506512

507513
resp->success = static_cast<bool>(command_interfaces_[CommandInterfaces::ZERO_FTSENSOR_ASYNC_SUCCESS].get_value());
@@ -524,6 +530,20 @@ void GPIOController::initMsgs()
524530
io_msg_.analog_out_states.resize(2);
525531
}
526532

533+
bool GPIOController::waitForAsyncCommand(std::function<double(void)> get_value)
534+
{
535+
const auto maximum_retries = params_.check_io_successfull_retries;
536+
int retries = 0;
537+
while (get_value() == ASYNC_WAITING) {
538+
std::this_thread::sleep_for(std::chrono::milliseconds(50));
539+
retries++;
540+
541+
if (retries > maximum_retries)
542+
return false;
543+
}
544+
return true;
545+
}
546+
527547
} // namespace ur_controllers
528548

529549
#include "pluginlib/class_list_macros.hpp"

ur_controllers/src/gpio_controller_parameters.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ gpio_controller:
44
default_value: "",
55
description: "Urdf prefix of the corresponding arm"
66
}
7+
check_io_successfull_retries: {
8+
type: int,
9+
default_value: 10,
10+
description: "Amount of retries for checking if the selected gpio was set successfully"
11+
}

0 commit comments

Comments
 (0)