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 */
d
unsigned int command_size_
bool isClosed(unsigned port)
uint16_t length
Definition: wg_util.h:119
ROSCONSOLE_DECL void initialize()
uint8_t forwarded_rx_error[4]
bool isGreaterThan(unsigned value) const
pthread_mutex_t newDiagnosticsIndexLock_
et1x00_error_counters errorCountersPrev_
virtual void packCommand(unsigned char *buffer, bool halt, bool reset)
unsigned newDiagnosticsIndex_
uint8_t lost_link[4]
diagnostic_updater::DiagnosticStatusWrapper diagnostic_status_
uint8_t res[2]
virtual bool publishTrace(const string &reason, unsigned level, unsigned delay)
Asks device to publish (motor) trace. Only works for devices that support it.
int writeData(EthercatCom *com, EC_UINT address, void const *buffer, EC_UINT length, AddrMode addrMode)
pthread_mutex_t diagnosticsLock_
bool hasLink(unsigned port)
void zero()
static const EC_UINT BASE_ADDR
EtherCAT_SlaveHandler * sh_
bool writeData(EthercatCom *com, EtherCAT_SlaveHandler *sh, EthercatDevice::AddrMode addrMode, unsigned num) const
bool hasCommunication(unsigned port)
unsigned int status_size_
virtual bool unpackState(unsigned char *this_buffer, unsigned char *prev_buffer)
uint8_t epu_error
uint8_t pdi_error
int readWriteData(EthercatCom *com, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode)
int readData(EthercatCom *com, EC_UINT address, void *buffer, EC_UINT length, AddrMode addrMode)
struct EthercatPortDiagnostics __attribute__
bool readData(EthercatCom *com, EtherCAT_SlaveHandler *sh, EthercatDevice::AddrMode addrMode, unsigned num)


ethercat_hardware
Author(s): Rob Wheeler , Derek King
autogenerated on Fri Mar 15 2019 02:53:29