Skyward boardcore
Loading...
Searching...
No Matches
CurrentSensor.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Conterio
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
37class CurrentSensor : public Sensor<CurrentData>
38{
39public:
40 static constexpr int MOVING_AVERAGE_N = 20;
41
42 CurrentSensor(std::function<ADCData()> getVoltage,
43 std::function<float(float)> voltageToCurrent)
44 : getVoltage(getVoltage), voltageToCurrent(voltageToCurrent)
45 {
47 }
48
49 bool init() override { return true; };
50
51 bool selfTest() override { return true; };
52
53protected:
56 {
57 ADCData adc_data = getVoltage();
58
59 if (lastSample.current == 0)
60 lastSample.current = voltageToCurrent(adc_data.voltage);
61
62 CurrentData current_data;
63 current_data.currentTimestamp = adc_data.voltageTimestamp;
64
65 // Moving average
66 current_data.current = lastSample.current * MOVING_AVERAGE_COMP_COEFF;
67 current_data.current +=
68 voltageToCurrent(adc_data.voltage) * MOVING_AVERAGE_COEFF;
69
70 return current_data;
71 };
72
73private:
75 std::function<ADCData()> getVoltage;
76
78 std::function<float(float)> voltageToCurrent;
79
80 static constexpr float MOVING_AVERAGE_COEFF = 1 / (float)MOVING_AVERAGE_N;
81 static constexpr float MOVING_AVERAGE_COMP_COEFF = 1 - MOVING_AVERAGE_COEFF;
82};
83
84} // namespace Boardcore
Common class for current sensors.
bool selfTest() override
Check if the sensor is working.
static constexpr int MOVING_AVERAGE_N
CurrentSensor(std::function< ADCData()> getVoltage, std::function< float(float)> voltageToCurrent)
bool init() override
Initialize the sensor.
CurrentData sampleImpl() override
< Converts the voltage value to pressure
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.
Structure to handle ADC data.
Definition SensorData.h:356
Structure to handle current data.
Definition SensorData.h:324