Skip to content

Commit af1a840

Browse files
authored
[jtc] Improve trajectory sampling efficiency (#1297) (#1358)
1 parent fd8b434 commit af1a840

File tree

3 files changed

+137
-4
lines changed

3 files changed

+137
-4
lines changed

joint_trajectory_controller/include/joint_trajectory_controller/trajectory.hpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class Trajectory
6969
* acceleration respectively. Deduction assumes that the provided velocity or acceleration have to
7070
* be reached at the time defined in the segment.
7171
*
72+
* This function assumes that sampling is only done at monotonically increasing \p sample_time
73+
* for any trajectory.
74+
*
7275
* Specific case returns for start_segment_itr and end_segment_itr:
7376
* - Sampling before the trajectory start:
7477
* start_segment_itr = begin(), end_segment_itr = begin()
@@ -85,9 +88,12 @@ class Trajectory
8588
*
8689
* \param[in] sample_time Time at which trajectory will be sampled.
8790
* \param[in] interpolation_method Specify whether splines, another method, or no interpolation at
88-
* all. \param[out] expected_state Calculated new at \p sample_time. \param[out] start_segment_itr
89-
* Iterator to the start segment for given \p sample_time. See description above. \param[out]
90-
* end_segment_itr Iterator to the end segment for given \p sample_time. See description above.
91+
* all.
92+
* \param[out] output_state Calculated new at \p sample_time.
93+
* \param[out] start_segment_itr Iterator to the start segment for given \p sample_time. See
94+
* description above.
95+
* \param[out] end_segment_itr Iterator to the end segment for given \p sample_time. See
96+
* description above.
9197
*/
9298
JOINT_TRAJECTORY_CONTROLLER_PUBLIC
9399
bool sample(
@@ -147,6 +153,14 @@ class Trajectory
147153
JOINT_TRAJECTORY_CONTROLLER_PUBLIC
148154
bool is_sampled_already() const { return sampled_already_; }
149155

156+
/// Get the index of the segment start returned by the last \p sample() operation.
157+
/**
158+
* As the trajectory is only accessed at monotonically increasing sampling times, this index is
159+
* used to speed up the selection of relevant trajectory points.
160+
*/
161+
JOINT_TRAJECTORY_CONTROLLER_PUBLIC
162+
size_t last_sample_index() const { return last_sample_idx_; }
163+
150164
private:
151165
void deduce_from_derivatives(
152166
trajectory_msgs::msg::JointTrajectoryPoint & first_state,
@@ -160,6 +174,7 @@ class Trajectory
160174
trajectory_msgs::msg::JointTrajectoryPoint state_before_traj_msg_;
161175

162176
bool sampled_already_ = false;
177+
size_t last_sample_idx_ = 0;
163178
};
164179

165180
/**

joint_trajectory_controller/src/trajectory.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void Trajectory::update(std::shared_ptr<trajectory_msgs::msg::JointTrajectory> j
8585
trajectory_msg_ = joint_trajectory;
8686
trajectory_start_time_ = static_cast<rclcpp::Time>(joint_trajectory->header.stamp);
8787
sampled_already_ = false;
88+
last_sample_idx_ = 0;
8889
}
8990

9091
bool Trajectory::sample(
@@ -150,7 +151,7 @@ bool Trajectory::sample(
150151

151152
// time_from_start + trajectory time is the expected arrival time of trajectory
152153
const auto last_idx = trajectory_msg_->points.size() - 1;
153-
for (size_t i = 0; i < last_idx; ++i)
154+
for (size_t i = last_sample_idx_; i < last_idx; ++i)
154155
{
155156
auto & point = trajectory_msg_->points[i];
156157
auto & next_point = trajectory_msg_->points[i + 1];
@@ -176,13 +177,15 @@ bool Trajectory::sample(
176177
}
177178
start_segment_itr = begin() + i;
178179
end_segment_itr = begin() + (i + 1);
180+
last_sample_idx_ = i;
179181
return true;
180182
}
181183
}
182184

183185
// whole animation has played out
184186
start_segment_itr = --end();
185187
end_segment_itr = end();
188+
last_sample_idx_ = last_idx;
186189
output_state = (*start_segment_itr);
187190
// the trajectories in msg may have empty velocities/accel, so resize them
188191
if (output_state.velocities.empty())

0 commit comments

Comments
 (0)