yarp-devices
Loading...
Searching...
No Matches
CanRxTxThreads.hpp
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3#ifndef __CAN_RX_TH_THREADS_HPP__
4#define __CAN_RX_TH_THREADS_HPP__
5
6#include <mutex>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11#include <yarp/os/Bottle.h>
12#include <yarp/os/BufferedPort.h>
13#include <yarp/os/Stamp.h>
14#include <yarp/os/Thread.h>
15
16#include <yarp/dev/CanBusInterface.h>
17
18#include "ICanBusSharer.hpp"
19
20namespace roboticslab
21{
22
30class CanReaderWriterThread : public yarp::os::Thread
31{
32public:
34 CanReaderWriterThread(const std::string & type, const std::string & id, double delay, unsigned int bufferSize)
35 : iCanBus(nullptr), iCanBufferFactory(nullptr),
36 dumpPort(nullptr), dumpMutex(nullptr), busLoadMonitor(nullptr),
37 bufferSize(bufferSize), delay(delay), type(type), id(id)
38 { }
39
41 ~CanReaderWriterThread() override = default;
42
44 bool threadInit() override
45 { canBuffer = iCanBufferFactory->createBuffer(bufferSize); return true; }
46
48 void threadRelease() override
49 { iCanBufferFactory->destroyBuffer(canBuffer); }
50
52 void beforeStart() override;
53
55 void afterStart(bool success) override;
56
58 void onStop() override;
59
61 void setCanHandles(yarp::dev::ICanBus * iCanBus, yarp::dev::ICanBufferFactory * iCanBufferFactory)
62 {
63 this->iCanBus = iCanBus;
64 this->iCanBufferFactory = iCanBufferFactory;
65 }
66
68 void attachDumpWriter(yarp::os::BufferedPort<yarp::os::Bottle> * dumpPort, std::mutex * dumpMutex)
69 {
70 this->dumpPort = dumpPort;
71 this->dumpMutex = dumpMutex;
72 }
73
76 { this->busLoadMonitor = busLoadMonitor; }
77
78protected:
80 static void dumpMessage(const can_message & msg, yarp::os::Bottle & b);
81
82 yarp::dev::ICanBus * iCanBus {nullptr};
83 yarp::dev::ICanBufferFactory * iCanBufferFactory {nullptr};
84 yarp::dev::CanBuffer canBuffer;
85
86 yarp::os::BufferedPort<yarp::os::Bottle> * dumpPort {nullptr};
87 std::mutex * dumpMutex {nullptr};
88
89 yarp::os::Stamp lastStamp;
90
91 ICanMessageNotifier * busLoadMonitor {nullptr};
92
93 unsigned int bufferSize {0};
94 double delay {0.0};
95
96private:
97 std::string type;
98 std::string id;
99};
100
108{
109public:
111 CanReaderThread(const std::string & id, double delay, unsigned int bufferSize);
112
115
117 const std::vector<ICanBusSharer *> & getHandles() const
118 { return handles; }
119
121 void attachCanNotifier(ICanMessageNotifier * canMessageNotifier)
122 { this->canMessageNotifier = canMessageNotifier; }
123
124 void run() override;
125
126private:
127 std::vector<ICanBusSharer *> handles;
128 std::unordered_map<unsigned int, ICanBusSharer *> canIdToHandle;
129 ICanMessageNotifier * canMessageNotifier {nullptr};
130};
131
140{
141public:
143 CanWriterThread(const std::string & id, double delay, unsigned int bufferSize);
144
146 ~CanWriterThread() override;
147
150 { return sender; }
151
153 void flush();
154
155 void run() override;
156
157private:
159 void handlePartialWrite(unsigned int sent);
160
161 unsigned int preparedMessages {0};
162 ICanSenderDelegate * sender {nullptr};
163 mutable std::mutex bufferMutex;
164};
165
166} // namespace roboticslab
167
168#endif // __CAN_RX_TH_THREADS_HPP__
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