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

[CanProtocol] Added BusLoadEstimation to CanProtocol

parent a1380238
No related branches found
No related tags found
No related merge requests found
...@@ -40,7 +40,7 @@ namespace Canbus ...@@ -40,7 +40,7 @@ namespace Canbus
class BusLoadEstimation class BusLoadEstimation
{ {
static constexpr uint16_t BUFFER_LEN = 100; static constexpr uint16_t BUFFER_LEN = 20;
public: public:
struct BusLoadInfo struct BusLoadInfo
...@@ -60,6 +60,7 @@ public: ...@@ -60,6 +60,7 @@ public:
BusLoadInfo getLoadInfo() BusLoadInfo getLoadInfo()
{ {
// implements a simple "moving avg"
Lock<FastMutex> l(mutex); Lock<FastMutex> l(mutex);
if (c.count() < 2) if (c.count() < 2)
{ {
......
...@@ -31,8 +31,16 @@ namespace Canbus ...@@ -31,8 +31,16 @@ namespace Canbus
{ {
CanProtocol::CanProtocol(CanbusDriver* can, MsgHandler onReceive) CanProtocol::CanProtocol(CanbusDriver* can, MsgHandler onReceive)
{
// We assume the bus to be configured at its max velocity
CanProtocol(can, onReceive, 500 * 1000);
}
CanProtocol::CanProtocol(CanbusDriver* can, MsgHandler onReceive,
uint32_t baudRate)
: can(can), onReceive(onReceive) : can(can), onReceive(onReceive)
{ {
loadEstimator = new BusLoadEstimation(baudRate);
} }
bool CanProtocol::start() bool CanProtocol::start()
...@@ -138,6 +146,8 @@ void CanProtocol::sendMessage(const CanMessage& msg) ...@@ -138,6 +146,8 @@ void CanProtocol::sendMessage(const CanMessage& msg)
// Send the first packet // Send the first packet
can->send(packet); can->send(packet);
// Updates the loadEstimator
loadEstimator->addPacket(packet);
leftToSend--; leftToSend--;
// Prepare the remaining packets // Prepare the remaining packets
...@@ -155,6 +165,8 @@ void CanProtocol::sendMessage(const CanMessage& msg) ...@@ -155,6 +165,8 @@ void CanProtocol::sendMessage(const CanMessage& msg)
packet.data[k] = msg.payload[i] >> (8 * k); packet.data[k] = msg.payload[i] >> (8 * k);
can->send(packet); can->send(packet);
// Updates the loadEstimator
loadEstimator->addPacket(packet);
leftToSend--; leftToSend--;
} }
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#pragma once #pragma once
#include <diagnostic/PrintLogger.h> #include <diagnostic/PrintLogger.h>
#include <drivers/canbus/CanDriver/BusLoadEstimation.h>
#include <drivers/canbus/CanDriver/CanDriver.h> #include <drivers/canbus/CanDriver/CanDriver.h>
#include <utils/Debug.h> #include <utils/Debug.h>
#include <utils/collections/SyncCircularBuffer.h> #include <utils/collections/SyncCircularBuffer.h>
...@@ -54,8 +55,24 @@ public: ...@@ -54,8 +55,24 @@ public:
* @brief Construct a new CanProtocol object. * @brief Construct a new CanProtocol object.
* *
* @param can Pointer to a CanbusDriver object. * @param can Pointer to a CanbusDriver object.
*
* @param onReceive function to be called when a new message is received.
*/
CanProtocol::CanProtocol(CanbusDriver* can, MsgHandler onReceive);
/**
* @brief Construct a new CanProtocol object.
*
* @param can Pointer to a CanbusDriver object.
*
* @param onReceive function to be called when a new message is received.
*
* @param baudRate used to calculate bus usage.
*/ */
CanProtocol(CanbusDriver* can, MsgHandler onReceive);
CanProtocol::CanProtocol(CanbusDriver* can, MsgHandler onReceive,
uint32_t baudRate);
/** /**
* @brief Start the receiving and sending threads. * @brief Start the receiving and sending threads.
...@@ -183,6 +200,8 @@ private: ...@@ -183,6 +200,8 @@ private:
SyncCircularBuffer<CanMessage, 10> outQueue; SyncCircularBuffer<CanMessage, 10> outQueue;
BusLoadEstimation* loadEstimator;
PrintLogger logger = Logging::getLogger("canprotocol"); PrintLogger logger = Logging::getLogger("canprotocol");
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment