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
13#include "ethercat_master.h"
14#include "ethercat_slave.h"
15#include "igh_dynamic.h"
16#include "igh_od_entry.h"
17#include "motion_master_error.h"
18
19#define MAX_FILE_SIZE 9000
20#define EC_MAX_OCTET_STRING_LENGTH 128
21#define DEFTYPE_ENUM_START 0x800u
22#define DEFTYPE_ENUM_END 0xFFFu
23
24class IghEthercatSlave;
25class Notifier;
26
27class IghEthercatMaster : public EthercatMaster {
28 public:
29 explicit IghEthercatMaster(Notifier &notifier, uint32_t master_id = 0);
30 ~IghEthercatMaster() override;
31
37 size_t start() override;
38
42 void stop() override;
43
51 size_t restart() override;
52
59 int32_t execute(uint64_t pending_signals) override;
60
66 bool is_running() override;
67
73 int get_slaves_responding() override;
74
80 std::list<VirtualDevice *> get_slaves() override;
81
87 bool disable_all_slaves() override;
88
89 void abort_all_client_requests() override {};
90
91 /**************************** SLAVE OPERATIONS ****************************/
102 std::variant<MotionMasterError, std::list<std::string>> get_file_list(
103 uint16_t absolute_slave_position);
104
113 std::variant<MotionMasterError, std::vector<uint8_t>> read_file(
114 uint16_t absolute_slave_position, const std::string &file_name);
115
126 MotionMasterError write_file(uint16_t absolute_slave_position,
127 const std::string &file_name,
128 const std::vector<uint8_t> &content,
129 std::function<void(uint8_t)> progress_callback);
130
139 MotionMasterError remove_file(uint16_t absolute_slave_position,
140 const std::string &file_name);
141
153 int32_t write_sii(uint16_t absolute_slave_position,
154 const std::vector<uint8_t> &content);
155
163 static uint8_t get_slave_state(IghEthercatSlave &slave);
164
172 uint8_t get_slave_state(uint16_t absolute_slave_position);
173
185 bool request_slave_state_switch(IghEthercatSlave &slave, uint8_t al_state);
186
198 bool request_slave_state_switch(uint16_t absolute_slave_position,
199 uint8_t al_state);
200
211 bool set_slave_state(IghEthercatSlave &slave, uint8_t al_state);
212
223 bool set_slave_state(uint16_t absolute_slave_position, uint8_t al_state);
224
225 /***************************** SDO OPERATIONS *****************************/
234 IghOdEntry::Error sdo_upload(IghOdEntry &od_entry, bool blocking = true);
235
244 IghOdEntry::Error sdo_download(IghOdEntry &od_entry, bool blocking = true);
245
246 private:
247 // Master information
248 ec_master_t *master_;
249
250 // Variables for data structures
251 ec_domain_t *domain_ = nullptr;
252 uint8_t *process_data_ = nullptr;
253 std::vector<ec_pdo_entry_reg_t> domain_reg_;
254
255 // Diagnostic data structures
256 ec_master_state_t master_state_{};
257 ec_domain_state_t domain_state_{};
258
259 // Map of detected slaves (<absolute slave position, unique pointer to base
260 // class>)
261 std::map<uint16_t, std::unique_ptr<EthercatSlave>> slaveMap;
262
263 Notifier &notifier_;
264
265 std::atomic_bool running_;
266 bool pdo_communication_possible_ = false;
267
274 void update_master_state();
275
282 void update_all_slave_states();
283
289 bool do_start();
290
294 void do_stop();
295
312 std::tuple<uint16_t, uint16_t> init_slave(IghEthercatSlave &slave,
313 uint16_t absolute_slave_position,
314 uint16_t reference_alias,
315 uint16_t relative_position);
316
327 void setup_slave(IghEthercatSlave &slave);
328
336 void refresh_slave(IghEthercatSlave &slave);
337
345 void setup_default_pdos(IghEthercatSlave &slave);
346
354 void setup_pdos(IghEthercatSlave &slave);
355
364 static void setup_sdo_requests(IghEthercatSlave &slave, IghOdEntry &od_entry);
365
375 IghOdEntry::Error upload_via_sdo_request_blocking(IghOdEntry &od_entry);
376
386 static IghOdEntry::Error upload_via_sdo_request(IghOdEntry &od_entry);
387
395 static IghOdEntry::Error read_value_from_sdo_request(IghOdEntry &od_entry);
396
406 IghOdEntry::Error download_via_sdo_request_blocking(IghOdEntry &od_entry);
407
417 static IghOdEntry::Error download_via_sdo_request(IghOdEntry &od_entry);
418
426 static IghOdEntry::Error write_value_to_sdo_request(IghOdEntry &od_entry);
427
439 bool is_supported(IghEthercatSlave &slave);
440
446 void setup_od_entries(IghEthercatSlave &slave);
447
459 void config_detected_smm(IghEthercatSlave &slave);
460
466 static void setup_sync_managers(IghEthercatSlave &slave);
467
475 void cache_files(IghEthercatSlave &slave);
476
485 static void init_hardware_id(IghEthercatSlave &slave);
486
494 std::string get_ecat_foe_error_message(int error_code);
495
503 MotionMasterError prepare_get_file_content(IghEthercatSlave &slave,
504 const std::string &file_name);
505
515 std::variant<MotionMasterError, std::vector<uint8_t>> get_file_content(
516 uint16_t absolute_slave_position, const std::string &file_name);
517
528 std::list<std::string> do_get_file_list(IghEthercatSlave &slave);
529
540 bool write_file_as_parts(uint16_t absolute_slave_position,
541 const std::string &file_name,
542 const std::vector<uint8_t> &content,
543 std::function<void(uint8_t)> progress_callback);
544
556 int32_t set_file_content(uint16_t absolute_slave_position,
557 const std::string &file_name,
558 const std::vector<uint8_t> &content);
559
568 static std::list<std::string> get_file_parts(std::list<std::string> file_list,
569 const std::string &file_name);
570
579 MotionMasterError do_remove_file(uint16_t absolute_slave_position,
580 const std::string &file_name);
581};
582
583#endif
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:1545
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:9