NJointControllerRegistry.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::NJointController
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
28
30
32{
34}
35
36namespace armarx
37{
39
41 {
42 private:
44 virtual NJointControllerBasePtr create(RobotUnitModule::ControllerManagement* cmngr,
45 const NJointControllerConfigPtr&,
47 bool deletable,
48 bool internal,
49 const std::string& instanceName) const = 0;
51 GenerateConfigDescription(const VirtualRobot::RobotPtr&,
52 const std::map<std::string, ConstControlDevicePtr>&,
53 const std::map<std::string, ConstSensorDevicePtr>&) const = 0;
54 virtual NJointControllerConfigPtr
55 GenerateConfigFromVariants(const StringVariantBaseMap&) const = 0;
56 virtual bool hasRemoteConfiguration() const = 0;
57
58 protected:
59 static thread_local bool ConstructorIsRunning_;
60
61 public:
62 virtual ~NJointControllerRegistryEntry() = default;
63
64 static bool
69 };
70
72
73 template <class ControllerType>
75
76 namespace detail
77 {
79 hasGenerateConfigDescription,
80 GenerateConfigDescription,
83 hasGenerateConfigFromVariants,
84 GenerateConfigFromVariants,
86 typename T::ConfigPtrT>);
87
88 template <class NJointControllerT>
90 {
91 static_assert(hasGenerateConfigDescription<NJointControllerT>::value ==
92 hasGenerateConfigFromVariants<NJointControllerT>::value,
93 "Either overload both GenerateConfigDescription and "
94 "GenerateConfigFromVariants, or none!");
95 static constexpr bool hasRemoteConfiguration_ =
96 hasGenerateConfigDescription<NJointControllerT>::value;
97
98 NJointControllerBasePtr
100 const NJointControllerConfigPtr& config,
101 const VirtualRobot::RobotPtr& rob,
102 bool deletable,
103 bool internal,
104 const std::string& instanceName) const final override
105 {
106 ARMARX_CHECK_EXPRESSION(cmngr) << "ControllerManagement module is NULL!";
107
108 using ConfigPtrT = typename NJointControllerT::ConfigPtrT;
109 ConfigPtrT cfg = ConfigPtrT::dynamicCast(config);
111 << "The configuration is of the wrong type! it has to be an instance of: "
113
115 << "Two NJointControllers are created at the same time";
116 NJointControllerBasePtr ptr;
117 {
120 {
121 ConstructorIsRunning_ = false;
122 };
123 ptr = new NJointControllerT(getRobotUnit(cmngr), cfg, rob);
124 }
125 ptr->deletable = deletable;
126 ptr->internal = internal;
127 ptr->rtClassName_ = ptr->getClassName(Ice::emptyCurrent);
128 ptr->instanceName_ = instanceName;
129 return ptr;
130 }
131
133 GenerateConfigDescription(
134 const VirtualRobot::RobotPtr& robot,
135 const std::map<std::string, ConstControlDevicePtr>& controlDevices,
136 const std::map<std::string, ConstSensorDevicePtr>& sensorDevices)
137 const final override
138 {
139 if constexpr (hasRemoteConfiguration_)
140 {
141 try
142 {
143 return NJointControllerT::GenerateConfigDescription(
144 robot, controlDevices, sensorDevices);
145 }
146 catch (Ice::UserException& e)
147 {
148 ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
149 << "::GenerateConfigDescription'"
150 << "\n---- file = " << e.ice_file()
151 << "\n---- line = " << e.ice_line()
152 << "\n---- id = " << e.ice_id() << "\n---- what:\n"
153 << e.what() << "\n---- stacktrace:\n"
154 << e.ice_stackTrace();
155 throw;
156 }
157 catch (std::exception& e)
158 {
159 ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
160 << "::GenerateConfigDescription'"
161 << "\n---- what:\n"
162 << e.what();
163 throw;
164 }
165 catch (...)
166 {
167 ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
168 << "::GenerateConfigDescription'";
169 throw;
170 }
171 }
172 else
173 {
174 ARMARX_CHECK_EXPRESSION(!"This function should never be called");
175 }
176 }
177
178 NJointControllerConfigPtr
179 GenerateConfigFromVariants(const StringVariantBaseMap& variants) const final override
180 {
181 if constexpr (hasRemoteConfiguration_)
182 {
183 try
184 {
185 return NJointControllerT::GenerateConfigFromVariants(variants);
186 }
187 catch (Ice::UserException& e)
188 {
189 ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
190 << "::GenerateConfigFromVariants'"
191 << "\n---- file = " << e.ice_file()
192 << "\n---- line = " << e.ice_line()
193 << "\n---- id = " << e.ice_id() << "\n---- what:\n"
194 << e.what() << "\n---- stacktrace:\n"
195 << e.ice_stackTrace();
196 throw;
197 }
198 catch (std::exception& e)
199 {
200 ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
201 << "::GenerateConfigFromVariants'"
202 << "\n---- what:\n"
203 << e.what();
204 throw;
205 }
206 catch (...)
207 {
208 ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
209 << "::GenerateConfigFromVariants'";
210 throw;
211 }
212 }
213 else
214 {
215 ARMARX_CHECK_EXPRESSION(!"This function should never be called");
216 }
217 }
218
219 bool
220 hasRemoteConfiguration() const final override
221 {
222 return hasRemoteConfiguration_;
223 }
224 };
225 } // namespace detail
226
228
229 template <class ControllerType>
231 {
232 NJointControllerRegistration(const std::string& name)
233 {
235 name,
236 std::unique_ptr<NJointControllerRegistryEntry>(
238 }
239 };
240} // namespace armarx
241
242#define ARMARX_ASSERT_NJOINTCONTROLLER_HAS_CONSTRUCTION_GUI(T) \
243 static_assert( \
244 ::armarx::detail::hasGenerateConfigDescription<T>::value, \
245 #T " does not offer a construction gui (missing: static GenerateConfigDescription)"); \
246 static_assert( \
247 ::armarx::detail::hasGenerateConfigFromVariants<T>::value, \
248 #T " does not offer a construction gui (missing: static GenerateConfigFromVariants)")
ConfigPrtType(*)(const StringVariantBaseMap &) GenerateConfigFromVariantsFunctionSignature
WidgetDescription::WidgetPtr(*)( const VirtualRobot::RobotPtr &, const std::map< std::string, ConstControlDevicePtr > &controlDevices, const std::map< std::string, ConstSensorDevicePtr > &sensorDevices) GenerateConfigDescriptionFunctionSignature
virtual ~NJointControllerRegistryEntry()=default
Stores key object pairs.
Definition Registrar.h:63
static void registerElement(const std::string &key, std::unique_ptr< NJointControllerRegistryEntry > element)
Definition Registrar.h:108
The RobotUnit class manages a robot and its controllers.
Definition RobotUnit.h:192
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_ON_SCOPE_EXIT
Executes given code when the enclosing scope is left.
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
::IceInternal::Handle<::armarx::WidgetDescription::Widget > WidgetPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
Registrar< std::unique_ptr< NJointControllerRegistryEntry > > NJointControllerRegistry
RobotUnit * getRobotUnit(RobotUnitModule::ControllerManagement *cmngr)
std::map< std::string, VariantBasePtr > StringVariantBaseMap
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
NJointControllerRegistration(const std::string &name)
#define ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK(CHECKNAME, FNCNAME, FNCSIG)