Skip to content
Snippets Groups Projects
Commit e7db2f43 authored by Alberto Nidasio's avatar Alberto Nidasio Committed by Matteo Pignataro
Browse files

[CurrentSensor][CanProtocol] Added CurrentData basic type and encoding functions in CanProtocol

parent df95518d
No related branches found
No related tags found
No related merge requests found
...@@ -76,6 +76,21 @@ inline Canbus::CanMessage toCanMessage(const TemperatureData& data) ...@@ -76,6 +76,21 @@ inline Canbus::CanMessage toCanMessage(const TemperatureData& data)
return message; return message;
} }
inline Canbus::CanMessage toCanMessage(const CurrentData& data)
{
Canbus::CanMessage message;
uint32_t current;
memcpy(&current, &(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) inline PitotData pitotDataFromCanMessage(const Canbus::CanMessage& msg)
{ {
PitotData data; PitotData data;
...@@ -115,4 +130,16 @@ inline TemperatureData temperatureDataFromCanMessage( ...@@ -115,4 +130,16 @@ inline TemperatureData temperatureDataFromCanMessage(
return data; return data;
} }
inline CurrentData currentDataFromCanMessage(const Canbus::CanMessage& msg)
{
CurrentData data;
uint32_t current = msg.payload[0];
memcpy(&(data.current), &current, sizeof(data.current));
data.currentTimestamp = (msg.payload[0] >> 30) & ~0x3;
return data;
}
} // namespace Boardcore } // namespace Boardcore
...@@ -54,7 +54,6 @@ ...@@ -54,7 +54,6 @@
#include <sensors/VN100/VN100Data.h> #include <sensors/VN100/VN100Data.h>
#include <sensors/analog/AnalogLoadCellData.h> #include <sensors/analog/AnalogLoadCellData.h>
#include <sensors/analog/BatteryVoltageSensorData.h> #include <sensors/analog/BatteryVoltageSensorData.h>
#include <sensors/analog/CurrentSensorData.h>
#include <sensors/analog/pressure/honeywell/HSCMAND015PAData.h> #include <sensors/analog/pressure/honeywell/HSCMAND015PAData.h>
#include <sensors/analog/pressure/honeywell/HSCMRNN030PAData.h> #include <sensors/analog/pressure/honeywell/HSCMRNN030PAData.h>
#include <sensors/analog/pressure/honeywell/HSCMRNN160KAData.h> #include <sensors/analog/pressure/honeywell/HSCMRNN160KAData.h>
...@@ -114,7 +113,7 @@ void registerTypes(Deserializer& ds) ...@@ -114,7 +113,7 @@ void registerTypes(Deserializer& ds)
ds.registerType<VN100Data>(); ds.registerType<VN100Data>();
ds.registerType<AnalogLoadCellData>(); ds.registerType<AnalogLoadCellData>();
ds.registerType<BatteryVoltageSensorData>(); ds.registerType<BatteryVoltageSensorData>();
ds.registerType<CurrentSensorData>(); ds.registerType<CurrentData>();
ds.registerType<HSCMAND015PAData>(); ds.registerType<HSCMAND015PAData>();
ds.registerType<HSCMRNN030PAData>(); ds.registerType<HSCMRNN030PAData>();
ds.registerType<HSCMRNN160KAData>(); ds.registerType<HSCMRNN160KAData>();
......
...@@ -272,4 +272,20 @@ struct ADCData ...@@ -272,4 +272,20 @@ struct ADCData
float voltage = 0; 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 } // namespace Boardcore
...@@ -26,8 +26,6 @@ ...@@ -26,8 +26,6 @@
#include <functional> #include <functional>
#include "CurrentSensorData.h"
namespace Boardcore namespace Boardcore
{ {
...@@ -36,7 +34,7 @@ namespace Boardcore ...@@ -36,7 +34,7 @@ namespace Boardcore
* *
* It needs a transfer function to convert the read voltage into current. * It needs a transfer function to convert the read voltage into current.
*/ */
class CurrentSensor : public Sensor<CurrentSensorData> class CurrentSensor : public Sensor<CurrentData>
{ {
public: public:
static constexpr int MOVING_AVERAGE_N = 20; static constexpr int MOVING_AVERAGE_N = 20;
...@@ -53,7 +51,7 @@ public: ...@@ -53,7 +51,7 @@ public:
bool selfTest() override { return true; }; bool selfTest() override { return true; };
///< Converts the voltage value to pressure ///< Converts the voltage value to pressure
CurrentSensorData sampleImpl() override CurrentData sampleImpl() override
{ {
ADCData adc_data = getVoltage(); ADCData adc_data = getVoltage();
...@@ -62,10 +60,8 @@ public: ...@@ -62,10 +60,8 @@ public:
lastSample.current = voltageToCurrent(adc_data.voltage); lastSample.current = voltageToCurrent(adc_data.voltage);
} }
CurrentSensorData current_data; CurrentData current_data;
current_data.voltageTimestamp = adc_data.voltageTimestamp; current_data.currentTimestamp = adc_data.voltageTimestamp;
current_data.channelId = adc_data.channelId;
current_data.voltage = adc_data.voltage;
// Moving average // Moving average
current_data.current = lastSample.current * MOVING_AVERAGE_COMP_COEFF; current_data.current = lastSample.current * MOVING_AVERAGE_COMP_COEFF;
......
/* 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
...@@ -80,9 +80,8 @@ int main() ...@@ -80,9 +80,8 @@ int main()
currentSensor.sample(); currentSensor.sample();
CurrentSensorData currentData = currentSensor.getLastSample(); CurrentData currentData = currentSensor.getLastSample();
printf("%llu %u %f %f \n", currentData.voltageTimestamp, printf("%llu %f \n", currentData.currentTimestamp,
currentData.channelId, currentData.voltage,
currentData.current); currentData.current);
miosix::Thread::sleep(100); miosix::Thread::sleep(100);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment