diff --git a/src/shared/sensors/analog/TrafagPressureSensor.h b/src/shared/sensors/analog/TrafagPressureSensor.h new file mode 100644 index 0000000000000000000000000000000000000000..7292b70de3591234e73e3bd95818029deaac2cd7 --- /dev/null +++ b/src/shared/sensors/analog/TrafagPressureSensor.h @@ -0,0 +1,88 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Authors: Davide Mor + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <sensors/Sensor.h> +#include <sensors/SensorData.h> + +#include <functional> + +namespace Boardcore +{ + +/** + * @brief Sensor class for a Trafag pressure sensor. + */ +class TrafagPressureSensor : public Sensor<PressureData> +{ +public: + /** + * @brief Construct a TrafagPressureSensor. + * + * @param getVoltage lambda to retrieve current voltage. + * @param shuntResistance shunt resistance value. + * @param maxPressure maximum pressure of this sensor. + * @param minCurrent current at the 0 pressure point. + * @param maxCurrent current at the maximum pressure point. + */ + TrafagPressureSensor(std::function<ADCData()> getVoltage, + float shuntResistance, float maxPressure, + float minCurrent = 4, float maxCurrent = 20) + : getVoltage{getVoltage}, shuntResistance{shuntResistance}, + maxPressure{maxPressure}, minCurrent{minCurrent}, maxCurrent{ + maxCurrent} + { + } + + bool init() override { return true; } + + bool selfTest() override { return true; } + +private: + PressureData sampleImpl() override + { + auto voltage = getVoltage(); + return {voltage.voltageTimestamp, voltageToPressure(voltage.voltage)}; + } + + float voltageToPressure(float voltage) + { + // First convert voltage to current + float current = (voltage / shuntResistance) * 1000.0f; + + // Convert to a value between 0 and 1 + float value = (current - minCurrent) / (maxCurrent - minCurrent); + + // Scale from 0 to maxPressure + return value * maxPressure; + } + + std::function<ADCData()> getVoltage; + + const float shuntResistance; + const float maxPressure; + const float minCurrent; + const float maxCurrent; +}; + +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/sensors/analog/TwoPointAnalogLoadCell.h b/src/shared/sensors/analog/TwoPointAnalogLoadCell.h new file mode 100644 index 0000000000000000000000000000000000000000..e2efa14dcf9a40f60a258c7ab712b94db13ccad8 --- /dev/null +++ b/src/shared/sensors/analog/TwoPointAnalogLoadCell.h @@ -0,0 +1,99 @@ +/* Copyright (c) 2024 Skyward Experimental Rocketry + * Authors: Davide Mor + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <sensors/Sensor.h> +#include <sensors/SensorData.h> + +#include <functional> + +namespace Boardcore +{ + +/** + * @brief Sensor class for a two point calibrated load cell. + */ +class TwoPointAnalogLoadCell : public Sensor<LoadCellData> +{ +public: + /** + * @brief Construct a TwoPointAnalogLoadCell. + * + * @param getVoltage lambda to retrieve current voltage. + * @param p0Voltage voltage of point 0. + * @param p0Mass mass of point 0. + * @param p1Voltage voltage of point 1. + * @param p1Mass mass of point 1. + */ + TwoPointAnalogLoadCell(std::function<ADCData()> getVoltage, float p0Voltage, + float p0Mass, float p1Voltage, float p1Mass) + : getVoltage{getVoltage}, staticScale{(p1Mass - p0Mass) / + (p1Voltage - p0Voltage)}, + staticOffset{p0Mass - staticScale * p0Voltage} + { + } + + bool init() override { return true; } + + bool selfTest() override { return true; } + + /** + * @brief Set the current load cell offset. Ignores any previous offsets. + */ + void setOffset(float value) + { + miosix::Lock<miosix::FastMutex> lock{offsetMutex}; + dynamicOffset = value; + } + + /** + * @brief Update the current load cell offset. Adds the new value to the old + * offset. + */ + void updateOffset(float value) + { + miosix::Lock<miosix::FastMutex> lock{offsetMutex}; + dynamicOffset += value; + } + +private: + LoadCellData sampleImpl() override + { + auto voltage = getVoltage(); + auto mass = -(voltage.voltage * staticScale + staticOffset); + + miosix::Lock<miosix::FastMutex> lock{offsetMutex}; + return {voltage.voltageTimestamp, mass - dynamicOffset}; + } + + std::function<ADCData()> getVoltage; + + // std::atomic<float> does not support += + miosix::FastMutex offsetMutex; + float dynamicOffset = 0.0f; + + const float staticScale; + const float staticOffset; +}; + +} // namespace Boardcore \ No newline at end of file