ethercat_driver_ros2  main
C++ ROS test
ec_master.hpp
Go to the documentation of this file.
1 // Copyright 2022 ICUBE Laboratory, University of Strasbourg
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ETHERCAT_INTERFACE__EC_MASTER_HPP_
16 #define ETHERCAT_INTERFACE__EC_MASTER_HPP_
17 
18 #include <ecrt.h>
19 
20 #include <time.h>
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <chrono>
26 
27 
29 {
30 
31 class EcMaster
32 {
33 public:
34  explicit EcMaster(const int master = 0);
35  virtual ~EcMaster();
36 
42  void addSlave(uint16_t alias, uint16_t position, EcSlave * slave);
43 
46  int configSlaveSdo(uint16_t slave_position, SdoConfigEntry sdo_config, uint32_t * abort_code);
47 
49  bool activate();
50 
52  virtual void update(uint32_t domain = 0);
53 
56  typedef void (* SIMPLECAT_CONTRL_CALLBACK)(void);
57  virtual void run(SIMPLECAT_CONTRL_CALLBACK user_callback);
58 
60  virtual void stop() {running_ = false;}
61 
64  virtual double elapsedTime();
65 
67  virtual uint64_t elapsedCycles();
68 
71  typedef void (* SIMPLECAT_EXIT_CALLBACK)(int);
72  static void setCtrlCHandler(SIMPLECAT_EXIT_CALLBACK user_callback = NULL);
73 
76  static void setThreadHighPriority();
77 
81  static void setThreadRealTime();
82 
83  void setCtrlFrequency(double frequency)
84  {
85  interval_ = 1000000000.0 / frequency;
86  }
87 
88  uint32_t getInterval() {return interval_;}
89 
90  void readData(uint32_t domain = 0);
91  void writeData(uint32_t domain = 0);
92 
93 private:
95  volatile bool running_ = false;
96 
98  std::chrono::time_point<std::chrono::system_clock> start_t_, curr_t_;
99 
100  // EtherCAT Control
101 
103  struct DomainInfo;
104  void registerPDOInDomain(
105  uint16_t alias, uint16_t position,
106  std::vector<uint32_t> & channel_indices,
107  DomainInfo * domain_info,
108  EcSlave * slave);
109 
111  void checkDomainState(uint32_t domain);
112 
114  void checkMasterState();
115 
117  void checkSlaveStates();
118 
120  static void printWarning(const std::string & message);
121 
123  ec_master_t * master_ = NULL;
124  ec_master_state_t master_state_ = {};
125 
127  struct DomainInfo
128  {
129  explicit DomainInfo(ec_master_t * master);
130  ~DomainInfo();
131 
132  ec_domain_t * domain = NULL;
133  ec_domain_state_t domain_state = {};
134  uint8_t * domain_pd = NULL;
135 
138  std::vector<ec_pdo_entry_reg_t> domain_regs;
139 
141  struct Entry
142  {
143  EcSlave * slave = NULL;
144  int num_pdos = 0;
145  uint32_t * offset = NULL;
146  uint32_t * bit_position = NULL;
147  };
148 
149  std::vector<Entry> entries;
150  };
151 
153  std::map<uint32_t, DomainInfo *> domain_info_;
154 
156  struct SlaveInfo
157  {
158  EcSlave * slave = NULL;
159  ec_slave_config_t * config = NULL;
160  ec_slave_config_state_t config_state = {0};
161  };
162 
163  std::vector<SlaveInfo> slave_info_;
164 
166  uint64_t update_counter_ = 0;
167 
170  uint32_t check_state_frequency_ = 10;
171 
172  uint32_t interval_;
173 };
174 
175 } // namespace ethercat_interface
176 
177 #endif // ETHERCAT_INTERFACE__EC_MASTER_HPP_
Definition: ec_master.hpp:32
virtual uint64_t elapsedCycles()
void readData(uint32_t domain=0)
virtual double elapsedTime()
static void setCtrlCHandler(SIMPLECAT_EXIT_CALLBACK user_callback=NULL)
virtual void update(uint32_t domain=0)
void(* SIMPLECAT_CONTRL_CALLBACK)(void)
Definition: ec_master.hpp:56
void writeData(uint32_t domain=0)
int configSlaveSdo(uint16_t slave_position, SdoConfigEntry sdo_config, uint32_t *abort_code)
configure slave using SDO
EcMaster(const int master=0)
void setCtrlFrequency(double frequency)
Definition: ec_master.hpp:83
void(* SIMPLECAT_EXIT_CALLBACK)(int)
Definition: ec_master.hpp:71
virtual void run(SIMPLECAT_CONTRL_CALLBACK user_callback)
static void setThreadHighPriority()
virtual void stop()
Definition: ec_master.hpp:60
void addSlave(uint16_t alias, uint16_t position, EcSlave *slave)
add a slave device to the master alias and position can be found by running the following command /op...
uint32_t getInterval()
Definition: ec_master.hpp:88
Definition: ec_slave.hpp:32
Definition: ec_sdo_manager.hpp:31
Definition: ec_master.hpp:29
uint32_t * offset
Definition: ec_master.hpp:145
uint32_t * bit_position
Definition: ec_master.hpp:146
int num_pdos
Definition: ec_master.hpp:144
EcSlave * slave
Definition: ec_master.hpp:143