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