3#ifndef __CAN_RX_TH_THREADS_HPP__
4#define __CAN_RX_TH_THREADS_HPP__
8#include <unordered_map>
11#include <yarp/os/Bottle.h>
12#include <yarp/os/BufferedPort.h>
13#include <yarp/os/Stamp.h>
14#include <yarp/os/Thread.h>
16#include <yarp/dev/CanBusInterface.h>
18#include "ICanBusSharer.hpp"
35 : iCanBus(nullptr), iCanBufferFactory(nullptr),
36 dumpPort(nullptr), dumpMutex(nullptr), busLoadMonitor(nullptr),
37 bufferSize(bufferSize), delay(delay), type(type), id(id)
45 { canBuffer = iCanBufferFactory->createBuffer(bufferSize);
return true; }
49 { iCanBufferFactory->destroyBuffer(canBuffer); }
61 void setCanHandles(yarp::dev::ICanBus * iCanBus, yarp::dev::ICanBufferFactory * iCanBufferFactory)
63 this->iCanBus = iCanBus;
64 this->iCanBufferFactory = iCanBufferFactory;
68 void attachDumpWriter(yarp::os::BufferedPort<yarp::os::Bottle> * dumpPort, std::mutex * dumpMutex)
70 this->dumpPort = dumpPort;
71 this->dumpMutex = dumpMutex;
76 { this->busLoadMonitor = busLoadMonitor; }
82 yarp::dev::ICanBus * iCanBus {
nullptr};
83 yarp::dev::ICanBufferFactory * iCanBufferFactory {
nullptr};
84 yarp::dev::CanBuffer canBuffer;
86 yarp::os::BufferedPort<yarp::os::Bottle> * dumpPort {
nullptr};
87 std::mutex * dumpMutex {
nullptr};
89 yarp::os::Stamp lastStamp;
91 ICanMessageNotifier * busLoadMonitor {
nullptr};
93 unsigned int bufferSize {0};
111 CanReaderThread(
const std::string &
id,
double delay,
unsigned int bufferSize);
122 { this->canMessageNotifier = canMessageNotifier; }
127 std::vector<ICanBusSharer *> handles;
128 std::unordered_map<unsigned int, ICanBusSharer *> canIdToHandle;
143 CanWriterThread(
const std::string &
id,
double delay,
unsigned int bufferSize);
161 unsigned int preparedMessages {0};
162 ICanSenderDelegate * sender {
nullptr};
163 mutable std::mutex bufferMutex;
void attachCanNotifier(ICanMessageNotifier *canMessageNotifier)
Attach custom CAN message responder handle.
Definition CanRxTxThreads.hpp:121
CanReaderThread(const std::string &id, double delay, unsigned int bufferSize)
Constructor.
Definition CanRxTxThreads.cpp:51
const std::vector< ICanBusSharer * > & getHandles() const
Retrieve collection of handles to wrapped CAN devices.
Definition CanRxTxThreads.hpp:117
void registerHandle(ICanBusSharer *p)
Map CAN node ids with handles.
Definition CanRxTxThreads.cpp:57
void setCanHandles(yarp::dev::ICanBus *iCanBus, yarp::dev::ICanBufferFactory *iCanBufferFactory)
Configure CAN interface handles.
Definition CanRxTxThreads.hpp:61
bool threadInit() override
Invoked by the thread right before it is started.
Definition CanRxTxThreads.hpp:44
void attachBusLoadMonitor(ICanMessageNotifier *busLoadMonitor)
Attach CAN bus load monitor.
Definition CanRxTxThreads.hpp:75
void beforeStart() override
Invoked by the caller right before the thread is started.
Definition CanRxTxThreads.cpp:18
void threadRelease() override
Invoked by the thread right after it is started.
Definition CanRxTxThreads.hpp:48
~CanReaderWriterThread() override=default
Virtual destructor.
void attachDumpWriter(yarp::os::BufferedPort< yarp::os::Bottle > *dumpPort, std::mutex *dumpMutex)
Attach YARP port writer for CAN message dumping.
Definition CanRxTxThreads.hpp:68
CanReaderWriterThread(const std::string &type, const std::string &id, double delay, unsigned int bufferSize)
Constructor.
Definition CanRxTxThreads.hpp:34
static void dumpMessage(const can_message &msg, yarp::os::Bottle &b)
Dump a CAN message to a YARP bottle.
Definition CanRxTxThreads.cpp:39
void onStop() override
Callback on thread stop.
Definition CanRxTxThreads.cpp:32
void afterStart(bool success) override
Invoked by the caller right before the thread is joined.
Definition CanRxTxThreads.cpp:25
void handlePartialWrite(unsigned int sent)
In case a write did not succeed, rearrange the CAN message buffer.
Definition CanRxTxThreads.cpp:219
~CanWriterThread() override
Destructor.
Definition CanRxTxThreads.cpp:139
void flush()
Send awaiting messages and clear the queue.
Definition CanRxTxThreads.cpp:146
CanWriterThread(const std::string &id, double delay, unsigned int bufferSize)
Constructor.
Definition CanRxTxThreads.cpp:132
ICanSenderDelegate * getDelegate()
Retrieve a handle to the CAN sender delegate.
Definition CanRxTxThreads.hpp:149
Abstract base for a CAN bus sharer.
Definition ICanBusSharer.hpp:25
Implementation-agnostic consumer for RX CAN transfers.
Definition ICanMessageNotifier.hpp:22
Implementation-agnostic consumer for TX CAN transfers.
Definition ICanSenderDelegate.hpp:22
The main, catch-all namespace for Robotics Lab UC3M.
Definition groups.dox:6
Proxy CAN message structure.
Definition CanMessage.hpp:20