Robowflex  v0.1
Making MoveIt Easy
tf.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston, Constantinos Chamzas */
2 
5 #include <robowflex_library/tf.h>
6 
7 using namespace robowflex;
8 
10 {
11  return RobotPose::Identity();
12 }
13 
14 RobotPose TF::createPoseXYZ(double x, double y, double z)
15 {
16  return createPoseXYZ(x, y, z, 0, 0, 0);
17 }
18 
19 RobotPose TF::createPoseXYZ(const Eigen::Ref<const Eigen::Vector3d> &translation)
20 {
21  RobotPose pose = RobotPose::Identity();
22  pose.translation() = translation;
23 
24  return pose;
25 }
26 
27 RobotPose TF::createPoseXYZ(double x, double y, double z, double X, double Y, double Z)
28 {
29  return createPoseXYZ(Eigen::Vector3d{x, y, z}, //
30  Eigen::Vector3d{X, Y, Z});
31 }
32 
33 RobotPose TF::createPoseXYZ(const Eigen::Ref<const Eigen::Vector3d> &translation,
34  const Eigen::Ref<const Eigen::Vector3d> &rotation)
35 {
36  RobotPose pose = RobotPose::Identity();
37  pose.translation() = translation;
38 
39  pose.linear() = (Eigen::AngleAxisd(rotation[0], Eigen::Vector3d::UnitX()) *
40  Eigen::AngleAxisd(rotation[1], Eigen::Vector3d::UnitY()) *
41  Eigen::AngleAxisd(rotation[2], Eigen::Vector3d::UnitZ()))
42  .toRotationMatrix();
43 
44  return pose;
45 }
46 
47 RobotPose TF::createPoseQ(double x, double y, double z, double W, double X, double Y, double Z)
48 {
49  return createPoseQ(Eigen::Vector3d{x, y, z}, //
50  Eigen::Vector4d{W, X, Y, Z});
51 }
52 
53 RobotPose TF::createPoseQ(const Eigen::Ref<const Eigen::Vector3d> &translation,
54  const Eigen::Ref<const Eigen::Vector4d> &rotation)
55 {
56  return createPoseQ(translation, Eigen::Quaterniond(rotation));
57 }
58 
59 RobotPose TF::createPoseQ(const Eigen::Ref<const Eigen::Vector3d> &translation,
60  const Eigen::Quaterniond &rotation)
61 {
62  RobotPose pose = RobotPose::Identity();
63  pose.translation() = translation;
64  pose.linear() = rotation.toRotationMatrix();
65 
66  return pose;
67 }
68 
69 Eigen::Quaterniond TF::getPoseRotation(const RobotPose &pose)
70 {
71  return Eigen::Quaterniond(pose.rotation());
72 }
73 
74 Eigen::Vector3d TF::pointMsgToEigen(const geometry_msgs::Point &msg)
75 {
76  Eigen::Vector3d vector;
77  vector[0] = msg.x;
78  vector[1] = msg.y;
79  vector[2] = msg.z;
80 
81  return vector;
82 }
83 
84 geometry_msgs::Point TF::pointEigenToMsg(const Eigen::Vector3d &vector)
85 {
86  geometry_msgs::Point msg;
87  msg.x = vector[0];
88  msg.y = vector[1];
89  msg.z = vector[2];
90 
91  return msg;
92 }
93 
94 Eigen::Vector3d TF::vectorMsgToEigen(const geometry_msgs::Vector3 &msg)
95 {
96  Eigen::Vector3d vector;
97  vector[0] = msg.x;
98  vector[1] = msg.y;
99  vector[2] = msg.z;
100 
101  return vector;
102 }
103 
104 geometry_msgs::Vector3 TF::vectorEigenToMsg(const Eigen::Vector3d &vector)
105 {
106  geometry_msgs::Vector3 msg;
107  msg.x = vector[0];
108  msg.y = vector[1];
109  msg.z = vector[2];
110 
111  return msg;
112 }
113 
114 RobotPose TF::poseMsgToEigen(const geometry_msgs::Pose &msg)
115 {
116  return RobotPose(Eigen::Translation3d(msg.position.x, msg.position.y, msg.position.z) *
117  quaternionMsgToEigen(msg.orientation));
118 }
119 
120 geometry_msgs::Pose TF::poseEigenToMsg(const RobotPose &pose)
121 {
122  geometry_msgs::Pose msg;
123 
124  const auto &t = pose.translation();
125  msg.position.x = t.x();
126  msg.position.y = t.y();
127  msg.position.z = t.z();
128 
129  const auto &r = getPoseRotation(pose);
130  msg.orientation = quaternionEigenToMsg(r);
131 
132  return msg;
133 }
134 
135 Eigen::Quaterniond TF::quaternionMsgToEigen(const geometry_msgs::Quaternion &msg)
136 {
137  return Eigen::Quaterniond(msg.w, msg.x, msg.y, msg.z);
138 }
139 
140 geometry_msgs::Quaternion TF::quaternionEigenToMsg(const Eigen::Quaterniond &quaternion)
141 {
142  geometry_msgs::Quaternion msg;
143  msg.w = quaternion.w();
144  msg.x = quaternion.x();
145  msg.y = quaternion.y();
146  msg.z = quaternion.z();
147 
148  return msg;
149 }
150 
151 moveit_msgs::BoundingVolume TF::getBoundingVolume(const RobotPose &pose, const GeometryConstPtr &geometry)
152 {
153  moveit_msgs::BoundingVolume bv;
154 
155  if (geometry->isMesh())
156  {
157  bv.meshes.push_back(geometry->getMeshMsg());
158  bv.mesh_poses.push_back(TF::poseEigenToMsg(pose));
159  }
160  else
161  {
162  bv.primitives.push_back(geometry->getSolidMsg());
163  bv.primitive_poses.push_back(TF::poseEigenToMsg(pose));
164  }
165 
166  return bv;
167 }
168 
169 moveit_msgs::PositionConstraint TF::getPositionConstraint(const std::string &ee_name,
170  const std::string &base_name, const RobotPose &pose,
171  const GeometryConstPtr &geometry)
172 {
173  moveit_msgs::PositionConstraint constraint;
174 
175  constraint.header.frame_id = base_name;
176  constraint.link_name = ee_name;
177 
178  // TODO: Expose as a parameter
179  constraint.target_point_offset.x = 0;
180  constraint.target_point_offset.y = 0;
181  constraint.target_point_offset.z = 0;
182 
183  constraint.constraint_region = getBoundingVolume(pose, geometry);
184  constraint.weight = 1;
185 
186  return constraint;
187 }
188 
189 moveit_msgs::OrientationConstraint TF::getOrientationConstraint(const std::string &ee_name,
190  const std::string &base_name,
191  const Eigen::Quaterniond &orientation,
192  const Eigen::Vector3d &tolerances)
193 {
194  moveit_msgs::OrientationConstraint constraint;
195 
196  constraint.header.frame_id = base_name;
197  constraint.link_name = ee_name;
198  constraint.absolute_x_axis_tolerance = tolerances[0];
199  constraint.absolute_y_axis_tolerance = tolerances[1];
200  constraint.absolute_z_axis_tolerance = tolerances[2];
201  constraint.orientation = TF::quaternionEigenToMsg(orientation);
202  constraint.weight = 1;
203 
204  return constraint;
205 }
206 
207 Eigen::Quaterniond TF::sampleOrientation(const Eigen::Quaterniond &orientation,
208  const Eigen::Vector3d &tolerances)
209 {
210  const auto vec = RNG::uniformVec(tolerances);
211  Eigen::Quaterniond sampled = Eigen::AngleAxisd(vec[0], Eigen::Vector3d::UnitX()) //
212  * Eigen::AngleAxisd(vec[1], Eigen::Vector3d::UnitY()) //
213  * Eigen::AngleAxisd(vec[2], Eigen::Vector3d::UnitZ());
214 
215  return orientation * sampled;
216 }
217 
218 Eigen::Quaterniond TF::sampleOrientationUniform(const Eigen::Vector3d &tolerances)
219 {
220  const auto vec = RNG::uniformRPY(tolerances);
221  Eigen::Quaterniond sampled = Eigen::AngleAxisd(vec[0], Eigen::Vector3d::UnitX()) //
222  * Eigen::AngleAxisd(vec[1], Eigen::Vector3d::UnitY()) //
223  * Eigen::AngleAxisd(vec[2], Eigen::Vector3d::UnitZ());
224 
225  return sampled;
226 }
227 
228 Eigen::Quaterniond TF::offsetOrientation(const Eigen::Quaterniond &orientation, const Eigen::Vector3d &axis,
229  double value)
230 {
231  return Eigen::AngleAxisd(value, axis) * orientation;
232 }
233 
234 Eigen::Vector3d TF::samplePositionUniform(const Eigen::Vector3d &bounds)
235 {
236  return RNG::uniformVec(bounds);
237 }
238 
239 Eigen::Vector3d TF::samplePositionGaussian(const Eigen::Vector3d &stddev)
240 {
241  return RNG::gaussianVec(stddev);
242 }
243 
244 RobotPose TF::samplePoseUniform(const Eigen::Vector3d &pos_bounds, const Eigen::Vector3d &orn_bounds)
245 {
246  auto sampled = RobotPose::Identity();
247  sampled.translation() = samplePositionUniform(pos_bounds);
248  sampled.linear() = sampleOrientationUniform(orn_bounds).toRotationMatrix();
249 
250  return sampled;
251 }
252 
253 RobotPose TF::samplePoseGaussian(const Eigen::Vector3d &pos_stddev, const Eigen::Vector3d &orn_bounds)
254 {
255  auto sampled = RobotPose::Identity();
256  sampled.translation() = samplePositionGaussian(pos_stddev);
257  sampled.linear() = sampleOrientationUniform(orn_bounds).toRotationMatrix();
258 
259  return sampled;
260 }
261 
262 geometry_msgs::TransformStamped TF::transformEigenToMsg(const std::string &source, const std::string &target,
263  const RobotPose &tf)
264 {
265  geometry_msgs::TransformStamped msg;
266 
267  msg.header.stamp = ros::Time::now();
268  msg.header.frame_id = source;
269  msg.child_frame_id = target;
270 
271  const auto &t = tf.translation();
272  msg.transform.translation.x = t.x();
273  msg.transform.translation.y = t.y();
274  msg.transform.translation.z = t.z();
275 
276  const auto &r = TF::getPoseRotation(tf);
277  msg.transform.rotation = quaternionEigenToMsg(r);
278 
279  return msg;
280 }
281 
282 RobotPose TF::transformMsgToEigen(const geometry_msgs::TransformStamped &tf)
283 {
284  RobotPose pose;
285  pose.translation().x() = tf.transform.translation.x;
286  pose.translation().y() = tf.transform.translation.y;
287  pose.translation().z() = tf.transform.translation.z;
288 
289  pose.linear() = quaternionMsgToEigen(tf.transform.rotation).toRotationMatrix();
290  return pose;
291 }
292 
293 double TF::angleNormalize(double v)
294 {
295  return (v > constants::pi) ? constants::two_pi - v : v;
296 }
297 
298 double TF::toDegrees(double v)
299 {
300  double n = angleNormalize(v);
301  double d = n * 180. / constants::pi;
302  if (n >= 0)
303  return d;
304 
305  return 360. + d;
306 }
307 
308 double TF::toRadians(double v)
309 {
310  if (v < 0)
311  v += 360.;
312  if (v >= 360.)
313  v -= 360.;
314 
315  if (v <= 180.)
316  return v * constants::pi / 180.;
317 
318  return -(360. - v) * constants::pi / 180.;
319 }
320 
321 bool TF::isVecZero(const Eigen::Ref<const Eigen::VectorXd> &v, double tolerance)
322 {
323  return v.norm() < tolerance;
324 }
A const shared pointer wrapper for robowflex::Geometry.
Eigen::Vector3d uniformRPY(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
Uniform random sampling of Euler roll-pitch-yaw angles within lower bound lbound and upper bound ubou...
Definition: random.cpp:57
Eigen::Vector3d uniformVec(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
Generate a uniform real vector within given bounds: [lower_bound, upper_bound)
Definition: random.cpp:88
Eigen::Vector3d gaussianVec(const Eigen::Vector3d &mean, const Eigen::Vector3d &stddev)
Generate a random real vector using a normal distribution with given mean and standard deviation.
Definition: random.cpp:103
moveit_msgs::OrientationConstraint getOrientationConstraint(const std::string &ee_name, const std::string &base_name, const Eigen::Quaterniond &orientation, const Eigen::Vector3d &tolerances)
Get an orientation constraint message.
Definition: tf.cpp:189
geometry_msgs::Vector3 vectorEigenToMsg(const Eigen::Vector3d &vector)
Converts an Eigen::Vector3d to a vector message.
Definition: tf.cpp:104
Eigen::Vector3d vectorMsgToEigen(const geometry_msgs::Vector3 &msg)
Converts a vector message to an Eigen::Vector3d.
Definition: tf.cpp:94
double toDegrees(double v)
Convert an angle to degrees.
Definition: tf.cpp:298
geometry_msgs::TransformStamped transformEigenToMsg(const std::string &source, const std::string &target, const RobotPose &tf)
Encode a transform as a message.
Definition: tf.cpp:262
RobotPose poseMsgToEigen(const geometry_msgs::Pose &msg)
Converts a pose message to RobotPose.
Definition: tf.cpp:114
RobotPose createPoseQ(double x, double y, double z, double W, double X, double Y, double Z)
Creates a robot pose from a linear component and a Quaternion.
Definition: tf.cpp:47
double toRadians(double v)
Convert an angle to radians.
Definition: tf.cpp:308
RobotPose createPoseXYZ(double x, double y, double z)
Creates a robot pose from a linear component and zero orientation.
Definition: tf.cpp:14
Eigen::Vector3d samplePositionGaussian(const Eigen::Vector3d &stddev)
Sample a position from a gaussian distribution with mean zero and given standard deviation.
Definition: tf.cpp:239
Eigen::Quaterniond sampleOrientation(const Eigen::Quaterniond &orientation, const Eigen::Vector3d &tolerances)
Sample an orientation from a given orientation with XYZ Euler angle tolerances.
Definition: tf.cpp:207
Eigen::Vector3d pointMsgToEigen(const geometry_msgs::Point &msg)
Converts a point message to an Eigen::Vector3d.
Definition: tf.cpp:74
geometry_msgs::Pose poseEigenToMsg(const RobotPose &pose)
Converts an RobotPose to a pose message.
Definition: tf.cpp:120
RobotPose samplePoseUniform(const Eigen::Vector3d &pos_bounds, const Eigen::Vector3d &orn_bounds)
Sample a pose within the given position, orientation bounds.
Definition: tf.cpp:244
double angleNormalize(double v)
Normalize an angle between -pi to pi.
Definition: tf.cpp:293
Eigen::Vector3d samplePositionUniform(const Eigen::Vector3d &bounds)
Sample a position within the given bounds using a uniform distribution.
Definition: tf.cpp:234
Eigen::Quaterniond quaternionMsgToEigen(const geometry_msgs::Quaternion &msg)
Converts a quaternion message to Eigen::Quaterniond.
Definition: tf.cpp:135
geometry_msgs::Point pointEigenToMsg(const Eigen::Vector3d &vector)
Converts an Eigen::Vector3d to a point message.
Definition: tf.cpp:84
Eigen::Quaterniond offsetOrientation(const Eigen::Quaterniond &orientation, const Eigen::Vector3d &axis, double value)
Offset an orientation by a rotation about an axis.
Definition: tf.cpp:228
moveit_msgs::BoundingVolume getBoundingVolume(const RobotPose &pose, const GeometryConstPtr &geometry)
Get a bounding volume message for given geometry at a pose.
Definition: tf.cpp:151
Eigen::Quaterniond sampleOrientationUniform(const Eigen::Vector3d &tolerances)
Sample an orientation within the XYZ Euler angle tolerances.
Definition: tf.cpp:218
RobotPose identity()
Creates the Identity pose.
Definition: tf.cpp:9
moveit_msgs::PositionConstraint getPositionConstraint(const std::string &ee_name, const std::string &base_name, const RobotPose &pose, const GeometryConstPtr &geometry)
Get a position constraint message.
Definition: tf.cpp:169
RobotPose samplePoseGaussian(const Eigen::Vector3d &pos_stddev, const Eigen::Vector3d &orn_bounds)
Sample a pose with gaussian sampling for the position with given standard deviations and uniform samp...
Definition: tf.cpp:253
bool isVecZero(const Eigen::Ref< const Eigen::VectorXd > &v, double tolerance=constants::eps)
Checks if a vector is close to zero.
Definition: tf.cpp:321
geometry_msgs::Quaternion quaternionEigenToMsg(const Eigen::Quaterniond &quaternion)
Converts an Eigen::Quaterniond to a quaternion message.
Definition: tf.cpp:140
RobotPose transformMsgToEigen(const geometry_msgs::TransformStamped &tf)
Decode a message as a transform.
Definition: tf.cpp:282
Eigen::Quaterniond getPoseRotation(const RobotPose &pose)
Get the rotational component of a robot pose.
Definition: tf.cpp:69
static const double two_pi
Definition: constants.h:24
static const double pi
Definition: constants.h:21
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Definition: adapter.h:24