dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
unittest.cpp
Go to the documentation of this file.
1 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, see <http://www.gnu.org/licenses/>
24  */
25 
26 /*
27  * NOTE: This program uses the Google Test infrastructure to drive the unit test
28  *
29  * Main site for Google Test: http://code.google.com/p/googletest/
30  * Documentation and examples: http://code.google.com/p/googletest/wiki/Documentation
31  */
32 
33 #include "gtest/gtest.h"
34 
35 #include <stdio.h> /* printf */
36 #include <stdlib.h> /* abort */
37 #include <string.h> /* memset */
38 #include <stdint.h> /* uint*_t */
39 
40 extern "C" {
41 
42 #include "coordinate_conversions.h" /* API for coordinate_conversions functions */
43 
44 }
45 
46 #include <math.h> /* fabs() */
47 
48 // To use a test fixture, derive a class from testing::Test.
49 class CoordConversion : public testing::Test {
50 protected:
51  virtual void SetUp() {
52  }
53 
54  virtual void TearDown() {
55  }
56 };
57 
58 // Test fixture for Rne From LLA, minimal
60 protected:
61  virtual void SetUp() {
62  }
63 
64  virtual void TearDown() {
65  }
66 };
67 
69  float LLA[] = { 0, 0, 0 };
70  float Rne[3][3];
71 
72  RneFromLLA(LLA, Rne);
73 
74  float eps = 0.000001f;
75  ASSERT_NEAR(0, Rne[0][0], eps);
76  ASSERT_NEAR(0, Rne[0][1], eps);
77  ASSERT_NEAR(1, Rne[0][2], eps);
78 
79  ASSERT_NEAR(0, Rne[1][0], eps);
80  ASSERT_NEAR(1, Rne[1][1], eps);
81  ASSERT_NEAR(0, Rne[1][2], eps);
82 
83  ASSERT_NEAR(-1, Rne[2][0], eps);
84  ASSERT_NEAR(0, Rne[2][1], eps);
85  ASSERT_NEAR(0, Rne[2][2], eps);
86 };
87 
88 class QuatRPYTest : public CoordConversion {
89  virtual void SetUp() {
90  }
91 
92  virtual void TearDown() {
93  }
94 };
95 
96 TEST_F(QuatRPYTest, BackAndForth) {
97  float eps = 0.8f;
98 
99  float quat[4];
100  float rpy[3];
101  float rpy_out[3];
102 
103  rpy[0] = 1; rpy[1] = 1; rpy[2] = 1;
104 
105  RPY2Quaternion(rpy, quat);
106  Quaternion2RPY(quat, rpy_out);
107 
108  for (int i = 0; i < 3; i++) {
109  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
110  }
111 
112  rpy[0] = 179; rpy[1] = 1; rpy[2] = 1;
113 
114  RPY2Quaternion(rpy, quat);
115  Quaternion2RPY(quat, rpy_out);
116 
117  for (int i = 0; i < 3; i++) {
118  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
119  }
120 
121  rpy[0] = 40; rpy[1] = 89; rpy[2] = 0;
122 
123  RPY2Quaternion(rpy, quat);
124  Quaternion2RPY(quat, rpy_out);
125 
126  for (int i = 0; i < 3; i++) {
127  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
128  }
129 
130  rpy[0] = 45; rpy[1] = 89; rpy[2] = 0;
131 
132  RPY2Quaternion(rpy, quat);
133  Quaternion2RPY(quat, rpy_out);
134 
135  for (int i = 0; i < 3; i++) {
136  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
137  }
138 
139  rpy[0] = -179; rpy[1] = -89; rpy[2] = 0;
140 
141  RPY2Quaternion(rpy, quat);
142  Quaternion2RPY(quat, rpy_out);
143 
144  for (int i = 0; i < 3; i++) {
145  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
146  }
147 
148  rpy[0] = 0; rpy[1] = -90; rpy[2] = 0;
149 
150  RPY2Quaternion(rpy, quat);
151  Quaternion2RPY(quat, rpy_out);
152 
153  for (int i = 0; i < 3; i++) {
154  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
155  }
156 
157  rpy[0] = 90; rpy[1] = -90; rpy[2] = 0;
158 
159  RPY2Quaternion(rpy, quat);
160  Quaternion2RPY(quat, rpy_out);
161 
162  for (int i = 0; i < 3; i++) {
163  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
164  }
165 
166  rpy[0] = -130; rpy[1] = -90; rpy[2] = 90;
167 
168  RPY2Quaternion(rpy, quat);
169  Quaternion2RPY(quat, rpy_out);
170 
171  ASSERT_NEAR(rpy[1], rpy_out[1], eps);
172 
173  for (float f = -179.5; f < 179.5f; f += 0.1f) {
174  rpy[0] = f; rpy[1] = -90; rpy[2] = 0;
175 
176  RPY2Quaternion(rpy, quat);
177  Quaternion2RPY(quat, rpy_out);
178 
179  for (int i = 0; i < 3; i++) {
180  ASSERT_NEAR(rpy[i], rpy_out[i], eps);
181  }
182  }
183 }
virtual void TearDown()
Definition: unittest.cpp:54
TEST_F(RneFromLLATest, Equator)
Definition: unittest.cpp:68
void RPY2Quaternion(const float rpy[3], float q[4])
void RneFromLLA(float LLA[3], float Rne[3][3])
virtual void SetUp()
Definition: unittest.cpp:89
virtual void TearDown()
Definition: unittest.cpp:92
uint8_t i
Definition: msp_messages.h:97
Header for Coordinate conversions library in coordinate_conversions.c.
tuple f
Definition: px_mkfw.py:81
virtual void SetUp()
Definition: unittest.cpp:61
virtual void SetUp()
Definition: unittest.cpp:51
virtual void TearDown()
Definition: unittest.cpp:64
void Quaternion2RPY(const float q[4], float rpy[3])