Refactor IMU unit test

This commit is contained in:
Dan Nixon 2017-06-11 11:14:24 +01:00
parent fa4517fa8b
commit 3dae29611e
3 changed files with 237 additions and 124 deletions

View File

@ -92,7 +92,7 @@ static float magneticDeclination = 0.0f; // calculated at startup from con
static imuRuntimeConfig_t imuRuntimeConfig; static imuRuntimeConfig_t imuRuntimeConfig;
STATIC_UNIT_TESTED float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f; // quaternion of sensor frame relative to earth frame STATIC_UNIT_TESTED float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f; // quaternion of sensor frame relative to earth frame
static float rMat[3][3]; STATIC_UNIT_TESTED float rMat[3][3];
attitudeEulerAngles_t attitude = { { 0, 0, 0 } }; // absolute angle inclination in multiple of 0.1 degree 180 deg = 1800 attitudeEulerAngles_t attitude = { { 0, 0, 0 } }; // absolute angle inclination in multiple of 0.1 degree 180 deg = 1800

View File

@ -0,0 +1,236 @@
/*
* 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>
#include <cmath>
#undef BARO
extern "C" {
#include "build/debug.h"
#include "common/axis.h"
#include "common/maths.h"
#include "config/parameter_group_ids.h"
#include "drivers/accgyro/accgyro.h"
#include "drivers/compass/compass.h"
#include "drivers/sensor.h"
#include "sensors/sensors.h"
#include "sensors/gyro.h"
#include "sensors/compass.h"
#include "sensors/acceleration.h"
#include "sensors/barometer.h"
#include "fc/runtime_config.h"
#include "fc/rc_controls.h"
#include "rx/rx.h"
#include "flight/mixer.h"
#include "flight/pid.h"
#include "flight/imu.h"
void imuComputeRotationMatrix(void);
void imuUpdateEulerAngles(void);
extern float q0, q1, q2, q3;
extern float rMat[3][3];
PG_REGISTER(rcControlsConfig_t, rcControlsConfig, PG_RC_CONTROLS_CONFIG, 0);
PG_REGISTER(barometerConfig_t, barometerConfig, PG_BAROMETER_CONFIG, 0);
}
#include "unittest_macros.h"
#include "gtest/gtest.h"
const float sqrt2over2 = sqrt(2) / 2.0f;
TEST(FlightImuTest, TestCalculateRotationMatrix)
{
#define TOL 1e-6
// No rotation
q0 = 1.0f;
q1 = 0.0f;
q2 = 0.0f;
q3 = 0.0f;
imuComputeRotationMatrix();
EXPECT_FLOAT_EQ(1.0f, rMat[0][0]);
EXPECT_FLOAT_EQ(0.0f, rMat[0][1]);
EXPECT_FLOAT_EQ(0.0f, rMat[0][2]);
EXPECT_FLOAT_EQ(0.0f, rMat[1][0]);
EXPECT_FLOAT_EQ(1.0f, rMat[1][1]);
EXPECT_FLOAT_EQ(0.0f, rMat[1][2]);
EXPECT_FLOAT_EQ(0.0f, rMat[2][0]);
EXPECT_FLOAT_EQ(0.0f, rMat[2][1]);
EXPECT_FLOAT_EQ(1.0f, rMat[2][2]);
// 90 degrees around Z axis
q0 = sqrt2over2;
q1 = 0.0f;
q2 = 0.0f;
q3 = sqrt2over2;
imuComputeRotationMatrix();
EXPECT_NEAR(0.0f, rMat[0][0], TOL);
EXPECT_NEAR(-1.0f, rMat[0][1], TOL);
EXPECT_NEAR(0.0f, rMat[0][2], TOL);
EXPECT_NEAR(1.0f, rMat[1][0], TOL);
EXPECT_NEAR(0.0f, rMat[1][1], TOL);
EXPECT_NEAR(0.0f, rMat[1][2], TOL);
EXPECT_NEAR(0.0f, rMat[2][0], TOL);
EXPECT_NEAR(0.0f, rMat[2][1], TOL);
EXPECT_NEAR(1.0f, rMat[2][2], TOL);
// 60 degrees around X axis
q0 = 0.866f;
q1 = 0.5f;
q2 = 0.0f;
q3 = 0.0f;
imuComputeRotationMatrix();
EXPECT_NEAR(1.0f, rMat[0][0], TOL);
EXPECT_NEAR(0.0f, rMat[0][1], TOL);
EXPECT_NEAR(0.0f, rMat[0][2], TOL);
EXPECT_NEAR(0.0f, rMat[1][0], TOL);
EXPECT_NEAR(0.5f, rMat[1][1], TOL);
EXPECT_NEAR(-0.866f, rMat[1][2], TOL);
EXPECT_NEAR(0.0f, rMat[2][0], TOL);
EXPECT_NEAR(0.866f, rMat[2][1], TOL);
EXPECT_NEAR(0.5f, rMat[2][2], TOL);
}
TEST(FlightImuTest, TestUpdateEulerAngles)
{
// No rotation
memset(rMat, 0.0, sizeof(float) * 9);
imuUpdateEulerAngles();
EXPECT_EQ(0, attitude.values.roll);
EXPECT_EQ(0, attitude.values.pitch);
EXPECT_EQ(0, attitude.values.yaw);
// 45 degree yaw
memset(rMat, 0.0, sizeof(float) * 9);
rMat[0][0] = sqrt2over2;
rMat[0][1] = sqrt2over2;
rMat[1][0] = -sqrt2over2;
rMat[1][1] = sqrt2over2;
imuUpdateEulerAngles();
EXPECT_EQ(0, attitude.values.roll);
EXPECT_EQ(0, attitude.values.pitch);
EXPECT_EQ(450, attitude.values.yaw);
}
TEST(FlightImuTest, TestSmallAngle)
{
const float r1 = 0.898;
const float r2 = 0.438;
// given
imuConfigMutable()->small_angle = 25;
// and
memset(rMat, 0.0, sizeof(float) * 9);
// when
imuUpdateEulerAngles();
// expect
EXPECT_EQ(0, STATE(SMALL_ANGLE));
// given
rMat[0][0] = r1;
rMat[0][2] = r2;
rMat[2][0] = -r2;
rMat[2][2] = r1;
// when
imuUpdateEulerAngles();
// expect
EXPECT_EQ(SMALL_ANGLE, STATE(SMALL_ANGLE));
// given
memset(rMat, 0.0, sizeof(float) * 9);
// when
imuUpdateEulerAngles();
// expect
EXPECT_EQ(0, STATE(SMALL_ANGLE));
}
// STUBS
extern "C" {
uint32_t rcModeActivationMask;
float rcCommand[4];
int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT];
gyro_t gyro;
acc_t acc;
mag_t mag;
uint8_t GPS_numSat;
uint16_t GPS_speed;
uint16_t GPS_ground_course;
uint8_t debugMode;
int16_t debug[DEBUG16_VALUE_COUNT];
uint8_t stateFlags;
uint16_t flightModeFlags;
uint8_t armingFlags;
pidProfile_t *currentPidProfile;
uint16_t enableFlightMode(flightModeFlags_e mask)
{
return flightModeFlags |= (mask);
}
uint16_t disableFlightMode(flightModeFlags_e mask)
{
return flightModeFlags &= ~(mask);
}
bool sensors(uint32_t mask)
{
UNUSED(mask);
return false;
};
uint32_t millis(void) { return 0; }
uint32_t micros(void) { return 0; }
bool isBaroCalibrationComplete(void) { return true; }
void performBaroCalibrationCycle(void) {}
int32_t baroCalculateAltitude(void) { return 0; }
}

View File

@ -1,123 +0,0 @@
/*
* 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 "build/debug.h"
#include "common/axis.h"
#include "common/maths.h"
#include "sensors/sensors.h"
#include "drivers/sensor.h"
#include "drivers/accgyro.h"
#include "drivers/compass.h"
#include "sensors/gyro.h"
#include "sensors/compass.h"
#include "sensors/acceleration.h"
#include "sensors/barometer.h"
#include "fc/runtime_config.h"
#include "rx/rx.h"
#include "flight/mixer.h"
#include "flight/pid.h"
#include "flight/imu.h"
}
#include "unittest_macros.h"
#include "gtest/gtest.h"
#define DOWNWARDS_THRUST true
#define UPWARDS_THRUST false
TEST(FlightImuTest, TestCalculateHeading)
{
//TODO: Add test cases using the Z dimension.
t_fp_vector north = {.A={1.0f, 0.0f, 0.0f}};
EXPECT_EQ(imuCalculateHeading(&north), 0);
t_fp_vector east = {.A={0.0f, 1.0f, 0.0f}};
EXPECT_EQ(imuCalculateHeading(&east), 90);
t_fp_vector south = {.A={-1.0f, 0.0f, 0.0f}};
EXPECT_EQ(imuCalculateHeading(&south), 180);
t_fp_vector west = {.A={0.0f, -1.0f, 0.0f}};
EXPECT_EQ(imuCalculateHeading(&west), 270);
t_fp_vector north_east = {.A={1.0f, 1.0f, 0.0f}};
EXPECT_EQ(imuCalculateHeading(&north_east), 45);
}
// STUBS
extern "C" {
uint32_t rcModeActivationMask;
float rcCommand[4];
int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT];
acc_t acc;
int16_t heading;
gyro_t gyro;
int32_t magADC[XYZ_AXIS_COUNT];
int32_t BaroAlt;
int16_t debug[DEBUG16_VALUE_COUNT];
uint8_t stateFlags;
uint16_t flightModeFlags;
uint8_t armingFlags;
int32_t sonarAlt;
int16_t accADC[XYZ_AXIS_COUNT];
int32_t gyroADC[XYZ_AXIS_COUNT];
uint16_t enableFlightMode(flightModeFlags_e mask)
{
return flightModeFlags |= (mask);
}
uint16_t disableFlightMode(flightModeFlags_e mask)
{
return flightModeFlags &= ~(mask);
}
void gyroUpdate(void) {};
bool sensors(uint32_t mask)
{
UNUSED(mask);
return false;
};
void updateAccelerationReadings(rollAndPitchTrims_t *rollAndPitchTrims)
{
UNUSED(rollAndPitchTrims);
}
uint32_t micros(void) { return 0; }
bool isBaroCalibrationComplete(void) { return true; }
void performBaroCalibrationCycle(void) {}
int32_t baroCalculateAltitude(void) { return 0; }
}