humoto
rotary_state.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
5  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
6 
7  @brief
8 */
9 
10 #pragma once
11 
12 namespace humoto
13 {
14  namespace rigidbody
15  {
16  /**
17  * @brief Types of Euler angles
18  */
20  {
21  public:
22  enum Type
23  {
24  UNDEFINED = 0,
25  XYZ = 1,
26  YPR = 1,
27  ZYX = 2,
28  RPY = 2
29  };
30  };
31 
32 
33  /**
34  * @brief Convert rotation matrix to Euler angles.
35  *
36  * @param[in] rotation_matrix
37  * @param[in] euler_angles_type
38  *
39  * @return Euler angles
40  */
41  inline etools::Vector3 convertMatrixToEulerAngles( const etools::Matrix3 &rotation_matrix,
42  const EulerAngles::Type euler_angles_type)
43  {
44  switch (euler_angles_type)
45  {
46  case EulerAngles::YPR:
47  return (rotation_matrix.eulerAngles(0, 1, 2));
48  break;
49 
50  case EulerAngles::RPY:
51  return (rotation_matrix.eulerAngles(2, 1, 0).reverse());
52  break;
53 
54  default:
55  std::stringstream error_msg;
56  error_msg << "Euler angles of type '" << euler_angles_type << "' are not yet supported.";
57  HUMOTO_THROW_MSG(error_msg.str());
58  break;
59  }
60  }
61 
62 
63  /**
64  * @brief Convert rotation matrix to Euler angles.
65  *
66  * @param[in] euler_angles
67  * @param[in] euler_angles_type
68  *
69  * @return Euler angles
70  */
71  inline etools::Matrix3 convertEulerAnglesToMatrix( const etools::Vector3 &euler_angles,
72  const EulerAngles::Type euler_angles_type)
73  {
74  switch (euler_angles_type)
75  {
76  case EulerAngles::RPY:
77  return( ( Eigen::AngleAxisd(euler_angles(AngleIndex::YAW), Eigen::Vector3d::UnitZ())
78  * Eigen::AngleAxisd(euler_angles(AngleIndex::PITCH), Eigen::Vector3d::UnitY())
79  * Eigen::AngleAxisd(euler_angles(AngleIndex::ROLL), Eigen::Vector3d::UnitX())).matrix() );
80  break;
81 
82  case EulerAngles::YPR:
83  return( ( Eigen::AngleAxisd(euler_angles(AngleIndex::ROLL), Eigen::Vector3d::UnitX())
84  * Eigen::AngleAxisd(euler_angles(AngleIndex::PITCH), Eigen::Vector3d::UnitY())
85  * Eigen::AngleAxisd(euler_angles(AngleIndex::YAW), Eigen::Vector3d::UnitZ())).matrix() );
86  break;
87 
88  default:
89  std::stringstream error_msg;
90  error_msg << "Euler angles of type '" << euler_angles_type << "' are not yet supported.";
91  HUMOTO_THROW_MSG(error_msg.str());
92  break;
93  }
94  }
95 
96 
97 
98  /**
99  * @brief Convert Euler angles to a different set ofEuler angles
100  *
101  * @param[in] euler_angles
102  * @param[in] euler_angles_type_from
103  * @param[in] euler_angles_type_to
104  *
105  * @return Euler angles
106  */
107  inline etools::Vector3 convertEulerAngles( const etools::Vector3 &euler_angles,
108  const EulerAngles::Type euler_angles_type_from,
109  const EulerAngles::Type euler_angles_type_to)
110  {
111  etools::Matrix3 rotation_matrix = convertEulerAnglesToMatrix(euler_angles, euler_angles_type_from);
112  return (convertMatrixToEulerAngles(rotation_matrix, euler_angles_type_to));
113  }
114 
115 
117  const etools::Matrix3 & reference)
118  {
119  return (reference * current.transpose());
120  }
121 
122 
123  inline etools::Vector3 getRotationErrorAngleAxis( const etools::Matrix3 & current,
124  const etools::Matrix3 & reference)
125  {
126  return (0.5 * ( current.col(0).cross(reference.col(0))
127  +
128  current.col(1).cross(reference.col(1))
129  +
130  current.col(2).cross(reference.col(2)) ) );
131  }
132 
133 
134  /**
135  * @brief Returns matrix M such that w = M * de.
136  *
137  * @param[in] euler_angles current orientation (Euler angles)
138  * @param[in] euler_angles_type euler angles type
139  *
140  * @return 3x3 matrix
141  */
143  const etools::Vector3 &euler_angles,
144  const EulerAngles::Type euler_angles_type)
145  {
146  switch (euler_angles_type)
147  {
148  case EulerAngles::RPY:
149  return( ( etools::Matrix3()
150  << cos(euler_angles.y()) * cos(euler_angles.z()), -sin(euler_angles.z()), 0.0,
151  cos(euler_angles.y()) * sin(euler_angles.z()), cos(euler_angles.z()), 0.0,
152  -sin(euler_angles.y()), 0.0, 1.0 ).finished() );
153  break;
154 
155  default:
156  std::stringstream error_msg;
157  error_msg << "Euler angles of type '" << euler_angles_type << "' are not yet supported.";
158  HUMOTO_THROW_MSG(error_msg.str());
159  break;
160  }
161  }
162 
163 
164 
165  /**
166  * @brief Integrate angular velocity to obtain orientation matrix.
167  * Simple version.
168  *
169  * @param[in] orientation
170  * @param[in] angular_velocity
171  * @param[in] dt
172  *
173  * @return new orientation matrix
174  */
175  inline etools::Matrix3
177  const etools::Matrix3 & orientation,
178  const etools::Vector3 & angular_velocity,
179  const double dt)
180  {
181  return(orientation + dt * orientation * etools::CrossProductMatrix(angular_velocity));
182  }
183 
184 
185  /**
186  * @brief Integrate angular velocity to obtain orientation matrix.
187  * Use Rodrigues formula.
188  *
189  * @param[in] orientation
190  * @param[in] angular_velocity
191  * @param[in] dt
192  * @param[in] tolerance
193  *
194  * @return new orientation matrix
195  */
196  inline etools::Matrix3
198  const etools::Matrix3 & orientation,
199  const etools::Vector3 & angular_velocity,
200  const double dt,
201  const double tolerance = humoto::g_generic_tolerance)
202  {
203  double vel = angular_velocity.norm();
204 
205  if (std::abs(vel) < tolerance)
206  {
207  return(orientation);
208  }
209  else
210  {
211  etools::CrossProductMatrix tilde_axis(angular_velocity/vel);
212 
213  //R*(I + sin(v*dt)*tilde(ax) + (1-cos(v*dt))*tilde(ax)*tilde(ax))
214  etools::Matrix3 orient_axis = orientation * tilde_axis;
215  return ( orientation
216  + sin(vel*dt) * orient_axis
217  + (1 - cos(vel*dt)) * orient_axis * tilde_axis );
218  }
219  }
220 
221 
222  /**
223  * @brief Class that groups together parameters related to a robot foot
224  */
226  {
227  #define HUMOTO_CONFIG_SECTION_ID "RotaryState"
228  #define HUMOTO_CONFIG_CONSTRUCTOR RotaryState
229  #define HUMOTO_CONFIG_ENTRIES \
230  HUMOTO_CONFIG_COMPOUND_(rpy) \
231  HUMOTO_CONFIG_COMPOUND_(angular_velocity) \
232  HUMOTO_CONFIG_COMPOUND_(angular_acceleration)
233  #include HUMOTO_CONFIG_DEFINE_ACCESSORS
234 
235 
236  public:
237  etools::Vector3 rpy_;
238  etools::Vector3 angular_velocity_;
239  etools::Vector3 angular_acceleration_;
240 
241 
242  public:
243  /**
244  * @brief Default constructor.
245  */
247  {
248  setDefaults();
249  }
250 
251 
252  /**
253  * @brief Initialize state (everything is set to zeros).
254  */
255  void setDefaults()
256  {
257  rpy_.setZero();
258  angular_velocity_.setZero();
259  angular_acceleration_.setZero();
260  }
261 
262 
263  /**
264  * @brief Initialize state (everything is set to NaN).
265  */
266  void unset()
267  {
268  etools::unsetMatrix(rpy_);
269  etools::unsetMatrix(angular_velocity_);
270  etools::unsetMatrix(angular_acceleration_);
271  }
272 
273 
274  /**
275  * @brief Return orientation as a 3d matrix.
276  *
277  * @return 3d orientation matrix
278  */
280  {
282  }
283 
284 
285  /**
286  * @brief Log
287  *
288  * @param[in,out] logger logger
289  * @param[in] parent parent
290  * @param[in] name name
291  */
293  const LogEntryName &parent = LogEntryName(),
294  const std::string &name = "rotary_state") const
295  {
296  LogEntryName subname = parent; subname.add(name);
297 
298  logger.log(LogEntryName(subname).add("rpy") , rpy_);
299  logger.log(LogEntryName(subname).add("angular_velocity") , angular_velocity_);
300  logger.log(LogEntryName(subname).add("angular_acceleration"), angular_acceleration_);
301  }
302  };
303  }
304 }
etools::Vector3 angular_velocity_
Definition: rotary_state.h:238
void unset()
Initialize state (everything is set to NaN).
Definition: rotary_state.h:266
etools::Matrix3 getRotationError(const etools::Matrix3 &current, const etools::Matrix3 &reference)
Definition: rotary_state.h:116
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="rotary_state") const
Log.
Definition: rotary_state.h:292
#define HUMOTO_LOCAL
Definition: export_import.h:26
etools::Matrix3 integrateAngularVelocityRodrigues(const etools::Matrix3 &orientation, const etools::Vector3 &angular_velocity, const double dt, const double tolerance=humoto::g_generic_tolerance)
Integrate angular velocity to obtain orientation matrix. Use Rodrigues formula.
Definition: rotary_state.h:197
RotaryState()
Default constructor.
Definition: rotary_state.h:246
Types of Euler angles.
Definition: rotary_state.h:19
#define HUMOTO_GLOBAL_LOGGER_IF_DEFINED
Definition: logger.h:997
Default configurable base is strict.
Definition: config.h:353
etools::Vector3 convertEulerAngles(const etools::Vector3 &euler_angles, const EulerAngles::Type euler_angles_type_from, const EulerAngles::Type euler_angles_type_to)
Convert Euler angles to a different set ofEuler angles.
Definition: rotary_state.h:107
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
Represents log entry name.
Definition: logger.h:169
Class that groups together parameters related to a robot foot.
Definition: rotary_state.h:225
etools::Vector3 angular_acceleration_
Definition: rotary_state.h:239
etools::Vector3 convertMatrixToEulerAngles(const etools::Matrix3 &rotation_matrix, const EulerAngles::Type euler_angles_type)
Convert rotation matrix to Euler angles.
Definition: rotary_state.h:41
void unsetMatrix(Eigen::DenseBase< t_Derived > &matrix)
Unset matrix (initialize to NaN)
Definition: eigentools.h:178
etools::Vector3 getRotationErrorAngleAxis(const etools::Matrix3 &current, const etools::Matrix3 &reference)
Definition: rotary_state.h:123
etools::Matrix3 integrateAngularVelocity(const etools::Matrix3 &orientation, const etools::Vector3 &angular_velocity, const double dt)
Integrate angular velocity to obtain orientation matrix. Simple version.
Definition: rotary_state.h:176
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
The root namespace of HuMoTo.
Definition: config.h:12
void setDefaults()
Initialize state (everything is set to zeros).
Definition: rotary_state.h:255
etools::Matrix3 getOrientationMatrix() const
Return orientation as a 3d matrix.
Definition: rotary_state.h:279
etools::Matrix3 getEulerRatesToAngularVelocityTransform(const etools::Vector3 &euler_angles, const EulerAngles::Type euler_angles_type)
Returns matrix M such that w = M * de.
Definition: rotary_state.h:142
Skew-symmetric cross product matrix.
Definition: cross_product.h:18
LogEntryName & add(const char *name)
extends entry name with a subname
Definition: logger.h:232
EIGENTOOLS_CONSTANT_SIZE_MATRIX Matrix3
Definition: eigentools.h:77
const double g_generic_tolerance
Generic tolerance, should be used by default.
Definition: constants.h:28
etools::Matrix3 convertEulerAnglesToMatrix(const etools::Vector3 &euler_angles, const EulerAngles::Type euler_angles_type)
Convert rotation matrix to Euler angles.
Definition: rotary_state.h:71