Skip to content

Commit 7a5e57a

Browse files
Add trajectory start time
1 parent 27b4161 commit 7a5e57a

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

joint_trajectory_controller/src/joint_trajectory_controller.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ controller_interface::return_type JointTrajectoryController::update(
137137
// update gains of controller
138138
// TODO(christophfroehlich) activate this
139139
// once https://github.com/ros-controls/ros2_controllers/pull/761 is merged
140-
// if (use_closed_loop_control_law_)
140+
// if (traj_contr_)
141141
// {
142142
// if(traj_contr_->updateGainsRT() == false)
143143
// {
@@ -183,11 +183,11 @@ controller_interface::return_type JointTrajectoryController::update(
183183
auto new_external_msg = traj_msg_external_point_ptr_.readFromRT();
184184
// Discard,
185185
// if a goal is pending but still not active (somewhere stuck in goal_handle_timer_)
186-
// and if use_closed_loop_control_law_: wait until control law is computed by the traj_contr_
186+
// and if traj_contr_: wait until control law is computed by the traj_contr_
187187
if (
188188
current_external_msg != *new_external_msg &&
189189
(*(rt_has_pending_goal_.readFromRT()) && !active_goal) == false &&
190-
(use_closed_loop_control_law_ == false || traj_contr_->is_valid()))
190+
(traj_contr_ == nullptr || traj_contr_->is_ready()))
191191
{
192192
fill_partial_goal(*new_external_msg);
193193
sort_to_local_joint_order(*new_external_msg);
@@ -226,6 +226,11 @@ controller_interface::return_type JointTrajectoryController::update(
226226
{
227227
traj_external_point_ptr_->set_point_before_trajectory_msg(time, state_current_);
228228
}
229+
if (traj_contr_)
230+
{
231+
// set start time of trajectory to traj_contr_
232+
traj_contr_->start(time, traj_external_point_ptr_->time_from_start());
233+
}
229234
}
230235

231236
// find segment for current timestamp
@@ -298,7 +303,7 @@ controller_interface::return_type JointTrajectoryController::update(
298303
// set values for next hardware write() if tolerance is met
299304
if (!tolerance_violated_while_moving && within_goal_time)
300305
{
301-
if (use_closed_loop_control_law_)
306+
if (traj_contr_)
302307
{
303308
traj_contr_->computeCommands(
304309
tmp_command_, state_current_, state_error_, state_desired_, time, period);
@@ -1038,7 +1043,7 @@ controller_interface::CallbackReturn JointTrajectoryController::on_activate(
10381043
read_state_from_state_interfaces(last_commanded_state_);
10391044
}
10401045

1041-
if (use_closed_loop_control_law_)
1046+
if (traj_contr_)
10421047
{
10431048
traj_contr_->activate();
10441049
}
@@ -1552,7 +1557,7 @@ void JointTrajectoryController::add_new_trajectory_msg(
15521557
traj_msg_external_point_ptr_.writeFromNonRT(traj_msg);
15531558

15541559
// compute gains of controller
1555-
if (use_closed_loop_control_law_)
1560+
if (traj_contr_)
15561561
{
15571562
if (traj_contr_->computeControlLawNonRT(traj_msg) == false)
15581563
{

joint_trajectory_controller_plugins/include/joint_trajectory_controller_plugins/trajectory_controller_base.hpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,30 @@ class TrajectoryControllerBase
7575
bool computeControlLawNonRT(
7676
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & trajectory)
7777
{
78-
rt_controller_valid_.writeFromNonRT(false);
78+
rt_control_law_ready_.writeFromNonRT(false);
7979
auto ret = computeControlLaw(trajectory);
80-
rt_controller_valid_.writeFromNonRT(true);
80+
rt_control_law_ready_.writeFromNonRT(true);
8181
return ret;
8282
}
8383

84+
/**
85+
* @brief set the time when the current trajectory started
86+
*
87+
* (same logic as trajectory class)
88+
*/
89+
void start(const rclcpp::Time time, const rclcpp::Time trajectory_start_time)
90+
{
91+
if (trajectory_start_time.seconds() == 0.0)
92+
{
93+
trajectory_start_time_ = time;
94+
}
95+
else
96+
{
97+
trajectory_start_time_ = trajectory_start_time;
98+
}
99+
start_trajectory_ = true;
100+
}
101+
84102
/**
85103
* @brief update the gains from a RT thread
86104
*
@@ -108,16 +126,20 @@ class TrajectoryControllerBase
108126
virtual void reset() = 0;
109127

110128
/**
111-
* @return true if the control law is valid (updated with the trajectory)
129+
* @return true if the control law is ready (updated with the trajectory)
112130
*/
113131
JOINT_TRAJECTORY_CONTROLLER_PLUGINS_PUBLIC
114-
bool is_valid() { return rt_controller_valid_.readFromRT(); }
132+
bool is_ready() { return rt_control_law_ready_.readFromRT(); }
115133

116134
protected:
117135
// the node handle for parameter handling
118136
rclcpp_lifecycle::LifecycleNode::SharedPtr node_;
119137
// Are we computing the control law or is it valid?
120-
realtime_tools::RealtimeBuffer<bool> rt_controller_valid_;
138+
realtime_tools::RealtimeBuffer<bool> rt_control_law_ready_;
139+
// time when the current trajectory started, can be used to interpolate time-varying gains
140+
rclcpp::Time trajectory_start_time_;
141+
// use this variable to activate new gains from the non-RT thread
142+
bool start_trajectory_ = false;
121143

122144
/**
123145
* @brief compute the control law from the given trajectory

0 commit comments

Comments
 (0)