Bump clang version to 18 (#14116)
[betaflight.git] / src / test / unit / althold_unittest.cc
blob4dbba143bb9f95b4271f78ac42ec269863441eb4
1 /*
2 * This file is part of Betaflight.
4 * Betaflight 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 * Betaflight 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 Betaflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <limits.h>
22 extern "C" {
24 #include "platform.h"
25 #include "build/debug.h"
26 #include "pg/pg_ids.h"
28 #include "common/filter.h"
29 #include "common/vector.h"
31 #include "fc/core.h"
32 #include "fc/rc_controls.h"
33 #include "fc/runtime_config.h"
35 #include "flight/alt_hold.h"
36 #include "flight/failsafe.h"
37 #include "flight/imu.h"
38 #include "flight/pid.h"
39 #include "flight/position.h"
41 #include "io/gps.h"
43 #include "rx/rx.h"
45 #include "pg/alt_hold.h"
46 #include "pg/autopilot.h"
48 #include "sensors/acceleration.h"
49 #include "sensors/gyro.h"
51 PG_REGISTER(accelerometerConfig_t, accelerometerConfig, PG_ACCELEROMETER_CONFIG, 0);
52 PG_REGISTER(altHoldConfig_t, altHoldConfig, PG_ALTHOLD_CONFIG, 0);
53 PG_REGISTER(apConfig_t, apConfig, PG_AUTOPILOT, 0);
54 PG_REGISTER(gyroConfig_t, gyroConfig, PG_GYRO_CONFIG, 0);
55 PG_REGISTER(positionConfig_t, positionConfig, PG_POSITION, 0);
56 PG_REGISTER(rcControlsConfig_t, rcControlsConfig, PG_RC_CONTROLS_CONFIG, 0);
58 bool failsafeIsActive(void) { return false; }
59 timeUs_t currentTimeUs = 0;
60 bool isAltHoldActive();
63 #include "unittest_macros.h"
64 #include "gtest/gtest.h"
66 uint32_t millisRW;
67 uint32_t millis() {
68 return millisRW;
71 TEST(AltholdUnittest, altHoldTransitionsTest)
73 updateAltHold(currentTimeUs);
74 EXPECT_EQ(isAltHoldActive(), false);
76 flightModeFlags |= ALT_HOLD_MODE;
77 millisRW = 42;
78 updateAltHold(currentTimeUs);
79 EXPECT_EQ(isAltHoldActive(), true);
81 flightModeFlags ^= ALT_HOLD_MODE;
82 millisRW = 56;
83 updateAltHold(currentTimeUs);
84 EXPECT_EQ(isAltHoldActive(), false);
86 flightModeFlags |= ALT_HOLD_MODE;
87 millisRW = 64;
88 updateAltHold(currentTimeUs);
89 EXPECT_EQ(isAltHoldActive(), true);
92 TEST(AltholdUnittest, altHoldTransitionsTestUnfinishedExitEnter)
94 altHoldInit();
95 EXPECT_EQ(isAltHoldActive(), false);
97 flightModeFlags |= ALT_HOLD_MODE;
98 millisRW = 42;
99 updateAltHold(currentTimeUs);
100 EXPECT_EQ(isAltHoldActive(), true);
103 // STUBS
105 extern "C" {
106 uint8_t armingFlags = 0;
107 int16_t debug[DEBUG16_VALUE_COUNT];
108 uint8_t debugMode;
109 uint16_t flightModeFlags = 0;
110 uint8_t stateFlags = 0;
112 acc_t acc;
113 attitudeEulerAngles_t attitude;
114 gpsSolutionData_t gpsSol;
116 float getAltitudeCm(void) { return 0.0f; }
117 float getAltitudeDerivative(void) { return 0.0f; }
118 float getCosTiltAngle(void) { return 0.0f; }
119 float getGpsDataIntervalSeconds(void) { return 0.01f; }
120 float getGpsDataFrequencyHz(void) { return 10.0f; }
121 float rcCommand[4];
123 bool gpsHasNewData(uint16_t* gpsStamp) {
124 UNUSED(*gpsStamp);
125 return true;
128 void GPS_distance2d(const gpsLocation_t* /*from*/, const gpsLocation_t* /*to*/, vector2_t* /*dest*/) { }
130 void parseRcChannels(const char *input, rxConfig_t *rxConfig) {
131 UNUSED(input);
132 UNUSED(rxConfig);
135 throttleStatus_e calculateThrottleStatus() {
136 return THROTTLE_LOW;