RobotUnitComponentPlugin.cpp
Go to the documentation of this file.
2
3#include <thread>
4
7
8namespace armarx::plugins
9{
11 RobotUnitComponentPlugin::startDataStreaming(const RobotUnitDataStreaming::Config& cfg)
12 {
13 //ok to create smart ptr from parent, since ice handels this
15 }
16
17 void
19 {
20 if (!_ctrls.empty())
21 {
22 ARMARX_CHECK_NOT_NULL(_robotUnit);
23 std::vector<std::string> cs(_ctrls.begin(), _ctrls.end());
24 _robotUnit->deactivateAndDeleteNJointControllers(cs);
25 }
26 _robotUnit = nullptr;
27 }
28
29 NJointControllerInterfacePrx
31 const std::string& className,
32 const std::string& instanceName,
33 const armarx::NJointControllerConfigPtr& config,
34 bool doManageController)
35 {
36 ARMARX_CHECK_NOT_NULL(_robotUnit);
38 {
39 out << "creating ";
40 if (doManageController)
41 {
42 out << "and managing ";
43 }
44 out << " controller '" << instanceName << "' of class '" << instanceName << "'";
45 };
46 const auto prx =
47 _robotUnit->createOrReplaceNJointController(className, instanceName, config);
49 if (doManageController)
50 {
51 manageController(instanceName);
52 }
53 return prx;
54 }
55
56 void
57 RobotUnitComponentPlugin::manageController(const armarx::NJointControllerInterfacePrx& ctrl)
58 {
60 manageController(ctrl->getInstanceName());
61 }
62
63 void
65 {
66 _ctrls.emplace(ctrl);
67 }
68
69 void
71 {
72 if (_robotUnitName.empty())
73 {
74 parent<Component>().getProperty(_robotUnitName, PROPERTY_NAME);
75 }
76
77 if (not _isRobotUnitOptionalDependency)
78 {
79 parent<Component>().usingProxy(_robotUnitName);
80 }
81 }
82
83 void
85 {
86 if (not _robotUnitName.empty())
87 {
88 parent<Component>().getProxy(_robotUnit, _robotUnitName);
89 }
90 }
91
92 void
95 {
96 if (!properties->hasDefinition(PROPERTY_NAME))
97 {
98 if (_isRobotUnitOptionalDependency)
99 {
100 properties->defineOptionalProperty<std::string>(
101 PROPERTY_NAME, "", "Name of the RobotUnit");
102 }
103 else
104 {
105 properties->defineRequiredProperty<std::string>(PROPERTY_NAME,
106 "Name of the RobotUnit");
107 }
108 }
109 }
110
113 {
114 return _robotUnit;
115 }
116
117 void
119 {
120 ARMARX_CHECK_EMPTY(_robotUnitName);
121 _robotUnitName = name;
122 }
123
124 const std::string&
126 {
127 return _robotUnitName;
128 }
129
130 bool
132 {
133 return not _robotUnitName.empty();
134 }
135
136 void
138 {
139 _isRobotUnitOptionalDependency = isOptional;
140 }
141
142 void
143 RobotUnitComponentPlugin::waitUntilRobotUnitIsRunning(const std::function<bool()>& termCond)
144 {
145 ARMARX_INFO << "Waiting until robot unit is running ...";
146
147 if (not hasRobotUnitName())
148 {
149 ARMARX_ERROR << "Could not wait for a robotUnit without a name!";
150 return;
151 }
152
153 parent<Component>().usingProxy(_robotUnitName);
154 while (not termCond() and not robotUnitIsRunning())
155 {
156 ARMARX_INFO << deactivateSpam() << "Still waiting for robot unit to start...";
157 std::this_thread::sleep_for(std::chrono::milliseconds(100));
158 }
159
160 ARMARX_INFO << "Robot unit is up and running.";
161 }
162
163 bool
165 {
166 if (not hasRobotUnitName())
167 {
168 // An empty robotUnit can never run
169 return false;
170 }
171 return not isNullptr(getRobotUnit()) and getRobotUnit()->isRunning();
172 }
173
174
175} // namespace armarx::plugins
176
177namespace armarx
178{
183
186 {
187 return plugin->getRobotUnit();
188 }
189
195
196 void
198 const std::function<bool()>& termCond) const
199 {
200 ARMARX_INFO << "Waiting until robot unit is running ...";
201
202 while ((not termCond()) and
203 ((isNullptr(getRobotUnit()) or (not getRobotUnit()->isRunning()))))
204 {
205 std::this_thread::sleep_for(std::chrono::milliseconds(100));
206 }
207
208 ARMARX_INFO << "Robot unit is up and running.";
209 }
210} // namespace armarx
#define ARMARX_CHECK_EMPTY(c)
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
#define ARMARX_STREAM_PRINTER
use this macro to write output code that is executed when printed and thus not executed if the debug ...
Definition Logging.h:310
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
plugins::RobotUnitComponentPlugin & getRobotUnitComponentPlugin()
void waitUntilRobotUnitIsRunning(const std::function< bool()> &termCond=[] { return false;}) const
Waits until the robot unit is running.
RobotUnitDataStreamingReceiverPtr startDataStreaming(const RobotUnitDataStreaming::Config &cfg)
void postCreatePropertyDefinitions(PropertyDefinitionsPtr &properties) override
void manageController(const NJointControllerInterfacePrx &ctrl)
void waitUntilRobotUnitIsRunning(const std::function< bool()> &termCond=[] { return false;})
Waits until the robot unit is running.
PrxT createNJointController(const std::string &className, const std::string &instanceName, const NJointControllerConfigPtr &config, bool doManageController=true)
#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_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
This file is part of ArmarX.
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotUnitInterface > RobotUnitInterfacePrx
auto make_shared(Args &&... args)
bool isNullptr(const T &p)
Definition Pointer.h:60
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
std::shared_ptr< class RobotUnitDataStreamingReceiver > RobotUnitDataStreamingReceiverPtr