EtherCATState.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <ostream>
5
6#include <ethercattype.h>
7
9{
10
11 /**
12 * @class EtherCATState
13 * @ingroup Library-ethercat
14 *
15 * @brief This class is a wrapper around an enum containing the different EtherCAT states.
16 *
17 * The EtherCAT states have been defined by Beckhoff here:
18 * https://infosys.beckhoff.com/english.php?content=../content/1033/ax2000-b110/html/bt_ecbasics_ecstatemachine.htm
19 *
20 * This wrapper allows for easy, well defined construction from std::uint16_t
21 * and for converting the state into a string representation.
22 * The underlying numerical values are the same as defined by the SOEM library here:
23 * https://github.com/OpenEtherCATsociety/SOEM/blob/master/soem/ethercattype.h
24 */
26 {
27 public:
28 /**
29 * @brief The EtherCAT state enum
30 */
31 enum Value : std::uint16_t
32 {
33 /// State is not valid, e.g. if a request for reading the actual bus state failed.
34 invalid = EC_STATE_NONE,
35 /// Initial state after switch a EtherCAT slave on.
36 /// No communication is possible.
37 init = EC_STATE_INIT,
38 /// Pre-operational state.
39 /// SDO communication is possible, but no PDOs are updated.
40 preOp = EC_STATE_PRE_OP,
41 /// Boot state. The firmware could be updated in this state, but this functionality
42 /// is not supported in theĆ­s framework.
43 boot = EC_STATE_BOOT,
44 /// Safe-operational state. PDO inputs (data from the slaves) can be updated, but
45 /// no data can be sent vie PDOs to the slaves.
46 safeOp = EC_STATE_SAFE_OP,
47 /// Operational state.
48 /// Both PDO inputs and outputs can be updated.
49 op = EC_STATE_OPERATIONAL,
50 /// Safe-operational state after an error has happend.
51 safeOpError = EC_STATE_SAFE_OP + EC_STATE_ERROR,
52 };
53
54 EtherCATState() = default;
55
56 /**
57 * @brief Construct a new EtherCATState from another EtherCATState.
58 */
59 constexpr EtherCATState(Value state) : value(state)
60 {
61 }
62
63 /**
64 * @brief Construct a new EtherCATState from a std::uint16_t (e.g. a state value provided
65 * by SOEM).
66 */
67 constexpr EtherCATState(std::uint16_t state) : value(fromUInt16(state))
68 {
69 }
70
71 /**
72 * @brief Returns the wrapped enum value.
73 */
74 operator Value() const
75 {
76 return value;
77 }
78
79 /**
80 * @brief Delete bool-operator to prevent usages like if(state) which does not make sense
81 * in this context and might lead to unexpected behaviour if being allowed to use.
82 */
83 explicit operator bool() = delete;
84
85 /**
86 * @returns A string representation of this EtherCATState.
87 */
88 constexpr const char*
89 c_str() const
90 {
91 switch (value)
92 {
93 case Value::invalid:
94 return "EC_STATE_NONE";
95 case Value::init:
96 return "EC_STATE_INIT";
97 case Value::preOp:
98 return "EC_STATE_PRE_OP";
99 case Value::boot:
100 return "EC_STATE_BOOT";
101 case Value::safeOp:
102 return "EC_STATE_SAFE_OP";
103 case Value::op:
104 return "EC_STATE_OPERATIONAL";
106 return "EC_STATE_SAFE_OP + EC_STATE_ERROR";
107 }
108 return "UNKNOWN_STATE";
109 }
110
111 friend std::ostream&
112 operator<<(std::ostream& stream, const EtherCATState& rhs)
113 {
114 stream << rhs.c_str();
115 return stream;
116 }
117
118 private:
119 Value value;
120
121 constexpr Value
122 fromUInt16(std::uint16_t state)
123 {
124 switch (state)
125 {
126 case EC_STATE_INIT:
127 return Value::init;
128 case EC_STATE_PRE_OP:
129 return Value::preOp;
130 case EC_STATE_BOOT:
131 return Value::boot;
132 case EC_STATE_SAFE_OP:
133 return Value::safeOp;
134 case EC_STATE_OPERATIONAL:
135 return Value::op;
136 case EC_STATE_SAFE_OP + EC_STATE_ERROR:
137 return Value::safeOpError;
138 default:
139 return Value::invalid;
140 }
141 }
142 };
143
144} // namespace armarx::control::ethercat
constexpr const char * c_str() const
@ init
Initial state after switch a EtherCAT slave on.
@ invalid
State is not valid, e.g. if a request for reading the actual bus state failed.
@ safeOpError
Safe-operational state after an error has happend.
constexpr EtherCATState(std::uint16_t state)
Construct a new EtherCATState from a std::uint16_t (e.g.
constexpr EtherCATState(Value state)
Construct a new EtherCATState from another EtherCATState.
friend std::ostream & operator<<(std::ostream &stream, const EtherCATState &rhs)