SlaveInterface.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <memory>
5 
8 
9 #include "ErrorReporting.h"
10 #include "SlaveIdentifier.h"
11 
12 
14 {
15 
16  class Bus;
17 
18  class SlaveErrorRegistersDevice;
19  using SlaveErrorRegistersDevicePtr = std::shared_ptr<SlaveErrorRegistersDevice>;
20 
21 
22  /**
23  * @class SlaveInterface
24  * @ingroup Library-ethercat
25  * @brief Brief description of class SlaveInterface.
26  *
27  * Detailed description of class SlaveInterface.
28  */
30  {
31 
32  public:
34  virtual ~SlaveInterface() override
35  {
36  }
37 
38  /**
39  * This is called after EtherCAT Bus is PreOp Mode. This is where the PDO Mapping can be configured for the slave.
40  */
41  virtual void doMappings() = 0;
42 
43  /**
44  * This gets triggered by the bus controller before it will start the control loop.
45  * If a slave needs more preparation than just getting in EtherCAT Op-Mode this should be done here.
46  * So slaves can assume that the EtherCAT state machine is in Op-Mode so PDO's are available.
47  * Attention!!! This needs to be implemented cooperative
48  * @return true if the prepare is finished an don't needs to be called again
49  */
50  virtual bool prepareForRun() = 0;
51 
52  /**
53  * This method gets triggered by the Bus Controller, this function hast to be implemented cooperative.
54  * The Bus controller will guarantee that the process data will be update before each call.
55  */
56  virtual void execute() = 0;
57 
58  /**
59  * This gets triggered by the bus Controller before it will close the EtherCAT bus.
60  * So if the device need to do something before to get in a safe state, this can be done here.
61  * Attention!!! This needs to be implemented cooperative
62  * @return if slave is shut down
63  */
64  virtual bool shutdown() = 0;
65 
66  virtual void setInputPDO(void* ptr) = 0;
67 
68  virtual void setOutputPDO(void* ptr) = 0;
69 
70  /**
71  * This gets called between the SafeOp an the Op state of the bus at the initizialisation.
72  * The slave can assume that the PDO is already mapped
73  */
74  virtual void prepareForOp() = 0;
75 
76  /**
77  * @brief This gets called after prepareForOp() was called. This is useful if prepareForOp()
78  * executes a long running initialization and needs to be done before the slave goes into op
79  */
80  virtual void
82  {
83  }
84 
85  virtual void
87  {
88  }
89 
90  virtual void
92  {
93  }
94 
95  /**
96  * This function indicates if there is a error or Problem with this slave. It should not
97  * @return true if there is an error/problem with this slave otherwise false;
98  */
99  virtual bool hasError() = 0;
100 
101  virtual bool
103  {
104  return false;
105  }
106 
107  virtual bool
109  {
110  return true;
111  }
112 
113  /**
114  * This tries to clear oder fix the errors or problems of the slave or just gives detailed information about the problem.
115  * If hasError == false this function does nothing.
116  * @return true if the function is trying to recover the slave or there is no error, false is send if this just reports the info
117  */
118  virtual bool handleErrors();
119 
120 
121  std::uint16_t getSlaveNumber() const;
122 
123  const SlaveIdentifier& getSlaveIdentifier() const;
124 
125  virtual void setName(const std::string& name);
126  void setParentDeviceName(const std::string& name);
129 
130  virtual bool
132  {
133  return false;
134  }
135 
136  protected:
139  };
140 
141 
142  template <class InputT, class OutputT>
144  {
145 
146  public:
148 
149  void
150  setInputPDO(void* ptr) override
151  {
152  const auto ptrAsInt = reinterpret_cast<std::uint64_t>(ptr);
153  ARMARX_CHECK_EXPRESSION((ptrAsInt % alignof(InputT)) == 0)
154  << "\nThe alignment is wrong!\nIt has to be " << alignof(InputT)
155  << ", but the data is aligned with " << ptrAsInt % alignof(std::max_align_t)
156  << "!\nThis is an offset of " << (ptrAsInt % alignof(InputT))
157  << " bytes!\nThe datatype is " << GetTypeString<InputT>() << "\nIts size is "
158  << sizeof(InputT) << " bytes";
159  inputs = static_cast<InputT*>(ptr);
160  }
161 
162  void
163  setOutputPDO(void* ptr) override
164  {
165  const auto ptrAsInt = reinterpret_cast<std::uint64_t>(ptr);
166  ARMARX_CHECK_EXPRESSION((ptrAsInt % alignof(OutputT)) == 0)
167  << "\nThe alignment is wrong!\nIt has to be " << alignof(OutputT)
168  << ", but the data is aligned with " << ptrAsInt % alignof(std::max_align_t)
169  << "!\nThis is an offset of " << (ptrAsInt % alignof(OutputT))
170  << " bytes!\nThe datatype is " << GetTypeString<OutputT>() << "\nIts size is "
171  << sizeof(OutputT) << " bytes";
172  outputs = static_cast<OutputT*>(ptr);
173  }
174 
175  bool
176  hasPDOMapping() const final override
177  {
178  return true;
179  }
180 
181  OutputT*
183  {
184  return outputs;
185  }
186 
187  InputT*
189  {
190  return inputs;
191  }
192 
193  protected:
194  InputT* inputs{nullptr};
195  OutputT* outputs;
196  };
197 
198 
199  template <typename Slave>
200  std::unique_ptr<armarx::control::ethercat::SlaveInterface>
202  {
203  SlaveIdentifier sidWithDefaultName(sid);
204  sidWithDefaultName.setName(Slave::getDefaultName());
205 
206  if (Slave::isSlaveIdentifierAccepted(sidWithDefaultName))
207  {
208  return std::make_unique<Slave>(sidWithDefaultName);
209  }
210 
211  return nullptr;
212  }
213 
214 } // namespace armarx::control::ethercat
armarx::control::ethercat::SlaveInterface::prepareForSafeOp
virtual void prepareForSafeOp()
Definition: SlaveInterface.h:86
armarx::control::ethercat::SlaveInterface::isEmergencyStopActive
virtual bool isEmergencyStopActive() const
Definition: SlaveInterface.h:102
armarx::control::ethercat::SlaveInterfaceWithIO::inputs
InputT * inputs
Definition: SlaveInterface.h:194
armarx::control::ethercat::SlaveInterface
Brief description of class SlaveInterface.
Definition: SlaveInterface.h:29
armarx::control::ethercat::SlaveInterface::slaveIdentifier
SlaveIdentifier slaveIdentifier
Definition: SlaveInterface.h:137
armarx::control::ethercat::createSlave
std::unique_ptr< armarx::control::ethercat::SlaveInterface > createSlave(const SlaveIdentifier &sid)
Definition: SlaveInterface.h:201
armarx::control::ethercat::SlaveInterfaceWithIO::getOutputsPtr
OutputT * getOutputsPtr()
Definition: SlaveInterface.h:182
armarx::control::ethercat::SlaveInterface::prepareForRun
virtual bool prepareForRun()=0
This gets triggered by the bus controller before it will start the control loop.
armarx::control::ethercat::reporting::Type::Bus
@ Bus
Bussdfnödf.
armarx::control::ethercat::SlaveInterface::prepareForOp
virtual void prepareForOp()=0
This gets called between the SafeOp an the Op state of the bus at the initizialisation.
armarx::control::ethercat::SlaveInterface::setName
virtual void setName(const std::string &name)
Definition: SlaveInterface.cpp:41
armarx::control::ethercat::SlaveInterface::getErrorRegistersDevice
SlaveErrorRegistersDevicePtr getErrorRegistersDevice() const
Definition: SlaveInterface.cpp:61
armarx::control::ethercat::SlaveInterfaceWithIO::outputs
OutputT * outputs
Definition: SlaveInterface.h:195
armarx::control::ethercat::SlaveInterfaceWithIO::setOutputPDO
void setOutputPDO(void *ptr) override
Definition: SlaveInterface.h:163
armarx::control::ethercat::SlaveInterface::doMappings
virtual void doMappings()=0
This is called after EtherCAT Bus is PreOp Mode.
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::SlaveIdentifier::setName
bool setName(const std::string &name)
Sets the slave name of a SlaveIdentifier and returns whether the new name fits together with the pare...
Definition: SlaveIdentifier.cpp:56
armarx::control::ethercat::SlaveInterface::getSlaveIdentifier
const SlaveIdentifier & getSlaveIdentifier() const
Definition: SlaveInterface.cpp:34
armarx::control::ethercat::SlaveInterface::errorRegistersDevice
SlaveErrorRegistersDevicePtr errorRegistersDevice
Definition: SlaveInterface.h:138
armarx::control::ethercat::SlaveInterface::execute
virtual void execute()=0
This method gets triggered by the Bus Controller, this function hast to be implemented cooperative.
armarx::control::ethercat::SlaveInterface::setOutputPDO
virtual void setOutputPDO(void *ptr)=0
armarx::control::ethercat::SlaveInterfaceWithIO::getInputsPtr
InputT * getInputsPtr()
Definition: SlaveInterface.h:188
armarx::control::ethercat::SlaveInterface::shutdown
virtual bool shutdown()=0
This gets triggered by the bus Controller before it will close the EtherCAT bus.
armarx::control::ethercat::SlaveInterface::setErrorRegistersDevice
void setErrorRegistersDevice(SlaveErrorRegistersDevicePtr errorRegistersDevice)
Definition: SlaveInterface.cpp:53
armarx::control::ethercat::SlaveInterfaceWithIO::setInputPDO
void setInputPDO(void *ptr) override
Definition: SlaveInterface.h:150
armarx::control::ethercat::SlaveInterface::hasPDOMapping
virtual bool hasPDOMapping() const
Definition: SlaveInterface.h:131
armarx::control::ethercat::SlaveInterface::hasError
virtual bool hasError()=0
This function indicates if there is a error or Problem with this slave.
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::control::ethercat::SlaveInterface::handleErrors
virtual bool handleErrors()
This tries to clear oder fix the errors or problems of the slave or just gives detailed information a...
Definition: SlaveInterface.cpp:27
armarx::control::ethercat::SlaveInterfaceWithIO
Definition: SlaveInterface.h:143
armarx::control::ethercat::SlaveInterface::setParentDeviceName
void setParentDeviceName(const std::string &name)
Definition: SlaveInterface.cpp:47
armarx::control::ethercat::SlaveInterface::SlaveInterface
SlaveInterface(const SlaveIdentifier &slaveIdentifier)
Definition: SlaveInterface.cpp:10
armarx::control::ethercat::SlaveInterface::~SlaveInterface
virtual ~SlaveInterface() override
Definition: SlaveInterface.h:34
armarx::control::ethercat::SlaveInterface::setInputPDO
virtual void setInputPDO(void *ptr)=0
ErrorReporting.h
ExpressionException.h
armarx::control::ethercat::SlaveInterfaceWithIO::hasPDOMapping
bool hasPDOMapping() const final override
Definition: SlaveInterface.h:176
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::control::ethercat::SlaveInterface::finishPreparingForOp
virtual void finishPreparingForOp()
This gets called after prepareForOp() was called.
Definition: SlaveInterface.h:81
armarx::control::ethercat::SlaveErrorRegistersDevicePtr
std::shared_ptr< SlaveErrorRegistersDevice > SlaveErrorRegistersDevicePtr
Definition: SlaveInterface.h:19
armarx::control::ethercat::SlaveInterface::recoverFromEmergencyStop
virtual bool recoverFromEmergencyStop()
Definition: SlaveInterface.h:108
armarx::control::ethercat::SlaveInterface::finishPreparingForSafeOp
virtual void finishPreparingForSafeOp()
Definition: SlaveInterface.h:91
Logging.h
SlaveIdentifier.h
armarx::control::ethercat::SlaveInterface::getSlaveNumber
std::uint16_t getSlaveNumber() const
This returns the slave number of the slave on the bus +1 because slave 0 is the master.
Definition: SlaveInterface.cpp:20