Skip to content

Commit 438ade4

Browse files
committed
refactor baro & mag
1 parent a7dc113 commit 438ade4

File tree

13 files changed

+272
-277
lines changed

13 files changed

+272
-277
lines changed

src/madflight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define MADFLIGHT_VERSION "madflight v1.3.3"
1+
#define MADFLIGHT_VERSION "madflight v1.3.4-DEV"
22

33
/*==========================================================================================
44
madflight - Flight Controller for ESP32 / ESP32-S3 / RP2350 / RP2040 / STM32
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include "baro_interface.h"
4+
#include "../common/MF_I2C.h"
5+
#include "BMP280/BMP280.h"
6+
7+
class BaroSensorBMP280: public BaroSensor {
8+
protected:
9+
Adafruit_BMP280 baro_BMP280;
10+
public:
11+
int setup(MF_I2C *i2c, int8_t i2c_adr, uint32_t sampleRate) override {
12+
(void) sampleRate; //TODO use sampleRate
13+
unsigned status;
14+
status = baro_BMP280.begin(i2c, i2c_adr, BMP280_CHIPID);
15+
Serial.printf("BARO: BARO_USE_BMP280 BARO_I2C_ADR=0x%02X SensorID=0x%02X\n", i2c_adr, baro_BMP280.sensorID());
16+
17+
if (!status) {
18+
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or try a different address!"));
19+
Serial.print("SensorID was: 0x");
20+
Serial.println(baro_BMP280.sensorID(),16);
21+
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
22+
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
23+
Serial.print(" ID of 0x60 represents a BME 280.\n");
24+
Serial.print(" ID of 0x61 represents a BME 680.\n");
25+
}
26+
27+
baro_BMP280.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
28+
Adafruit_BMP280::SAMPLING_X1, /* Temp. oversampling */
29+
Adafruit_BMP280::SAMPLING_X1, /* Pressure oversampling */
30+
Adafruit_BMP280::FILTER_OFF, /* Filtering. */
31+
Adafruit_BMP280::STANDBY_MS_1); /* Standby time. */
32+
return status;
33+
}
34+
35+
bool update(float *press, float *temp) override {
36+
//driver does not return whether data is fresh, return true if pressure changed
37+
float press_new = baro_BMP280.readPressure();
38+
bool rv = (press_new != *press);
39+
*press = press_new;
40+
*temp = baro_BMP280.readTemperature();
41+
return rv;
42+
}
43+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include "baro_interface.h"
4+
#include "../common/MF_I2C.h"
5+
#include "bmp3/bmp3.h"
6+
7+
class BaroSensorBMP390: public BaroSensor {
8+
protected:
9+
bfs::Bmp3 bmp;
10+
public:
11+
int setup(MF_I2C *i2c, int8_t i2c_adr, uint32_t sampleRate) override {
12+
Serial.printf("BARO: BARO_USE_BMP390/BARO_USE_BMP388 BARO_I2C_ADR=0x%02X\n", i2c_adr);
13+
bmp.Config(i2c, (bfs::Bmp3::I2cAddr)i2c_adr);
14+
if (!bmp.Begin()) {
15+
Serial.println("BARO: BARO_USE_BMP390/BARO_USE_BMP388 sensor not found");
16+
return 1;
17+
}
18+
19+
bmp.ConfigFilterCoef(bfs::Bmp3::FILTER_COEF_OFF);
20+
21+
//set equal or next higher sample rate
22+
if(sampleRate > 100) {
23+
//f = 200;
24+
bmp.ConfigOsMode(bfs::Bmp3::OS_MODE_PRES_1X_TEMP_1X);
25+
}else if(sampleRate > 50) {
26+
//f = 100;
27+
bmp.ConfigOsMode(bfs::Bmp3::OS_MODE_PRES_2X_TEMP_1X);
28+
}else if(sampleRate > 25) {
29+
//f = 50;
30+
bmp.ConfigOsMode(bfs::Bmp3::OS_MODE_PRES_8X_TEMP_1X);
31+
}else if(sampleRate > 12) {
32+
//f = 25;
33+
bmp.ConfigOsMode(bfs::Bmp3::OS_MODE_PRES_16X_TEMP_2X);
34+
}else {
35+
//f = 12.5;
36+
bmp.ConfigOsMode(bfs::Bmp3::OS_MODE_PRES_32X_TEMP_2X); //12.5Hz
37+
}
38+
39+
return 0;
40+
}
41+
42+
bool update (float *press, float *temp) override {
43+
if (!bmp.Read()) return false;
44+
*press = bmp.pres_pa();
45+
*temp = bmp.die_temp_c();
46+
return true;
47+
}
48+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include "baro_interface.h"
4+
#include "../common/MF_I2C.h"
5+
#include "MS5611/MS5611.h"
6+
7+
class BaroSensorMS5611: public BaroSensor {
8+
protected:
9+
MS5611 ms5611;
10+
public:
11+
//float press_pa = 0;
12+
//float temp_c = 0;
13+
14+
int setup(MF_I2C *i2c, int8_t i2c_adr, uint32_t sampleRate) override {
15+
(void) sampleRate;
16+
(void) i2c_adr; //sensor has fixed address 0x77
17+
// Initialize MS5611 sensor
18+
// Ultra high resolution: MS5611_ULTRA_HIGH_RES
19+
// (default) High resolution: MS5611_HIGH_RES
20+
// Standard: MS5611_STANDARD
21+
// Low power: MS5611_LOW_POWER
22+
// Ultra low power: MS5611_ULTRA_LOW_POWER
23+
while(!ms5611.begin(i2c, MS5611_ULTRA_HIGH_RES))
24+
{
25+
Serial.println("BARO: BARO_USE_MS5611 failed, retry...\n");
26+
delay(500);
27+
}
28+
Serial.printf("BARO: BARO_USE_MS5611 BARO_I2C_ADR=0x%02X refresh_rate=%dHz\n", BARO_I2C_ADR, (int)1000000/ms5611.getDelayUs());
29+
return 0;
30+
}
31+
32+
bool update(float *press, float *temp) override {
33+
return (ms5611.getMeasurements(press, temp) == 1); //ms5611.getMeasurements returns: 0=no update, 1=pressure updated, 2=temp updated
34+
}
35+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "baro_interface.h"
4+
5+
class BaroSensorNone: public BaroSensor {
6+
public:
7+
int setup(MF_I2C *i2c, int8_t i2c_adr, uint32_t sampleRate) override {
8+
(void) i2c;
9+
(void) i2c_adr;
10+
(void) sampleRate;
11+
Serial.println("BARO: BARO_USE_NONE");
12+
return 0;
13+
}
14+
15+
//returns true if pressure was updated
16+
bool update(float *press, float *temp) override {
17+
(void) press;
18+
(void) temp;
19+
return false;
20+
}
21+
};

0 commit comments

Comments
 (0)