JointController.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::ArmarXObjects::JointController
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 
28 #include <Ice/ProxyHandle.h>
29 
31 
32 namespace IceProxy::armarx
33 {
34  class DebugDrawerInterface;
35  class DebugObserverInterface;
36 } // namespace IceProxy::armarx
37 
38 namespace armarx
39 {
41  typedef ::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface>
43  typedef ::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface>
45 
46  /**
47  * @brief The JointController class represents one joint in one control mode.
48  * It holds a \ref ControlTargetBase.
49  * This target is used as setpoint for the controller.
50  */
52  {
53  public:
54  virtual ~JointController() = default;
55 
56  virtual ControlTargetBase* getControlTarget() = 0;
57 
58  virtual const ControlTargetBase* getControlTarget() const;
59 
60  /// @return The \ref JointController's \ref ControlTargetBase "ControlTarget" casted to the given type (may be nullptr)
61  template <class T>
62  const T*
64  {
65  return getControlTarget()->asA<const T*>();
66  }
67 
68  virtual void rtResetTarget();
69 
70  virtual bool rtIsTargetValid() const;
71 
72  virtual const std::string& getControlMode() const;
73 
74  virtual std::string getHardwareControlMode() const;
75 
76  std::size_t getControlModeHash() const;
77  std::size_t rtGetControlModeHash() const;
78  std::size_t getHardwareControlModeHash() const;
79  std::size_t rtGetHardwareControlModeHash() const;
80 
81  ControlDevice& getParent() const;
82 
83  /**
84  * Hook for publishing data from JointController, mainly for debugging purposes. The preferred way is to use the
85  * return value of the function to publish the data. This function is called in the publish thread, **not** the RT thread.
86  * Thus, appropriate lock-free synchronization (e.g. atomic variables or TrippleBuffer) must be used to move the data from RT
87  * thread to the publish thread.
88  * @param draw Interface proxy to the DebugDrawer topic.
89  * @param observer Interface proxy to DebugObserver topic.
90  * @return These values are published on the RobotUnitObserver in the channel "ControlDevices". The keys of the map are prepended
91  * with "{ControlDeviceName}_{ControlModeName}_" and are used as keys of the datafields.
92  *
93  */
95  const DebugObserverInterfacePrx& observer) const;
96 
97  ControlDevice& rtGetParent() const;
98 
99  template <class T>
100  T&
101  rtGetParent() const
102  {
103  return dynamic_cast<T&>(rtGetParent);
104  }
105 
106  protected:
107  //called by the owning ControlDevice
108  /// @brief called when this JointController is run
109  virtual void rtRun(const IceUtil::Time& sensorValuesTimestamp,
110  const IceUtil::Time& timeSinceLastIteration) = 0;
111  /// @brief called when this JointController is activated
112  virtual void rtPreActivateController();
113  virtual void rtPostDeactivateController();
114  /// @brief called when this JointController is deactivated
115  private:
116  friend class ControlDevice;
117  std::atomic_bool activated{false};
118  ControlDevice* parent{nullptr};
119  std::size_t controlModeHash{0};
120  std::size_t hardwareControlModeHash{0};
121  void rtActivate();
122  void rtDeactivate();
123  };
124 
125  template <class ControlTargetType>
127  {
129  "ControlTargetType has to inherit SensorValueBase");
130 
131  public:
132  using JointController::JointController;
133 
134  ControlTargetType* getControlTarget() final;
135 
136  ControlTargetType controlTarget;
137  };
138 } // namespace armarx
139 
140 //inline functions
141 namespace armarx
142 {
143  inline const ControlTargetBase*
145  {
146  return const_cast<const ControlTargetBase*>(
147  const_cast<JointController*>(this)->getControlTarget());
148  }
149 
150  inline void
152  {
153  getControlTarget()->reset();
154  }
155 
156  inline bool
158  {
159  return getControlTarget()->isValid();
160  }
161 
162  inline const std::string&
164  {
165  return getControlTarget()->getControlMode();
166  }
167 
168  inline std::string
170  {
171  return "";
172  }
173 
174  inline std::size_t
176  {
177  return controlModeHash;
178  }
179 
180  inline std::size_t
182  {
183  return controlModeHash;
184  }
185 
186  inline std::size_t
188  {
189  return hardwareControlModeHash;
190  }
191 
192  inline std::size_t
194  {
195  return hardwareControlModeHash;
196  }
197 
198  inline ControlDevice&
200  {
201  return *parent;
202  }
203 
204  inline void
206  {
207  }
208 
209  inline void
211  {
212  }
213 
214  inline void
215  JointController::rtActivate()
216  {
218  activated = true;
219  }
220 
221  inline void
222  JointController::rtDeactivate()
223  {
224  activated = false;
226  rtResetTarget();
227  }
228 
229  template <class ControlTargetType>
230  inline ControlTargetType*
232  {
233  return &controlTarget;
234  }
235 } // namespace armarx
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::JointController::publish
virtual StringVariantBaseMap publish(const DebugDrawerInterfacePrx &draw, const DebugObserverInterfacePrx &observer) const
Hook for publishing data from JointController, mainly for debugging purposes.
Definition: JointController.cpp:37
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
IceProxy::armarx
Definition: LogSender.h:38
armarx::JointController::rtRun
virtual void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)=0
called when this JointController is run
armarx::JointControllerTemplate::controlTarget
ControlTargetType controlTarget
Definition: JointController.h:136
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
ControlTargetBase.h
armarx::ControlDevice
The ControlDevice class represents a logical unit accepting commands via its JointControllers.
Definition: ControlDevice.h:63
armarx::JointController::rtPostDeactivateController
virtual void rtPostDeactivateController()
Definition: JointController.h:210
armarx::JointControllerTemplate
Definition: JointController.h:126
armarx::DebugObserverInterfacePrx
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
Definition: JointController.h:44
armarx::JointController
The JointController class represents one joint in one control mode.
Definition: JointController.h:51
armarx::ControlTargetBase::getControlMode
virtual const std::string & getControlMode() const =0
armarx::JointController::rtGetHardwareControlModeHash
std::size_t rtGetHardwareControlModeHash() const
Definition: JointController.h:193
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::JointControllerTemplate::getControlTarget
ControlTargetType * getControlTarget() final
Definition: JointController.h:231
armarx::ControlTargetBase::reset
virtual void reset()=0
armarx::JointController::getHardwareControlMode
virtual std::string getHardwareControlMode() const
Definition: JointController.h:169
armarx::ControlTargetBase::isValid
virtual bool isValid() const =0
armarx::JointController::rtPreActivateController
virtual void rtPreActivateController()
called when this JointController is activated
Definition: JointController.h:205
armarx::JointController::rtIsTargetValid
virtual bool rtIsTargetValid() const
Definition: JointController.h:157
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::JointController::getControlTarget
const T * getControlTarget() const
Definition: JointController.h:63
armarx::JointController::rtGetParent
ControlDevice & rtGetParent() const
Definition: JointController.h:199
armarx::JointController::getHardwareControlModeHash
std::size_t getHardwareControlModeHash() const
Definition: JointController.h:187
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::JointController::~JointController
virtual ~JointController()=default
armarx::JointController::rtGetControlModeHash
std::size_t rtGetControlModeHash() const
Definition: JointController.h:181
armarx::JointController::getControlMode
virtual const std::string & getControlMode() const
Definition: JointController.h:163
armarx::JointController::getControlTarget
virtual ControlTargetBase * getControlTarget()=0
Definition: JointController.h:144
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::DebugDrawerInterfacePrx
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
Definition: JointController.h:40
armarx::JointController::rtGetParent
T & rtGetParent() const
Definition: JointController.h:101
armarx::JointController::rtResetTarget
virtual void rtResetTarget()
Definition: JointController.h:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::JointController::getParent
ControlDevice & getParent() const
Definition: JointController.cpp:30
armarx::JointController::getControlModeHash
std::size_t getControlModeHash() const
Definition: JointController.h:175