PlatformControllerExecutor.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <filesystem>
5#include <utility>
6
13
19#include <armarx/navigation/platform_controller/aron/PlatformLocalTrajectoryControllerConfig.aron.generated.h>
22
24{
25
27 ControllerComponentPlugin& controllerComponentPlugin,
28 const Properties& properties) :
29 controllerPlugin_(controllerComponentPlugin), properties_(properties)
30 {
32
33 if (properties_.rtUnitDynamicallyLoadLibraries)
34 {
35 ARMARX_WARNING
36 << "Dynamic loading of the controller library is deactivated (and this "
37 "is also desired). If loading the controllers is failing subsequently, "
38 "make sure that the RobotUnit's property `LoadLibraries` contains "
39 "`armarx_navigation:armarx_navigation_platform_controller`.";
40 }
41
42 // make default configs available to the memory
43 ARMARX_INFO << "Loading default configs";
44 {
45 // const std::filesystem::path configBasePath =
46 // PackagePath("armarx_navigation", "controller_config").toSystemPath();
47
48 ARMARX_TRACE;
49 // armarx::control::memory::config::parseAndStoreDefaultConfigs<
50 // armarx::navigation::common::ControllerType::PlatformTrajectory>(
51 // "" /*configBasePath*/, controllerComponentPlugin.configMemoryWriter());
52
53 ARMARX_TRACE;
54 }
55
56 // initialize controller
57 ARMARX_INFO << "Initializing local trajectory controller";
58 {
59 ARMARX_TRACE;
60 auto builder = controllerPlugin_.createControllerBuilder<
61 armarx::navigation::common::ControllerType::PlatformLocalTrajectory>();
62
63 ARMARX_TRACE;
64
65 const armarx::PackagePath configPath = [&]() -> armarx::PackagePath
66 {
67 const armarx::PackagePath baseConfigPath(
68 "armarx_navigation", "controller_config/PlatformTrajectory/default.json");
69
70 ARMARX_CHECK(std::filesystem::exists(baseConfigPath.toSystemPath()))
71 << baseConfigPath;
72
73 const armarx::PackagePath robotSpecificConfigPath(
74 "armarx_navigation",
75 "controller_config/PlatformTrajectory/" + properties_.robotName +
76 "default.json");
77
78 if (std::filesystem::exists(robotSpecificConfigPath.toSystemPath()))
79 {
80 return robotSpecificConfigPath;
81 }
82
83 return baseConfigPath;
84 }();
85
86 auto ctrlWrapper = builder.withNodeSet("PlatformPlanning")
87 .withConfig(configPath.toSystemPath())
88 .create();
89
91 ARMARX_CHECK_NOT_NULL(ctrlWrapper);
92 localTrajCtrl_ = std::move(ctrlWrapper);
93 localTrajCtrlInitialConfig_ = localTrajCtrl_->config;
94 }
95
97
98 ARMARX_INFO << "Initializing global trajectory controller";
99 {
101 auto builder = controllerPlugin_.createControllerBuilder<
103
105
106 builder.withNodeSet("PlatformPlanning");
107
108
109 // prefer robot-specific config if available
110 const armarx::PackagePath robotSpecificConfigPath(
111 "armarx_navigation",
112 "controller_config/GlobalTrajectory/" + properties.robotName + ".json");
113
114 if (std::filesystem::exists(robotSpecificConfigPath.toSystemPath()))
115 {
116 ARMARX_INFO << "Using robot specific config: " << robotSpecificConfigPath;
117 builder.withConfig(robotSpecificConfigPath.toSystemPath());
118 }
119 else // fallback: use default config
120 {
121 const armarx::PackagePath configPath(
122 "armarx_navigation", "controller_config/GlobalTrajectory/default.json");
123 builder.withConfig(configPath.toSystemPath());
124 }
125
126 auto ctrlWrapper = builder.create();
127
129 ARMARX_CHECK_NOT_NULL(ctrlWrapper);
130 globalTrajCtrl_ = std::move(ctrlWrapper);
131 globalTrajCtrlInitialConfig_ = globalTrajCtrl_->config;
132 }
133
135
137 ARMARX_CHECK_NOT_NULL(localTrajCtrl_);
138 ARMARX_VERBOSE << "PlatformControllerExecutor: init done.";
139 }
140
141 PlatformControllerExecutor::~PlatformControllerExecutor() = default;
142
143 void
145 const bool activateController)
146 {
147 ARMARX_VERBOSE << "Received trajectory for execution with " << trajectory.points().size()
148 << " points";
149
150 toAron(localTrajCtrl_->config.targets.trajectory, trajectory);
151
152 // sends the updated config to the controller and stores it in the memory
153 localTrajCtrl_->updateConfig();
154
155 if (activateController and not localTrajCtrl_->ctrl()->isControllerActive())
156 {
157 ARMARX_INFO << "Start local trajectory controller";
158
159 localTrajCtrl_->activate();
160 }
161 }
162
163 void
165 const bool activateController)
166 {
167 ARMARX_VERBOSE << "Received trajectory for execution with " << trajectory.points().size()
168 << " points";
169
170 toAron(globalTrajCtrl_->config.targets.trajectory, trajectory);
171
172 ARMARX_INFO << "Sending trajectory with "
173 << globalTrajCtrl_->config.targets.trajectory.points.size()
174 << " to controller.";
175
176 // sends the updated config to the controller and stores it in the memory
177 globalTrajCtrl_->updateConfig();
178
179 if (activateController and not globalTrajCtrl_->ctrl()->isControllerActive())
180 {
181 globalTrajCtrl_->activate();
182 }
183 }
184
185 void
187 {
188 ARMARX_INFO << "Start";
189 switch (controllerType)
190 {
192 if (globalTrajCtrl_ and not globalTrajCtrl_->ctrl()->isControllerActive())
193 {
194 globalTrajCtrl_->activate();
195 }
196 break;
198 if (localTrajCtrl_ and not localTrajCtrl_->ctrl()->isControllerActive())
199 {
200 localTrajCtrl_->activate();
201 }
202 break;
204 ARMARX_INFO << "No controller selected.";
205 break;
206 }
207 }
208
209 void
211 {
212 switch (controllerType)
213 {
215 if (globalTrajCtrl_)
216 {
217 if (not globalTrajCtrl_->ctrl()->isControllerActive())
218 {
220 << "The global trajectory controller is not active but "
221 "should be. Activating now.";
222 globalTrajCtrl_->activate();
223 }
224 }
225 break;
227 if (localTrajCtrl_)
228 {
229 if (not localTrajCtrl_->ctrl()->isControllerActive())
230 {
232 << "The local trajectory controller is not active but "
233 "should be. Activating now.";
234 localTrajCtrl_->activate();
235 }
236 }
237 break;
239 ARMARX_INFO << "No controller selected.";
240 break;
241 }
242 }
243
244 void
246 {
247 lastActiveController_ = ControllerType::None;
248
249 ARMARX_INFO << "Stopping local trajectory controller (if available and active)";
250 if (localTrajCtrl_ && localTrajCtrl_->ctrl() &&
251 localTrajCtrl_->ctrl()->isControllerActive())
252 {
253 ARMARX_INFO << "Stopping local trajectory controller.";
254 localTrajCtrl_->deactivate();
255 lastActiveController_ = ControllerType::LocalTrajectory;
256 }
257 else
258 {
259 ARMARX_INFO << "Local trajectory controller was not available or active.";
260 }
261
262 ARMARX_INFO << "Stopping global trajectory controller (if available and active)";
263 if (globalTrajCtrl_ && globalTrajCtrl_->ctrl() &&
264 globalTrajCtrl_->ctrl()->isControllerActive())
265 {
266 ARMARX_INFO << "Stopping global trajectory controller.";
267 globalTrajCtrl_->deactivate();
268 lastActiveController_ = ControllerType::GlobalTrajectory;
269 }
270 else
271 {
272 ARMARX_INFO << "Global trajectory controller was not available or active.";
273 }
274
275
276 auto robotUnit = controllerPlugin_.getRobotUnitPlugin().getRobotUnit();
277 if (robotUnit)
278 {
279 const auto platformUnitPrx = robotUnit->getPlatformUnit();
280 if (platformUnitPrx)
281 {
282 ARMARX_INFO << "Stopping platform via PlatformUnit";
283 platformUnitPrx->stopPlatform();
284 }
285 else
286 {
287 ARMARX_WARNING << "PlatformUnit proxy not available, cannot stop platform";
288 }
289 }
290 else
291 {
292 ARMARX_INFO << "RobotUnit not available, cannot stop platform";
293 }
294 }
295
296 void
298 {
299
300 if (localTrajCtrl_->ctrl()->isControllerActive())
301 {
302 // keep the initial config as the upper limit
303 localTrajCtrl_->config.params.limits.linear =
304 std::min(limits.linear, localTrajCtrlInitialConfig_.params.limits.linear);
305 localTrajCtrl_->config.params.limits.angular =
306 std::min(limits.angular, localTrajCtrlInitialConfig_.params.limits.angular);
307
308 localTrajCtrl_->updateConfig();
309 }
310
311 if (globalTrajCtrl_->ctrl()->isControllerActive())
312 {
313 // keep the initial config as the upper limit
314 globalTrajCtrl_->config.params.limits.linear =
315 std::min(limits.linear, globalTrajCtrlInitialConfig_.params.limits.linear);
316 globalTrajCtrl_->config.params.limits.angular =
317 std::min(limits.angular, globalTrajCtrlInitialConfig_.params.limits.angular);
318
319 globalTrajCtrl_->updateConfig();
320 }
321 }
322
323 void
325 {
326 ARMARX_CHECK_POSITIVE(velocityFactor)
327 << "Scaling factor may not be negative, but is " << velocityFactor;
328 ARMARX_CHECK_LESS_EQUAL(velocityFactor, 1)
329 << "Scaling factor may not be > 1, but is " << velocityFactor;
330
331 if (localTrajCtrl_->ctrl()->isControllerActive())
332 {
333 localTrajCtrl_->config.params.velocityFactor = velocityFactor;
334
335 localTrajCtrl_->updateConfig();
336 }
337
338 if (globalTrajCtrl_->ctrl()->isControllerActive())
339 {
340 globalTrajCtrl_->config.params.velocityFactor = velocityFactor;
341
342 globalTrajCtrl_->updateConfig();
343 }
344 }
345
346} // namespace armarx::navigation::server
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition Clock.cpp:99
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition Duration.cpp:72
void ensureIsActive(ControllerType controllerType) override
PlatformControllerExecutor(ControllerComponentPlugin &controllerComponentPlugin, const Properties &properties)
void updateVelocityLimits(const core::TwistLimits &limits) override
armarx::control::client::ComponentPlugin ControllerComponentPlugin
void execute(const core::LocalTrajectory &trajectory, bool activateController=false) override
#define ARMARX_CHECK_POSITIVE(number)
This macro evaluates whether number is positive (> 0) and if it turns out to be false it will throw a...
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file is part of ArmarX.
void toAron(arondto::PackagePath &dto, const PackageFileLocation &bo)
#define ARMARX_TRACE
Definition trace.h:77