Skip to content

Commit b0391e2

Browse files
RobertWilbrandtchristophfroehlichbmagyar
authored
[jtc] Improve trajectory sampling efficiency (ros-controls#1297)
--------- Co-authored-by: Christoph Fröhlich <[email protected]> Co-authored-by: Bence Magyar <[email protected]>
1 parent 9439764 commit b0391e2

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
@@ -84,6 +84,7 @@ void Trajectory::update(std::shared_ptr<trajectory_msgs::msg::JointTrajectory> j
8484
trajectory_msg_ = joint_trajectory;
8585
trajectory_start_time_ = static_cast<rclcpp::Time>(joint_trajectory->header.stamp);
8686
sampled_already_ = false;
87+
last_sample_idx_ = 0;
8788
}
8889

8990
bool Trajectory::sample(
@@ -149,7 +150,7 @@ bool Trajectory::sample(
149150

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

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

0 commit comments

Comments
 (0)