Skip to content

Commit

Permalink
add altitude estimator, add calinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
qqqlab committed Jan 14, 2025
1 parent 8720bf4 commit dcaea77
Show file tree
Hide file tree
Showing 28 changed files with 1,639 additions and 111 deletions.
10 changes: 7 additions & 3 deletions examples/02.QuadcopterAdvanced/02.QuadcopterAdvanced.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Blink interval longer than 1 second imu_loop() is taking too much time
fast blinking Something is wrong, connect USB serial for info
MIT license
Copyright (c) 2023-2024 https://madflight.com
Copyright (c) 2023-2025 https://madflight.com
##########################################################################################################################*/

#include "madflight_config.h" //Edit this header file to setup the pins, hardware, radio, etc. for madflight
Expand Down Expand Up @@ -120,8 +120,12 @@ void loop() {

//update all I2C sensors, called from loop() with SPI IMU, or called from imu_loop() with I2C IMU
void i2c_sensors_update() {
if(bat.update()) bb.log_bat(); //update battery, and log if battery was updated.
if(baro.update()) bb.log_baro(); //log if pressure updated
if(bat.update()) bb.log_bat(); //update battery, and log if battery was updated.
alt.updateAccelUp(ahrs.getAccelUp(), ahrs.ts); //NOTE: do this here and not in imu_loop() because `alt` object is not thread safe. - Update altitude estimator with current earth-frame up acceleration measurement
if(baro.update()) {
alt.updateBaroAlt(baro.alt, baro.ts); //update altitude estimator with current altitude measurement
bb.log_baro(); //log if pressure updated
}
mag.update();
}

Expand Down
36 changes: 22 additions & 14 deletions src/madflight.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define MADFLIGHT_VERSION "madflight v1.3.0"
#define MADFLIGHT_VERSION "madflight v1.3.1-DEV"

/*==========================================================================================
madflight - Flight Controller for ESP32 / ESP32-S3 / RP2350 / RP2040 / STM32
Expand Down Expand Up @@ -28,6 +28,22 @@ SOFTWARE.

#pragma once

//include hardware specific code & default board pinout
#if defined ARDUINO_ARCH_ESP32
#include <madflight/hw_ESP32/hw_ESP32.h>
#elif defined ARDUINO_ARCH_RP2040
#include <madflight/hw_RP2040/hw_RP2040.h>
#elif defined ARDUINO_ARCH_STM32
#include <madflight/hw_STM32/hw_STM32.h>
#else
#error "Unknown hardware architecture, expected ESP32 / RP2040 / STM32"
#endif

//default for ALT (Altitude Estimation)
#ifndef ALT_USE
#define ALT_USE ALT_USE_BARO
#endif

//defaults for OUT
#ifndef HW_OUT_COUNT
#define HW_OUT_COUNT 0
Expand All @@ -51,17 +67,6 @@ SOFTWARE.
#define IMU_MAG_LP_HZ 1e10
#endif

//include hardware specific code & default board pinout
#if defined ARDUINO_ARCH_ESP32
#include <madflight/hw_ESP32/hw_ESP32.h>
#elif defined ARDUINO_ARCH_RP2040
#include <madflight/hw_RP2040/hw_RP2040.h>
#elif defined ARDUINO_ARCH_STM32
#include <madflight/hw_STM32/hw_STM32.h>
#else
#error "Unknown hardware architecture, expected ESP32 / RP2040 / STM32"
#endif


//for testing individual modules use: #define MF_TEST MF_TEST_LED | MF_TEST_RCIN
#define MF_TEST_BASE 0x0000
Expand All @@ -77,6 +82,7 @@ SOFTWARE.
#define MF_TEST_BB 0x0200
#define MF_TEST_CLI 0x0400
#define MF_TEST_OUT 0x0800
#define MF_TEST_ALT 0x1000

//include all modules. Before including madflight.h define the all module options, for example: #define IMU_USE IMU_USE_SPI_MPU6500
//load config first, so that cfg.xxx can be used by other modules
Expand Down Expand Up @@ -116,6 +122,9 @@ SOFTWARE.
#if !defined(MF_TEST) || ((MF_TEST) & MF_TEST_OUT)
#include "madflight/out/out.h"
#endif
#if !defined(MF_TEST) || ((MF_TEST) & MF_TEST_ALT)
#include "madflight/alt/alt.h"
#endif
#if !defined(MF_TEST) || ((MF_TEST) & MF_TEST_CLI)
#include "madflight/cli/cli.h"
#endif
Expand All @@ -129,8 +138,6 @@ SOFTWARE.
#define MF_SETUP_DELAY_S 6
#endif



//===============================================================================================
// HELPERS
//===============================================================================================
Expand Down Expand Up @@ -187,6 +194,7 @@ SOFTWARE.
bat.setup(); //Battery Monitor
bb.setup(); //Black Box
gps_setup(); //GPS
alt.setup(baro.alt); //Altitude Estimator

ahrs.setup(IMU_GYR_LP_HZ, IMU_ACC_LP_HZ, IMU_MAG_LP_HZ); //setup low pass filters for Mahony/Madgwick filters
ahrs.setInitalOrientation(); //do this before IMU update handler is started
Expand Down
46 changes: 7 additions & 39 deletions src/madflight/ahrs/ahrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,8 @@ Each BARO_USE_xxx section in this file defines a specific Barometer class
#define AHRS_USE_MADGWICK 3
#define AHRS_USE_VQF 4

#include "../interface.h"

/*class definition in interface.h
class Ahrs {
public:
float gx = 0, gy = 0, gz = 0; //corrected and filtered imu gyro measurements in deg/sec
float ax = 0, ay = 0, az = 0; //corrected and filtered imu accel measurements in g
float mx = 0, my = 0, mz = 0; //corrected and filtered external magnetometer or internal imu mag measurements in uT
float q[4] = {1,0,0,0}; //quaternion NED reference frame
float roll = 0; //roll in degrees - roll right is positive
float pitch = 0; //pitch in degrees - pitch up is positive
float yaw = 0; //yaw in degrees - yaw right is positive
float B_gyr = 1.0; //gyr filter constant
float B_acc = 1.0; //acc filter constant
float B_mag = 1.0; //mag filter constant
static constexpr float rad_to_deg = 57.2957795132f;
static constexpr float deg_to_rad = 0.0174532925199f;
virtual void setup(float gyrLpFreq, float accLpFreq, float magLpFreq) = 0;
virtual void setInitalOrientation() {}
void update(); //get imu+mag data, filter it, and call fusionUpdate() to update q
static float lowpass_to_beta(float f0, float fs); //compute beta coeffient for low pass filter
protected:
virtual void fusionUpdate() = 0;
void computeAngles();
void setFromMag(float *q);
};
extern Ahrs &ahrs;
*/

#include "../interface.h" //Ahrs class definition
#include "../common/common.h" //lowpass_to_beta

void Ahrs::update() {
// get sensor data from imu and mag
Expand Down Expand Up @@ -95,6 +62,8 @@ void Ahrs::update() {

//update euler angles
computeAngles();

ts = imu.ts;
}

void Ahrs::setFromMag(float *q) {
Expand Down Expand Up @@ -138,10 +107,9 @@ void Ahrs::computeAngles() {
yaw = atan2(q[1]*q[2] + q[0]*q[3], 0.5f - q[2]*q[2] - q[3]*q[3]) * rad_to_deg; //degrees - yaw right is positive
}


//lowpass frequency to filter beta constant
float Ahrs::lowpass_to_beta(float f0, float fs) {
return constrain(1 - exp(-2 * PI * f0 / fs), 0.0f, 1.0f);
//get acceleration in earth-frame up direction in [m/s^2]
float Ahrs::getAccelUp() {
return 9.80665 * ((2*q[1]*q[3] - 2*q[0]*q[2])*ax + (2*q[2]*q[3] + 2*q[0]*q[1])*ay + (q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3])*az - 1.0);
}


Expand Down
108 changes: 108 additions & 0 deletions src/madflight/alt/alt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*==========================================================================================
alt.h - madflight altitude estimator
MIT License
Copyright (c) 2025 https://madflight.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
===========================================================================================*/


/*=================================================================================================
Each ALT_USE_xxx section in this file defines a specific altimeter estimator class
=================================================================================================*/

#pragma once

#define ALT_USE_NONE 1
#define ALT_USE_KALMAN2 2 // Kalman filter estimates h and v from barometer and acceleration
#define ALT_USE_KALMAN3 3 // Kalman filter estimates h, v, and abias from barometer and acceleration
#define ALT_USE_BARO 4 // Filtered barometer
#define ALT_USE_COMP 5 // Complementary filter

#include "../interface.h" //declares AltEst - Altimeter Estimator class

//=================================================================================================
// None or undefined
//=================================================================================================
#if ALT_USE == ALT_USE_NONE || !defined ALT_USE

class AltEst_None : public AltEst {
public:
void setup(float alt) {
Serial.printf("ALT: ALT_USE_NONE\n");
}
void updateAccelUp(float a, uint32_t ts) {};
void updateBaroAlt(float alt, uint32_t ts) {}
float getH() {return 0;}
float getV() {return 0;}
void print() {}
};

AltEst_None alt_instance;

//=================================================================================================
// ALT_USE_KALMAN2 - Simple Kalman filter estimates h and vup from barometer and acceleration
//=================================================================================================
#elif ALT_USE == ALT_USE_KALMAN2

#include "alt_kalman2/alt_kalman2.h"

AltEst_Kalman2 alt_instance;


//=================================================================================================
// ALT_USE_KALMAN3 - Kalman filter estimates h, vup, and abias from barometer and acceleration
//=================================================================================================
#elif ALT_USE == ALT_USE_KALMAN3

#include "alt_kalman3/alt_kalman3.h"

AltEst_Kalman3 alt_instance;


//=================================================================================================
// ALT_USE_BARO - Filtered barometer only
//=================================================================================================
#elif ALT_USE == ALT_USE_BARO

#include "alt_baro/alt_baro.h"

AltEst_Baro alt_instance;


//=================================================================================================
// ALT_USE_COMP - Complementary filter altitude and accelation
//=================================================================================================
#elif ALT_USE == ALT_USE_COMP

#include "alt_comp/alt_comp.h"

AltEst_Comp alt_instance;


//=================================================================================================
// ERROR OTHER VALUE
//=================================================================================================
#else
#error "Invalid #define ALT_USE value"
#endif

AltEst &alt = alt_instance;
76 changes: 76 additions & 0 deletions src/madflight/alt/alt_baro/alt_baro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*==========================================================================================
alt_baro.h - madflight altitude estimator based on filtered barometer readings
MIT License
Copyright (c) 2025 https://madflight.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
===========================================================================================*/

#pragma once

#include "../../interface.h" //baro.getSampleRate()
#include "../../common/common.h" //lowpass_to_beta()

class AltEst_Baro : public AltEst {
public:
void setup(float alt) {
Serial.printf("ALT: ALT_USE_BARO\n");

float sampleRate = baro.getSampleRate();
float filterHHertz = 2.0;
float filterVHertz = 0.5;

B_h = lowpass_to_beta(filterHHertz, sampleRate);
B_v = lowpass_to_beta(filterVHertz, sampleRate);
h = alt;
v = 0;
ts = 0;
}

void updateAccelUp(float a, uint32_t ts) {}; //a: accel up in [m/s^2], ts: timestamp in [us]

void updateBaroAlt(float alt, uint32_t ts) { //altitude: barometric altitude in [m], ts: timestamp in [us]
if(this->ts != 0) {
float dt = 1e-6 * (ts - this->ts);
float hnew = h + B_h * (alt - h); //Low-pass filtered altitude
float vnew = (hnew - h) / dt;
v += B_v * (vnew - v); //Low-pass filtered velocity
h = hnew;
}
this->ts = ts;
}

float getH() {return h;} //altitude estimate in [m]
float getV() {return v;} //vertical up speed (climb rate) estimate in [m/s]

void print() {
Serial.printf("alt.h:%.2f\t", h);
Serial.printf("alt.v:%+.2f\t", v);
}

float h = 0; // Filtered approximate International Standard Atmosphere (ISA) Altitude in [m]
float v = 0; // Filtered vertical speed in [m/s], up is positive

protected:
float B_h = 1.0; //alt filter constant
float B_v = 1.0; //vz filter constant
uint32_t ts = 0;
};
Loading

0 comments on commit dcaea77

Please sign in to comment.