Merge pull request #11190 from mathiasvr/pr-arraylen
[betaflight.git] / src / test / unit / telemetry_hott_unittest.cc
blobef996a38df296900c3dbc9776a2eabd9408b1d61
1 /*
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/>.
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <string.h>
22 #include <limits.h>
24 extern "C" {
25 #include "platform.h"
27 #include "build/debug.h"
29 #include "common/axis.h"
30 #include "common/gps_conversion.h"
32 #include "pg/pg.h"
33 #include "pg/pg_ids.h"
35 #include "drivers/system.h"
36 #include "drivers/serial.h"
37 #include "drivers/system.h"
39 #include "fc/runtime_config.h"
41 #include "flight/pid.h"
43 #include "io/gps.h"
44 #include "io/serial.h"
46 #include "sensors/barometer.h"
47 #include "sensors/battery.h"
48 #include "sensors/sensors.h"
50 #include "telemetry/telemetry.h"
51 #include "telemetry/hott.h"
53 PG_REGISTER(telemetryConfig_t, telemetryConfig, PG_TELEMETRY_CONFIG, 0);
54 PG_REGISTER(gpsConfig_t, gpsConfig, PG_GPS_CONFIG, 0);
56 uint16_t testBatteryVoltage = 0;
57 int32_t testAmperage = 0;
58 int32_t testMAhDrawn = 0;
62 #include "unittest_macros.h"
63 #include "gtest/gtest.h"
65 extern "C" {
66 void addGPSCoordinates(HOTT_GPS_MSG_t *hottGPSMessage, int32_t latitude, int32_t longitude);
68 // See http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
70 HOTT_GPS_MSG_t hottGPSMessage;
72 HOTT_GPS_MSG_t *getGPSMessageForTest(void)
74 memset(&hottGPSMessage, 0, sizeof(hottGPSMessage));
75 return &hottGPSMessage;
78 TEST(TelemetryHottTest, UpdateGPSCoordinates1)
80 // given
81 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
83 // Mayrhofen, Austria
84 uint32_t longitude = GPS_coord_to_degrees("4710.5186");
85 uint32_t latitude = GPS_coord_to_degrees("1151.4252");
87 // when
88 addGPSCoordinates(hottGPSMessage, latitude, longitude);
90 // then
91 EXPECT_EQ(hottGPSMessage->pos_NS, 0);
92 EXPECT_EQ(hottGPSMessage->pos_NS_dm_H << 8 | hottGPSMessage->pos_NS_dm_L, 1151);
93 EXPECT_EQ((int16_t)(hottGPSMessage->pos_NS_sec_H << 8 | hottGPSMessage->pos_NS_sec_L), 4251);
95 EXPECT_EQ(hottGPSMessage->pos_EW, 0);
96 EXPECT_EQ(hottGPSMessage->pos_EW_dm_H << 8 | hottGPSMessage->pos_EW_dm_L, 4710);
97 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_sec_H << 8 | hottGPSMessage->pos_EW_sec_L), 5186);
100 TEST(TelemetryHottTest, UpdateGPSCoordinates2)
102 // given
103 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
105 // Hampstead Heath, London
106 // 51.563886, -0.159960
107 uint32_t longitude = GPS_coord_to_degrees("5156.3886");
108 uint32_t latitude = -GPS_coord_to_degrees("015.9960");
110 // when
111 addGPSCoordinates(hottGPSMessage, longitude, latitude);
113 // then
114 EXPECT_EQ(hottGPSMessage->pos_NS, 0);
115 EXPECT_EQ(hottGPSMessage->pos_NS_dm_H << 8 | hottGPSMessage->pos_NS_dm_L, 5156);
116 EXPECT_EQ(hottGPSMessage->pos_NS_sec_H << 8 | hottGPSMessage->pos_NS_sec_L, 3886);
118 EXPECT_EQ(hottGPSMessage->pos_EW, 1);
119 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_dm_H << 8 | hottGPSMessage->pos_EW_dm_L), -15);
120 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_sec_H << 8 | hottGPSMessage->pos_EW_sec_L), -9960);
124 TEST(TelemetryHottTest, UpdateGPSCoordinates3)
126 // given
127 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
129 int32_t longitude = -GPS_coord_to_degrees("17999.9999");
130 int32_t latitude = GPS_coord_to_degrees("8999.9999");
132 // when
133 addGPSCoordinates(hottGPSMessage, longitude, latitude);
135 // then
136 EXPECT_EQ(hottGPSMessage->pos_NS, 1);
137 EXPECT_EQ((int16_t)(hottGPSMessage->pos_NS_dm_H << 8 | hottGPSMessage->pos_NS_dm_L), -18039);
138 EXPECT_EQ((int16_t)(hottGPSMessage->pos_NS_sec_H << 8 | hottGPSMessage->pos_NS_sec_L), -9999);
140 EXPECT_EQ(hottGPSMessage->pos_EW, 0);
141 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_dm_H << 8 | hottGPSMessage->pos_EW_dm_L), 9039);
142 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_sec_H << 8 | hottGPSMessage->pos_EW_sec_L), 9999);
146 TEST(TelemetryHottTest, PrepareGPSMessage_Altitude1m)
148 // given
149 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
151 stateFlags = GPS_FIX;
152 uint16_t altitudeInMeters = 1;
153 GPS_altitude = altitudeInMeters * (1 / 0.1f); // 1 = 0.1m
155 // when
156 hottPrepareGPSResponse(hottGPSMessage);
158 // then
159 EXPECT_EQ((int16_t)(hottGPSMessage->altitude_H << 8 | hottGPSMessage->altitude_L), 1 + HOTT_GPS_ALTITUDE_OFFSET);
163 // STUBS
165 extern "C" {
167 int16_t debug[DEBUG16_VALUE_COUNT];
169 uint8_t stateFlags;
171 uint16_t batteryWarningVoltage;
172 uint8_t useHottAlarmSoundPeriod (void) { return 0; }
175 gpsSolutionData_t gpsSol;
176 uint16_t GPS_distanceToHome; // distance to home point in meters
177 int16_t GPS_directionToHome; // direction to home or hol point in degrees
180 uint32_t fixedMillis = 0;
182 baro_t baro;
184 int32_t getEstimatedAltitudeCm() { return 0; }
185 int16_t getEstimatedVario() { return 0; }
187 uint32_t millis(void)
189 return fixedMillis;
192 uint32_t micros(void) { return 0; }
194 uint32_t serialRxBytesWaiting(const serialPort_t *instance)
196 UNUSED(instance);
197 return 0;
200 uint32_t serialTxBytesFree(const serialPort_t *instance)
202 UNUSED(instance);
203 return 0;
206 uint8_t serialRead(serialPort_t *instance)
208 UNUSED(instance);
209 return 0;
212 void serialWrite(serialPort_t *instance, uint8_t ch)
214 UNUSED(instance);
215 UNUSED(ch);
218 void serialSetMode(serialPort_t *instance, portMode_e mode)
220 UNUSED(instance);
221 UNUSED(mode);
224 serialPort_t *openSerialPort(serialPortIdentifier_e identifier, serialPortFunction_e functionMask, serialReceiveCallbackPtr callback, void *callbackData, uint32_t baudRate, portMode_e mode, portOptions_e options)
226 UNUSED(identifier);
227 UNUSED(functionMask);
228 UNUSED(baudRate);
229 UNUSED(callback);
230 UNUSED(callbackData);
231 UNUSED(mode);
232 UNUSED(options);
234 return NULL;
237 void closeSerialPort(serialPort_t *serialPort)
239 UNUSED(serialPort);
242 const serialPortConfig_t *findSerialPortConfig(serialPortFunction_e function)
244 UNUSED(function);
246 return NULL;
249 bool sensors(uint32_t mask)
251 UNUSED(mask);
252 return false;
255 bool telemetryDetermineEnabledState(portSharing_e)
257 return true;
260 bool telemetryIsSensorEnabled(sensor_e sensor)
262 UNUSED(sensor);
263 return true;
266 portSharing_e determinePortSharing(const serialPortConfig_t *, serialPortFunction_e)
268 return PORTSHARING_NOT_SHARED;
271 batteryState_e getBatteryState(void)
273 return BATTERY_OK;
276 batteryState_e getVoltageState(void)
278 return BATTERY_OK;
281 batteryState_e getConsumptionState(void)
283 return BATTERY_OK;
286 uint16_t getBatteryVoltage(void)
288 return testBatteryVoltage;
291 uint16_t getLegacyBatteryVoltage(void)
293 return (testBatteryVoltage + 5) / 10;
296 int32_t getAmperage(void)
298 return testAmperage;
301 int32_t getMAhDrawn(void)
303 return testMAhDrawn;