robot_trajectory.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan, Adam Leeper */
36 
40 #include <boost/math/constants/constants.hpp>
41 #include <numeric>
42 
43 namespace robot_trajectory
44 {
45 RobotTrajectory::RobotTrajectory(const robot_model::RobotModelConstPtr& robot_model, const std::string& group)
46  : robot_model_(robot_model), group_(group.empty() ? nullptr : robot_model->getJointModelGroup(group))
47 {
48 }
49 
50 RobotTrajectory::RobotTrajectory(const robot_model::RobotModelConstPtr& robot_model,
51  const robot_model::JointModelGroup* group)
52  : robot_model_(robot_model), group_(group)
53 {
54 }
55 
56 void RobotTrajectory::setGroupName(const std::string& group_name)
57 {
58  group_ = robot_model_->getJointModelGroup(group_name);
59 }
60 
61 const std::string& RobotTrajectory::getGroupName() const
62 {
63  if (group_)
64  return group_->getName();
65  static const std::string empty;
66  return empty;
67 }
68 
70 {
71  if (duration_from_previous_.empty())
72  return 0.0;
73  else
74  return std::accumulate(duration_from_previous_.begin(), duration_from_previous_.end(), 0.0) /
75  (double)duration_from_previous_.size();
76 }
77 
79 {
80  robot_model_.swap(other.robot_model_);
81  std::swap(group_, other.group_);
82  waypoints_.swap(other.waypoints_);
84 }
85 
86 void RobotTrajectory::append(const RobotTrajectory& source, double dt)
87 {
88  waypoints_.insert(waypoints_.end(), source.waypoints_.begin(), source.waypoints_.end());
89  std::size_t index = duration_from_previous_.size();
91  source.duration_from_previous_.end());
92  if (duration_from_previous_.size() > index)
93  duration_from_previous_[index] += dt;
94 }
95 
97 {
98  std::reverse(waypoints_.begin(), waypoints_.end());
99  if (!duration_from_previous_.empty())
100  {
102  std::reverse(duration_from_previous_.begin(), duration_from_previous_.end());
103  duration_from_previous_.pop_back();
104  }
105 }
106 
108 {
109  if (waypoints_.empty())
110  return;
111 
112  const std::vector<const robot_model::JointModel*>& cont_joints =
113  group_ ? group_->getContinuousJointModels() : robot_model_->getContinuousJointModels();
114 
115  for (std::size_t i = 0; i < cont_joints.size(); ++i)
116  {
117  // unwrap continuous joints
118  double running_offset = 0.0;
119  double last_value = waypoints_[0]->getJointPositions(cont_joints[i])[0];
120 
121  for (std::size_t j = 1; j < waypoints_.size(); ++j)
122  {
123  double current_value = waypoints_[j]->getJointPositions(cont_joints[i])[0];
124  if (last_value > current_value + boost::math::constants::pi<double>())
125  running_offset += 2.0 * boost::math::constants::pi<double>();
126  else if (current_value > last_value + boost::math::constants::pi<double>())
127  running_offset -= 2.0 * boost::math::constants::pi<double>();
128 
129  last_value = current_value;
130  if (running_offset > std::numeric_limits<double>::epsilon() ||
131  running_offset < -std::numeric_limits<double>::epsilon())
132  {
133  current_value += running_offset;
134  waypoints_[j]->setJointPositions(cont_joints[i], &current_value);
135  }
136  }
137  }
138  for (std::size_t j = 0; j < waypoints_.size(); ++j)
139  waypoints_[j]->update();
140 }
141 
143 {
144  if (waypoints_.empty())
145  return;
146 
147  const std::vector<const robot_model::JointModel*>& cont_joints =
148  group_ ? group_->getContinuousJointModels() : robot_model_->getContinuousJointModels();
149 
150  for (std::size_t i = 0; i < cont_joints.size(); ++i)
151  {
152  double reference_value0 = state.getJointPositions(cont_joints[i])[0];
153  double reference_value = reference_value0;
154  cont_joints[i]->enforcePositionBounds(&reference_value);
155 
156  // unwrap continuous joints
157  double running_offset = reference_value0 - reference_value;
158 
159  double last_value = waypoints_[0]->getJointPositions(cont_joints[i])[0];
160  if (running_offset > std::numeric_limits<double>::epsilon() ||
161  running_offset < -std::numeric_limits<double>::epsilon())
162  {
163  double current_value = last_value + running_offset;
164  waypoints_[0]->setJointPositions(cont_joints[i], &current_value);
165  }
166 
167  for (std::size_t j = 1; j < waypoints_.size(); ++j)
168  {
169  double current_value = waypoints_[j]->getJointPositions(cont_joints[i])[0];
170  if (last_value > current_value + boost::math::constants::pi<double>())
171  running_offset += 2.0 * boost::math::constants::pi<double>();
172  else if (current_value > last_value + boost::math::constants::pi<double>())
173  running_offset -= 2.0 * boost::math::constants::pi<double>();
174 
175  last_value = current_value;
176  if (running_offset > std::numeric_limits<double>::epsilon() ||
177  running_offset < -std::numeric_limits<double>::epsilon())
178  {
179  current_value += running_offset;
180  waypoints_[j]->setJointPositions(cont_joints[i], &current_value);
181  }
182  }
183  }
184  for (std::size_t j = 0; j < waypoints_.size(); ++j)
185  waypoints_[j]->update();
186 }
187 
189 {
190  waypoints_.clear();
191  duration_from_previous_.clear();
192 }
193 
194 void RobotTrajectory::getRobotTrajectoryMsg(moveit_msgs::RobotTrajectory& trajectory) const
195 {
196  trajectory = moveit_msgs::RobotTrajectory();
197  if (waypoints_.empty())
198  return;
199  const std::vector<const robot_model::JointModel*>& jnt =
200  group_ ? group_->getActiveJointModels() : robot_model_->getActiveJointModels();
201 
202  std::vector<const robot_model::JointModel*> onedof;
203  std::vector<const robot_model::JointModel*> mdof;
204  trajectory.joint_trajectory.joint_names.clear();
205  trajectory.multi_dof_joint_trajectory.joint_names.clear();
206 
207  for (std::size_t i = 0; i < jnt.size(); ++i)
208  if (jnt[i]->getVariableCount() == 1)
209  {
210  trajectory.joint_trajectory.joint_names.push_back(jnt[i]->getName());
211  onedof.push_back(jnt[i]);
212  }
213  else
214  {
215  trajectory.multi_dof_joint_trajectory.joint_names.push_back(jnt[i]->getName());
216  mdof.push_back(jnt[i]);
217  }
218  if (!onedof.empty())
219  {
220  trajectory.joint_trajectory.header.frame_id = robot_model_->getModelFrame();
221  trajectory.joint_trajectory.header.stamp = ros::Time(0);
222  trajectory.joint_trajectory.points.resize(waypoints_.size());
223  }
224 
225  if (!mdof.empty())
226  {
227  trajectory.multi_dof_joint_trajectory.header.frame_id = robot_model_->getModelFrame();
228  trajectory.multi_dof_joint_trajectory.header.stamp = ros::Time(0);
229  trajectory.multi_dof_joint_trajectory.points.resize(waypoints_.size());
230  }
231 
232  static const ros::Duration zero_duration(0.0);
233  double total_time = 0.0;
234  for (std::size_t i = 0; i < waypoints_.size(); ++i)
235  {
236  if (duration_from_previous_.size() > i)
237  total_time += duration_from_previous_[i];
238 
239  if (!onedof.empty())
240  {
241  trajectory.joint_trajectory.points[i].positions.resize(onedof.size());
242  trajectory.joint_trajectory.points[i].velocities.reserve(onedof.size());
243 
244  for (std::size_t j = 0; j < onedof.size(); ++j)
245  {
246  trajectory.joint_trajectory.points[i].positions[j] =
247  waypoints_[i]->getVariablePosition(onedof[j]->getFirstVariableIndex());
248  // if we have velocities/accelerations/effort, copy those too
249  if (waypoints_[i]->hasVelocities())
250  trajectory.joint_trajectory.points[i].velocities.push_back(
251  waypoints_[i]->getVariableVelocity(onedof[j]->getFirstVariableIndex()));
252  if (waypoints_[i]->hasAccelerations())
253  trajectory.joint_trajectory.points[i].accelerations.push_back(
254  waypoints_[i]->getVariableAcceleration(onedof[j]->getFirstVariableIndex()));
255  if (waypoints_[i]->hasEffort())
256  trajectory.joint_trajectory.points[i].effort.push_back(
257  waypoints_[i]->getVariableEffort(onedof[j]->getFirstVariableIndex()));
258  }
259  // clear velocities if we have an incomplete specification
260  if (trajectory.joint_trajectory.points[i].velocities.size() != onedof.size())
261  trajectory.joint_trajectory.points[i].velocities.clear();
262  // clear accelerations if we have an incomplete specification
263  if (trajectory.joint_trajectory.points[i].accelerations.size() != onedof.size())
264  trajectory.joint_trajectory.points[i].accelerations.clear();
265  // clear effort if we have an incomplete specification
266  if (trajectory.joint_trajectory.points[i].effort.size() != onedof.size())
267  trajectory.joint_trajectory.points[i].effort.clear();
268 
269  if (duration_from_previous_.size() > i)
270  trajectory.joint_trajectory.points[i].time_from_start = ros::Duration(total_time);
271  else
272  trajectory.joint_trajectory.points[i].time_from_start = zero_duration;
273  }
274  if (!mdof.empty())
275  {
276  trajectory.multi_dof_joint_trajectory.points[i].transforms.resize(mdof.size());
277  for (std::size_t j = 0; j < mdof.size(); ++j)
278  {
279  tf::transformEigenToMsg(waypoints_[i]->getJointTransform(mdof[j]),
280  trajectory.multi_dof_joint_trajectory.points[i].transforms[j]);
281  // TODO: currently only checking for planar multi DOF joints / need to add check for floating
282  if (waypoints_[i]->hasVelocities() && (mdof[j]->getType() == robot_model::JointModel::JointType::PLANAR))
283  {
284  const std::vector<std::string> names = mdof[j]->getVariableNames();
285  const double* velocities = waypoints_[i]->getJointVelocities(mdof[j]);
286 
287  geometry_msgs::Twist point_velocity;
288 
289  for (std::size_t k = 0; k < names.size(); ++k)
290  {
291  if (names[k].find("/x") != std::string::npos)
292  {
293  point_velocity.linear.x = velocities[k];
294  }
295  else if (names[k].find("/y") != std::string::npos)
296  {
297  point_velocity.linear.y = velocities[k];
298  }
299  else if (names[k].find("/z") != std::string::npos)
300  {
301  point_velocity.linear.z = velocities[k];
302  }
303  else if (names[k].find("/theta") != std::string::npos)
304  {
305  point_velocity.angular.z = velocities[k];
306  }
307  }
308  trajectory.multi_dof_joint_trajectory.points[i].velocities.push_back(point_velocity);
309  }
310  }
311  if (duration_from_previous_.size() > i)
312  trajectory.multi_dof_joint_trajectory.points[i].time_from_start = ros::Duration(total_time);
313  else
314  trajectory.multi_dof_joint_trajectory.points[i].time_from_start = zero_duration;
315  }
316  }
317 }
318 
320  const trajectory_msgs::JointTrajectory& trajectory)
321 {
322  // make a copy just in case the next clear() removes the memory for the reference passed in
323  robot_state::RobotState copy = reference_state;
324  clear();
325  std::size_t state_count = trajectory.points.size();
326  ros::Time last_time_stamp = trajectory.header.stamp;
327  ros::Time this_time_stamp = last_time_stamp;
328 
329  for (std::size_t i = 0; i < state_count; ++i)
330  {
331  this_time_stamp = trajectory.header.stamp + trajectory.points[i].time_from_start;
332  robot_state::RobotStatePtr st(new robot_state::RobotState(copy));
333  st->setVariablePositions(trajectory.joint_names, trajectory.points[i].positions);
334  if (!trajectory.points[i].velocities.empty())
335  st->setVariableVelocities(trajectory.joint_names, trajectory.points[i].velocities);
336  if (!trajectory.points[i].accelerations.empty())
337  st->setVariableAccelerations(trajectory.joint_names, trajectory.points[i].accelerations);
338  if (!trajectory.points[i].effort.empty())
339  st->setVariableEffort(trajectory.joint_names, trajectory.points[i].effort);
340  addSuffixWayPoint(st, (this_time_stamp - last_time_stamp).toSec());
341  last_time_stamp = this_time_stamp;
342  }
343 }
344 
346  const moveit_msgs::RobotTrajectory& trajectory)
347 {
348  // make a copy just in case the next clear() removes the memory for the reference passed in
349  robot_state::RobotState copy = reference_state;
350  clear();
351 
352  std::size_t state_count =
353  std::max(trajectory.joint_trajectory.points.size(), trajectory.multi_dof_joint_trajectory.points.size());
354  ros::Time last_time_stamp = trajectory.joint_trajectory.points.empty() ?
355  trajectory.multi_dof_joint_trajectory.header.stamp :
356  trajectory.joint_trajectory.header.stamp;
357  ros::Time this_time_stamp = last_time_stamp;
358 
359  for (std::size_t i = 0; i < state_count; ++i)
360  {
361  robot_state::RobotStatePtr st(new robot_state::RobotState(copy));
362  if (trajectory.joint_trajectory.points.size() > i)
363  {
364  st->setVariablePositions(trajectory.joint_trajectory.joint_names,
365  trajectory.joint_trajectory.points[i].positions);
366  if (!trajectory.joint_trajectory.points[i].velocities.empty())
367  st->setVariableVelocities(trajectory.joint_trajectory.joint_names,
368  trajectory.joint_trajectory.points[i].velocities);
369  if (!trajectory.joint_trajectory.points[i].accelerations.empty())
370  st->setVariableAccelerations(trajectory.joint_trajectory.joint_names,
371  trajectory.joint_trajectory.points[i].accelerations);
372  if (!trajectory.joint_trajectory.points[i].effort.empty())
373  st->setVariableEffort(trajectory.joint_trajectory.joint_names, trajectory.joint_trajectory.points[i].effort);
374  this_time_stamp =
375  trajectory.joint_trajectory.header.stamp + trajectory.joint_trajectory.points[i].time_from_start;
376  }
377  if (trajectory.multi_dof_joint_trajectory.points.size() > i)
378  {
379  for (std::size_t j = 0; j < trajectory.multi_dof_joint_trajectory.joint_names.size(); ++j)
380  {
381  Eigen::Affine3d t;
382  tf::transformMsgToEigen(trajectory.multi_dof_joint_trajectory.points[i].transforms[j], t);
383  st->setJointPositions(trajectory.multi_dof_joint_trajectory.joint_names[j], t);
384  }
385  this_time_stamp = trajectory.multi_dof_joint_trajectory.header.stamp +
386  trajectory.multi_dof_joint_trajectory.points[i].time_from_start;
387  }
388 
389  addSuffixWayPoint(st, (this_time_stamp - last_time_stamp).toSec());
390  last_time_stamp = this_time_stamp;
391  }
392 }
393 
395  const moveit_msgs::RobotState& state,
396  const moveit_msgs::RobotTrajectory& trajectory)
397 {
398  robot_state::RobotState st(reference_state);
400  setRobotTrajectoryMsg(st, trajectory);
401 }
402 
403 void RobotTrajectory::findWayPointIndicesForDurationAfterStart(const double& duration, int& before, int& after,
404  double& blend) const
405 {
406  if (duration < 0.0)
407  {
408  before = 0;
409  after = 0;
410  blend = 0;
411  return;
412  }
413 
414  // Find indicies
415  std::size_t index = 0, num_points = waypoints_.size();
416  double running_duration = 0.0;
417  for (; index < num_points; ++index)
418  {
419  running_duration += duration_from_previous_[index];
420  if (running_duration >= duration)
421  break;
422  }
423  before = std::max<int>(index - 1, 0);
424  after = std::min<int>(index, num_points - 1);
425 
426  // Compute duration blend
427  double before_time = running_duration - duration_from_previous_[index];
428  if (after == before)
429  blend = 1.0;
430  else
431  blend = (duration - before_time) / duration_from_previous_[index];
432 }
433 
434 double RobotTrajectory::getWayPointDurationFromStart(std::size_t index) const
435 {
436  if (duration_from_previous_.empty())
437  return 0.0;
438  if (index >= duration_from_previous_.size())
439  index = duration_from_previous_.size() - 1;
440 
441  double time = 0.0;
442  for (std::size_t i = 0; i <= index; ++i)
443  time += duration_from_previous_[i];
444  return time;
445 }
446 
447 double RobotTrajectory::getWaypointDurationFromStart(std::size_t index) const
448 {
449  return getWayPointDurationFromStart(index);
450 }
451 
452 bool RobotTrajectory::getStateAtDurationFromStart(const double request_duration,
453  robot_state::RobotStatePtr& output_state) const
454 {
455  // If there are no waypoints we can't do anything
456  if (getWayPointCount() == 0)
457  return false;
458 
459  int before = 0, after = 0;
460  double blend = 1.0;
461  findWayPointIndicesForDurationAfterStart(request_duration, before, after, blend);
462  // ROS_DEBUG_NAMED("robot_trajectory", "Interpolating %.3f of the way between index %d and %d.", blend, before,
463  // after);
464  waypoints_[before]->interpolate(*waypoints_[after], blend, *output_state);
465  return true;
466 }
467 
468 } // end of namespace robot_trajectory
void append(const RobotTrajectory &source, double dt)
Add a trajectory to the end of the current trajectory.
Core components of MoveIt!
void swap(robot_trajectory::RobotTrajectory &other)
std::deque< robot_state::RobotStatePtr > waypoints_
const std::string & getGroupName() const
void setGroupName(const std::string &group_name)
const robot_model::JointModelGroup * group_
double getWayPointDurationFromStart(std::size_t index) const
Returns the duration after start that a waypoint will be reached.
const std::string & getName() const
Get the name of the joint group.
void transformEigenToMsg(const Eigen::Affine3d &e, geometry_msgs::Transform &m)
robot_model::RobotModelConstPtr robot_model_
RobotTrajectory(const robot_model::RobotModelConstPtr &robot_model, const std::string &group)
std::string getName(void *handle)
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
void getRobotTrajectoryMsg(moveit_msgs::RobotTrajectory &trajectory) const
const std::vector< const JointModel * > & getContinuousJointModels() const
Get the array of continuous joints used in this group (may include mimic joints). ...
unsigned int index
bool getStateAtDurationFromStart(const double request_duration, robot_state::RobotStatePtr &output_state) const
Gets a robot state corresponding to a supplied duration from start for the trajectory, using linear time interpolation.
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")
Maintain a sequence of waypoints and the time durations between these waypoints.
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
Convert a robot state msg (with accompanying extra transforms) to a MoveIt! robot state...
MOVEIT_DEPRECATED double getWaypointDurationFromStart(std::size_t index) const
std::size_t getWayPointCount() const
void transformMsgToEigen(const geometry_msgs::Transform &m, Eigen::Affine3d &e)
Representation of a robot&#39;s state. This includes position, velocity, acceleration and effort...
Definition: robot_state.h:123
const std::vector< const JointModel * > & getActiveJointModels() const
Get the active joints in this group (that have controllable DOF). This does not include mimic joints...
const double * getJointPositions(const std::string &joint_name) const
Definition: robot_state.h:576
void findWayPointIndicesForDurationAfterStart(const double &duration, int &before, int &after, double &blend) const
Finds the waypoint indicies before and after a duration from start.
void addSuffixWayPoint(const robot_state::RobotState &state, double dt)
Add a point to the trajectory.
void setRobotTrajectoryMsg(const robot_state::RobotState &reference_state, const trajectory_msgs::JointTrajectory &trajectory)
Copy the content of the trajectory message into this class. The trajectory message itself is not requ...
std::deque< double > duration_from_previous_


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Tue Jun 12 2018 02:47:31