yarp-devices
Loading...
Searching...
No Matches
CanBusPeak.hpp
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3#ifndef __CAN_BUS_PEAK_HPP__
4#define __CAN_BUS_PEAK_HPP__
5
6#include <cstdint>
7
8#include <mutex>
9#include <set>
10
11#include <yarp/dev/DeviceDriver.h>
12#include <yarp/dev/CanBusInterface.h>
13
14#include <libpcanfd.h>
15
16#include "PeakCanMessage.hpp"
17#include "CanBusPeak_ParamsParser.h"
18
29class ImplementPeakCanBufferFactory : public yarp::dev::ImplementCanBufferFactory<roboticslab::PeakCanMessage, struct pcanfd_msg>
30{
31public:
32 yarp::dev::CanBuffer createBuffer(int elem) override
33 {
34 yarp::dev::CanBuffer ret;
35 struct pcanfd_msg * storage = new pcanfd_msg[elem];
36 yarp::dev::CanMessage ** messages = new yarp::dev::CanMessage *[elem];
37 auto * tmp = new roboticslab::PeakCanMessage[elem];
38
39 std::memset(storage, 0, sizeof(struct pcanfd_msg) * elem);
40
41 for (int k = 0; k < elem; k++)
42 {
43 messages[k] = &tmp[k];
44 messages[k]->setBuffer(reinterpret_cast<unsigned char *>(&storage[k]));
45
46 // Changes wrt default implementation:
47 storage[k].type = PCANFD_TYPE_CAN20_MSG;
48 storage[k].flags = PCANFD_MSG_STD;
49 }
50
51 ret.resize(messages, elem);
52 return ret;
53 }
54};
55
60class CanBusPeak : public yarp::dev::DeviceDriver,
61 public yarp::dev::ICanBus,
62 public yarp::dev::ICanBusErrors,
65{
66public:
67
68 // --------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp ---------
69
70 bool open(yarp::os::Searchable& config) override;
71 bool close() override;
72
73 // --------- ICanBus declarations. Implementation in ICanBusImpl.cpp ---------
74
75 bool canSetBaudRate(unsigned int rate) override;
76 bool canGetBaudRate(unsigned int * rate) override;
77 bool canIdAdd(unsigned int id) override;
78 bool canIdDelete(unsigned int id) override;
79 bool canRead(yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * read, bool wait = false) override;
80 bool canWrite(const yarp::dev::CanBuffer & msgs, unsigned int size, unsigned int * sent, bool wait = false) override;
81
82 // --------- ICanBusErrors declarations. Implementation in ICanBusErrorsImpl.cpp ---------
83
84 bool canGetErrors(yarp::dev::CanErrors & err) override;
85
86private:
87
88 enum io_operation { READ, WRITE };
89
90 bool waitUntilTimeout(io_operation op, bool * bufferReady);
91
92 std::uint64_t computeAcceptanceCodeAndMask();
93
94 int fileDescriptor {0};
95
96 mutable std::mutex canBusReady;
97
98 std::set<unsigned int> activeFilters;
99};
100
101#endif // __CAN_BUS_PEAK_HPP__
Definition CanBusPeak_ParamsParser.h:49
Specifies the PeakCan behaviour and specifications.
Definition CanBusPeak.hpp:65
Custom buffer of PeakCanMessage instances.
Definition CanBusPeak.hpp:30
YARP wrapper for PeakCAN messages.
Definition PeakCanMessage.hpp:21