mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-18 22:05:17 +03:00
Decouple altitudehold.c from config.c. Update flight_imu_unittest and
altitude_hold_unittest.
This commit is contained in:
parent
f8b13d7c62
commit
503e7a0817
15 changed files with 243 additions and 121 deletions
|
@ -36,6 +36,7 @@ CXXFLAGS += -g -Wall -Wextra -pthread -ggdb3 -O0 -DUNIT_TEST
|
|||
TESTS = \
|
||||
battery_unittest \
|
||||
flight_imu_unittest \
|
||||
altitude_hold_unittest \
|
||||
gps_conversion_unittest \
|
||||
telemetry_hott_unittest \
|
||||
rc_controls_unittest \
|
||||
|
@ -105,10 +106,6 @@ battery_unittest : $(OBJECT_DIR)/sensors/battery.o $(OBJECT_DIR)/battery_unittes
|
|||
|
||||
|
||||
|
||||
$(OBJECT_DIR)/flight/altitudehold.o : $(USER_DIR)/flight/altitudehold.c $(USER_DIR)/flight/altitudehold.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/flight/altitudehold.c -o $@
|
||||
|
||||
$(OBJECT_DIR)/flight/imu.o : $(USER_DIR)/flight/imu.c $(USER_DIR)/flight/imu.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/flight/imu.c -o $@
|
||||
|
@ -122,6 +119,21 @@ flight_imu_unittest : $(OBJECT_DIR)/flight/imu.o $(OBJECT_DIR)/flight/altitudeho
|
|||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
||||
|
||||
|
||||
$(OBJECT_DIR)/flight/altitudehold.o : $(USER_DIR)/flight/altitudehold.c $(USER_DIR)/flight/altitudehold.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/flight/altitudehold.c -o $@
|
||||
|
||||
$(OBJECT_DIR)/altitude_hold_unittest.o : $(TEST_DIR)/altitude_hold_unittest.cc \
|
||||
$(USER_DIR)/flight/altitudehold.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/altitude_hold_unittest.cc -o $@
|
||||
|
||||
altitude_hold_unittest : $(OBJECT_DIR)/flight/altitudehold.o $(OBJECT_DIR)/altitude_hold_unittest.o $(OBJECT_DIR)/gtest_main.a
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $(OBJECT_DIR)/$@
|
||||
|
||||
|
||||
|
||||
$(OBJECT_DIR)/flight/gps_conversion.o : $(USER_DIR)/flight/gps_conversion.c $(USER_DIR)/flight/gps_conversion.h $(GTEST_HEADERS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(CPPFLAGS) $(CXXFLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/flight/gps_conversion.c -o $@
|
||||
|
|
145
src/test/unit/altitude_hold_unittest.cc
Normal file
145
src/test/unit/altitude_hold_unittest.cc
Normal file
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
* This file is part of Cleanflight.
|
||||
*
|
||||
* Cleanflight is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Cleanflight is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#define BARO
|
||||
|
||||
extern "C" {
|
||||
#include "common/axis.h"
|
||||
#include "flight/flight.h"
|
||||
|
||||
#include "sensors/sensors.h"
|
||||
#include "drivers/accgyro.h"
|
||||
#include "sensors/acceleration.h"
|
||||
#include "sensors/barometer.h"
|
||||
|
||||
#include "flight/mixer.h"
|
||||
#include "flight/mixer.h"
|
||||
|
||||
#include "io/escservo.h"
|
||||
#include "rx/rx.h"
|
||||
#include "io/rc_controls.h"
|
||||
|
||||
#include "config/runtime_config.h"
|
||||
|
||||
#include "flight/altitudehold.h"
|
||||
|
||||
}
|
||||
|
||||
#include "unittest_macros.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#define DOWNWARDS_THRUST true
|
||||
#define UPWARDS_THRUST false
|
||||
|
||||
|
||||
extern "C" {
|
||||
bool isThrustFacingDownwards(rollAndPitchInclination_t *inclinations);
|
||||
}
|
||||
|
||||
typedef struct inclinationExpectation_s {
|
||||
rollAndPitchInclination_t inclination;
|
||||
bool expectDownwardsThrust;
|
||||
} inclinationExpectation_t;
|
||||
|
||||
TEST(AltitudeHoldTest, IsThrustFacingDownwards)
|
||||
{
|
||||
// given
|
||||
|
||||
inclinationExpectation_t inclinationExpectations[] = {
|
||||
{ { 0, 0 }, DOWNWARDS_THRUST },
|
||||
{ { 799, 799 }, DOWNWARDS_THRUST },
|
||||
{ { 800, 799 }, UPWARDS_THRUST },
|
||||
{ { 799, 800 }, UPWARDS_THRUST },
|
||||
{ { 800, 800 }, UPWARDS_THRUST },
|
||||
{ { 801, 801 }, UPWARDS_THRUST },
|
||||
{ { -799, -799 }, DOWNWARDS_THRUST },
|
||||
{ { -800, -799 }, UPWARDS_THRUST },
|
||||
{ { -799, -800 }, UPWARDS_THRUST },
|
||||
{ { -800, -800 }, UPWARDS_THRUST },
|
||||
{ { -801, -801 }, UPWARDS_THRUST }
|
||||
};
|
||||
uint8_t testIterationCount = sizeof(inclinationExpectations) / sizeof(inclinationExpectation_t);
|
||||
|
||||
// expect
|
||||
|
||||
for (uint8_t index = 0; index < testIterationCount; index ++) {
|
||||
inclinationExpectation_t *angleInclinationExpectation = &inclinationExpectations[index];
|
||||
printf("iteration: %d\n", index);
|
||||
bool result = isThrustFacingDownwards(&angleInclinationExpectation->inclination);
|
||||
EXPECT_EQ(angleInclinationExpectation->expectDownwardsThrust, result);
|
||||
}
|
||||
}
|
||||
|
||||
// STUBS
|
||||
|
||||
extern "C" {
|
||||
uint32_t rcModeActivationMask;
|
||||
int16_t rcCommand[4];
|
||||
|
||||
uint32_t accTimeSum ; // keep track for integration of acc
|
||||
int accSumCount;
|
||||
float accVelScale;
|
||||
|
||||
rollAndPitchInclination_t inclination;
|
||||
|
||||
//uint16_t acc_1G;
|
||||
//int16_t heading;
|
||||
//gyro_t gyro;
|
||||
int32_t accSum[XYZ_AXIS_COUNT];
|
||||
//int16_t magADC[XYZ_AXIS_COUNT];
|
||||
int32_t BaroAlt;
|
||||
int16_t debug[4];
|
||||
|
||||
uint8_t stateFlags;
|
||||
uint16_t flightModeFlags;
|
||||
uint8_t armingFlags;
|
||||
|
||||
int32_t sonarAlt;
|
||||
|
||||
|
||||
void gyroGetADC(void) {};
|
||||
bool sensors(uint32_t mask)
|
||||
{
|
||||
UNUSED(mask);
|
||||
return false;
|
||||
};
|
||||
void updateAccelerationReadings(rollAndPitchTrims_t *rollAndPitchTrims)
|
||||
{
|
||||
UNUSED(rollAndPitchTrims);
|
||||
}
|
||||
|
||||
void accSum_reset(void) {};
|
||||
|
||||
int32_t applyDeadband(int32_t, int32_t) { return 0; }
|
||||
uint32_t micros(void) { return 0; }
|
||||
bool isBaroCalibrationComplete(void) { return true; }
|
||||
void performBaroCalibrationCycle(void) {}
|
||||
int32_t baroCalculateAltitude(void) { return 0; }
|
||||
int constrain(int amt, int low, int high)
|
||||
{
|
||||
UNUSED(amt);
|
||||
UNUSED(low);
|
||||
UNUSED(high);
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -25,7 +25,6 @@
|
|||
extern "C" {
|
||||
#include "common/axis.h"
|
||||
#include "flight/flight.h"
|
||||
#include "flight/altitudehold.h"
|
||||
|
||||
#include "sensors/sensors.h"
|
||||
#include "drivers/accgyro.h"
|
||||
|
@ -47,47 +46,17 @@ extern "C" {
|
|||
#define UPWARDS_THRUST false
|
||||
|
||||
|
||||
extern "C" {
|
||||
bool isThrustFacingDownwards(rollAndPitchInclination_t *inclinations);
|
||||
}
|
||||
|
||||
typedef struct inclinationExpectation_s {
|
||||
rollAndPitchInclination_t inclination;
|
||||
bool expectDownwardsThrust;
|
||||
} inclinationExpectation_t;
|
||||
|
||||
TEST(FlightImuTest, IsThrustFacingDownwards)
|
||||
TEST(FlightImuTest, Placeholder)
|
||||
{
|
||||
// given
|
||||
|
||||
inclinationExpectation_t inclinationExpectations[] = {
|
||||
{ { 0, 0 }, DOWNWARDS_THRUST },
|
||||
{ { 799, 799 }, DOWNWARDS_THRUST },
|
||||
{ { 800, 799 }, UPWARDS_THRUST },
|
||||
{ { 799, 800 }, UPWARDS_THRUST },
|
||||
{ { 800, 800 }, UPWARDS_THRUST },
|
||||
{ { 801, 801 }, UPWARDS_THRUST },
|
||||
{ { -799, -799 }, DOWNWARDS_THRUST },
|
||||
{ { -800, -799 }, UPWARDS_THRUST },
|
||||
{ { -799, -800 }, UPWARDS_THRUST },
|
||||
{ { -800, -800 }, UPWARDS_THRUST },
|
||||
{ { -801, -801 }, UPWARDS_THRUST }
|
||||
};
|
||||
uint8_t testIterationCount = sizeof(inclinationExpectations) / sizeof(inclinationExpectation_t);
|
||||
|
||||
// expect
|
||||
|
||||
for (uint8_t index = 0; index < testIterationCount; index ++) {
|
||||
inclinationExpectation_t *angleInclinationExpectation = &inclinationExpectations[index];
|
||||
printf("iteration: %d\n", index);
|
||||
bool result = isThrustFacingDownwards(&angleInclinationExpectation->inclination);
|
||||
EXPECT_EQ(angleInclinationExpectation->expectDownwardsThrust, result);
|
||||
}
|
||||
// TODO test things
|
||||
EXPECT_EQ(true, true);
|
||||
}
|
||||
|
||||
// STUBS
|
||||
|
||||
extern "C" {
|
||||
uint32_t rcModeActivationMask;
|
||||
int16_t rcCommand[4];
|
||||
|
||||
uint16_t acc_1G;
|
||||
int16_t heading;
|
||||
|
@ -114,6 +83,8 @@ void updateAccelerationReadings(rollAndPitchTrims_t *rollAndPitchTrims)
|
|||
UNUSED(rollAndPitchTrims);
|
||||
}
|
||||
|
||||
int32_t applyDeadband(int32_t, int32_t) { return 0; }
|
||||
|
||||
uint32_t micros(void) { return 0; }
|
||||
bool isBaroCalibrationComplete(void) { return true; }
|
||||
void performBaroCalibrationCycle(void) {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue