2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
27 #include "common/axis.h"
29 #include "drivers/system.h"
30 #include "drivers/serial.h"
32 #include "sensors/sensors.h"
33 #include "sensors/battery.h"
35 #include "io/serial.h"
38 #include "telemetry/telemetry.h"
39 #include "telemetry/hott.h"
41 #include "flight/pid.h"
42 #include "flight/gps_conversion.h"
44 #include "config/runtime_config.h"
47 #include "unittest_macros.h"
48 #include "gtest/gtest.h"
51 void addGPSCoordinates(HOTT_GPS_MSG_t
*hottGPSMessage
, int32_t latitude
, int32_t longitude
);
53 // See http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
55 HOTT_GPS_MSG_t hottGPSMessage
;
57 HOTT_GPS_MSG_t
*getGPSMessageForTest(void)
59 memset(&hottGPSMessage
, 0, sizeof(hottGPSMessage
));
60 return &hottGPSMessage
;
63 TEST(TelemetryHottTest
, UpdateGPSCoordinates1
)
66 HOTT_GPS_MSG_t
*hottGPSMessage
= getGPSMessageForTest();
69 uint32_t longitude
= GPS_coord_to_degrees("4710.5186");
70 uint32_t latitude
= GPS_coord_to_degrees("1151.4252");
73 addGPSCoordinates(hottGPSMessage
, latitude
, longitude
);
76 EXPECT_EQ(hottGPSMessage
->pos_NS
, 0);
77 EXPECT_EQ(hottGPSMessage
->pos_NS_dm_H
<< 8 | hottGPSMessage
->pos_NS_dm_L
, 1151);
78 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_NS_sec_H
<< 8 | hottGPSMessage
->pos_NS_sec_L
), 4251);
80 EXPECT_EQ(hottGPSMessage
->pos_EW
, 0);
81 EXPECT_EQ(hottGPSMessage
->pos_EW_dm_H
<< 8 | hottGPSMessage
->pos_EW_dm_L
, 4710);
82 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_EW_sec_H
<< 8 | hottGPSMessage
->pos_EW_sec_L
), 5186);
85 TEST(TelemetryHottTest
, UpdateGPSCoordinates2
)
88 HOTT_GPS_MSG_t
*hottGPSMessage
= getGPSMessageForTest();
90 // Hampstead Heath, London
91 // 51.563886, -0.159960
92 uint32_t longitude
= GPS_coord_to_degrees("5156.3886");
93 uint32_t latitude
= -GPS_coord_to_degrees("015.9960");
96 addGPSCoordinates(hottGPSMessage
, longitude
, latitude
);
99 EXPECT_EQ(hottGPSMessage
->pos_NS
, 0);
100 EXPECT_EQ(hottGPSMessage
->pos_NS_dm_H
<< 8 | hottGPSMessage
->pos_NS_dm_L
, 5156);
101 EXPECT_EQ(hottGPSMessage
->pos_NS_sec_H
<< 8 | hottGPSMessage
->pos_NS_sec_L
, 3886);
103 EXPECT_EQ(hottGPSMessage
->pos_EW
, 1);
104 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_EW_dm_H
<< 8 | hottGPSMessage
->pos_EW_dm_L
), -15);
105 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_EW_sec_H
<< 8 | hottGPSMessage
->pos_EW_sec_L
), -9960);
109 TEST(TelemetryHottTest
, UpdateGPSCoordinates3
)
112 HOTT_GPS_MSG_t
*hottGPSMessage
= getGPSMessageForTest();
114 int32_t longitude
= -GPS_coord_to_degrees("17999.9999");
115 int32_t latitude
= GPS_coord_to_degrees("8999.9999");
118 addGPSCoordinates(hottGPSMessage
, longitude
, latitude
);
121 EXPECT_EQ(hottGPSMessage
->pos_NS
, 1);
122 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_NS_dm_H
<< 8 | hottGPSMessage
->pos_NS_dm_L
), -18039);
123 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_NS_sec_H
<< 8 | hottGPSMessage
->pos_NS_sec_L
), -9999);
125 EXPECT_EQ(hottGPSMessage
->pos_EW
, 0);
126 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_EW_dm_H
<< 8 | hottGPSMessage
->pos_EW_dm_L
), 9039);
127 EXPECT_EQ((int16_t)(hottGPSMessage
->pos_EW_sec_H
<< 8 | hottGPSMessage
->pos_EW_sec_L
), 9999);
130 TEST(TelemetryHottTest
, PrepareGPSMessage_Altitude1m
)
133 HOTT_GPS_MSG_t
*hottGPSMessage
= getGPSMessageForTest();
135 stateFlags
= GPS_FIX
;
136 uint16_t altitudeInMeters
= 1;
137 GPS_altitude
= altitudeInMeters
* (1 / 0.1f
); // 1 = 0.1m
140 hottPrepareGPSResponse(hottGPSMessage
);
143 EXPECT_EQ((int16_t)(hottGPSMessage
->altitude_H
<< 8 | hottGPSMessage
->altitude_L
), 1 + HOTT_GPS_ALTITUDE_OFFSET
);
157 int32_t GPS_coord
[2];
158 uint16_t GPS_speed
; // speed in 0.1m/s
159 uint16_t GPS_distanceToHome
; // distance to home point in meters
160 uint16_t GPS_altitude
; // altitude in 0.1m
162 int16_t GPS_directionToHome
; // direction to home or hol point in degrees
167 uint32_t micros(void) { return 0; }
169 uint8_t serialTotalBytesWaiting(serialPort_t
*instance
) {
174 uint8_t serialRead(serialPort_t
*instance
) {
179 void serialWrite(serialPort_t
*instance
, uint8_t ch
) {
184 void serialSetMode(serialPort_t
*instance
, portMode_t mode
) {
189 void serialSetBaudRate(serialPort_t
*instance
, uint32_t baudRate
) {
194 void beginSerialPortFunction(serialPort_t
*port
, serialPortFunction_e function
) {
199 void endSerialPortFunction(serialPort_t
*port
, serialPortFunction_e function
) {
204 serialPort_t
*openSerialPort(serialPortFunction_e functionMask
, serialReceiveCallbackPtr callback
, uint32_t baudRate
, portMode_t mode
, serialInversion_e inversion
) {
205 UNUSED(functionMask
);
214 serialPort_t
*findOpenSerialPort(uint16_t functionMask
) {
215 UNUSED(functionMask
);
219 bool sensors(uint32_t mask
) {