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/>.
23 //#define DEBUG_ALTITUDE_HOLD
30 #include "common/axis.h"
31 #include "common/maths.h"
33 #include "drivers/sensor.h"
34 #include "drivers/accgyro.h"
36 #include "sensors/sensors.h"
37 #include "sensors/acceleration.h"
38 #include "sensors/barometer.h"
40 #include "io/escservo.h"
41 #include "io/rc_controls.h"
45 #include "flight/mixer.h"
46 #include "flight/pid.h"
47 #include "flight/imu.h"
48 #include "flight/position.h"
50 #include "config/runtime_config.h"
54 #include "unittest_macros.h"
55 #include "gtest/gtest.h"
57 #define DOWNWARDS_THRUST true
58 #define UPWARDS_THRUST false
62 bool isThrustFacingDownwards(rollAndPitchInclination_t *inclinations);
63 uint16_t calculateTiltAngle(rollAndPitchInclination_t *inclinations);
66 typedef struct inclinationExpectation_s {
67 rollAndPitchInclination_t inclination;
68 bool expectDownwardsThrust;
69 } inclinationExpectation_t;
71 TEST(AltitudeHoldTest, IsThrustFacingDownwards)
75 inclinationExpectation_t inclinationExpectations[] = {
76 { {{ 0, 0 }}, DOWNWARDS_THRUST },
77 { {{ 799, 799 }}, DOWNWARDS_THRUST },
78 { {{ 800, 799 }}, UPWARDS_THRUST },
79 { {{ 799, 800 }}, UPWARDS_THRUST },
80 { {{ 800, 800 }}, UPWARDS_THRUST },
81 { {{ 801, 801 }}, UPWARDS_THRUST },
82 { {{ -799, -799 }}, DOWNWARDS_THRUST },
83 { {{ -800, -799 }}, UPWARDS_THRUST },
84 { {{ -799, -800 }}, UPWARDS_THRUST },
85 { {{ -800, -800 }}, UPWARDS_THRUST },
86 { {{ -801, -801 }}, UPWARDS_THRUST }
88 uint8_t testIterationCount = sizeof(inclinationExpectations) / sizeof(inclinationExpectation_t);
92 for (uint8_t index = 0; index < testIterationCount; index ++) {
93 inclinationExpectation_t *angleInclinationExpectation = &inclinationExpectations[index];
94 #ifdef DEBUG_ALTITUDE_HOLD
95 printf("iteration: %d\n", index);
97 bool result = isThrustFacingDownwards(&angleInclinationExpectation->inclination);
98 EXPECT_EQ(angleInclinationExpectation->expectDownwardsThrust, result);
102 typedef struct inclinationAngleExpectations_s {
103 rollAndPitchInclination_t inclination;
104 uint16_t expected_angle;
105 } inclinationAngleExpectations_t;
107 TEST(AltitudeHoldTest, TestCalculateTiltAngle)
109 inclinationAngleExpectations_t inclinationAngleExpectations[] = {
121 rollAndPitchInclination_t inclination = {{0, 0}};
122 uint16_t tilt_angle = calculateTiltAngle(&inclination);
123 EXPECT_EQ(tilt_angle, 0);
125 for (uint8_t i = 0; i < 9; i++) {
126 inclinationAngleExpectations_t *expectation = &inclinationAngleExpectations[i];
127 uint16_t result = calculateTiltAngle(&expectation->inclination);
128 EXPECT_EQ(expectation->expected_angle, result);
135 uint32_t rcModeActivationMask;
137 int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT];
139 uint32_t accTimeSum ; // keep track for integration of acc
143 rollAndPitchInclination_t inclination;
148 int32_t accSum[XYZ_AXIS_COUNT];
149 //int16_t magADC[XYZ_AXIS_COUNT];
151 int16_t debug[DEBUG16_VALUE_COUNT];
154 uint16_t flightModeFlags;
160 uint16_t enableFlightMode(flightModeFlags_e mask)
162 return flightModeFlags |= (mask);
165 uint16_t disableFlightMode(flightModeFlags_e mask)
167 return flightModeFlags &= ~(mask);
170 void gyroUpdate(void) {};
171 bool sensors(uint32_t mask)
176 void updateAccelerationReadings(rollAndPitchTrims_t *rollAndPitchTrims)
178 UNUSED(rollAndPitchTrims);
181 void imuResetAccelerationSum(void) {};
183 int32_t applyDeadband(int32_t, int32_t) { return 0; }
184 uint32_t micros(void) { return 0; }
185 bool isBaroCalibrationComplete(void) { return true; }
186 void performBaroCalibrationCycle(void) {}
187 int32_t baroCalculateAltitude(void) { return 0; }
188 int constrain(int amt, int low, int high)