Skip to content

Commit

Permalink
update Quadcopter
Browse files Browse the repository at this point in the history
  • Loading branch information
qqqlab committed May 19, 2024
1 parent 65d50cc commit 5e76182
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 353 deletions.
172 changes: 90 additions & 82 deletions examples/01.Quadcopter/01.Quadcopter.ino

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/madflight.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define MADFLIGHT_VERSION "madflight v1.1.1"
#define MADFLIGHT_VERSION "madflight v1.1.2-DEV"

/*==========================================================================================
madflight - Flight Controller for ESP32 / RP2040 / STM32
Expand Down
1 change: 0 additions & 1 deletion src/madflight/baro/BMP280.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ class Adafruit_BMP280 {
uint16_t read16_LE(byte reg);
int16_t readS16_LE(byte reg);

uint8_t _i2caddr;

int32_t _sensorID = 0;
int32_t t_fine;
Expand Down
83 changes: 50 additions & 33 deletions src/madflight/baro/baro.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ Each BARO_USE_xxx section in this file defines a specific Barometer class
#define BARO_USE_BMP280 2
#define BARO_USE_MS5611 3

#include "../interface.h"

/* INTERFACE
class Barometer {
class BarometerSensor {
public:
float press_pa = 0; //pressure in Pascal
float temp_c = 0; //temperature in Celcius
virtual int setup() = 0;
virtual bool update() = 0; //returns true if pressure was updated
virtual bool update(float *press, float *temp) = 0; //returns true if pressure was updated
};

extern Barometer &baro;
*/

#ifndef BARO_I2C_ADR
#define BARO_I2C_ADR 0
Expand All @@ -30,23 +23,22 @@ extern Barometer &baro;
// None or undefined
//=================================================================================================
#if BARO_USE == BARO_USE_NONE || !defined BARO_USE
class BarometerNone: public Barometer {
class BarometerNone: public BarometerSensor {
public:
//float press_pa = 0; //pressure in Pascal
//float temp_c = 0; //temperature in Celcius

int setup() {
Serial.println("BARO_USE_NONE");
return 0;
}

//returns true if pressure was updated
bool update() {
bool update(float *press, float *temp) {
(void) press;
(void) temp;
return false;
}
};

BarometerNone baro_instance;
BarometerNone baro_sensor;

//=================================================================================================
// BMP280
Expand All @@ -57,22 +49,19 @@ BarometerNone baro_instance;

Adafruit_BMP280 baro_BMP280(i2c);

class BarometerBMP280: public Barometer {
class BarometerBMP280: public BarometerSensor {

public:
//float press_pa = 0;
//float temp_c = 0;

int setup() {
Serial.println();
unsigned status;
status = baro_BMP280.begin(BARO_I2C_ADR, BMP280_CHIPID);
Serial.printf("BARO_USE_BMP280 BARO_I2C_ADR: 0x%02X SensorID: 0x%02X\n", BARO_I2C_ADR, baro_BMP280.sensorID());

if (!status) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(baro_BMP280.sensorID(),16);
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or try a different address!"));
Serial.print("SensorID was: 0x");
Serial.println(baro_BMP280.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Expand All @@ -87,17 +76,17 @@ class BarometerBMP280: public Barometer {
return status;
}

bool update() {
bool update(float *press, float *temp) {
//driver does not return whether data is fresh, return true if pressure changed
float pressure_pa_new = baro_BMP280.readPressure();
bool rv = (pressure_pa_new != press_pa);
press_pa = pressure_pa_new;
temp_c = baro_BMP280.readTemperature();
float press_new = baro_BMP280.readPressure();
bool rv = (press_new != *press);
*press = press_new;
*temp = baro_BMP280.readTemperature();
return rv;
}
};

BarometerBMP280 baro_instance;
BarometerBMP280 baro_sensor;

//=================================================================================================
// MS5611
Expand All @@ -106,7 +95,7 @@ BarometerBMP280 baro_instance;

#include "MS5611.h"

class BarometerMS5611: public Barometer {
class BarometerMS5611: public BarometerSensor {
private:
MS5611 ms5611;

Expand All @@ -130,12 +119,12 @@ class BarometerMS5611: public Barometer {
return 0;
}

bool update() {
return (ms5611.getMeasurements(&press_pa, &temp_c) == 1); //ms5611.getMeasurements returns: 0=no update, 1=pressure updated, 2=temp updated
bool update(float *press, float *temp) {
return (ms5611.getMeasurements(press, temp) == 1); //ms5611.getMeasurements returns: 0=no update, 1=pressure updated, 2=temp updated
}
};

BarometerMS5611 baro_instance;
BarometerMS5611 baro_sensor;

//=================================================================================================
// Invalid value
Expand All @@ -144,4 +133,32 @@ BarometerMS5611 baro_instance;
#error "invalid BARO_USE value"
#endif

Barometer &baro = baro_instance;

//========================================================================================================================//
// Barometer Class Implementation
//========================================================================================================================//

#include "../interface.h"

int Barometer::setup(uint32_t sampleRate) {
_sampleRate = sampleRate;
_samplePeriod = 1000000 / sampleRate;
dt = 0;
ts = micros();
return baro_sensor.setup();
}

bool Barometer::update() {
if (micros() - ts >= _samplePeriod) {
baro_sensor.update(&press, &temp);
alt = (101325.0 - press) / 12.0;
uint32_t tsnew = micros();
dt = (tsnew - ts) / 1000000.0;
ts = tsnew;
return true;
}
return false;
}

//global Barometer class instance
Barometer baro;
4 changes: 2 additions & 2 deletions src/madflight/bb/bb.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class BlackBox {
if(bbw.isBusy()) return;
bbw.writeBeginRecord(BB_REC_BARO, "BARO");
bbw.writeU("ts",micros());
bbw.writeU("baro_pa",baro.press_pa); //Barometer pressure (Pa)
bbw.writeI("baro_t*100",baro.temp_c*100); //barometer temp (C)
bbw.writeU("baro_pa",baro.press); //Barometer pressure (Pa)
bbw.writeI("baro_t*100",baro.temp*100); //barometer temp (C)
bbw.writeEndrecord();
}

Expand Down
Loading

0 comments on commit 5e76182

Please sign in to comment.