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
32namespace IceProxy::armarx
33{
34 class DebugDrawerInterface;
35 class DebugObserverInterface;
36} // namespace IceProxy::armarx
37
38namespace armarx
39{
40 class ControlDevice;
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
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
98
99 template <class T>
100 T&
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 {
128 static_assert(std::is_base_of<ControlTargetBase, ControlTargetType>::value,
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
141namespace armarx
142{
143 inline const ControlTargetBase*
145 {
146 return const_cast<const ControlTargetBase*>(
147 const_cast<JointController*>(this)->getControlTarget());
148 }
149
150 inline void
155
156 inline bool
158 {
159 return getControlTarget()->isValid();
160 }
161
162 inline const std::string&
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
208
209 inline void
213
214 inline void
215 JointController::rtActivate()
216 {
218 activated = true;
219 }
220
221 inline void
222 JointController::rtDeactivate()
223 {
224 activated = false;
227 }
228
229 template <class ControlTargetType>
230 inline ControlTargetType*
235} // namespace armarx
The ControlDevice class represents a logical unit accepting commands via its JointControllers.
Brief description of class JointControlTargetBase.
virtual void reset()=0
virtual const std::string & getControlMode() const =0
virtual bool isValid() const =0
ControlTargetType * getControlTarget() final
The JointController class represents one joint in one control mode.
virtual ControlTargetBase * getControlTarget()=0
std::size_t getHardwareControlModeHash() const
ControlDevice & getParent() const
ControlDevice & rtGetParent() const
std::size_t rtGetHardwareControlModeHash() const
virtual StringVariantBaseMap publish(const DebugDrawerInterfacePrx &draw, const DebugObserverInterfacePrx &observer) const
Hook for publishing data from JointController, mainly for debugging purposes.
virtual bool rtIsTargetValid() const
virtual void rtPreActivateController()
called when this JointController is activated
virtual std::string getHardwareControlMode() const
virtual void rtPostDeactivateController()
virtual const std::string & getControlMode() const
friend class ControlDevice
called when this JointController is deactivated
std::size_t getControlModeHash() const
virtual ~JointController()=default
const T * getControlTarget() const
virtual void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)=0
called when this JointController is run
std::size_t rtGetControlModeHash() const
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::map< std::string, VariantBasePtr > StringVariantBaseMap
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx