Skip to content
Snippets Groups Projects
Commit 091f9921 authored by Federico Mandelli's avatar Federico Mandelli
Browse files

[CanProtocol] Created encode/decode functions for PressureData and...

[CanProtocol] Created encode/decode functions for PressureData and TemperatureData, added enqueueSimplePacket() to send a packet with a manually crafted payload
parent d8d10834
No related branches found
No related tags found
1 merge request!163Can protocol
Pipeline #5933 passed
......@@ -162,6 +162,14 @@ void CanProtocol::sendMessage(const CanMessage& msg)
bool CanProtocol::enqueueEvent(uint8_t priority, uint8_t primaryType,
uint8_t source, uint8_t destination,
uint8_t secondaryType)
{
return enqueueSimplePacket(priority, primaryType, source, destination,
secondaryType, 0xFF);
}
bool CanProtocol::enqueueSimplePacket(uint8_t priority, uint8_t primaryType,
uint8_t source, uint8_t destination,
uint8_t secondaryType, uint64_t payload)
{
if (priority > 0xF || primaryType > 0x3F || source > 0xF ||
destination > 0xF || secondaryType > 0xF)
......@@ -171,7 +179,7 @@ bool CanProtocol::enqueueEvent(uint8_t priority, uint8_t primaryType,
// Length set to a minumum of 1 even if there is no payload
msg.length = 1;
msg.payload[0] = 0xFF;
msg.payload[0] = payload;
// clang-format off
msg.id = priority << static_cast<uint32_t>(CanProtocolShiftInformation::PRIORITY);
......
......@@ -100,6 +100,15 @@ public:
bool enqueueEvent(uint8_t priority, uint8_t primaryType, uint8_t source,
uint8_t destination, uint8_t secondaryType);
/**
* @brief Non-blocking send function for a simple packet with a payload of 1
* can packet, useful to send generic events/commands with a short payload
* without using an encoding/decoding function.
*/
bool enqueueSimplePacket(uint8_t priority, uint8_t primaryType,
uint8_t source, uint8_t destination,
uint8_t secondaryType, uint64_t payload);
/**
* @brief Non-blocking send function for a generic data type.
*
......
......@@ -30,29 +30,6 @@ namespace Boardcore
namespace Canbus
{
/**
* The CanProtocol allows to transmit arbitrarily sized messages over the CanBus
* overcoming the 8 byte limitation of each single packet.
*
* Our CanProtocol uses the extended can packet, the 29 bits id is divided such
* as:
* - Priority 4 bit \
* - Primary type 6 bit |
* - Source 4 bit | 22 bits - Message informations
* - Destination 4 bit |
* - Secondary type 4 bit /
* - First packet flag 1 bit \ 7 bits - Sequential informations
* - Remaining packets 6 bit /
* shiftNameOfField the number of shift needed to reach that field
*
* The id is split into 2 parts:
* - Message information: Common to every packet of a given message
* - Sequential information: Used to distinguish between packets
*
* The sender splits into multiple packets a message that is then recomposed on
* the receiver end. The message informations are encoded into the packets id,
* therefore they have an effect on packets priorities.
*/
/**
* The CanProtocol allows to transmit arbitrarily sized messages over the CanBus
* overcoming the 8 byte limitation of each single packet.
......
......@@ -22,6 +22,7 @@
#pragma once
#include <sensors/SensorData.h>
#include <sensors/analog/Pitot/PitotData.h>
#include <cstring>
......@@ -35,15 +36,42 @@ inline Canbus::CanMessage toCanMessage(const PitotData& data)
{
Canbus::CanMessage message;
uint32_t deltaP, airspeed;
memcpy(&deltaP, &(data.deltaP), sizeof(deltaP));
uint32_t airspeed;
memcpy(&airspeed, &(data.airspeed), sizeof(airspeed));
message.id = -1;
message.length = 2;
message.length = 1;
message.payload[0] = (data.timestamp & ~0x3) << 30;
message.payload[0] |= deltaP;
message.payload[1] = airspeed;
message.payload[0] |= airspeed;
return message;
}
inline Canbus::CanMessage toCanMessage(const PressureData& data)
{
Canbus::CanMessage message;
uint32_t pressure;
memcpy(&pressure, &(data.pressure), sizeof(pressure));
message.id = -1;
message.length = 1;
message.payload[0] = (data.pressureTimestamp & ~0x3) << 30;
message.payload[0] |= pressure;
return message;
}
inline Canbus::CanMessage toCanMessage(const TemperatureData& data)
{
Canbus::CanMessage message;
uint32_t temperature;
memcpy(&temperature, &(data.temperature), sizeof(temperature));
message.id = -1;
message.length = 1;
message.payload[0] = (data.temperatureTimestamp & ~0x3) << 30;
message.payload[0] |= temperature;
return message;
}
......@@ -52,14 +80,39 @@ inline PitotData pitotDataFromCanMessage(const Canbus::CanMessage& msg)
{
PitotData data;
uint32_t deltaP = msg.payload[0];
uint32_t airspeed = msg.payload[1];
memcpy(&(data.deltaP), &deltaP, sizeof(data.deltaP));
uint32_t airspeed = msg.payload[0];
memcpy(&(data.airspeed), &airspeed, sizeof(data.airspeed));
data.deltaP = 0.0; // put to 0 to avoid undefined behaviour
data.timestamp = (msg.payload[0] >> 30) & ~0x3;
return data;
}
inline PressureData pressureDataFromCanMessage(const Canbus::CanMessage& msg)
{
PressureData data;
uint32_t pressure = msg.payload[0];
memcpy(&(data.pressure), &pressure, sizeof(data.pressure));
data.pressureTimestamp = (msg.payload[0] >> 30) & ~0x3;
return data;
}
inline TemperatureData temperatureDataFromCanMessage(
const Canbus::CanMessage& msg)
{
TemperatureData data;
uint32_t temperature = msg.payload[0];
memcpy(&(data.temperature), &temperature, sizeof(data.temperature));
data.temperatureTimestamp = (msg.payload[0] >> 30) & ~0x3;
return data;
}
} // namespace Boardcore
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment