RobotStateComponentPlugin.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 * @package RobotAPI::ArmarXObjects::RobotAPIComponentPlugins
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2019
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
25#include <VirtualRobot/Robot.h>
26#include <VirtualRobot/RobotNodeSet.h>
27#include <VirtualRobot/XML/RobotIO.h>
28
30
33
34namespace armarx::plugins
35{
38 {
39 ARMARX_CHECK_NOT_NULL(_nameHelper);
40 return _nameHelper;
41 }
42
43 void
45 {
47 ARMARX_CHECK_NULL(_robotStateComponent);
48 _robotStateComponent = rsc;
49 }
50
51 bool
52 RobotStateComponentPlugin::hasRobot(const std::string& id) const
53 {
54 std::lock_guard g{_robotsMutex};
55 return _robots.count(id);
56 }
57
60 const VirtualRobot::RobotPtr& robot,
61 const VirtualRobot::RobotNodeSetPtr& rns,
62 const VirtualRobot::RobotNodePtr& node)
63 {
65 if (rns)
66 {
67 ARMARX_CHECK_EXPRESSION(robot == rns->getRobot());
68 }
69 if (node)
70 {
71 ARMARX_CHECK_EXPRESSION(robot == node->getRobot());
72 }
73 std::lock_guard g {_robotsMutex};
74 ARMARX_CHECK_EXPRESSION(!hasRobot(id)) << "Robot with id '" << id << "' was already added";
75 if (rns && !node)
76 {
77 _robots[id] = {robot, rns, rns->getTCP()};
78 }
79 else
80 {
81 _robots[id] = {robot, rns, node};
82 }
83 return robot;
84 }
85
88 const VirtualRobot::RobotPtr& robot,
89 const std::string& rnsName,
90 const std::string& nodeName)
91 {
92 ARMARX_CHECK_NOT_NULL(robot) << "robot added with id '" << id << "' is null";
93 VirtualRobot::RobotNodeSetPtr rns;
94 VirtualRobot::RobotNodePtr node;
95 if (!rnsName.empty())
96 {
97 rns = robot->getRobotNodeSet(rnsName);
98 ARMARX_CHECK_NOT_NULL(rns) << "The robot has no node set with name '" << rnsName
99 << "'. Available RNS: " << robot->getRobotNodeSetNames();
100 }
101 if (!nodeName.empty())
102 {
103 auto node = robot->getRobotNode(nodeName);
104 ARMARX_CHECK_NOT_NULL(node) << "The robot has no node with name '" << nodeName
105 << "'. Available nodes: " << robot->getRobotNodeNames();
106 }
107 else if (rns)
108 {
109 node = rns->getTCP();
110 }
111 return addRobot(id, robot, rns, node);
112 }
113
116 VirtualRobot::RobotIO::RobotDescription loadMode,
117 const std::string& rnsName,
118 const std::string& nodeName)
119 {
120 return addRobot(id,
121 RemoteRobot::createLocalCloneFromFile(_robotStateComponent, loadMode),
122 rnsName,
123 nodeName);
124 }
125
128 const std::string& filename,
129 const Ice::StringSeq packages,
130 VirtualRobot::RobotIO::RobotDescription loadMode,
131 const std::string& rnsName,
132 const std::string& nodeName)
133 {
134 return addRobot(
135 id,
136 RemoteRobot::createLocalClone(_robotStateComponent, filename, packages, loadMode),
137 rnsName,
138 nodeName);
139 }
140
142 RobotStateComponentPlugin::getRobot(const std::string& id) const
143 {
144 return getRobotData(id).robot;
145 }
146
148 RobotStateComponentPlugin::getRobotData(const std::string& id) const
149 {
150 std::lock_guard g{_robotsMutex};
151 ARMARX_CHECK_EXPRESSION(hasRobot(id)) << "No robot with id '" << id << "' loaded";
152 return _robots.at(id);
153 }
154
155 void
157 const std::string& rnsName,
158 const std::string& nodeName)
159 {
160 std::lock_guard g{_robotsMutex};
161 ARMARX_CHECK_EXPRESSION(hasRobot(id)) << "No robot with id '" << id << "' loaded";
162 auto& r = _robots.at(id);
163 ARMARX_CHECK_EXPRESSION(!rnsName.empty())
164 << "There was no robot node set specified! "
165 << "Available RNS: " << r.robot->getRobotNodeSetNames();
166 auto rns = r.robot->getRobotNodeSet(rnsName);
167 ARMARX_CHECK_NOT_NULL(rns) << "The robot has no node set with name '" << rnsName
168 << "'. Available RNS: " << r.robot->getRobotNodeSetNames();
169 if (nodeName.empty())
170 {
171 r.rns = rns;
172 r.node = rns->getTCP();
173 }
174 else
175 {
176 auto node = r.robot->getRobotNode(nodeName);
177 ARMARX_CHECK_NOT_NULL(node) << "The robot has no node with name '" << nodeName
178 << "'. Available nodes: " << r.robot->getRobotNodeNames();
179 r.rns = rns;
180 r.node = node;
181 }
182 }
183
186 {
187 return _robotStateComponent;
188 }
189
190 bool
192 {
193 return RemoteRobot::synchronizeLocalClone(robot, _robotStateComponent);
194 }
195
196 bool
198 Ice::Long timestamp) const
199 {
201 robot, _robotStateComponent, timestamp);
202 }
203
204 bool
206 const RobotStateConfig& state) const
207 {
209 }
210
211 bool
216
217 bool
219 Ice::Long timestamp) const
220 {
222 }
223
224 bool
226 const RobotStateConfig& state) const
227 {
228 return synchronizeLocalClone(rdata.robot, state);
229 }
230
231 bool
233 {
235 }
236
237 bool
239 Ice::Long timestamp) const
240 {
242 }
243
244 bool
246 const RobotStateConfig& state) const
247 {
248 return synchronizeLocalClone(getRobot(id), state);
249 }
250
253 const Eigen::Matrix4f& targetPose,
254 const SimpleDiffIK::Parameters& params) const
255 {
256 return getRobotData(id).calculateRobotDiffIK(targetPose, params);
257 }
258
261 const std::string& id,
262 const std::vector<Eigen::Matrix4f>& targets,
263 const Eigen::VectorXf& initialJV,
264 const SimpleDiffIK::Parameters& params) const
265 {
266 return getRobotData(id).calculateRobotReachability(targets, initialJV, params);
267 }
268
269 void
271 {
272 if (!_deactivated && !_robotStateComponent && _robotStateComponentName.empty())
273 {
274 parent<Component>().getProperty(_robotStateComponentName,
275 makePropertyName(_propertyName));
276 }
277
278 if (!_deactivated && !_robotStateComponent)
279 {
280 parent<Component>().usingProxy(_robotStateComponentName);
281 }
282 }
283
284 void
286 {
287 if (!_deactivated && !_robotStateComponent)
288 {
289 parent<Component>().getProxy(_robotStateComponent, _robotStateComponentName);
290 }
291 if (!_deactivated)
292 {
293 _nameHelper = std::make_shared<RobotNameHelper>(_robotStateComponent->getRobotInfo());
294 }
295 }
296
297 void
299 {
300 _robotStateComponent = nullptr;
301 }
302
303 void
305 {
306 if (!properties->hasDefinition(makePropertyName(_propertyName)))
307 {
308 properties->defineOptionalProperty<std::string>(makePropertyName(_propertyName),
309 "RobotStateComponent",
310 "Name of the robot state component");
311 }
312 }
313
316 const Eigen::Matrix4f& targetPose,
317 const SimpleDiffIK::Parameters& params) const
318 {
319 ARMARX_CHECK_NOT_NULL(rns) << "No robot node set configured for the robot";
320 ARMARX_CHECK_NOT_NULL(node) << "No tcp configured for the robot";
321 return SimpleDiffIK::CalculateDiffIK(targetPose, rns, node, params);
322 }
323
326 const std::vector<Eigen::Matrix4f>& targets,
327 const Eigen::VectorXf& initialJV,
328 const SimpleDiffIK::Parameters& params) const
329 {
330 ARMARX_CHECK_NOT_NULL(rns) << "No robot node set configured for the robot";
331 ARMARX_CHECK_NOT_NULL(node) << "No tcp configured for the robot ";
332 return SimpleDiffIK::CalculateReachability(targets, initialJV, rns, node, params);
333 }
334
335 void
337 {
339 ARMARX_CHECK_EMPTY(_robotStateComponentName);
340 _robotStateComponentName = name;
341 }
342
343 const std::string&
345 {
346 return _robotStateComponentName;
347 }
348
349 void
351 {
352 _deactivated = true;
353 }
354
355 Eigen::Matrix4f
357 const std::string& to,
358 const VirtualRobot::RobotPtr& rob)
359 {
361 armarx::FramedPose fp{Eigen::Matrix4f::Identity(), from, rob->getName()};
362 fp.changeFrame(rob, to);
363 return fp.toEigen();
364 }
365} // namespace armarx::plugins
366
367namespace armarx
368{
370 {
371 addPlugin(_robotStateComponentPlugin);
372 }
373
376 {
377 return *_robotStateComponentPlugin;
378 }
379
382 {
383 return *_robotStateComponentPlugin;
384 }
385
391
397
398 bool
399 RobotStateComponentPluginUser::hasRobot(const std::string& id) const
400 {
402 }
403
406 const VirtualRobot::RobotPtr& robot,
407 const VirtualRobot::RobotNodeSetPtr& rns,
408 const VirtualRobot::RobotNodePtr& node)
409 {
410 return getRobotStateComponentPlugin().addRobot(id, robot, rns, node);
411 }
412
415 const VirtualRobot::RobotPtr& robot,
416 const std::string& rnsName,
417 const std::string& nodeName)
418 {
419 return getRobotStateComponentPlugin().addRobot(id, robot, rnsName, nodeName);
420 }
421
424 VirtualRobot::RobotIO::RobotDescription loadMode,
425 const std::string& rnsName,
426 const std::string& nodeName)
427 {
428 return getRobotStateComponentPlugin().addRobot(id, loadMode, rnsName, nodeName);
429 }
430
433 const std::string& filename,
434 const Ice::StringSeq packages,
435 VirtualRobot::RobotIO::RobotDescription loadMode,
436 const std::string& rnsName,
437 const std::string& nodeName)
438 {
440 id, filename, packages, loadMode, rnsName, nodeName);
441 }
442
444 RobotStateComponentPluginUser::getRobot(const std::string& id) const
445 {
447 }
448
451 {
453 }
454
455 void
457 const std::string& rnsName,
458 const std::string& nodeName)
459 {
460 getRobotStateComponentPlugin().setRobotRNSAndNode(id, rnsName, nodeName);
461 }
462
468
469 bool
474
475 bool
481
482 bool
484 const RobotStateConfig& state) const
485 {
487 }
488
489 bool
495
496 bool
503
504 bool
507 const RobotStateConfig& state) const
508 {
510 }
511
512 bool
517
518 bool
524
525 bool
527 const RobotStateConfig& state) const
528 {
530 }
531
534 const Eigen::Matrix4f& targetPose,
535 const SimpleDiffIK::Parameters& params)
536 {
537 return getRobotStateComponentPlugin().calculateRobotDiffIK(id, targetPose, params);
538 }
539
542 const std::string& id,
543 const std::vector<Eigen::Matrix4f> targets,
544 const Eigen::VectorXf& initialJV,
545 const SimpleDiffIK::Parameters& params)
546 {
548 id, targets, initialJV, params);
549 }
550
551} // namespace armarx
std::string timestamp()
#define ARMARX_CHECK_NOT_EMPTY(c)
#define ARMARX_CHECK_NULL(ptr)
#define ARMARX_CHECK_EMPTY(c)
The FramedPose class.
Definition FramedPose.h:281
void changeFrame(const SharedRobotInterfacePrx &referenceRobot, const std::string &newFrame)
std::string makePropertyName(const std::string &name)
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)
static VirtualRobot::RobotPtr createLocalCloneFromFile(RobotStateComponentInterfacePrx robotStatePrx, VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
This is a convenience function for createLocalClone, which automatically gets the filename from the R...
static bool synchronizeLocalCloneToState(VirtualRobot::RobotPtr robot, RobotStateConfig const &state)
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
static bool synchronizeLocalCloneToTimestamp(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx, Ice::Long timestamp)
Synchronizes a local robot to a robot state at timestamp.
static VirtualRobot::RobotPtr createLocalClone(RobotStateComponentInterfacePrx robotStatePrx, const std::string &filename=std::string(), const Ice::StringSeq packages=Ice::StringSeq(), VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
SimpleDiffIK::Result calculateRobotDiffIK(const std::string &id, const Eigen::Matrix4f &targetPose, const SimpleDiffIK::Parameters &params={})
void setRobotRNSAndNode(const std::string &id, const std::string &rnsName, const std::string &nodeName)
RobotStateComponentPlugin::RobotData getRobotData(const std::string &id) const
bool synchronizeLocalClone(const VirtualRobot::RobotPtr &robot) const
const RobotStateComponentPlugin & getRobotStateComponentPlugin() const
armarx::plugins::RobotStateComponentPlugin RobotStateComponentPlugin
SimpleDiffIK::Reachability calculateRobotReachability(const std::string &id, const std::vector< Eigen::Matrix4f > targets, const Eigen::VectorXf &initialJV, const SimpleDiffIK::Parameters &params={})
VirtualRobot::RobotPtr addRobot(const std::string &id, const VirtualRobot::RobotPtr &robot, const VirtualRobot::RobotNodeSetPtr &rns={}, const VirtualRobot::RobotNodePtr &node={})
const RobotStateComponentInterfacePrx & getRobotStateComponent() const
VirtualRobot::RobotPtr getRobot(const std::string &id) const
static Reachability CalculateReachability(const std::vector< Eigen::Matrix4f > targets, const Eigen::VectorXf &initialJV, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp=VirtualRobot::RobotNodePtr(), Parameters params=Parameters())
Use this to check a trajectory of waypoints.
static Result CalculateDiffIK(const Eigen::Matrix4f targetPose, VirtualRobot::RobotNodeSetPtr rns, VirtualRobot::RobotNodePtr tcp=VirtualRobot::RobotNodePtr(), Parameters params=Parameters())
Eigen::Matrix4f transformFromTo(const std::string &from, const std::string &to, const VirtualRobot::RobotPtr &rob)
void setRobotRNSAndNode(const std::string &id, const std::string &rnsName, const std::string &nodeName)
RobotData getRobotData(const std::string &id) const
bool synchronizeLocalClone(const VirtualRobot::RobotPtr &robot) const
void postCreatePropertyDefinitions(PropertyDefinitionsPtr &properties) override
VirtualRobot::RobotPtr addRobot(const std::string &id, const VirtualRobot::RobotPtr &robot, const VirtualRobot::RobotNodeSetPtr &rns={}, const VirtualRobot::RobotNodePtr &node={})
const RobotStateComponentInterfacePrx & getRobotStateComponent() const
VirtualRobot::RobotPtr getRobot(const std::string &id) const
SimpleDiffIK::Reachability calculateRobotReachability(const std::string &id, const std::vector< Eigen::Matrix4f > &targets, const Eigen::VectorXf &initialJV, const SimpleDiffIK::Parameters &params={}) const
SimpleDiffIK::Result calculateRobotDiffIK(const std::string &id, const Eigen::Matrix4f &targetPose, const SimpleDiffIK::Parameters &params={}) const
void setRobotStateComponent(const RobotStateComponentInterfacePrx &rsc)
Brief description of class targets.
Definition targets.h:39
#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...
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file is part of ArmarX.
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx
std::shared_ptr< class RobotNameHelper > RobotNameHelperPtr
SimpleDiffIK::Reachability calculateRobotReachability(const std::vector< Eigen::Matrix4f > &targets, const Eigen::VectorXf &initialJV, const SimpleDiffIK::Parameters &params={}) const
SimpleDiffIK::Result calculateRobotDiffIK(const Eigen::Matrix4f &targetPose, const SimpleDiffIK::Parameters &params={}) const