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
49 public:
50 enum class OpMode {
51 kTuning = -128,
53 kDiagnostics = -2,
55 kNo = 0,
56 kPp, // Profile position
57 kVel, // Velocity
58 kPv, // Profile velocity
59 kPt, // Profile torque
60 kRes, // Reserved
61 kHom, // Homing
62 kIp, // Interpolated position
63 kCsp, // Cyclic synchronous position
64 kCsv, // Cyclic synchronous velocity
65 kCst // Cyclic synchronous torque
66 };
67
68 enum class Cia402State {
69 kNotReady = 0,
76 kFault
77 };
78
79 enum class Cia402Command {
80 kNone = 0,
88 };
89
90 // map<encoder ordinal, encoder configuration index>
91 static inline std::map<uint8_t, uint16_t> fw_v5_encoder_configuration_map_ = {
94
95 // map<encoder ordinal, encoder feedback index>
96 static inline std::map<uint8_t, uint16_t> fw_v5_encoder_feedback_map_ = {
98
99 enum class EncoderFunction {
100 kDisabled,
106 kPosition,
108 kVelocity, // only for FW >= v5
109 kCommutationAndPosition // only for FW >= v5
110 };
111
112 // Controller (position, velocity, torque) to encoder function
113 static inline std::map<std::tuple<bool, bool, bool>, EncoderFunction>
115 {{false, false, false}, EncoderFunction::kDisabled},
116 {{false, false, true}, EncoderFunction::kCommutationOnly},
117 {{false, true, false}, EncoderFunction::kVelocity},
118 {{false, true, true}, EncoderFunction::kCommutationAndVelocity},
119 {{true, false, false}, EncoderFunction::kPosition},
120 {{true, false, true}, EncoderFunction::kCommutationAndPosition},
121 {{true, true, false}, EncoderFunction::kMotionControlFeedbackOnly},
122 {{true, true, true},
123 EncoderFunction::kCommutationAndMotionControlFeedback}};
124
125 enum class EncoderType {
126 kNone = 0,
127 kHallEncoder = 1,
128 kQeiEncoder = 2,
129 kBissEncoder = 4,
130 kRem14Encoder = 5,
131 kRem16MtEncoder = 6,
132 kSsiEncoder = 7
133 };
134
135 enum class BrakeStatus { kNotConfigured = 0, kEngaged = 1, kDisengaged = 2 };
136
141 };
142
143 explicit Cia402Drive(VirtualDevice* virtual_device);
144
150 [[nodiscard]] int32_t get_id() const;
151
157 [[nodiscard]] uint16_t get_hardware_id() const;
158
165
172
179
186
193
200
207
214
220 void set_halt_bit(bool value);
221
227 void set_target_position(int32_t position);
228
234 void set_target_velocity(int32_t velocity);
235
241 void set_target_torque(int32_t torque);
242
248 uint16_t get_statusword();
249
255 int32_t get_target_position();
256
262 int32_t get_target_velocity();
263
269 int32_t get_target_torque();
270
277
284
290 int32_t get_current_position();
291
297 int32_t get_current_velocity();
298
304 int32_t get_current_torque();
305
311 uint32_t get_current_timestamp();
312
322 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
323 int32_t value);
324
335 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
336 int32_t value, bool reload_configuration);
337
347 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
348 float value);
349
360 MotionMasterError set_parameter(uint16_t index, uint8_t subindex, float value,
361 bool reload_configuration);
362
372 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
373 double value);
374
385 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
386 double value, bool reload_configuration);
387
397 MotionMasterError set_parameter(uint16_t index, uint8_t subindex,
398 const std::string& value);
399
408 VirtualParameter* get_parameter(uint16_t index, uint8_t subindex);
409
423 MotionMasterError reload_drive(bool ignore_fault_reset_timeout = false);
424
430 [[nodiscard]] std::variant<MotionMasterError, std::list<std::string>>
431 get_file_list() const;
432
440 [[nodiscard]] std::variant<MotionMasterError, std::string> read_file(
441 const std::string& file_name) const;
442
451 MotionMasterError write_file(const std::string& name,
452 const std::string& content);
453
461 MotionMasterError remove_file(const std::string& file_name);
462
468 std::string get_log();
469
480 std::map<uint16_t, EncoderFunction> get_all_encoder_functions();
481
491 bool set_encoder_function(uint16_t index, EncoderFunction encoder_function);
492
500
508
517
529
538 double get_gear_ratio();
539
545 double get_feed_constant();
546
553 bool is_brake_engaged();
554
563
565
569 void quick_stop();
570
577
584
591
597 bool is_running();
598
604 bool get_fault_state();
605
613 MotionMasterError reset_fault(bool ignore_timeout_error = false);
614
620 bool get_warning_status();
621
629
637 bool is_operational();
638
648 Cia402State target_state);
649
658
668 std::vector<int16_t> get_cogging_torque_compensation_data();
669
679 MotionMasterError read_encoder_register(uint8_t encoder_ordinal,
680 uint8_t address, uint8_t& value);
681
691 MotionMasterError write_encoder_register(uint8_t encoder_ordinal,
692 uint8_t address, uint8_t value);
693
701 std::tuple<uint8_t, uint8_t, uint8_t> get_controller_encoder_sources();
702
703 private:
704 VirtualDevice* virtual_device_;
705
709 void refresh_current_position();
710
716 void set_controlword(uint16_t controlword);
717
723 void set_op_mode(OpMode op_mode);
724
732 [[nodiscard]] Cia402State cia402_read_state(uint16_t statusword) const;
733
742 static uint16_t cia402_command(Cia402Command command, uint16_t controlword);
743
754 static uint16_t cia402_go_to_state(Cia402State target_state,
755 Cia402State current_state,
756 uint16_t controlword,
757 bool skip_states = true);
758
764 std::vector<VirtualParameter*> get_sensor_port_virtual_parameters();
765};
Definition: cia402_drive.h:48
static std::map< uint8_t, uint16_t > fw_v5_encoder_feedback_map_
Definition: cia402_drive.h:96
bool get_internal_limit_reached()
Check if the internal limit reached statusword bit is active.
Definition: cia402_drive.cc:625
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:537
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:874
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:811
static std::map< uint8_t, uint16_t > fw_v5_encoder_configuration_map_
Definition: cia402_drive.h:91
uint16_t get_statusword()
Get the raw statusword.
Definition: cia402_drive.cc:279
EncoderFunction
Definition: cia402_drive.h:99
bool is_running()
Check if the device is currently running.
Definition: cia402_drive.cc:527
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:1914
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:739
MotionMasterError quick_stop_blocking()
Request an asynchronous quick stop on a device.
Definition: cia402_drive.cc:432
void reset_torque_target_and_offset()
Definition: cia402_drive.cc:382
static std::map< std::tuple< bool, bool, bool >, EncoderFunction > controller_to_encoder_function_map_
Definition: cia402_drive.h:114
MotionMasterError init_slave_profile_position()
Initialize the position profile on the slave.
Definition: cia402_drive.cc:69
BrakeStatus
Definition: cia402_drive.h:135
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:369
Cia402Command
Definition: cia402_drive.h:79
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:631
EncoderType
Definition: cia402_drive.h:125
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:718
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:714
MotionMasterError engage_brake(bool value)
Apply or release the brake.
Definition: cia402_drive.cc:1602
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:356
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:956
BrakeReleaseStrategy
Definition: cia402_drive.h:137
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:295
std::vector< int16_t > get_cogging_torque_compensation_data()
Get the cogging torque compensation values.
Definition: cia402_drive.cc:791
bool get_fault_state()
Get the current fault state.
Definition: cia402_drive.cc:531
int32_t load_configuration_from_flash()
Load the configuration from the flash memory.
Definition: cia402_drive.cc:503
bool get_warning_status()
Get the current warning status.
Definition: cia402_drive.cc:621
EncoderType get_commutation_encoder_type()
Get the type of the encoder set as the commutation encoder.
Definition: cia402_drive.cc:1190
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:1441
MotionMasterError write_file(const std::string &name, const std::string &content)
Write the file to the flash memory.
Definition: cia402_drive.cc:754
MotionMasterError remove_file(const std::string &file_name)
Remove a file from the flash memory.
Definition: cia402_drive.cc:760
bool is_brake_engaged()
Check if the brake is currently engaged.
Definition: cia402_drive.cc:1585
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:839
bool is_operational()
Check if the device is operational.
Definition: cia402_drive.cc:629
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:1002
int32_t get_current_position()
Get the current position.
Definition: cia402_drive.cc:330
int32_t save_current_configuration_to_flash()
Save the current device configuration to the flash memory.
Definition: cia402_drive.cc:479
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:1358
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:1272
std::string get_log()
Get the slave log from the flash memory.
Definition: cia402_drive.cc:764
double get_feed_constant()
Get and calculate the feed constant if set.
Definition: cia402_drive.cc:1534
Cia402State
Definition: cia402_drive.h:68
void quick_stop()
Request a quick stop on a device.
Definition: cia402_drive.cc:416
uint16_t get_hardware_id() const
Get the hardware ID.
Definition: cia402_drive.cc:58
OpMode
Definition: cia402_drive.h:50
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:743
double get_gear_ratio()
Get the configured gear ratio.
Definition: cia402_drive.cc:1497
int32_t get_current_velocity()
Get the current velocity.
Definition: cia402_drive.cc:343
Definition: motion_master_error.h:6
Definition: virtual_device.h:20
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