yarp-devices
SdoClient.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __SDO_CLIENT_HPP__
4 #define __SDO_CLIENT_HPP__
5 
6 #include <cstdint>
7 
8 #include <string>
9 #include <type_traits>
10 #include <utility>
11 
12 #include "ICanSenderDelegate.hpp"
13 #include "StateObserver.hpp"
14 
15 namespace roboticslab
16 {
17 
31 class SdoClient final
32 {
33 public:
35  SdoClient(std::uint8_t id, std::uint16_t cobRx, std::uint16_t cobTx, double timeout, ICanSenderDelegate * sender = nullptr)
36  : id(id), cobRx(cobRx), cobTx(cobTx), logId("ID" + std::to_string(id)), sender(sender), stateObserver(timeout)
37  {}
38 
40  std::uint16_t getCobIdRx() const
41  { return cobRx + id; }
42 
44  std::uint16_t getCobIdTx() const
45  { return cobTx + id; }
46 
49  { this->sender = sender; }
50 
52  bool notify(const std::uint8_t * raw)
53  { return stateObserver.notify(raw, 8); }
54 
56  bool ping();
57 
68  template<typename T>
69  bool upload(const std::string & name, T * data, std::uint16_t index, std::uint8_t subindex = 0x00)
70  {
71  static_assert(std::is_integral_v<T>, "Integral required.");
72  return uploadInternal(name, data, sizeof(T), index, subindex);
73  }
74 
86  template<typename T, typename Fn>
87  bool upload(const std::string & name, Fn && fn, std::uint16_t index, std::uint8_t subindex = 0x00)
88  {
89  T data;
90  return upload(name, &data, index, subindex) && (std::forward<Fn>(fn)(data), true);
91  }
92 
102  template<typename T>
103  bool download(const std::string & name, T data, std::uint16_t index, std::uint8_t subindex = 0x00)
104  {
105  static_assert(std::is_integral_v<T>, "Integral required.");
106  return downloadInternal(name, &data, sizeof(T), index, subindex);
107  }
108 
117  bool upload(const std::string & name, std::string & s, std::uint16_t index, std::uint8_t subindex = 0x00);
118 
129  template<typename Fn>
130  bool upload(const std::string & name, Fn && fn, std::uint16_t index, std::uint8_t subindex = 0x00)
131  {
132  std::string s;
133  return upload(name, s, index, subindex) && (std::forward<Fn>(fn)(s), true);
134  }
135 
144  bool download(const std::string & name, const std::string & s, std::uint16_t index, std::uint8_t subindex = 0x00);
145 
147  bool download(const std::string & name, const char * s, std::uint16_t index, std::uint8_t subindex = 0x00)
148  { return download(name, std::string(s), index, subindex); }
149 
150 private:
151  bool send(const std::uint8_t * msg);
152  bool uploadInternal(const std::string & name, void * data, std::uint32_t size, std::uint16_t index, std::uint8_t subindex);
153  bool downloadInternal(const std::string & name, const void * data, std::uint32_t size, std::uint16_t index, std::uint8_t subindex);
154  bool performTransfer(const std::string & name, const std::uint8_t * req, std::uint8_t * resp);
155 
156  std::uint8_t id;
157  std::uint16_t cobRx;
158  std::uint16_t cobTx;
159  std::string logId;
160 
161  ICanSenderDelegate * sender;
163 };
164 
165 } // namespace roboticslab
166 
167 #endif // __SDO_CLIENT_HPP__
Implementation-agnostic consumer for TX CAN transfers.
Definition: ICanSenderDelegate.hpp:22
Representation of SDO client protocol.
Definition: SdoClient.hpp:32
std::uint16_t getCobIdRx() const
Retrieve COB ID of SDO packages received by the drive.
Definition: SdoClient.hpp:40
std::uint16_t getCobIdTx() const
Retrieve COB ID of SDO packages sent by the drive.
Definition: SdoClient.hpp:44
bool download(const std::string &name, const char *s, std::uint16_t index, std::uint8_t subindex=0x00)
String literal overload, overrides templated variant.
Definition: SdoClient.hpp:147
void configureSender(ICanSenderDelegate *sender)
Configure CAN sender delegate handle.
Definition: SdoClient.hpp:48
bool upload(const std::string &name, Fn &&fn, std::uint16_t index, std::uint8_t subindex=0x00)
Request an SDO package from the drive with callback, only integral types.
Definition: SdoClient.hpp:87
bool download(const std::string &name, T data, std::uint16_t index, std::uint8_t subindex=0x00)
Send an SDO package to the drive, only integral types.
Definition: SdoClient.hpp:103
bool notify(const std::uint8_t *raw)
Notify observers on an SDO package sent by the drive.
Definition: SdoClient.hpp:52
bool ping()
Test whether the node is available or not.
Definition: SdoClient.cpp:103
bool upload(const std::string &name, T *data, std::uint16_t index, std::uint8_t subindex=0x00)
Request an SDO package from the drive, only integral types.
Definition: SdoClient.hpp:69
SdoClient(std::uint8_t id, std::uint16_t cobRx, std::uint16_t cobTx, double timeout, ICanSenderDelegate *sender=nullptr)
Constructor, registers CAN sender handle.
Definition: SdoClient.hpp:35
bool upload(const std::string &name, Fn &&fn, std::uint16_t index, std::uint8_t subindex=0x00)
Request an SDO package from the drive with callback, only string type.
Definition: SdoClient.hpp:130
Type state observer for unsigned char arrays.
Definition: StateObserver.hpp:142
bool notify(const std::uint8_t *raw, std::size_t len)
Wakes up a waiting thread.
Definition: StateObserver.hpp:151
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6