parsing_utilities.hpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // © Copyright 2020, Septentrio NV/SA.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived
15 // from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // *****************************************************************************
30 
31 #pragma once
32 
33 // C++ library includes
34 #include <cmath> // C++ header, corresponds to <math.h> in C
35 #include <cstdint> // C++ header, corresponds to <stdint.h> in C
36 #include <ctime> // C++ header, corresponds to <time.h> in C
37 #include <string> // C++ header, corresponds to <string.h> in C
38 // Eigen Includes
39 #include <Eigen/Core>
40 #include <Eigen/LU>
41 // Boost includes
42 #include <boost/math/constants/constants.hpp>
43 // ROS includes
44 #ifdef ROS2
46 #endif
47 #ifdef ROS1
49 #endif
50 
57 namespace parsing_utilities {
58 
59  const double pihalf = boost::math::constants::half_pi<double>();
60 
61  /***********************************************************************
62  * Square value
63  **********************************************************************/
64  template <class T>
65  [[nodiscard]] inline T square(T val)
66  {
67  return val * val;
68  }
69 
70  /***********************************************************************
71  * Convert degrees to radians
72  **********************************************************************/
73  template <class T>
74  [[nodiscard]] inline T deg2rad(T deg)
75  {
76  return deg * boost::math::constants::degree<T>();
77  }
78 
79  /***********************************************************************
80  * Convert degrees^2 to radians^2
81  **********************************************************************/
82  template <class T>
83  [[nodiscard]] inline T deg2radSq(T deg)
84  {
85  return deg * boost::math::constants::degree<T>() *
86  boost::math::constants::degree<T>();
87  }
88 
89  /***********************************************************************
90  * Convert radians to degree
91  **********************************************************************/
92  template <class T>
93  [[nodiscard]] inline T rad2deg(T rad)
94  {
95  return rad * boost::math::constants::radian<T>();
96  }
97 
98  /***********************************************************************
99  * Convert Euler angles to rotation matrix
100  **********************************************************************/
101  [[nodiscard]] inline Eigen::Matrix3d rpyToRot(double roll, double pitch,
102  double yaw)
103  {
104  Eigen::Matrix3d M;
105  double sa, ca, sb, cb, sc, cc;
106  sa = std::sin(roll);
107  ca = std::cos(roll);
108  sb = std::sin(pitch);
109  cb = std::cos(pitch);
110  sc = std::sin(yaw);
111  cc = std::cos(yaw);
112 
113  M << cb * cc, -ca * sc + sa * sb * cc, sc * sa + ca * sb * cc, cb * sc,
114  ca * cc + sa * sb * sc, -sa * cc + ca * sb * sc, -sb, sa * cb, ca * cb;
115  return M;
116  }
117 
122  [[nodiscard]] inline double wrapAngle180to180(double angle)
123  {
124  return std::remainder(angle, 360.0);
125  }
126 
132  [[nodiscard]] double parseDouble(const uint8_t* buffer);
133 
146  [[nodiscard]] bool parseDouble(const std::string& string, double& value);
147 
153  [[nodiscard]] float parseFloat(const uint8_t* buffer);
154 
167  [[nodiscard]] bool parseFloat(const std::string& string, float& value);
168 
174  [[nodiscard]] int16_t parseInt16(const uint8_t* buffer);
175 
190  [[nodiscard]] bool parseInt16(const std::string& string, int16_t& value,
191  int32_t base = 10);
192 
198  [[nodiscard]] int32_t parseInt32(const uint8_t* buffer);
199 
214  [[nodiscard]] bool parseInt32(const std::string& string, int32_t& value,
215  int32_t base = 10);
216 
231  [[nodiscard]] bool parseUInt8(const std::string& string, uint8_t& value,
232  int32_t base = 10);
233 
239  [[nodiscard]] uint16_t parseUInt16(const uint8_t* buffer);
240 
255  [[nodiscard]] bool parseUInt16(const std::string& string, uint16_t& value,
256  int32_t base = 10);
257 
263  [[nodiscard]] uint32_t parseUInt32(const uint8_t* buffer);
264 
279  [[nodiscard]] bool parseUInt32(const std::string& string, uint32_t& value,
280  int32_t base = 10);
281 
289  [[nodiscard]] double convertUTCDoubleToSeconds(double utc_double);
290 
301  [[nodiscard]] std::time_t convertUTCtoUnix(double utc_double);
302 
312  [[nodiscard]] double convertDMSToDegrees(double dms);
313 
321  [[nodiscard]] inline Eigen::Quaterniond
326  convertEulerToQuaternion(double roll, double pitch, double yaw)
327  {
328  double cy = std::cos(yaw * 0.5);
329  double sy = std::sin(yaw * 0.5);
330  double cp = std::cos(pitch * 0.5);
331  double sp = std::sin(pitch * 0.5);
332  double cr = std::cos(roll * 0.5);
333  double sr = std::sin(roll * 0.5);
334 
335  return Eigen::Quaterniond(
336  cr * cp * cy + sr * sp * sy, sr * cp * cy - cr * sp * sy,
337  cr * sp * cy + sr * cp * sy, cr * cp * sy - sr * sp * cy);
338  }
339 
347  [[nodiscard]] inline QuaternionMsg
348  quaternionToQuaternionMsg(const Eigen::Quaterniond& q)
349  {
350  QuaternionMsg qm;
351 
352  qm.w = q.w();
353  qm.x = q.x();
354  qm.y = q.y();
355  qm.z = q.z();
356 
357  return qm;
358  }
359 
365  [[nodiscard]] inline QuaternionMsg
366  convertEulerToQuaternionMsg(double roll, double pitch, double yaw)
367  {
368  return quaternionToQuaternionMsg(convertEulerToQuaternion(roll, pitch, yaw));
369  }
370 
371  inline void setQuaternionNaN(QuaternionMsg& quaternion)
372  {
373  quaternion.w = std::numeric_limits<double>::quiet_NaN();
374  quaternion.x = std::numeric_limits<double>::quiet_NaN();
375  quaternion.y = std::numeric_limits<double>::quiet_NaN();
376  quaternion.z = std::numeric_limits<double>::quiet_NaN();
377  }
378 
379  inline void setVector3NaN(Vector3Msg& v)
380  {
381  v.x = std::numeric_limits<double>::quiet_NaN();
382  v.y = std::numeric_limits<double>::quiet_NaN();
383  v.z = std::numeric_limits<double>::quiet_NaN();
384  }
385 
392  [[nodiscard]] inline Eigen::Quaterniond q_enu_ecef(double lat, double lon)
393  {
394  double sr = sin((pihalf - lat) / 2.0);
395  double cr = cos((pihalf - lat) / 2.0);
396  double sy = sin((lon + pihalf) / 2.0);
397  double cy = cos((lon + pihalf) / 2.0);
398 
399  return Eigen::Quaterniond(cr * cy, sr * cy, sr * sy, cr * sy);
400  }
401 
408  [[nodiscard]] inline Eigen::Quaterniond q_ned_ecef(double lat, double lon)
409  {
410  double sp = sin((-lat - pihalf) / 2.0);
411  double cp = cos((-lat - pihalf) / 2.0);
412  double sy = sin(lon / 2.0);
413  double cy = cos(lon / 2.0);
414 
415  return Eigen::Quaterniond(cp * cy, -sp * sy, sp * cy, cp * sy);
416  }
417 
424  [[nodiscard]] inline Eigen::Matrix3d R_enu_ecef(double lat, double lon)
425  {
426  Eigen::Matrix3d R;
427 
428  double sin_lat = sin(lat);
429  double cos_lat = cos(lat);
430  double sin_lon = sin(lon);
431  double cos_lon = cos(lon);
432 
433  R(0, 0) = -sin_lon;
434  R(0, 1) = -cos_lon * sin_lat;
435  R(0, 2) = cos_lon * cos_lat;
436  R(1, 0) = cos_lon;
437  R(1, 1) = -sin_lon * sin_lat;
438  R(1, 2) = sin_lon * cos_lat;
439  R(2, 0) = 0.0;
440  R(2, 1) = cos_lat;
441  R(2, 2) = sin_lat;
442 
443  return R;
444  }
445 
452  [[nodiscard]] inline Eigen::Matrix3d R_ned_ecef(double lat, double lon)
453  {
454  Eigen::Matrix3d R;
455 
456  double sin_lat = sin(lat);
457  double cos_lat = cos(lat);
458  double sin_lon = sin(lon);
459  double cos_lon = cos(lon);
460 
461  R(0, 0) = -cos_lon * sin_lat;
462  R(0, 1) = -sin_lon;
463  R(0, 2) = -cos_lon * cos_lat;
464  R(1, 0) = -sin_lon * sin_lat;
465  R(1, 1) = cos_lon;
466  R(1, 2) = -sin_lon * cos_lat;
467  R(2, 0) = cos_lat;
468  R(2, 1) = 0.0;
469  R(2, 2) = -sin_lat;
470 
471  return R;
472  }
473 
482  [[nodiscard]] std::string convertUserPeriodToRxCommand(uint32_t period_user);
483 
490  [[nodiscard]] uint16_t getCrc(const std::vector<uint8_t>& message);
491 
498  [[nodiscard]] uint16_t getId(const std::vector<uint8_t>& message);
499 
506  [[nodiscard]] uint16_t getLength(const std::vector<uint8_t>& message);
507 
514  [[nodiscard]] uint32_t getTow(const std::vector<uint8_t>& message);
515 
522  [[nodiscard]] uint16_t getWnc(const std::vector<uint8_t>& message);
523 
524  inline double convertAutoCovariance(double val)
525  {
526  return std::isnan(val) ? -1.0 : deg2radSq(val);
527  }
528 
529  inline double convertCovariance(double val)
530  {
531  return std::isnan(val) ? 0.0 : deg2radSq(val);
532  }
533 } // namespace parsing_utilities
parsing_utilities::quaternionToQuaternionMsg
QuaternionMsg quaternionToQuaternionMsg(const Eigen::Quaterniond &q)
Transforms Euler angles to a QuaternionMsg.
Definition: parsing_utilities.hpp:348
parsing_utilities::getTow
uint32_t getTow(const std::vector< uint8_t > &message)
Get the time of week in ms of the SBF message.
Definition: parsing_utilities.cpp:325
parsing_utilities::rad2deg
T rad2deg(T rad)
Definition: parsing_utilities.hpp:93
parsing_utilities::wrapAngle180to180
double wrapAngle180to180(double angle)
Wraps an angle between -180 and 180 degrees.
Definition: parsing_utilities.hpp:122
parsing_utilities::convertUTCtoUnix
std::time_t convertUTCtoUnix(double utc_double)
Converts UTC time from the without-colon-delimiter format to Unix Epoch time (a number-of-seconds-sin...
Definition: parsing_utilities.cpp:258
QuaternionMsg
geometry_msgs::msg::Quaternion QuaternionMsg
Definition: typedefs.hpp:108
parsing_utilities::convertUserPeriodToRxCommand
std::string convertUserPeriodToRxCommand(uint32_t period_user)
Transforms the input polling period [milliseconds] into a std::string number that can be appended to ...
Definition: parsing_utilities.cpp:291
parsing_utilities::convertCovariance
double convertCovariance(double val)
Definition: parsing_utilities.hpp:529
parsing_utilities::getWnc
uint16_t getWnc(const std::vector< uint8_t > &message)
Get the GPS week counter of the SBF message.
Definition: parsing_utilities.cpp:330
parsing_utilities::parseInt32
int32_t parseInt32(const uint8_t *buffer)
Converts a 4-byte-buffer into a signed 32-bit integer.
Definition: parsing_utilities.cpp:123
parsing_utilities::parseFloat
float parseFloat(const uint8_t *buffer)
Converts a 4-byte-buffer into a float.
Definition: parsing_utilities.cpp:66
parsing_utilities::R_enu_ecef
Eigen::Matrix3d R_enu_ecef(double lat, double lon)
Generates the matrix to rotate from local ENU to ECEF.
Definition: parsing_utilities.hpp:424
parsing_utilities
Definition: parsing_utilities.hpp:57
parsing_utilities::convertEulerToQuaternion
Eigen::Quaterniond convertEulerToQuaternion(double roll, double pitch, double yaw)
Transforms Euler angles to a quaternion.
Definition: parsing_utilities.hpp:326
parsing_utilities::getLength
uint16_t getLength(const std::vector< uint8_t > &message)
Get the length of the SBF message.
Definition: parsing_utilities.cpp:320
parsing_utilities::convertUTCDoubleToSeconds
double convertUTCDoubleToSeconds(double utc_double)
Converts UTC time from the without-colon-delimiter format to the number-of-seconds-since-midnight for...
Definition: parsing_utilities.cpp:221
parsing_utilities::pihalf
const double pihalf
Definition: parsing_utilities.hpp:59
parsing_utilities::getCrc
uint16_t getCrc(const std::vector< uint8_t > &message)
Get the CRC of the SBF message.
Definition: parsing_utilities.cpp:305
parsing_utilities::parseUInt32
uint32_t parseUInt32(const uint8_t *buffer)
Converts a 4-byte-buffer into an unsigned 32-bit integer.
Definition: parsing_utilities.cpp:198
parsing_utilities::parseUInt16
uint16_t parseUInt16(const uint8_t *buffer)
Converts a 2-byte-buffer into an unsigned 16-bit integer.
Definition: parsing_utilities.cpp:166
parsing_utilities::convertEulerToQuaternionMsg
QuaternionMsg convertEulerToQuaternionMsg(double roll, double pitch, double yaw)
Convert Eigen quaternion to a QuaternionMsg.
Definition: parsing_utilities.hpp:366
Vector3Msg
geometry_msgs::msg::Vector3 Vector3Msg
Definition: typedefs.hpp:112
parsing_utilities::R_ned_ecef
Eigen::Matrix3d R_ned_ecef(double lat, double lon)
Generates the matrix to rotate from local NED to ECEF.
Definition: parsing_utilities.hpp:452
parsing_utilities::convertAutoCovariance
double convertAutoCovariance(double val)
Definition: parsing_utilities.hpp:524
parsing_utilities::getId
uint16_t getId(const std::vector< uint8_t > &message)
Get the ID of the SBF message.
Definition: parsing_utilities.cpp:310
parsing_utilities::convertDMSToDegrees
double convertDMSToDegrees(double dms)
Converts latitude or longitude from the DMS notation (in the without-colon-delimiter format),...
Definition: parsing_utilities.cpp:235
parsing_utilities::square
T square(T val)
Definition: parsing_utilities.hpp:65
typedefs.hpp
parsing_utilities::rpyToRot
Eigen::Matrix3d rpyToRot(double roll, double pitch, double yaw)
Definition: parsing_utilities.hpp:101
parsing_utilities::q_ned_ecef
Eigen::Quaterniond q_ned_ecef(double lat, double lon)
Generates the quaternion to rotate from local NED to ECEF.
Definition: parsing_utilities.hpp:408
parsing_utilities::q_enu_ecef
Eigen::Quaterniond q_enu_ecef(double lat, double lon)
Generates the quaternion to rotate from local ENU to ECEF.
Definition: parsing_utilities.hpp:392
angle
TF2SIMD_FORCE_INLINE tf2Scalar angle(const Quaternion &q1, const Quaternion &q2)
parsing_utilities::setQuaternionNaN
void setQuaternionNaN(QuaternionMsg &quaternion)
Definition: parsing_utilities.hpp:371
typedefs_ros1.hpp
parsing_utilities::deg2radSq
T deg2radSq(T deg)
Definition: parsing_utilities.hpp:83
parsing_utilities::parseUInt8
bool parseUInt8(const std::string &string, uint8_t &value, int32_t base=10)
Interprets the contents of "string" as a unsigned integer number of type uint8_t.
Definition: parsing_utilities.cpp:146
parsing_utilities::parseInt16
int16_t parseInt16(const uint8_t *buffer)
Converts a 2-byte-buffer into a signed 16-bit integer.
Definition: parsing_utilities.cpp:90
parsing_utilities::parseDouble
double parseDouble(const uint8_t *buffer)
Converts an 8-byte-buffer into a double.
Definition: parsing_utilities.cpp:49
parsing_utilities::setVector3NaN
void setVector3NaN(Vector3Msg &v)
Definition: parsing_utilities.hpp:379
parsing_utilities::deg2rad
T deg2rad(T deg)
Definition: parsing_utilities.hpp:74


septentrio_gnss_driver
Author(s): Tibor Dome, Thomas Emter
autogenerated on Sat May 10 2025 03:03:10