mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
attitude_converter.h
Go to the documentation of this file.
1 // clang: TomasFormat
12 #ifndef ATTITUDE_CONVERTER_H
13 #define ATTITUDE_CONVERTER_H
14 
15 #include <vector>
16 #include <cmath>
17 #include <Eigen/Dense>
18 #include <tuple>
19 
20 #include <tf2_ros/transform_listener.h>
21 #include <tf2_ros/buffer.h>
22 #include <tf2_eigen/tf2_eigen.h>
23 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
24 #include <tf/transform_datatypes.h>
25 #include <tf_conversions/tf_eigen.h>
26 
27 #include <mrs_lib/geometry/misc.h>
28 
29 namespace mrs_lib
30 {
31 
32 // type of the object we are grasping
33 typedef enum
34 {
35 
36  RPY_INTRINSIC = 1,
37  RPY_EXTRINSIC = 2,
38 
39 } RPY_convention_t;
40 
41 /* class EulerAttitude //{ */
42 
47 public:
55  EulerAttitude(const double& roll, const double& pitch, const double& yaw);
56 
62  double roll(void) const;
63 
69  double pitch(void) const;
70 
76  double yaw(void) const;
77 
78 private:
79  double roll_, pitch_, yaw_;
80 };
81 
82 //}
83 
84 /* class Vector3Converter //{ */
85 
91 public:
97  Vector3Converter(const tf2::Vector3& vector3) : vector3_(vector3){};
98 
104  Vector3Converter(const Eigen::Vector3d& vector3);
105 
111  Vector3Converter(const geometry_msgs::Vector3& vector3);
112 
120  Vector3Converter(const double& x, const double& y, const double& z);
121 
127  operator tf2::Vector3() const;
128 
134  operator Eigen::Vector3d() const;
135 
141  operator geometry_msgs::Vector3() const;
142 
143 private:
144  tf2::Vector3 vector3_;
145 };
146 
147 //}
148 
154 public:
155  /* exceptions //{ */
156 
158  struct GetHeadingException : public std::exception
159  {
160  const char* what() const throw() {
161  return "AttitudeConverter: can not calculate the heading, the rotated x-axis is parallel to the world's z-axis";
162  }
163  };
164 
166  struct MathErrorException : public std::exception
167  {
168  const char* what() const throw() {
169  return "AttitudeConverter: math error";
170  }
171  };
172 
174  struct InvalidAttitudeException : public std::exception
175  {
176  const char* what() const throw() {
177  return "AttitudeConverter: invalid attitude, the input probably constains NaNs";
178  }
179  };
180 
182  struct EulerFormatException : public std::exception
183  {
184  const char* what() const throw() {
185  return "AttitudeConverter: invalid Euler angle format";
186  }
187  };
188 
190  struct SetHeadingException : public std::exception
191  {
192  const char* what() const throw() {
193  return "AttitudeConverter: cannot set the desired heading, the thrust vector's Z component is 0";
194  }
195  };
196 
197  //}
198 
199  /* constructors //{ */
200 
209  AttitudeConverter(const double& roll, const double& pitch, const double& yaw, const RPY_convention_t& format = RPY_EXTRINSIC);
210 
216  AttitudeConverter(const tf::Quaternion quaternion);
217 
223  AttitudeConverter(const geometry_msgs::Quaternion quaternion);
224 
230  AttitudeConverter(const mrs_lib::EulerAttitude& euler_attitude);
231 
237  AttitudeConverter(const Eigen::Quaterniond quaternion);
238 
244  AttitudeConverter(const Eigen::Matrix3d matrix);
245 
252  template <class T>
253  AttitudeConverter(const Eigen::AngleAxis<T> angle_axis) {
254  double angle = angle_axis.angle();
255  tf2::Vector3 axis(angle_axis.axis()[0], angle_axis.axis()[1], angle_axis.axis()[2]);
256 
257  tf2_quaternion_.setRotation(axis, angle);
258  }
259 
265  AttitudeConverter(const tf2::Quaternion quaternion);
266 
272  AttitudeConverter(const tf2::Matrix3x3 matrix);
273 
274  //}
275 
276  /* operators //{ */
277 
283  operator tf2::Quaternion() const;
284 
290  operator tf::Quaternion() const;
291 
297  operator geometry_msgs::Quaternion() const;
298 
304  operator EulerAttitude() const;
305 
313  template <class T>
314  operator Eigen::AngleAxis<T>() const {
315 
316  double angle = tf2_quaternion_.getAngle();
317  Eigen::Vector3d axis(tf2_quaternion_.getAxis()[0], tf2_quaternion_.getAxis()[1], tf2_quaternion_.getAxis()[2]);
318 
319  Eigen::AngleAxis<T> angle_axis(angle, axis);
320 
321  return angle_axis;
322  }
323 
324 
332  template <class T>
333  operator Eigen::Quaternion<T>() const {
334 
335  return Eigen::Quaternion<T>(tf2_quaternion_.w(), tf2_quaternion_.x(), tf2_quaternion_.y(), tf2_quaternion_.z());
336  }
337 
338  operator Eigen::Matrix3d() const;
339 
345  operator std::tuple<double&, double&, double&>();
346 
352  operator tf2::Matrix3x3() const;
353 
359  operator tf2::Transform() const;
360 
361  //}
362 
363  /* getters //{ */
364 
370  double getRoll(void);
371 
377  double getPitch(void);
378 
384  double getYaw(void);
385 
391  double getHeading(void);
392 
400  double getHeadingRate(const Vector3Converter& attitude_rate);
401 
409  double getYawRateIntrinsic(const double& heading_rate);
410 
417 
424 
431 
437  std::tuple<double, double, double> getIntrinsicRPY();
438 
444  std::tuple<double, double, double> getExtrinsicRPY();
445 
446  //}
447 
448  /* setters //{ */
449 
457  AttitudeConverter setHeading(const double& heading);
458 
466  AttitudeConverter setYaw(const double& new_yaw);
467 
468  //}
469 
470  template <std::size_t I>
471  constexpr auto get();
472 
473 private:
477  tf2::Quaternion tf2_quaternion_;
478 
482  void calculateRPY(void);
483 
487  void validateOrientation(void);
488 
492  double roll_, pitch_, yaw_;
493  bool got_rpy_ = false;
494 };
495 
496 
497 template <std::size_t I>
498 constexpr auto AttitudeConverter::get() {
499 
500  calculateRPY();
501 
502  // call compilation error if I > 2
503  static_assert(I <= 2);
504 
505  // get the RPY components based on the index in the tuple
506  if constexpr (I == 0) {
507  return static_cast<double>(roll_);
508  } else if constexpr (I == 1) {
509  return static_cast<double>(pitch_);
510  } else if constexpr (I == 2) {
511  return static_cast<double>(yaw_);
512  }
513 }
514 
515 } // namespace mrs_lib
516 
517 template <>
518 struct std::tuple_size<mrs_lib::AttitudeConverter>
519 { static constexpr int value = 3; };
520 
521 template <>
522 struct std::tuple_element<0, mrs_lib::AttitudeConverter>
523 { using type = double; };
524 
525 template <>
526 struct std::tuple_element<1, mrs_lib::AttitudeConverter>
527 { using type = double; };
528 
529 template <>
530 struct std::tuple_element<2, mrs_lib::AttitudeConverter>
531 { using type = double; };
532 
533 #endif
mrs_lib::AttitudeConverter::AttitudeConverter
AttitudeConverter(const double &roll, const double &pitch, const double &yaw, const RPY_convention_t &format=RPY_EXTRINSIC)
Euler angles constructor.
Definition: attitude_converter.cpp:78
mrs_lib::AttitudeConverter::getHeading
double getHeading(void)
get the angle of the rotated x-axis in the original XY plane, a.k.a
Definition: attitude_converter.cpp:254
mrs_lib::AttitudeConverter::getVectorY
Vector3Converter getVectorY(void)
get a unit vector pointing in the Y direction
Definition: attitude_converter.cpp:354
mrs_lib::AttitudeConverter::GetHeadingException
is thrown when calculating of heading is not possible due to atan2 exception
Definition: attitude_converter.h:158
mrs_lib::Vector3Converter::Vector3Converter
Vector3Converter(const tf2::Vector3 &vector3)
Constructor with tf2::Vector3.
Definition: attitude_converter.h:97
mrs_lib::AttitudeConverter::AttitudeConverter
AttitudeConverter(const Eigen::AngleAxis< T > angle_axis)
Eigen::AngleAxis constructor.
Definition: attitude_converter.h:253
mrs_lib::AttitudeConverter::getIntrinsicRPY
std::tuple< double, double, double > getIntrinsicRPY()
get the Roll, Pitch, Yaw angles in the Intrinsic convention
Definition: attitude_converter.cpp:381
mrs_lib::AttitudeConverter::getYawRateIntrinsic
double getYawRateIntrinsic(const double &heading_rate)
get the intrinsic yaw rate from a heading rate
Definition: attitude_converter.cpp:267
mrs_lib::AttitudeConverter::getYaw
double getYaw(void)
get the yaw angle
Definition: attitude_converter.cpp:233
mrs_lib::AttitudeConverter::MathErrorException
is thrown when math breaks
Definition: attitude_converter.h:166
mrs_lib::AttitudeConverter::getRoll
double getRoll(void)
get the roll angle
Definition: attitude_converter.cpp:240
mrs_lib::AttitudeConverter
The main convertor class. Instantiate with any type in constructor and get the value in any other typ...
Definition: attitude_converter.h:153
mrs_lib::EulerAttitude::pitch
double pitch(void) const
get the pitch angle
Definition: attitude_converter.cpp:20
mrs_lib::AttitudeConverter::getVectorZ
Vector3Converter getVectorZ(void)
get a unit vector pointing in the Z direction
Definition: attitude_converter.cpp:363
mrs_lib::AttitudeConverter::getExtrinsicRPY
std::tuple< double, double, double > getExtrinsicRPY()
get the Roll, Pitch, Yaw angles in the Extrinsic convention. The same as the default AttitudeConverte...
Definition: attitude_converter.cpp:372
misc.h
Defines useful geometry utilities and functions.
mrs_lib::AttitudeConverter::setYaw
AttitudeConverter setYaw(const double &new_yaw)
Updates the extrinsic yaw of the current orientation.
Definition: attitude_converter.cpp:394
mrs_lib::EulerAttitude::roll
double roll(void) const
get the roll angle
Definition: attitude_converter.cpp:16
mrs_lib::EulerAttitude::EulerAttitude
EulerAttitude(const double &roll, const double &pitch, const double &yaw)
A simple class for storing the Euler angles.
Definition: attitude_converter.cpp:13
mrs_lib::AttitudeConverter::getVectorX
Vector3Converter getVectorX(void)
get a unit vector pointing in the X direction
Definition: attitude_converter.cpp:345
mrs_lib::AttitudeConverter::EulerFormatException
is thrown when the Euler angle format is set wrongly
Definition: attitude_converter.h:182
mrs_lib
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition: attitude_converter.h:29
mrs_lib::EulerAttitude::yaw
double yaw(void) const
get the yaw angle
Definition: attitude_converter.cpp:24
mrs_lib::EulerAttitude
A small class for storing the Euler angles.
Definition: attitude_converter.h:46
mrs_lib::AttitudeConverter::getPitch
double getPitch(void)
get the pitch angle
Definition: attitude_converter.cpp:247
mrs_lib::Vector3Converter
Converter of Vector3 representations. Instantiate it with any type of vector3 in constructor and conv...
Definition: attitude_converter.h:90
mrs_lib::AttitudeConverter::setHeading
AttitudeConverter setHeading(const double &heading)
Updates the heading of the current orientation by updating the intrinsic yaw.
Definition: attitude_converter.cpp:403
mrs_lib::AttitudeConverter::getHeadingRate
double getHeadingRate(const Vector3Converter &attitude_rate)
get heading rate base on the orientation and body-based attitude rate
Definition: attitude_converter.cpp:312
mrs_lib::AttitudeConverter::SetHeadingException
is thrown when the heading cannot be set to an existing attitude
Definition: attitude_converter.h:190
mrs_lib::AttitudeConverter::InvalidAttitudeException
is thrown when the internal attitude becomes invalid
Definition: attitude_converter.h:174