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  {
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 
49  // armarx::control::memory::config::parseAndStoreDefaultConfigs<
50  // armarx::navigation::common::ControllerType::PlatformTrajectory>(
51  // "" /*configBasePath*/, controllerComponentPlugin.configMemoryWriter());
52 
54  }
55 
56  // initialize controller
57  ARMARX_INFO << "Initializing local trajectory controller";
58  {
60  auto builder = controllerPlugin_.createControllerBuilder<
62 
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  {
100  ARMARX_TRACE;
101  auto builder = controllerPlugin_.createControllerBuilder<
103 
104  ARMARX_TRACE;
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 
128  ARMARX_TRACE;
129  ARMARX_CHECK_NOT_NULL(ctrlWrapper);
130  globalTrajCtrl_ = std::move(ctrlWrapper);
131  globalTrajCtrlInitialConfig_ = globalTrajCtrl_->config;
132  }
133 
135 
136  ARMARX_TRACE;
137  ARMARX_CHECK_NOT_NULL(localTrajCtrl_);
138  ARMARX_VERBOSE << "PlatformControllerExecutor: init done.";
139  }
140 
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  if (localTrajCtrl_->ctrl()->isControllerActive())
250  {
251  ARMARX_INFO << "Stopping local trajectory controller.";
252  localTrajCtrl_->deactivate();
253  lastActiveController_ = ControllerType::LocalTrajectory;
254  }
255 
256  if (globalTrajCtrl_->ctrl()->isControllerActive())
257  {
258  ARMARX_INFO << "Stopping global trajectory controller.";
259  globalTrajCtrl_->deactivate();
260  lastActiveController_ = ControllerType::GlobalTrajectory;
261  }
262 
263 
264  const auto platformUnitPrx =
265  controllerPlugin_.getRobotUnitPlugin().getRobotUnit()->getPlatformUnit();
266  if (platformUnitPrx)
267  {
268  ARMARX_INFO << "Stopping platform via PlatformUnit";
269  platformUnitPrx->stopPlatform();
270  }
271  }
272 
273  void
275  {
276 
277  if (localTrajCtrl_->ctrl()->isControllerActive())
278  {
279  // keep the initial config as the upper limit
280  localTrajCtrl_->config.params.limits.linear =
281  std::min(limits.linear, localTrajCtrlInitialConfig_.params.limits.linear);
282  localTrajCtrl_->config.params.limits.angular =
283  std::min(limits.angular, localTrajCtrlInitialConfig_.params.limits.angular);
284 
285  localTrajCtrl_->updateConfig();
286  }
287 
288  if (globalTrajCtrl_->ctrl()->isControllerActive())
289  {
290  // keep the initial config as the upper limit
291  globalTrajCtrl_->config.params.limits.linear =
292  std::min(limits.linear, globalTrajCtrlInitialConfig_.params.limits.linear);
293  globalTrajCtrl_->config.params.limits.angular =
294  std::min(limits.angular, globalTrajCtrlInitialConfig_.params.limits.angular);
295 
296  globalTrajCtrl_->updateConfig();
297  }
298  }
299 } // namespace armarx::navigation::server
armarx::navigation::core::GlobalTrajectory
Definition: Trajectory.h:70
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::navigation::server::PlatformControllerExecutor::PlatformControllerExecutor
PlatformControllerExecutor(ControllerComponentPlugin &controllerComponentPlugin, const Properties &properties)
Definition: PlatformControllerExecutor.cpp:26
armarx::navigation::server::PlatformControllerExecutor::start
void start(ControllerType controllerType) override
Definition: PlatformControllerExecutor.cpp:186
armarx::navigation::server::PlatformControllerExecutor::ensureIsActive
void ensureIsActive(ControllerType controllerType) override
Definition: PlatformControllerExecutor.cpp:210
armarx::navigation::server::PlatformControllerExecutor::Properties::robotName
std::string robotName
Definition: PlatformControllerExecutor.h:36
armarx::navigation::server::PlatformControllerExecutor::stop
void stop() override
Definition: PlatformControllerExecutor.cpp:245
armarx::navigation::server::ExecutorInterface::ControllerType::GlobalTrajectory
@ GlobalTrajectory
armarx::core::time::Clock::WaitFor
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition: Clock.cpp:99
ARMARX_CHECK_NOT_NULL
#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...
Definition: ExpressionException.h:206
trace.h
Duration.h
armarx::navigation::common::ControllerType::PlatformGlobalTrajectory
@ PlatformGlobalTrajectory
armarx::navigation::core::TwistLimits::linear
float linear
Definition: types.h:82
armarx::navigation::server::ExecutorInterface::ControllerType::LocalTrajectory
@ LocalTrajectory
armarx::navigation::server::PlatformControllerExecutor::execute
void execute(const core::LocalTrajectory &trajectory, bool activateController=false) override
Definition: PlatformControllerExecutor.cpp:144
armarx::navigation::common::ControllerType::PlatformLocalTrajectory
@ PlatformLocalTrajectory
armarx::navigation::core::LocalTrajectory::points
const std::vector< LocalTrajectoryPoint > & points() const
Definition: Trajectory.cpp:839
armarx::navigation::core::TwistLimits::angular
float angular
Definition: types.h:83
armarx::navigation::server::ExecutorInterface::ControllerType::None
@ None
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::navigation::server::PlatformControllerExecutor::updateVelocityLimits
void updateVelocityLimits(const core::TwistLimits &limits) override
Definition: PlatformControllerExecutor.cpp:274
Clock.h
armarx::navigation::server::PlatformControllerExecutor::~PlatformControllerExecutor
~PlatformControllerExecutor() override
armarx::navigation::server
This file is part of ArmarX.
Definition: EventPublishingInterface.h:6
armarx::navigation::server::PlatformControllerExecutor::Properties
Definition: PlatformControllerExecutor.h:34
armarx::control::client::ComponentPlugin
Definition: ComponentPlugin.h:68
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:72
armarx::PackagePath::toSystemPath
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
Definition: PackagePath.cpp:47
armarx::control::client::ComponentPlugin::getRobotUnitPlugin
armarx::plugins::RobotUnitComponentPlugin & getRobotUnitPlugin()
Definition: ComponentPlugin.h:104
armarx::navigation::core::TwistLimits
Definition: types.h:80
armarx::control::client::ComponentPlugin::createControllerBuilder
auto createControllerBuilder(Args... args)
Definition: ComponentPlugin.h:81
armarx::navigation::core::LocalTrajectory
Definition: Trajectory.h:170
PlatformControllerExecutor.h
controller_types.h
armarx::navigation::server::ExecutorInterface::ControllerType
ControllerType
Definition: ExecutorInterface.h:24
ExpressionException.h
armarx::navigation::server::PlatformControllerExecutor::Properties::rtUnitDynamicallyLoadLibraries
bool rtUnitDynamicallyLoadLibraries
Definition: PlatformControllerExecutor.h:37
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
aron_conversions.h
aron_conversions.h
ComponentPlugin.h
Logging.h
armarx::toAron
void toAron(arondto::PackagePath &dto, const PackageFileLocation &bo)
min
T min(T t1, T t2)
Definition: gdiam.h:44
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
Trajectory.h
armarx::PackagePath
Definition: PackagePath.h:52
controller_descriptions.h
armarx::navigation::core::GlobalTrajectory::points
const std::vector< GlobalTrajectoryPoint > & points() const
Definition: Trajectory.cpp:734
types.h
armarx::plugins::RobotUnitComponentPlugin::getRobotUnit
RobotUnitInterfacePrx getRobotUnit() const
Definition: RobotUnitComponentPlugin.cpp:112
PackagePath.h