⚡ Replace third party libs with i2c bus drivers
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Wire.h>
|
||||
#include <utils/math_utils.h>
|
||||
|
||||
#include <Adafruit_HMC5883_U.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
|
||||
#include <peripherals/sensor.hpp>
|
||||
#include <peripherals/drivers/hmc5883l.h>
|
||||
|
||||
struct MagnetometerMsg {
|
||||
float rpy[3] {0, 0, 0};
|
||||
@@ -19,34 +14,29 @@ class Magnetometer : public SensorBase<MagnetometerMsg> {
|
||||
public:
|
||||
bool initialize() override {
|
||||
_msg.success = _mag.begin();
|
||||
if (_msg.success) {
|
||||
ESP_LOGI("MAG", "HMC5883L initialized successfully");
|
||||
} else {
|
||||
ESP_LOGE("MAG", "HMC5883L initialization failed");
|
||||
}
|
||||
return _msg.success;
|
||||
}
|
||||
|
||||
bool update() override {
|
||||
if (!_msg.success) return false;
|
||||
sensors_event_t event;
|
||||
bool updated = _mag.getEvent(&event);
|
||||
if (!updated) return false;
|
||||
_msg.rpy[0] = event.magnetic.x;
|
||||
_msg.rpy[1] = event.magnetic.y;
|
||||
_msg.rpy[2] = event.magnetic.z;
|
||||
_msg.heading = atan2(event.magnetic.y, event.magnetic.x);
|
||||
_msg.heading += declinationAngle;
|
||||
if (_msg.heading < 0) _msg.heading += 2 * PI;
|
||||
if (_msg.heading > 2 * PI) _msg.heading -= 2 * PI;
|
||||
_msg.heading *= 180 / M_PI;
|
||||
if (!_mag.update()) return false;
|
||||
_msg.rpy[0] = _mag.getMagX();
|
||||
_msg.rpy[1] = _mag.getMagY();
|
||||
_msg.rpy[2] = _mag.getMagZ();
|
||||
_msg.heading = _mag.getHeading();
|
||||
return true;
|
||||
}
|
||||
|
||||
float getMagX() { return _msg.rpy[0]; }
|
||||
|
||||
float getMagY() { return _msg.rpy[1]; }
|
||||
|
||||
float getMagZ() { return _msg.rpy[2]; }
|
||||
|
||||
float getHeading() { return _msg.heading; }
|
||||
|
||||
private:
|
||||
Adafruit_HMC5883_Unified _mag {12345};
|
||||
const float declinationAngle = 0.22;
|
||||
};
|
||||
HMC5883LDriver _mag;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user