mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
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 <cmath>
16#include <Eigen/Dense>
17#include <tuple>
18
19#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
20
22
23namespace mrs_lib
24{
25
26 // type of the object we are grasping
27 typedef enum
28 {
29
30 RPY_INTRINSIC = 1,
31 RPY_EXTRINSIC = 2,
32
33 } RPY_convention_t;
34
35 /* class EulerAttitude //{ */
36
41 {
42 public:
50 EulerAttitude(const double& roll, const double& pitch, const double& yaw);
51
57 double roll(void) const;
58
64 double pitch(void) const;
65
71 double yaw(void) const;
72
73 private:
74 double roll_, pitch_, yaw_;
75 };
76
77 //}
78
79 /* class Vector3Converter //{ */
80
86 {
87 public:
93 Vector3Converter(const tf2::Vector3& vector3) : vector3_(vector3){};
94
100 Vector3Converter(const Eigen::Vector3d& vector3);
101
107 Vector3Converter(const geometry_msgs::msg::Vector3& vector3);
108
116 Vector3Converter(const double& x, const double& y, const double& z);
117
123 operator tf2::Vector3() const;
124
130 operator Eigen::Vector3d() const;
131
137 operator geometry_msgs::msg::Vector3() const;
138
139 private:
140 tf2::Vector3 vector3_;
141 };
142
143 //}
144
150 {
151 public:
152 /* exceptions //{ */
153
155 struct GetHeadingException : public std::exception
156 {
157 const char* what() const throw()
158 {
159 return "AttitudeConverter: can not calculate the heading, the rotated x-axis is parallel to the world's z-axis";
160 }
161 };
162
164 struct MathErrorException : public std::exception
165 {
166 const char* what() const throw()
167 {
168 return "AttitudeConverter: math error";
169 }
170 };
171
173 struct InvalidAttitudeException : public std::exception
174 {
175 const char* what() const throw()
176 {
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 {
186 return "AttitudeConverter: invalid Euler angle format";
187 }
188 };
189
191 struct SetHeadingException : public std::exception
192 {
193 const char* what() const throw()
194 {
195 return "AttitudeConverter: cannot set the desired heading, the thrust vector's Z component is 0";
196 }
197 };
198
199 //}
200
201 /* constructors //{ */
202
211 AttitudeConverter(const double& roll, const double& pitch, const double& yaw, const RPY_convention_t& format = RPY_EXTRINSIC);
212
218 AttitudeConverter(const geometry_msgs::msg::Quaternion quaternion);
219
225 AttitudeConverter(const mrs_lib::EulerAttitude& euler_attitude);
226
232 AttitudeConverter(const Eigen::Quaterniond quaternion);
233
239 AttitudeConverter(const Eigen::Matrix3d matrix);
240
247 template <class T>
248 AttitudeConverter(const Eigen::AngleAxis<T> angle_axis)
249 {
250 double angle = angle_axis.angle();
251 tf2::Vector3 axis(angle_axis.axis()[0], angle_axis.axis()[1], angle_axis.axis()[2]);
252
253 tf2_quaternion_.setRotation(axis, angle);
254 }
255
261 AttitudeConverter(const tf2::Quaternion quaternion);
262
268 AttitudeConverter(const tf2::Matrix3x3 matrix);
269
270 //}
271
272 /* operators //{ */
273
279 operator tf2::Quaternion() const;
280
286 operator geometry_msgs::msg::Quaternion() const;
287
293 operator EulerAttitude() const;
294
302 template <class T>
303 operator Eigen::AngleAxis<T>() const
304 {
305
306 double angle = tf2_quaternion_.getAngle();
307 Eigen::Vector3d axis(tf2_quaternion_.getAxis()[0], tf2_quaternion_.getAxis()[1], tf2_quaternion_.getAxis()[2]);
308
309 Eigen::AngleAxis<T> angle_axis(angle, axis);
310
311 return angle_axis;
312 }
313
314
322 template <class T>
323 operator Eigen::Quaternion<T>() const
324 {
325
326 return Eigen::Quaternion<T>(tf2_quaternion_.w(), tf2_quaternion_.x(), tf2_quaternion_.y(), tf2_quaternion_.z());
327 }
328
329 operator Eigen::Matrix3d() const;
330
336 operator std::tuple<double&, double&, double&>();
337
343 operator tf2::Matrix3x3() const;
344
350 operator tf2::Transform() const;
351
352 //}
353
354 /* getters //{ */
355
361 double getRoll(void);
362
368 double getPitch(void);
369
375 double getYaw(void);
376
382 double getHeading(void);
383
391 double getHeadingRate(const Vector3Converter& attitude_rate);
392
400 double getYawRateIntrinsic(const double& heading_rate);
401
408
415
422
428 std::tuple<double, double, double> getIntrinsicRPY();
429
435 std::tuple<double, double, double> getExtrinsicRPY();
436
437 //}
438
439 /* setters //{ */
440
448 AttitudeConverter setHeading(const double& heading);
449
457 AttitudeConverter setYaw(const double& new_yaw);
458
459 //}
460
461 template <std::size_t I>
462 constexpr auto get();
463
464 private:
468 tf2::Quaternion tf2_quaternion_;
469
473 void calculateRPY(void);
474
478 void validateOrientation(void);
479
483 double roll_, pitch_, yaw_;
484 bool got_rpy_ = false;
485 };
486
487
488 template <std::size_t I>
489 constexpr auto AttitudeConverter::get()
490 {
491
492 calculateRPY();
493
494 // call compilation error if I > 2
495 static_assert(I <= 2);
496
497 // get the RPY components based on the index in the tuple
498 if constexpr (I == 0)
499 {
500 return static_cast<double>(roll_);
501 } else if constexpr (I == 1)
502 {
503 return static_cast<double>(pitch_);
504 } else if constexpr (I == 2)
505 {
506 return static_cast<double>(yaw_);
507 }
508 }
509
510} // namespace mrs_lib
511
512template <>
513struct std::tuple_size<mrs_lib::AttitudeConverter>
514{
515 static constexpr int value = 3;
516};
517
518template <>
519struct std::tuple_element<0, mrs_lib::AttitudeConverter>
520{
521 using type = double;
522};
523
524template <>
525struct std::tuple_element<1, mrs_lib::AttitudeConverter>
526{
527 using type = double;
528};
529
530template <>
531struct std::tuple_element<2, mrs_lib::AttitudeConverter>
532{
533 using type = double;
534};
535
536#endif
The main convertor class. Instantiate with any type in constructor and get the value in any other typ...
Definition attitude_converter.h:150
AttitudeConverter(const Eigen::AngleAxis< T > angle_axis)
Eigen::AngleAxis constructor.
Definition attitude_converter.h:248
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:391
Vector3Converter getVectorX(void)
get a unit vector pointing in the X direction
Definition attitude_converter.cpp:361
Vector3Converter getVectorZ(void)
get a unit vector pointing in the Z direction
Definition attitude_converter.cpp:381
double getPitch(void)
get the pitch angle
Definition attitude_converter.cpp:254
double getHeading(void)
get the angle of the rotated x-axis in the original XY plane, a.k.a
Definition attitude_converter.cpp:262
double getRoll(void)
get the roll angle
Definition attitude_converter.cpp:246
Vector3Converter getVectorY(void)
get a unit vector pointing in the Y direction
Definition attitude_converter.cpp:371
AttitudeConverter setYaw(const double &new_yaw)
Updates the extrinsic yaw of the current orientation.
Definition attitude_converter.cpp:415
double getYaw(void)
get the yaw angle
Definition attitude_converter.cpp:238
double getHeadingRate(const Vector3Converter &attitude_rate)
get heading rate base on the orientation and body-based attitude rate
Definition attitude_converter.cpp:326
std::tuple< double, double, double > getIntrinsicRPY()
get the Roll, Pitch, Yaw angles in the Intrinsic convention
Definition attitude_converter.cpp:401
AttitudeConverter setHeading(const double &heading)
Updates the heading of the current orientation by updating the intrinsic yaw.
Definition attitude_converter.cpp:425
double getYawRateIntrinsic(const double &heading_rate)
get the intrinsic yaw rate from a heading rate
Definition attitude_converter.cpp:277
A small class for storing the Euler angles.
Definition attitude_converter.h:41
double roll(void) const
get the roll angle
Definition attitude_converter.cpp:19
double yaw(void) const
get the yaw angle
Definition attitude_converter.cpp:29
double pitch(void) const
get the pitch angle
Definition attitude_converter.cpp:24
Converter of Vector3 representations. Instantiate it with any type of vector3 in constructor and conv...
Definition attitude_converter.h:86
Vector3Converter(const tf2::Vector3 &vector3)
Constructor with tf2::Vector3.
Definition attitude_converter.h:93
Defines useful geometry utilities and functions.
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
is thrown when the Euler angle format is set wrongly
Definition attitude_converter.h:183
is thrown when calculating of heading is not possible due to atan2 exception
Definition attitude_converter.h:156
is thrown when the internal attitude becomes invalid
Definition attitude_converter.h:174
is thrown when math breaks
Definition attitude_converter.h:165
is thrown when the heading cannot be set to an existing attitude
Definition attitude_converter.h:192