yarp-devices
SingleBusBroker.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __SINGLE_BUS_BROKER_HPP__
4 #define __SINGLE_BUS_BROKER_HPP__
5 
6 #include <mutex>
7 #include <string>
8 
9 #include <yarp/os/Bottle.h>
10 #include <yarp/os/Port.h>
11 #include <yarp/os/PortReaderBuffer.h>
12 #include <yarp/os/PortWriterBuffer.h>
13 #include <yarp/os/RpcServer.h>
14 #include <yarp/os/Searchable.h>
15 #include <yarp/os/TypedReaderCallback.h>
16 
17 #include <yarp/dev/CanBusInterface.h>
18 #include <yarp/dev/PolyDriver.h>
19 
20 #include "CanRxTxThreads.hpp"
21 #include "SdoReplier.hpp"
22 #include "BusLoadMonitor.hpp"
23 
24 namespace roboticslab
25 {
26 
39 class SingleBusBroker final : public yarp::os::TypedReaderCallback<yarp::os::Bottle>
40 {
41 public:
43  SingleBusBroker(const std::string & name);
44 
46  ~SingleBusBroker() override;
47 
49  bool configure(const yarp::os::Searchable & config);
50 
52  bool registerDevice(yarp::dev::PolyDriver * driver);
53 
55  bool clearFilters();
56 
58  bool startThreads();
59 
61  bool stopThreads();
62 
65  { return readerThread; }
66 
69  { return writerThread; }
70 
72  std::string getName() const
73  { return name; }
74 
76  void onRead(yarp::os::Bottle & b) override;
77 
78 private:
80  bool createPorts(const std::string & prefix);
81 
82  std::string name;
83 
84  CanReaderThread * readerThread;
85  CanWriterThread * writerThread;
86 
87  yarp::dev::ICanBus * iCanBus;
88  yarp::dev::ICanBufferFactory * iCanBufferFactory;
89 
90  yarp::os::Port dumpPort;
91  yarp::os::PortWriterBuffer<yarp::os::Bottle> dumpWriter;
92  std::mutex dumpMutex;
93 
94  yarp::os::Port sendPort;
95  yarp::os::PortReaderBuffer<yarp::os::Bottle> commandReader;
96 
97  yarp::os::RpcServer sdoPort;
98  SdoReplier sdoReplier;
99 
100  yarp::os::Port busLoadPort;
101  BusLoadMonitor * busLoadMonitor;
102 };
103 
104 } // namespace roboticslab
105 
106 #endif // __SINGLE_BUS_BROKER_HPP__
Periodically sends CAN bus load stats through a YARP port.
Definition: BusLoadMonitor.hpp:40
A thread that deals with CAN reads.
Definition: CanRxTxThreads.hpp:110
A thread that attends CAN writes.
Definition: CanRxTxThreads.hpp:142
RPC replier for remote SDO transfers.
Definition: SdoReplier.hpp:25
Message broker of a CAN bus with YARP port interfaces.
Definition: SingleBusBroker.hpp:40
void onRead(yarp::os::Bottle &b) override
Callback on incoming remote CAN commands.
Definition: SingleBusBroker.cpp:276
bool configure(const yarp::os::Searchable &config)
Configure this CAN bus.
Definition: SingleBusBroker.cpp:47
bool clearFilters()
Clear CAN acceptance filters.
Definition: SingleBusBroker.cpp:198
bool stopThreads()
Stop CAN read/write threads.
Definition: SingleBusBroker.cpp:242
CanReaderThread * getReader() const
Get handle of the CAN RX thread.
Definition: SingleBusBroker.hpp:64
std::string getName() const
Retrieve string identifier for this CAN bus.
Definition: SingleBusBroker.hpp:72
bool startThreads()
Start CAN read/write threads.
Definition: SingleBusBroker.cpp:217
bool registerDevice(yarp::dev::PolyDriver *driver)
Register CAN handles associated to the input device driver.
Definition: SingleBusBroker.cpp:87
bool createPorts(const std::string &prefix)
Open remote CAN interface ports.
Definition: SingleBusBroker.cpp:129
~SingleBusBroker() override
Destructor.
Definition: SingleBusBroker.cpp:27
SingleBusBroker(const std::string &name)
Constructor, passes string identifier of the CAN bus.
Definition: SingleBusBroker.cpp:16
CanWriterThread * getWriter() const
Get handle.
Definition: SingleBusBroker.hpp:68
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6