Lely core libraries  1.9.2
lely::canopen::Node Class Reference

The base class for CANopen nodes. More...

#include <node.hpp>

Inheritance diagram for lely::canopen::Node:
Collaboration diagram for lely::canopen::Node:

Data Structures

struct  Impl_
 The internal implementation of the CANopen node. More...
 

Public Member Functions

 Node (aio::TimerBase &timer, aio::CanBusBase &bus, const ::std::string &dcf_txt, const ::std::string &dcf_bin="", uint8_t id=0xff)
 Creates a new CANopen node. More...
 
void Reset ()
 (Re)starts the node. More...
 
- Public Member Functions inherited from lely::canopen::IoContext
 IoContext (aio::TimerBase &timer, aio::CanBusBase &bus, BasicLockable *mutex=nullptr)
 Creates a new I/O context. More...
 
aio::ExecutorBase GetExecutor () const noexcept
 Returns the executor used to process I/O events on the CAN bus.
 
template<class F >
void Post (F &&f)
 Schedules the specified Callable object for execution. More...
 
- Public Member Functions inherited from lely::canopen::Device
 Device (const ::std::string &dcf_txt, const ::std::string &dcf_bin="", uint8_t id=0xff, BasicLockable *mutex=nullptr)
 Creates a new CANopen device description. More...
 
uint8_t netid () const noexcept
 Returns the network-ID.
 
uint8_t id () const noexcept
 Returns the node-ID.
 
template<class T >
typename ::std::enable_if< detail::IsCanopenType< T >::value, T >::type Read (uint16_t idx, uint8_t subidx) const
 Submits an SDO upload request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenType< T >::value, T >::type Read (uint16_t idx, uint8_t subidx, ::std::error_code &ec) const
 Submits an SDO upload request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenBasic< T >::value >::type Write (uint16_t idx, uint8_t subidx, T value)
 Submits an SDO download request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenBasic< T >::value >::type Write (uint16_t idx, uint8_t subidx, T value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenArray< T >::value >::type Write (uint16_t idx, uint8_t subidx, const T &value)
 Submits an SDO download request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenArray< T >::value >::type Write (uint16_t idx, uint8_t subidx, const T &value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char *value)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char *value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char16_t *value)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char16_t *value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const void *p, ::std::size_t n)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const void *p, ::std::size_t n, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 

Protected Member Functions

virtual void lock () final override
 Blocks until a lock can be obtained for the current execution agent (thread, process, task). More...
 
virtual void unlock () final override
 Releases the lock held by the execution agent. Throws no exceptions.
 
void OnCanState (CanState new_state, CanState old_state) noexcept override
 Implements the default behavior for a CAN bus state change. More...
 
CONMTnmt () const noexcept
 Returns a pointer to the internal CANopen NMT master/slave service from <lely/co/nmt.hpp>.
 
void RpdoRtr (int num=0)
 Requests the transmission of a PDO. More...
 
void TpdoEvent (int num=0)
 Triggers the transmission of an event-driven (asynchronous) PDO. More...
 
virtual void OnCommand (NmtCommand cs) noexcept
 The function invoked when an NMT command is received from the master. More...
 
virtual void OnHeartbeat (uint8_t id, bool occurred) noexcept
 The function invoked when a heartbeat timeout event occurs or is resolved. More...
 
virtual void OnState (uint8_t id, NmtState st) noexcept
 The function invoked when an NMT state change or boot-up event is detected for a remote node by the heartbeat protocol. More...
 
virtual void OnRpdo (int num, ::std::error_code ec, const void *p, ::std::size_t n) noexcept
 The function invoked when a Receive-PDO is processed. More...
 
virtual void OnRpdoError (int num, uint16_t eec, uint8_t er) noexcept
 The function invoked when a Receive-PDO length mismatch or timeout error occurs. More...
 
virtual void OnTpdo (int num, ::std::error_code ec, const void *p, ::std::size_t n) noexcept
 The function invoked after a Transmit-PDO is sent or an error occurs. More...
 
virtual void OnSync (uint8_t cnt, const time_point &t) noexcept
 The function invoked when a SYNC message is sent/received. More...
 
virtual void OnSyncError (uint16_t eec, uint8_t er) noexcept
 The function invoked when the data length of a received SYNC message does not match. More...
 
virtual void OnTime (const ::std::chrono::system_clock::time_point &abs_time) noexcept
 The function invoked when a TIME message is received. More...
 
