ros2_control - rolling
trajectory.hpp
1 // Copyright 2017 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef JOINT_TRAJECTORY_CONTROLLER__TRAJECTORY_HPP_
16 #define JOINT_TRAJECTORY_CONTROLLER__TRAJECTORY_HPP_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "joint_trajectory_controller/interpolation_methods.hpp"
22 #include "joint_trajectory_controller/visibility_control.h"
23 #include "rclcpp/time.hpp"
24 #include "trajectory_msgs/msg/joint_trajectory.hpp"
25 #include "trajectory_msgs/msg/joint_trajectory_point.hpp"
27 {
28 using TrajectoryPointIter = std::vector<trajectory_msgs::msg::JointTrajectoryPoint>::iterator;
29 using TrajectoryPointConstIter =
30  std::vector<trajectory_msgs::msg::JointTrajectoryPoint>::const_iterator;
31 
33 {
34 public:
35  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
36  Trajectory();
37 
38  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
39  explicit Trajectory(std::shared_ptr<trajectory_msgs::msg::JointTrajectory> joint_trajectory);
40 
41  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
42  explicit Trajectory(
43  const rclcpp::Time & current_time,
44  const trajectory_msgs::msg::JointTrajectoryPoint & current_point,
45  std::shared_ptr<trajectory_msgs::msg::JointTrajectory> joint_trajectory);
46 
55  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
57  const rclcpp::Time & current_time,
58  const trajectory_msgs::msg::JointTrajectoryPoint & current_point,
59  const std::vector<bool> & joints_angle_wraparound = std::vector<bool>());
60 
61  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
62  void update(std::shared_ptr<trajectory_msgs::msg::JointTrajectory> joint_trajectory);
63 
66 
102  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
103  bool sample(
104  const rclcpp::Time & sample_time,
105  const interpolation_methods::InterpolationMethod interpolation_method,
106  trajectory_msgs::msg::JointTrajectoryPoint & output_state,
107  TrajectoryPointConstIter & start_segment_itr, TrajectoryPointConstIter & end_segment_itr,
108  const bool search_monotonically_increasing = true);
109 
131  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
133  const rclcpp::Time & time_a, const trajectory_msgs::msg::JointTrajectoryPoint & state_a,
134  const rclcpp::Time & time_b, const trajectory_msgs::msg::JointTrajectoryPoint & state_b,
135  const rclcpp::Time & sample_time, trajectory_msgs::msg::JointTrajectoryPoint & output);
136 
137  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
138  TrajectoryPointConstIter begin() const;
139 
140  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
141  TrajectoryPointConstIter end() const;
142 
143  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
144  rclcpp::Time time_from_start() const;
145 
146  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
147  bool has_trajectory_msg() const;
148 
149  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
150  bool has_nontrivial_msg() const;
151 
152  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
153  std::shared_ptr<trajectory_msgs::msg::JointTrajectory> get_trajectory_msg() const
154  {
155  return trajectory_msg_;
156  }
157 
158  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
159  bool is_sampled_already() const { return sampled_already_; }
160 
162 
166  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
167  size_t last_sample_index() const { return last_sample_idx_; }
168 
169 private:
170  void deduce_from_derivatives(
171  trajectory_msgs::msg::JointTrajectoryPoint & first_state,
172  trajectory_msgs::msg::JointTrajectoryPoint & second_state, const size_t dim,
173  const double delta_t);
174 
175  std::shared_ptr<trajectory_msgs::msg::JointTrajectory> trajectory_msg_;
176  rclcpp::Time trajectory_start_time_;
177 
178  rclcpp::Time time_before_traj_msg_;
179  trajectory_msgs::msg::JointTrajectoryPoint state_before_traj_msg_;
180 
181  bool sampled_already_ = false;
182  size_t last_sample_idx_ = 0;
183 };
184 
190 template <class T>
191 inline std::vector<size_t> mapping(const T & t1, const T & t2)
192 {
193  // t1 must be a subset of t2
194  if (t1.size() > t2.size())
195  {
196  return std::vector<size_t>();
197  }
198 
199  std::vector<size_t> mapping_vector(t1.size()); // Return value
200  for (auto t1_it = t1.begin(); t1_it != t1.end(); ++t1_it)
201  {
202  auto t2_it = std::find(t2.begin(), t2.end(), *t1_it);
203  if (t2.end() == t2_it)
204  {
205  return std::vector<size_t>();
206  }
207  else
208  {
209  const size_t t1_dist = static_cast<size_t>(std::distance(t1.begin(), t1_it));
210  const size_t t2_dist = static_cast<size_t>(std::distance(t2.begin(), t2_it));
211  mapping_vector[t1_dist] = t2_dist;
212  }
213  }
214  return mapping_vector;
215 }
216 
224 void wraparound_joint(
225  std::vector<double> & current_position, const std::vector<double> next_position,
226  const std::vector<bool> & joints_angle_wraparound);
227 
228 } // namespace joint_trajectory_controller
229 
230 #endif // JOINT_TRAJECTORY_CONTROLLER__TRAJECTORY_HPP_
Definition: trajectory.hpp:33
JOINT_TRAJECTORY_CONTROLLER_PUBLIC void interpolate_between_points(const rclcpp::Time &time_a, const trajectory_msgs::msg::JointTrajectoryPoint &state_a, const rclcpp::Time &time_b, const trajectory_msgs::msg::JointTrajectoryPoint &state_b, const rclcpp::Time &sample_time, trajectory_msgs::msg::JointTrajectoryPoint &output)
Definition: trajectory.cpp:205
JOINT_TRAJECTORY_CONTROLLER_PUBLIC size_t last_sample_index() const
Get the index of the segment start returned by the last sample() operation.
Definition: trajectory.hpp:167
JOINT_TRAJECTORY_CONTROLLER_PUBLIC bool sample(const rclcpp::Time &sample_time, const interpolation_methods::InterpolationMethod interpolation_method, trajectory_msgs::msg::JointTrajectoryPoint &output_state, TrajectoryPointConstIter &start_segment_itr, TrajectoryPointConstIter &end_segment_itr, const bool search_monotonically_increasing=true)
Definition: trajectory.cpp:90
JOINT_TRAJECTORY_CONTROLLER_PUBLIC void set_point_before_trajectory_msg(const rclcpp::Time &current_time, const trajectory_msgs::msg::JointTrajectoryPoint &current_point, const std::vector< bool > &joints_angle_wraparound=std::vector< bool >())
Definition: trajectory.cpp:45
Definition: interpolation_methods.hpp:24
std::vector< size_t > mapping(const T &t1, const T &t2)
Definition: trajectory.hpp:191
void wraparound_joint(std::vector< double > &current_position, const std::vector< double > next_position, const std::vector< bool > &joints_angle_wraparound)
Definition: trajectory.cpp:59