Skyward boardcore
Loading...
Searching...
No Matches
AnalogLoadCell.h
Go to the documentation of this file.
1/* Copyright (c) 2022 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
29#include "AnalogLoadCellData.h"
30
31namespace Boardcore
32{
33
34class AnalogLoadCell : public Sensor<AnalogLoadCellData>
35{
36public:
37 AnalogLoadCell(std::function<ADCData()> getVoltage, const float mVtoV,
38 const unsigned int fullScale, const float supplyVoltage = 5)
39 : getVoltage(getVoltage),
40 conversionCoeff(mVtoV * supplyVoltage / fullScale / 1e3)
41 {
42 }
43
44 bool init() override { return true; };
45
46 bool selfTest() override { return true; };
47
51 void setOffset(const float offset) { this->offset = offset; }
52
53 void updateOffset(float offset) { this->offset += offset; }
54
55protected:
57 {
58 ADCData adcData = getVoltage();
59
61 data.loadTimestamp = adcData.voltageTimestamp;
62 data.voltage = adcData.voltage;
63
64 if (data.voltage != 0)
65 data.load = data.voltage / conversionCoeff - offset;
66 else
67 data.load = -offset;
68
69 return data;
70 }
71
72private:
74 std::function<ADCData()> getVoltage;
75
76 const float conversionCoeff;
77 float offset = 0;
78};
79
80} // namespace Boardcore
AnalogLoadCell(std::function< ADCData()> getVoltage, const float mVtoV, const unsigned int fullScale, const float supplyVoltage=5)
bool selfTest() override
Check if the sensor is working.
void updateOffset(float offset)
bool init() override
Initialize the sensor.
AnalogLoadCellData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
void setOffset(const float offset)
Sets the offset that will be removed from the measured load.
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