Make the PressureSensor more flexible
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 4 Nov 2024 14:16:37 +0000 (15:16 +0100)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 4 Nov 2024 14:16:37 +0000 (15:16 +0100)
Add some parameter to PressureSensor to make using other sensors easier.

PressureSensor.h
PressureSensor.ino

index b3347f9d5ff50334deac63b16a78e73a113c6464..a5287f6f2e816f7233943f23db470bf362ffe0e5 100644 (file)
@@ -7,12 +7,19 @@ class PressureSensor
                typedef int pin;
 
                const pin       m_analog_pin;
+               const float     m_max_pressure;
+               const float m_resistance;       // in Ohms
 
-               static float    signal_to_pressure(int ad_signal);
+               static const float      MIN_CURRENT = 4e-3;                     // in A
+               static const float      MAX_CURRENT = 20e-3;            // in A
+               static const float      REFERENCE_VOLTAGE = 5;          // in V
+               static const int        CONVERSION_BIT_NUM = 10;
+
+               float   signal_to_pressure(int ad_signal);
 
        public:
                PressureSensor() = delete;
-               PressureSensor(pin analog_pin);
+               PressureSensor(pin analog_pin, float max_pressure = 10, float resistance = 240);
                PressureSensor(const PressureSensor &other) = delete;
                ~PressureSensor();
 
index a645a3c56ef05430ffc06507648d866eb0da3ffb..8a717f17f251f52400c24e928ee021b686126ad7 100644 (file)
@@ -1,7 +1,10 @@
 #include "PressureSensor.h"
 
-PressureSensor::PressureSensor(pin analog_pin)
+PressureSensor::PressureSensor(pin analog_pin,
+               float max_pressure, float resistance)
        : m_analog_pin{analog_pin}
+       , m_max_pressure{max_pressure}
+       , m_resistance{resistance}
 {
 }
 
@@ -9,17 +12,39 @@ PressureSensor::~PressureSensor()
 {
 }
 
-// The Pressure sensor gives a current I/mA = 4 + 1.6 * p/bar with p absolute
-// pressure from 0 bar to 10 bar (giving current between 4 mA and 20 mA).
-// This current is passed through a 240 Ω resistor giving a voltage between
-// 0.96 V and 4.8 V. Thus U/V = 0.96 + 0.384 * p/bar.
-// Using the 10-bit AD convertor we get a signal from 0 to 2^10 - 1 on the range
-// of 0 V to 5 V.
-// Hence signal = (2^10 - 1) / (5 V) * U = 1023 / 5 * (0.96 + 0.384 * p/bar)
-// And p/bar = 1 / 0.384 * (5 / (2^10 - 1) * signal - 0.96)
+// The Pressure sensor gives a current
+// I = MIN_CURRENT + I_R * p_ratio
+// where I_R is MAX_CURRENT - MIN_CURRENT (current range)
+// and p_ratio is p / m_max_pressure
+//
+// This current is passed through a resistor with m_resistance.
+// This resistance should be chosen so that at max current the voltage drop is
+// at most 4.7 V (for safe operation of board).
+// U = I * R = (MIN_CURRENT + I_R * p_ratio) * m_resistance
+//
+// Using the CONVERSION_BIT_NUM-bit AD convertor
+// we get a signal from 0 to 2^CONVERSION_BIT_NUM - 1 on the range
+// from 0 V to REFERENCE_VOLTAGE.
+// signal = U / REFERENCE_VOLTAGE * (2^CONVERSION_BIT_NUM - 1)
+//
+//          (MIN_CURRENT + (I_R) * p_ratio) * m_resistance
+// signal = ---------------------------------------------- * (2^CONVERSION_BIT_NUM - 1)
+//                       REFERENCE_VOLTAGE
+//
+//          /        signal * REFERENCE_VOLTAGE                       \    m_max_pressure
+// And p = |  ----------------------------------------- - MIN_CURRENT  | * --------------
+//          \ m_resistance * (2^CONVERSION_BIT_NUM - 1)               /         I_R
+
 float  PressureSensor::signal_to_pressure(int ad_signal)
 {
-       return (((static_cast<float>(ad_signal) * 5) / (2 << 10 - 1) - 0.96) / 0.384);
+       float   signal_str;
+       float   voltage;
+       float   current;
+
+       signal_str = static_cast<float>(ad_signal) / ((1 << CONVERSION_BIT_NUM) - 1);
+       voltage = signal_str * REFERENCE_VOLTAGE;
+       current = voltage / m_resistance;
+       return ((current - MIN_CURRENT) / (MAX_CURRENT - MIN_CURRENT) * m_max_pressure);
 }
 
 float  PressureSensor::get_pressure()