frame_tf.h
Go to the documentation of this file.
1 
11 /*
12  * Copyright 2016,2017 Vladimir Ermakov.
13  * Copyright 2017,2018 Nuno Marques.
14  *
15  * This file is part of the mavros package and subject to the license terms
16  * in the top-level LICENSE file of the mavros repository.
17  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
18  */
19 
20 #pragma once
21 
22 #include <array>
23 #include <Eigen/Eigen>
24 #include <Eigen/Geometry>
25 #include <ros/assert.h>
26 
27 // for Covariance types
28 #include <sensor_msgs/Imu.h>
29 #include <geometry_msgs/Point.h>
30 #include <geometry_msgs/Vector3.h>
31 #include <geometry_msgs/Quaternion.h>
32 #include <geometry_msgs/PoseWithCovariance.h>
33 
34 namespace mavros {
35 namespace ftf {
37 using Covariance3d = sensor_msgs::Imu::_angular_velocity_covariance_type;
38 
40 using Covariance6d = geometry_msgs::PoseWithCovariance::_covariance_type;
41 
43 using Covariance9d = boost::array<double, 81>;
44 
46 using EigenMapCovariance3d = Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor> >;
47 using EigenMapConstCovariance3d = Eigen::Map<const Eigen::Matrix<double, 3, 3, Eigen::RowMajor> >;
48 
50 using EigenMapCovariance6d = Eigen::Map<Eigen::Matrix<double, 6, 6, Eigen::RowMajor> >;
51 using EigenMapConstCovariance6d = Eigen::Map<const Eigen::Matrix<double, 6, 6, Eigen::RowMajor> >;
52 
54 using EigenMapCovariance9d = Eigen::Map<Eigen::Matrix<double, 9, 9, Eigen::RowMajor> >;
55 using EigenMapConstCovariance9d = Eigen::Map<const Eigen::Matrix<double, 9, 9, Eigen::RowMajor> >;
56 
60 enum class StaticTF {
61  NED_TO_ENU,
62  ENU_TO_NED,
65  ECEF_TO_ENU,
67 };
68 
69 namespace detail {
77 Eigen::Quaterniond transform_orientation(const Eigen::Quaterniond &q, const StaticTF transform);
78 
84 Eigen::Vector3d transform_frame(const Eigen::Vector3d &vec, const Eigen::Quaterniond &q);
85 
91 Covariance3d transform_frame(const Covariance3d &cov, const Eigen::Quaterniond &q);
92 
98 Covariance6d transform_frame(const Covariance6d &cov, const Eigen::Quaterniond &q);
99 
105 Covariance9d transform_frame(const Covariance9d &cov, const Eigen::Quaterniond &q);
106 
112 Eigen::Vector3d transform_static_frame(const Eigen::Vector3d &vec, const StaticTF transform);
113 
119 Covariance3d transform_static_frame(const Covariance3d &cov, const StaticTF transform);
120 
126 Covariance6d transform_static_frame(const Covariance6d &cov, const StaticTF transform);
127 
133 Covariance9d transform_static_frame(const Covariance9d &cov, const StaticTF transform);
134 
141 Eigen::Vector3d transform_static_frame(const Eigen::Vector3d &vec, const Eigen::Vector3d &map_origin, const StaticTF transform);
142 
143 } // namespace detail
144 
145 // -*- frame tf -*-
146 
151 template<class T>
152 inline T transform_orientation_ned_enu(const T &in) {
154 }
155 
160 template<class T>
161 inline T transform_orientation_enu_ned(const T &in) {
163 }
164 
169 template<class T>
172 }
173 
178 template<class T>
181 }
182 
186 template<class T>
187 inline T transform_frame_ned_enu(const T &in) {
189 }
190 
195 template<class T>
196 inline T transform_frame_enu_ned(const T &in) {
198 }
199 
204 template<class T>
205 inline T transform_frame_aircraft_baselink(const T &in) {
207 }
208 
213 template<class T>
214 inline T transform_frame_baselink_aircraft(const T &in) {
216 }
217 
225 template<class T>
226 inline T transform_frame_ecef_enu(const T &in, const T &map_origin) {
228 }
229 
237 template<class T>
238 inline T transform_frame_enu_ecef(const T &in, const T &map_origin) {
240 }
241 
246 template<class T>
247 inline T transform_frame_aircraft_ned(const T &in, const Eigen::Quaterniond &q) {
248  return detail::transform_frame(in, q);
249 }
250 
255 template<class T>
256 inline T transform_frame_ned_aircraft(const T &in, const Eigen::Quaterniond &q) {
257  return detail::transform_frame(in, q);
258 }
259 
264 template<class T>
265 inline T transform_frame_aircraft_enu(const T &in, const Eigen::Quaterniond &q) {
266  return detail::transform_frame(in, q);
267 }
268 
273 template<class T>
274 inline T transform_frame_enu_aircraft(const T &in, const Eigen::Quaterniond &q) {
275  return detail::transform_frame(in, q);
276 }
277 
282 template<class T>
283 inline T transform_frame_enu_baselink(const T &in, const Eigen::Quaterniond &q) {
284  return detail::transform_frame(in, q);
285 }
286 
291 template<class T>
292 inline T transform_frame_baselink_enu(const T &in, const Eigen::Quaterniond &q) {
293  return detail::transform_frame(in, q);
294 }
295 
296 // -*- utils -*-
297 
298 
302 Eigen::Quaterniond quaternion_from_rpy(const Eigen::Vector3d &rpy);
303 
309 inline Eigen::Quaterniond quaternion_from_rpy(const double roll, const double pitch, const double yaw) {
310  return quaternion_from_rpy(Eigen::Vector3d(roll, pitch, yaw));
311 }
312 
318 Eigen::Vector3d quaternion_to_rpy(const Eigen::Quaterniond &q);
319 
323 inline void quaternion_to_rpy(const Eigen::Quaterniond &q, double &roll, double &pitch, double &yaw)
324 {
325  const auto rpy = quaternion_to_rpy(q);
326  roll = rpy.x();
327  pitch = rpy.y();
328  yaw = rpy.z();
329 }
330 
336 double quaternion_get_yaw(const Eigen::Quaterniond &q);
337 
344 inline void quaternion_to_mavlink(const Eigen::Quaterniond &q, std::array<float, 4> &qmsg)
345 {
346  qmsg[0] = q.w();
347  qmsg[1] = q.x();
348  qmsg[2] = q.y();
349  qmsg[3] = q.z();
350 }
351 
355 inline Eigen::Quaterniond mavlink_to_quaternion(const std::array<float, 4> &q)
356 {
357  return Eigen::Quaterniond(q[0], q[1], q[2], q[3]);
358 }
359 
363 template<class T, std::size_t SIZE>
364 inline void covariance_to_mavlink(const T &cov, std::array<float, SIZE> &covmsg)
365 {
366  std::copy(cov.cbegin(), cov.cend(), covmsg.begin());
367 }
368 
372 template<class T, std::size_t ARR_SIZE>
373 inline void covariance_urt_to_mavlink(const T &covmap, std::array<float, ARR_SIZE> &covmsg)
374 {
375  auto m = covmap;
376  std::size_t COV_SIZE = m.rows() * (m.rows() + 1) / 2;
377  ROS_ASSERT_MSG(COV_SIZE == ARR_SIZE,
378  "frame_tf: covariance matrix URT size (%lu) is different from Mavlink msg covariance field size (%lu)",
379  COV_SIZE, ARR_SIZE);
380 
381  auto out = covmsg.begin();
382 
383  for (size_t x = 0; x < m.cols(); x++) {
384  for (size_t y = x; y < m.rows(); y++)
385  *out++ = m(y, x);
386  }
387 }
388 
393 template<class T, std::size_t ARR_SIZE>
394 inline void mavlink_urt_to_covariance_matrix(const std::array<float, ARR_SIZE> &covmsg, T &covmat)
395 {
396  std::size_t COV_SIZE = covmat.rows() * (covmat.rows() + 1) / 2;
397  ROS_ASSERT_MSG(COV_SIZE == ARR_SIZE,
398  "frame_tf: covariance matrix URT size (%lu) is different from Mavlink msg covariance field size (%lu)",
399  COV_SIZE, ARR_SIZE);
400 
401  auto in = covmsg.begin();
402 
403  for (size_t x = 0; x < covmat.cols(); x++) {
404  for (size_t y = x; y < covmat.rows(); y++) {
405  covmat(x, y) = static_cast<double>(*in++);
406  covmat(y, x) = covmat(x, y);
407  }
408  }
409 }
410 
411 // [[[cog:
412 // def make_to_eigen(te, tr, fields):
413 // cog.outl("""//! @brief Helper to convert common ROS geometry_msgs::{tr} to Eigen::{te}""".format(**locals()))
414 // cog.outl("""inline Eigen::{te} to_eigen(const geometry_msgs::{tr} r) {{""".format(**locals()))
415 // cog.outl("""\treturn Eigen::{te}({fl});""".format(te=te, fl=", ".join(["r." + f for f in fields])))
416 // cog.outl("""}""")
417 //
418 // make_to_eigen("Vector3d", "Point", "xyz")
419 // make_to_eigen("Vector3d", "Vector3", "xyz")
420 // make_to_eigen("Quaterniond", "Quaternion", "wxyz")
421 // ]]]
423 inline Eigen::Vector3d to_eigen(const geometry_msgs::Point r) {
424  return Eigen::Vector3d(r.x, r.y, r.z);
425 }
427 inline Eigen::Vector3d to_eigen(const geometry_msgs::Vector3 r) {
428  return Eigen::Vector3d(r.x, r.y, r.z);
429 }
431 inline Eigen::Quaterniond to_eigen(const geometry_msgs::Quaternion r) {
432  return Eigen::Quaterniond(r.w, r.x, r.y, r.z);
433 }
434 // [[[end]]] (checksum: 1b3ada1c4245d4e31dcae9768779b952)
435 } // namespace ftf
436 } // namespace mavros
will change orientation from being expressed WRT NED frame to WRT ENU frame
Eigen::Quaterniond mavlink_to_quaternion(const std::array< float, 4 > &q)
Convert Mavlink float[4] quaternion to Eigen.
Definition: frame_tf.h:355
StaticTF
Orientation transform options when applying rotations to data.
Definition: frame_tf.h:60
Eigen::Map< Eigen::Matrix< double, 6, 6, Eigen::RowMajor > > EigenMapCovariance6d
Eigen::Map for Covariance6d.
Definition: frame_tf.h:50
change from expressed WRT ENU frame to WRT ECEF frame
Eigen::Map< const Eigen::Matrix< double, 3, 3, Eigen::RowMajor > > EigenMapConstCovariance3d
Definition: frame_tf.h:47
Eigen::Quaterniond transform_orientation(const Eigen::Quaterniond &q, const StaticTF transform)
Transform representation of attitude from 1 frame to another (e.g. transfrom attitude from representi...
T transform_orientation_enu_ned(const T &in)
Transform from attitude represented WRT ENU frame to attitude represented WRT NED frame...
Definition: frame_tf.h:161
void mavlink_urt_to_covariance_matrix(const std::array< float, ARR_SIZE > &covmsg, T &covmat)
Convert MAVLink float[n] format (upper right triangular of a covariance matrix) to Eigen::MatrixXd<n...
Definition: frame_tf.h:394
Eigen::Vector3d transform_static_frame(const Eigen::Vector3d &vec, const StaticTF transform)
Transform data expressed in one frame to another frame.
T transform_frame_enu_aircraft(const T &in, const Eigen::Quaterniond &q)
Transform data expressed in ENU to aircraft frame. Assumes quaternion represents rotation from ENU to...
Definition: frame_tf.h:274
Eigen::Vector3d transform_frame(const Eigen::Vector3d &vec, const Eigen::Quaterniond &q)
Transform data expressed in one frame to another frame.
T transform_frame_enu_baselink(const T &in, const Eigen::Quaterniond &q)
Transform data expressed in ENU to base_link frame. Assumes quaternion represents rotation from ENU t...
Definition: frame_tf.h:283
change from expressed WRT ECEF frame to WRT ENU frame
T transform_frame_aircraft_baselink(const T &in)
Transform data expressed in Aircraft frame to Baselink frame.
Definition: frame_tf.h:205
change from expressed WRT aircraft frame to WRT to baselink frame
Eigen::Vector3d quaternion_to_rpy(const Eigen::Quaterniond &q)
Convert quaternion to euler angles.
#define ROS_ASSERT_MSG(cond,...)
T transform_frame_ned_aircraft(const T &in, const Eigen::Quaterniond &q)
Transform data expressed in NED to aircraft frame. Assumes quaternion represents rotation from NED to...
Definition: frame_tf.h:256
Eigen::Vector3d transform_static_frame(const Eigen::Vector3d &vec, const Eigen::Vector3d &map_origin, const StaticTF transform)
Transform data expressed in one frame to another frame with additional map origin parameter...
Eigen::Map< Eigen::Matrix< double, 9, 9, Eigen::RowMajor > > EigenMapCovariance9d
Eigen::Map for Covariance9d.
Definition: frame_tf.h:54
T transform_frame_aircraft_enu(const T &in, const Eigen::Quaterniond &q)
Transform data expressed in aircraft frame to ENU frame. Assumes quaternion represents rotation from ...
Definition: frame_tf.h:265
T transform_frame_ned_enu(const T &in)
Transform data expressed in NED to ENU frame.
Definition: frame_tf.h:187
Eigen::Vector3d to_eigen(const geometry_msgs::Point r)
Helper to convert common ROS geometry_msgs::Point to Eigen::Vector3d.
Definition: frame_tf.h:423
void covariance_urt_to_mavlink(const T &covmap, std::array< float, ARR_SIZE > &covmsg)
Convert upper right triangular of a covariance matrix to MAVLink float[n] format. ...
Definition: frame_tf.h:373
geometry_msgs::PoseWithCovariance::_covariance_type Covariance6d
Type matching rosmsg for 6x6 covariance matrix.
Definition: frame_tf.h:40
T transform_frame_baselink_aircraft(const T &in)
Transform data expressed in Baselink frame to Aircraft frame.
Definition: frame_tf.h:214
boost::array< double, 81 > Covariance9d
Type matching rosmsg for 9x9 covariance matrix.
Definition: frame_tf.h:43
Eigen::Map< Eigen::Matrix< double, 3, 3, Eigen::RowMajor > > EigenMapCovariance3d
Eigen::Map for Covariance3d.
Definition: frame_tf.h:46
T transform_frame_enu_ecef(const T &in, const T &map_origin)
Transform data expressed in ENU frame to ECEF frame.
Definition: frame_tf.h:238
T transform_frame_enu_ned(const T &in)
Transform data expressed in ENU to NED frame.
Definition: frame_tf.h:196
sensor_msgs::Imu::_angular_velocity_covariance_type Covariance3d
Type matching rosmsg for 3x3 covariance matrix.
Definition: frame_tf.h:37
Eigen::Quaterniond quaternion_from_rpy(const Eigen::Vector3d &rpy)
Convert euler angles to quaternion.
change from expressed WRT baselnk to WRT aircraft
Covariance9d transform_frame(const Covariance9d &cov, const Eigen::Quaterniond &q)
Transform 9x9 convariance expressed in one frame to another.
double quaternion_get_yaw(const Eigen::Quaterniond &q)
Get Yaw angle from quaternion.
T transform_frame_ecef_enu(const T &in, const T &map_origin)
Transform data expressed in ECEF frame to ENU frame.
Definition: frame_tf.h:226
T transform_frame_aircraft_ned(const T &in, const Eigen::Quaterniond &q)
Transform data expressed in aircraft frame to NED frame. Assumes quaternion represents rotation from ...
Definition: frame_tf.h:247
T transform_orientation_ned_enu(const T &in)
Transform from attitude represented WRT NED frame to attitude represented WRT ENU frame...
Definition: frame_tf.h:152
void quaternion_to_mavlink(const Eigen::Quaterniond &q, std::array< float, 4 > &qmsg)
Store Quaternion to MAVLink float[4] format.
Definition: frame_tf.h:344
T transform_orientation_baselink_aircraft(const T &in)
Transform from attitude represented WRT baselink frame to attitude represented WRT body frame...
Definition: frame_tf.h:179
void covariance_to_mavlink(const T &cov, std::array< float, SIZE > &covmsg)
Convert covariance matrix to MAVLink float[n] format.
Definition: frame_tf.h:364
T transform_orientation_aircraft_baselink(const T &in)
Transform from attitude represented WRT aircraft frame to attitude represented WRT base_link frame...
Definition: frame_tf.h:170
Eigen::Map< const Eigen::Matrix< double, 9, 9, Eigen::RowMajor > > EigenMapConstCovariance9d
Definition: frame_tf.h:55
Eigen::Map< const Eigen::Matrix< double, 6, 6, Eigen::RowMajor > > EigenMapConstCovariance6d
Definition: frame_tf.h:51
change from expressed WRT ENU frame to WRT NED frame
T transform_frame_baselink_enu(const T &in, const Eigen::Quaterniond &q)
Transform data expressed in baselink to ENU frame. Assumes quaternion represents rotation from basel_...
Definition: frame_tf.h:292


mavros
Author(s): Vladimir Ermakov
autogenerated on Mon Jul 8 2019 03:20:10