diff --git a/src/shared/drivers/canbus/CanProtocol/CanProtocol.cpp b/src/shared/drivers/canbus/CanProtocol/CanProtocol.cpp index 4c1501948161ff22120d8ca5190d7c5faede55ba..c2714aedf1c103ccf7343572468b60ad4a1fbc99 100644 --- a/src/shared/drivers/canbus/CanProtocol/CanProtocol.cpp +++ b/src/shared/drivers/canbus/CanProtocol/CanProtocol.cpp @@ -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); diff --git a/src/shared/drivers/canbus/CanProtocol/CanProtocol.h b/src/shared/drivers/canbus/CanProtocol/CanProtocol.h index fc79de55e28f26e80d359a7f1d875f807c6a02dc..3df511ecd13d5e11a4387707a5cd0ec1c779a70a 100644 --- a/src/shared/drivers/canbus/CanProtocol/CanProtocol.h +++ b/src/shared/drivers/canbus/CanProtocol/CanProtocol.h @@ -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. * diff --git a/src/shared/drivers/canbus/CanProtocol/CanProtocolData.h b/src/shared/drivers/canbus/CanProtocol/CanProtocolData.h index b73b40ca8535cf0f5bfacffc48d703ede106ed66..463687c764d772656a3b710f5956cb12eb7fbfd0 100644 --- a/src/shared/drivers/canbus/CanProtocol/CanProtocolData.h +++ b/src/shared/drivers/canbus/CanProtocol/CanProtocolData.h @@ -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. diff --git a/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h b/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h index 3d4cf55c42b733501f5621d956a8b571fed1fa9d..42c15d3f4713fb229af2ba783f24e48b3f37974a 100644 --- a/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h +++ b/src/shared/drivers/canbus/CanProtocol/CanProtocolTypes.h @@ -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