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();
#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}
{
}
{
}
-// 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()