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
17namespace VirtualRobot
18{
19 using RobotPtr = std::shared_ptr<class Robot>;
20} // namespace VirtualRobot
21
22namespace armarx
23{
24 class RapidXmlReaderNode;
25 class DefaultRapidXmlReaderNode;
26} // namespace armarx
27
29{
30 class SlaveInterface;
31 class DeviceInterface;
33
34 enum class PDOValidity
35 {
37 OnlyInputs, // slaves can "send" updates, but master cannot "send" updates
39 };
40
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
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
EtherCATState rtGetEtherCATState() const
Definition Bus.cpp:1360
void setIfName(const std::string &ifname)
Definition Bus.cpp:468
void configureErrorCountersReading(bool enable, unsigned int periodInMS)
configureErrorCountersReading configure stuff
Definition Bus.cpp:1462
std::uint64_t getIterationCount() const
Definition Bus.cpp:1486
PDOValidity getPDOValidity() const
Definition Bus.cpp:644
bool rtIsEmergencyStopActive() const
Definition Bus.cpp:1428
std::vector< std::experimental::observer_ptr< SlaveInterface > > getSlaves() const
Returns all identifiied slaves on the bus.
Definition Bus.cpp:1392
BusFunctionalState rtGetFunctionalState() const
Definition Bus.cpp:1366
void registerSlaveFactory(const SlaveFactory &factory)
Definition Bus.cpp:1372
const std::vector< std::shared_ptr< DeviceInterface > > & getDevices() const
Returns all initialized devices.
Definition Bus.cpp:1404
std::string logErrorsToFile(std::filesystem::path path, unsigned int lastSeconds)
Definition Bus.cpp:36
void shutdown()
Shuts the bus down by executing the shutdown-hook for each slave and afterwards switch all slaves to ...
Definition Bus.cpp:593
std::experimental::observer_ptr< SlaveInterface > getSlaveAtIndex(std::uint16_t slaveIndex) const
Definition Bus.cpp:1414
std::function< std::unique_ptr< SlaveInterface >(SlaveIdentifier sid)> SlaveFactory
Definition Bus.h:132
static Bus & getBus()
This returns the one and only Bus object.
Definition Bus.cpp:29
std::function< std::shared_ptr< DeviceInterface >(hardware_config::DeviceConfig &config, const VirtualRobot::RobotPtr &robot)> DeviceFactory
Definition Bus.h:136
void setAndParseHardwareConfig(const MultiNodeRapidXMLReader &hwconfig)
Definition Bus.cpp:1384
void setRobot(const VirtualRobot::RobotPtr &robot)
Definition Bus.cpp:1456
void setSocketFileDescriptor(int socketFileDescriptor)
Definition Bus.cpp:462
void registerDeviceFactory(const std::string &name, const DeviceFactory &factory)
Definition Bus.cpp:1378
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
Brief description of class DeviceInterface.
This class is a wrapper around an enum containing the different EtherCAT states.
The SlaveIdentifier class is a POD-type representing a unique set of values identifying an EtherCAT s...
Brief description of class SlaveInterface.
Brief description of class SlaveRegisterReadingScheduler.
Represents a point in time.
Definition DateTime.h:25
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.