EtherCATDataTypes.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*!
4  * Copied from https://gitlab.com/h2t/student-projects/pse-ws2021/etherkitten/-/tree/master/reader/src/etherkitten/reader
5  *
6  * !!!
7  * Needs correct license message!!!!
8  * !!!
9  */
10 
11 #include <bitset>
12 #include <cstdint>
13 #include <variant>
14 #include <vector>
15 
17 {
18  /**
19  * @enum EtherCATDataTypeEnum
20  * @ingroup Namespace-datatypes
21  *
22  * \brief Holds all the types we support via EtherCAT in a type-agnostic enum.
23  *
24  * The enum values correspond to the values defined in the standard.
25  * We use these to identify which types different DataObjects have.
26  */
28  {
29  BOOLEAN = 1,
30  INTEGER8 = 2,
31  INTEGER16 = 3,
32  INTEGER32 = 4,
33  UNSIGNED8 = 5,
34  UNSIGNED16 = 6,
35  UNSIGNED32 = 7,
36  REAL32 = 8,
37  VISIBLE_STRING = 9,
38  OCTET_STRING = 10,
39  UNICODE_STRING = 11,
40  TIME_OF_DAY = 12,
41  TIME_DIFFERENCE = 13,
42  INTEGER24 = 16,
43  REAL64 = 17,
44  INTEGER64 = 21,
45  UNSIGNED24 = 22,
46  UNSIGNED64 = 27,
47  BYTE = 30,
48  BIT1 = 48,
49  BIT2 = 49,
50  BIT3 = 50,
51  BIT4 = 51,
52  BIT5 = 52,
53  BIT6 = 53,
54  BIT7 = 54,
55  BIT8 = 55,
56  };
57 
58  /*!
59  * \brief Holds all the types we support via EtherCAT as their C++ equivalents
60  * along with some tools to handle them.
61  *
62  * We use the types defined here to actually move the data around,
63  * and the templates to perform type-agnostic operations.
64  */
65  namespace EtherCATDataType
66  {
67  using BOOLEAN = bool;
68  using INTEGER8 = int8_t;
69  using INTEGER16 = int16_t;
70  using INTEGER32 = int32_t;
71  using UNSIGNED8 = uint8_t;
72  using UNSIGNED16 = uint16_t;
73  using UNSIGNED32 = uint32_t;
74  using REAL32 = float;
75  using VISIBLE_STRING = std::string;
76  using OCTET_STRING = std::vector<uint8_t>;
77  using UNICODE_STRING = std::string;
78  using TIME_OF_DAY = std::bitset<48>; // NOLINT
79  using TIME_DIFFERENCE = uint64_t;
80  using INTEGER24 = std::bitset<24>; // NOLINT
81  using REAL64 = double;
82  using INTEGER64 = int64_t;
83  using UNSIGNED24 = std::bitset<24>; // NOLINT
84  using UNSIGNED64 = uint64_t;
85  using BYTE = uint8_t;
86  using BIT1 = std::bitset<1>;
87  using BIT2 = std::bitset<2>;
88  using BIT3 = std::bitset<3>;
89  using BIT4 = std::bitset<4>;
90  using BIT5 = std::bitset<5>; // NOLINT
91  using BIT6 = std::bitset<6>; // NOLINT
92  using BIT7 = std::bitset<7>; // NOLINT
93  using BIT8 = std::bitset<8>; // NOLINT
94 
95  /*!
96  * \brief Defines the length of the various EtherCATDataTypes in bits.
97  *
98  * If a type does not have a well-defined length, `bitLength<T> == -1`.
99  *
100  * Note that `bitLength<T> != sizeof(T) * 8` in general.
101  * \tparam T the EtherCATDataType to get the length of
102  */
103  template <typename T>
104  constexpr int bitLength = -1;
105  template <>
106  inline constexpr int bitLength<BOOLEAN> = 8;
107  template <>
108  inline constexpr int bitLength<INTEGER8> = 8;
109  template <>
110  inline constexpr int bitLength<INTEGER16> = 16;
111  template <>
112  inline constexpr int bitLength<INTEGER32> = 32;
113  template <>
114  inline constexpr int bitLength<UNSIGNED8> = 8;
115  template <>
116  inline constexpr int bitLength<UNSIGNED16> = 16;
117  template <>
118  inline constexpr int bitLength<UNSIGNED32> = 32;
119  template <>
120  inline constexpr int bitLength<REAL32> = 32;
121  template <>
122  inline constexpr int bitLength<VISIBLE_STRING> = -1;
123  template <>
124  inline constexpr int bitLength<OCTET_STRING> = -1;
125  template <>
126  inline constexpr int bitLength<TIME_OF_DAY> = 48;
127  template <>
128  inline constexpr int bitLength<INTEGER24> = 24; // NOLINT
129  template <>
130  inline constexpr int bitLength<REAL64> = 64;
131  template <>
132  inline constexpr int bitLength<INTEGER64> = 64;
133  template <>
134  inline constexpr int bitLength<UNSIGNED64> = 64;
135  template <>
136  inline constexpr int bitLength<BIT1> = 1;
137  template <>
138  inline constexpr int bitLength<BIT2> = 2;
139  template <>
140  inline constexpr int bitLength<BIT3> = 3;
141  template <>
142  inline constexpr int bitLength<BIT4> = 4;
143  template <>
144  inline constexpr int bitLength<BIT5> = 5; // NOLINT
145  template <>
146  inline constexpr int bitLength<BIT6> = 6; // NOLINT
147  template <>
148  inline constexpr int bitLength<BIT7> = 7; // NOLINT
149  template <>
150  inline constexpr int bitLength<BIT8> = 8; // NOLINT
151  } // namespace EtherCATDataType
152 
153 
154  /*!
155  * \brief Maps from EtherCATDataTypeEnum to EtherCATDataType.
156  * \tparam E the EtherCATDataTypeEnum to translate to an EtherCATDataType
157  */
158  template <EtherCATDataTypeEnum E>
159  struct TypeMap
160  {
161  };
162 
163  // clang-format off
164  template<>
166  template<>
168  template<>
170  template<>
172  template<>
174  template<>
176  template<>
178  template<>
180  template<>
182  template<>
184  template<>
186  template<>
188  template<>
190  template<>
192  template<>
194  template<>
196  template<>
198  template<>
200  template<>
202  template<>
204  template<>
206  template<>
208  template<>
210  template<>
212  template<>
214  template<>
216  template<>
218  // clang-format on
219 } // namespace armarx::control::ethercat::datatypes
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::INTEGER64
@ INTEGER64
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< UNSIGNED8 >
constexpr int bitLength< UNSIGNED8 >
Definition: EtherCATDataTypes.h:114
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BOOLEAN >
constexpr int bitLength< BOOLEAN >
Definition: EtherCATDataTypes.h:106
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT5 >
constexpr int bitLength< BIT5 >
Definition: EtherCATDataTypes.h:144
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BOOLEAN >::type
EtherCATDataType::BOOLEAN type
Definition: EtherCATDataTypes.h:165
armarx::control::ethercat::datatypes::EtherCATDataType::INTEGER64
int64_t INTEGER64
Definition: EtherCATDataTypes.h:82
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::UNSIGNED64
@ UNSIGNED64
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< INTEGER64 >
constexpr int bitLength< INTEGER64 >
Definition: EtherCATDataTypes.h:132
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT2 >::type
EtherCATDataType::BIT2 type
Definition: EtherCATDataTypes.h:205
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::INTEGER16 >::type
EtherCATDataType::INTEGER16 type
Definition: EtherCATDataTypes.h:169
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT8
@ BIT8
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::UNSIGNED16 >::type
EtherCATDataType::UNSIGNED16 type
Definition: EtherCATDataTypes.h:175
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum
EtherCATDataTypeEnum
Holds all the types we support via EtherCAT in a type-agnostic enum.
Definition: EtherCATDataTypes.h:27
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< UNSIGNED64 >
constexpr int bitLength< UNSIGNED64 >
Definition: EtherCATDataTypes.h:134
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::INTEGER32
@ INTEGER32
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::REAL64 >::type
EtherCATDataType::REAL64 type
Definition: EtherCATDataTypes.h:195
armarx::control::ethercat::datatypes::TypeMap
Maps from EtherCATDataTypeEnum to EtherCATDataType.
Definition: EtherCATDataTypes.h:159
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< VISIBLE_STRING >
constexpr int bitLength< VISIBLE_STRING >
Definition: EtherCATDataTypes.h:122
armarx::control::ethercat::datatypes::EtherCATDataType::UNSIGNED16
uint16_t UNSIGNED16
Definition: EtherCATDataTypes.h:72
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::TIME_OF_DAY
@ TIME_OF_DAY
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< REAL64 >
constexpr int bitLength< REAL64 >
Definition: EtherCATDataTypes.h:130
armarx::control::ethercat::datatypes::EtherCATDataType::UNSIGNED8
uint8_t UNSIGNED8
Definition: EtherCATDataTypes.h:71
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::UNSIGNED32 >::type
EtherCATDataType::UNSIGNED32 type
Definition: EtherCATDataTypes.h:177
armarx::control::ethercat::datatypes::EtherCATDataType::UNSIGNED24
std::bitset< 24 > UNSIGNED24
Definition: EtherCATDataTypes.h:83
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT8 >::type
EtherCATDataType::BIT8 type
Definition: EtherCATDataTypes.h:217
armarx::control::ethercat::datatypes::EtherCATDataType::BIT7
std::bitset< 7 > BIT7
Definition: EtherCATDataTypes.h:92
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::TIME_DIFFERENCE >::type
EtherCATDataType::TIME_DIFFERENCE type
Definition: EtherCATDataTypes.h:191
armarx::control::ethercat::datatypes::EtherCATDataType::TIME_OF_DAY
std::bitset< 48 > TIME_OF_DAY
Definition: EtherCATDataTypes.h:78
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< UNSIGNED16 >
constexpr int bitLength< UNSIGNED16 >
Definition: EtherCATDataTypes.h:116
armarx::control::ethercat::datatypes::EtherCATDataType::VISIBLE_STRING
std::string VISIBLE_STRING
Definition: EtherCATDataTypes.h:75
armarx::control::ethercat::datatypes::EtherCATDataType::BIT8
std::bitset< 8 > BIT8
Definition: EtherCATDataTypes.h:93
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT5
@ BIT5
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::OCTET_STRING >::type
EtherCATDataType::OCTET_STRING type
Definition: EtherCATDataTypes.h:185
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT2 >
constexpr int bitLength< BIT2 >
Definition: EtherCATDataTypes.h:138
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT7
@ BIT7
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::OCTET_STRING
@ OCTET_STRING
armarx::control::ethercat::datatypes::EtherCATDataType::OCTET_STRING
std::vector< uint8_t > OCTET_STRING
Definition: EtherCATDataTypes.h:76
armarx::control::ethercat::datatypes::EtherCATDataType::BIT2
std::bitset< 2 > BIT2
Definition: EtherCATDataTypes.h:87
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT6 >::type
EtherCATDataType::BIT6 type
Definition: EtherCATDataTypes.h:213
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::UNSIGNED24
@ UNSIGNED24
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::INTEGER8 >::type
EtherCATDataType::INTEGER8 type
Definition: EtherCATDataTypes.h:167
armarx::control::ethercat::datatypes::EtherCATDataType::INTEGER16
int16_t INTEGER16
Definition: EtherCATDataTypes.h:69
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::UNSIGNED8
@ UNSIGNED8
armarx::control::ethercat::datatypes::EtherCATDataType::UNSIGNED64
uint64_t UNSIGNED64
Definition: EtherCATDataTypes.h:84
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT6
@ BIT6
armarx::control::ethercat::datatypes::EtherCATDataType::BIT3
std::bitset< 3 > BIT3
Definition: EtherCATDataTypes.h:88
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::UNSIGNED32
@ UNSIGNED32
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT7 >::type
EtherCATDataType::BIT7 type
Definition: EtherCATDataTypes.h:215
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::UNSIGNED64 >::type
EtherCATDataType::UNSIGNED64 type
Definition: EtherCATDataTypes.h:179
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT6 >
constexpr int bitLength< BIT6 >
Definition: EtherCATDataTypes.h:146
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::UNSIGNED24 >::type
EtherCATDataType::UNSIGNED24 type
Definition: EtherCATDataTypes.h:199
armarx::control::ethercat::datatypes::EtherCATDataType::BIT6
std::bitset< 6 > BIT6
Definition: EtherCATDataTypes.h:91
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::INTEGER32 >::type
EtherCATDataType::INTEGER32 type
Definition: EtherCATDataTypes.h:171
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< REAL32 >
constexpr int bitLength< REAL32 >
Definition: EtherCATDataTypes.h:120
armarx::control::ethercat::datatypes::EtherCATDataType::UNICODE_STRING
std::string UNICODE_STRING
Definition: EtherCATDataTypes.h:77
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< OCTET_STRING >
constexpr int bitLength< OCTET_STRING >
Definition: EtherCATDataTypes.h:124
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BOOLEAN
@ BOOLEAN
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT4
@ BIT4
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::REAL64
@ REAL64
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< INTEGER32 >
constexpr int bitLength< INTEGER32 >
Definition: EtherCATDataTypes.h:112
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT3
@ BIT3
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BYTE >::type
EtherCATDataType::BYTE type
Definition: EtherCATDataTypes.h:201
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::REAL32
@ REAL32
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT1 >
constexpr int bitLength< BIT1 >
Definition: EtherCATDataTypes.h:136
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::UNICODE_STRING >::type
EtherCATDataType::UNICODE_STRING type
Definition: EtherCATDataTypes.h:187
armarx::control::ethercat::datatypes::EtherCATDataType::TIME_DIFFERENCE
uint64_t TIME_DIFFERENCE
Definition: EtherCATDataTypes.h:79
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::UNSIGNED16
@ UNSIGNED16
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT5 >::type
EtherCATDataType::BIT5 type
Definition: EtherCATDataTypes.h:211
armarx::control::ethercat::datatypes::EtherCATDataType::INTEGER8
int8_t INTEGER8
Definition: EtherCATDataTypes.h:68
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::UNSIGNED8 >::type
EtherCATDataType::UNSIGNED8 type
Definition: EtherCATDataTypes.h:173
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::TIME_DIFFERENCE
@ TIME_DIFFERENCE
armarx::control::ethercat::datatypes::EtherCATDataType::UNSIGNED32
uint32_t UNSIGNED32
Definition: EtherCATDataTypes.h:73
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT4 >::type
EtherCATDataType::BIT4 type
Definition: EtherCATDataTypes.h:209
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< UNSIGNED32 >
constexpr int bitLength< UNSIGNED32 >
Definition: EtherCATDataTypes.h:118
armarx::control::ethercat::datatypes::EtherCATDataType::INTEGER32
int32_t INTEGER32
Definition: EtherCATDataTypes.h:70
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::REAL32 >::type
EtherCATDataType::REAL32 type
Definition: EtherCATDataTypes.h:181
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::INTEGER64 >::type
EtherCATDataType::INTEGER64 type
Definition: EtherCATDataTypes.h:197
armarx::control::ethercat::datatypes::EtherCATDataType::BYTE
uint8_t BYTE
Definition: EtherCATDataTypes.h:85
armarx::control::ethercat::datatypes::EtherCATDataType::BIT4
std::bitset< 4 > BIT4
Definition: EtherCATDataTypes.h:89
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::INTEGER8
@ INTEGER8
float
#define float
Definition: 16_Level.h:22
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< INTEGER24 >
constexpr int bitLength< INTEGER24 >
Definition: EtherCATDataTypes.h:128
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::TIME_OF_DAY >::type
EtherCATDataType::TIME_OF_DAY type
Definition: EtherCATDataTypes.h:189
armarx::control::ethercat::datatypes
Definition: EtherCATDataTypes.h:16
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BYTE
@ BYTE
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength
constexpr int bitLength
Defines the length of the various EtherCATDataTypes in bits.
Definition: EtherCATDataTypes.h:104
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::UNICODE_STRING
@ UNICODE_STRING
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::INTEGER24 >::type
EtherCATDataType::INTEGER24 type
Definition: EtherCATDataTypes.h:193
armarx::control::ethercat::datatypes::EtherCATDataType::INTEGER24
std::bitset< 24 > INTEGER24
Definition: EtherCATDataTypes.h:80
armarx::control::ethercat::datatypes::EtherCATDataType::BIT1
std::bitset< 1 > BIT1
Definition: EtherCATDataTypes.h:86
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT3 >::type
EtherCATDataType::BIT3 type
Definition: EtherCATDataTypes.h:207
armarx::control::ethercat::datatypes::EtherCATDataType::BOOLEAN
bool BOOLEAN
Definition: EtherCATDataTypes.h:67
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::VISIBLE_STRING >::type
EtherCATDataType::VISIBLE_STRING type
Definition: EtherCATDataTypes.h:183
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< INTEGER16 >
constexpr int bitLength< INTEGER16 >
Definition: EtherCATDataTypes.h:110
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT7 >
constexpr int bitLength< BIT7 >
Definition: EtherCATDataTypes.h:148
armarx::control::ethercat::datatypes::EtherCATDataType::REAL32
float REAL32
Definition: EtherCATDataTypes.h:74
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT2
@ BIT2
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT3 >
constexpr int bitLength< BIT3 >
Definition: EtherCATDataTypes.h:140
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::INTEGER16
@ INTEGER16
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< TIME_OF_DAY >
constexpr int bitLength< TIME_OF_DAY >
Definition: EtherCATDataTypes.h:126
armarx::control::ethercat::datatypes::EtherCATDataType::BIT5
std::bitset< 5 > BIT5
Definition: EtherCATDataTypes.h:90
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::BIT1
@ BIT1
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT4 >
constexpr int bitLength< BIT4 >
Definition: EtherCATDataTypes.h:142
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::VISIBLE_STRING
@ VISIBLE_STRING
armarx::control::ethercat::datatypes::EtherCATDataTypeEnum::INTEGER24
@ INTEGER24
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< BIT8 >
constexpr int bitLength< BIT8 >
Definition: EtherCATDataTypes.h:150
armarx::control::ethercat::datatypes::EtherCATDataType::REAL64
double REAL64
Definition: EtherCATDataTypes.h:81
armarx::control::ethercat::datatypes::EtherCATDataType::bitLength< INTEGER8 >
constexpr int bitLength< INTEGER8 >
Definition: EtherCATDataTypes.h:108
armarx::control::ethercat::datatypes::TypeMap< EtherCATDataTypeEnum::BIT1 >::type
EtherCATDataType::BIT1 type
Definition: EtherCATDataTypes.h:203