diff --git a/src/shared/sensors/analog/pressure/MPXHZ6130A/MPXHZ6130A.h b/src/shared/sensors/analog/pressure/MPXHZ6130A/MPXHZ6130A.h index 9ab705349a7685e0acec5d45a4f08f1285de59ee..2fdb8688102da0f44af4d82ad84045fa70e1c69b 100644 --- a/src/shared/sensors/analog/pressure/MPXHZ6130A/MPXHZ6130A.h +++ b/src/shared/sensors/analog/pressure/MPXHZ6130A/MPXHZ6130A.h @@ -22,7 +22,9 @@ #pragma once -#include "../AnalogPressureSensor.h" +#include <sensors/analog/pressure/AnalogPressureSensor.h> +#include <utils/Stats/Stats.h> + #include "MPXHZ6130AData.h" namespace Boardcore @@ -34,51 +36,7 @@ namespace Boardcore class MPXHZ6130A final : public AnalogPressureSensor<MPXHZ6130AData> { public: - MPXHZ6130A(std::function<ADCData()> getSensorVoltage, - const float supplyVoltage = 5.0, - const unsigned int num_calib_samples_ = 200, - const float moving_avg_coeff_ = 1.0, - const float ref_press_ = 101325.0f) - : AnalogPressureSensor(getSensorVoltage, supplyVoltage, 130000), - offset(0.0), num_calib_samples(num_calib_samples_), - moving_avg_coeff(moving_avg_coeff_), ref_press(ref_press_) - { - } - - MPXHZ6130AData sampleImpl() - { - lastSample = AnalogPressureSensor<MPXHZ6130AData>::sampleImpl(); - - if (calibrating) - { - press_stats.add(lastSample.pressure); - - if (press_stats.getStats().nSamples >= num_calib_samples) - { - calibrating = false; - offset = ref_press - press_stats.getStats().mean; - - TRACE("MPXHZ6130A barometer offset : %.2f \n", offset); - } - } - - lastSample.pressure = lastSample.pressure + offset; - - lastSample.pressure = movingAverage(lastSample.pressure); - - return lastSample; - } - - void setReferencePressure(float p) { ref_press = p; } - - void calibrate() - { - press_stats.reset(); - offset = 0.0f; - calibrating = true; - } - - bool isCalibrating() { return calibrating; } + using AnalogPressureSensor<MPXHZ6130AData>::AnalogPressureSensor; private: float voltageToPressure(float voltage) override @@ -86,27 +44,9 @@ private: return (((voltage / supplyVoltage) + CONST_B) / CONST_A) * 1000; } - float movingAverage(float new_value) - { - accumulator = (moving_avg_coeff * new_value) + - (1.0 - moving_avg_coeff) * accumulator; - return accumulator; - } - // Constants from datasheet static constexpr float CONST_A = 0.007826; static constexpr float CONST_B = 0.07739; - - bool calibrating = false; - float offset; - Stats press_stats; - unsigned int num_calib_samples; - - // moving average - const float moving_avg_coeff; - float accumulator = 0.0; - - float ref_press; }; } // namespace Boardcore diff --git a/src/shared/sensors/analog/pressure/honeywell/SSCDRRN015PDA.h b/src/shared/sensors/analog/pressure/honeywell/SSCDRRN015PDA.h index 1e2785023c487613b58f094d19113e9d5fe51c6f..088f18adef9fba276b4e33aa2b53a744a3e2c09b 100644 --- a/src/shared/sensors/analog/pressure/honeywell/SSCDRRN015PDA.h +++ b/src/shared/sensors/analog/pressure/honeywell/SSCDRRN015PDA.h @@ -40,47 +40,9 @@ public: const float supplyVoltage = 5.0, const unsigned int num_calib_samples_ = 200) : HoneywellPressureSensor(getSensorVoltage, supplyVoltage, 103421.3594, - -103421.3594), - offset(0.0), num_calib_samples(num_calib_samples_) + -103421.3594) { } - - SSCDRRN015PDAData sampleImpl() override - { - lastSample = HoneywellPressureSensor<SSCDRRN015PDAData>::sampleImpl(); - - if (calibrating) - { - press_stats.add(lastSample.pressure); - - if (press_stats.getStats().nSamples >= num_calib_samples) - { - calibrating = false; - offset = press_stats.getStats().mean; - - TRACE("Differential barometer offset : %.2f \n", offset); - } - } - - lastSample.pressure = lastSample.pressure - offset; - - return lastSample; - } - - void calibrate() - { - press_stats.reset(); - offset = 0.0f; - calibrating = true; - } - - bool isCalibrating() { return calibrating; } - -private: - bool calibrating = false; - float offset; - Stats press_stats; - unsigned int num_calib_samples; }; } // namespace Boardcore diff --git a/src/shared/utils/Stats/Stats.h b/src/shared/utils/Stats/Stats.h index 3d57492c1c5bdd1a82ef15a217ed5db9d97c23cc..72fa685b976293b14e55b1b098795bf49d59cf32 100644 --- a/src/shared/utils/Stats/Stats.h +++ b/src/shared/utils/Stats/Stats.h @@ -28,15 +28,15 @@ namespace Boardcore { /** - * @brief Statisitics computed by the Stats class. + * @brief Statistics computed by the Stats class. */ struct StatsResult { - float minValue; ///< Min value found so far - float maxValue; ///< Max value found so far - float mean; ///< Mean of datased - float stdDev; ///< Standard deviation of datset - unsigned int nSamples; ///< Number of samples + float minValue; ///< Min value found so far. + float maxValue; ///< Max value found so far. + float mean; ///< Mean of dataset. + float stdDev; ///< Standard deviation of dataset. + uint32_t nSamples; ///< Number of samples. }; /** @@ -45,35 +45,27 @@ struct StatsResult std::ostream& operator<<(std::ostream& os, const StatsResult& sr); /** - * @brief Computes on-line statisitics of a dataset. This class should - * theoretically work with datasets of up to 2^32-1 elements + * @brief Computes on-line statistics of a dataset. + * + * This class should theoretically work with datasets of up to 2^32-1 elements. */ class Stats { public: - /** - * Constructor - */ Stats(); - /** - * Add an element - */ void add(float data); - /** - * Reset all the stats - */ void reset(); /** - * Return statistics of the elements added so far + * @brief Return statistics of the elements added so far. */ StatsResult getStats() const; private: float minValue, maxValue, mean, m2; - unsigned int n; + uint32_t n; }; } // namespace Boardcore