YARP
Yet Another Robot Platform
yarpRosHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_ROSMSG_IMPL_HELPER_H
10 #define YARP_ROSMSG_IMPL_HELPER_H
11 
12 
13 #include <iostream>
14 #include <climits>
15 #include <cmath>
16 #include <yarp/os/Log.h>
17 
18 typedef enum
19 {
23  ROS_only
24 }
26 
27 constexpr double PI = 3.1415926535897932384626433;
28 
30 inline double convertDegreesToRadians(double degrees)
31 {
32  return degrees / 180.0 * PI;
33 }
34 
35 inline void convertDegreesToRadians(std::vector<yarp::os::NetFloat64> &degrees)
36 {
37  for(size_t i=0; i<degrees.size(); i++)
38  degrees[i] = convertDegreesToRadians(degrees[i]);
39 }
40 
41 /* return false if errors occourr, like norm of the resulting vector is not 1*/
42 inline bool convertEulerAngleYXZrads_to_quaternion(double *eulerXYZ, double *quaternion)
43 {
44  bool ret = true;
45 
46  quaternion[0] = -sin(eulerXYZ[0]/2) * sin(eulerXYZ[1]/2) * sin(eulerXYZ[2]/2) + cos(eulerXYZ[0]/2) * cos(eulerXYZ[1]/2) * cos(eulerXYZ[2]/2);
47  quaternion[1] = sin(eulerXYZ[0]/2) * cos(eulerXYZ[1]/2) * cos(eulerXYZ[2]/2) + cos(eulerXYZ[0]/2) * sin(eulerXYZ[1]/2) * sin(eulerXYZ[2]/2);
48  quaternion[2] = -sin(eulerXYZ[0]/2) * cos(eulerXYZ[1]/2) * sin(eulerXYZ[2]/2) + cos(eulerXYZ[0]/2) * sin(eulerXYZ[1]/2) * cos(eulerXYZ[2]/2);
49  quaternion[3] = sin(eulerXYZ[0]/2) * sin(eulerXYZ[1]/2) * cos(eulerXYZ[2]/2) + cos(eulerXYZ[0]/2) * cos(eulerXYZ[1]/2) * sin(eulerXYZ[2]/2);
50 
51  // verifica norma vettore
52 
53  double norma = 0;
54  for(int i=0; i<4; i++)
55  {
56  norma += quaternion[i] * quaternion[i];
57  }
58  norma = sqrt(norma);
59 
60  if((norma -1) >= 0.05)
61  {
62  yError("convertEulerAngleYXZrads_to_quaternion: Error on quaternion conversion.");
63  ret = false;
64  }
65 
66  return ret;
67 }
68 
69 inline bool convertEulerAngleYXZdegrees_to_quaternion(double *eulerXYZ, double *quaternion)
70 {
71  eulerXYZ[0] = convertDegreesToRadians(eulerXYZ[0]);
72  eulerXYZ[1] = convertDegreesToRadians(eulerXYZ[1]);
73  eulerXYZ[2] = convertDegreesToRadians(eulerXYZ[2]);
74  return convertEulerAngleYXZrads_to_quaternion(eulerXYZ, quaternion);
75 }
76 
77 #endif // YARP_ROSMSG_IMPL_HELPER_H
ROS_only
@ ROS_only
Definition: yarpRosHelper.h:23
PI
constexpr double PI
Definition: yarpRosHelper.h:27
ROS_enabled
@ ROS_enabled
Definition: yarpRosHelper.h:22
convertDegreesToRadians
double convertDegreesToRadians(double degrees)
convert degrees to radiants for ROS messages
Definition: yarpRosHelper.h:30
convertEulerAngleYXZrads_to_quaternion
bool convertEulerAngleYXZrads_to_quaternion(double *eulerXYZ, double *quaternion)
Definition: yarpRosHelper.h:42
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yError
#define yError(...)
Definition: Log.h:282
Log.h
ROSTopicUsageType
ROSTopicUsageType
Definition: yarpRosHelper.h:19
ROS_disabled
@ ROS_disabled
Definition: yarpRosHelper.h:21
convertEulerAngleYXZdegrees_to_quaternion
bool convertEulerAngleYXZdegrees_to_quaternion(double *eulerXYZ, double *quaternion)
Definition: yarpRosHelper.h:69
ROS_config_error
@ ROS_config_error
Definition: yarpRosHelper.h:20