☁️ Makes barometer facade
This commit is contained in:
@@ -51,6 +51,7 @@ class Spot {
|
|||||||
void readSensors() {
|
void readSensors() {
|
||||||
_peripherals.readIMU();
|
_peripherals.readIMU();
|
||||||
_peripherals.readMag();
|
_peripherals.readMag();
|
||||||
|
_peripherals.readBMP();
|
||||||
}
|
}
|
||||||
|
|
||||||
// plan
|
// 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 {
|
class Magnetometer {
|
||||||
public:
|
public:
|
||||||
Magnetometer() : _mag(12345) {}
|
Magnetometer() : _mag(12345) {}
|
||||||
bool initialize() { return _mag.begin(); }
|
|
||||||
|
bool initialize() {
|
||||||
|
mag_success = _mag.begin();
|
||||||
|
return mag_success;
|
||||||
|
}
|
||||||
|
|
||||||
bool readMagnetometer() {
|
bool readMagnetometer() {
|
||||||
if (!mag_success) return false;
|
if (!mag_success) return false;
|
||||||
|
|||||||
@@ -15,11 +15,10 @@
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
#include <Adafruit_BMP085_U.h>
|
|
||||||
#include <Adafruit_Sensor.h>
|
|
||||||
#include <NewPing.h>
|
#include <NewPing.h>
|
||||||
#include <peripherals/imu.h>
|
#include <peripherals/imu.h>
|
||||||
#include <peripherals/magnetometer.h>
|
#include <peripherals/magnetometer.h>
|
||||||
|
#include <peripherals/barometer.h>
|
||||||
|
|
||||||
#define EVENT_CONFIGURATION_SETTINGS "peripheralSettings"
|
#define EVENT_CONFIGURATION_SETTINGS "peripheralSettings"
|
||||||
|
|
||||||
@@ -47,9 +46,6 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
|
|||||||
: endpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this),
|
: endpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this),
|
||||||
_eventEndpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this,
|
_eventEndpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this,
|
||||||
EVENT_CONFIGURATION_SETTINGS),
|
EVENT_CONFIGURATION_SETTINGS),
|
||||||
#if FT_ENABLED(USE_BMP)
|
|
||||||
_bmp(10085),
|
|
||||||
#endif
|
|
||||||
_persistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, DEVICE_CONFIG_FILE) {
|
_persistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, DEVICE_CONFIG_FILE) {
|
||||||
_accessMutex = xSemaphoreCreateMutex();
|
_accessMutex = xSemaphoreCreateMutex();
|
||||||
addUpdateHandler([&](const String &originId) { updatePins(); }, false);
|
addUpdateHandler([&](const String &originId) { updatePins(); }, false);
|
||||||
@@ -78,12 +74,8 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
|
|||||||
if (!_mag.initialize()) ESP_LOGE("IMUService", "MAG initialize failed");
|
if (!_mag.initialize()) ESP_LOGE("IMUService", "MAG initialize failed");
|
||||||
#endif
|
#endif
|
||||||
#if FT_ENABLED(USE_BMP)
|
#if FT_ENABLED(USE_BMP)
|
||||||
bmp_success = _bmp.begin();
|
if (!_bmp.initialize()) ESP_LOGE("IMUService", "BMP initialize failed");
|
||||||
if (!bmp_success) {
|
|
||||||
ESP_LOGE("IMUService", "BMP initialize failed");
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if FT_ENABLED(USE_USS)
|
#if FT_ENABLED(USE_USS)
|
||||||
_left_sonar = new NewPing(USS_LEFT_PIN, USS_LEFT_PIN, MAX_DISTANCE);
|
_left_sonar = new NewPing(USS_LEFT_PIN, USS_LEFT_PIN, MAX_DISTANCE);
|
||||||
_right_sonar = new NewPing(USS_RIGHT_PIN, USS_RIGHT_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;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* BMP FUNCTIONS */
|
bool readBMP() {
|
||||||
float getAltitude() {
|
bool updated = false;
|
||||||
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;
|
|
||||||
#if FT_ENABLED(USE_BMP)
|
#if FT_ENABLED(USE_BMP)
|
||||||
sensors_event_t event;
|
beginTransaction();
|
||||||
_bmp.getEvent(&event);
|
updated = _bmp.readBarometer();
|
||||||
pressure = bmp_success && event.pressure ? event.pressure : -1;
|
endTransaction();
|
||||||
#endif
|
#endif
|
||||||
return pressure;
|
return updated;
|
||||||
}
|
|
||||||
|
|
||||||
float getTemperature() {
|
|
||||||
float temperature = 0;
|
|
||||||
#if FT_ENABLED(USE_BMP)
|
|
||||||
_bmp.getTemperature(&temperature);
|
|
||||||
temperature = bmp_success ? temperature : -1;
|
|
||||||
#endif
|
|
||||||
return temperature;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void readSonar() {
|
void readSonar() {
|
||||||
@@ -217,11 +188,7 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
|
|||||||
_mag.readMagnetometer(root);
|
_mag.readMagnetometer(root);
|
||||||
#endif
|
#endif
|
||||||
#if FT_ENABLED(USE_BMP)
|
#if FT_ENABLED(USE_BMP)
|
||||||
if (bmp_success) {
|
_bmp.readBarometer(root);
|
||||||
doc["pressure"] = round2(getPressure());
|
|
||||||
doc["altitude"] = round2(getAltitude());
|
|
||||||
doc["bmp_temp"] = round2(getTemperature());
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
serializeJson(doc, message);
|
serializeJson(doc, message);
|
||||||
socket.emit(EVENT_IMU, message);
|
socket.emit(EVENT_IMU, message);
|
||||||
@@ -254,8 +221,7 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
|
|||||||
Magnetometer _mag;
|
Magnetometer _mag;
|
||||||
#endif
|
#endif
|
||||||
#if FT_ENABLED(USE_BMP)
|
#if FT_ENABLED(USE_BMP)
|
||||||
Adafruit_BMP085_Unified _bmp;
|
Barometer _bmp;
|
||||||
bool bmp_success {false};
|
|
||||||
#endif
|
#endif
|
||||||
#if FT_ENABLED(USE_USS)
|
#if FT_ENABLED(USE_USS)
|
||||||
NewPing *_left_sonar;
|
NewPing *_left_sonar;
|
||||||
|
|||||||
Reference in New Issue
Block a user