ControllerWrapper.cpp
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 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2024
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#include "ControllerWrapper.h"
23
25
28
30#include <armarx/control/interface/ConfigurableNJointControllerInterface.h>
31#include <armarx/control/memory/config/Writer.h>
33
35{
36
37
39 const std::string& controllerName,
40 const std::string& controllerTypeName,
41 const memory::config::Writer& configWriter) :
42 controller_(controller),
43 controllerName_(controllerName),
44 controllerTypeName_(controllerTypeName),
45 configWriter_(configWriter)
46 {
47 }
48
50 {
51 // deactivate the controller
52 if (not daemonized_)
53 {
54 try
55 {
56 ARMARX_INFO << "Deactivating and deleting controller `" << controllerName_ << "`";
57 controller_->deactivateAndDeleteController();
58 }
59 // Swallow any exception: destructors must not throw (especially not during
60 // stack unwinding, where it would call std::terminate). The downside is that
61 // if the RPC fails here, the controller may be leaked on the RobotUnit side
62 // and never actually deleted.
63 // TODO: fix this properly — ensure the controller is reliably deactivated
64 // and deleted on the RobotUnit even when the wrapper is destroyed during
65 // an exception (e.g. retry, defer cleanup to a non-throwing path, or make
66 // the server-side call idempotent and infallible).
67 catch (...)
68 {
69 ARMARX_WARNING << "Exception while deactivating and deleting controller `"
70 << controllerName_
71 << "`. The controller may not have been deleted on the "
72 "RobotUnit: "
74 }
75 }
76 }
77
78 void
80 {
81 ARMARX_CHECK_EXPRESSION(controller_);
82 ARMARX_INFO << "Activating controller `" << controllerName_ << "`";
83 controller_->activateController();
84 }
85
86 bool
88 {
89 ARMARX_CHECK_EXPRESSION(controller_);
90 return controller_->isControllerActive();
91 }
92
93 void
95 {
96 ARMARX_CHECK_EXPRESSION(controller_);
97 ARMARX_INFO << "Deactivating controller `" << controllerName_ << "`";
98 controller_->deactivateController();
99 }
100
101 void
103 {
104 ARMARX_INFO << "Daemonizing controller `" << controllerName_ << "`";
105 daemonized_ = daemonize;
106 }
107
110 {
111 ARMARX_CHECK_EXPRESSION(controller_);
112 return controller_;
113 }
114
115 void
118 {
119 ARMARX_VERBOSE << "Updating config for controller `" << controllerName_ << "`";
120
121 ARMARX_CHECK_NOT_NULL(controller_);
122 auto start = armarx::DateTime::Now();
123 auto cfgDTO = config.toAronDTO();
124 ARMARX_DEBUG << "--> config.toAronDTO() of control cfg took "
125 << (armarx::DateTime::Now() - start).toSecondsDouble() << " sec.";
126 start = armarx::DateTime::Now();
127 controller_->updateConfig(cfgDTO);
129 << "--> sending the converted AronDTO (controller_->updateConfig(cfgDTO)) took "
130 << (armarx::DateTime::Now() - start).toSecondsDouble() << " sec.";
131
132 // controller_->updateConfig(config.toAronDTO());
133
134 // store the config also in the memory (logging only)
135 start = armarx::DateTime::Now();
136 configWriter_.storeConfig(controllerTypeName_, controllerName_, config.toAron());
137 ARMARX_DEBUG << "--> store config in memory (configWriter_.storeConfig) took "
138 << (armarx::DateTime::Now() - start).toSecondsDouble() << " sec.";
139 }
140} // namespace armarx::control::client::detail
static DateTime Now()
Definition DateTime.cpp:51
virtual armarx::aron::data::dto::DictPtr toAronDTO() const =0
Convert the current bo to the ice representation of an aron variant data dict.
virtual armarx::aron::data::DictPtr toAron() const =0
Convert the current bo to an aron variant data dict.
void updateConfig(const armarx::aron::codegenerator::cpp::AronGeneratedObjectBase &config)
Updates the configuration of the controller.
void activate()
Requests the activation of the controller in the RobotUnit.
void deactivate()
Requests the deactivation of the controller in the RobotUnit.
ControllerPrx & ctrl()
Returns the "raw" controller object.
bool isActive()
Check the status of the controller.
armarx::control::ConfigurableNJointControllerInterfacePrx ControllerPrx
void daemonize(bool daemonize=true)
Sets whether the controller should be daemonized.
~ControllerWrapperBase()
Destructor for the ControllerWrapperBase class.
#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_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_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#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
aron::cpp::AronGeneratedObjectBase AronGeneratedObjectBase
This file is part of ArmarX.
std::string GetHandledExceptionString()