Skip to content

Commit 2331a0d

Browse files
mergify[bot]christophfroehlichsaikishor
authored
JTC: Use std::atomic<bool> (backport #1720) (#1722)
Co-authored-by: Christoph Fröhlich <[email protected]> Co-authored-by: Sai Kishor Kothakota <[email protected]>
1 parent 9aeb391 commit 2331a0d

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

joint_trajectory_controller/include/joint_trajectory_controller/joint_trajectory_controller.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
1616
#define JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
1717

18+
#include <atomic>
1819
#include <chrono>
1920
#include <functional> // for std::reference_wrapper
2021
#include <memory>
@@ -160,7 +161,7 @@ class JointTrajectoryController : public controller_interface::ControllerInterfa
160161
// Timeout to consider commands old
161162
double cmd_timeout_;
162163
// True if holding position or repeating last trajectory point in case of success
163-
realtime_tools::RealtimeBuffer<bool> rt_is_holding_;
164+
std::atomic<bool> rt_is_holding_;
164165
// TODO(karsten1987): eventually activate and deactivate subscriber directly when its supported
165166
bool subscriber_is_active_ = false;
166167
rclcpp::Subscription<trajectory_msgs::msg::JointTrajectory>::SharedPtr joint_command_subscriber_ =
@@ -192,7 +193,7 @@ class JointTrajectoryController : public controller_interface::ControllerInterfa
192193

193194
rclcpp_action::Server<FollowJTrajAction>::SharedPtr action_server_;
194195
RealtimeGoalHandleBuffer rt_active_goal_; ///< Currently active action goal, if any.
195-
realtime_tools::RealtimeBuffer<bool> rt_has_pending_goal_; ///< Is there a pending action goal?
196+
std::atomic<bool> rt_has_pending_goal_; ///< Is there a pending action goal?
196197
rclcpp::TimerBase::SharedPtr goal_handle_timer_;
197198
rclcpp::Duration action_monitor_period_ = rclcpp::Duration(50ms);
198199

joint_trajectory_controller/src/joint_trajectory_controller.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ controller_interface::return_type JointTrajectoryController::update(
171171
auto current_external_msg = traj_external_point_ptr_->get_trajectory_msg();
172172
auto new_external_msg = traj_msg_external_point_ptr_.readFromRT();
173173
// Discard, if a goal is pending but still not active (somewhere stuck in goal_handle_timer_)
174-
if (
175-
current_external_msg != *new_external_msg &&
176-
(*(rt_has_pending_goal_.readFromRT()) && !active_goal) == false)
174+
if (current_external_msg != *new_external_msg && (rt_has_pending_goal_ && !active_goal) == false)
177175
{
178176
fill_partial_goal(*new_external_msg);
179177
sort_to_local_joint_order(*new_external_msg);
@@ -242,7 +240,7 @@ controller_interface::return_type JointTrajectoryController::update(
242240
// have we reached the end, are not holding position, and is a timeout configured?
243241
// Check independently of other tolerances
244242
if (
245-
!before_last_point && *(rt_is_holding_.readFromRT()) == false && cmd_timeout_ > 0.0 &&
243+
!before_last_point && !rt_is_holding_ && cmd_timeout_ > 0.0 &&
246244
time_difference > cmd_timeout_)
247245
{
248246
RCLCPP_WARN(logger, "Aborted due to command timeout");
@@ -260,15 +258,15 @@ controller_interface::return_type JointTrajectoryController::update(
260258
// is the last point
261259
// print output per default, goal will be aborted afterwards
262260
if (
263-
(before_last_point || first_sample) && *(rt_is_holding_.readFromRT()) == false &&
261+
(before_last_point || first_sample) && !rt_is_holding_ &&
264262
!check_state_tolerance_per_joint(
265263
state_error_, index, active_tol->state_tolerance[index], true /* show_errors */))
266264
{
267265
tolerance_violated_while_moving = true;
268266
}
269267
// past the final point, check that we end up inside goal tolerance
270268
if (
271-
!before_last_point && *(rt_is_holding_.readFromRT()) == false &&
269+
!before_last_point && !rt_is_holding_ &&
272270
!check_state_tolerance_per_joint(
273271
state_error_, index, active_tol->goal_state_tolerance[index], false /* show_errors */))
274272
{
@@ -355,7 +353,7 @@ controller_interface::return_type JointTrajectoryController::update(
355353
// TODO(matthew-reynolds): Need a lock-free write here
356354
// See https://github.com/ros-controls/ros2_controllers/issues/168
357355
rt_active_goal_.writeFromNonRT(RealtimeGoalHandlePtr());
358-
rt_has_pending_goal_.writeFromNonRT(false);
356+
rt_has_pending_goal_ = false;
359357

360358
RCLCPP_WARN(logger, "Aborted due to state tolerance violation");
361359

@@ -374,7 +372,7 @@ controller_interface::return_type JointTrajectoryController::update(
374372
// TODO(matthew-reynolds): Need a lock-free write here
375373
// See https://github.com/ros-controls/ros2_controllers/issues/168
376374
rt_active_goal_.writeFromNonRT(RealtimeGoalHandlePtr());
377-
rt_has_pending_goal_.writeFromNonRT(false);
375+
rt_has_pending_goal_ = false;
378376

379377
RCLCPP_INFO(logger, "Goal reached, success!");
380378

@@ -393,7 +391,7 @@ controller_interface::return_type JointTrajectoryController::update(
393391
// TODO(matthew-reynolds): Need a lock-free write here
394392
// See https://github.com/ros-controls/ros2_controllers/issues/168
395393
rt_active_goal_.writeFromNonRT(RealtimeGoalHandlePtr());
396-
rt_has_pending_goal_.writeFromNonRT(false);
394+
rt_has_pending_goal_ = false;
397395

398396
RCLCPP_WARN(logger, "%s", error_string.c_str());
399397

@@ -402,16 +400,15 @@ controller_interface::return_type JointTrajectoryController::update(
402400
}
403401
}
404402
}
405-
else if (tolerance_violated_while_moving && *(rt_has_pending_goal_.readFromRT()) == false)
403+
else if (tolerance_violated_while_moving && !rt_has_pending_goal_)
406404
{
407405
// we need to ensure that there is no pending goal -> we get a race condition otherwise
408406
RCLCPP_ERROR(logger, "Holding position due to state tolerance violation");
409407

410408
traj_msg_external_point_ptr_.reset();
411409
traj_msg_external_point_ptr_.initRT(set_hold_position());
412410
}
413-
else if (
414-
!before_last_point && !within_goal_time && *(rt_has_pending_goal_.readFromRT()) == false)
411+
else if (!before_last_point && !within_goal_time && !rt_has_pending_goal_)
415412
{
416413
RCLCPP_ERROR(logger, "Exceeded goal_time_tolerance: holding position...");
417414

@@ -1014,7 +1011,7 @@ controller_interface::CallbackReturn JointTrajectoryController::on_activate(
10141011

10151012
// The controller should start by holding position at the beginning of active state
10161013
add_new_trajectory_msg(set_hold_position());
1017-
rt_is_holding_.writeFromNonRT(true);
1014+
rt_is_holding_ = true;
10181015

10191016
// parse timeout parameter
10201017
if (params_.cmd_timeout > 0.0)
@@ -1046,7 +1043,7 @@ controller_interface::CallbackReturn JointTrajectoryController::on_deactivate(
10461043
const auto active_goal = *rt_active_goal_.readFromNonRT();
10471044
if (active_goal)
10481045
{
1049-
rt_has_pending_goal_.writeFromNonRT(false);
1046+
rt_has_pending_goal_ = false;
10501047
auto action_res = std::make_shared<FollowJTrajAction::Result>();
10511048
action_res->set__error_code(FollowJTrajAction::Result::INVALID_GOAL);
10521049
action_res->set__error_string("Current goal cancelled during deactivate transition.");
@@ -1217,7 +1214,7 @@ void JointTrajectoryController::topic_callback(
12171214
if (subscriber_is_active_)
12181215
{
12191216
add_new_trajectory_msg(msg);
1220-
rt_is_holding_.writeFromNonRT(false);
1217+
rt_is_holding_ = false;
12211218
}
12221219
};
12231220

@@ -1256,7 +1253,7 @@ rclcpp_action::CancelResponse JointTrajectoryController::goal_cancelled_callback
12561253
get_node()->get_logger(), "Canceling active action goal because cancel callback received.");
12571254

12581255
// Mark the current goal as canceled
1259-
rt_has_pending_goal_.writeFromNonRT(false);
1256+
rt_has_pending_goal_ = false;
12601257
auto action_res = std::make_shared<FollowJTrajAction::Result>();
12611258
active_goal->setCanceled(action_res);
12621259
rt_active_goal_.writeFromNonRT(RealtimeGoalHandlePtr());
@@ -1271,7 +1268,7 @@ void JointTrajectoryController::goal_accepted_callback(
12711268
std::shared_ptr<rclcpp_action::ServerGoalHandle<FollowJTrajAction>> goal_handle)
12721269
{
12731270
// mark a pending goal
1274-
rt_has_pending_goal_.writeFromNonRT(true);
1271+
rt_has_pending_goal_ = true;
12751272

12761273
// Update new trajectory
12771274
{
@@ -1280,7 +1277,7 @@ void JointTrajectoryController::goal_accepted_callback(
12801277
std::make_shared<trajectory_msgs::msg::JointTrajectory>(goal_handle->get_goal()->trajectory);
12811278

12821279
add_new_trajectory_msg(traj_msg);
1283-
rt_is_holding_.writeFromNonRT(false);
1280+
rt_is_holding_ = false;
12841281
}
12851282

12861283
// Update the active goal
@@ -1593,7 +1590,7 @@ JointTrajectoryController::set_hold_position()
15931590
hold_position_msg_ptr_->points[0].positions = state_current_.positions;
15941591

15951592
// set flag, otherwise tolerances will be checked with holding position too
1596-
rt_is_holding_.writeFromNonRT(true);
1593+
rt_is_holding_ = true;
15971594

15981595
return hold_position_msg_ptr_;
15991596
}
@@ -1607,7 +1604,7 @@ JointTrajectoryController::set_success_trajectory_point()
16071604
hold_position_msg_ptr_->points[0].time_from_start = rclcpp::Duration(0, 0);
16081605

16091606
// set flag, otherwise tolerances will be checked with success_trajectory_point too
1610-
rt_is_holding_.writeFromNonRT(true);
1607+
rt_is_holding_ = true;
16111608

16121609
return hold_position_msg_ptr_;
16131610
}

0 commit comments

Comments
 (0)