joint_trajectory_segment.h
Go to the documentation of this file.
1 // Copyright (C) 2013, PAL Robotics S.L.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of PAL Robotics S.L. nor the names of its
12 // contributors may be used to endorse or promote products derived from
13 // this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
27 
29 
30 #pragma once
31 
32 
33 // C++ standard
34 #include <cmath>
35 #include <stdexcept>
36 #include <string>
37 #include <vector>
38 
39 // angles
40 #include <angles/angles.h>
41 
42 // ROS messages
43 #include <control_msgs/FollowJointTrajectoryAction.h>
44 #include <trajectory_msgs/JointTrajectoryPoint.h>
45 
46 // ros_controls
48 
49 // Project
52 
54 {
55 
69 template <class Segment>
70 class JointTrajectorySegment : public Segment
71 {
72 public:
73  typedef typename Segment::Scalar Scalar;
74  typedef typename Segment::Time Time;
75 
78 
79  struct State : public Segment::State
80  {
81  typedef typename Segment::State::Scalar Scalar;
82  State() : Segment::State() {}
83  State(unsigned int size) : Segment::State(size) {}
84 
93  State(const trajectory_msgs::JointTrajectoryPoint& point,
94  const std::vector<Scalar>& position_offset = std::vector<Scalar>())
95  {
96  init(point, position_offset);
97  }
98 
99  void init(const trajectory_msgs::JointTrajectoryPoint& point,
100  const std::vector<Scalar>& position_offset = std::vector<Scalar>())
101  {
102  using std::invalid_argument;
103 
104  const unsigned int joint_dim = point.positions.size();
105 
106  // Preconditions
107  if (!isValid(point, joint_dim))
108  {
109  throw(invalid_argument("Size mismatch in trajectory point position, velocity or acceleration data."));
110  }
111  if (!position_offset.empty() && joint_dim != position_offset.size())
112  {
113  throw(invalid_argument("Size mismatch between trajectory point "
114  "and vector specifying whether joints wrap around."));
115  }
116 
117  // Initialize state
118  if (!point.positions.empty()) {this->position.resize(joint_dim);}
119  if (!point.velocities.empty()) {this->velocity.resize(joint_dim);}
120  if (!point.accelerations.empty()) {this->acceleration.resize(joint_dim);}
121 
122  for (unsigned int i = 0; i < joint_dim; ++i)
123  {
124  // Apply position offset only if it was specified
125  const Scalar offset = position_offset.empty() ? 0.0 : position_offset[i];
126 
127  if (!point.positions.empty()) {this->position[i] = point.positions[i] + offset;}
128  if (!point.velocities.empty()) {this->velocity[i] = point.velocities[i];}
129  if (!point.accelerations.empty()) {this->acceleration[i] = point.accelerations[i];}
130  }
131 
132  this->time_from_start = point.time_from_start.toSec();
133  }
134  };
135 
144  JointTrajectorySegment(const Time& start_time,
145  const State& start_state,
146  const Time& end_time,
147  const State& end_state)
148  : rt_goal_handle_(),
149  tolerances_()
150  {
151  Segment::init(start_time, start_state, end_time, end_state);
152  }
153 
165  JointTrajectorySegment(const ros::Time& traj_start_time,
166  const trajectory_msgs::JointTrajectoryPoint& start_point,
167  const trajectory_msgs::JointTrajectoryPoint& end_point,
168  const std::vector<Scalar>& position_offset = std::vector<Scalar>())
169  : rt_goal_handle_(),
170  tolerances_()
171  {
172  if (start_point.positions.size() != end_point.positions.size())
173  {
174  throw(std::invalid_argument("Can't construct segment from ROS message: "
175  "Start/end points data size mismatch."));
176  }
177 
178  const Time start_time = (traj_start_time + start_point.time_from_start).toSec();
179  const Time end_time = (traj_start_time + end_point.time_from_start).toSec();
180 
181  try
182  {
183  const State start_state(start_point, position_offset);
184  const State end_state(end_point, position_offset);
185 
186  this->init(start_time, start_state,
187  end_time, end_state);
188  }
189  catch(const std::invalid_argument& ex)
190  {
191  std::string msg = "Can't construct segment from ROS message: " + std::string(ex.what());
192  throw(std::invalid_argument(msg));
193  }
194  }
195 
198 
200  void setGoalHandle(RealtimeGoalHandlePtr rt_goal_handle) {rt_goal_handle_ = rt_goal_handle;}
201 
204 
206  void setTolerances(const SegmentTolerancesPerJoint<Scalar>& tolerances) {tolerances_ = tolerances;}
207 
208 private:
211 };
212 
222 template <class Scalar>
223 Scalar wraparoundJointOffset(const Scalar& prev_position,
224  const Scalar& next_position,
225  const bool& angle_wraparound)
226 {
227  // Return value
228  Scalar pos_offset = 0.0;
229 
230  if (angle_wraparound)
231  {
232  Scalar dist = angles::shortest_angular_distance(prev_position, next_position);
233 
234  // Deal with singularity at M_PI shortest distance
235  if (std::abs(std::abs(dist) - M_PI) < 1e-9)
236  {
237  dist = next_position > prev_position ? std::abs(dist) : -std::abs(dist);
238  }
239  pos_offset = (prev_position + dist) - next_position;
240  }
241 
242  return pos_offset;
243 }
244 
245 } // namespace
realtime_server_goal_handle.h
angles::shortest_angular_distance
static double shortest_angular_distance(double from, double to)
joint_trajectory_msg_utils.h
joint_trajectory_controller::isValid
bool isValid(const trajectory_msgs::JointTrajectoryPoint &point, const unsigned int joint_dim)
Definition: joint_trajectory_msg_utils.h:83
boost::shared_ptr< RealtimeGoalHandle >
joint_trajectory_controller::JointTrajectorySegment::Time
Segment::Time Time
Definition: joint_trajectory_segment.h:74
joint_trajectory_controller::JointTrajectorySegment::State::Scalar
Segment::State::Scalar Scalar
Definition: joint_trajectory_segment.h:81
joint_trajectory_controller::JointTrajectorySegment::Scalar
Segment::Scalar Scalar
Definition: joint_trajectory_segment.h:73
joint_trajectory_controller::JointTrajectorySegment::getGoalHandle
RealtimeGoalHandlePtr getGoalHandle() const
Definition: joint_trajectory_segment.h:197
joint_trajectory_controller::JointTrajectorySegment::JointTrajectorySegment
JointTrajectorySegment(const Time &start_time, const State &start_state, const Time &end_time, const State &end_state)
Construct segment from start and end states (boundary conditions).
Definition: joint_trajectory_segment.h:144
joint_trajectory_controller::JointTrajectorySegment::State::init
void init(const trajectory_msgs::JointTrajectoryPoint &point, const std::vector< Scalar > &position_offset=std::vector< Scalar >())
Definition: joint_trajectory_segment.h:99
joint_trajectory_controller::SegmentTolerancesPerJoint
Trajectory segment tolerances per Joint.
Definition: tolerances.h:95
joint_trajectory_controller::JointTrajectorySegment::setGoalHandle
void setGoalHandle(RealtimeGoalHandlePtr rt_goal_handle)
Set the (realtime) goal handle associated to this segment.
Definition: joint_trajectory_segment.h:200
tolerances.h
joint_trajectory_controller::JointTrajectorySegment::setTolerances
void setTolerances(const SegmentTolerancesPerJoint< Scalar > &tolerances)
Set the tolerances this segment is associated to.
Definition: joint_trajectory_segment.h:206
joint_trajectory_controller
Definition: hold_trajectory_builder.h:35
joint_trajectory_controller::JointTrajectorySegment::State
Definition: joint_trajectory_segment.h:79
joint_trajectory_controller::JointTrajectorySegment
Class representing a multi-dimensional quintic spline segment with a start and end time.
Definition: joint_trajectory_segment.h:70
joint_trajectory_controller::JointTrajectorySegment::State::State
State()
Definition: joint_trajectory_segment.h:82
joint_trajectory_controller::JointTrajectorySegment::getTolerances
const SegmentTolerancesPerJoint< Scalar > & getTolerances() const
Definition: joint_trajectory_segment.h:203
joint_trajectory_controller::JointTrajectorySegment::RealtimeGoalHandle
realtime_tools::RealtimeServerGoalHandle< control_msgs::FollowJointTrajectoryAction > RealtimeGoalHandle
Definition: joint_trajectory_segment.h:76
joint_trajectory_controller::wraparoundJointOffset
Scalar wraparoundJointOffset(const Scalar &prev_position, const Scalar &next_position, const bool &angle_wraparound)
Definition: joint_trajectory_segment.h:223
joint_trajectory_controller::JointTrajectorySegment::State::State
State(const trajectory_msgs::JointTrajectoryPoint &point, const std::vector< Scalar > &position_offset=std::vector< Scalar >())
Definition: joint_trajectory_segment.h:93
joint_trajectory_controller::JointTrajectorySegment::JointTrajectorySegment
JointTrajectorySegment(const ros::Time &traj_start_time, const trajectory_msgs::JointTrajectoryPoint &start_point, const trajectory_msgs::JointTrajectoryPoint &end_point, const std::vector< Scalar > &position_offset=std::vector< Scalar >())
Construct a segment from start and end points (boundary conditions) specified in ROS message format.
Definition: joint_trajectory_segment.h:165
realtime_tools::RealtimeServerGoalHandle
ros::Time
init
void init(const M_string &remappings)
joint_trajectory_controller::JointTrajectorySegment::tolerances_
SegmentTolerancesPerJoint< Scalar > tolerances_
Definition: joint_trajectory_segment.h:210
joint_trajectory_controller::JointTrajectorySegment::RealtimeGoalHandlePtr
boost::shared_ptr< RealtimeGoalHandle > RealtimeGoalHandlePtr
Definition: joint_trajectory_segment.h:77
joint_trajectory_controller::JointTrajectorySegment::rt_goal_handle_
RealtimeGoalHandlePtr rt_goal_handle_
Definition: joint_trajectory_segment.h:209
joint_trajectory_controller::JointTrajectorySegment::State::State
State(unsigned int size)
Definition: joint_trajectory_segment.h:83
angles.h


joint_trajectory_controller
Author(s): Adolfo Rodriguez Tsouroukdissian
autogenerated on Fri May 24 2024 02:41:24