constrain fastA2I to [0-9] (vice [0-9A])
[inav.git] / src / test / unit / telemetry_hott_unittest.cc
blob88b64d9681da250d326174abf8b7a721af335948
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 "config/parameter_group.h"
33 #include "config/parameter_group_ids.h"
35 #include "drivers/serial.h"
36 #include "drivers/system.h"
38 #include "fc/runtime_config.h"
40 #include "flight/pid.h"
42 #include "io/gps.h"
43 #include "io/serial.h"
45 #include "navigation/navigation.h"
47 #include "sensors/battery.h"
48 #include "sensors/sensors.h"
50 #include "telemetry/hott.h"
51 #include "telemetry/telemetry.h"
54 PG_REGISTER(telemetryConfig_t, telemetryConfig, PG_TELEMETRY_CONFIG, 0);
56 uint16_t testBatteryVoltage = 0;
57 int16_t testAmperage = 0;
58 int32_t testMAhDrawn = 0;
61 #include "unittest_macros.h"
62 #include "gtest/gtest.h"
64 extern "C" {
65 void addGPSCoordinates(HOTT_GPS_MSG_t *hottGPSMessage, int32_t latitude, int32_t longitude);
67 // See http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
69 HOTT_GPS_MSG_t hottGPSMessage;
71 HOTT_GPS_MSG_t *getGPSMessageForTest(void)
73 memset(&hottGPSMessage, 0, sizeof(hottGPSMessage));
74 return &hottGPSMessage;
77 TEST(TelemetryHottTest, UpdateGPSCoordinates1)
79 // given
80 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
82 // Mayrhofen, Austria
83 uint32_t longitude = GPS_coord_to_degrees("4710.5186");
84 uint32_t latitude = GPS_coord_to_degrees("1151.4252");
86 // when
87 addGPSCoordinates(hottGPSMessage, latitude, longitude);
89 // then
90 EXPECT_EQ(hottGPSMessage->pos_NS, 0);
91 EXPECT_EQ(hottGPSMessage->pos_NS_dm_H << 8 | hottGPSMessage->pos_NS_dm_L, 1151);
92 EXPECT_EQ((int16_t)(hottGPSMessage->pos_NS_sec_H << 8 | hottGPSMessage->pos_NS_sec_L), 4251);
94 EXPECT_EQ(hottGPSMessage->pos_EW, 0);
95 EXPECT_EQ(hottGPSMessage->pos_EW_dm_H << 8 | hottGPSMessage->pos_EW_dm_L, 4710);
96 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_sec_H << 8 | hottGPSMessage->pos_EW_sec_L), 5186);
99 TEST(TelemetryHottTest, UpdateGPSCoordinates2)
101 // given
102 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
104 // Hampstead Heath, London
105 // 51.563886, -0.159960
106 uint32_t longitude = GPS_coord_to_degrees("5156.3886");
107 uint32_t latitude = -GPS_coord_to_degrees("015.9960");
109 // when
110 addGPSCoordinates(hottGPSMessage, longitude, latitude);
112 // then
113 EXPECT_EQ(hottGPSMessage->pos_NS, 0);
114 EXPECT_EQ(hottGPSMessage->pos_NS_dm_H << 8 | hottGPSMessage->pos_NS_dm_L, 5156);
115 EXPECT_EQ(hottGPSMessage->pos_NS_sec_H << 8 | hottGPSMessage->pos_NS_sec_L, 3886);
117 EXPECT_EQ(hottGPSMessage->pos_EW, 1);
118 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_dm_H << 8 | hottGPSMessage->pos_EW_dm_L), -15);
119 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_sec_H << 8 | hottGPSMessage->pos_EW_sec_L), -9960);
123 TEST(TelemetryHottTest, UpdateGPSCoordinates3)
125 // given
126 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
128 int32_t longitude = -GPS_coord_to_degrees("17999.9999");
129 int32_t latitude = GPS_coord_to_degrees("8999.9999");
131 // when
132 addGPSCoordinates(hottGPSMessage, longitude, latitude);
134 // then
135 EXPECT_EQ(hottGPSMessage->pos_NS, 1);
136 EXPECT_EQ((int16_t)(hottGPSMessage->pos_NS_dm_H << 8 | hottGPSMessage->pos_NS_dm_L), -18039);
137 EXPECT_EQ((int16_t)(hottGPSMessage->pos_NS_sec_H << 8 | hottGPSMessage->pos_NS_sec_L), -9999);
139 EXPECT_EQ(hottGPSMessage->pos_EW, 0);
140 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_dm_H << 8 | hottGPSMessage->pos_EW_dm_L), 9039);
141 EXPECT_EQ((int16_t)(hottGPSMessage->pos_EW_sec_H << 8 | hottGPSMessage->pos_EW_sec_L), 9999);
145 TEST(TelemetryHottTest, PrepareGPSMessage_Altitude1m)
147 // given
148 HOTT_GPS_MSG_t *hottGPSMessage = getGPSMessageForTest();
150 stateFlags = GPS_FIX;
151 uint16_t altitudeInMeters = 1;
152 //!!GPS_altitude = altitudeInMeters * (1 / 0.1f); // 1 = 0.1m
154 // when
155 hottPrepareGPSResponse(hottGPSMessage);
157 // then
158 EXPECT_EQ((int16_t)(hottGPSMessage->altitude_H << 8 | hottGPSMessage->altitude_L), 1 + HOTT_GPS_ALTITUDE_OFFSET);
162 // STUBS
164 extern "C" {
166 int32_t debug[DEBUG32_VALUE_COUNT];
168 uint32_t stateFlags;
170 uint16_t batteryWarningVoltage;
171 uint8_t useHottAlarmSoundPeriod (void) { return 0; }
173 gpsSolutionData_t gpsSol;
174 uint8_t GPS_numSat;
175 int32_t GPS_coord[2];
176 uint16_t GPS_speed; // speed in 0.1m/s
177 uint32_t GPS_distanceToHome; // distance to home point in meters
178 uint16_t GPS_altitude; // altitude in 0.1m
179 uint16_t vbat;
180 int16_t GPS_directionToHome; // direction to home or hol point in degrees
182 int32_t amperage;
183 int32_t mAhDrawn;
185 uint32_t fixedMillis = 0;
187 uint32_t millis(void) {
188 return fixedMillis;
191 uint32_t micros(void) { return 0; }
193 uint32_t serialRxBytesWaiting(const serialPort_t *instance) {
194 UNUSED(instance);
195 return 0;
198 uint32_t serialTxBytesFree(const serialPort_t *instance) {
199 UNUSED(instance);
200 return 0;
203 uint8_t serialRead(serialPort_t *instance) {
204 UNUSED(instance);
205 return 0;
208 void serialWrite(serialPort_t *instance, uint8_t ch) {
209 UNUSED(instance);
210 UNUSED(ch);
213 void serialSetMode(serialPort_t *instance, portMode_t mode) {
214 UNUSED(instance);
215 UNUSED(mode);
219 serialPort_t *openSerialPort(serialPortIdentifier_e identifier, serialPortFunction_e function,
220 serialReceiveCallbackPtr rxCallback, void *rxCallbackData,
221 uint32_t baudRate, portMode_t mode, portOptions_t options)
223 UNUSED(identifier);
224 UNUSED(function);
225 UNUSED(rxCallback);
226 UNUSED(rxCallbackData);
227 UNUSED(baudRate);
228 UNUSED(mode);
229 UNUSED(options);
231 return NULL;
234 void closeSerialPort(serialPort_t *serialPort) {
235 UNUSED(serialPort);
238 serialPortConfig_t *findSerialPortConfig(serialPortFunction_e function) {
239 UNUSED(function);
241 return NULL;
244 bool sensors(uint32_t mask) {
245 UNUSED(mask);
246 return false;
249 bool telemetryDetermineEnabledState(portSharing_e) {
250 return true;
253 portSharing_e determinePortSharing(const serialPortConfig_t *, serialPortFunction_e) {
254 return PORTSHARING_NOT_SHARED;
257 batteryState_e getBatteryState(void) {
258 return BATTERY_OK;
261 float getEstimatedActualPosition(int) {
262 return 0.0f;
265 float getEstimatedActualVelocity(int) {
266 return 0.0f;
269 uint16_t getBatteryVoltage(void) {
270 return testBatteryVoltage;
273 int16_t getAmperage(void) {
274 return testAmperage;
277 int32_t getMAhDrawn(void) {
278 return testMAhDrawn;