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  * @brief Construct a new EtherCATState from another EtherCATState.
57  */
58  constexpr EtherCATState(Value state) : value(state)
59  {
60  }
61  /**
62  * @brief Construct a new EtherCATState from a std::uint16_t (e.g. a state value provided
63  * by SOEM).
64  */
65  constexpr EtherCATState(std::uint16_t state) : value(fromUInt16(state))
66  {
67  }
68 
69  /**
70  * @brief Returns the wrapped enum value.
71  */
72  operator Value() const
73  {
74  return value;
75  }
76 
77  /**
78  * @brief Delete bool-operator to prevent usages like if(state) which does not make sense
79  * in this context and might lead to unexpected behaviour if being allowed to use.
80  */
81  explicit operator bool() = delete;
82 
83  /**
84  * @returns A string representation of this EtherCATState.
85  */
86  constexpr const char*
87  c_str() const
88  {
89  switch (value)
90  {
91  case Value::invalid:
92  return "EC_STATE_NONE";
93  case Value::init:
94  return "EC_STATE_INIT";
95  case Value::preOp:
96  return "EC_STATE_PRE_OP";
97  case Value::boot:
98  return "EC_STATE_BOOT";
99  case Value::safeOp:
100  return "EC_STATE_SAFE_OP";
101  case Value::op:
102  return "EC_STATE_OPERATIONAL";
103  case Value::safeOpError:
104  return "EC_STATE_SAFE_OP + EC_STATE_ERROR";
105  }
106  return "UNKNOWN_STATE";
107  }
108 
109  friend std::ostream&
110  operator<<(std::ostream& stream, const EtherCATState& rhs)
111  {
112  stream << rhs.c_str();
113  return stream;
114  }
115 
116  private:
117  Value value;
118 
119  constexpr Value
120  fromUInt16(std::uint16_t state)
121  {
122  switch (state)
123  {
124  case EC_STATE_INIT:
125  return Value::init;
126  case EC_STATE_PRE_OP:
127  return Value::preOp;
128  case EC_STATE_BOOT:
129  return Value::boot;
130  case EC_STATE_SAFE_OP:
131  return Value::safeOp;
132  case EC_STATE_OPERATIONAL:
133  return Value::op;
134  case EC_STATE_SAFE_OP + EC_STATE_ERROR:
135  return Value::safeOpError;
136  default:
137  return Value::invalid;
138  }
139  }
140  };
141 
142 } // namespace armarx::control::ethercat
armarx::control::ethercat::EtherCATState::c_str
constexpr const char * c_str() const
Definition: EtherCATState.h:87
armarx::control::ethercat::EtherCATState::EtherCATState
constexpr EtherCATState(std::uint16_t state)
Construct a new EtherCATState from a std::uint16_t (e.g.
Definition: EtherCATState.h:65
armarx::control::ethercat::EtherCATState
This class is a wrapper around an enum containing the different EtherCAT states.
Definition: EtherCATState.h:25
armarx::control::ethercat::EtherCATState::EtherCATState
EtherCATState()=default
armarx::control::ethercat::EtherCATState::EtherCATState
constexpr EtherCATState(Value state)
Construct a new EtherCATState from another EtherCATState.
Definition: EtherCATState.h:58
armarx::control::ethercat::EtherCATState::boot
@ boot
Boot state.
Definition: EtherCATState.h:43
armarx::control::ethercat::EtherCATState::invalid
@ invalid
State is not valid, e.g. if a request for reading the actual bus state failed.
Definition: EtherCATState.h:34
armarx::control::ethercat::EtherCATState::safeOpError
@ safeOpError
Safe-operational state after an error has happend.
Definition: EtherCATState.h:51
armarx::control::ethercat::EtherCATState::Value
Value
The EtherCAT state enum.
Definition: EtherCATState.h:31
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::control::ethercat::EtherCATState::preOp
@ preOp
Pre-operational state.
Definition: EtherCATState.h:40
armarx::control::ethercat::EtherCATState::operator<<
friend std::ostream & operator<<(std::ostream &stream, const EtherCATState &rhs)
Definition: EtherCATState.h:110
armarx::control::ethercat::EtherCATState::safeOp
@ safeOp
Safe-operational state.
Definition: EtherCATState.h:46
armarx::control::ethercat::EtherCATState::init
@ init
Initial state after switch a EtherCAT slave on.
Definition: EtherCATState.h:37
armarx::control::ethercat::EtherCATState::op
@ op
Operational state.
Definition: EtherCATState.h:49