yarp-devices
CuiAbsolute.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __CUI_ABSOLUTE_HPP__
4 #define __CUI_ABSOLUTE_HPP__
5 
6 #include <cstdint>
7 
8 #include <mutex>
9 #include <string>
10 
11 #include <yarp/conf/numeric.h>
12 
13 #include <yarp/dev/DeviceDriver.h>
14 #include <yarp/dev/IEncodersTimed.h>
15 
16 #include "ICanBusSharer.hpp"
17 #include "StateObserver.hpp"
18 
19 #define CHECK_JOINT(j) do { int ax; if (getAxes(&ax), (j) != ax - 1) return false; } while (0)
20 
21 namespace roboticslab
22 {
23 
35 class CuiAbsolute : public yarp::dev::DeviceDriver,
36  public yarp::dev::IEncodersTimedRaw,
37  public ICanBusSharer
38 {
39 public:
40 
41  CuiAbsolute()
42  : canId(0), timeout(0.0), maxRetries(0), retry(0), reverse(false),
43  cuiMode(CuiMode::OFF), pushDelay(0),
44  encoder(), encoderTimestamp(0.0),
45  sender(nullptr), pushStateObserver(nullptr), pollStateObserver(nullptr)
46  { }
47 
48  ~CuiAbsolute() override
49  { close(); }
50 
51  // --------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp ---------
52 
53  bool open(yarp::os::Searchable & config) override;
54  bool close() override;
55 
56  // --------- ICanBusSharer declarations. Implementation in ICanBusSharerImpl.cpp ---------
57 
58  unsigned int getId() override;
59  bool notifyMessage(const can_message & message) override;
60  bool initialize() override;
61  bool finalize() override;
62  bool registerSender(ICanSenderDelegate * sender) override;
63  bool synchronize(double timestamp) override;
64 
65  // ---------- IEncodersRaw declarations. Implementation in IEncodersRawImpl.cpp ----------
66 
67  bool getAxes(int * ax) override;
68  bool resetEncoderRaw(int j) override;
69  bool resetEncodersRaw() override;
70  bool setEncoderRaw(int j, double val) override;
71  bool setEncodersRaw(const double * vals) override;
72  bool getEncoderRaw(int j, double * v) override;
73  bool getEncodersRaw(double * encs) override;
74  bool getEncoderSpeedRaw(int j, double * sp) override;
75  bool getEncoderSpeedsRaw(double * spds) override;
76  bool getEncoderAccelerationRaw(int j, double * spds) override;
77  bool getEncoderAccelerationsRaw(double * accs) override;
78 
79  // ---------- IEncodersTimedRaw declarations. Implementation in IEncodersRawImpl.cpp ----------
80 
81  bool getEncodersTimedRaw(double * encs, double * time) override;
82  bool getEncoderTimedRaw(int j, double * encs, double * time) override;
83 
84 private:
85 
86  using encoder_t = yarp::conf::float32_t;
87 
88  enum class CuiMode { PUSH, PULL, OFF };
89  enum class CuiCommand : std::uint8_t { PUSH_START = 1, PUSH_STOP = 2, POLL = 3 };
90 
91  bool performRequest(const std::string & name, unsigned int len, const std::uint8_t * msgData, encoder_t * resp = nullptr);
92  bool startPushMode();
93  bool stopPushMode();
94  bool pollEncoderRead(encoder_t * enc);
95  void normalize(encoder_t * v);
96 
97  unsigned int canId;
98  double timeout;
99  int maxRetries;
100  int retry;
101  bool reverse;
102 
103  CuiMode cuiMode;
104  std::uint8_t pushDelay;
105 
106  encoder_t encoder;
107  double encoderTimestamp;
108 
109  ICanSenderDelegate * sender;
110  StateObserver * pushStateObserver;
111  TypedStateObserver<encoder_t> * pollStateObserver;
112 
113  mutable std::mutex mutex;
114 };
115 
116 } // namespace roboticslab
117 
118 #endif // __CUI_ABSOLUTE_HPP__
Implementation for the Cui Absolute Encoder custom UC3M circuit as a single CAN bus joint (control bo...
Definition: CuiAbsolute.hpp:38
bool finalize() override
Finalize CAN node communications.
Definition: ICanBusSharerImpl.cpp:36
bool initialize() override
Perform CAN node initialization.
Definition: ICanBusSharerImpl.cpp:24
bool synchronize(double timestamp) override
Perform synchronized action on CAN master's request.
Definition: ICanBusSharerImpl.cpp:92
bool notifyMessage(const can_message &message) override
Notify observers that a new CAN message has arrived.
Definition: ICanBusSharerImpl.cpp:49
unsigned int getId() override
Retrieve CAN node ID.
Definition: ICanBusSharerImpl.cpp:17
bool registerSender(ICanSenderDelegate *sender) override
Pass a handle to a CAN sender delegate instance.
Definition: ICanBusSharerImpl.cpp:84
Abstract base for a CAN bus sharer.
Definition: ICanBusSharer.hpp:25
Implementation-agnostic consumer for TX CAN transfers.
Definition: ICanSenderDelegate.hpp:22
Data-free state observer.
Definition: StateObserver.hpp:75
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6
Proxy CAN message structure.
Definition: CanMessage.hpp:20