Code refactoring

This commit is contained in:
2024-01-27 21:35:57 +07:00
parent 2aa9648dd4
commit fa0a664e43
14 changed files with 57 additions and 40 deletions

View File

@@ -64,7 +64,7 @@ class Barometer {
GyverBME280 *_bme;
float _startedAltitude = 0;
bool _isCalibration = false;
uint32_t _calibrationTimer;
uint32_t _calibrationTimer = 0;
int _calibrationIterationsCounter = 0;
OnMeasuringFinishedCb _measuaringHeightFinishedCb = nullptr;
static constexpr int _calibrationIterationsCount = 1500;

View File

@@ -196,17 +196,17 @@ class MPU {
private:
MPU6050_6Axis_MotionApps20 *_mpu = nullptr;
float _ypr[3];
float _ax, _ay, _az;
float _gx, _gy, _gz;
float _gravity;
float _ypr[3] = { 0 };
float _ax = 0, _ay = 0, _az = 0;
float _gx = 0, _gy = 0, _gz = 0;
float _gravity = 0;
float _AccZInertial = 0;
float _axOffset = 0, _ayOffset = 0, _azOffset = 0;
float _gxOffset = 0, _gyOffset = 0, _gzOffset = 0;
float _prOffset[2]; // yaw isn't used
float _prOffset[2] = { 0 }; // yaw isn't used
uint8_t _fifoBuffer[45];
uint8_t _fifoBuffer[45] = { 0 };
Preferences _preferences;
const char *_TAG = "MPU6050 module";

View File

@@ -1,6 +1,10 @@
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
#include "Sensors.hpp"
Sensors::Sensors(Barometer *barometer, MPU *mpu, Kalman2DFilter *filter, BatteryController *battery, bool loop_on_fail) {
Sensors::Sensors(Barometer *barometer, MPU *mpu, Kalman2DFilter *filter,
BatteryController *battery, volatile bool loop_on_fail) {
assert(barometer != nullptr);
assert(mpu != nullptr);
assert(filter != nullptr);
@@ -56,7 +60,7 @@ float Sensors::rawFlightHeight(void) const {
float Sensors::flightHeight(void) const {
/* Returns flight height from cache immediatly */
if (_flightHeightFromBarometer == NAN) {
if (_flightHeightFromBarometer == 0.f) {
ESP_LOGW(_tag, "flightHeight called, but tick() has called never");
return rawFlightHeight();
} else {
@@ -65,7 +69,7 @@ float Sensors::flightHeight(void) const {
}
MpuData Sensors::mpuData() const {
if (_mpuData.ax == NAN and _mpuData.gravity == NAN) {
if (_mpuData.ax == 0.f and _mpuData.gravity == 0.f) {
ESP_LOGW(_tag, "MPU data looks like .tick() has never called");
}
return _mpuData;
@@ -78,7 +82,8 @@ bool Sensors::tick(void) {
bool err;
bool isMpuDataReady = _mpu->tick(err);
if (isMpuDataReady and !err) {
_2d_filter->filter(_mpu->accZInertial(), _barometer->flightHeight(), _flightHeightFromBarometer, _zVelocityAltitude);
_2d_filter->filter(_mpu->accZInertial(), _barometer->flightHeight(),
_flightHeightFromBarometer, _zVelocityAltitude);
_mpuData = { .ax = _mpu->ax(),
.ay = _mpu->ay(),
.az = _mpu->az(),

View File

@@ -16,7 +16,8 @@ struct MpuData {
class Sensors {
public:
Sensors(Barometer *barometer, MPU *mpu, Kalman2DFilter *filter, BatteryController *battery, bool loop_on_fail = true);
Sensors(Barometer *barometer, MPU *mpu, Kalman2DFilter *filter,
BatteryController *battery, bool loop_on_fail = true);
~Sensors();
@@ -42,9 +43,9 @@ class Sensors {
Kalman2DFilter *_2d_filter = nullptr;
/* cached filtered values */
float _flightHeightFromBarometer = NAN;
float _zVelocityAltitude = NAN;
MpuData _mpuData = { NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN };
float _flightHeightFromBarometer = 0.f;
float _zVelocityAltitude = 0.f;
MpuData _mpuData = { 0.f };
static constexpr const char *_tag = "Sensors";