Skyward boardcore
Loading...
Searching...
No Matches
TwoPointAnalogLoadCell.h
Go to the documentation of this file.
1/* Copyright (c) 2024 Skyward Experimental Rocketry
2 * Author: Davide Mor, Niccolò Betto
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#include <sensors/SensorData.h>
27
28#include <atomic>
29#include <functional>
30
31namespace Boardcore
32{
33
37class TwoPointAnalogLoadCell : public Sensor<LoadCellData>
38{
39public:
49 TwoPointAnalogLoadCell(std::function<VoltageData()> getVoltage,
50 float p0Voltage, float p0Mass, float p1Voltage,
51 float p1Mass)
52 : getVoltage{std::move(getVoltage)},
53 staticScale{(p1Mass - p0Mass) / (p1Voltage - p0Voltage)},
54 staticOffset{p0Mass - staticScale * p0Voltage}
55 {
56 }
57
59 : getVoltage{std::move(other.getVoltage)},
60 dynamicOffset{other.dynamicOffset.load()},
61 staticScale{other.staticScale}, staticOffset{other.staticOffset}
62 {
63 }
64
65 bool init() override { return true; }
66
67 bool selfTest() override { return true; }
68
72 void setOffset(float value) { dynamicOffset = value; }
73
78 void updateOffset(float value)
79 {
80 float currentOffset = 0.0f;
81 // Compare/exchange loop because operator+= on atomic floats is only
82 // supported since C++20
83 do
84 {
85 currentOffset = dynamicOffset.load();
86 } while (!dynamicOffset.compare_exchange_weak(currentOffset,
87 currentOffset + value));
88 }
89
93 float getOffset() { return dynamicOffset; }
94
95protected:
97 {
98 auto voltage = getVoltage();
99 auto mass = voltage.voltage * staticScale + staticOffset;
100
101 return {voltage.voltageTimestamp, mass - dynamicOffset};
102 }
103
104private:
105 std::function<VoltageData()> getVoltage;
106
107 std::atomic<float> dynamicOffset{0.0f};
108
109 const float staticScale;
110 const float staticOffset;
111};
112
113} // namespace Boardcore
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
Sensor class for a two point calibrated load cell.
LoadCellData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
TwoPointAnalogLoadCell(TwoPointAnalogLoadCell &&other)
bool init() override
Initialize the sensor.
TwoPointAnalogLoadCell(std::function< VoltageData()> getVoltage, float p0Voltage, float p0Mass, float p1Voltage, float p1Mass)
Construct a TwoPointAnalogLoadCell.
bool selfTest() override
Check if the sensor is working.
void setOffset(float value)
Set the current load cell offset. Ignores any previous offsets.
float getOffset()
Retrieve the current offset.
void updateOffset(float value)
Update the current load cell offset. Adds the new value to the old offset.
This file includes all the types the logdecoder script will decode.
Definition WIZ5500.h:318
Structure to handle voltage data.
Definition SensorData.h:340