🔥 Remove json from sensor implementations
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <utils/math_utils.h>
|
#include <utils/math_utils.h>
|
||||||
|
|
||||||
#include <Adafruit_BMP085_U.h>
|
#include <Adafruit_BMP085_U.h>
|
||||||
@@ -11,29 +9,11 @@
|
|||||||
|
|
||||||
#include <peripherals/sensor.hpp>
|
#include <peripherals/sensor.hpp>
|
||||||
|
|
||||||
struct BarometerMsg : public SensorMessageBase {
|
struct BarometerMsg {
|
||||||
float pressure {-1};
|
float pressure {-1};
|
||||||
float altitude {-1};
|
float altitude {-1};
|
||||||
float temperature {-1};
|
float temperature {-1};
|
||||||
bool success {false};
|
bool success {false};
|
||||||
|
|
||||||
void toJson(JsonVariant v) const override {
|
|
||||||
JsonArray arr = v.to<JsonArray>();
|
|
||||||
arr.add(pressure);
|
|
||||||
arr.add(altitude);
|
|
||||||
arr.add(temperature);
|
|
||||||
arr.add(success);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fromJson(JsonVariantConst v) override {
|
|
||||||
JsonArrayConst arr = v.as<JsonArrayConst>();
|
|
||||||
pressure = arr[0] | -1.0f;
|
|
||||||
altitude = arr[1] | -1.0f;
|
|
||||||
temperature = arr[2] | -1.0f;
|
|
||||||
success = arr[3] | false;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend void toJson(JsonVariant v, BarometerMsg const& a) { a.toJson(v); }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Barometer : public SensorBase<BarometerMsg> {
|
class Barometer : public SensorBase<BarometerMsg> {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <ArduinoJson.h>
|
|
||||||
|
|
||||||
enum gesture_t : uint16_t {
|
enum gesture_t : uint16_t {
|
||||||
eGestureNone = 0x0000,
|
eGestureNone = 0x0000,
|
||||||
@@ -21,16 +20,6 @@ enum orient_t : uint8_t { kRot0 = 0, kRot90, kRot180, kRot270 };
|
|||||||
struct GestureMsg {
|
struct GestureMsg {
|
||||||
gesture_t gesture {eGestureNone};
|
gesture_t gesture {eGestureNone};
|
||||||
bool success {false};
|
bool success {false};
|
||||||
friend void toJson(JsonVariant v, GestureMsg const& a) {
|
|
||||||
JsonArray arr = v.to<JsonArray>();
|
|
||||||
arr.add(static_cast<uint16_t>(a.gesture));
|
|
||||||
arr.add(a.success);
|
|
||||||
}
|
|
||||||
void fromJson(JsonVariantConst o) {
|
|
||||||
JsonArrayConst arr = o.as<JsonArrayConst>();
|
|
||||||
gesture = static_cast<gesture_t>(arr[0].as<uint16_t>());
|
|
||||||
success = arr[1].as<bool>();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class PAJ7620U2 {
|
class PAJ7620U2 {
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <utils/math_utils.h>
|
#include <utils/math_utils.h>
|
||||||
|
|
||||||
#if FT_ENABLED(USE_MPU6050)
|
#if FT_ENABLED(USE_MPU6050)
|
||||||
@@ -16,30 +14,10 @@
|
|||||||
|
|
||||||
#include <peripherals/sensor.hpp>
|
#include <peripherals/sensor.hpp>
|
||||||
|
|
||||||
struct IMUAnglesMsg : public SensorMessageBase {
|
struct IMUAnglesMsg {
|
||||||
float rpy[3] {0, 0, 0};
|
float rpy[3] {0, 0, 0};
|
||||||
float temperature {-1};
|
float temperature {-1};
|
||||||
bool success {false};
|
bool success {false};
|
||||||
|
|
||||||
void toJson(JsonVariant v) const override {
|
|
||||||
JsonArray arr = v.to<JsonArray>();
|
|
||||||
arr.add(rpy[0]);
|
|
||||||
arr.add(rpy[1]);
|
|
||||||
arr.add(rpy[2]);
|
|
||||||
arr.add(temperature);
|
|
||||||
arr.add(success);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fromJson(JsonVariantConst v) override {
|
|
||||||
JsonArrayConst arr = v.as<JsonArrayConst>();
|
|
||||||
rpy[0] = arr[0] | -1.0f;
|
|
||||||
rpy[1] = arr[1] | -1.0f;
|
|
||||||
rpy[2] = arr[2] | -1.0f;
|
|
||||||
temperature = arr[3] | -1.0f;
|
|
||||||
success = arr[4] | false;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend void toJson(JsonVariant v, IMUAnglesMsg const& a) { a.toJson(v); }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class IMU : public SensorBase<IMUAnglesMsg> {
|
class IMU : public SensorBase<IMUAnglesMsg> {
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <utils/math_utils.h>
|
#include <utils/math_utils.h>
|
||||||
|
|
||||||
#include <Adafruit_HMC5883_U.h>
|
#include <Adafruit_HMC5883_U.h>
|
||||||
@@ -11,29 +9,10 @@
|
|||||||
|
|
||||||
#include <peripherals/sensor.hpp>
|
#include <peripherals/sensor.hpp>
|
||||||
|
|
||||||
struct MagnetometerMsg : public SensorMessageBase {
|
struct MagnetometerMsg {
|
||||||
float rpy[3] {0, 0, 0};
|
float rpy[3] {0, 0, 0};
|
||||||
float heading {-1};
|
float heading {-1};
|
||||||
|
bool success {false};
|
||||||
void toJson(JsonVariant v) const override {
|
|
||||||
JsonArray arr = v.to<JsonArray>();
|
|
||||||
arr.add(rpy[0]);
|
|
||||||
arr.add(rpy[1]);
|
|
||||||
arr.add(rpy[2]);
|
|
||||||
arr.add(heading);
|
|
||||||
arr.add(success);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fromJson(JsonVariantConst v) override {
|
|
||||||
JsonArrayConst arr = v.as<JsonArrayConst>();
|
|
||||||
rpy[0] = arr[0] | 0.0f;
|
|
||||||
rpy[1] = arr[1] | 0.0f;
|
|
||||||
rpy[2] = arr[2] | 0.0f;
|
|
||||||
heading = arr[3] | -1.0f;
|
|
||||||
success = arr[4] | false;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend void toJson(JsonVariant v, MagnetometerMsg const& a) { a.toJson(v); }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Magnetometer : public SensorBase<MagnetometerMsg> {
|
class Magnetometer : public SensorBase<MagnetometerMsg> {
|
||||||
|
|||||||
@@ -47,14 +47,8 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
|
|||||||
|
|
||||||
void scanI2C(uint8_t lower = 1, uint8_t higher = 127);
|
void scanI2C(uint8_t lower = 1, uint8_t higher = 127);
|
||||||
|
|
||||||
void getI2CResult(JsonVariant &root);
|
void getI2CScanProto(socket_message_I2CScanData &data);
|
||||||
void getI2CResultProto(socket_message_I2CScanData &data);
|
|
||||||
|
|
||||||
void getIMUResult(JsonVariant &root);
|
|
||||||
void getIMUProto(socket_message_IMUData &data);
|
void getIMUProto(socket_message_IMUData &data);
|
||||||
|
|
||||||
void getSonarResult(JsonVariant &root);
|
|
||||||
|
|
||||||
void getSettingsProto(socket_message_PeripheralSettingsData &data);
|
void getSettingsProto(socket_message_PeripheralSettingsData &data);
|
||||||
|
|
||||||
/* IMU FUNCTIONS */
|
/* IMU FUNCTIONS */
|
||||||
@@ -92,8 +86,6 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
|
|||||||
|
|
||||||
inline void endTransaction() { xSemaphoreGiveRecursive(_accessMutex); }
|
inline void endTransaction() { xSemaphoreGiveRecursive(_accessMutex); }
|
||||||
|
|
||||||
JsonDocument doc;
|
|
||||||
char message[MAX_ESP_IMU_SIZE];
|
|
||||||
#if FT_ENABLED(USE_MPU6050 || USE_BNO055)
|
#if FT_ENABLED(USE_MPU6050 || USE_BNO055)
|
||||||
IMU _imu;
|
IMU _imu;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,30 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
|
|
||||||
struct SensorMessageBase {
|
|
||||||
bool success;
|
|
||||||
virtual void toJson(JsonVariant v) const = 0;
|
|
||||||
virtual void fromJson(JsonVariantConst v) = 0;
|
|
||||||
|
|
||||||
virtual ~SensorMessageBase() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class SensorBase {
|
class SensorBase {
|
||||||
static_assert(std::is_base_of<SensorMessageBase, T>::value, "T must inherit from SensorMessageBase");
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SensorBase() {}
|
|
||||||
|
|
||||||
virtual bool initialize() = 0;
|
virtual bool initialize() = 0;
|
||||||
|
|
||||||
virtual bool update() = 0;
|
virtual bool update() = 0;
|
||||||
|
|
||||||
virtual void getResults(JsonVariant &root) { _msg.toJson(root); }
|
|
||||||
|
|
||||||
virtual T getResult() { return _msg; }
|
|
||||||
|
|
||||||
virtual bool isActive() { return _msg.success; }
|
virtual bool isActive() { return _msg.success; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
+1
-1
@@ -155,7 +155,7 @@ void setupEventSocket() {
|
|||||||
socket.on<socket_message_I2CScanDataRequest>([&](const socket_message_I2CScanDataRequest &data, int clientId) {
|
socket.on<socket_message_I2CScanDataRequest>([&](const socket_message_I2CScanDataRequest &data, int clientId) {
|
||||||
peripherals.scanI2C();
|
peripherals.scanI2C();
|
||||||
socket_message_I2CScanData result = socket_message_I2CScanData_init_zero;
|
socket_message_I2CScanData result = socket_message_I2CScanData_init_zero;
|
||||||
peripherals.getI2CResultProto(result);
|
peripherals.getI2CScanProto(result);
|
||||||
socket.emit(result, clientId);
|
socket.emit(result, clientId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -52,18 +52,7 @@ void Peripherals::updatePins() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Peripherals::getI2CResult(JsonVariant &root) {
|
void Peripherals::getI2CScanProto(socket_message_I2CScanData &data) {
|
||||||
char output[150];
|
|
||||||
root["sda"] = state().sda;
|
|
||||||
root["scl"] = state().scl;
|
|
||||||
JsonArray addresses = root["addresses"].to<JsonArray>();
|
|
||||||
for (auto &address : addressList) {
|
|
||||||
addresses.add(address);
|
|
||||||
}
|
|
||||||
ESP_LOGI("Peripherals", "Emitting I2C scan results: %d", addressList.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Peripherals::getI2CResultProto(socket_message_I2CScanData &data) {
|
|
||||||
data.devices_count = 0;
|
data.devices_count = 0;
|
||||||
for (auto &address : addressList) {
|
for (auto &address : addressList) {
|
||||||
if (data.devices_count >= 16) break;
|
if (data.devices_count >= 16) break;
|
||||||
@@ -196,29 +185,6 @@ gesture_t Peripherals::takeGesture() {
|
|||||||
float Peripherals::leftDistance() { return _left_distance; }
|
float Peripherals::leftDistance() { return _left_distance; }
|
||||||
float Peripherals::rightDistance() { return _right_distance; }
|
float Peripherals::rightDistance() { return _right_distance; }
|
||||||
|
|
||||||
void Peripherals::getIMUResult(JsonVariant &root) {
|
|
||||||
#if FT_ENABLED(USE_MPU6050 || USE_BNO055)
|
|
||||||
JsonVariant imu = root["imu"].to<JsonVariant>();
|
|
||||||
_imu.getResults(imu);
|
|
||||||
#endif
|
|
||||||
#if FT_ENABLED(USE_HMC5883)
|
|
||||||
JsonVariant mag = root["mag"].to<JsonVariant>();
|
|
||||||
_mag.getResults(mag);
|
|
||||||
#endif
|
|
||||||
#if FT_ENABLED(USE_BMP180)
|
|
||||||
JsonVariant bmp = root["bmp"].to<JsonVariant>();
|
|
||||||
_bmp.getResults(bmp);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void Peripherals::getSonarResult(JsonVariant &root) {
|
|
||||||
#if FT_ENABLED(USE_USS)
|
|
||||||
JsonArray array = root.to<JsonArray>();
|
|
||||||
array[0] = _left_distance;
|
|
||||||
array[1] = _right_distance;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Peripherals::calibrateIMU() {
|
bool Peripherals::calibrateIMU() {
|
||||||
#if FT_ENABLED(USE_MPU6050 || USE_BNO055)
|
#if FT_ENABLED(USE_MPU6050 || USE_BNO055)
|
||||||
beginTransaction();
|
beginTransaction();
|
||||||
|
|||||||
Reference in New Issue
Block a user