ethercat_device.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #ifndef ETHERCAT_DEVICE_H
36 #define ETHERCAT_DEVICE_H
37 
38 #include <vector>
39 
40 
41 #include <ethercat/ethercat_defs.h>
42 #include <al/ethercat_slave_handler.h>
43 
45 
47 
48 #include <diagnostic_msgs/DiagnosticArray.h>
49 
51 
53 
54 using namespace std;
55 
57 {
58  struct {
59  uint8_t invalid_frame;
60  uint8_t rx_error;
61  } __attribute__((__packed__)) port[4];
62  uint8_t forwarded_rx_error[4];
63  uint8_t epu_error;
64  uint8_t pdi_error;
65  uint8_t res[2];
66  uint8_t lost_link[4];
67  static const EC_UINT BASE_ADDR=0x300;
68  bool isGreaterThan(unsigned value) const;
69  bool isGreaterThan(const et1x00_error_counters &value) const;
70  void zero();
71 } __attribute__((__packed__));
72 
74 {
75  uint16_t status;
76  bool hasLink(unsigned port);
77  bool isClosed(unsigned port);
78  bool hasCommunication(unsigned port);
79  static const EC_UINT BASE_ADDR=0x110;
80 } __attribute__((__packed__));
81 
83 {
85  void zeroTotals();
86  bool hasLink;
87  bool isClosed;
89  uint64_t rxErrorTotal;
90  uint64_t invalidFrameTotal;
92  uint64_t lostLinkTotal;
93 };
94 
96 {
97 public:
99 
100  // Collects diagnostic data from specific ethercat slave, and updates object state
101  //
102  // com EtherCAT communication object is used send/recv packets to/from ethercat chain.
103  // sh slaveHandler of device to collect Diagnostics from
104  // prev previously collected diagnostics (can be pointer to this object)
105  //
106  // collectDiagnotics will send/recieve multiple packets, and may considerable amount of time complete.
107  //
108  void collect(EthercatCom *com, EtherCAT_SlaveHandler *sh);
109 
110  // Puts reviously diagnostic collected diagnostic state to DiagnosticStatus object
111  //
112  // d DiagnositcState to add diagnostics to.
113  // numPorts Number of ports device is supposed to have. 4 is max, 1 is min.
114  void publish(diagnostic_updater::DiagnosticStatusWrapper &d, unsigned numPorts=4) const;
115 
116 protected:
117  void zeroTotals();
118  void accumulate(const et1x00_error_counters &next, const et1x00_error_counters &prev);
119  uint64_t pdiErrorTotal_;
120  uint64_t epuErrorTotal_;
121  EthercatPortDiagnostics portDiagnostics_[4];
122  unsigned nodeAddress_;
125 
130 };
131 
132 
134 {
135 public:
137  virtual void construct(EtherCAT_SlaveHandler *sh, int &start_address);
138 
140  virtual void construct(ros::NodeHandle &nh);
141 
142  EthercatDevice();
143  virtual ~EthercatDevice();
144 
145  virtual int initialize(pr2_hardware_interface::HardwareInterface *, bool allow_unprogrammed=0) = 0;
146 
151  virtual void packCommand(unsigned char *buffer, bool halt, bool reset) {}
152 
153  virtual bool unpackState(unsigned char *this_buffer, unsigned char *prev_buffer) {return true;}
154 
161  virtual void multiDiagnostics(vector<diagnostic_msgs::DiagnosticStatus> &vec, unsigned char *buffer);
162 
169  virtual void diagnostics(diagnostic_updater::DiagnosticStatusWrapper &d, unsigned char *buffer);
170 
176  void ethercatDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &d, unsigned numPorts);
177 
178  virtual void collectDiagnostics(EthercatCom *com);
179 
187  virtual bool publishTrace(const string &reason, unsigned level, unsigned delay) {return false;}
188 
189  enum AddrMode {FIXED_ADDR=0, POSITIONAL_ADDR=1};
190 
194  static int writeData(EthercatCom *com, EtherCAT_SlaveHandler *sh, EC_UINT address, void const* buffer, EC_UINT length, AddrMode addrMode);
195  inline int writeData(EthercatCom *com, EC_UINT address, void const* buffer, EC_UINT length, AddrMode addrMode) {
196  return writeData(com, sh_, address, buffer, length, addrMode);
197  }
198 
202  static int readData(EthercatCom *com, EtherCAT_SlaveHandler *sh, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode);
203  inline int readData(EthercatCom *com, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode) {
204  return readData(com, sh_, address, buffer, length, addrMode);
205  }
206 
210  static int readWriteData(EthercatCom *com, EtherCAT_SlaveHandler *sh, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode);
211  inline int readWriteData(EthercatCom *com, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode) {
212  return readWriteData(com, sh_, address, buffer, length, addrMode);
213  }
214 
215  bool use_ros_;
216 
217  EtherCAT_SlaveHandler *sh_;
218  unsigned int command_size_;
219  unsigned int status_size_;
220 
221  // The device diagnostics are collected with a non-readtime thread that calls collectDiagnostics()
222  // The device published from the realtime loop by indirectly invoking ethercatDiagnostics()
223  // To avoid blocking of the realtime thread (for long) a double buffer is used the
224  // The publisher thread will lock newDiagnosticsIndex when publishing data.
225  // The collection thread will lock deviceDiagnostics when updating deviceDiagnostics
226  // The collection thread will also lock newDiagnosticsIndex at end of update, just before swapping buffers.
228  pthread_mutex_t newDiagnosticsIndexLock_;
229  EthercatDeviceDiagnostics deviceDiagnostics[2];
230  pthread_mutex_t diagnosticsLock_;
231 
232  // Keep diagnostics status as cache. Avoids a lot of construction/destruction of status object.
234 };
235 
236 #endif /* ETHERCAT_DEVICE_H */
EthercatCom
Definition: ethercat_com.h:44
EthercatPortDiagnostics::hasCommunication
bool hasCommunication
Definition: ethercat_device.h:88
et1x00_error_counters
Definition: ethercat_device.h:56
EthercatPortDiagnostics::rxErrorTotal
uint64_t rxErrorTotal
Definition: ethercat_device.h:89
EthercatDeviceDiagnostics
Definition: ethercat_device.h:95
writeData
bool writeData(EthercatCom *com, EtherCAT_SlaveHandler *sh, EthercatDevice::AddrMode addrMode, unsigned num) const
et1x00_error_counters::pdi_error
uint8_t pdi_error
Definition: ethercat_device.h:64
EthercatDevice::writeData
int writeData(EthercatCom *com, EC_UINT address, void const *buffer, EC_UINT length, AddrMode addrMode)
Definition: ethercat_device.h:195
isGreaterThan
bool isGreaterThan(unsigned value) const
EthercatDevice::diagnosticsLock_
pthread_mutex_t diagnosticsLock_
Definition: ethercat_device.h:230
readData
bool readData(EthercatCom *com, EtherCAT_SlaveHandler *sh, EthercatDevice::AddrMode addrMode, unsigned num)
EthercatDeviceDiagnostics::diagnosticsValid_
bool diagnosticsValid_
Definition: ethercat_device.h:127
EthercatDeviceDiagnostics::diagnosticsFirst_
bool diagnosticsFirst_
Definition: ethercat_device.h:126
EthercatDevice::newDiagnosticsIndexLock_
pthread_mutex_t newDiagnosticsIndexLock_
Definition: ethercat_device.h:228
EthercatDevice::readWriteData
int readWriteData(EthercatCom *com, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode)
Definition: ethercat_device.h:211
EthercatDeviceDiagnostics::nodeAddress_
unsigned nodeAddress_
Definition: ethercat_device.h:122
EthercatDevice::AddrMode
AddrMode
Definition: ethercat_device.h:189
EthercatDevice::use_ros_
bool use_ros_
Definition: ethercat_device.h:215
EthercatPortDiagnostics::lostLinkTotal
uint64_t lostLinkTotal
Definition: ethercat_device.h:92
EthercatDeviceDiagnostics::devicesRespondingToNodeAddress_
int devicesRespondingToNodeAddress_
Definition: ethercat_device.h:129
EthercatPortDiagnostics::forwardedRxErrorTotal
uint64_t forwardedRxErrorTotal
Definition: ethercat_device.h:91
res
uint8_t res[2]
Definition: ethercat_device.h:9
EthercatDevice::newDiagnosticsIndex_
unsigned newDiagnosticsIndex_
Definition: ethercat_device.h:227
lost_link
uint8_t lost_link[4]
Definition: ethercat_device.h:10
isClosed
bool isClosed(unsigned port)
pr2_hardware_interface::HardwareInterface
EthercatDevice::publishTrace
virtual bool publishTrace(const string &reason, unsigned level, unsigned delay)
Asks device to publish (motor) trace. Only works for devices that support it.
Definition: ethercat_device.h:187
hardware_interface.h
EthercatDeviceDiagnostics::resetDetected_
bool resetDetected_
Definition: ethercat_device.h:128
EthercatPortDiagnostics::invalidFrameTotal
uint64_t invalidFrameTotal
Definition: ethercat_device.h:90
EthercatPortDiagnostics::isClosed
bool isClosed
Definition: ethercat_device.h:87
EthercatDevice::sh_
EtherCAT_SlaveHandler * sh_
Definition: ethercat_device.h:217
class_list_macros.h
et1x00_dl_status
Definition: ethercat_device.h:73
ethercat_com.h
et1x00_dl_status::status
uint16_t status
Definition: ethercat_device.h:75
hasLink
bool hasLink(unsigned port)
EthercatDeviceDiagnostics::errorCountersPrev_
et1x00_error_counters errorCountersPrev_
Definition: ethercat_device.h:123
EthercatDevice
Definition: ethercat_device.h:133
zero
void zero()
EthercatDevice::diagnostic_status_
diagnostic_updater::DiagnosticStatusWrapper diagnostic_status_
Definition: ethercat_device.h:233
EthercatPortDiagnostics::hasLink
bool hasLink
Definition: ethercat_device.h:86
d
d
DiagnosticStatusWrapper.h
et1x00_error_counters::epu_error
uint8_t epu_error
Definition: ethercat_device.h:63
hasCommunication
bool hasCommunication(unsigned port)
EthercatDeviceDiagnostics::epuErrorTotal_
uint64_t epuErrorTotal_
Definition: ethercat_device.h:120
EthercatDevice::packCommand
virtual void packCommand(unsigned char *buffer, bool halt, bool reset)
Definition: ethercat_device.h:151
BASE_ADDR
static const EC_UINT BASE_ADDR
Definition: ethercat_device.h:11
EthercatDeviceDiagnostics::pdiErrorTotal_
uint64_t pdiErrorTotal_
Definition: ethercat_device.h:119
std
EthercatDevice::status_size_
unsigned int status_size_
Definition: ethercat_device.h:219
length
uint16_t length
Definition: wg_util.h:151
et1x00_error_counters::invalid_frame
uint8_t invalid_frame
Definition: ethercat_device.h:59
initialize
ROSCONSOLE_DECL void initialize()
EthercatDevice::readData
int readData(EthercatCom *com, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode)
Definition: ethercat_device.h:203
diagnostic_updater::DiagnosticStatusWrapper
__attribute__
struct EthercatPortDiagnostics __attribute__
EthercatDevice::command_size_
unsigned int command_size_
Definition: ethercat_device.h:218
EthercatDeviceDiagnostics::errorCountersMayBeCleared_
bool errorCountersMayBeCleared_
Definition: ethercat_device.h:124
EthercatPortDiagnostics
Definition: ethercat_device.h:82
et1x00_error_counters::rx_error
uint8_t rx_error
Definition: ethercat_device.h:60
ros::NodeHandle
EthercatDevice::unpackState
virtual bool unpackState(unsigned char *this_buffer, unsigned char *prev_buffer)
Definition: ethercat_device.h:153
forwarded_rx_error
uint8_t forwarded_rx_error[4]
Definition: ethercat_device.h:6


ethercat_hardware
Author(s): Rob Wheeler , Derek King
autogenerated on Sun Apr 2 2023 02:44:04