diff --git a/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h b/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h index 42c15d3f4713fb229af2ba783f24e48b3f37974a..b8e51a6d979cd173af1e3170f808de705d1e2691 100644 --- a/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h +++ b/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h @@ -76,6 +76,21 @@ inline Canbus::CanMessage toCanMessage(const TemperatureData& data) return message; } +inline Canbus::CanMessage toCanMessage(const CurrentData& data) +{ + Canbus::CanMessage message; + + uint32_t current; + memcpy(¤t, &(data.current), sizeof(current)); + + message.id = -1; + message.length = 1; + message.payload[0] = (data.currentTimestamp & ~0x3) << 30; + message.payload[0] |= current; + + return message; +} + inline PitotData pitotDataFromCanMessage(const Canbus::CanMessage& msg) { PitotData data; @@ -115,4 +130,16 @@ inline TemperatureData temperatureDataFromCanMessage( return data; } +inline CurrentData currentDataFromCanMessage(const Canbus::CanMessage& msg) +{ + CurrentData data; + + uint32_t current = msg.payload[0]; + memcpy(&(data.current), ¤t, sizeof(data.current)); + + data.currentTimestamp = (msg.payload[0] >> 30) & ~0x3; + + return data; +} + } // namespace Boardcore diff --git a/src/shared/logger/LogTypes.h b/src/shared/logger/LogTypes.h index 4d36569c99e8b0ec9beb8972ee11ed3db4c75379..82989363bcae3126d2b6244723c39cfded9f6155 100644 --- a/src/shared/logger/LogTypes.h +++ b/src/shared/logger/LogTypes.h @@ -54,7 +54,6 @@ #include <sensors/VN100/VN100Data.h> #include <sensors/analog/AnalogLoadCellData.h> #include <sensors/analog/BatteryVoltageSensorData.h> -#include <sensors/analog/CurrentSensorData.h> #include <sensors/analog/pressure/honeywell/HSCMAND015PAData.h> #include <sensors/analog/pressure/honeywell/HSCMRNN030PAData.h> #include <sensors/analog/pressure/honeywell/HSCMRNN160KAData.h> @@ -114,7 +113,7 @@ void registerTypes(Deserializer& ds) ds.registerType<VN100Data>(); ds.registerType<AnalogLoadCellData>(); ds.registerType<BatteryVoltageSensorData>(); - ds.registerType<CurrentSensorData>(); + ds.registerType<CurrentData>(); ds.registerType<HSCMAND015PAData>(); ds.registerType<HSCMRNN030PAData>(); ds.registerType<HSCMRNN160KAData>(); diff --git a/src/shared/sensors/SensorData.h b/src/shared/sensors/SensorData.h index 002ca80a728277c8a20c6b5375e029b68a71357a..01dcac4b1d842c757f8f5ed35641d530f193d643 100644 --- a/src/shared/sensors/SensorData.h +++ b/src/shared/sensors/SensorData.h @@ -272,4 +272,20 @@ struct ADCData float voltage = 0; }; +/** + * @brief Structure to handle current data. + */ +struct CurrentData +{ + uint64_t currentTimestamp = 0; + float current = 0; + + static std::string header() { return "timestamp,current\n"; } + + void print(std::ostream& os) const + { + os << currentTimestamp << "," << current << "\n"; + } +}; + } // namespace Boardcore diff --git a/src/shared/sensors/analog/CurrentSensor.h b/src/shared/sensors/analog/CurrentSensor.h index 478c0f4edaa39444f730932e36f486953ffd597a..035e2bbf7db011fb2b73dbd7f1e7717cef06654b 100644 --- a/src/shared/sensors/analog/CurrentSensor.h +++ b/src/shared/sensors/analog/CurrentSensor.h @@ -26,8 +26,6 @@ #include <functional> -#include "CurrentSensorData.h" - namespace Boardcore { @@ -36,7 +34,7 @@ namespace Boardcore * * It needs a transfer function to convert the read voltage into current. */ -class CurrentSensor : public Sensor<CurrentSensorData> +class CurrentSensor : public Sensor<CurrentData> { public: static constexpr int MOVING_AVERAGE_N = 20; @@ -53,7 +51,7 @@ public: bool selfTest() override { return true; }; ///< Converts the voltage value to pressure - CurrentSensorData sampleImpl() override + CurrentData sampleImpl() override { ADCData adc_data = getVoltage(); @@ -62,10 +60,8 @@ public: lastSample.current = voltageToCurrent(adc_data.voltage); } - CurrentSensorData current_data; - current_data.voltageTimestamp = adc_data.voltageTimestamp; - current_data.channelId = adc_data.channelId; - current_data.voltage = adc_data.voltage; + CurrentData current_data; + current_data.currentTimestamp = adc_data.voltageTimestamp; // Moving average current_data.current = lastSample.current * MOVING_AVERAGE_COMP_COEFF; diff --git a/src/shared/sensors/analog/CurrentSensorData.h b/src/shared/sensors/analog/CurrentSensorData.h deleted file mode 100644 index ba35efbcf9d3a06bc71b4a5323c15de00ba31711..0000000000000000000000000000000000000000 --- a/src/shared/sensors/analog/CurrentSensorData.h +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright (c) 2021 Skyward Experimental Rocketry - * Author: Alberto Nidasio - * - * 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/SensorData.h> - -namespace Boardcore -{ - -/** - * @brief Structure to handle current sense data. - */ -struct CurrentSensorData : public ADCData -{ - float current = 0; - - static std::string header() - { - return "voltageTimestamp,channelId,voltage,current\n"; - } - - void print(std::ostream& os) const - { - os << voltageTimestamp << "," << (int)channelId << "," << voltage << "," - << current << "\n"; - } -}; - -} // namespace Boardcore diff --git a/src/tests/sensors/analog/test-current-sensor.cpp b/src/tests/sensors/analog/test-current-sensor.cpp index b223d9041c2fb626326adb5c3e5bd29cf2063163..c1b73bc4f618de612c2da445a2aee903328b504c 100644 --- a/src/tests/sensors/analog/test-current-sensor.cpp +++ b/src/tests/sensors/analog/test-current-sensor.cpp @@ -80,9 +80,8 @@ int main() currentSensor.sample(); - CurrentSensorData currentData = currentSensor.getLastSample(); - printf("%llu %u %f %f \n", currentData.voltageTimestamp, - currentData.channelId, currentData.voltage, + CurrentData currentData = currentSensor.getLastSample(); + printf("%llu %f \n", currentData.currentTimestamp, currentData.current); miosix::Thread::sleep(100);