Skip to content
Snippets Groups Projects
Commit 129f21c5 authored by Alberto Nidasio's avatar Alberto Nidasio
Browse files

[AnalogPressureSensors] Removed calibration code

parent 4d987db0
Branches
Tags
No related merge requests found
...@@ -22,7 +22,9 @@ ...@@ -22,7 +22,9 @@
#pragma once #pragma once
#include "../AnalogPressureSensor.h" #include <sensors/analog/pressure/AnalogPressureSensor.h>
#include <utils/Stats/Stats.h>
#include "MPXHZ6130AData.h" #include "MPXHZ6130AData.h"
namespace Boardcore namespace Boardcore
...@@ -34,51 +36,7 @@ namespace Boardcore ...@@ -34,51 +36,7 @@ namespace Boardcore
class MPXHZ6130A final : public AnalogPressureSensor<MPXHZ6130AData> class MPXHZ6130A final : public AnalogPressureSensor<MPXHZ6130AData>
{ {
public: public:
MPXHZ6130A(std::function<ADCData()> getSensorVoltage, using AnalogPressureSensor<MPXHZ6130AData>::AnalogPressureSensor;
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; }
private: private:
float voltageToPressure(float voltage) override float voltageToPressure(float voltage) override
...@@ -86,27 +44,9 @@ private: ...@@ -86,27 +44,9 @@ private:
return (((voltage / supplyVoltage) + CONST_B) / CONST_A) * 1000; 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 // Constants from datasheet
static constexpr float CONST_A = 0.007826; static constexpr float CONST_A = 0.007826;
static constexpr float CONST_B = 0.07739; 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 } // namespace Boardcore
...@@ -40,47 +40,9 @@ public: ...@@ -40,47 +40,9 @@ public:
const float supplyVoltage = 5.0, const float supplyVoltage = 5.0,
const unsigned int num_calib_samples_ = 200) const unsigned int num_calib_samples_ = 200)
: HoneywellPressureSensor(getSensorVoltage, supplyVoltage, 103421.3594, : HoneywellPressureSensor(getSensorVoltage, supplyVoltage, 103421.3594,
-103421.3594), -103421.3594)
offset(0.0), num_calib_samples(num_calib_samples_)
{ {
} }
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 } // namespace Boardcore
...@@ -28,15 +28,15 @@ namespace Boardcore ...@@ -28,15 +28,15 @@ namespace Boardcore
{ {
/** /**
* @brief Statisitics computed by the Stats class. * @brief Statistics computed by the Stats class.
*/ */
struct StatsResult struct StatsResult
{ {
float minValue; ///< Min value found so far float minValue; ///< Min value found so far.
float maxValue; ///< Max value found so far float maxValue; ///< Max value found so far.
float mean; ///< Mean of datased float mean; ///< Mean of dataset.
float stdDev; ///< Standard deviation of datset float stdDev; ///< Standard deviation of dataset.
unsigned int nSamples; ///< Number of samples uint32_t nSamples; ///< Number of samples.
}; };
/** /**
...@@ -45,35 +45,27 @@ struct StatsResult ...@@ -45,35 +45,27 @@ struct StatsResult
std::ostream& operator<<(std::ostream& os, const StatsResult& sr); std::ostream& operator<<(std::ostream& os, const StatsResult& sr);
/** /**
* @brief Computes on-line statisitics of a dataset. This class should * @brief Computes on-line statistics of a dataset.
* theoretically work with datasets of up to 2^32-1 elements *
* This class should theoretically work with datasets of up to 2^32-1 elements.
*/ */
class Stats class Stats
{ {
public: public:
/**
* Constructor
*/
Stats(); Stats();
/**
* Add an element
*/
void add(float data); void add(float data);
/**
* Reset all the stats
*/
void reset(); void reset();
/** /**
* Return statistics of the elements added so far * @brief Return statistics of the elements added so far.
*/ */
StatsResult getStats() const; StatsResult getStats() const;
private: private:
float minValue, maxValue, mean, m2; float minValue, maxValue, mean, m2;
unsigned int n; uint32_t n;
}; };
} // namespace Boardcore } // namespace Boardcore
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment