3 #ifndef __PDO_PROTOCOL_HPP__
4 #define __PDO_PROTOCOL_HPP__
10 #include <type_traits>
13 #include "ICanSenderDelegate.hpp"
14 #include "SdoClient.hpp"
47 constexpr
operator std::uint8_t()
const
55 template<std::u
int8_t n>
59 static_assert(n >= 0x01 && n <= 0xF0,
"Illegal argument.");
118 static_assert(std::is_integral_v<T>,
"Integral required.");
119 static_assert(
sizeof(T) <=
sizeof(std::uint32_t),
"Size exceeds 4 bytes.");
120 addMappingInternal((index << 16) + (subindex << 8) +
sizeof(T) * 8);
125 void addMappingInternal(std::uint32_t value);
142 : id(id), cob(cob), n(n), sdo(sdo)
150 {
return cob + id; };
162 template<
typename... Tn>
163 static constexpr std::size_t size()
164 {
return (
sizeof(Tn) + ... + 0); }
187 { this->sender = sender; }
196 template<
typename... Ts>
199 static_assert(
sizeof...(Ts) > 0 && size<Ts...>() <= 8,
"Illegal cumulative size.");
200 std::uint8_t raw[size<Ts...>()];
unsigned int count = 0;
201 (pack(&data, raw, &count), ...);
202 return writeInternal(raw, count);
207 {
return PdoType::RPDO; }
211 void pack(
const T * data, std::uint8_t * buff,
unsigned int * count)
213 static_assert(std::is_integral_v<T>,
"Integral required.");
214 packInternal(buff + *count, data,
sizeof(T));
218 void packInternal(std::uint8_t * buff,
const void * data,
unsigned int size);
219 bool writeInternal(
const std::uint8_t * data,
unsigned int size);
221 ICanSenderDelegate * sender;
234 bool accept(
const std::uint8_t * data,
unsigned int size)
235 {
return (
bool)callback && callback(data, size); }
244 template<
typename... Ts,
typename Fn>
247 static_assert(
sizeof...(Ts) > 0 && size<Ts...>() <= 8,
"Illegal cumulative size.");
248 callback = [
this, fn](
const std::uint8_t * raw,
unsigned int len)
249 {
unsigned int count = 0;
250 return size<Ts...>() == len && (
ordered_call{fn, unpack<Ts>(raw, &count)...},
true); };
255 { callback = HandlerFn(); }
259 {
return PdoType::TPDO; }
262 using HandlerFn = std::function<bool(
const std::uint8_t * data,
unsigned int size)>;
267 template<
typename Fn,
typename... Ts>
269 { std::forward<Fn>(fn)(std::forward<Ts>(ts)...); }
273 T unpack(
const std::uint8_t * buff,
unsigned int * count)
275 static_assert(std::is_integral_v<T>,
"Integral required.");
277 unpackInternal(&data, buff + *count,
sizeof(T));
282 void unpackInternal(
void * data,
const std::uint8_t * buff,
unsigned int size);
Implementation-agnostic consumer for TX CAN transfers.
Definition: ICanSenderDelegate.hpp:22
Set of SDO configuration values for a PdoProtocol.
Definition: PdoProtocol.hpp:80
PdoConfiguration & addMapping(std::uint16_t index, std::uint8_t subindex=0x00)
Configure PDO mapping, uses template parameter to deduce object size.
Definition: PdoProtocol.hpp:116
PdoConfiguration & setRtr(bool value)
Set or reset RTR bit.
Definition: PdoProtocol.cpp:62
~PdoConfiguration()
Destructor.
Definition: PdoProtocol.cpp:36
PdoConfiguration & setValid(bool value)
Set or reset valid bit.
Definition: PdoProtocol.cpp:56
PdoConfiguration & setEventTimer(std::uint16_t value)
Set event timer.
Definition: PdoProtocol.cpp:80
PdoConfiguration & setInhibitTime(std::uint16_t value)
Set inhibit time.
Definition: PdoProtocol.cpp:74
PdoConfiguration & operator=(const PdoConfiguration &)
Copy assignment operator.
Definition: PdoProtocol.cpp:45
PdoConfiguration()
Constructor.
Definition: PdoProtocol.cpp:32
PdoConfiguration & setSyncStartValue(std::uint8_t value)
Set sync start value.
Definition: PdoProtocol.cpp:86
PdoConfiguration & setTransmissionType(PdoTransmissionType value)
Set transmission type.
Definition: PdoProtocol.cpp:68
Abstract representation of PDO protocol.
Definition: PdoProtocol.hpp:138
bool configure(const PdoConfiguration &config)
Configure this PDO drive-side via SDO packages.
Definition: PdoProtocol.cpp:97
virtual ~PdoProtocol()=default
Virtual destructor.
PdoType
PDO type.
Definition: PdoProtocol.hpp:157
std::uint16_t getCobId() const
Retrieve COB ID.
Definition: PdoProtocol.hpp:149
PdoProtocol(std::uint8_t id, std::uint16_t cob, unsigned int n, SdoClient *sdo)
Constructor, registers SDO client handle.
Definition: PdoProtocol.hpp:141
virtual PdoType getType() const =0
Retrieve PDO type.
Wrapped enumeration of a PDO transmission type.
Definition: PdoProtocol.hpp:26
transmission_type
Wrapped enumerators.
Definition: PdoProtocol.hpp:30
@ EVENT_DRIVEN_DEVICE_APP_PROFILE
Device application profile-specific event-driven.
Definition: PdoProtocol.hpp:36
@ SYNCHRONOUS_ACYCLIC
Synchronous acyclic.
Definition: PdoProtocol.hpp:31
@ RTR_EVENT_DRIVEN
Event-driven RTR.
Definition: PdoProtocol.hpp:34
@ RTR_SYNCHRONOUS
Synchronous RTR.
Definition: PdoProtocol.hpp:33
@ EVENT_DRIVEN_MANUFACTURER
Manufacturer-specific event-driven.
Definition: PdoProtocol.hpp:35
@ SYNCHRONOUS_CYCLIC
Synchronous cyclic.
Definition: PdoProtocol.hpp:32
constexpr PdoTransmissionType()=default
Default constructor.
static constexpr PdoTransmissionType SYNCHRONOUS_CYCLIC_N(std::uint8_t n)
Cast input byte to an PdoTransmissionType enumerator.
Definition: PdoProtocol.hpp:51
constexpr PdoTransmissionType(transmission_type type)
Constructor, accepts initial transmission type.
Definition: PdoProtocol.hpp:43
static constexpr PdoTransmissionType SYNCHRONOUS_CYCLIC_N()
Cast input byte to an PdoTransmissionType enumerator, performs static check on range [0x01-0xF0].
Definition: PdoProtocol.hpp:56
Representation of RPDO protocol.
Definition: PdoProtocol.hpp:178
void configureSender(ICanSenderDelegate *sender)
Configure CAN sender delegate handle.
Definition: PdoProtocol.hpp:186
PdoType getType() const override
Retrieve PDO type.
Definition: PdoProtocol.hpp:206
ReceivePdo(std::uint8_t id, std::uint16_t cob, unsigned int n, SdoClient *sdo, ICanSenderDelegate *sender=nullptr)
Constructor, registers SDO and CAN sender handles.
Definition: PdoProtocol.hpp:181
bool write(Ts... data)
Send data to the drive.
Definition: PdoProtocol.hpp:197
Representation of SDO client protocol.
Definition: SdoClient.hpp:32
Representation of TPDO protocol.
Definition: PdoProtocol.hpp:229
void registerHandler(Fn &&fn)
Register callback.
Definition: PdoProtocol.hpp:245
void unregisterHandler()
Unregister callback.
Definition: PdoProtocol.hpp:254
PdoType getType() const override
Retrieve PDO type.
Definition: PdoProtocol.hpp:258
bool accept(const std::uint8_t *data, unsigned int size)
Invoke registered callback on raw CAN message data.
Definition: PdoProtocol.hpp:234
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6
Definition: PdoProtocol.hpp:266