virtual void OnEmcy (uint8_t id, uint16_t eec, uint8_t er, uint8_t msef[5]) noexcept
 The function invoked when an EMCY message is received. More...
 
- Protected Member Functions inherited from lely::canopen::IoContext
CANNetnet () const noexcept
 Returns a pointer to the internal CAN network interface from <lely/can/net.hpp>.
 
void SetTime ()
 Update the CAN network time. More...
 
virtual void OnCanError (CanError error) noexcept
 The function invoked when an error is detected on the CAN bus. More...
 
 IoContext (aio::TimerBase &timer, aio::CanBusBase &bus, BasicLockable *mutex=nullptr)
 Creates a new I/O context. More...
 
aio::ExecutorBase GetExecutor () const noexcept
 Returns the executor used to process I/O events on the CAN bus.
 
template<class F >
void Post (F &&f)
 Schedules the specified Callable object for execution. More...
 
- Protected Member Functions inherited from lely::canopen::Device
CODevdev () const noexcept
 Returns a pointer to the internal CANopen device from <lely/co/dev.hpp>.
 
const ::std::type_info & Type (uint16_t idx, uint8_t subidx) const
 Returns the type of a sub-object. More...
 
const ::std::type_info & Type (uint16_t idx, uint8_t subidx, ::std::error_code &ec) const
 Returns the type of a sub-object. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenType< T >::value, T >::type Get (uint16_t idx, uint8_t subidx) const
 Reads the value of a sub-object. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenType< T >::value, T >::type Get (uint16_t idx, uint8_t subidx, ::std::error_code &ec) const
 Reads the value of a sub-object. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenBasic< T >::value >::type Set (uint16_t idx, uint8_t subidx, T value)
 Writes a CANopen basic value to a sub-object. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenBasic< T >::value >::type Set (uint16_t idx, uint8_t subidx, T value, ::std::error_code &ec)
 Writes a CANopen basic value to a sub-object. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenArray< T >::value >::type Set (uint16_t idx, uint8_t subidx, const T &value)
 Writes a CANopen array value to a sub-object. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenArray< T >::value >::type Set (uint16_t idx, uint8_t subidx, const T &value, ::std::error_code &ec)
 Writes a CANopen array value to a sub-object. More...
 
void Set (uint16_t idx, uint8_t subidx, const char *value)
 Writes a VISIBLE_STRING to a sub-object. More...
 
void Set (uint16_t idx, uint8_t subidx, const char *value, ::std::error_code &ec)
 Writes a VISIBLE_STRING to a sub-object. More...
 
void Set (uint16_t idx, uint8_t subidx, const char16_t *value)
 Writes a UNICODE_STRING to a sub-object. More...
 
void Set (uint16_t idx, uint8_t subidx, const char16_t *value, ::std::error_code &ec)
 Writes a UNICODE_STRING to a sub-object. More...
 
void Set (uint16_t idx, uint8_t subidx, const void *p, ::std::size_t n)
 Writes an OCTET_STRING or DOMAIN value to a sub-object. More...
 
void Set (uint16_t idx, uint8_t subidx, const void *p, ::std::size_t n, ::std::error_code &ec)
 Writes an OCTET_STRING or DOMAIN value to a sub-object. More...
 
 Device (const ::std::string &dcf_txt, const ::std::string &dcf_bin="", uint8_t id=0xff, BasicLockable *mutex=nullptr)
 Creates a new CANopen device description. More...
 
uint8_t netid () const noexcept
 Returns the network-ID.
 
uint8_t id () const noexcept
 Returns the node-ID.
 
template<class T >
typename ::std::enable_if< detail::IsCanopenType< T >::value, T >::type Read (uint16_t idx, uint8_t subidx) const
 Submits an SDO upload request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenType< T >::value, T >::type Read (uint16_t idx, uint8_t subidx, ::std::error_code &ec) const
 Submits an SDO upload request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenBasic< T >::value >::type Write (uint16_t idx, uint8_t subidx, T value)
 Submits an SDO download request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenBasic< T >::value >::type Write (uint16_t idx, uint8_t subidx, T value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenArray< T >::value >::type Write (uint16_t idx, uint8_t subidx, const T &value)
 Submits an SDO download request to the local object dictionary. More...
 
template<class T >
typename ::std::enable_if< detail::IsCanopenArray< T >::value >::type Write (uint16_t idx, uint8_t subidx, const T &value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char *value)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char *value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char16_t *value)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const char16_t *value, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const void *p, ::std::size_t n)
 Submits an SDO download request to the local object dictionary. More...
 
