☁️ Makes barometer facade

This commit is contained in:
Rune Harlyk
2024-11-14 16:51:02 +01:00
committed by Rune Harlyk
parent b5a8fe88ca
commit 1a6e3626f6
4 changed files with 70 additions and 45 deletions
+1
View File
@@ -51,6 +51,7 @@ class Spot {
void readSensors() {
_peripherals.readIMU();
_peripherals.readMag();
_peripherals.readBMP();
}
// plan
@@ -0,0 +1,54 @@
#pragma once
#include <list>
#include <SPI.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <utils/math_utils.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_Sensor.h>
class Barometer {
public:
Barometer() : _bmp(10085) {}
bool initialize() {
bmp_success = _bmp.begin();
return bmp_success;
}
bool readBarometer() {
if (!bmp_success) return false;
_bmp.getTemperature(&temperature);
sensors_event_t event;
_bmp.getEvent(&event);
pressure = event.pressure;
altitude = _bmp.pressureToAltitude(seaLevelPressure, pressure);
return true;
}
float getPressure() { return pressure; }
float getAltitude() { return altitude; }
float getTemperature() { return temperature; }
void readBarometer(JsonObject& root) {
if (!bmp_success) return;
root["pressure"] = round2(getPressure());
root["altitude"] = round2(getAltitude());
root["bmp_temp"] = round2(getTemperature());
}
bool active() { return bmp_success; }
private:
Adafruit_BMP085_Unified _bmp;
bool bmp_success {false};
float pressure {0};
float altitude {0};
float temperature {0};
const float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
};
@@ -12,7 +12,11 @@
class Magnetometer {
public:
Magnetometer() : _mag(12345) {}
bool initialize() { return _mag.begin(); }
bool initialize() {
mag_success = _mag.begin();
return mag_success;
}
bool readMagnetometer() {
if (!mag_success) return false;
@@ -15,11 +15,10 @@
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_Sensor.h>
#include <NewPing.h>
#include <peripherals/imu.h>
#include <peripherals/magnetometer.h>
#include <peripherals/barometer.h>
#define EVENT_CONFIGURATION_SETTINGS "peripheralSettings"
@@ -47,9 +46,6 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
: endpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this),
_eventEndpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this,
EVENT_CONFIGURATION_SETTINGS),
#if FT_ENABLED(USE_BMP)
_bmp(10085),
#endif
_persistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, DEVICE_CONFIG_FILE) {
_accessMutex = xSemaphoreCreateMutex();
addUpdateHandler([&](const String &originId) { updatePins(); }, false);
@@ -78,12 +74,8 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
if (!_mag.initialize()) ESP_LOGE("IMUService", "MAG initialize failed");
#endif
#if FT_ENABLED(USE_BMP)
bmp_success = _bmp.begin();
if (!bmp_success) {
ESP_LOGE("IMUService", "BMP initialize failed");
}
if (!_bmp.initialize()) ESP_LOGE("IMUService", "BMP initialize failed");
#endif
#if FT_ENABLED(USE_USS)
_left_sonar = new NewPing(USS_LEFT_PIN, USS_LEFT_PIN, MAX_DISTANCE);
_right_sonar = new NewPing(USS_RIGHT_PIN, USS_RIGHT_PIN, MAX_DISTANCE);
@@ -163,35 +155,14 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
return updated;
}
/* BMP FUNCTIONS */
float getAltitude() {
float altitude = -1;
#if FT_ENABLED(USE_MAG)
sensors_event_t event;
_bmp.getEvent(&event);
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
altitude = bmp_success && event.pressure ? _bmp.pressureToAltitude(seaLevelPressure, event.pressure) : -1;
#endif
return altitude;
}
float getPressure() {
float pressure = -1;
bool readBMP() {
bool updated = false;
#if FT_ENABLED(USE_BMP)
sensors_event_t event;
_bmp.getEvent(&event);
pressure = bmp_success && event.pressure ? event.pressure : -1;
beginTransaction();
updated = _bmp.readBarometer();
endTransaction();
#endif
return pressure;
}
float getTemperature() {
float temperature = 0;
#if FT_ENABLED(USE_BMP)
_bmp.getTemperature(&temperature);
temperature = bmp_success ? temperature : -1;
#endif
return temperature;
return updated;
}
void readSonar() {
@@ -217,11 +188,7 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
_mag.readMagnetometer(root);
#endif
#if FT_ENABLED(USE_BMP)
if (bmp_success) {
doc["pressure"] = round2(getPressure());
doc["altitude"] = round2(getAltitude());
doc["bmp_temp"] = round2(getTemperature());
}
_bmp.readBarometer(root);
#endif
serializeJson(doc, message);
socket.emit(EVENT_IMU, message);
@@ -254,8 +221,7 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
Magnetometer _mag;
#endif
#if FT_ENABLED(USE_BMP)
Adafruit_BMP085_Unified _bmp;
bool bmp_success {false};
Barometer _bmp;
#endif
#if FT_ENABLED(USE_USS)
NewPing *_left_sonar;