Motion Master
Loading...
Searching...
No Matches
cia402_drive.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5#include <functional>
6#include <list>
7#include <map>
8#include <mutex>
9#include <string>
10#include <unordered_map>
11#include <variant>
12#include <vector>
13
14#include "dictionary_symbols.h"
15#include "virtual_parameter.h"
16
17/*
18 * CiA402 State defines
19 */
20
21#define CIA402_STATUS_WORD_MASQ_A 0x6fu
22#define CIA402_STATUS_WORD_MASQ_B 0x4fu
23#define CIA402_STATUS_WORD_MASQ_WARN 0x80u
24
25#define CIA402_STATUS_NOT_READY 0x00u /* masq B */
26#define CIA402_STATUS_SWITCH_ON_DISABLED 0x40u /* masq B */
27#define CIA402_STATUS_READY_SWITCH_ON 0x21u
28#define CIA402_STATUS_SWITCHED_ON 0x23u
29#define CIA402_STATUS_OP_ENABLED 0x27u
30#define CIA402_STATUS_QUICK_STOP_ACTIVE 0x07u
31#define CIA402_STATUS_FAULT_REACTION_ACTIVE 0x0fu /* masq B */
32#define CIA402_STATUS_FAULT 0x08u /* masq B */
33
34#define CIA402_STATUS_INTERNAL_LIMIT_REACHED 0x0800u
35
36#define CIA402_CONTROL_BIT_ENABLE_OP 0x08u
37#define CIA402_CONTROL_BIT_QUICK_STOP 0x04u
38#define CIA402_CONTROL_BIT_ENABLE_VOLTAGE 0x02u
39#define CIA402_CONTROL_BIT_SWITCH_ON 0x01u
40#define CIA402_CONTROL_BIT_FAULT_RESET 0x80u
41
42#define FAULT_RESET_TIMEOUT_ATTEMPTS 2000
43
45class VirtualDevice;
47
66
74
80 uint8_t signalType;
81
88
95
103
110
118};
119
121 public:
122 enum class OpMode {
123 kTuning = -128,
127 kNo = 0,
128 kPp, // Profile position
129 kVel, // Velocity
130 kPv, // Profile velocity
131 kPt, // Profile torque
132 kRes, // Reserved
133 kHom, // Homing
134 kIp, // Interpolated position
135 kCsp, // Cyclic synchronous position
136 kCsv, // Cyclic synchronous velocity
137 kCst // Cyclic synchronous torque
138 };
139
150
161
162 // map<encoder ordinal, encoder configuration index>
163 static inline std::map<uint8_t, uint16_t> fw_v5_encoder_configuration_map_ = {
166
167 // map<encoder ordinal, encoder feedback index>
168 static inline std::map<uint8_t, uint16_t> fw_v5_encoder_feedback_map_ = {
170
183
184 // Controller (position, velocity, torque) to encoder function
185 static inline std::map<std::tuple<bool, bool, bool>, EncoderFunction>
187 {{false, false, false}, EncoderFunction::kDisabled},
188 {{false, false, true}, EncoderFunction::kCommutationOnly},
189 {{false, true, false}, EncoderFunction::kVelocity},
190 {{false, true, true}, EncoderFunction::kCommutationAndVelocity},
191 {{true, false, false}, EncoderFunction::kPosition},
192 {{true, false, true}, EncoderFunction::kCommutationAndPosition},
193 {{true, true, false}, EncoderFunction::kMotionControlFeedbackOnly},
194 {{true, true, true},
195 EncoderFunction::kCommutationAndMotionControlFeedback}};
196
206
207 enum class BrakeStatus { kNotConfigured = 0, kEngaged = 1, kDisengaged = 2 };
208
214
215 explicit Cia402Drive(VirtualDevice* virtual_device);
216
222 [[nodiscard]] int32_t get_id() const;
223
229 [[nodiscard]] uint16_t get_hardware_id() const;
230
237
244
251
258
265
272
279
286
292 void set_halt_bit(bool value);
293
299 void set_target_position(int32_t position);
300
306 void set_target_velocity(int32_t velocity);
307
313 void set_target_torque(int32_t torque);
314
320 uint16_t get_statusword();
321
327 int32_t get_target_position();
328
334 int32_t get_target_velocity();
335
341 int32_t get_target_torque();
342
349
356
362 int32_t get_current_position();
363
369 int32_t get_current_velocity();
370
376 int32_t get_current_torque();
377
383 uint32_t get_current_timestamp();
384
394 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
395 int32_t value);
396
407 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
408 int32_t value, bool reload_configuration);
409
419 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
420 float value);
421
432 MotionMasterError set_parameter(uint16_t index, uint8_t subindex, float value,
433 bool reload_configuration);
434
444 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
445 double value);
446
457 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
458 double value, bool reload_configuration);
459
469 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
470 const std::string& value);
471
480 VirtualParameter* get_parameter(uint16_t index, uint8_t subindex);
481
495 MotionMasterError reload_drive(bool ignore_fault_reset_timeout = false);
496
502 [[nodiscard]] std::variant<MotionMasterError, std::list<std::string>>
503 get_file_list() const;
504
512 [[nodiscard]] std::variant<MotionMasterError, std::string> read_file(
513 const std::string& file_name) const;
514
523 MotionMasterError write_file(const std::string& name,
524 const std::string& content);
525
533 MotionMasterError remove_file(const std::string& file_name);
534
540 std::string get_log();
541
552 std::map<uint16_t, EncoderFunction> get_all_encoder_functions();
553
563 bool set_encoder_function(uint16_t index, EncoderFunction encoder_function);
564
572
580
589
601
610 double get_gear_ratio();
611
617 double get_feed_constant();
618
625 bool is_brake_engaged();
626
635
637
641 void quick_stop();
642
649
656
663
669 bool is_running();
670
676 bool get_fault_state();
677
685 MotionMasterError reset_fault(bool ignore_timeout_error = false);
686
692 bool get_warning_status();
693
701
709 bool is_operational();
710
720 Cia402State target_state);
721
730
740 std::vector<int16_t> get_cogging_torque_compensation_data();
741
751 MotionMasterError read_encoder_register(uint8_t encoder_ordinal,
752 uint8_t address, uint8_t& value);
753
763 MotionMasterError write_encoder_register(uint8_t encoder_ordinal,
764 uint8_t address, uint8_t value);
765
773 std::tuple<uint8_t, uint8_t, uint8_t> get_controller_encoder_sources();
774
776
777 std::vector<uint8_t> read_all_hrd_files();
778
779 std::variant<MotionMasterError, std::vector<uint8_t>> run_hrd_streaming(
780 uint8_t data_index, uint32_t duration_ms);
781
782 std::variant<MotionMasterError, std::vector<uint8_t>> run_chirp_signal(
783 ChirpSignalOptions options);
784
785 private:
786 VirtualDevice* virtual_device_;
787
791 void refresh_current_position();
792
798 void set_controlword(uint16_t controlword);
799
805 void set_op_mode(OpMode op_mode);
806
814 [[nodiscard]] Cia402State cia402_read_state(uint16_t statusword) const;
815
824 static uint16_t cia402_command(Cia402Command command, uint16_t controlword);
825
836 static uint16_t cia402_go_to_state(Cia402State target_state,
837 Cia402State current_state,
838 uint16_t controlword,
839 bool skip_states = true);
840
846 std::vector<VirtualParameter*> get_sensor_port_virtual_parameters();
847};
Cia402Drive::EncoderFunction EncoderFunction
Definition circulo_encoder_configuration.cc:14
static std::map< uint8_t, uint16_t > fw_v5_encoder_feedback_map_
Definition cia402_drive.h:168
std::vector< uint8_t > read_all_hrd_files()
Definition cia402_drive.cc:2277
bool get_internal_limit_reached()
Check if the internal limit reached statusword bit is active.
Definition cia402_drive.cc:640
int32_t get_target_velocity()
Get the current target velocity.
Definition cia402_drive.cc:237
MotionMasterError reset_fault(bool ignore_timeout_error=false)
Reset the current fault.
Definition cia402_drive.cc:552
std::tuple< uint8_t, uint8_t, uint8_t > get_controller_encoder_sources()
Get the encoder used for each of the controllers.
Definition cia402_drive.cc:897
MotionMasterError read_encoder_register(uint8_t encoder_ordinal, uint8_t address, uint8_t &value)
Read a value from an encoder register.
Definition cia402_drive.cc:834
static std::map< uint8_t, uint16_t > fw_v5_encoder_configuration_map_
Definition cia402_drive.h:163
uint16_t get_statusword()
Get the raw statusword.
Definition cia402_drive.cc:279
EncoderFunction
Definition cia402_drive.h:171
@ kMonitoringOnly
Definition cia402_drive.h:176
@ kCommutationOnly
Definition cia402_drive.h:177
@ kDisabled
Definition cia402_drive.h:172
@ kCommutationAndMonitoring
Definition cia402_drive.h:174
@ kVelocity
Definition cia402_drive.h:180
@ kCommutationAndMotionControlFeedback
Definition cia402_drive.h:173
@ kMotionControlFeedbackOnly
Definition cia402_drive.h:175
@ kCommutationAndPosition
Definition cia402_drive.h:181
@ kPosition
Definition cia402_drive.h:178
@ kCommutationAndVelocity
Definition cia402_drive.h:179
bool is_running()
Check if the device is currently running.
Definition cia402_drive.cc:542
void set_target_torque(int32_t torque)
Set the target torque.
Definition cia402_drive.cc:116
MotionMasterError state_machine_control(OpMode target_op_mode, Cia402State target_state)
Change the device OP mode and CiA402 state.
Definition cia402_drive.cc:1939
MotionMasterError init_position_controller()
Initialize the position controller.
Definition cia402_drive.cc:62
MotionMasterError init_velocity_controller()
Initialize the velocity controller.
Definition cia402_drive.cc:88
void set_target_velocity(int32_t velocity)
Set the target velocity.
Definition cia402_drive.cc:96
std::variant< MotionMasterError, std::list< std::string > > get_file_list() const
Get the list of available files on the flash memory.
Definition cia402_drive.cc:762
void remove_all_hrd_files()
Definition cia402_drive.cc:2271
MotionMasterError quick_stop_blocking()
Request an asynchronous quick stop on a device.
Definition cia402_drive.cc:447
std::variant< MotionMasterError, std::vector< uint8_t > > run_chirp_signal(ChirpSignalOptions options)
Definition cia402_drive.cc:2336
void reset_torque_target_and_offset()
Definition cia402_drive.cc:386
static std::map< std::tuple< bool, bool, bool >, EncoderFunction > controller_to_encoder_function_map_
Definition cia402_drive.h:186
MotionMasterError init_slave_profile_position()
Initialize the position profile on the slave.
Definition cia402_drive.cc:69
BrakeStatus
Definition cia402_drive.h:207
@ kEngaged
Definition cia402_drive.h:207
@ kDisengaged
Definition cia402_drive.h:207
@ kNotConfigured
Definition cia402_drive.h:207
bool execute_profile_position_ramp()
Execute a position profile ramp on the slave.
Definition cia402_drive.cc:149
uint32_t get_current_timestamp()
Get the current timestamp.
Definition cia402_drive.cc:373
Cia402Command
Definition cia402_drive.h:151
@ kNone
Definition cia402_drive.h:152
@ kSwitchOn
Definition cia402_drive.h:154
@ kEnableOperation
Definition cia402_drive.h:158
@ kFaultReset
Definition cia402_drive.h:159
@ kDisableOperation
Definition cia402_drive.h:157
@ kQuickStop
Definition cia402_drive.h:156
@ kShutdown
Definition cia402_drive.h:153
@ kDisableVoltage
Definition cia402_drive.h:155
MotionMasterError set_parameter(uint16_t index, uint8_t subindex, int32_t value)
Set parameter value (and reload the device if requested)
Definition cia402_drive.cc:653
EncoderType
Definition cia402_drive.h:197
@ kBissEncoder
Definition cia402_drive.h:201
@ kHallEncoder
Definition cia402_drive.h:199
@ kRem14Encoder
Definition cia402_drive.h:202
@ kQeiEncoder
Definition cia402_drive.h:200
@ kRem16MtEncoder
Definition cia402_drive.h:203
@ kSsiEncoder
Definition cia402_drive.h:204
int32_t get_id() const
Get the device ID.
Definition cia402_drive.cc:56
MotionMasterError init_slave_profile_velocity()
Initialize the velocity profile on the slave.
Definition cia402_drive.cc:92
void set_target_position(int32_t position)
Set the target position.
Definition cia402_drive.cc:76
MotionMasterError reload_drive(bool ignore_fault_reset_timeout=false)
Make the CiA402 drive reload (soft restart)
Definition cia402_drive.cc:741
void set_halt_bit(bool value)
Set the halt bit (up or down) in the controlword.
Definition cia402_drive.cc:198
VirtualParameter * get_parameter(uint16_t index, uint8_t subindex)
Get a parameter by index/subindex.
Definition cia402_drive.cc:737
MotionMasterError engage_brake(bool value)
Apply or release the brake.
Definition cia402_drive.cc:1627
MotionMasterError init_slave_profile_torque()
Initialize the torque profile on the slave.
Definition cia402_drive.cc:112
int32_t get_current_torque()
Get the current torque.
Definition cia402_drive.cc:360
int32_t get_target_torque()
Get the current target torque.
Definition cia402_drive.cc:257
std::map< uint16_t, EncoderFunction > get_all_encoder_functions()
Get all encoder functions.
Definition cia402_drive.cc:979
BrakeReleaseStrategy
Definition cia402_drive.h:209
@ kManualOutputVoltage
Definition cia402_drive.h:210
@ kPinBrakeControl
Definition cia402_drive.h:212
@ kClutchStyleBrakeControl
Definition cia402_drive.h:211
Cia402Drive(VirtualDevice *virtual_device)
Definition cia402_drive.cc:30
MotionMasterError deinit_current_controller()
Deinitialize a controller.
Definition cia402_drive.cc:128
OpMode get_op_mode()
Get the current OP mode.
Definition cia402_drive.cc:299
std::vector< int16_t > get_cogging_torque_compensation_data()
Get the cogging torque compensation values.
Definition cia402_drive.cc:814
bool get_fault_state()
Get the current fault state.
Definition cia402_drive.cc:546
int32_t load_configuration_from_flash()
Load the configuration from the flash memory.
Definition cia402_drive.cc:518
bool get_warning_status()
Get the current warning status.
Definition cia402_drive.cc:636
EncoderType get_commutation_encoder_type()
Get the type of the encoder set as the commutation encoder.
Definition cia402_drive.cc:1213
int32_t get_target_position()
Get the current target position.
Definition cia402_drive.cc:217
bool is_gearbox_potentially_present()
Determine if a gearbox is potentially present depending on the encoder configuration.
Definition cia402_drive.cc:1464
MotionMasterError write_file(const std::string &name, const std::string &content)
Write the file to the flash memory.
Definition cia402_drive.cc:777
MotionMasterError remove_file(const std::string &file_name)
Remove a file from the flash memory.
Definition cia402_drive.cc:783
bool is_brake_engaged()
Check if the brake is currently engaged.
Definition cia402_drive.cc:1610
Cia402State get_cia402_state()
Get the current CiA402 state.
Definition cia402_drive.cc:291
MotionMasterError write_encoder_register(uint8_t encoder_ordinal, uint8_t address, uint8_t value)
Write a value into an encoder register.
Definition cia402_drive.cc:862
bool is_operational()
Check if the device is operational.
Definition cia402_drive.cc:645
std::variant< MotionMasterError, std::vector< uint8_t > > run_hrd_streaming(uint8_t data_index, uint32_t duration_ms)
Definition cia402_drive.cc:2296
bool set_encoder_function(uint16_t index, EncoderFunction encoder_function)
(Re)set an encoder function based on the get_all_encoder_functions output
Definition cia402_drive.cc:1025
int32_t get_current_position()
Get the current position.
Definition cia402_drive.cc:334
int32_t save_current_configuration_to_flash()
Save the current device configuration to the flash memory.
Definition cia402_drive.cc:494
uint32_t get_position_control_encoder_resolution()
Find the resolution of the encoder used for motion feedback (function 1 or 3)
Definition cia402_drive.cc:1381
MotionMasterError init_torque_controller()
Initialize the torque controller.
Definition cia402_drive.cc:108
uint32_t get_commutation_encoder_resolution()
Find the resolution of the encoder used for commutation feedback.
Definition cia402_drive.cc:1295
std::string get_log()
Get the slave log from the flash memory.
Definition cia402_drive.cc:787
double get_feed_constant()
Get and calculate the feed constant if set.
Definition cia402_drive.cc:1558
Cia402State
Definition cia402_drive.h:140
@ kQuickStopActive
Definition cia402_drive.h:146
@ kSwitchedOn
Definition cia402_drive.h:144
@ kReadySwitchOn
Definition cia402_drive.h:143
@ kFaultReactionActive
Definition cia402_drive.h:147
@ kNotReady
Definition cia402_drive.h:141
@ kOpEnabled
Definition cia402_drive.h:145
@ kFault
Definition cia402_drive.h:148
@ kSwitchOnDisabled
Definition cia402_drive.h:142
void quick_stop()
Request a quick stop on a device.
Definition cia402_drive.cc:429
uint16_t get_hardware_id() const
Get the hardware ID.
Definition cia402_drive.cc:58
OpMode
Definition cia402_drive.h:122
@ kPv
Definition cia402_drive.h:130
@ kCoggingCompensationRecording
Definition cia402_drive.h:126
@ kPt
Definition cia402_drive.h:131
@ kPp
Definition cia402_drive.h:128
@ kCsv
Definition cia402_drive.h:136
@ kTuning
Definition cia402_drive.h:123
@ kCst
Definition cia402_drive.h:137
@ kNo
Definition cia402_drive.h:127
@ kVel
Definition cia402_drive.h:129
@ kDiagnostics
Definition cia402_drive.h:125
@ kCsp
Definition cia402_drive.h:135
@ kHom
Definition cia402_drive.h:133
@ kIp
Definition cia402_drive.h:134
@ kRes
Definition cia402_drive.h:132
@ kOpenLoopFieldMode
Definition cia402_drive.h:124
std::variant< MotionMasterError, std::string > read_file(const std::string &file_name) const
Get the file content from a file from the flash memory.
Definition cia402_drive.cc:766
double get_gear_ratio()
Get the configured gear ratio.
Definition cia402_drive.cc:1520
int32_t get_current_velocity()
Get the current velocity.
Definition cia402_drive.cc:347
Definition motion_master_error.h:6
Definition virtual_device.h:22
Definition virtual_parameter.h:9
const char ** name
Definition co_dictionary.h:7
uint8_t * value
Definition co_dictionary.h:9
uint16_t index
Definition co_dictionary.h:0
#define OD_INDEX_ENCODER_1_CONFIGURATION
Definition dictionary_symbols.h:329
#define OD_INDEX_ENCODER_1_FEEDBACK
Definition dictionary_symbols.h:357
#define OD_INDEX_ENCODER_2_FEEDBACK
Definition dictionary_symbols.h:389
#define OD_INDEX_ENCODER_2_CONFIGURATION
Definition dictionary_symbols.h:361
Defines the parameters used to execute a chirp signal on a device.
Definition cia402_drive.h:56
int32_t targetAmplitude
Target amplitude of the chirp signal, expressed in permils (‰) of the rated torque.
Definition cia402_drive.h:102
int hrdStreamingDuration
How long to record high-resolution data during the chirp signal.
Definition cia402_drive.h:65
uint8_t startProcedure
When to start the chirp signal procedure.
Definition cia402_drive.h:94
int modesOfOperation
Modes of operation to set before starting the chirp signal procedure. If not provided,...
Definition cia402_drive.h:73
int32_t transitionTime
Duration of the frequency sweep, in milliseconds (ms), representing the transition time from start to...
Definition cia402_drive.h:117
int32_t targetFrequency
Target frequency of the chirp signal, in millihertz (mHz).
Definition cia402_drive.h:109
int32_t startFrequency
Start frequency of the chirp signal, in millihertz (mHz).
Definition cia402_drive.h:87
uint8_t signalType
Type of chirp signal to be used.
Definition cia402_drive.h:80