Motion Master
Loading...
Searching...
No Matches
igh_ethercat_master.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef LINUX_BUILD
4
5#include <atomic>
6#include <chrono>
7#include <ctime>
8#include <map>
9#include <thread>
10#include <variant>
11#include <vector>
12#include <zmq.hpp>
13
14#include "ethercat_master.h"
15#include "ethercat_slave.h"
16#include "igh_dynamic.h"
17#include "igh_od_entry.h"
18#include "motion_master_error.h"
19
20#define MAX_FILE_SIZE 9000
21#define EC_MAX_OCTET_STRING_LENGTH 128
22#define DEFTYPE_ENUM_START 0x800u
23#define DEFTYPE_ENUM_END 0xFFFu
24
25class IghEthercatSlave;
26class Notifier;
27
28class IghEthercatMaster : public EthercatMaster {
29 public:
30 explicit IghEthercatMaster(Notifier &notifier, uint32_t master_id = 0);
31 ~IghEthercatMaster() override;
32
38 size_t start() override;
39
43 void stop() override;
44
52 size_t restart() override;
53
60 int32_t execute(uint64_t pending_signals) override;
61
67 bool is_running() override;
68
74 int get_slaves_responding() override;
75
81 std::list<VirtualDevice *> get_slaves() override;
82
88 bool disable_all_slaves() override;
89
90 void abort_all_client_requests() override {};
91
92 /**************************** SLAVE OPERATIONS ****************************/
103 std::variant<MotionMasterError, std::list<std::string>> get_file_list(
104 uint16_t absolute_slave_position);
105
114 std::variant<MotionMasterError, std::vector<uint8_t>> read_file(
115 uint16_t absolute_slave_position, const std::string &file_name);
116
127 MotionMasterError write_file(uint16_t absolute_slave_position,
128 const std::string &file_name,
129 const std::vector<uint8_t> &content,
130 std::function<void(uint8_t)> progress_callback);
131
140 MotionMasterError remove_file(uint16_t absolute_slave_position,
141 const std::string &file_name);
142
154 int32_t write_sii(uint16_t absolute_slave_position,
155 const std::vector<uint8_t> &content);
156
164 static uint8_t get_slave_state(IghEthercatSlave &slave);
165
173 uint8_t get_slave_state(uint16_t absolute_slave_position);
174
186 bool request_slave_state_switch(IghEthercatSlave &slave, uint8_t al_state);
187
199 bool request_slave_state_switch(uint16_t absolute_slave_position,
200 uint8_t al_state);
201
212 bool set_slave_state(IghEthercatSlave &slave, uint8_t al_state);
213
224 bool set_slave_state(uint16_t absolute_slave_position, uint8_t al_state);
225
226 /***************************** SDO OPERATIONS *****************************/
235 IghOdEntry::Error sdo_upload(IghOdEntry &od_entry, bool blocking = true);
236
245 IghOdEntry::Error sdo_download(IghOdEntry &od_entry, bool blocking = true);
246
247 private:
248 // Master information
249 ec_master_t *master_;
250
251 // Variables for data structures
252 ec_domain_t *domain_ = nullptr;
253 uint8_t *process_data_ = nullptr;
254 std::vector<ec_pdo_entry_reg_t> domain_reg_;
255
256 // Diagnostic data structures
257 ec_master_state_t master_state_{};
258 ec_domain_state_t domain_state_{};
259
260 // Map of detected slaves (<absolute slave position, unique pointer to base
261 // class>)
262 std::map<uint16_t, std::unique_ptr<EthercatSlave>> slaveMap;
263
264 Notifier &notifier_;
265
266 std::atomic_bool running_;
267 bool pdo_communication_possible_ = false;
268
275 void update_master_state();
276
283 void update_all_slave_states();
284
290 bool do_start();
291
295 void do_stop();
296
313 std::tuple<uint16_t, uint16_t> init_slave(IghEthercatSlave &slave,
314 uint16_t absolute_slave_position,
315 uint16_t reference_alias,
316 uint16_t relative_position);
317
328 void setup_slave(IghEthercatSlave &slave);
329
337 void refresh_slave(IghEthercatSlave &slave);
338
346 void setup_default_pdos(IghEthercatSlave &slave);
347
355 void setup_pdos(IghEthercatSlave &slave);
356
365 static void setup_sdo_requests(IghEthercatSlave &slave, IghOdEntry &od_entry);
366
376 IghOdEntry::Error upload_via_sdo_request_blocking(IghOdEntry &od_entry);
377
387 static IghOdEntry::Error upload_via_sdo_request(IghOdEntry &od_entry);
388
396 static IghOdEntry::Error read_value_from_sdo_request(IghOdEntry &od_entry);
397
407 IghOdEntry::Error download_via_sdo_request_blocking(IghOdEntry &od_entry);
408
418 static IghOdEntry::Error download_via_sdo_request(IghOdEntry &od_entry);
419
427 static IghOdEntry::Error write_value_to_sdo_request(IghOdEntry &od_entry);
428
440 bool is_supported(IghEthercatSlave &slave);
441
447 void setup_od_entries(IghEthercatSlave &slave);
448
460 void config_detected_smm(IghEthercatSlave &slave);
461
467 static void setup_sync_managers(IghEthercatSlave &slave);
468
476 void cache_files(IghEthercatSlave &slave);
477
486 static void init_hardware_id(IghEthercatSlave &slave);
487
495 std::string get_ecat_foe_error_message(int error_code);
496
504 MotionMasterError prepare_get_file_content(IghEthercatSlave &slave,
505 const std::string &file_name);
506
516 std::variant<MotionMasterError, std::vector<uint8_t>> get_file_content(
517 uint16_t absolute_slave_position, const std::string &file_name);
518
529 std::list<std::string> do_get_file_list(IghEthercatSlave &slave);
530
541 bool write_file_as_parts(uint16_t absolute_slave_position,
542 const std::string &file_name,
543 const std::vector<uint8_t> &content,
544 std::function<void(uint8_t)> progress_callback);
545
557 int32_t set_file_content(uint16_t absolute_slave_position,
558 const std::string &file_name,
559 const std::vector<uint8_t> &content);
560
569 static std::list<std::string> get_file_parts(std::list<std::string> file_list,
570 const std::string &file_name);
571
580 MotionMasterError do_remove_file(uint16_t absolute_slave_position,
581 const std::string &file_name);
582};
583
584#endif
Definition: ethercat_master.h:9
virtual std::list< VirtualDevice * > get_slaves()=0
Get all of the available slaves.
virtual void stop()=0
Stop the EtherCAT master.
virtual size_t restart()=0
Restart the EtherCAT master.
virtual bool is_running()=0
Check if the EtherCAT master is currently running.
virtual bool disable_all_slaves()=0
Switch all slaves to PREOP.
virtual void abort_all_client_requests()=0
virtual size_t start()=0
Start the EtherCAT master.
virtual int32_t execute(uint64_t pending_signals)=0
Main EtherCAT master loop function to be executed at one millisecond interval.
virtual int get_slaves_responding()=0
Return the number of slaves actually responding on the bus.
Definition: motion_master_error.h:6
Definition: notifier.h:10
std::list< std::string > get_file_parts(std::list< std::string > file_list, const std::string &file_name)
Process the provided file list and find the file or its parts.
Definition: util.cc:1576