ControlDevice.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <atomic>
26 #include <memory>
27 #include <string>
28 
29 #include "../ControlModes.h"
30 #include "../JointControllers/JointController.h"
31 #include "../util.h"
32 #include "DeviceBase.h"
33 
35 {
37 }
38 
40 {
41  using namespace DeviceTags;
42  const static std::string PeriodicPosition = "ControlDeviceTag_PeriodicPosition";
43  const static std::string CreateNoDefaultController =
44  "ControlDeviceTag_CreateNoDefaultController";
45  const static std::string ForcePositionThroughVelocity =
46  "ControlDeviceTag_ForcePositionThroughVelocity";
47 } // namespace armarx::ControlDeviceTags
48 
49 namespace armarx
50 {
52 
53  /**
54  * @brief The ControlDevice class represents a logical unit accepting commands via its JointControllers.
55  *
56  * It holds a set of JointControllers with different Controll modes.
57  * It always has a JointController with mode ControlModes::EmergencyStop which is used in case of an error.
58  * It always has a JointController with mode ControlModes::StopMovement which is used when the controled device should stop any movement.
59  *
60  * It holds a set of tags.
61  * These tags are used by different components to create default controllers, check for abilities or present output to the user.
62  */
63  class ControlDevice : public virtual DeviceBase
64  {
65  public:
66  /// @brief A static const nullptr in case a const ref to a nullptr needs to be returned
67  static const ControlDevicePtr NullPtr;
68 
69  /// @brief Create a ControlDevice with the given name
70  ControlDevice(const std::string& name) : DeviceBase{name}
71  {
72  }
73 
74  //controller management
75  /// @return the jointEmergencyStopController of this ControlDevice
76  JointController* rtGetJointEmergencyStopController();
77  /// @return the jointEmergencyStopController of this ControlDevice
78  JointController* getJointEmergencyStopController();
79  /// @return the jointStopMovementController of this ControlDevice
80  JointController* rtGetJointStopMovementController();
81  /// @return the jointStopMovementController of this ControlDevice
82  JointController* getJointStopMovementController();
83  /// @return the active JointController of this ControlDevice
84  JointController* rtGetActiveJointController();
85  /// @return all JointControllers of this ControlDevice
86  const std::vector<JointController*>& rtGetJointControllers();
87  std::vector<const JointController*> getJointControllers() const;
88  /// @return the JointController for the given mode of this ControlDevice (or nullptr if there is no controller for this mode)
89  JointController* getJointController(const std::string& mode);
90  const JointController* getJointController(const std::string& mode) const;
91  JointController* getJointController(std::size_t i);
92  const JointController* getJointController(std::size_t i) const;
93  bool hasJointController(const std::string& mode) const;
94  const std::vector<std::string>& getControlModes() const;
95  /**
96  * @brief Activates the given JointController for this device
97  * @throw std::logic_error if the Joint controller does not belong to this ControlDevice
98  */
99  virtual void rtSetActiveJointController(JointController* jointCtrl);
100  /**
101  * @brief runs the active Joint Controller and write the target values into the control device
102  * @param sensorValuesTimestamp
103  * @param timeSinceLastIteration
104  * @see writeTargetValues
105  */
106  void rtRun(const IceUtil::Time& sensorValuesTimestamp,
107  const IceUtil::Time& timeSinceLastIteration);
108 
109  /**
110  * @brief This is a hook for implementations to write the setpoints to a bus.
111  * @param sensorValuesTimestamp The timestamp of the current \ref SensorValueBase "SensorValues"
112  * @param timeSinceLastIteration The time delta between the last two updates of \ref SensorValueBase "SensorValues"
113  */
114  virtual void
115  rtWriteTargetValues(const IceUtil::Time& sensorValuesTimestamp,
116  const IceUtil::Time& timeSinceLastIteration)
117  {
118  }
119 
120  RobotUnitModule::Devices* getOwner() const;
121 
122  std::map<std::string, std::string> getJointControllerToTargetTypeNameMap() const;
123 
124  protected:
125  /**
126  * @brief adds the Joint controller to this ControlDevice
127  * @throw std::logic_error if the JointController was already added to a ControlDevice
128  * @throw std::invalid_argument if a JointController with the same control mode was already added to this device
129  */
130  void addJointController(JointController* jointCtrl);
131 
134  {
135  return {};
136  }
137 
138  private:
140  /// @brief The owning \ref RobotUnit. (is set when this \ref SensorDevice is added to the \ref RobotUnit)
141  std::atomic<RobotUnitModule::Devices*> owner{nullptr};
142  /// @brief The \ref JointController "JointControllers" managed by this \ref ControlDevice
144  /// @brief The currently executed \ref JointController
145  JointController* activeJointController{nullptr};
146  /// @brief This \ref ControlDevice "ControlDevice's" emergency stop controller
147  JointController* jointEmergencyStopController{nullptr};
148  /// @brief This \ref ControlDevice "ControlDevice's" stop movement controller
149  JointController* jointStopMovementController{nullptr};
150  };
151 } // namespace armarx
152 
153 //inline functions
154 namespace armarx
155 {
156  inline JointController*
158  {
159  return jointEmergencyStopController;
160  }
161 
162  inline JointController*
164  {
165  return jointStopMovementController;
166  }
167 
168  inline JointController*
170  {
171  return activeJointController;
172  }
173 
174  inline const std::vector<JointController*>&
176  {
177  return jointControllers.values();
178  }
179 
180  inline std::vector<const JointController*>
182  {
183  return {jointControllers.values().begin(), jointControllers.values().end()};
184  }
185 
186  inline JointController*
187  ControlDevice::getJointController(const std::string& mode)
188  {
189  return jointControllers.at(mode, nullptr);
190  }
191 
192  inline const JointController*
193  ControlDevice::getJointController(const std::string& mode) const
194  {
195  return jointControllers.at(mode, nullptr);
196  }
197 
198  inline JointController*
200  {
201  return jointControllers.at(i);
202  }
203 
204  inline const JointController*
206  {
207  return jointControllers.at(i);
208  }
209 
210  inline bool
211  ControlDevice::hasJointController(const std::string& mode) const
212  {
213  return jointControllers.has(mode);
214  }
215 
216  inline const std::vector<std::string>&
218  {
219  return jointControllers.keys();
220  }
221 } // namespace armarx
armarx::ControlDevice::NullPtr
static const ControlDevicePtr NullPtr
A static const nullptr in case a const ref to a nullptr needs to be returned.
Definition: ControlDevice.h:67
armarx::ControlDevice::rtGetJointStopMovementController
JointController * rtGetJointStopMovementController()
Definition: ControlDevice.h:163
armarx::ControlDevice::getControlTargetAccessToken
ControlTargetBase::ControlDeviceAccessToken getControlTargetAccessToken() const
Definition: ControlDevice.h:133
armarx::ControlDevice
The ControlDevice class represents a logical unit accepting commands via its JointControllers.
Definition: ControlDevice.h:63
armarx::ControlDevice::getJointController
JointController * getJointController(const std::string &mode)
Definition: ControlDevice.h:187
armarx::ControlDevice::rtWriteTargetValues
virtual void rtWriteTargetValues(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
This is a hook for implementations to write the setpoints to a bus.
Definition: ControlDevice.h:115
armarx::JointController
The JointController class represents one joint in one control mode.
Definition: JointController.h:51
armarx::DeviceBase
Definition: DeviceBase.h:29
armarx::RobotUnitModule::TYPEDEF_PTRS_HANDLE
TYPEDEF_PTRS_HANDLE(Devices)
armarx::ControlTargetBase::ControlDeviceAccessToken
Definition: ControlTargetBase.h:51
armarx::ControlDevice::rtGetJointControllers
const std::vector< JointController * > & rtGetJointControllers()
Definition: ControlDevice.h:175
armarx::TYPEDEF_PTRS_SHARED
TYPEDEF_PTRS_SHARED(ControlDevice)
armarx::ControlDevice::rtGetJointEmergencyStopController
JointController * rtGetJointEmergencyStopController()
Definition: ControlDevice.h:157
armarx::ControlDevice::ControlDevice
ControlDevice(const std::string &name)
Create a ControlDevice with the given name.
Definition: ControlDevice.h:70
armarx::ControlDevice::getControlModes
const std::vector< std::string > & getControlModes() const
Definition: ControlDevice.h:217
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
DeviceBase.h
armarx::KeyValueVector
This class is pretty much similar to a map.
Definition: KeyValueVector.h:43
armarx::RobotUnitModule::Devices
This Module manages sensor and control devices for a RobotUnit and only allows save and sane access.
Definition: RobotUnitModuleDevices.h:67
armarx::ControlDevice::getJointControllers
std::vector< const JointController * > getJointControllers() const
Definition: ControlDevice.h:181
armarx::RobotUnitModule
Definition: ControlDevice.h:34
armarx::ControlDevice::rtGetActiveJointController
JointController * rtGetActiveJointController()
Definition: ControlDevice.h:169
armarx::ControlDevice::hasJointController
bool hasJointController(const std::string &mode) const
Definition: ControlDevice.h:211
armarx::ControlDeviceTags
Definition: ControlDevice.h:39
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28