void Write (uint16_t idx, uint8_t subidx, const void *p, ::std::size_t n, ::std::error_code &ec)
 Submits an SDO download request to the local object dictionary. More...
 

Detailed Description

The base class for CANopen nodes.

This class implements the lely::canopen::BasicLockable mutex used by lely::canopen::IoContext and lely::canopen::Device. The mutex MUST be unlocked when any public member function is invoked (Reset()); it will be locked for the duration of any call to a virtual member function.

Definition at line 109 of file node.hpp.

Constructor & Destructor Documentation

◆ Node()

lely::canopen::Node::Node ( aio::TimerBase &  timer,
aio::CanBusBase &  bus,
const ::std::string &  dcf_txt,
const ::std::string &  dcf_bin = "",
uint8_t  id = 0xff 
)

Creates a new CANopen node.

After creation, the node is in the NMT 'Initialisation' state and does not yet create any services or perform any communication. Call Reset() to start the boot-up process.

Parameters
timerthe timer used for CANopen events.
busa handle to the CAN bus.
dcf_txtthe path of the text EDS or DCF containing the device description.
dcf_binthe path of the (binary) concise DCF containing the values of (some of) the objets in the object dictionary. If dcf_bin is empty, no concise DCF is loaded.
idthe node-ID (in the range [1..127, 255]). If id is 255 (unconfigured), the node-ID is obtained from the DCF.

Definition at line 93 of file node.cpp.

Member Function Documentation

◆ Reset()

void lely::canopen::Node::Reset ( )

(Re)starts the node.

This function behaves as if an NMT 'reset node' command has been received. Note that this function will cause the invocation of OnCommand() and therefore MUST NOT be called from that function.

Definition at line 103 of file node.cpp.

◆ lock()

void lely::canopen::Node::lock ( )
finaloverrideprotectedvirtual

Blocks until a lock can be obtained for the current execution agent (thread, process, task).

If an exception is thrown, no lock is obtained.

Implements lely::canopen::BasicLockable.

Definition at line 114 of file node.cpp.

◆ OnCanState()

void lely::canopen::Node::OnCanState ( CanState  new_state,
CanState  old_state 
)
overrideprotectedvirtualnoexcept

Implements the default behavior for a CAN bus state change.

If the CAN bus is in error passive mode or has recovered from bus off, an EMCY message is sent (see Table 26 in CiA 301 v4.2.0).

Reimplemented from lely::canopen::IoContext.

Definition at line 123 of file node.cpp.

◆ RpdoRtr()

void lely::canopen::Node::RpdoRtr ( int  num = 0)
protected

Requests the transmission of a PDO.

Parameters
numthe PDO number (in the range [1..512]).

Definition at line 147 of file node.cpp.

◆ TpdoEvent()

void lely::canopen::Node::TpdoEvent ( int  num = 0)
protected

Triggers the transmission of an event-driven (asynchronous) PDO.

Parameters
numthe PDO number (in the range [1..512]).
Exceptions
std::system_error(std::errc::resource_unavailable_try_again)if the inhibit time has not yet elapsed.

Definition at line 156 of file node.cpp.

◆ OnCommand()

virtual void lely::canopen::Node::OnCommand ( NmtCommand  cs)
inlineprotectedvirtualnoexcept

The function invoked when an NMT command is received from the master.

Note that Reset() MUST NOT be called from OnCommand(), since the node is undergoing an NMT state transition, possibly triggered by Reset() itself.

Parameters
csthe NMT command specifier.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 201 of file node.hpp.

◆ OnHeartbeat()

virtual void lely::canopen::Node::OnHeartbeat ( uint8_t  id,
bool  occurred 
)
inlineprotectedvirtualnoexcept

The function invoked when a heartbeat timeout event occurs or is resolved.

Note that depending on the value of object 1029:01 (Error behavior object), the occurrence of a heartbeat timeout event MAY trigger an NMT state transition. If so, this function is called after the state change completes.

Parameters
idthe node-ID (in the range [1..127]).
occurredtrue if the heartbeat timeout event occurred, false if it was resolved.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 217 of file node.hpp.

◆ OnState()

virtual void lely::canopen::Node::OnState ( uint8_t  id,
NmtState  st 
)
inlineprotectedvirtualnoexcept

The function invoked when an NMT state change or boot-up event is detected for a remote node by the heartbeat protocol.

Parameters
idthe node-ID (in the range [1..127]).
stthe state of the remote node. Note that the NMT sub-states NmtState::RESET_NODE and NmtState::RESET_COMM are never reported for remote nodes.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 232 of file node.hpp.

