YARP CiA-402 EtherCAT Device
0.6.0
YARP device plugin for EtherCAT CiA-402 drives
Toggle main menu visibility
Loading...
Searching...
No Matches
CiA402StateMachine.h
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: Fondazione Istituto Italiano di Tecnologia (IIT)
2
// SPDX-License-Identifier: BSD-3-Clause
3
//
4
//
5
// CiA402StateMachine │
6
//
7
// A *header‑only* helper that drives the CiA‑402 power‑state machine and the
8
// “dynamic Op‑Mode switching” logic for a single EtherCAT drive.
9
//
10
// Usage (pseudo‑code inside your cyclic task)
11
//
12
// CiA402StateMachine sm;
13
// ...
14
// sm.setRequestedOpMode(ciaOpValue); // whenever the user changes mode
15
// auto cmd = sm.update(tx.Statusword, tx.OpModeDisplay);
16
// rx.Controlword = cmd.controlword;
17
// if (cmd.writeOpMode) rx.OpMode = cmd.opMode;
18
//
19
// The class keeps the power stage enabled whenever the drive is already in
20
// *Operation‑enabled* and the only thing that changes is the mode (object 0x6060).
21
// Otherwise it gracefully walks through Shutdown → Switched‑on → Operation
22
// following the CiA‑402 specification, waiting for each state to be *reached*
23
// before sending the next control‑word.
24
//
25
26
#ifndef YARP_DEV_CIA402_STATE_MACHINE_H
27
#define YARP_DEV_CIA402_STATE_MACHINE_H
28
29
#include <cstdint>
30
#include <memory>
31
#include <string_view>
32
33
namespace
CiA402
34
{
35
36
enum class
State
: uint8_t
37
{
38
NotReadyToSwitchOn
= 0,
// 0x0000
39
SwitchOnDisabled
= 1,
// 0x0040
40
ReadyToSwitchOn
= 2,
// 0x0021
41
SwitchedOn
= 3,
// 0x0023
42
OperationEnabled
= 4,
// 0x0027
43
QuickStopActive
= 5,
// 0x0007
44
FaultReaction
= 6,
// 0x000F
45
Fault
= 7,
// 0x0008
46
Unknown
= 8
47
};
48
49
/** Convert a raw status-word to the canonical CiA-402 state.
50
*
51
* Only the relevant bits (0,1,2,3,4,5,6) are masked, exactly as the spec does.
52
* Patterns are taken from IEC 61800-7-204, table 13.
53
*/
54
static
inline
constexpr
State
sw_to_state
(uint16_t sw)
55
{
56
const
uint16_t x = sw & 0x006F;
// keep bits 0,1,2,4,5,6
57
const
uint16_t y = sw & 0x004F;
// same, but without bit 5 (QS)
58
59
if
(x == 0x0000)
60
return
State::NotReadyToSwitchOn
;
// 0000 0000 0000 0000
61
if
(y == 0x0040)
62
return
State::SwitchOnDisabled
;
// 0100 0000
63
if
(x == 0x0021)
64
return
State::ReadyToSwitchOn
;
// 0010 0001
65
if
(x == 0x0023)
66
return
State::SwitchedOn
;
// 0010 0011
67
if
(x == 0x0027)
68
return
State::OperationEnabled
;
// 0010 0111
69
if
(x == 0x0007)
70
return
State::QuickStopActive
;
// 0000 0111
71
if
(y == 0x000F)
72
return
State::FaultReaction
;
// 0000 1111 (ignore QS)
73
if
(y == 0x0008)
74
return
State::Fault
;
// 0000 1000
75
return
State::Unknown
;
76
}
77
78
// Only for debugging
79
static
inline
constexpr
std::string_view
state_to_string
(
State
s)
80
{
81
switch
(s)
82
{
83
case
State::SwitchOnDisabled
:
84
return
"SwitchOnDisabled"
;
85
case
State::ReadyToSwitchOn
:
86
return
"ReadyToSwitchOn"
;
87
case
State::SwitchedOn
:
88
return
"SwitchedOn"
;
89
case
State::OperationEnabled
:
90
return
"OperationEnabled"
;
91
case
State::QuickStopActive
:
92
return
"QuickStopActive"
;
93
case
State::FaultReaction
:
94
return
"FaultReaction"
;
95
case
State::Fault
:
96
return
"Fault"
;
97
default
:
98
return
"Unknown"
;
99
}
100
}
101
102
// ────────────────────────────────────────────────────────────────────────────
103
// The state‑machine class
104
// ────────────────────────────────────────────────────────────────────────────
105
class
StateMachine
106
{
107
public
:
108
/**
109
* @brief Command structure.
110
*
111
* This structure contains the control word, operation mode, and a flag indicating
112
* if the operation mode should be written.
113
*/
114
struct
Command
115
{
116
uint16_t
controlword
;
///< Control word to send to the drive.
117
int8_t
opMode
;
///< Operation mode to send to the drive.
118
bool
writeOpMode
;
///< Flag indicating if the operation mode should be written.
119
};
120
121
/**
122
* @brief Constructor.
123
*
124
* Initializes the CiA-402 state machine for a single EtherCAT drive.
125
*/
126
StateMachine
();
127
/**
128
* @brief Destructor.
129
*
130
* Cleans up the resources used by the state machine.
131
*/
132
~StateMachine
();
133
134
/**
135
* @brief Resets the internal state machine.
136
*
137
* Call this after power-on or after a drive fault to reinitialize the state machine.
138
*/
139
void
reset
();
140
141
/**
142
* @brief Computes the next control word and operation mode to send to the drive.
143
*
144
* This function should be called cyclically. It determines the correct control word and
145
* (optionally) the operation mode to send, based on the current statusword and opModeDisplay.
146
*
147
* @param statusword Value read from TxPDO.Statusword.
148
* @param opModeDisplay Value read from TxPDO.OpModeDisplay (object 0x6061).
149
* @param opReq Requested operation mode (object 0x6060).
150
* @param hwInhibit Hardware inhibit state (true if inhibited, false otherwise).
151
* @return Command structure containing the control word, operation mode, and a flag indicating
152
* if the opMode should be written.
153
*/
154
Command
update
(uint16_t statusword, int8_t opModeDisplay, int8_t opReq,
bool
hwInhibit);
155
156
int8_t
getActiveOpMode
() const noexcept;
157
158
/**
159
* @brief Generates a fault reset command.
160
*
161
* This function generates a command to reset the fault state of the drive.
162
*
163
* @return Command structure containing the control word and operation mode for fault reset.
164
*/
165
Command
faultReset
() noexcept;
166
167
/**
168
* Check if the operation is enabled.
169
* @param sw Status word.
170
* @return true if the operation is enabled, false otherwise.
171
*/
172
static inline
bool
isOpEnabled
(uint16_t sw)
173
{
174
return
(sw & 0x0004) != 0;
175
}
176
177
/**
178
* Check if the drive is in a fault state.
179
* @param sw Status word.
180
* @return true if the drive is in a fault state, false otherwise.
181
*/
182
static
inline
bool
isFault
(uint16_t sw)
183
{
184
return
sw_to_state
(sw) ==
State::Fault
;
185
}
186
187
/**
188
* Check if the fault reaction is active.
189
* @param sw Status word.
190
* @return true if the fault reaction is active, false otherwise.
191
*/
192
static
inline
bool
isFaultReactionActive
(uint16_t sw)
193
{
194
return
sw_to_state
(sw) ==
State::FaultReaction
;
195
}
196
197
/**
198
* Check if the drive is not ready to switch on.
199
* @param sw Status word.
200
* @return true if the drive is not ready to switch on, false otherwise.
201
*/
202
static
inline
bool
isQuickStopActive
(uint16_t sw)
203
{
204
return
sw_to_state
(sw) ==
State::QuickStopActive
;
205
}
206
207
/**
208
* Check if the drive is switched on.
209
* @param sw Status word.
210
* @return true if the drive is switched on, false otherwise.
211
*/
212
static
inline
bool
isOperationEnabled
(uint16_t sw)
213
{
214
return
sw_to_state
(sw) ==
State::OperationEnabled
;
215
}
216
217
/**
218
* Check if the drive is switched on.
219
* @param sw Status word.
220
* @return true if the drive is switched on, false otherwise.
221
*/
222
static
inline
bool
isSwitchedOn
(uint16_t sw)
223
{
224
return
sw_to_state
(sw) ==
State::SwitchedOn
;
225
}
226
227
private
:
228
struct
Impl;
229
std::unique_ptr<Impl>
m_impl;
230
};
231
232
}
// namespace CiA402
233
234
#endif
// YARP_DEV_CIA402_STATE_MACHINE_H
CiA402::StateMachine::isOperationEnabled
static bool isOperationEnabled(uint16_t sw)
Definition
CiA402StateMachine.h:212
CiA402::StateMachine::update
Command update(uint16_t statusword, int8_t opModeDisplay, int8_t opReq, bool hwInhibit)
Computes the next control word and operation mode to send to the drive.
Definition
CiA402StateMachine.cpp:40
CiA402::StateMachine::isQuickStopActive
static bool isQuickStopActive(uint16_t sw)
Definition
CiA402StateMachine.h:202
CiA402::StateMachine::isOpEnabled
static bool isOpEnabled(uint16_t sw)
Definition
CiA402StateMachine.h:172
CiA402::StateMachine::~StateMachine
~StateMachine()
Destructor.
CiA402::StateMachine::isFault
static bool isFault(uint16_t sw)
Definition
CiA402StateMachine.h:182
CiA402::StateMachine::reset
void reset()
Resets the internal state machine.
Definition
CiA402StateMachine.cpp:29
CiA402::StateMachine::faultReset
Command faultReset() noexcept
Generates a fault reset command.
Definition
CiA402StateMachine.cpp:133
CiA402::StateMachine::isSwitchedOn
static bool isSwitchedOn(uint16_t sw)
Definition
CiA402StateMachine.h:222
CiA402::StateMachine::isFaultReactionActive
static bool isFaultReactionActive(uint16_t sw)
Definition
CiA402StateMachine.h:192
CiA402::StateMachine::getActiveOpMode
int8_t getActiveOpMode() const noexcept
Definition
CiA402StateMachine.cpp:35
CiA402::StateMachine::StateMachine
StateMachine()
Constructor.
Definition
CiA402StateMachine.cpp:22
CiA402
Definition
CiA402StateMachine.h:34
CiA402::sw_to_state
static constexpr State sw_to_state(uint16_t sw)
Definition
CiA402StateMachine.h:54
CiA402::state_to_string
static constexpr std::string_view state_to_string(State s)
Definition
CiA402StateMachine.h:79
CiA402::State
State
Definition
CiA402StateMachine.h:37
CiA402::State::OperationEnabled
@ OperationEnabled
Definition
CiA402StateMachine.h:42
CiA402::State::Fault
@ Fault
Definition
CiA402StateMachine.h:45
CiA402::State::SwitchedOn
@ SwitchedOn
Definition
CiA402StateMachine.h:41
CiA402::State::FaultReaction
@ FaultReaction
Definition
CiA402StateMachine.h:44
CiA402::State::Unknown
@ Unknown
Definition
CiA402StateMachine.h:46
CiA402::State::NotReadyToSwitchOn
@ NotReadyToSwitchOn
Definition
CiA402StateMachine.h:38
CiA402::State::SwitchOnDisabled
@ SwitchOnDisabled
Definition
CiA402StateMachine.h:39
CiA402::State::ReadyToSwitchOn
@ ReadyToSwitchOn
Definition
CiA402StateMachine.h:40
CiA402::State::QuickStopActive
@ QuickStopActive
Definition
CiA402StateMachine.h:43
CiA402::StateMachine::Command
Command structure.
Definition
CiA402StateMachine.h:115
CiA402::StateMachine::Command::writeOpMode
bool writeOpMode
Flag indicating if the operation mode should be written.
Definition
CiA402StateMachine.h:118
CiA402::StateMachine::Command::opMode
int8_t opMode
Operation mode to send to the drive.
Definition
CiA402StateMachine.h:117
CiA402::StateMachine::Command::controlword
uint16_t controlword
Control word to send to the drive.
Definition
CiA402StateMachine.h:116
std::unique_ptr
device
CiA402MotionControl
CiA402StateMachine.h
Generated by
1.18.0