ethercatcpp
cia402_device.h
Go to the documentation of this file.
1 /* File: cia402_device.h
2  * This file is part of the program ethercatcpp-core
3  * Program description : EtherCAT driver libraries for UNIX
4  * Copyright (C) 2017-2024 - Robin Passama (LIRMM / CNRS) Arnaud Meline
5  * (LIRMM / CNRS) Benjamin Navarro (LIRMM / CNRS). All Right reserved.
6  *
7  * This software is free software: you can redistribute it and/or modify
8  * it under the terms of the CeCILL-C license as published by
9  * the CEA CNRS INRIA, either version 1
10  * of the License, or (at your option) any later version.
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * CeCILL-C License for more details.
15  *
16  * You should have received a copy of the CeCILL-C License
17  * along with this software. If not, it can be found on the official
18  * website of the CeCILL licenses family (http://www.cecill.info/index.en.html).
19  */
27 #pragma once
28 
31 
33 
35 public:
41  explicit CIA402ObjectDictionary(
42  std::initializer_list<std::pair<std::string_view, entry>>);
43 
44  ~CIA402ObjectDictionary() = default;
45 };
46 
48 
55 class CIA402Device : public SlaveDevice {
56 public:
57  // cia402 object dictionary default codes
58  static constexpr const uint16_t controlword_addr = 0x6040;
59  static constexpr const uint16_t statusword_addr = 0x6041;
60  static constexpr const uint16_t set_modes_of_operation_addr = 0x6060;
61  static constexpr const uint16_t get_modes_of_operation_addr = 0x6061;
62 
63  // flags to check operation state in statusword
64  static constexpr const uint16_t not_ready_to_switch_on_code = 0x0000;
65  static constexpr const uint16_t switch_on_disabled_code = 0x0040;
66  static constexpr const uint16_t ready_to_switch_on_code = 0x0021;
67  static constexpr const uint16_t switched_on_code = 0x0023;
68  static constexpr const uint16_t operation_enabled_code = 0x0027;
69  static constexpr const uint16_t quick_stop_active_code = 0x0007;
70  static constexpr const uint16_t fault_reaction_active_code = 0x000F;
71  static constexpr const uint16_t fault_code = 0x0008;
72 
73  // values for CIA402 standard operation modes
74  static constexpr const int8_t monitor_mode = 0;
75  static constexpr const int8_t PP_mode = 1;
76  static constexpr const int8_t PV_mode = 3;
77  static constexpr const int8_t PT_mode = 4;
78  static constexpr const int8_t HOMING_mode = 6;
79  static constexpr const int8_t IP_mode = 7;
80  static constexpr const int8_t CSP_mode = 8;
81  static constexpr const int8_t CSV_mode = 9;
82  static constexpr const int8_t CST_mode = 10;
83 
88  enum state_t {
98  };
99 
104  enum control_t {
113  };
114 
115  static const std::map<state_t, std::string_view> device_state_decode_;
116 
124  static void set_control_word(uint16_t& controlword, control_t ctrl);
125 
132  static state_t operation_state(uint16_t statusword);
133 
134  CIA402Device(uint16_t& control_word, uint16_t& status_word,
135  int8_t& mode_set, int8_t& mode_read);
136 
137  virtual bool profile_mode() const;
138  virtual bool cyclic_mode() const;
139  virtual bool position_mode() const;
140  virtual bool velocity_mode() const;
141  virtual bool torque_mode() const;
142  virtual bool profile_mode_queried() const;
143  virtual bool cyclic_mode_queried() const;
144  virtual bool position_mode_queried() const;
145  virtual bool velocity_mode_queried() const;
146  virtual bool torque_mode_queried() const;
147 
148  CIA402Device();
149 
150  virtual ~CIA402Device();
151 
156  state_t operation_state() const;
157 
162  std::string_view operation_state_str() const;
163 
169  static std::string_view operation_state_to_str(state_t);
170 
176  static std::string_view control_mode_to_str(int8_t);
177 
185  void acknowledge_fault();
186 
190  void quick_stop();
191 
197  bool voltage_enabled() const;
198 
204  bool fault_alert() const;
205 
211  bool warning_alert() const;
212 
217  bool is_configurable() const;
218 
231  bool process_state();
232 
240  void startup();
241 
249  void finish();
250 
258  void pause(bool doit);
259 
266  bool active() const;
267 
272  bool paused() const;
273 
274  // mode specific functions
280  bool target_reached() const;
281 
287  bool manage_new_setpoint();
288 
294  bool motion_halted() const;
295 
301  void halt_motion(bool choice);
302 
308  bool new_setpoint_following() const;
309 
315  bool internal_limit() const;
316 
324  void reset_fault();
325 
326 protected:
331  void set_operation_state(control_t state);
332 
340  void set_quick_stop_reaction(std::function<void()> reaction);
341 
350  std::function<bool()> check_quick_stop);
351 
359  void set_entering_enable_reaction(std::function<bool()> reaction);
360 
367  void set_entering_switchon_reaction(std::function<void()> reaction);
368 
375  void set_entering_disabled_reaction(std::function<void()> reaction);
376 
385  void initialize_state_variables(uint16_t& control_word,
386  uint16_t& status_word, int8_t& mode_set,
387  int8_t& mode_read);
388 
397  void initialize_state_variables(uint16_t* control_word,
398  uint16_t* status_word, int8_t* mode_set,
399  int8_t* mode_read);
400 
409  void initialize_state_variables(uint8_t* control_word_addr,
410  uint8_t* status_word_addr,
411  uint8_t* mode_set_addr,
412  uint8_t* mode_read_addr);
413 
420  int8_t current_control_mode() const;
421 
428 
435 
442  void managed_set_point();
443 
444  static const std::map<int8_t, std::string_view> std_control_mode_str;
445  static bool profile_mode(int8_t);
446  static bool cyclic_mode(int8_t);
447  static bool position_mode(int8_t);
448  static bool velocity_mode(int8_t);
449  static bool torque_mode(int8_t);
450 
451 private:
452  bool active_;
453  bool paused_;
456  READY_TO_ACCEPT, // control (bit 4,5=0) status (bit 12=0)
457  SET_POINT_SET, // control (bit 4,5=1) status (bit 12=0)
458  ACCEPTED_SET_POINT // control (bit 4,5=1) status (bit 12=1)
459  };
462  uint16_t* control_word_;
463  uint16_t* status_word_;
464  int8_t* mode_set_;
465  int8_t* mode_read_;
466  std::function<void()> quick_stop_reaction_;
468  std::function<bool()> entering_enable_reaction_;
469  std::function<void()> entering_switch_on_reaction_;
471 
472  void reset_set_point();
473  void follow_set_point();
474 };
475 } // namespace ethercatcpp::coe::cia402
ethercatcpp::coe::cia402::CIA402Device::quick_stop_active_code
static constexpr const uint16_t quick_stop_active_code
Definition: cia402_device.h:69
ethercatcpp::coe::cia402::CIA402Device::current_control_mode
int8_t current_control_mode() const
Utility function for implementing the control_mode() function of derived classes.
ethercatcpp::coe::cia402::CIA402Device::voltage_enabled
bool voltage_enabled() const
Tell whether device is powered with high voltage.
ethercatcpp::coe::cia402::CIA402Device::set_point_defined_
bool set_point_defined_
Definition: cia402_device.h:461
ethercatcpp::coe::cia402::CIA402Device::active_
bool active_
Definition: cia402_device.h:452
ethercatcpp::coe::cia402::CIA402Device::not_ready_to_switch_on
@ not_ready_to_switch_on
Definition: cia402_device.h:90
ethercatcpp::coe::cia402::CIA402Device::finish
void finish()
Put the sensojoint is a safe, configuration possible, state.
ethercatcpp::coe::cia402::CIA402Device::control_mode_to_str
static std::string_view control_mode_to_str(int8_t)
convert the control mode into a human readable information
ethercatcpp::coe::cia402::CIA402Device::PP_mode
static constexpr const int8_t PP_mode
Profile position.
Definition: cia402_device.h:75
ethercatcpp::coe::cia402::CIA402Device::fault_reset_op
@ fault_reset_op
Command to active state transition 15.
Definition: cia402_device.h:112
ethercatcpp::coe::cia402::CIA402Device::position_mode
virtual bool position_mode() const
ethercatcpp::coe::cia402::CIA402Device::not_ready_to_switch_on_code
static constexpr const uint16_t not_ready_to_switch_on_code
Definition: cia402_device.h:64
ethercatcpp::coe::cia402::CIA402Device::set_quick_stop_reaction
void set_quick_stop_reaction(std::function< void()> reaction)
Set the quick stop reaction function called when a quick stop procedure starts.
ethercatcpp::coe::cia402::CIA402Device::set_control_word
static void set_control_word(uint16_t &controlword, control_t ctrl)
Set the control word of a CiA402 device according to the desired control state.
ethercatcpp::coe::cia402::CIA402Device::leaving_enabled_requires_quick_stop_
std::function< bool()> leaving_enabled_requires_quick_stop_
Definition: cia402_device.h:467
slave_device.h
Header file for SlaveDevice class.
ethercatcpp::coe::cia402::CIA402Device::mode_set_
int8_t * mode_set_
Definition: cia402_device.h:464
ethercatcpp::coe::cia402::CIA402Device::go_back_to_monitor_mode_
bool go_back_to_monitor_mode_
Definition: cia402_device.h:454
ethercatcpp::coe::cia402::CIA402Device::operation_enabled
@ operation_enabled
Definition: cia402_device.h:94
ethercatcpp::coe::cia402::CIA402Device::ACCEPTED_SET_POINT
@ ACCEPTED_SET_POINT
Definition: cia402_device.h:458
ethercatcpp::coe::cia402::CIA402Device::quick_stop_reaction_
std::function< void()> quick_stop_reaction_
Definition: cia402_device.h:466
ethercatcpp::coe::cia402::CIA402Device::fault_alert
bool fault_alert() const
Tell whether device reports an error.
ethercatcpp::coe::cia402::CIA402Device::profile_mode
virtual bool profile_mode() const
ethercatcpp::coe::cia402::CIA402Device::unknown_state
@ unknown_state
Definition: cia402_device.h:89
ethercatcpp::coe::cia402::CIA402Device::mode_read_
int8_t * mode_read_
Definition: cia402_device.h:465
ethercatcpp::coe::cia402::CIA402Device::fault_code
static constexpr const uint16_t fault_code
Definition: cia402_device.h:71
ethercatcpp::coe::cia402::CIA402Device::control_t
control_t
Possible control over a CiA402 device state machine.
Definition: cia402_device.h:104
ethercatcpp::coe::cia402::CIA402Device::initialize_state_variables
void initialize_state_variables(uint16_t &control_word, uint16_t &status_word, int8_t &mode_set, int8_t &mode_read)
Set the variables used to control the state machine.
ethercatcpp::coe::cia402::CIA402ObjectDictionary::~CIA402ObjectDictionary
~CIA402ObjectDictionary()=default
ethercatcpp::coe::cia402::CIA402Device::active
bool active() const
Tell whether the sensojoint is active (is running or will run commands) or not (is or will be in a sa...
ethercatcpp::coe::cia402::CIA402Device::CST_mode
static constexpr const int8_t CST_mode
Cyclic synchronous torque.
Definition: cia402_device.h:82
ethercatcpp::coe::cia402::CIA402Device::reset_set_point
void reset_set_point()
ethercatcpp::coe::cia402::CIA402Device::follow_set_point
void follow_set_point()
ethercatcpp::coe::cia402::CIA402Device::control_word_
uint16_t * control_word_
Definition: cia402_device.h:462
ethercatcpp::coe::cia402::CIA402Device::enable_op
@ enable_op
Command to active state transition 4, 16.
Definition: cia402_device.h:111
ethercatcpp::coe::cia402::CIA402Device::set_entering_enable_reaction
void set_entering_enable_reaction(std::function< bool()> reaction)
Set the reaction function called when device enters the operation enabled state.
ethercatcpp::coe::cia402::CIA402Device::fault_reaction_active
@ fault_reaction_active
Definition: cia402_device.h:96
ethercatcpp::coe::cia402::CIA402Device::switched_on
@ switched_on
Definition: cia402_device.h:93
ethercatcpp::coe::cia402::CIA402Device::is_configurable
bool is_configurable() const
Tell whether the sensojoint can be immediately configured.
ethercatcpp::coe::cia402::CIA402Device::paused_
bool paused_
Definition: cia402_device.h:453
ethercatcpp::coe::cia402::CIA402Device::position_mode_queried
virtual bool position_mode_queried() const
ethercatcpp::coe::cia402::CIA402Device::get_modes_of_operation_addr
static constexpr const uint16_t get_modes_of_operation_addr
Definition: cia402_device.h:61
ethercatcpp::coe::cia402::CIA402Device
A container of static functions and types used to ease implementation of CiA 402 devices.
Definition: cia402_device.h:55
ethercatcpp::coe::cia402::CIA402Device::setpoint_management_
SetPointCtrl setpoint_management_
Definition: cia402_device.h:460
ethercatcpp::coe::cia402
Definition: cia402_device.h:32
ethercatcpp::coe::cia402::CIA402Device::PV_mode
static constexpr const int8_t PV_mode
Profile velocity.
Definition: cia402_device.h:76
ethercatcpp::coe::cia402::CIA402Device::switch_on_disabled_code
static constexpr const uint16_t switch_on_disabled_code
Definition: cia402_device.h:65
ethercatcpp::coe::cia402::CIA402Device::pause
void pause(bool doit)
State transition is paused/unpaused.
ethercatcpp::coe::cia402::CIA402Device::monitor_mode
static constexpr const int8_t monitor_mode
Definition: cia402_device.h:74
ethercatcpp::coe::cia402::CIA402Device::set_modes_of_operation_addr
static constexpr const uint16_t set_modes_of_operation_addr
Definition: cia402_device.h:60
ethercatcpp::coe::cia402::CIA402Device::operation_state_str
std::string_view operation_state_str() const
get string representation of current state of the device.
ethercatcpp::coe::cia402::CIA402Device::switch_on_and_enable_op
@ switch_on_and_enable_op
Command to active state transition 3, 4.
Definition: cia402_device.h:107
ethercatcpp::coe::cia402::CIA402Device::velocity_mode
virtual bool velocity_mode() const
ethercatcpp::coe::cia402::CIA402Device::set_operation_state
void set_operation_state(control_t state)
Function used to change the state of device.
ethercatcpp::coe::cia402::CIA402Device::device_state_termination
void device_state_termination()
Performs operations supposed to put the device in a safe state.
coe_utilities.h
Main header file for CoE utilities.
ethercatcpp::coe::cia402::CIA402Device::motion_halted
bool motion_halted() const
Tell whether motion generated by a profile mode is halted.
ethercatcpp::coe::cia402::CIA402Device::warning_alert
bool warning_alert() const
Tell whether device reports a warning.
ethercatcpp::coe::cia402::CIA402Device::cyclic_mode_queried
virtual bool cyclic_mode_queried() const
ethercatcpp::coe::cia402::CIA402Device::state_t
state_t
Possible state of a CiA402 device state machine.
Definition: cia402_device.h:88
ethercatcpp::coe::cia402::CIA402Device::velocity_mode_queried
virtual bool velocity_mode_queried() const
ethercatcpp::coe::cia402::CIA402Device::paused
bool paused() const
Tell whether the device state transition is paused.
ethercatcpp::coe::cia402::CIA402Device::manage_new_setpoint
bool manage_new_setpoint()
Make the controller manage a new setpoint (e.g. value of target position). Only in profile mode.
ethercatcpp::coe::cia402::CIA402Device::ready_to_switch_on_code
static constexpr const uint16_t ready_to_switch_on_code
Definition: cia402_device.h:66
ethercatcpp::coe::cia402::CIA402Device::switch_on
@ switch_on
Command to active state transition 3.
Definition: cia402_device.h:106
ethercatcpp::coe::cia402::CIA402Device::operation_enabled_code
static constexpr const uint16_t operation_enabled_code
Definition: cia402_device.h:68
ethercatcpp::coe::cia402::CIA402Device::set_entering_switchon_reaction
void set_entering_switchon_reaction(std::function< void()> reaction)
Set the reaction function called when device enters the switched on state.
ethercatcpp::coe::cia402::CIA402Device::quickstop
@ quickstop
Command to active state transition 11.
Definition: cia402_device.h:109
ethercatcpp::coe::cia402::CIA402Device::entering_switch_on_reaction_
std::function< void()> entering_switch_on_reaction_
Definition: cia402_device.h:469
ethercatcpp::coe::cia402::CIA402Device::controlword_addr
static constexpr const uint16_t controlword_addr
Definition: cia402_device.h:58
ethercatcpp::coe::cia402::CIA402Device::startup
void startup()
Make sensojoint controlable and control mode cannot be changed.
ethercatcpp::coe::cia402::CIA402ObjectDictionary::CIA402ObjectDictionary
CIA402ObjectDictionary()
ethercatcpp::coe::cia402::CIA402Device::status_word_
uint16_t * status_word_
Definition: cia402_device.h:463
ethercatcpp::coe::cia402::CIA402Device::IP_mode
static constexpr const int8_t IP_mode
Profile interpolated.
Definition: cia402_device.h:79
ethercatcpp::coe::cia402::CIA402Device::operation_state
state_t operation_state() const
get current state of the device.
ethercatcpp::coe::cia402::CIA402ObjectDictionary
Definition: cia402_device.h:34
ethercatcpp::coe::cia402::CIA402Device::internal_limit
bool internal_limit() const
In profile modes tell whether there is an internal limit has been reached.
ethercatcpp::coe::cia402::CIA402Device::disable_voltage
@ disable_voltage
Command to active state transition 7, 9, 10, 12.
Definition: cia402_device.h:108
ethercatcpp::coe::cia402::CIA402Device::disable_op
@ disable_op
Command to active state transition 5.
Definition: cia402_device.h:110
ethercatcpp::coe::cia402::CIA402Device::PT_mode
static constexpr const int8_t PT_mode
Profile torque.
Definition: cia402_device.h:77
ethercatcpp::coe::cia402::CIA402Device::SET_POINT_SET
@ SET_POINT_SET
Definition: cia402_device.h:457
ethercatcpp::coe::cia402::CIA402Device::std_control_mode_str
static const std::map< int8_t, std::string_view > std_control_mode_str
Definition: cia402_device.h:444
ethercatcpp::coe::cia402::CIA402Device::acknowledge_fault
void acknowledge_fault()
Acknowledge fault and reset faulty status.
ethercatcpp::coe::cia402::CIA402Device::reset_fault
void reset_fault()
Reset fault so that the device can start again.
ethercatcpp::coe::cia402::CIA402Device::cyclic_mode
virtual bool cyclic_mode() const
ethercatcpp::coe::cia402::CIA402Device::target_reached
bool target_reached() const
Tell whether the target given by a profile mode has been reached.
ethercatcpp::coe::cia402::CIA402Device::switched_on_code
static constexpr const uint16_t switched_on_code
Definition: cia402_device.h:67
ethercatcpp::coe::cia402::CIA402Device::quick_stop_active
@ quick_stop_active
Definition: cia402_device.h:95
ethercatcpp::coe::cia402::CIA402Device::READY_TO_ACCEPT
@ READY_TO_ACCEPT
Definition: cia402_device.h:456
ethercatcpp::coe::cia402::CIA402Device::managed_set_point
void managed_set_point()
Ask the device to take into account the new set point given. Only in profile position modes.
ethercatcpp::coe::cia402::CIA402Device::torque_mode_queried
virtual bool torque_mode_queried() const
ethercatcpp::coe::cia402::CIA402Device::SetPointCtrl
SetPointCtrl
Definition: cia402_device.h:455
ethercatcpp::coe::cia402::CIA402Device::statusword_addr
static constexpr const uint16_t statusword_addr
Definition: cia402_device.h:59
ethercatcpp::coe::cia402::CIA402Device::shutdown
@ shutdown
Command to active state transition 2, 6, 8.
Definition: cia402_device.h:105
ethercatcpp::coe::cia402::CIA402Device::HOMING_mode
static constexpr const int8_t HOMING_mode
Homing.
Definition: cia402_device.h:78
ethercatcpp::coe::cia402::CIA402Device::torque_mode
virtual bool torque_mode() const
ethercatcpp::coe::cia402::CIA402Device::device_state_initialization
void device_state_initialization()
Performs operations supposed to put the device in a safe state.
ethercatcpp::coe::cia402::CIA402Device::halt_motion
void halt_motion(bool choice)
Halt motor axle, only in profile mode.
ethercatcpp::coe::cia402::CIA402Device::profile_mode_queried
virtual bool profile_mode_queried() const
ethercatcpp::coe::cia402::CIA402Device::new_setpoint_following
bool new_setpoint_following() const
Tell whether a new set point is tracked. Only in profile position mode.
ethercatcpp::coe::cia402::CIA402Device::CSP_mode
static constexpr const int8_t CSP_mode
Cyclic synchronous position.
Definition: cia402_device.h:80
ethercatcpp::coe::cia402::CIA402Device::switch_on_disabled
@ switch_on_disabled
Definition: cia402_device.h:91
ethercatcpp::coe::cia402::CIA402Device::set_entering_disabled_reaction
void set_entering_disabled_reaction(std::function< void()> reaction)
Set the reaction function called when device enters the switched on disabled state.
ethercatcpp::coe::cia402::CIA402Device::operation_state_to_str
static std::string_view operation_state_to_str(state_t)
convert the operation state into a human readable information
ethercatcpp::coe::cia402::CIA402Device::entering_switch_on_disabled_reaction_
std::function< void()> entering_switch_on_disabled_reaction_
Definition: cia402_device.h:470
ethercatcpp::coe::cia402::CIA402Device::entering_enable_reaction_
std::function< bool()> entering_enable_reaction_
Definition: cia402_device.h:468
ethercatcpp::coe::cia402::CIA402Device::set_leaving_enable_requires_quick_stop
void set_leaving_enable_requires_quick_stop(std::function< bool()> check_quick_stop)
Set the check function called when device leaves the operation enabled state.
ethercatcpp::coe::cia402::CIA402Device::process_state
bool process_state()
Perform adequate operations depending on state.
ethercatcpp::SlaveDevice
This class define an EtherCAT unit device.
Definition: slave_device.h:53
ethercatcpp::coe::cia402::CIA402Device::fault_reaction_active_code
static constexpr const uint16_t fault_reaction_active_code
Definition: cia402_device.h:70
ethercatcpp::coe::cia402::CIA402Device::CIA402Device
CIA402Device()
ethercatcpp::coe::cia402::CIA402Device::faulty
@ faulty
Definition: cia402_device.h:97
ethercatcpp::coe::cia402::CIA402Device::quick_stop
void quick_stop()
Launch the quick stop procedure.
ethercatcpp::coe::ObjectDictionary
represent the CoE dictionnary of a device
Definition: coe_utilities.h:67
ethercatcpp::coe::cia402::CIA402Device::~CIA402Device
virtual ~CIA402Device()
ethercatcpp::coe::cia402::CIA402Device::ready_to_switch_on
@ ready_to_switch_on
Definition: cia402_device.h:92
ethercatcpp::coe::cia402::CIA402Device::CSV_mode
static constexpr const int8_t CSV_mode
Cyclic synchronous velocity.
Definition: cia402_device.h:81
ethercatcpp::coe::cia402::CIA402Device::device_state_decode_
static const std::map< state_t, std::string_view > device_state_decode_
Definition: cia402_device.h:115