Motorcortex Core  version: 2.7.6
ecat_domain.h
1 /*
2  * Developer : Alexey Zakharov (alexey.zakharov@vectioneer.com)
3  * All rights reserved. Copyright (c) 2015 - 2018 VECTIONEER.
4  */
5 
6 #ifndef MOTORCORTEX_LIB_ECAT_DOMAIN_H
7 #define MOTORCORTEX_LIB_ECAT_DOMAIN_H
8 
9 #include "ecat_domainbase.h"
10 #include "ps_handlers.h"
11 #include "ps_parameter.h"
12 #include "utl_cmdparser.h"
13 #include <algorithm>
14 #include <cstdint>
15 #include <list>
16 #include <string>
17 #include <vector>
18 
19 namespace tinyxml2 {
20 class XMLElement;
21 }
22 
23 namespace mcx {
24 
25 namespace container {
26 struct TaskTime;
27 }
28 
29 namespace ecat {
30 
31 class Domain : public DomainBase {
32 
33  struct PdoData {
34  std::string name;
35  size_t bit_offset;
36  size_t bit_length;
37  uint64_t* param_ptr;
39  std::vector<PdoData> entries;
40  std::vector<ParameterLink> links;
41  };
42 
43  struct SdoData {
44  size_t sdo_hash;
45  bool* read;
46  parameter_server::ParamHandle write_handle;
48  std::vector<ParameterLink> links;
49  std::vector<SdoData> entries;
50  };
51 
52  using MemoryPool = std::vector<uint64_t*>;
53 
54 public:
55  Domain(const std::string& name, const std::string& path, const std::string& filename);
56 
57  Domain(const std::string& path, const std::string& filename);
58 
59  Domain(const std::string& path, tinyxml2::XMLElement* domain_xml, std::string version);
60 
61  virtual ~Domain();
62 
63  [[nodiscard]] const char* getName() const;
64 
65  [[nodiscard]] const char* getPath() const;
66 
67  [[nodiscard]] const char* fileName() const;
68 
69  [[nodiscard]] const char* getFullPath() const;
70 
71  bool load();
72 
73  [[nodiscard]] bool hasError() const;
74 
75  [[nodiscard]] double getDtSec() const;
76 
77  static std::tuple<parameter_server::DataType, size_t, size_t> toParamDataType(DataTypeDesc data_type_desc);
78 
79 private:
80  static size_t bitsToBytes(size_t bits);
81 
82  static void updateInputs(const uint8_t* data, std::vector<PdoData>& pdo_inputs);
83 
84  static bool updateOutputs(std::vector<PdoData>& pdo_outputs, uint8_t* data);
85 
86  // implementation of the interfaces for the ethercat drivers
87  void updateProcessData(DomainState state, uint8_t* data, unsigned int length) override;
88 
89  std::list<SdoId> updateServiceQueue() override;
90 
91  EcatDomainConfig& getDomainConfig() override;
92 
93  // implementation for the ethercat module
94  friend class Module;
95 
96  void setDtSec(double sec);
97 
98  bool addParameters(parameter_server::Parameter* local_branch, utils::SystemMode system_mode);
99 
100  static bool linkInputParameters(parameter_server::Parameter* root, utils::SystemMode system_mode,
101  std::vector<Domain::PdoData>& pdo_inputs);
102 
103  static bool linkOutputParameters(parameter_server::Parameter* root, utils::SystemMode system_mode,
104  std::vector<PdoData>& pdo_outputs);
105 
106  static bool linkSdoParameters(parameter_server::Parameter* root, utils::SystemMode system_mode,
107  std::vector<SdoData>& sdos);
108 
109  bool linkParameters(parameter_server::Parameter* root, utils::SystemMode system_mode);
110 
111  void readSdo();
112 
113  // internal stuff
114  static inline uint64_t toggleBits(uint64_t l, uint64_t r) { return ((1UL << r) - 1UL) ^ ((1UL << l) - 1UL); }
115 
116  static parameter_server::ParameterType toParamDir(EcatDirection dir, utils::SystemMode system_mode);
117 
118  static bool cutPdoDataTree(std::vector<PdoData>& pdos);
119 
120  static bool hasNamedChildren(const EcatPdoInfo& entry);
121 
122  Domain::PdoData addPdoEntry(const std::string& path, const EcatPdoInfo& entry, EcatDirection dir,
123  parameter_server::Parameter* local_branch, utils::SystemMode system_mode,
124  std::vector<uint64_t*>& mem_pool);
125 
126  void addSdoEntry(const std::string& path, const EcatSdoConfig& entry, parameter_server::Parameter* local_branch,
127  utils::SystemMode system_mode, std::vector<SdoData>& sdos);
128 
129  std::string name_;
130  std::string path_;
131  std::string filename_;
132  std::string full_path_;
133 
134  struct {
135  std::string version;
136  tinyxml2::XMLElement* element{};
137 
138  [[nodiscard]] bool isLoaded() const { return element; }
139  } domain_xml_{};
140 
141  EcatDomainConfig domain_config_;
142  std::vector<PdoData> pdo_inputs_;
143  std::vector<PdoData> pdo_outputs_;
144  std::vector<SdoData> sdos_;
145  MemoryPool memory_pool_;
146 
147  DomainState state_{DomainState::DOMAIN_OFF};
148 
149  bool error_{};
150  struct {
151  bool read;
152  bool write;
153  bool recover;
154  } update_domain_sdos_{};
155  struct {
156  double error_counter;
157  double no_error_counter;
158  double dt_sec;
159  double error_timeout;
160  double no_error_timeout;
161  } timing_{0, 0, 0.001, 1.0, 1.0};
162 };
163 
164 } // namespace ecat
165 
166 } // namespace mcx
167 
168 #endif // MOTORCORTEX_LIB_ECAT_DOMAIN_H
mcx::ecat::EcatPdoInfo
Definition: ecat_xmltypes.h:113
mcx::parameter_server::Parameter
Definition: ps_parameter.h:45
mcx::ecat::Domain
Definition: ecat_domain.h:31
mcx::ecat::EcatDomainConfig
Definition: ecat_xmltypes.h:255
mcx::ecat::Module
Definition: ecat_module.h:18
mcx::ecat::DataTypeDesc
Definition: ecat_datatype.h:24
mcx::ecat::EcatSdoConfig
Definition: ecat_xmltypes.h:197
mcx::ecat::DomainBase
Definition: ecat_domainbase.h:24
mcx::parameter_server::ParamHandle
Definition: ps_handlers.h:43