Skyward boardcore
Loading...
Searching...
No Matches
AnalogPressureSensor.h
Go to the documentation of this file.
1/* Copyright (c) 2020 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <sensors/Sensor.h>
26
27#include <functional>
28
29namespace Boardcore
30{
31
38template <typename AnalogPressureData>
39class AnalogPressureSensor : public Sensor<AnalogPressureData>
40{
41public:
43 const float supplyVoltage = 5.0,
44 const float maxPressure = 0,
45 const float minPressure = 0)
48 {
49 }
50
57
58 bool init() override { return true; }
59
60 bool selfTest() override { return true; }
61
66 void setOffset(float value)
67 {
68 miosix::Lock<miosix::FastMutex> lock{offsetMutex};
69 this->offset = value;
70 }
71
76 void updateOffset(float value)
77 {
78 miosix::Lock<miosix::FastMutex> lock{offsetMutex};
79 this->offset += value;
80 }
81
85 float getOffset()
86 {
87 miosix::Lock<miosix::FastMutex> lock{offsetMutex};
88 return offset;
89 }
90
91protected:
92 AnalogPressureData sampleImpl() override
93 {
94 AnalogPressureData pressure;
95
96 // Retrieve the voltage
97 ADCData voltage = getVoltage();
98
99 // Save the timestamp and convert the voltage
100 pressure.pressureTimestamp = voltage.voltageTimestamp;
101 pressure.pressure = voltageToPressure(voltage.voltage);
102
103 // Check if the pressure is in range
104 if (pressure.pressure < minPressure)
105 pressure.pressure = minPressure;
106 else if (pressure.pressure > maxPressure)
107 pressure.pressure = maxPressure;
108
109 // Apply offset correction
110 {
111 miosix::Lock<miosix::FastMutex> lock{offsetMutex};
112 pressure.pressure -= offset;
113 }
114
115 return pressure;
116 }
117
119 virtual float voltageToPressure(float voltage) = 0;
120
122 std::function<ADCData()> getVoltage;
123
124 // std::atomic<float> does not support +=
125 miosix::FastMutex offsetMutex;
126 float offset = 0;
127
128 const float supplyVoltage;
129 const float maxPressure;
130 const float minPressure;
131};
132
133} // namespace Boardcore
Common class for all analog pressure sensors.
bool init() override
Initialize the sensor.
void setOffset(float value)
Set the current analog pressure sensor offset. Ignores any previous offsets.
virtual float voltageToPressure(float voltage)=0
Function that returns the sensor voltage.
std::function< ADCData()> getVoltage
float getOffset()
Retrieve the current offset.
void updateOffset(float value)
Update the current analog pressure sensor offset. Adds the new value to the old offset.
AnalogPressureData sampleImpl() override
Conversion function from volts to pascals.
bool selfTest() override
Check if the sensor is working.
AnalogPressureSensor(std::function< ADCData()> getVoltage, const float supplyVoltage=5.0, const float maxPressure=0, const float minPressure=0)
AnalogPressureSensor(AnalogPressureSensor &&other)
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
This file includes all the types the logdecoder script will decode.
Definition WIZ5500.h:318
Structure to handle ADC data.
Definition SensorData.h:356
uint64_t voltageTimestamp
Definition SensorData.h:357