Skip to content

Commit 6e2a152

Browse files
Fix potential bad optional access
1 parent 7983150 commit 6e2a152

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

picknik_reset_fault_controller/src/picknik_reset_fault_controller.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ controller_interface::return_type PicknikResetFaultController::update(
6464
{
6565
if (realtime_publisher_)
6666
{
67-
state_.data = static_cast<bool>(state_interfaces_[StateInterfaces::IN_FAULT].get_optional().value());
67+
auto state_op =
68+
command_interfaces_[CommandInterfaces::RESET_FAULT_ASYNC_SUCCESS].get_optional();
69+
if (state_op.has_value())
70+
{
71+
state_.data = static_cast<bool>(state_op.value());
72+
}
6873
realtime_publisher_->try_publish(state_);
6974
}
7075

@@ -120,14 +125,23 @@ bool PicknikResetFaultController::resetFault(
120125

121126
RCLCPP_INFO(get_node()->get_logger(), "Trying to reset faults on hardware controller.");
122127

123-
while (command_interfaces_[CommandInterfaces::RESET_FAULT_ASYNC_SUCCESS].get_optional().value() ==
124-
ASYNC_WAITING)
128+
while (command_interfaces_[CommandInterfaces::RESET_FAULT_ASYNC_SUCCESS].get_optional().value_or(
129+
ASYNC_WAITING) == ASYNC_WAITING)
125130
{
126131
// Asynchronous wait until the hardware interface has set the io value
127132
std::this_thread::sleep_for(std::chrono::milliseconds(50));
128133
}
129-
resp->success = static_cast<bool>(
130-
command_interfaces_[CommandInterfaces::RESET_FAULT_ASYNC_SUCCESS].get_optional().value());
134+
auto async_result_op =
135+
command_interfaces_[CommandInterfaces::RESET_FAULT_ASYNC_SUCCESS].get_optional();
136+
if (!async_result_op.has_value())
137+
{
138+
RCLCPP_ERROR(
139+
get_node()->get_logger(),
140+
"Failed to get result of fault reset command from hardware controller.");
141+
resp->success = false;
142+
return resp->success;
143+
}
144+
resp->success = static_cast<bool>(async_result_op.value());
131145

132146
RCLCPP_INFO(
133147
get_node()->get_logger(), "Resetting fault on hardware controller '%s'!",

0 commit comments

Comments
 (0)