◆ OnRpdo()

virtual void lely::canopen::Node::OnRpdo ( int  num,
::std::error_code  ec,
const void *  p,
::std::size_t  n 
)
inlineprotectedvirtualnoexcept

The function invoked when a Receive-PDO is processed.

In case of a PDO length mismatch error, OnRpdoError() is invoked after this function.

Parameters
numthe PDO number (in the range [1..512]).
ecthe SDO abort code:
  • 0 on success;
  • SdoErrc::NO_OBJ or SdoErrc::NO_SUB if one of the objects or sub-objects does not exist, respectively;
  • SdoErrc::NO_WRITE if one of the sub-objects is read only;
  • SdoErrc::NO_PDO if one of the sub-objects cannot be mapped to a PDO;
  • SdoErrc::PDO_LEN if the number and length of the mapped objects exceeds the PDO length (OnRpdoError() will be invoked after this function returns);
  • the abort code generated by the SDO download request to the local object dictionary.
pa pointer to the bytes received.
nthe number of bytes at p.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 258 of file node.hpp.

◆ OnRpdoError()

virtual void lely::canopen::Node::OnRpdoError ( int  num,
uint16_t  eec,
uint8_t  er 
)
inlineprotectedvirtualnoexcept

The function invoked when a Receive-PDO length mismatch or timeout error occurs.

The default implementation sends an EMCY message.

Parameters
numthe PDO number (in the range [1..512]).
eecthe emergency error code:
  • 0x8210: PDO not processed due to length error;
  • 0x8220: PDO length exceeded;
  • 0x8250: RPDO timeout.
erthe error register (0x10).

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 280 of file node.hpp.

◆ OnTpdo()

virtual void lely::canopen::Node::OnTpdo ( int  num,
::std::error_code  ec,
const void *  p,
::std::size_t  n 
)
inlineprotectedvirtualnoexcept

The function invoked after a Transmit-PDO is sent or an error occurs.

Parameters
numthe PDO number (in the range [1..512]).
ecthe SDO abort code:
  • 0 on success;
  • SdoErrc::NO_OBJ or SdoErrc::NO_SUB if one of the objects or sub-objects does not exist, respectively;
  • SdoErrc::NO_READ if one of the sub-objects is write only;
  • SdoErrc::NO_PDO if one of the sub-objects cannot be mapped to a PDO;
  • SdoErrc::PDO_LEN if the number and length of the mapped objects exceeds the PDO length;
  • SdoErrc::TIMEOUT if the synchronous time window expires;
  • the abort code generated by the SDO upload request to the local object dictionary.
pa pointer to the bytes sent.
nthe number of bytes at p.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 305 of file node.hpp.

◆ OnSync()

virtual void lely::canopen::Node::OnSync ( uint8_t  cnt,
const time_point &  t 
)
inlineprotectedvirtualnoexcept

The function invoked when a SYNC message is sent/received.

Note that this function is called after all PDOs are processed/sent.

Parameters
cntthe counter (in the range [1..240]), or 0 if the SYNC message is empty.
tthe time at which the SYNC message was sent/received.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 322 of file node.hpp.

◆ OnSyncError()

virtual void lely::canopen::Node::OnSyncError ( uint16_t  eec,
uint8_t  er 
)
inlineprotectedvirtualnoexcept

The function invoked when the data length of a received SYNC message does not match.

The default implementation transmits an EMCY message.

Parameters
eecthe emergency error code (0x8240).
erthe error register (0x10).

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 337 of file node.hpp.

◆ OnTime()

virtual void lely::canopen::Node::OnTime ( const ::std::chrono::system_clock::time_point &  abs_time)
inlineprotectedvirtualnoexcept

The function invoked when a TIME message is received.

Parameters
abs_timea time point representing the received time stamp.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 347 of file node.hpp.

◆ OnEmcy()

virtual void lely::canopen::Node::OnEmcy ( uint8_t  id,
uint16_t  eec,
uint8_t  er,
uint8_t  msef[5] 
)
inlineprotectedvirtualnoexcept

The function invoked when an EMCY message is received.

Parameters
idthe node-ID (in the range [1..127]).
eecthe emergency error code.
erthe error register.
msefthe manufacturer-specific error code.

Reimplemented in lely::canopen::AsyncMaster, and lely::canopen::BasicMaster.

Definition at line 360 of file node.hpp.


The documentation for this class was generated from the following files: