EtherCAT Client Library Code 0.0.1
EtherCAT Client library that uses different communication protocols with EtherCAT Master server.
Loading...
Searching...
No Matches
ec_types.h
1#ifndef EC_TYPES_H
2#define EC_TYPES_H
3
4#include <map>
5#include <vector>
6#include <tuple>
7
8#include <esc_info.h>
9#include "mechanism/protobuf/motor/advrf/advrf_pdo.h"
10#include "mechanism/protobuf/motor/cia402/cia402_pdo.h"
11#include "mechanism/protobuf/imu/imu_pdo.h"
12#include "mechanism/protobuf/ft/ft_pdo.h"
13#include "mechanism/protobuf/pow/pow_pdo.h"
14#include "mechanism/protobuf/valve/valve_pdo.h"
15#include "mechanism/protobuf/pump/pump_pdo.h"
16#include "mechanism/protobuf/gripper/gripper_pdo.h"
17
18namespace iit::ecat::detail {
19 inline std::map<uint32_t, std::string> build_ec_map(const std::string& device_type){
20 std::map<uint32_t, std::string> result;
21
22 for (const auto& [key, value] : iit::ecat::esc_type_map) {
23 if (value.find(device_type) != std::string::npos) {
24 std::string device = value;
25
26 if (device == "Motor")
27 device = "ADVRF_Motor";
28
29 result[key] = device;
30 }
31 }
32 return result;
33 }
34} // namespace iit::ecat::detail
35
36inline const std::map<uint32_t,std::string>& ec_motors(){
37 static const auto instance = iit::ecat::detail::build_ec_map("Motor");
38 return instance;
39}
40
41inline const std::map<uint32_t,std::string>& ec_valves(){
42 static const auto instance = iit::ecat::detail::build_ec_map("Valve");
43 return instance;
44}
45
46inline const std::map<uint32_t,std::string>& ec_pumps(){
47 static const auto instance = iit::ecat::detail::build_ec_map("Hpu");
48 return instance;
49}
50
51inline const std::map<uint32_t,std::string>& ec_grippers(){
52 static const auto instance = iit::ecat::detail::build_ec_map("Gripper");
53 return instance;
54}
55
56using MotorStatusMap = std::map<int32_t, MotorPdoRx::pdo_t>;
57using MotorStatus= std::vector<MotorPdoRx::pdo_t>;
58using MotorReferenceMap= std::map<int32_t, MotorPdoTx::pdo_t>;
59
60using PwrStatusMap= std::map<int32_t, PowPdoRx::pdo_t>;
61using PwrStatus= std::vector<PowPdoRx::pdo_t>;
62using PwrReferenceMap= std::map<int32_t, PowPdoTx::pdo_t>;
63
64using FtStatusMap= std::map<int32_t, FtPdoRx::pdo_t>;
65using FtStatus= std::vector<FtPdoRx::pdo_t>;
66
67using ImuStatusMap= std::map<int32_t, ImuPdoRx::pdo_t>;
68using ImuStatus= std::vector<ImuPdoRx::pdo_t>;
69using ImuReferenceMap= std::map<int32_t, ImuPdoTx::pdo_t>;
70
71using ValveStatusMap= std::map<int32_t, ValvePdoRx::pdo_t>;
72using ValveStatus= std::vector<ValvePdoRx::pdo_t>;
73using ValveReferenceMap= std::map<int32_t, ValvePdoTx::pdo_t>;
74
75using PumpStatusMap = std::map<int32_t, PumpPdoRx::pdo_t>;
76using PumpStatus= std::vector<PumpPdoRx::pdo_t>;
77using PumpReferenceMap= std::map<int32_t, PumpPdoTx::pdo_t >;
78
79using GripperStatusMap= std::map<int32_t, GripperPdoRx::pdo_t>;
80using GripperStatus= std::vector<GripperPdoRx::pdo_t>;
81using GripperReferenceMap= std::map<int32_t, GripperPdoTx::pdo_t >;
82
83// DevicesStarT
84using DST = std::vector<std::tuple<int32_t, int32_t, std::vector<float>>>;
85
86// Sdo commands
87using RD_SDO = std::vector<std::string>;
88using WR_SDO = std::vector<std::tuple<std::string, std::string>>;
89using RR_SDOS = std::map<std::string, std::string>;
90using SRD_SDO = std::map<uint32_t, RR_SDOS>;
91
92// PdoAuxCommand
93using PAC = std::vector<std::tuple<int32_t, int32_t>>;
94
95// SlaveSInfo
96using SSI = std::vector<std::tuple<int32_t, int32_t, int32_t>>;
97
98enum class RefFlags : uint32_t
99{
100 FLAG_NONE = 0x0, //
101 FLAG_MULTI_REF = 1 << 0,
102 FLAG_LAST_REF = 1 << 1,
103};
104
105enum ClientCmdType { STOP, START};
106
107enum class ClientStatusEnum : uint32_t
108{
109 ERROR = 0, // Error
110 NOT_ALIVE = 1 << 0,
111 IDLE = 1 << 1,
112 CONNECTED = 1 << 2,
113 DEVICES_MAPPED = 1 << 3,
114 DEVICES_STARTED = 1 << 4,
115 DEVICES_CTRL = 1 << 5,
116 DEVICES_STOPPED = 1 << 6,
117};
118
119
120template <typename E>
121constexpr auto to_underlying_enum(E e) noexcept
122{
123 return static_cast<std::underlying_type_t<E>>(e);
124}
125
126
127enum class PdoAuxCmdTypeEnum : int32_t
128{
129 BRAKE_RELEASE = 1,
130 BRAKE_ENGAGE,
131 LED_ON,
132 LED_OFF,
133};
134
135enum DeviceCtrlType{
136 MOTOR = 0,
137 VALVE = 1,
138 PUMP = 2,
139 GRIPPER = 3,
140 IMU = 4,
141 POW = 5,
142};
143
144template <typename T>
145class SpscValue {
146 static_assert(ATOMIC_INT_LOCK_FREE == 2,
147 "Target must provide always-lock-free integer atomics");
148
149public:
150 SpscValue() = default;
151
152 explicit SpscValue(std::size_t size)
153 {
154 resize(size);
155 }
156
157 // Initialization/reconfiguration only.
158 // Both producer and consumer must be stopped.
159 // Discards any pending update.
160 void resize(std::size_t size)
161 {
162 for (auto& buffer : _buffers)
163 buffer.resize(size);
164
165 _writer = 0;
166 _reader = 1;
167 _middle.store(2, std::memory_order_relaxed);
168 }
169
170 // Producer thread only.
171 void write(const T& value)
172 {
173 _buffers[_writer] = value;
174
175 const unsigned previous = _middle.exchange(
176 _writer | dirty_bit, std::memory_order_acq_rel);
177
178 _writer = previous & index_mask;
179 }
180
181 // Consumer thread only.
182 template <typename Func>
183 bool consume(Func&& callback)
184 {
185 if (!(_middle.load(std::memory_order_relaxed) & dirty_bit))
186 return false;
187
188 const unsigned previous = _middle.exchange(
189 _reader, std::memory_order_acq_rel);
190
191 _reader = previous & index_mask;
192
193 const T& value = _buffers[_reader];
194 callback(value);
195
196 return true;
197 }
198
199 // Consumer thread only.
200 bool read(T& value)
201 {
202 if (!(_middle.load(std::memory_order_relaxed) & dirty_bit))
203 return false;
204
205 const unsigned previous = _middle.exchange(
206 _reader, std::memory_order_acq_rel);
207
208 _reader = previous & index_mask;
209 value = _buffers[_reader];
210 return true;
211 }
212
213private:
214 static constexpr unsigned dirty_bit = 4;
215 static constexpr unsigned index_mask = 3;
216
217 T _buffers[3]{};
218
219 unsigned _writer = 0;
220 unsigned _reader = 1;
221 std::atomic<unsigned> _middle{2};
222};
223
224
225#endif // EC_TYPES_H