Bus.h
Go to the documentation of this file.
1 #pragma once
2 #include <experimental/memory>
3 
7 
10 
11 #include "BusErrorHandler.h"
12 #include "ErrorReporting.h"
13 #include "EtherCATState.h"
14 #include "SlaveIdentifier.h"
15 #include "bus_io/BusIO.h"
16 
17 namespace VirtualRobot
18 {
19  using RobotPtr = std::shared_ptr<class Robot>;
20 } // namespace VirtualRobot
21 
22 namespace armarx
23 {
24  class RapidXmlReaderNode;
25  class DefaultRapidXmlReaderNode;
26 } // namespace armarx
27 
29 {
30  class SlaveInterface;
31  class DeviceInterface;
32  class SlaveRegisterReadingScheduler;
33 
34  enum class PDOValidity
35  {
36  None,
37  OnlyInputs, // slaves can "send" updates, but master cannot "send" updates
38  Both
39  };
40 
41  enum class BusFunctionalState
42  {
43  Running, // normal operational mode, all slaves are working as intended
44  Reinitializing, // one or more slaves were lost and are either still lost or in the process of reinitializing
45  EmergencyStop, // one or more slaves report that their hardware emergency stop signal is active
46  Shutdown // bus is shutdown or it is in the process of initializing
47  };
48 
49  /**
50  * @class Bus
51  * @ingroup Library-ethercat
52  * @brief Brief description of class Bus.
53  *
54  * Detailed description of class Bus.
55  */
56  class Bus : public BusIO, virtual public Logging
57  {
58  friend BusErrorHandler;
59 
60  public:
61  /**
62  * This returns the one and only Bus object.
63  * An implements the singelton pattern
64  * @return The Bus instance
65  */
66  static Bus& getBus();
67 
68  std::string logErrorsToFile(std::filesystem::path path, unsigned int lastSeconds);
69 
70  void setSocketFileDescriptor(int socketFileDescriptor);
71 
72  void setIfName(const std::string& ifname);
73 
75 
76  void setRobot(const VirtualRobot::RobotPtr& robot);
77 
78  /**
79  * @brief configureErrorCountersReading configure stuff
80  *
81  * more configure stuff explanation
82  *
83  * @param enable sadösdm
84  * @param periodInMS asdsads
85  */
86  void configureErrorCountersReading(bool enable, unsigned int periodInMS);
87 
89 
90  /**
91  *
92  * @return false if the bus has not been switched to .
93  */
94  /**
95  * @brief Updates all information on the bus, so all commands will be send to the Bus and all
96  * sensor and other monitored values will be recived from the bus.
97  * @param doErrorHandling
98  * @return asdojis \ref EtherCATState::op
99  */
100  bool rtUpdateBus(bool doErrorHandling = true, size_t iterationCount = 0);
101 
102  /**
103  * Shuts the bus down by executing the shutdown-hook for each slave and afterwards switch
104  * all slaves to EC_STATE_INIT. If the bus state is already EC_STATE_INIT these steps are skipped.
105  * Afterwards the socket will be closed and no communication with the bus is possible anymore.
106  */
107  void shutdown();
108 
110  getSlaveAtIndex(std::uint16_t slaveIndex) const;
111 
112  /**
113  * Returns all identifiied slaves on the bus
114  * @return
115  */
116  std::vector<std::experimental::observer_ptr<SlaveInterface>> getSlaves() const;
117 
118  /**
119  * Returns all initialized devices.
120  * @return
121  */
122  const std::vector<std::shared_ptr<DeviceInterface>>& getDevices() const;
123 
125 
127 
128  bool rtIsEmergencyStopActive() const;
129 
130  bool rtHasError() const;
131 
132  using SlaveFactory = std::function<std::unique_ptr<SlaveInterface>(SlaveIdentifier sid)>;
133 
134  void registerSlaveFactory(const SlaveFactory& factory);
135 
136  using DeviceFactory =
137  std::function<std::shared_ptr<DeviceInterface>(hardware_config::DeviceConfig& config,
138  const VirtualRobot::RobotPtr& robot)>;
139 
140  void registerDeviceFactory(const std::string& name, const DeviceFactory& factory);
141 
142  // Stuff for switching state of slaves on bus
143  bool switchBusToInit();
144  bool switchBusToPreOp();
145  bool switchBusToSafeOp();
146  bool switchBusToOp();
147 
148  // getter for iteration count
149  std::uint64_t getIterationCount() const;
150 
151  private:
152  bool _initToPreOp();
153  bool _initToSafeOp();
154  bool _initToOp();
155  bool _preOpToInit();
156  bool _preOpToSafeOp();
157  bool _preOpToOp();
158  bool _safeOpToInit();
159  bool _safeOpToPreOp();
160  bool _safeOpToOp();
161  bool _opToInit();
162  bool _opToPreOp();
163  bool _opToSafeOp();
164 
165  bool changeBusState(EtherCATState state);
166 
167  bool validateBus() const;
168 
169  bool socketInitialized = false;
170  bool initSocket();
171 
172  private:
173  /**
174  * Default constructor private to enforce singleton.
175  * @see Bus::getBus()
176  */
177  Bus();
178  ~Bus() override;
179 
180  //avoid coping the object (singelton pattern)
181  Bus(const Bus&);
182  Bus& operator=(const Bus&);
183 
184  /**
185  * Creates the slaves and devices and adds them to the corresponding list.
186  * @see Bus::slaves
187  * @see Bus::devices
188  */
189  bool createDevices();
190 
191  /**
192  * This sets the pointers of the PDO mappings for the slaves
193  */
194  bool setPDOMappings();
195 
196  void readAllErrorCounters();
197 
198  std::string ifname;
199  /** Socketfiledescriptor on which the ethercat connection is running */
200  int socketFileDescriptor = -1;
201 
202  /** The current ethercat-state of the whole bus */
203  EtherCATState busState;
204 
205  /** The current functional state of the bus. This state describes the highlevel state. */
207 
208  /** current Bus group we are working on */
209  std::uint8_t currentGroup;
210 
211  /** List of all slaves */
212  std::vector<std::unique_ptr<SlaveInterface>> slaves;
213 
214  /** View of all slaves that is given to the outside **/
215  std::vector<std::experimental::observer_ptr<SlaveInterface>> slavesView;
216 
217  /** List of all devices */
218  std::vector<std::shared_ptr<DeviceInterface>> devices;
219 
220  /** List of all registered slave factories */
221  std::vector<SlaveFactory> slaveFactories;
222 
223  /** Map of all registered device factories */
224  std::map<std::string, DeviceFactory> deviceFactories;
225 
226  /** Current iteration count of the rtUpdateBus() function */
227  std::uint64_t iterationCount = 0;
228 
229  std::unique_ptr<SlaveRegisterReadingScheduler> errorRegisterReadingScheduler;
230 
231  PDOValidity pdoValidity = PDOValidity::None;
232 
233 
234  bool emergencyStopWasActivated = false;
235  armarx::core::time::DateTime busUpdateLastUpdateTime;
236 
238 
239  VirtualRobot::RobotPtr robot; // TODO: Remove if possible.
240 
241  BusErrorHandler errorHandler;
242  };
243 } // namespace armarx::control::ethercat
armarx::control::ethercat::Bus::switchBusToPreOp
bool switchBusToPreOp()
Definition: Bus.cpp:669
BusErrorHandler.h
ConfigParser.h
armarx::control::ethercat::BusErrorHandler
Brief description of class BusErrorHandler.
Definition: BusErrorHandler.h:46
armarx::control::ethercat::Bus::setSocketFileDescriptor
void setSocketFileDescriptor(int socketFileDescriptor)
Definition: Bus.cpp:462
armarx::control::ethercat::BusIO
Brief description of class BusIO.
Definition: BusIO.h:57
armarx::control::ethercat::PDOValidity::None
@ None
armarx::control::ethercat::EtherCATState
This class is a wrapper around an enum containing the different EtherCAT states.
Definition: EtherCATState.h:25
armarx::control::ethercat::Bus::configureErrorCountersReading
void configureErrorCountersReading(bool enable, unsigned int periodInMS)
configureErrorCountersReading configure stuff
Definition: Bus.cpp:1459
VirtualRobot
Definition: FramedPose.h:42
armarx::control::ethercat::BusFunctionalState
BusFunctionalState
Definition: Bus.h:41
BusIO.h
DateTime.h
EtherCATState.h
armarx::control::ethercat::Bus::rtGetEtherCATState
EtherCATState rtGetEtherCATState() const
Definition: Bus.cpp:1360
armarx::control::ethercat::Bus::logErrorsToFile
std::string logErrorsToFile(std::filesystem::path path, unsigned int lastSeconds)
Definition: Bus.cpp:36
armarx::control::ethercat::Bus::rtHasError
bool rtHasError() const
Definition: Bus.cpp:1444
armarx::control::ethercat::BusFunctionalState::Reinitializing
@ Reinitializing
armarx::control::ethercat::SlaveIdentifier
The SlaveIdentifier class is a POD-type representing a unique set of values identifying an EtherCAT s...
Definition: SlaveIdentifier.h:52
armarx::control::ethercat::Bus::switchBusToInit
bool switchBusToInit()
Definition: Bus.cpp:650
std::experimental::fundamentals_v2::observer_ptr
Definition: ManagedIceObject.h:53
armarx::MultiNodeRapidXMLReader
Definition: MultiNodeRapidXMLReader.h:36
armarx::control::ethercat::Bus::switchBusToSafeOp
bool switchBusToSafeOp()
Definition: Bus.cpp:689
armarx::control::ethercat::Bus
Brief description of class Bus.
Definition: Bus.h:56
armarx::control::ethercat::Bus::SlaveFactory
std::function< std::unique_ptr< SlaveInterface >(SlaveIdentifier sid)> SlaveFactory
Definition: Bus.h:132
armarx::control::ethercat::Bus::setRobot
void setRobot(const VirtualRobot::RobotPtr &robot)
Definition: Bus.cpp:1453
armarx::control::ethercat::Bus::rtGetFunctionalState
BusFunctionalState rtGetFunctionalState() const
Definition: Bus.cpp:1366
armarx::control::hardware_config::DeviceConfig
Definition: DeviceConfig.h:42
armarx::control::ethercat::PDOValidity
PDOValidity
Definition: Bus.h:34
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::control::ethercat::Bus::getBus
static Bus & getBus()
This returns the one and only Bus object.
Definition: Bus.cpp:29
armarx::control::ethercat::Bus::getSlaves
std::vector< std::experimental::observer_ptr< SlaveInterface > > getSlaves() const
Returns all identifiied slaves on the bus.
Definition: Bus.cpp:1392
armarx::control::ethercat::Bus::registerSlaveFactory
void registerSlaveFactory(const SlaveFactory &factory)
Definition: Bus.cpp:1372
ErrorReporting.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::control::ethercat::BusFunctionalState::Running
@ Running
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
armarx::control::ethercat::Bus::shutdown
void shutdown()
Shuts the bus down by executing the shutdown-hook for each slave and afterwards switch all slaves to ...
Definition: Bus.cpp:593
armarx::control::ethercat::Bus::registerDeviceFactory
void registerDeviceFactory(const std::string &name, const DeviceFactory &factory)
Definition: Bus.cpp:1378
armarx::control::ethercat::BusFunctionalState::Shutdown
@ Shutdown
armarx::control::ethercat::PDOValidity::Both
@ Both
armarx::control::ethercat::Bus::switchBusToOp
bool switchBusToOp()
Definition: Bus.cpp:709
armarx::control::ethercat::Bus::setIfName
void setIfName(const std::string &ifname)
Definition: Bus.cpp:468
armarx::control::ethercat::Bus::getDevices
const std::vector< std::shared_ptr< DeviceInterface > > & getDevices() const
Returns all initialized devices.
Definition: Bus.cpp:1404
armarx::control::ethercat::Bus::getSlaveAtIndex
std::experimental::observer_ptr< SlaveInterface > getSlaveAtIndex(std::uint16_t slaveIndex) const
Definition: Bus.cpp:1414
armarx::control::ethercat::PDOValidity::OnlyInputs
@ OnlyInputs
MultiNodeRapidXMLReader.h
armarx::control::ethercat::Bus::DeviceFactory
std::function< std::shared_ptr< DeviceInterface >(hardware_config::DeviceConfig &config, const VirtualRobot::RobotPtr &robot)> DeviceFactory
Definition: Bus.h:138
armarx::control::ethercat::Bus::rtUpdateBus
bool rtUpdateBus(bool doErrorHandling=true, size_t iterationCount=0)
Updates all information on the bus, so all commands will be send to the Bus and all sensor and other ...
Definition: Bus.cpp:474
armarx::control::hardware_config::HardwareConfig
Root of the config structure.
Definition: HardwareConfig.h:14
Logging.h
armarx::control::ethercat::Bus::setAndParseHardwareConfig
void setAndParseHardwareConfig(const MultiNodeRapidXMLReader &hwconfig)
Definition: Bus.cpp:1384
armarx::control::ethercat::Bus::rtIsEmergencyStopActive
bool rtIsEmergencyStopActive() const
Definition: Bus.cpp:1428
armarx::control::ethercat::Bus::getPDOValidity
PDOValidity getPDOValidity() const
Definition: Bus.cpp:644
armarx::control::ethercat::BusFunctionalState::EmergencyStop
@ EmergencyStop
armarx::control::ethercat::Bus::getIterationCount
std::uint64_t getIterationCount() const
Definition: Bus.cpp:1483
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
SlaveIdentifier.h
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19