YARP CiA-402 EtherCAT Device 0.6.0
YARP device plugin for EtherCAT CiA-402 drives
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
33namespace CiA402
34{
35
36enum 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
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 */
54static 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
79static inline constexpr std::string_view state_to_string(State s)
80{
81 switch (s)
82 {
84 return "SwitchOnDisabled";
86 return "ReadyToSwitchOn";
88 return "SwitchedOn";
90 return "OperationEnabled";
92 return "QuickStopActive";
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// ────────────────────────────────────────────────────────────────────────────
106{
107public:
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 */
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 {
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 {
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
227private:
228 struct Impl;
230};
231
232} // namespace CiA402
233
234#endif // YARP_DEV_CIA402_STATE_MACHINE_H
static bool isOperationEnabled(uint16_t sw)
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.
static bool isQuickStopActive(uint16_t sw)
static bool isOpEnabled(uint16_t sw)
~StateMachine()
Destructor.
static bool isFault(uint16_t sw)
void reset()
Resets the internal state machine.
Command faultReset() noexcept
Generates a fault reset command.
static bool isSwitchedOn(uint16_t sw)
static bool isFaultReactionActive(uint16_t sw)
int8_t getActiveOpMode() const noexcept
static constexpr State sw_to_state(uint16_t sw)
static constexpr std::string_view state_to_string(State s)
bool writeOpMode
Flag indicating if the operation mode should be written.
int8_t opMode
Operation mode to send to the drive.
uint16_t controlword
Control word to send to the drive.