Refactor Baro to floats, filter at position rate
[betaflight.git] / src / test / unit / vtx_unittest.cc
blobf9f3311f89872c00255b997de6c90f22e864ef7b
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 <stdint.h>
20 extern "C" {
21 #include "blackbox/blackbox.h"
22 #include "build/debug.h"
23 #include "common/maths.h"
24 #include "common/streambuf.h"
25 #include "config/feature.h"
26 #include "config/config.h"
27 #include "fc/controlrate_profile.h"
28 #include "fc/core.h"
29 #include "fc/rc_controls.h"
30 #include "fc/rc_modes.h"
31 #include "fc/runtime_config.h"
32 #include "flight/failsafe.h"
33 #include "flight/imu.h"
34 #include "flight/mixer.h"
35 #include "flight/pid.h"
36 #include "flight/servos.h"
37 #include "io/beeper.h"
38 #include "io/gps.h"
39 #include "io/vtx.h"
40 #include "pg/motor.h"
41 #include "pg/pg.h"
42 #include "pg/pg_ids.h"
43 #include "pg/rx.h"
44 #include "rx/rx.h"
45 #include "scheduler/scheduler.h"
46 #include "sensors/acceleration.h"
47 #include "sensors/gyro.h"
48 #include "telemetry/telemetry.h"
50 vtxSettingsConfig_t vtxGetSettings(void);
52 PG_REGISTER(accelerometerConfig_t, accelerometerConfig, PG_ACCELEROMETER_CONFIG, 0);
53 PG_REGISTER(blackboxConfig_t, blackboxConfig, PG_BLACKBOX_CONFIG, 0);
54 PG_REGISTER(gyroConfig_t, gyroConfig, PG_GYRO_CONFIG, 0);
55 PG_REGISTER(mixerConfig_t, mixerConfig, PG_MIXER_CONFIG, 0);
56 PG_REGISTER(pidConfig_t, pidConfig, PG_PID_CONFIG, 0);
57 PG_REGISTER(rxConfig_t, rxConfig, PG_RX_CONFIG, 0);
58 PG_REGISTER(servoConfig_t, servoConfig, PG_SERVO_CONFIG, 0);
59 PG_REGISTER(systemConfig_t, systemConfig, PG_SYSTEM_CONFIG, 0);
60 PG_REGISTER(telemetryConfig_t, telemetryConfig, PG_TELEMETRY_CONFIG, 0);
61 PG_REGISTER(failsafeConfig_t, failsafeConfig, PG_FAILSAFE_CONFIG, 0);
62 PG_REGISTER(motorConfig_t, motorConfig, PG_MOTOR_CONFIG, 0);
63 PG_REGISTER(imuConfig_t, imuConfig, PG_IMU_CONFIG, 0);
65 float rcCommand[4];
66 float rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT];
67 uint16_t averageSystemLoadPercent = 0;
68 uint8_t cliMode = 0;
69 uint8_t debugMode = 0;
70 int16_t debug[DEBUG16_VALUE_COUNT];
71 pidProfile_t *currentPidProfile;
72 controlRateConfig_t *currentControlRateProfile;
73 attitudeEulerAngles_t attitude;
74 gpsSolutionData_t gpsSol;
75 uint32_t targetPidLooptime;
76 bool cmsInMenu = false;
77 float axisPID_P[3], axisPID_I[3], axisPID_D[3], axisPIDSum[3];
78 rxRuntimeState_t rxRuntimeState = {};
79 acc_t acc;
82 uint32_t simulationFeatureFlags = 0;
83 uint32_t simulationTime = 0;
84 bool gyroCalibDone = false;
85 bool simulationHaveRx = false;
87 #include "gtest/gtest.h"
89 TEST(VtxTest, PitMode)
91 // given
92 modeActivationConditionsMutable(0)->auxChannelIndex = 0;
93 modeActivationConditionsMutable(0)->modeId = BOXVTXPITMODE;
94 modeActivationConditionsMutable(0)->range.startStep = CHANNEL_VALUE_TO_STEP(1750);
95 modeActivationConditionsMutable(0)->range.endStep = CHANNEL_VALUE_TO_STEP(CHANNEL_RANGE_MAX);
97 analyzeModeActivationConditions();
99 // and
100 vtxSettingsConfigMutable()->band = 0;
101 vtxSettingsConfigMutable()->freq = 5800;
102 vtxSettingsConfigMutable()->pitModeFreq = 5300;
104 // expect
105 EXPECT_EQ(5800, vtxGetSettings().freq);
107 // and
108 // enable vtx pit mode
109 rcData[AUX1] = 1800;
111 // when
112 updateActivatedModes();
114 // expect
115 EXPECT_TRUE(IS_RC_MODE_ACTIVE(BOXVTXPITMODE));
116 EXPECT_EQ(5300, vtxGetSettings().freq);
119 // STUBS
120 extern "C" {
121 uint8_t activePidLoopDenom = 1;
122 uint32_t micros(void) { return simulationTime; }
123 uint32_t millis(void) { return micros() / 1000; }
124 bool rxIsReceivingSignal(void) { return simulationHaveRx; }
126 bool featureIsEnabled(uint32_t f) { return simulationFeatureFlags & f; }
127 void warningLedFlash(void) {}
128 void warningLedDisable(void) {}
129 void warningLedUpdate(void) {}
130 void beeper(beeperMode_e) {}
131 void beeperConfirmationBeeps(uint8_t) {}
132 void beeperWarningBeeps(uint8_t) {}
133 void beeperSilence(void) {}
134 void systemBeep(bool) {}
135 void saveConfigAndNotify(void) {}
136 void blackboxFinish(void) {}
137 bool accIsCalibrationComplete(void) { return true; }
138 bool accHasBeenCalibrated(void) { return true; }
139 bool baroIsCalibrated(void) { return true; }
140 bool gyroIsCalibrationComplete(void) { return gyroCalibDone; }
141 void gyroStartCalibration(bool) {}
142 bool isFirstArmingGyroCalibrationRunning(void) { return false; }
143 void pidController(const pidProfile_t *, timeUs_t) {}
144 void pidStabilisationState(pidStabilisationState_e) {}
145 void mixTable(timeUs_t) {};
146 void writeMotors(void) {};
147 void writeServos(void) {};
148 bool calculateRxChannelsAndUpdateFailsafe(timeUs_t) { return true; }
149 bool isMixerUsingServos(void) { return false; }
150 void gyroUpdate() {}
151 timeDelta_t getTaskDeltaTimeUs(taskId_e) { return 0; }
152 void updateRSSI(timeUs_t) {}
153 bool failsafeIsMonitoring(void) { return false; }
154 void failsafeStartMonitoring(void) {}
155 void failsafeUpdateState(void) {}
156 bool failsafeIsActive(void) { return false; }
157 bool rxAreFlightChannelsValid(void) { return false; }
158 bool failsafeIsReceivingRxData(void) { return false; }
159 void pidResetIterm(void) {}
160 void updateAdjustmentStates(void) {}
161 void processRcAdjustments(controlRateConfig_t *) {}
162 void updateGpsWaypointsAndMode(void) {}
163 void mspSerialReleaseSharedTelemetryPorts(void) {}
164 void telemetryCheckState(void) {}
165 void mspSerialAllocatePorts(void) {}
166 void gyroReadTemperature(void) {}
167 void updateRcCommands(void) {}
168 void applyAltHold(void) {}
169 void resetYawAxis(void) {}
170 int16_t calculateThrottleAngleCorrection(uint8_t) { return 0; }
171 void processRcCommand(void) {}
172 void updateGpsStateForHomeAndHoldMode(void) {}
173 void blackboxUpdate(timeUs_t) {}
174 void transponderUpdate(timeUs_t) {}
175 void GPS_reset_home_position(void) {}
176 void accStartCalibration(void) {}
177 void baroSetGroundLevel(void) {}
178 void changePidProfile(uint8_t) {}
179 void changeControlRateProfile(uint8_t) {}
180 void dashboardEnablePageCycling(void) {}
181 void dashboardDisablePageCycling(void) {}
182 bool imuQuaternionHeadfreeOffsetSet(void) { return true; }
183 void rescheduleTask(taskId_e, timeDelta_t) {}
184 bool usbCableIsInserted(void) { return false; }
185 bool usbVcpIsConnected(void) { return false; }
186 void pidSetAntiGravityState(bool newState) { UNUSED(newState); }
187 void osdSuppressStats(bool) {}
188 void pidSetItermReset(bool) {}
189 void applyAccelerometerTrimsDelta(rollAndPitchTrims_t*) {}
190 bool isFixedWing(void) { return false; }
191 void compassStartCalibration(void) {}
192 bool compassIsCalibrationComplete(void) { return true; }
193 bool isUpright(void) { return true; }
194 void blackboxLogEvent(FlightLogEvent, union flightLogEventData_u *) {};
195 void gyroFiltering(timeUs_t) {};
196 timeDelta_t rxGetFrameDelta(timeDelta_t *) { return 0; }
197 void updateRcRefreshRate(timeUs_t) {};
198 uint16_t getAverageSystemLoadPercent(void) { return 0; }
199 bool isMotorProtocolEnabled(void) { return false; }
200 void pinioBoxTaskControl(void) {}
201 void sbufWriteU8(sbuf_t *, uint8_t) {}
202 void sbufWriteU16(sbuf_t *, uint16_t) {}
203 void sbufWriteU32(sbuf_t *, uint32_t) {}
204 void schedulerSetNextStateTime(timeDelta_t) {}