moveit2
The MoveIt Motion Planning Framework for ROS 2.
revolute_joint_model.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Ioan A. Sucan
5  * Copyright (c) 2008-2013, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the Willow Garage nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 /* Author: Ioan Sucan */
37 
39 #include <geometric_shapes/check_isometry.h>
40 #include <algorithm>
41 #include <cmath>
42 
43 namespace moveit
44 {
45 namespace core
46 {
47 RevoluteJointModel::RevoluteJointModel(const std::string& name, size_t joint_index, size_t first_variable_index)
48  : JointModel(name, joint_index, first_variable_index)
49  , axis_(0.0, 0.0, 0.0)
50  , continuous_(false)
51  , x2_(0.0)
52  , y2_(0.0)
53  , z2_(0.0)
54  , xy_(0.0)
55  , xz_(0.0)
56  , yz_(0.0)
57 {
58  type_ = REVOLUTE;
59  variable_names_.push_back(getName());
60  variable_bounds_.resize(1);
61  variable_bounds_[0].position_bounded_ = true;
62  variable_bounds_[0].min_position_ = -M_PI;
63  variable_bounds_[0].max_position_ = M_PI;
66 }
67 
69 {
70  return 1;
71 }
72 
74 {
75  axis_ = axis.normalized();
76  x2_ = axis_.x() * axis_.x();
77  y2_ = axis_.y() * axis_.y();
78  z2_ = axis_.z() * axis_.z();
79  xy_ = axis_.x() * axis_.y();
80  xz_ = axis_.x() * axis_.z();
81  yz_ = axis_.y() * axis_.z();
82 }
83 
85 {
86  continuous_ = flag;
87  if (flag)
88  {
89  variable_bounds_[0].position_bounded_ = false;
90  variable_bounds_[0].min_position_ = -M_PI;
91  variable_bounds_[0].max_position_ = M_PI;
92  }
93  else
94  variable_bounds_[0].position_bounded_ = true;
96 }
97 
98 double RevoluteJointModel::getMaximumExtent(const Bounds& /*other_bounds*/) const
99 {
100  return variable_bounds_[0].max_position_ - variable_bounds_[0].min_position_;
101 }
102 
103 void RevoluteJointModel::getVariableDefaultPositions(double* values, const Bounds& bounds) const
104 {
105  // if zero is a valid value
106  if (bounds[0].min_position_ <= 0.0 && bounds[0].max_position_ >= 0.0)
107  values[0] = 0.0;
108  else
109  values[0] = (bounds[0].min_position_ + bounds[0].max_position_) / 2.0;
110 }
111 
112 void RevoluteJointModel::getVariableRandomPositions(random_numbers::RandomNumberGenerator& rng, double* values,
113  const Bounds& bounds) const
114 {
115  values[0] = rng.uniformReal(bounds[0].min_position_, bounds[0].max_position_);
116 }
117 
118 void RevoluteJointModel::getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator& rng, double* values,
119  const Bounds& bounds, const double* near,
120  const double distance) const
121 {
122  if (continuous_)
123  {
124  values[0] = rng.uniformReal(near[0] - distance, near[0] + distance);
125  enforcePositionBounds(values, bounds);
126  }
127  else
128  values[0] = rng.uniformReal(std::max(bounds[0].min_position_, near[0] - distance),
129  std::min(bounds[0].max_position_, near[0] + distance));
130 }
131 
132 void RevoluteJointModel::interpolate(const double* from, const double* to, const double t, double* state) const
133 {
134  if (continuous_)
135  {
136  double diff = to[0] - from[0];
137  if (fabs(diff) <= M_PI)
138  state[0] = from[0] + diff * t;
139  else
140  {
141  if (diff > 0.0)
142  diff = 2.0 * M_PI - diff;
143  else
144  diff = -2.0 * M_PI - diff;
145  state[0] = from[0] - diff * t;
146  // input states are within bounds, so the following check is sufficient
147  if (state[0] > M_PI)
148  state[0] -= 2.0 * M_PI;
149  else if (state[0] < -M_PI)
150  state[0] += 2.0 * M_PI;
151  }
152  }
153  else
154  state[0] = from[0] + (to[0] - from[0]) * t;
155 }
156 
157 double RevoluteJointModel::distance(const double* values1, const double* values2) const
158 {
159  if (continuous_)
160  {
161  double d = fmod(fabs(values1[0] - values2[0]), 2.0 * M_PI);
162  return (d > M_PI) ? 2.0 * M_PI - d : d;
163  }
164  else
165  return fabs(values1[0] - values2[0]);
166 }
167 
168 bool RevoluteJointModel::satisfiesPositionBounds(const double* values, const Bounds& bounds, double margin) const
169 {
170  if (continuous_)
171  return true;
172  else
173  {
174  return !(values[0] < bounds[0].min_position_ - margin || values[0] > bounds[0].max_position_ + margin);
175  }
176 }
177 
178 bool RevoluteJointModel::harmonizePosition(double* values, const JointModel::Bounds& other_bounds) const
179 {
180  bool modified = false;
181  if (*values < other_bounds[0].min_position_)
182  {
183  while (*values + 2 * M_PI <= other_bounds[0].max_position_)
184  {
185  *values += 2 * M_PI;
186  modified = true;
187  }
188  }
189  else if (*values > other_bounds[0].max_position_)
190  {
191  while (*values - 2 * M_PI >= other_bounds[0].min_position_)
192  {
193  *values -= 2 * M_PI;
194  modified = true;
195  }
196  }
197  return modified;
198 }
199 
200 bool RevoluteJointModel::enforcePositionBounds(double* values, const Bounds& bounds) const
201 {
202  if (continuous_)
203  {
204  double& v = values[0];
205  if (v <= -M_PI || v > M_PI)
206  {
207  v = fmod(v, 2.0 * M_PI);
208  if (v <= -M_PI)
209  v += 2.0 * M_PI;
210  else if (v > M_PI)
211  v -= 2.0 * M_PI;
212  return true;
213  }
214  }
215  else
216  {
217  if (values[0] < bounds[0].min_position_)
218  {
219  values[0] = bounds[0].min_position_;
220  return true;
221  }
222  else if (values[0] > bounds[0].max_position_)
223  {
224  values[0] = bounds[0].max_position_;
225  return true;
226  }
227  }
228  return false;
229 }
230 
231 void RevoluteJointModel::computeTransform(const double* joint_values, Eigen::Isometry3d& transf) const
232 {
233  const double c = cos(joint_values[0]);
234  const double s = sin(joint_values[0]);
235  const double t = 1.0 - c;
236  const double txy = t * xy_;
237  const double txz = t * xz_;
238  const double tyz = t * yz_;
239 
240  const double zs = axis_.z() * s;
241  const double ys = axis_.y() * s;
242  const double xs = axis_.x() * s;
243 
244  // column major
245  double* d = transf.data();
246 
247  d[0] = t * x2_ + c;
248  d[1] = txy + zs;
249  d[2] = txz - ys;
250  d[3] = 0.0;
251 
252  d[4] = txy - zs;
253  d[5] = t * y2_ + c;
254  d[6] = tyz + xs;
255  d[7] = 0.0;
256 
257  d[8] = txz + ys;
258  d[9] = tyz - xs;
259  d[10] = t * z2_ + c;
260  d[11] = 0.0;
261 
262  d[12] = 0.0;
263  d[13] = 0.0;
264  d[14] = 0.0;
265  d[15] = 1.0;
266 
267  // transf = Eigen::Isometry3d(Eigen::AngleAxisd(joint_values[0], axis_));
268 }
269 
270 void RevoluteJointModel::computeVariablePositions(const Eigen::Isometry3d& transf, double* joint_values) const
271 {
272  ASSERT_ISOMETRY(transf) // unsanitized input, could contain a non-isometry
273  Eigen::Quaterniond q(transf.linear());
274  q.normalize();
275  size_t max_idx;
276  axis_.array().abs().maxCoeff(&max_idx);
277  joint_values[0] = 2. * atan2(q.vec()[max_idx] / axis_[max_idx], q.w());
278 }
279 
280 } // end of namespace core
281 } // end of namespace moveit
A joint from the robot. Models the transform that this joint applies in the kinematic chain....
Definition: joint_model.h:117
VariableIndexMap variable_index_map_
Map from variable names to the corresponding index in variable_names_ (indexing makes sense within th...
Definition: joint_model.h:488
JointType type_
The type of joint.
Definition: joint_model.h:473
Bounds variable_bounds_
The bounds for each variable (low, high) in the same order as variable_names_.
Definition: joint_model.h:482
std::vector< VariableBounds > Bounds
The datatype for the joint bounds.
Definition: joint_model.h:131
const std::string & getName() const
Get the name of the joint.
Definition: joint_model.h:145
double getMaximumExtent() const
Definition: joint_model.h:441
std::vector< std::string > variable_names_
The full names to use for the variables that make up this joint.
Definition: joint_model.h:479
void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, double *values, const Bounds &other_bounds, const double *near, const double distance) const override
Provide random values for the joint variables (within specified bounds). Enough memory is assumed to ...
bool continuous_
Flag indicating whether this joint wraps around.
Eigen::Vector3d axis_
The axis of the joint.
void computeVariablePositions(const Eigen::Isometry3d &transf, double *joint_values) const override
Given the transform generated by joint, compute the corresponding joint values. Make sure the passed ...
void computeTransform(const double *joint_values, Eigen::Isometry3d &transf) const override
Given the joint values for a joint, compute the corresponding transform. The computed transform is gu...
unsigned int getStateSpaceDimension() const override
Get the dimension of the state space that corresponds to this joint.
bool harmonizePosition(double *values, const Bounds &other_bounds) const override
void getVariableDefaultPositions(double *values, const Bounds &other_bounds) const override
Provide a default value for the joint given the joint variable bounds. Most joints will use the defau...
EIGEN_MAKE_ALIGNED_OPERATOR_NEW RevoluteJointModel(const std::string &name, size_t joint_index, size_t first_variable_index)
void interpolate(const double *from, const double *to, const double t, double *state) const override
Computes the state that lies at time t in [0, 1] on the segment that connects from state to to state....
double distance(const double *values1, const double *values2) const override
Compute the distance between two joint states of the same model (represented by the variable values)
void setAxis(const Eigen::Vector3d &axis)
Set the axis of rotation.
void getVariableRandomPositions(random_numbers::RandomNumberGenerator &rng, double *values, const Bounds &other_bounds) const override
Provide random values for the joint variables (within specified bounds). Enough memory is assumed to ...
bool enforcePositionBounds(double *values, const Bounds &other_bounds) const override
Force the specified values to be inside bounds and normalized. Quaternions are normalized,...
bool satisfiesPositionBounds(const double *values, const Bounds &other_bounds, double margin) const override
Check if the set of position values for the variables of this joint are within bounds,...
Vec3fX< details::Vec3Data< double > > Vector3d
Definition: fcl_compat.h:89
Main namespace for MoveIt.
Definition: exceptions.h:43
double distance(const urdf::Pose &transform)
Definition: pr2_arm_ik.h:55
d
Definition: setup.py:4
name
Definition: setup.py:7