Motion Master
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cmath>
5#include <deque>
6#include <future>
7#include <list>
8#include <nlohmann/json.hpp>
9#include <numeric>
10#include <semver.hpp>
11#include <string>
12#include <thread>
13#include <utility>
14
15#include "cia402_drive.h"
16#include "motion-master.pb.h"
17#include "motion_master_error.h"
18#include "notifier.h"
20#include "virtual_device.h"
21
22const char* const HARDWARE_DESCRIPTION_FILENAME = ".hardware_description";
23const char* const STACK_INFO_FILENAME = "stack_info.json";
24const char* const STACK_IMAGE_FILENAME = "stack_image.svg";
25const char* const ESI_FILENAME = "SOMANET_CiA_402.xml";
26const char* const CONFIG_FILENAME = "config.csv";
27const char* const PLANT_MODEL_FILENAME = "plant_model.csv";
28const char* const VARIANT_FILENAME = ".variant";
29
30struct PlantModel {
31 std::vector<double> numerator;
32 std::vector<double> denominator;
33 double start_frequency = 0.0; // Hz
34 double end_frequency = 0.0; // Hz
35 uint16_t torque_amplitude = 0; // ‰ of rated torque
36 double duration_seconds = 0.0; // seconds
37 uint8_t signal_type = 0; // 0 - logarithmic, 1 - linear
38 nlohmann::json to_json() {
39 nlohmann::json j;
40 j["numerator"] = numerator;
41 j["denominator"] = denominator;
42 j["start_frequency"] = start_frequency;
43 j["end_frequency"] = end_frequency;
44 j["torque_amplitude"] = torque_amplitude;
45 j["duration_seconds"] = duration_seconds;
46 j["signal_type"] = signal_type;
47 return j;
48 }
49 std::string bode_file_content;
50};
51
52enum class ControllerType { kPiP, kPpi };
53
55
56// The value is carried on the wire as the external_circulo_type field of
57// StartCirculoEncoderConfiguration, so members are only ever appended.
58enum class CirculoType {
64 // 16 pole pairs, 18 singleturn bits. No Circulo carries this ring; it is
65 // named after the resolution because there is no product name to borrow.
67};
68
69// The value is carried on the wire as the external_encoder_type field of
70// StartCirculoEncoderNarrowAngleCalibrationProcedure, so members are only ever
71// appended.
80
82
83#define JSON_TO_DOUBLE(jx) jx.is_number() ? static_cast<double>(jx) : 0.0
84
85// clang-format off
87 uint8_t vel_lp_type = 1; // 0 - disabled, 1 - 1st order, 2 - 2nd order (0x2021, 1)
88 uint32_t vel_lp_fc = 300; // [Hz] (0x2021, 2)
89 uint8_t pos_lp_type = 1; // 0 - disabled, 1 - 1st order, 2 - 2nd order (0x2022, 1)
90 uint32_t pos_lp_fc = 300; // [Hz] (0x2022, 2)
91 bool notch_en = true; // Enabled / disabled (0x2023, 1)
92 uint16_t notch_fw = 10; // [Hz] (0x2023, 3)
93 uint16_t notch_fc = 1000; // [Hz] (0x2023, 2)
94 int16_t vel_ff_gain = 0; // Permill, 0 - disabled (0x2015, 1)
95 uint16_t vel_ff_fc = 1000; // [Hz] (0x2015, 2)
96 int32_t L = 200; // Inductance, [μH] (0x2003, 4)
97 int32_t R = 300000; // Resistance, [μOhm] (0x2003, 3)
98 uint32_t vdc = 48000; // DC voltage, [mV] (0x6079, 0)
99 double current_ratio = 16384; // ADC conversion factor, [mA / inc] (from BSP file)
100 double tc_kp = 2500; // kp gain from FW v4.x.x, [-] (0x2010, 1)
101 double tc_ki = 40000; // ki gain from FW v4.x.x, [-] (0x2010, 2)
102 uint8_t switch_F = 0; // Switching frequency: 0 - 16kHz, 1 - 32kHz, 2 - 64kHz (0x2010, 9)
103 uint16_t tc_st = 1000; // Torque controller settling time, FW v5.x.x, [us] (0x2010, 10)
104 uint16_t tc_damping = 2000; // Torque controller damping ratio, FW v5.x.x, [permill] (0x2010, 11)
105 std::string software_version = "v4.4.1"; // FW version
106 unsigned int rated_torque; // Motor rated torque, [mNm]
107 double max_vel_noise; // Max value of the pre-recorded encoder noise, [RPM]
108 nlohmann::json to_json() {
109 nlohmann::json j;
110 j["vel_lp_type"] = vel_lp_type;
111 j["vel_lp_fc"] = vel_lp_fc;
112 j["pos_lp_type"] = pos_lp_type;
113 j["pos_lp_fc"] = pos_lp_fc;
114 j["notch_en"] = notch_en;
115 j["notch_fw"] = notch_fw;
116 j["notch_fc"] = notch_fc;
117 j["vel_ff_gain"] = vel_ff_gain;
118 j["vel_ff_fc"] = vel_ff_fc;
119 j["L"] = L;
120 j["R"] = R;
121 j["vdc"] = vdc;
122 j["current_ratio"] = current_ratio;
123 j["tc_kp"] = tc_kp;
124 j["tc_ki"] = tc_ki;
125 j["switch_F"] = switch_F;
126 j["tc_st"] = tc_st;
127 j["tc_damping"] = tc_damping;
128 j["software_version"] = software_version;
129 j["rated_torque"] = rated_torque;
130 j["max_vel_noise"] = max_vel_noise;
131 return j;
132 }
134// clang-format on
135
136typedef struct SystemParametersClass {
137 double time_step = 0.001;
138 uint32_t nominal_torque = 0;
139 uint32_t max_torque_mnm_ = 0;
142
143typedef struct TuningParametersClass {
144 // Chirp parameters
145 double duration_seconds = 5.0;
146 uint16_t torque_amplitude = 500;
147 double start_frequency = 3.0;
148 double end_frequency = 60.0;
149 double cutoff_frequency = 30.0;
150
151 // Position auto-tuning parameters
153 double settling_time = 0.2;
154 double position_damping = 2.0;
155 int16_t alpha_mult = 1;
156 uint8_t order = 4;
157
158 // Velocity auto-tuning parameters
160 float velocity_damping = 0.7f;
161
162 // Tuning sharpness limiter (nominal torque * warn_torque_const)
163 double warn_torque_const = 3.0;
164
165 // Position auto-tuning optimization boundaries
166 double lb = 2.0;
167 double ub = 314.0;
169
171
177
178const std::map<MotionMasterWarning, std::string>
180 {WAR_NONE, "No warning!"},
182 "Slave profile: The amplitude is outside of the configured limits - "
183 "using a limit value as the new amplitude!"},
184 {WAR_INTERNAL_LIMIT_REACHED, "Internal limit reached!"}};
185
186const std::map<int32_t, std::string> slave_error_map_ = {
187 // Jasper firmware codes (jasper libs/error_handler/error_handler.h). They
188 // do not overlap the CiA 402 codes below.
189 {0x0100, "Overvoltage (hardware)"},
190 {0x0101, "Undervoltage (hardware)"},
191 {0x0102, "Overcurrent (hardware)"},
192 {0x0103, "Overtemperature (hardware)"},
193 {0x0108, "Encoder 1 not responding"},
194 {0x0109, "Encoder 1 internal error"},
195 {0x010A, "Encoder 1 internal warning"},
196 {0x010B, "Encoder 1 CRC error"},
197 {0x010C, "Encoder 1 interface setup failure"},
198 {0x010D, "Encoder 1 EEPROM error"},
199 {0x010E, "Encoder 1 multiturn error"},
200 {0x010F, "Encoder 1 nonius error"},
201 {0x0110, "Encoder 1 signal frequency out of range"},
202 {0x0111, "Encoder 1 signal amplitude too low"},
203 {0x0112, "Encoder 1 startup not complete"},
204 {0x0118, "Encoder 2 not responding"},
205 {0x0119, "Encoder 2 internal error"},
206 {0x011A, "Encoder 2 internal warning"},
207 {0x011B, "Encoder 2 CRC error"},
208 {0x011C, "Encoder 2 interface setup failure"},
209 {0x011D, "Encoder 2 EEPROM error"},
210 {0x011E, "Encoder 2 multiturn error"},
211 {0x011F, "Encoder 2 nonius error"},
212 {0x0120, "Encoder 2 signal frequency out of range"},
213 {0x0121, "Encoder 2 signal amplitude too low"},
214 {0x0122, "Encoder 2 startup not complete"},
215 {0x0123, "Open circuit: terminal A"},
216 {0x0124, "Open circuit: terminal B"},
217 {0x0125, "Open circuit: terminal C"},
218 {0x0126, "Open circuit: High FET in phase A"},
219 {0x0127, "Open circuit: Low FET in phase A"},
220 {0x0128, "Open circuit: High FET in phase B"},
221 {0x0129, "Open circuit: Low FET in phase B"},
222 {0x012A, "Open circuit: High FET in phase C"},
223 {0x012B, "Open circuit: Low FET in phase C"},
224 {0x012C, "Motor winding overload (I2t)"},
225 {0x012D, "DC bus overvoltage (protection limit)"},
226 {0x012E, "DC bus undervoltage (protection limit)"},
227 {0x012F, "Phase overcurrent (protection limit)"},
228 {0x0130, "Overcurrent in phase A (hardware)"},
229 {0x0131, "Overcurrent in phase B (hardware)"},
230 {0x0132, "Overcurrent in phase C (hardware)"},
231 {0x0133, "External scaled measurement out of range"},
232 {0x0134, "FET overtemperature"},
233 {0x0135, "Torque sensor not responding"},
234 {0x0136, "Torque sensor value out of range"},
235 {0x0137, "Integrated encoder internal error"},
236 {0x0138, "Integrated encoder CRC error"},
237 {0x0139, "Integrated encoder internal warning"},
238 {0x013A, "Integrated encoder not responding"},
239 {0x013B, "Power management IC not responding"},
240 {0x0200, "Communication timeout"},
241 {0x0201, "EtherCAT connection aborted"},
242 {0x0303, "Position following error"},
243 {0x0305, "Impedance parameters out of range and clamped"},
244 {0x0306, "Motor and load positions diverged"},
245 {0x0307, "Velocity following error"},
246 {0x0308, "Software position limit exceeded"},
247 {0x0309, "Joint torque mode without a torque sensor"},
248 {0x030A, "Joint torque impedance: unsupported sensor assignment"},
249 {0x030B,
250 "Virtual torque sensor needs velocity feedback on the motor encoder"},
251 {0x030C, "Commanded velocity unreachable"},
252 {0x030D, "Overspeed"},
253 {0x0501, "Internal watchdog timeout"},
254 {0x0503, "Restarted after an internal watchdog timeout"},
255 {0x0700, "STO input fault: the two STO inputs differ"},
256 {0x0701, "STO diagnostic fault while STO is engaged"},
257 {0x0702, "STO diagnostic fault while STO is released"},
258 {0x1001, "Torque control timing error"},
259 {0x1002, "Motion control timing error"},
260 {0x1003, "Phase order detection error"},
261 {0x1004, "Commutation offset detection error"},
262 {0x1005, "Commutation offset not valid"},
263 {0x1006, "Stored configuration is corrupt, defaults are in use"},
264 // CiA 402 and SOMANET firmware codes.
265 {0x2110, "Short circuit/earth leakage (input)"},
266 {0x2120, "Earth leakage (input)"},
267 {0x2121, "Earth leakage phase L1"},
268 {0x2122, "Earth leakage phase L2"},
269 {0x2123, "Earth leakage phase L3"},
270 {0x2130, "Short circuit (input)"},
271 {0x2131, "Short circuit phases L1-L2"},
272 {0x2132, "Short circuit phases L2-L3"},
273 {0x2133, "Short circuit phases L3-L1"},
274 {0x2211, "Internal current no.1"},
275 {0x2212, "Internal current no.2"},
276 {0x2213, "Over-current in ramp function"},
277 {0x2214, "Over-current in the sequence"},
278 {0x2220, "Continuous over current (device internal)"},
279 {0x2221, "Continuous over current no.1"},
280 {0x2222, "Continuous over current no.2"},
281 {0x2230, "Short circuit/earth leakage (device internal)"},
282 {0x2240, "Earth leakage (device internal)"},
283 {0x2250, "Short circuit (device internal)"},
284 {0x2310, "Continuous over current"},
285 {0x2311, "Continuous over current no.1"},
286 {0x2312, "Continuous over current no.2"},
287 {0x2320, "Short circuit/earth leakage (motor-side)"},
288 {0x2330, "Earth leakage (motor-side)"},
289 {0x2331, "Earth leakage phase U"},
290 {0x2332, "Earth leakage phase V"},
291 {0x2333, "Earth leakage phase W"},
292 {0x2340, "Short circuit (motor-side)"},
293 {0x2341, "Short circuit phases U-V"},
294 {0x2342, "Earth leakage phase V-W"},
295 {0x2343, "Earth leakage phase W-U"},
296 {0x2350, "Load level fault (I2t, thermal state)"},
297 {0x2351, "Load level warning (I2t, thermal state)"},
298 {0x3110, "Mains overvoltage"},
299 {0x3111, "Mains overvoltage phase L1"},
300 {0x3112, "Mains overvoltage phase L2"},
301 {0x3113, "Mains overvoltage phase L3"},
302 {0x3120, "Mains undervoltage"},
303 {0x3121, "Mains undervoltage phase L1"},
304 {0x3122, "Mains undervoltage phase L2"},
305 {0x3123, "Mains undervoltage phase L3"},
306 {0x3130, "Phase failure"},
307 {0x3131, "Phase failure L1"},
308 {0x3132, "Phase failure L2"},
309 {0x3133, "Phase failure L3"},
310 {0x3134, "Phase sequence"},
311 {0x3140, "Mains frequency"},
312 {0x3141, "Mains frequency too great"},
313 {0x3142, "Mains frequency too small"},
314 {0x3210, "DC link overvoltage"},
315 {0x3211, "Overvoltage no. 1"},
316 {0x3212, "Overvoltage no. 2"},
317 {0x3220, "DC link undervoltage"},
318 {0x3221, "Undervoltage no. 1"},
319 {0x3222, "Undervoltage no. 2"},
320 {0x3230, "Load error"},
321 {0x3310, "Output overvoltage"},
322 {0x3311, "Output overvoltage phase U"},
323 {0x3312, "Output overvoltage phase V"},
324 {0x3313, "Output overvoltage phase W"},
325 {0x3320, "Armature circuit"},
326 {0x3321, "Armature circuit interrupted"},
327 {0x3330, "Field circuit"},
328 {0x3331, "Field circuit interrupted"},
329 {0x4110, "Excess ambient temperature"},
330 {0x4120, "Too low ambient temperature"},
331 {0x4130, "Temperature supply air"},
332 {0x4140, "Temperature air outlet"},
333 {0x4210, "Excess temperature device"},
334 {0x4220, "Too low temperature device"},
335 {0x4300, "Temperature drive"},
336 {0x4310, "Excess temperature drive"},
337 {0x4320, "Too low temperature drive"},
338 {0x4400, "Temperature supply"},
339 {0x4410, "Excess temperature supply"},
340 {0x4420, "Too low temperature supply"},
341 {0x5100, "Supply"},
342 {0x5110, "Supply low voltage"},
343 {0x5111, "U1 = supply ±15V"},
344 {0x5112, "U2 = supply +24 V"},
345 {0x5113, "U3 = supply +5 V"},
346 {0x5114, "U4 = manufacturer-specific"},
347 {0x5115, "U5 = manufacturer-specific"},
348 {0x5116, "U6 = manufacturer-specific"},
349 {0x5117, "U7 = manufacturer-specific"},
350 {0x5118, "U8 = manufacturer-specific"},
351 {0x5119, "U9 = manufacturer-specific"},
352 {0x5120, "Supply intermediate circuit"},
353 {0x5200, "Control"},
354 {0x5210, "Measurement circuit"},
355 {0x5220, "Computing circuit"},
356 {0x5300, "Operating unit"},
357 {0x5400, "Power section"},
358 {0x5410, "Output stages"},
359 {0x5420, "Chopper"},
360 {0x5430, "Input stages"},
361 {0x5440, "Contacts"},
362 {0x5441, "Contact 1 = manufacturer-specific"},
363 {0x5442, "Contact 2 = manufacturer-specific"},
364 {0x5443, "Contact 3 = manufacturer-specific"},
365 {0x5444, "Contact 4 = manufacturer-specific"},
366 {0x5445, "Contact 5 = manufacturer-specific"},
367 {0x5450, "Fuses"},
368 {0x5451, "S1 = l1"},
369 {0x5452, "S2 = l2"},
370 {0x5453, "S3 = l3"},
371 {0x5454, "S4 = manufacturer-specific"},
372 {0x5455, "S5 = manufacturer-specific"},
373 {0x5456, "S6 = manufacturer-specific"},
374 {0x5457, "S7 = manufacturer-specific"},
375 {0x5458, "S8 = manufacturer-specific"},
376 {0x5459, "S9 = manufacturer-specific"},
377 {0x5500, "Hardware memory"},
378 {0x5510, "RAM"},
379 {0x5520, "ROM/EPROM"},
380 {0x5530, "EEPROM"},
381 {0x6010, "Software reset (watchdog)"},
382 {0x6301, "to 630Fh Data record no. 1 to no. 15"},
383 {0x6310, "Loss of parameters"},
384 {0x6320, "Parameter error"},
385 {0x7100, "Power"},
386 {0x7110, "Brake chopper"},
387 {0x7111, "Failure brake chopper"},
388 {0x7112, "Over current brake chopper"},
389 {0x7113, "Protective circuit brake chopper"},
390 {0x7120, "Motor"},
391 {0x7121, "Motor blocked"},
392 {0x7122, "Motor error or commutation malfunction."},
393 {0x7123, "Motor tilted"},
394 {0x7200, "Measurement circuit"},
395 {0x7300, "Sensor"},
396 {0x7301, "Tacho fault"},
397 {0x7302, "Tacho wrong polarity"},
398 {0x7303, "Resolver 1 fault"},
399 {0x7304, "Resolver 2 fault"},
400 {0x7305, "Incremental encoder 1 fault"},
401 {0x7306, "Incremental encoder 2 fault"},
402 {0x7307, "Incremental encoder 3 fault"},
403 {0x7310, "Speed"},
404 {0x7320, "Position"},
405 {0x7400, "Computation circuit"},
406 {0x7500, "Communication"},
407 {0x7510, "Serial interface no. 1"},
408 {0x7520, "Serial interface no. 2"},
409 {0x7600, "Data storage (external)"},
410 {0x8300, "Torque control"},
411 {0x8311, "Excess torque"},
412 {0x8312, "Difficult start up"},
413 {0x8313, "Standstill torque"},
414 {0x8321, "Insufficient torque"},
415 {0x8331, "Torque fault"},
416 {0x8400, "Velocity speed controller"},
417 {0x8500, "Position controller"},
418 {0x8600, "Positioning controller"},
419 {0x8611, "Following error"},
420 {0x8612, "Reference limit"},
421 {0x8700, "Sync controller"},
422 {0x8800, "Winding controller"},
423 {0x8900, "Process data monitoring"},
424 {0x8A00, "Control"},
425 {0xF001, "Deceleration"},
426 {0xF002, "Sub-synchronous run"},
427 {0xF003, "Stroke operation"},
428 {0xF004, "Control"},
429 {0xFF03, "Warning - Excess temperature device"},
430 {0xFF04, "Pull brake voltage higher than measured on DC bus"},
431 {0xFF05, "Hold brake voltage higher than measured on DC bus"},
432 {0xFF06, "Open circuit: High FET in phase A"},
433 {0xFF07, "Open circuit: Low FET in phase A"},
434 {0xFF08, "Open circuit: High FET in phase B"},
435 {0xFF09, "Open circuit: Low FET in phase B"},
436 {0xFF0A, "Open circuit: High FET in phase C"},
437 {0xFF0B, "Open circuit: Low FET in phase C"},
438 {0xFF0C, "Commutation offset is missing"}};
439
440const std::map<int32_t, std::map<int32_t, int32_t>>
442 {8500, {{01, 20480}, {02, 24576}, {03, 16384}}},
443 {8501, {{01, 8192}, {02, 8192}, {03, 16384}}},
444 {8502, {{01, 20480}, {02, 24576}, {03, 16384}}},
445 {8503, {{01, 8192}, {02, 8192}, {03, 16384}}},
446 {8504, {{01, 20480}, {02, 24576}, {03, 16384}}},
447 {8505, {{01, 8192}, {02, 8192}, {03, 16384}}},
448 {9500, {{00, 32768}, {01, 32768}, {02, 32768}}},
449 {9501, {{01, 81920}, {02, 81920}}},
450 {9502, {{01, 16384}}},
451 {9506, {{01, 32768}}},
452 {9507, {{01, 81920}}},
453 {9508, {{01, 16384}}},
454 {9600, {{01, 32768}}},
455 {9601, {{01, 81920}}},
456 {9602, {{01, 16384}}},
457 {9606, {{01, 32768}}},
458 {9607, {{01, 81920}}},
459 {9608, {{01, 16384}}}};
460
461const std::map<int32_t, std::map<int32_t, int32_t>>
463 {8500, {{01, 40960}, {02, 49152}, {03, 32768}}},
464 {8501, {{01, 16384}, {02, 16384}, {03, 16384}}},
465 {8502, {{01, 40960}, {02, 49152}, {03, 32768}}},
466 {8503, {{01, 16384}, {02, 16384}, {03, 16384}}},
467 {8504, {{01, 40960}, {02, 49152}, {03, 32768}}},
468 {8505, {{01, 16384}, {02, 16384}, {03, 16384}}},
469 {9500, {{00, 32768}, {01, 32768}, {02, 32768}}},
470 {9501, {{01, 81920}, {02, 81920}}},
471 {9502, {{01, 16384}}},
472 {9506, {{01, 32768}}},
473 {9507, {{01, 81920}}},
474 {9508, {{01, 16384}}},
475 {9600, {{01, 32768}}},
476 {9601, {{01, 81920}}},
477 {9602, {{01, 16384}}},
478 {9606, {{01, 32768}}},
479 {9607, {{01, 81920}}},
480 {9608, {{01, 16384}}}};
481
483 kNotFound = 0x00008001,
484 kAccessDenied = 0x00008002,
485 kDiskFull = 0x00008003,
486 kIllegal = 0x00008004,
487 kPacketNumberWrong = 0x00008005,
488 kAlreadyExists = 0x00008006,
489 kNoUser = 0x00008007,
490 kBootstrapOnly = 0x00008008,
491 kNotBootstrap = 0x00008009,
492 kNoRights = 0x0000800a,
493 kProgramError = 0x0000800b
494};
495
504
512std::string get_slave_error_message(int32_t error_code);
513
514#ifdef __linux__
520int32_t set_default_thread_priority();
521
533int32_t set_max_thread_priority();
534#endif
535
544std::string etg1004_unit_string(uint32_t etg1004_unit_value);
545
554int32_t get_pdo_id(uint16_t index, uint8_t subindex,
555 std::map<int32_t, int32_t>& pdo_position_map);
556
557#ifdef __linux__
566std::string execute_system_command(const char* cmd);
567#endif
568
577bool abs_compare(float a, float b);
578
587template <typename T>
588bool opposite_signs(const T& a, const T& b) {
589 return ((a ^ b) >> (sizeof(T) * 8 - 1));
590}
591
597void interruptable_wait(size_t seconds);
598
608int32_t wrap_target_position(int32_t target, int32_t max_limit,
609 int32_t min_limit);
610
617
623void unregister_sync_signal(uint32_t signal_id);
624
632uint64_t wait_for_sync_signal(uint32_t signal_id);
640uint64_t wait_for_signal(int signal_fd);
641uint64_t wait_for_signal(uint32_t signal_id);
642
652std::vector<double> linspace(double lb, double ub, size_t n);
653
663
678 const ControllerGains& gains);
679
694 const ControllerGains& gains);
695
706bool check_gains(Cia402Drive& cia402_drive);
707
716bool string_ends_with(const std::string& value, const std::string& ending);
717
725std::vector<uint8_t> read_file_content(const std::string& path);
726
733void set_csv_configuration(VirtualDevice* virtual_device,
734 const std::vector<uint8_t>& csv_data);
735
746uint8_t calculate_crc(const std::vector<uint8_t>& data);
747
756uint32_t random_uint32(uint32_t min = 0, uint32_t max = UINT32_MAX);
757
763unsigned char random_char();
764
772std::string generate_uuid(size_t length);
773
781int32_t limit_int32_target(int64_t target);
782
788std::string run_env_info();
789
798bool validate_sii_data(const std::vector<uint8_t>& content, uint32_t device_id);
799
807bool file_exists(const std::string& path);
808
816std::string string_bytes_to_hex_array_string(const std::string& bytes);
817
827std::string get_firmware_version_string(Cia402Drive& cia402_drive);
828
838semver::version<> get_firmware_version(Cia402Drive& cia402_drive);
839
849
862 Cia402Drive& cia402_drive);
863
873 ControllerGains gains, uint32_t singleturn_resolution);
874
891CirculoType get_circulo_type(int32_t hardware_id, uint8_t encoder_ordinal);
892
901bool has_uncharacterised_magnet_rings(int32_t hardware_id);
902
912bool is_fw_based_on_v5(Cia402Drive& cia402_drive);
913
922bool is_fw_at_least(Cia402Drive& cia402_drive, const std::string& version);
923
932std::vector<uint8_t> zip(const std::string& file_name,
933 std::vector<uint8_t> data);
934
944std::vector<uint8_t> unzip(const std::vector<uint8_t>& data);
945
954uint32_t generate_od_entry_map_key(uint16_t index, uint8_t subindex);
955
964std::list<std::string> get_file_parts(std::list<std::string> file_list,
965 const std::string& file_name);
966
976 uint8_t encoder_ordinal);
977
985template <typename T>
986int32_t find_local_min_index_from_right(std::vector<T>& x) {
987 T min_value = x[x.size() - 1];
988
989 for (int32_t i = x.size() - 2; i >= 0; i--) {
990 if (x[i] <= min_value) {
991 min_value = x[i];
992 } else {
993 return i + 1;
994 }
995 }
996
997 return 1;
998}
999
1007template <typename T>
1008double compute_mean(const std::vector<T>& numbers) {
1009 if (numbers.empty()) {
1010 return 0.0;
1011 }
1012
1013 return std::accumulate(numbers.begin(), numbers.end(), 0.0) / numbers.size();
1014}
1015
1023template <typename T>
1024double compute_standard_deviation(const std::vector<T>& numbers) {
1025 if (numbers.empty()) {
1026 return 0.0;
1027 }
1028
1029 double mean = compute_mean(numbers);
1030
1031 std::vector<double> tmp(numbers.size());
1032 std::transform(numbers.begin(), numbers.end(), tmp.begin(),
1033 [mean](double x) { return x - mean; });
1034
1035 double sq_sum = std::inner_product(tmp.begin(), tmp.end(), tmp.begin(), 0.0);
1036 return std::sqrt(sq_sum / (numbers.size() - 1));
1037}
1038
1048template <typename T>
1049double compute_section_mean(const std::vector<T>& numbers, size_t start,
1050 size_t end) {
1051 std::vector<T> section(numbers.begin() + start, numbers.begin() + end);
1052
1053 if (section.empty()) {
1054 return 0.0;
1055 }
1056
1057 return std::accumulate(section.begin(), section.end(), 0.0) / section.size();
1058}
1059
1068template <typename T>
1069std::vector<T> erase_edge_elements(std::vector<T> v, size_t length) {
1070 v.erase(v.begin(), v.begin() + length);
1071 v.erase(v.end() - length, v.end());
1072 return v;
1073}
1074
1082template <typename R>
1083bool is_future_done(std::future<R> const& f) {
1084 try {
1085 return f.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
1086 } catch (std::future_error& e) {
1087 return true; // The future is not running since there is an error
1088 }
1089}
1090
1098template <typename T>
1099auto to_integral(T e) {
1100 return static_cast<std::underlying_type_t<T>>(e);
1101}
1102
1111template <typename T>
1112static std::vector<T> diff(std::vector<T> vector) {
1113 std::vector<T> result;
1114 result.reserve(vector.size() - 1);
1115
1116 for (size_t i = 1; i < vector.size(); i++) {
1117 result.push_back(vector[i] - vector[i - 1]);
1118 }
1119
1120 return result;
1121}
1122
1126template <typename... Args>
1127std::string string_format(const std::string& format, Args... args) {
1128 int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) +
1129 1; // Extra space for '\0'
1130 if (size_s <= 0) {
1131 throw std::runtime_error("Error during formatting.");
1132 }
1133 auto size = static_cast<size_t>(size_s);
1134 std::unique_ptr<char[]> buf(new char[size]);
1135 std::snprintf(buf.get(), size, format.c_str(), args...);
1136 return std::string(buf.get(),
1137 buf.get() + size - 1); // We don't want the '\0' inside
1138}
1139
1150 public:
1151 LowPassFilter(uint16_t cutoff_frequency, double ts);
1152
1153 double filter(double value);
1154
1155 private:
1156 std::deque<double> input_data_;
1157 std::deque<double> output_data_;
1158 double fc_, a0_, a1_, a2_, b0_, b1_, b2_;
1159};
1160
1170 public:
1171 HighPassFilter(uint16_t cutoff_frequency, double ts);
1172
1173 double filter(double value);
1174
1175 private:
1176 std::deque<double> input_data_;
1177 std::deque<double> output_data_;
1178 double fc_, a0_, a1_, a2_, a3_, a4_, b0_, b1_, b2_, b3_, b4_;
1179};
1180
1182
1183bool compareStringsCaseInsensitive(const std::string& s1,
1184 const std::string& s2);
1185
1187const uint16_t kDefaultEthernetPort = 8080;
1188
1201std::pair<std::string, uint16_t> parse_ethernet_endpoint(
1202 const std::string& endpoint);
CirculoType get_circulo_type(int32_t hardware_id, uint8_t encoder_ordinal)
Get the encoder class of one encoder, from the hardware ID and the encoder ordinal.
Definition util.cc:1288
std::vector< uint8_t > unzip(const std::vector< uint8_t > &data)
Unzip data containing a single file.
Definition util.cc:1414
const std::map< int32_t, std::string > slave_error_map_
Definition util.h:186
ControllerGains get_position_controller_gains(Cia402Drive &cia402_drive)
Get the current position controller gains of a drive.
Definition util.cc:296
const char *const HARDWARE_DESCRIPTION_FILENAME
Definition util.h:22
const uint16_t kDefaultEthernetPort
Default port used for SPoE Ethernet endpoints when none is specified.
Definition util.h:1187
double compute_section_mean(const std::vector< T > &numbers, size_t start, size_t end)
Compute the mean of a section of a given vector of integers.
Definition util.h:1049
bool has_uncharacterised_magnet_rings(int32_t hardware_id)
Check whether the hardware ID is a device whose magnetic rings have not been characterised for the ma...
Definition util.cc:1317
ControllerGains convert_auto_tuning_controller_gains(ControllerGains gains, uint32_t singleturn_resolution)
Convert gains to inc pos values and rpm velocity values.
Definition util.cc:1236
const std::map< int32_t, std::map< int32_t, int32_t > > adc_current_ratio_fw_v4_4_2_map
Definition util.h:462
uint64_t wait_for_signal(int signal_fd)
Wait for the system signal.
std::string controllerTypeToString(ControllerType ct)
Definition util.cc:166
uint32_t register_sync_signal()
Register a thread sync signal.
bool string_ends_with(const std::string &value, const std::string &ending)
Check if a string ends with another string.
Definition util.cc:480
const std::map< int32_t, std::map< int32_t, int32_t > > adc_current_ratio_fw_v4_4_1_map
Definition util.h:441
std::string generate_uuid(size_t length)
Generate a UUID of a custom length.
Definition util.cc:657
semver::version get_firmware_version(Cia402Drive &cia402_drive)
Get the (semver) version of the used firmware.
Definition util.cc:941
std::pair< std::string, uint16_t > parse_ethernet_endpoint(const std::string &endpoint)
Parse an SPoE Ethernet endpoint of the form "ip" or "ip:port".
Definition util.cc:1638
std::string get_slave_error_message(int32_t error_code)
Return the appropriate error message.
Definition util.cc:48
int32_t find_local_min_index_from_right(std::vector< T > &x)
Find a local minimum (from right) in the supplied vector.
Definition util.h:986
struct AutoTuningDriveConfigurationClass AutoTuningDriveConfiguration
const char *const STACK_INFO_FILENAME
Definition util.h:23
bool validate_sii_data(const std::vector< uint8_t > &content, uint32_t device_id)
Validate an SII data before writing.
double compute_mean(const std::vector< T > &numbers)
Compute mean given a vector of numbers.
Definition util.h:1008
bool opposite_signs(const T &a, const T &b)
Check if parameters have opposite signs.
Definition util.h:588
bool get_plant_model_from_flash(Cia402Drive &cia402_drive, PlantModel *m)
Get the plant model from the flash memory of the device.
Definition util.cc:990
const char *const ESI_FILENAME
Definition util.h:25
const std::map< MotionMasterWarning, std::string > motion_master_warning_to_message_map_
Definition util.h:179
void unregister_sync_signal(uint32_t signal_id)
Unregister a thread sync signal.
int32_t limit_int32_target(int64_t target)
Limit a target to integer 32 if necessary.
Definition util.cc:669
std::string run_env_info()
Get all sorts of run environment information.
Definition util.cc:689
const char *const CONFIG_FILENAME
Definition util.h:26
bool compareStringsCaseInsensitive(const std::string &s1, const std::string &s2)
Definition util.cc:1631
int32_t wrap_target_position(int32_t target, int32_t max_limit, int32_t min_limit)
Wrap the target position over limit.
std::vector< double > linspace(double lb, double ub, size_t n)
Generate equally spaced points inside an interval.
Definition util.cc:280
CirculoType
Definition util.h:58
@ kCirculo7
Definition util.h:60
@ kCirculo9Smm
Definition util.h:63
@ kCirculo7Smm
Definition util.h:61
@ kCirculo9
Definition util.h:62
@ kCirculo18Bit
Definition util.h:66
PositionControlStrategy
Definition util.h:170
@ kSimple
Definition util.h:170
@ kNone
Definition util.h:170
@ kCascaded
Definition util.h:170
ControllerType
Definition util.h:52
@ kPpi
Definition util.h:52
@ kPiP
Definition util.h:52
uint64_t wait_for_sync_signal(uint32_t signal_id)
Wait for the system sync signal.
std::string etg1004_unit_string(uint32_t etg1004_unit_value)
Convert ETG.1004 unit format to string.
Definition util.cc:102
EncoderLocation
Definition util.h:81
@ kMotorShaft
Definition util.h:81
@ kDrivingShaft
Definition util.h:81
MotionMasterWarning
Definition util.h:172
@ WAR_PROFILE_TARGET_LIMITED
Definition util.h:174
@ WAR_INTERNAL_LIMIT_REACHED
Definition util.h:175
@ WAR_NONE
Definition util.h:173
const char *const VARIANT_FILENAME
Definition util.h:28
std::vector< uint8_t > read_file_content(const std::string &path)
Read a file into a byte (uint8_t) vector.
Definition util.cc:488
uint8_t calculate_crc(const std::vector< uint8_t > &data)
Definition util.cc:618
uint32_t generate_od_entry_map_key(uint16_t index, uint8_t subindex)
Generate a map key for an OD entry using index/subindex.
Definition util.cc:1455
unsigned char random_char()
Generate a random character.
Definition util.cc:650
std::vector< uint8_t > zip(const std::string &file_name, std::vector< uint8_t > data)
Zip a single file.
Definition util.cc:1362
bool file_exists(const std::string &path)
Check if a file exists on the system where Motion Master is running.
Definition util.cc:903
std::string get_master_warning_message(MotionMasterWarning warning)
Return the appropriate warning message.
Definition util.cc:40
MotionMasterError set_position_controller_gains(Cia402Drive &cia402_drive, const ControllerGains &gains)
Set the position controller gains (0x2012)
Definition util.cc:400
bool check_gains(Cia402Drive &cia402_drive)
Check if the required gains are properly set.
Definition util.cc:437
CirculoEncoderType
Definition util.h:72
@ kCirculo7Inner
Definition util.h:74
@ kCirculo9Outer
Definition util.h:77
@ kCirculo18BitInner
Definition util.h:78
@ kUndefined
Definition util.h:73
@ kCirculo7Outer
Definition util.h:75
@ kCirculo9Inner
Definition util.h:76
bool abs_compare(float a, float b)
Compare absolute values of the input parameters.
Definition util.cc:217
void interruptable_wait(size_t seconds)
Blocks for the set amount of time, but allows to be interrupted.
Definition util.cc:219
uint32_t random_uint32(uint32_t min=0, uint32_t max=UINT32_MAX)
Generate a random unsigned 32-bit integer in a certain range.
Definition util.cc:643
double compute_standard_deviation(const std::vector< T > &numbers)
Compute the standard deviation given a vector of numbers.
Definition util.h:1024
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:1459
std::vector< T > erase_edge_elements(std::vector< T > v, size_t length)
Remove a number of the first and last elements in the vector.
Definition util.h:1069
int get_soem_slaves_responding()
Definition util.cc:1625
EncoderLocation get_encoder_location(Cia402Drive &cia402_drive, uint8_t encoder_ordinal)
Get the location of an encoder.
Definition util.cc:1503
int32_t get_pdo_id(uint16_t index, uint8_t subindex, std::map< int32_t, int32_t > &pdo_position_map)
Uses a special map and its keys to get the PDO array positions.
Definition util.cc:153
const char *const STACK_IMAGE_FILENAME
Definition util.h:24
std::string string_bytes_to_hex_array_string(const std::string &bytes)
Convert bytes (as string) to a string of hex values.
Definition util.cc:908
bool is_fw_based_on_v5(Cia402Drive &cia402_drive)
Check if the device requires the newer encoder configuration (FW >= v5 or FW >= v0....
Definition util.cc:1327
EcatSlaveFoeError
Definition util.h:482
@ kAccessDenied
Definition util.h:484
@ kNotBootstrap
Definition util.h:491
@ kProgramError
Definition util.h:493
@ kBootstrapOnly
Definition util.h:490
@ kNoRights
Definition util.h:492
@ kIllegal
Definition util.h:486
@ kAlreadyExists
Definition util.h:488
@ kNotFound
Definition util.h:483
@ kPacketNumberWrong
Definition util.h:487
@ kNoUser
Definition util.h:489
@ kDiskFull
Definition util.h:485
bool is_future_done(std::future< R > const &f)
Check if the future has finished executing.
Definition util.h:1083
bool is_fw_at_least(Cia402Drive &cia402_drive, const std::string &version)
Check if the drive uses at least a certain FW version.
Definition util.cc:1347
struct TuningParametersClass TuningParameters
struct SystemParametersClass SystemParameters
const char *const PLANT_MODEL_FILENAME
Definition util.h:27
MotionMasterError set_velocity_controller_gains(Cia402Drive &cia402_drive, const ControllerGains &gains)
Set the velocity controller gains (0x2011)
Definition util.cc:423
std::string get_firmware_version_string(Cia402Drive &cia402_drive)
Get the (string) version of the used firmware.
Definition util.cc:920
std::string string_format(const std::string &format, Args... args)
Format a string like printf()
Definition util.h:1127
auto to_integral(T e)
Return an integral type value of an enum class.
Definition util.h:1099
AutoTuningDriveConfiguration get_auto_tuning_drive_configuration(Cia402Drive &cia402_drive)
Get the configuration required for full auto-tuning.
Definition util.cc:1020
void set_csv_configuration(VirtualDevice *virtual_device, const std::vector< uint8_t > &csv_data)
Set the configuration parameter values from CSV data.
Definition util.cc:518
Definition cia402_drive.h:120
Holds position and velocity PID gains for a controller.
Definition standalone_autotuning.h:69
HighPassFilter(uint16_t cutoff_frequency, double ts)
Definition util.cc:1590
double filter(double value)
Definition util.cc:1611
double filter(double value)
Definition util.cc:1577
LowPassFilter(uint16_t cutoff_frequency, double ts)
Definition util.cc:1560
Definition motion_master_error.h:6
Definition virtual_device.h:23
uint8_t * value
Definition co_dictionary.h:9
uint16_t index
Definition co_dictionary.h:0
@ kUnspecified
Definition global.h:38
uint16_t notch_fw
Definition util.h:92
int32_t R
Definition util.h:97
int32_t L
Definition util.h:96
nlohmann::json to_json()
Definition util.h:108
double tc_kp
Definition util.h:100
uint16_t notch_fc
Definition util.h:93
int16_t vel_ff_gain
Definition util.h:94
uint16_t tc_st
Definition util.h:103
uint16_t tc_damping
Definition util.h:104
double max_vel_noise
Definition util.h:107
uint32_t vdc
Definition util.h:98
uint8_t vel_lp_type
Definition util.h:87
uint8_t switch_F
Definition util.h:102
uint32_t vel_lp_fc
Definition util.h:88
bool notch_en
Definition util.h:91
double tc_ki
Definition util.h:101
uint8_t pos_lp_type
Definition util.h:89
uint32_t pos_lp_fc
Definition util.h:90
std::string software_version
Definition util.h:105
unsigned int rated_torque
Definition util.h:106
uint16_t vel_ff_fc
Definition util.h:95
double current_ratio
Definition util.h:99
Definition util.h:30
double end_frequency
Definition util.h:34
uint16_t torque_amplitude
Definition util.h:35
double start_frequency
Definition util.h:33
std::string bode_file_content
Definition util.h:49
std::vector< double > denominator
Definition util.h:32
uint8_t signal_type
Definition util.h:37
double duration_seconds
Definition util.h:36
nlohmann::json to_json()
Definition util.h:38
std::vector< double > numerator
Definition util.h:31
Definition util.h:136
uint32_t nominal_torque
Definition util.h:138
double time_step
Definition util.h:137
uint32_t max_torque_mnm_
Definition util.h:139
uint32_t singleturn_resolution
Definition util.h:140
Definition util.h:143
int16_t alpha_mult
Definition util.h:155
double end_frequency
Definition util.h:148
double warn_torque_const
Definition util.h:163
double cutoff_frequency
Definition util.h:149
double lb
Definition util.h:166
uint16_t torque_amplitude
Definition util.h:146
float velocity_loop_bandwidth
Definition util.h:159
float velocity_damping
Definition util.h:160
double start_frequency
Definition util.h:147
ControllerType controller_type
Definition util.h:152
double duration_seconds
Definition util.h:145
double position_damping
Definition util.h:154
double ub
Definition util.h:167
double settling_time
Definition util.h:153
uint8_t order
Definition util.h:156