ethercat_driver_ros2  main
C++ ROS test
ec_sdo_manager.hpp
Go to the documentation of this file.
1 // Copyright 2023 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 // Author: Maciej Bednarczyk (macbednarczyk@gmail.com)
16 
17 #ifndef ETHERCAT_INTERFACE__EC_SDO_MANAGER_HPP_
18 #define ETHERCAT_INTERFACE__EC_SDO_MANAGER_HPP_
19 
20 #include <ecrt.h>
21 #include <string>
22 #include <vector>
23 #include <limits>
24 
25 #include "yaml-cpp/yaml.h"
26 
27 namespace ethercat_interface
28 {
29 
31 {
32 public:
35 
36  void buffer_write(uint8_t * buffer)
37  {
38  if (data_type == "uint8") {
39  EC_WRITE_U8(buffer, static_cast<uint8_t>(data));
40  } else if (data_type == "int8") {
41  EC_WRITE_S8(buffer, static_cast<int8_t>(data));
42  } else if (data_type == "uint16") {
43  EC_WRITE_U16(buffer, static_cast<uint16_t>(data));
44  } else if (data_type == "int16") {
45  EC_WRITE_S16(buffer, static_cast<int16_t>(data));
46  } else if (data_type == "uint32") {
47  EC_WRITE_U32(buffer, static_cast<uint32_t>(data));
48  } else if (data_type == "int32") {
49  EC_WRITE_S32(buffer, static_cast<int32_t>(data));
50  } else if (data_type == "uint64") {
51  EC_WRITE_U64(buffer, static_cast<uint64_t>(data));
52  } else if (data_type == "int64") {
53  EC_WRITE_S64(buffer, static_cast<int64_t>(data));
54  }
55  }
56 
57  bool load_from_config(YAML::Node sdo_config)
58  {
59  // index
60  if (sdo_config["index"]) {
61  index = sdo_config["index"].as<uint16_t>();
62  } else {
63  std::cerr << "missing sdo index info" << std::endl;
64  return false;
65  }
66  // sub_index
67  if (sdo_config["sub_index"]) {
68  sub_index = sdo_config["sub_index"].as<uint8_t>();
69  } else {
70  std::cerr << "sdo " << index << ": missing sdo info" << std::endl;
71  return false;
72  }
73  // data type
74  if (sdo_config["type"]) {
75  data_type = sdo_config["type"].as<std::string>();
76  } else {
77  std::cerr << "sdo " << index << ": missing sdo data type info" << std::endl;
78  return false;
79  }
80  // value
81  if (sdo_config["value"]) {
82  data = sdo_config["value"].as<int>();
83  } else {
84  std::cerr << "sdo " << index << ": missing sdo value" << std::endl;
85  return false;
86  }
87 
88  return true;
89  }
90 
91  size_t data_size()
92  {
93  return type2bytes(data_type);
94  }
95 
96  uint16_t index;
97  uint8_t sub_index;
98  std::string data_type;
99  int data;
100 
101 private:
102  size_t type2bytes(std::string type)
103  {
104  if (type == "int8" || type == "uint8") {
105  return 1;
106  } else if (type == "int16" || type == "uint16") {
107  return 2;
108  } else if (type == "int32" || type == "uint32") {
109  return 4;
110  } else if (type == "int64" || type == "uint64") {
111  return 8;
112  }
113  }
114 };
115 
116 } // namespace ethercat_interface
117 #endif // ETHERCAT_INTERFACE__EC_SDO_MANAGER_HPP_
Definition: ec_sdo_manager.hpp:31
void buffer_write(uint8_t *buffer)
Definition: ec_sdo_manager.hpp:36
bool load_from_config(YAML::Node sdo_config)
Definition: ec_sdo_manager.hpp:57
uint8_t sub_index
Definition: ec_sdo_manager.hpp:97
size_t data_size()
Definition: ec_sdo_manager.hpp:91
int data
Definition: ec_sdo_manager.hpp:99
~SdoConfigEntry()
Definition: ec_sdo_manager.hpp:34
uint16_t index
Definition: ec_sdo_manager.hpp:96
std::string data_type
Definition: ec_sdo_manager.hpp:98
SdoConfigEntry()
Definition: ec_sdo_manager.hpp:33
Definition: ec_master.hpp:29