quat_test.cpp
Go to the documentation of this file.
1 //
2 // Overview for using quaternion and relationship to roll, pitch, yaw
3 //
4 
5 #include <stdio.h>
6 #include <tf2/LinearMath/Quaternion.h>
7 #include <tf2/LinearMath/Matrix3x3.h>
8 
9 #define _USE_MATH_DEFINES
10 
11 #include <cmath>
12 #include <iostream>
13 
14 // see
15 // [2] https://quaternions.online/
16 
17 
18 int main(int argc, char *argv[])
19 {
20  double roll, pitch, yaw;
21 
22  roll = 0.1; //
23  pitch = 0.2;
24  yaw = 0.3;
25 
26  printf("Quaternion example program\n");
27 
28 
29  tf2::Quaternion myQuaternion;
30 
31  myQuaternion.setRPY(roll, pitch, yaw);
32 
33  char quatOrder[] = "xyzw";
34  for (int i = 0; i < 4; i++)
35  {
36  printf("%c-Quat[%d]: %8.6lf\n", quatOrder[i], i, myQuaternion[i]);
37  }
38 
39  tf2::Matrix3x3(myQuaternion).getRPY(roll, pitch, yaw);
40  for (int i = 0; i < 4; i++)
41  {
42  printf("%c-Quat[%d]: %8.6lf\n", quatOrder[i], i, myQuaternion[i]);
43  }
44 
45 
46  const char *axisDescr[] = {"roll", "pitch", "yaw"};
47 
48  tf2::Matrix3x3 m(myQuaternion);
49 
50  m.getRPY(roll, pitch, yaw);
51 
52 
53  printf("Rotationsmatrix\n");
54 
55  for (int i = 0; i < 3; i++)
56  {
57  for (int j = 0; j < 3; j++)
58  {
59 
60  printf("%10.3lf", m[i][j]);
61  // printf(m)
62  }
63  printf("\n");
64  }
65 
66  // manual construction
67 
68  tf2::Matrix3x3 rotz;
69  tf2::Matrix3x3 roty;
70  tf2::Matrix3x3 rotx;
71 
72  tf2::Matrix3x3 rot;
73 
74  for (int i = 0; i < 3; i++)
75  for (int j = 0; j < 3; j++)
76  {
77  rotz[i][j] = 0.0;
78  roty[i][j] = 0.0;
79  rotx[i][j] = 0.0;
80  }
81 
82  rotx[0][0] = 1.0;
83  roty[1][1] = 1.0;
84  rotz[2][2] = 1.0;
85 
86  rotz[0][0] = cos(yaw);
87  rotz[1][1] = cos(yaw);
88  rotz[0][1] = -sin(yaw);
89  rotz[1][0] = sin(yaw);
90 
91  roty[0][0] = cos(pitch);
92  roty[2][2] = cos(pitch);
93  roty[0][2] = sin(pitch);
94  roty[2][0] = -sin(pitch);
95 
96  rotx[1][1] = cos(roll);
97  rotx[2][2] = cos(roll);
98  rotx[1][2] = -sin(roll);
99  rotx[2][1] = sin(roll);
100 
101  rot = rotz * roty * rotx;
102 
103  const char* reflArr[] = {"rotz","roty","rotx","rot_result"};
104  for (int loop = 0; loop < 4; loop++)
105  {
106  tf2::Matrix3x3 *rotptr = NULL;
107  switch (loop)
108  {
109  case 0:
110  rotptr = &rotz;
111  break;
112  case 1:
113  rotptr = &roty;
114  break;
115  case 2:
116  rotptr = &rotx;
117  break;
118  case 3:
119  rotptr = &rot;
120 
121  }
122  printf("\nName: %s\n==========================\n", reflArr[loop]);
123  for (int i = 0; i < 3; i++)
124  {
125  for (int j = 0; j < 3; j++)
126  {
127 
128  printf("%10.3lf", (*rotptr)[i][j]);
129  // printf(m)
130  }
131  printf("\n");
132  }
133  }
134 
135  exit(0);
136 }
NULL
#define NULL
tf2::Quaternion::setRPY
void setRPY(const tf2Scalar &roll, const tf2Scalar &pitch, const tf2Scalar &yaw)
main
int main(int argc, char *argv[])
Definition: quat_test.cpp:18
tf2::Matrix3x3::getRPY
void getRPY(tf2Scalar &roll, tf2Scalar &pitch, tf2Scalar &yaw, unsigned int solution_number=1) const
tf2::Quaternion
The Quaternion implements quaternion to perform linear algebra rotations in combination with Matrix3x...
Definition: melodic/include/tf2/LinearMath/Quaternion.h:29
tf2::Matrix3x3
The Matrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with Q...
Definition: Matrix3x3.h:32


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:10