@@ -84,6 +84,7 @@ controller_interface::CallbackReturn RobotController::on_configure(const rclcpp_
84
84
auto callback =
85
85
[this ](const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> traj_msg) -> void
86
86
{
87
+ RCLCPP_INFO (get_node ()->get_logger (), " Received new trajectory." );
87
88
traj_msg_external_point_ptr_.writeFromNonRT (traj_msg);
88
89
new_msg_ = true ;
89
90
};
@@ -136,15 +137,26 @@ void interpolate_point(
136
137
137
138
void interpolate_trajectory_point (
138
139
const trajectory_msgs::msg::JointTrajectory & traj_msg, const rclcpp::Duration & cur_time,
139
- trajectory_msgs::msg::JointTrajectoryPoint & point_interp)
140
+ trajectory_msgs::msg::JointTrajectoryPoint & point_interp, bool & reached_end )
140
141
{
141
- double traj_len = traj_msg.points .size ();
142
- auto last_time = traj_msg.points [traj_len - 1 ] .time_from_start ;
142
+ double traj_len = static_cast < double >( traj_msg.points .size () );
143
+ auto last_time = traj_msg.points . back () .time_from_start ;
143
144
double total_time = last_time.sec + last_time.nanosec * 1E-9 ;
145
+ double cur_time_sec = cur_time.seconds ();
146
+ reached_end = (cur_time_sec >= total_time);
144
147
145
- size_t ind = cur_time.seconds () * (traj_len / total_time);
146
- ind = std::min (static_cast <double >(ind), traj_len - 2 );
147
- double delta = cur_time.seconds () - ind * (total_time / traj_len);
148
+ // If we reached the end of the trajectory, set the velocities to zero.
149
+ if (reached_end)
150
+ {
151
+ point_interp.positions = traj_msg.points .back ().positions ;
152
+ std::fill (point_interp.velocities .begin (), point_interp.velocities .end (), 0.0 );
153
+ return ;
154
+ }
155
+
156
+ size_t ind =
157
+ static_cast <size_t >(cur_time_sec * (traj_len / total_time)); // Assumes evenly spaced points.
158
+ ind = std::min (ind, static_cast <size_t >(traj_len) - 2 );
159
+ double delta = std::min (cur_time_sec - static_cast <double >(ind) * (total_time / traj_len), 1.0 );
148
160
interpolate_point (traj_msg.points [ind], traj_msg.points [ind + 1 ], point_interp, delta);
149
161
}
150
162
@@ -160,7 +172,16 @@ controller_interface::return_type RobotController::update(
160
172
161
173
if (trajectory_msg_ != nullptr )
162
174
{
163
- interpolate_trajectory_point (*trajectory_msg_, time - start_time_, point_interp_);
175
+ bool reached_end;
176
+ interpolate_trajectory_point (*trajectory_msg_, time - start_time_, point_interp_, reached_end);
177
+
178
+ // If we have reached the end of the trajectory, reset it..
179
+ if (reached_end)
180
+ {
181
+ RCLCPP_INFO (get_node ()->get_logger (), " Trajectory execution complete." );
182
+ trajectory_msg_.reset ();
183
+ }
184
+
164
185
for (size_t i = 0 ; i < joint_position_command_interface_.size (); i++)
165
186
{
166
187
joint_position_command_interface_[i].get ().set_value (point_interp_.positions [i]);
0 commit comments