WorkerNode.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package RobotComponents
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <algorithm>
27#include <atomic>
28#include <memory>
29#include <mutex>
30#include <thread>
31
33
34#include <RobotComponents/interface/components/MotionPlanning/Tasks/RRTConnect/WorkerNode.h>
35
37#include "../../util/Samplers.h"
38#include "Tree.h"
39#include "Updater.h"
40
41namespace armarx
42{
43 template <class IceBaseClass, class DerivedClass>
44 class GenericFactory;
45}
46
47namespace armarx::rrtconnect
48{
49 class WorkerNode;
50 /**
51 * @brief An ice handle for a WorkerNode of the RRTConnect algorithm
52 */
54
55 class WorkerNode : virtual public armarx::ManagedIceObject, virtual public WorkerNodeBase
56 {
57 public:
58 using ConfigType = VectorXf;
59
60 //inherit base ctors
61 using WorkerNodeBase::WorkerNodeBase;
62
63 /**
64 * @brief dtor. Asserts the worker thread was joined.
65 */
66 ~WorkerNode() override
67 {
68#pragma GCC diagnostic push
69#pragma GCC diagnostic ignored "-Wterminate"
71#pragma GCC diagnostic pop
72 }
73
74 //from managedIceObject
75
76 void onInitComponent() override;
77 void onConnectComponent() override;
78 void onExitComponent() override;
79
80 std::string
81 getDefaultName() const override
82 {
83 return "RRTConnectWorkerNode";
84 }
85
86 //from WorkerNodeBase
87 Update getUpdate(Ice::Long updateId,
88 const Ice::Current& = Ice::emptyCurrent) const override;
89 void setWorkerNodes(const WorkerNodeBasePrxList& workers,
90 const Ice::Current& = Ice::emptyCurrent) override;
91
92 void
93 abort(const ::Ice::Current& = Ice::emptyCurrent) override
94 {
95 abortRequest = true;
96 }
97
98 //from WorkerUpdateInterface
99 void updateTree(const Update& u, const Ice::Current& = Ice::emptyCurrent) override;
100
101 void applyUpdates();
102
103 //from ice
104 void ice_postUnmarshal() override;
105
106 protected:
107 friend class GenericFactory<WorkerNodeBase, WorkerNode>;
108 /**
109 * @brief Only used when transmitting through ice.
110 */
111 WorkerNode() = default;
112
113 /**
114 * @param workerNodeId The update's worker id.
115 * @param updateSubId The update's sub id.
116 * @return The update fetched from the manager node.
117 */
118 Update
119 getRemoteUpdate(Ice::Long workerNodeId, Ice::Long updateSubId)
120 {
121 ARMARX_VERBOSE_S << "[worker " << workerId << "] requesting update " << workerNodeId
122 << " / " << updateSubId;
123 ARMARX_CHECK_EXPRESSION(static_cast<std::size_t>(workerNodeId) < workers.size());
124 return workers.at(workerNodeId)->getUpdate(updateSubId);
125 }
126
127 /**
128 * @brief The worker task.
129 * It performs all planning.
130 */
131 void workerTask();
132
133 void sendUpdate();
134
135 NodeId addNode(const ConfigType& cfg, const NodeId& parent, bool addToPrimaryTree);
136
137 NodeId
138 addNodeToPrimaryTree(const ConfigType& cfg, const NodeId& parent)
139 {
140 return addNode(cfg, parent, true);
141 }
142
143 NodeId
144 addNodeToSecondaryTree(const ConfigType& cfg, const NodeId& parent)
145 {
146 return addNode(cfg, parent, false);
147 }
148
149 /**
150 * @return The cspace's dimensionality
151 */
152 std::size_t
154 {
155 return cspace->getDimensionality();
156 }
157
158 Tree&
163
164 Tree&
169
170 Ice::Byte
172 {
173 return fromStartIsPrimaryTree ? 0 : 1;
174 }
175
176 Ice::Byte
178 {
179 return fromStartIsPrimaryTree ? 1 : 0;
180 }
181
182 void
184 {
186 }
187
191
192 mutable std::mutex updateMutex;
194
195 std::atomic_bool doApplyUpdates{false};
196 WorkerNodeBasePrxList workers;
197
198 std::thread workerThread;
199
200 TreeUpdateInterfacePrx globalWorkers;
201
202 std::atomic_bool abortRequest;
203
204 std::deque<Update> localUpdates;
205
206 std::unique_ptr<CuboidSampler> sampler;
207 void prepareNextUpdate();
208 };
209} // namespace armarx::rrtconnect
The ManagedIceObject is the base class for all ArmarX objects.
void updateTree(const Update &u, const Ice::Current &=Ice::emptyCurrent) override
void onInitComponent() override
Pure virtual hook for the subclass.
void abort(const ::Ice::Current &=Ice::emptyCurrent) override
Definition WorkerNode.h:93
NodeId addNodeToSecondaryTree(const ConfigType &cfg, const NodeId &parent)
Definition WorkerNode.h:144
void workerTask()
The worker task.
std::unique_ptr< CuboidSampler > sampler
Definition WorkerNode.h:206
Ice::Byte getSecondaryTreeId() const
Definition WorkerNode.h:177
std::deque< Update > localUpdates
Definition WorkerNode.h:204
Ice::Byte getPrimaryTreeId() const
Definition WorkerNode.h:171
TreeUpdateInterfacePrx globalWorkers
Definition WorkerNode.h:200
Update getUpdate(Ice::Long updateId, const Ice::Current &=Ice::emptyCurrent) const override
WorkerNodeBasePrxList workers
Definition WorkerNode.h:196
NodeId addNodeToPrimaryTree(const ConfigType &cfg, const NodeId &parent)
Definition WorkerNode.h:138
std::atomic_bool abortRequest
Definition WorkerNode.h:202
std::atomic_bool doApplyUpdates
Definition WorkerNode.h:195
NodeId addNode(const ConfigType &cfg, const NodeId &parent, bool addToPrimaryTree)
void onConnectComponent() override
Pure virtual hook for the subclass.
Update getRemoteUpdate(Ice::Long workerNodeId, Ice::Long updateSubId)
Definition WorkerNode.h:119
void setWorkerNodes(const WorkerNodeBasePrxList &workers, const Ice::Current &=Ice::emptyCurrent) override
void onExitComponent() override
Hook for subclass.
WorkerNode()=default
Only used when transmitting through ice.
std::string getDefaultName() const override
Retrieve default name of component.
Definition WorkerNode.h:81
#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_VERBOSE_S
Definition Logging.h:207
IceInternal::Handle< WorkerNode > WorkerNodePtr
An ice handle for a WorkerNode of the RRTConnect algorithm.
Definition WorkerNode.h:53
This file offers overloads of toIce() and fromIce() functions for STL container types.