ethercat_hardware.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_HARDWARE_H
36 #define ETHERCAT_HARDWARE_H
37 
38 #include <iostream>
39 #include <string>
40 #include <vector>
41 
43 
44 #include <al/ethercat_AL.h>
45 #include <al/ethercat_master.h>
46 #include <al/ethercat_slave_handler.h>
47 
51 
53 
54 #include <boost/accumulators/accumulators.hpp>
55 #include <boost/accumulators/statistics/stats.hpp>
56 #include <boost/accumulators/statistics/max.hpp>
57 #include <boost/accumulators/statistics/mean.hpp>
58 
59 #include <boost/shared_ptr.hpp>
60 
61 #include <boost/thread/thread.hpp>
62 #include <boost/thread/mutex.hpp>
63 #include <boost/thread/condition_variable.hpp>
64 
65 #include <pluginlib/class_loader.h>
66 
67 #include <std_msgs/Bool.h>
68 
69 #include <boost/regex.hpp>
70 
71 using namespace boost::accumulators;
72 
74 {
76  void resetMaxTiming();
77  accumulator_set<double, stats<tag::max, tag::mean> > pack_command_acc_;
78  accumulator_set<double, stats<tag::max, tag::mean> > txandrx_acc_;
79  accumulator_set<double, stats<tag::max, tag::mean> > unpack_state_acc_;
80  accumulator_set<double, stats<tag::max, tag::mean> > publish_acc_;
82  double max_txandrx_;
84  double max_publish_;
86  unsigned device_count_;
87  bool pd_error_;
92  struct netif_counters counters_;
95  const char* motors_halted_reason_;
96 
97  static const bool collect_extra_timing_ = true;
98 };
99 
100 
115 {
116 public:
117 
120 
126  void initialize(const string &interface, unsigned int buffer_size,
127  const std::vector<boost::shared_ptr<EthercatDevice> > &slaves,
128  unsigned int num_ethercat_devices_,
129  unsigned timeout, unsigned max_pd_retries);
130 
138  void publish(const unsigned char *buffer, const EthercatHardwareDiagnostics &diagnostics);
139 
143  void stop();
144 
145 private:
146 
154  void publishDiagnostics();
155 
161  void diagnosticsThreadFunc();
162 
163 
167  static void timingInformation(
169  const string &key,
170  const accumulator_set<double, stats<tag::max, tag::mean> > &acc,
171  double max);
172 
174 
175  boost::mutex diagnostics_mutex_;
176  boost::condition_variable diagnostics_cond_;
178  boost::thread diagnostics_thread_;
179 
181 
183  unsigned char *diagnostics_buffer_;
184  unsigned int buffer_size_;
185  std::vector<boost::shared_ptr<EthercatDevice> > slaves_;
186  unsigned int num_ethercat_devices_;
187  string interface_;
188 
190  unsigned timeout_;
192  unsigned max_pd_retries_;
193 
199  static const unsigned dropped_packet_warning_hold_time_ = 10; //keep warning up for 10 seconds
200 
201  diagnostic_msgs::DiagnosticArray diagnostic_array_;
204  vector<diagnostic_msgs::KeyValue> values_;
206 };
207 
208 
210 {
211 public:
215  EthercatHardware(const std::string& name);
216 
220  ~EthercatHardware();
221 
227  void update(bool reset, bool halt);
228 
234  void init(char *interface, bool allow_unprogrammed);
235 
239  void collectDiagnostics();
240 
241  void printCounters(std::ostream &os=std::cout);
242 
246  bool txandrx_PD(unsigned buffer_size, unsigned char* buffer, unsigned tries);
247 
257  bool publishTrace(int position, const string &reason, unsigned level, unsigned delay);
258 
260 
261 private:
262  static void changeState(EtherCAT_SlaveHandler *sh, EC_State new_state);
263 
264  void loadNonEthercatDevices();
265  boost::shared_ptr<EthercatDevice> configNonEthercatDevice(const std::string &product_id, const std::string &data);
266 
267  void haltMotors(bool error, const char* reason);
268 
270 
271  struct netif *ni_;
272  string interface_;
273 
274  EtherCAT_AL *al_;
275  EtherCAT_Master *em_;
276 
277  boost::shared_ptr<EthercatDevice> configSlave(EtherCAT_SlaveHandler *sh);
278  std::vector<boost::shared_ptr<EthercatDevice> > slaves_;
279  unsigned int num_ethercat_devices_;
280 
281  unsigned char *this_buffer_;
282  unsigned char *prev_buffer_;
283  unsigned char *buffers_;
284  unsigned int buffer_size_;
285 
287  unsigned int reset_state_;
288 
289  unsigned timeout_;
290  unsigned max_pd_retries_;
291 
292  void publishDiagnostics();
293  static void updateAccMax(double &max, const accumulator_set<double, stats<tag::max, tag::mean> > &acc);
298 
300 
302 
304 };
305 
306 #endif /* ETHERCAT_HARDWARE_H */
vector< diagnostic_msgs::KeyValue > values_
unsigned reset_motors_service_count_
Number of times reset_motor service has been used.
EthercatHardwareDiagnostics diagnostics_
Diagnostics information use by publish function.
EtherCAT_Master * em_
ROSCONSOLE_DECL void initialize()
pr2_hardware_interface::HardwareInterface * hw_
Publishes EthercatHardware diagnostics.
std::vector< boost::shared_ptr< EthercatDevice > > slaves_
struct netif * ni_
accumulator_set< double, stats< tag::max, tag::mean > > pack_command_acc_
time taken by all devices packCommand functions
unsigned halt_motors_service_count_
Number of time halt_motor service call is used.
unsigned halt_motors_error_count_
Number of transitions into halt state due to device error.
pluginlib::ClassLoader< EthercatDevice > device_loader_
ros::NodeHandle node_
bool motors_halted_
True if motors are halted.
unsigned char * prev_buffer_
EthernetInterfaceInfo ethernet_interface_info_
Information about Ethernet interface used for EtherCAT communication.
accumulator_set< double, stats< tag::max, tag::mean > > txandrx_acc_
time taken by to transmit and recieve process data
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
const char * motors_halted_reason_
reason that motors first halted
unsigned timeout_
Timeout (in microseconds) to used for sending/recieving packets once in realtime mode.
unsigned int buffer_size_
uint16_t status
std::vector< boost::shared_ptr< EthercatDevice > > slaves_
uint64_t last_dropped_packet_count_
Count of dropped packets last diagnostics cycle.
EthercatHardwareDiagnostics diagnostics_
unsigned int reset_state_
unsigned char * buffers_
diagnostic_updater::DiagnosticStatusWrapper status_
unsigned int num_ethercat_devices_
realtime_tools::RealtimePublisher< std_msgs::Bool > motor_publisher_
boost::condition_variable diagnostics_cond_
EthercatOobCom * oob_com_
unsigned char * this_buffer_
diagnostic_msgs::DiagnosticArray diagnostic_array_
EthercatHardwareDiagnosticsPublisher diagnostics_publisher_
unsigned timeout_
Timeout controls how long EtherCAT driver waits for packet before declaring it as dropped...
unsigned max_pd_retries_
Number of times (in a row) to retry sending process data (realtime data) before halting motors...
boost::mutex diagnostics_mutex_
mutex protects all class data and cond variable
accumulator_set< double, stats< tag::max, tag::mean > > unpack_state_acc_
time taken by all devices updateState functions
unsigned max_pd_retries_
Max number of times to retry sending process data before halting motors.
bool halt_after_reset_
True if motor halt soon after motor reset.
ros::Time last_dropped_packet_time_
Time last packet was dropped 0 otherwise. Used for warning about dropped packets. ...
void init(char *interface)
Definition: motorconf.cpp:87
accumulator_set< double, stats< tag::max, tag::mean > > publish_acc_
time taken by any publishing step in main loop


ethercat_hardware
Author(s): Rob Wheeler , Derek King
autogenerated on Thu Mar 4 2021 03:10:21