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 <mutex>
27 #include <thread>
28 #include <atomic>
29 #include <memory>
30 #include <algorithm>
31 
33 
34 #include <RobotComponents/interface/components/MotionPlanning/Tasks/RRTConnect/WorkerNode.h>
35 #include "../../util/Samplers.h"
36 #include "../../util/CollisionCheckUtil.h"
37 
38 #include "Tree.h"
39 #include "Updater.h"
40 
41 namespace armarx
42 {
43  template <class IceBaseClass, class DerivedClass> class GenericFactory;
44 }
45 namespace armarx::rrtconnect
46 {
47  class WorkerNode;
48  /**
49  * @brief An ice handle for a WorkerNode of the RRTConnect algorithm
50  */
52 
53  class WorkerNode:
54  virtual public armarx::ManagedIceObject,
55  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  std::string getDefaultName() const override
80  {
81  return "RRTConnectWorkerNode";
82  }
83 
84  //from WorkerNodeBase
85  Update getUpdate(Ice::Long updateId, const Ice::Current& = Ice::emptyCurrent) const override;
86  void setWorkerNodes(const WorkerNodeBasePrxList& workers, const Ice::Current& = Ice::emptyCurrent) override;
87 
88  void abort(const ::Ice::Current& = Ice::emptyCurrent) override
89  {
90  abortRequest = true;
91  }
92 
93  //from WorkerUpdateInterface
94  void updateTree(const Update& u, const Ice::Current& = Ice::emptyCurrent) override;
95 
96  void applyUpdates();
97 
98  //from ice
99  void ice_postUnmarshal() override;
100 
101  protected:
102  friend class GenericFactory<WorkerNodeBase, WorkerNode>;
103  /**
104  * @brief Only used when transmitting through ice.
105  */
106  WorkerNode() = default;
107 
108  /**
109  * @param workerNodeId The update's worker id.
110  * @param updateSubId The update's sub id.
111  * @return The update fetched from the manager node.
112  */
113  Update getRemoteUpdate(Ice::Long workerNodeId, Ice::Long updateSubId)
114  {
115  ARMARX_VERBOSE_S << "[worker " << workerId
116  << "] requesting update " << workerNodeId << " / " << updateSubId;
117  ARMARX_CHECK_EXPRESSION(static_cast<std::size_t>(workerNodeId) < workers.size());
118  return workers.at(workerNodeId)->getUpdate(updateSubId);
119  }
120 
121  /**
122  * @brief The worker task.
123  * It performs all planning.
124  */
125  void workerTask();
126 
127  void sendUpdate();
128 
129  NodeId addNode(const ConfigType& cfg, const NodeId& parent, bool addToPrimaryTree);
130 
131  NodeId addNodeToPrimaryTree(const ConfigType& cfg, const NodeId& parent)
132  {
133  return addNode(cfg, parent, true);
134  }
135  NodeId addNodeToSecondaryTree(const ConfigType& cfg, const NodeId& parent)
136  {
137  return addNode(cfg, parent, false);
138  }
139 
140  /**
141  * @return The cspace's dimensionality
142  */
143  std::size_t getDimensionality()
144  {
145  return cspace->getDimensionality();
146  }
147 
149  {
151  }
153  {
155  }
156 
157  Ice::Byte getPrimaryTreeId() const
158  {
159  return fromStartIsPrimaryTree ? 0 : 1;
160  }
161  Ice::Byte getSecondaryTreeId() const
162  {
163  return fromStartIsPrimaryTree ? 1 : 0;
164  }
165 
166  void swapTrees()
167  {
168  fromStartIsPrimaryTree ^= true;
169  }
170 
174 
175  mutable std::mutex updateMutex;
177 
178  std::atomic_bool doApplyUpdates {false};
179  WorkerNodeBasePrxList workers;
180 
181  std::thread workerThread;
182 
183  TreeUpdateInterfacePrx globalWorkers;
184 
185  std::atomic_bool abortRequest;
186 
187  std::deque<Update> localUpdates;
188 
189  std::unique_ptr<CuboidSampler> sampler;
190  void prepareNextUpdate();
191  };
192 }
193 
armarx::rrtconnect::WorkerNode::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: WorkerNode.h:79
armarx::rrtconnect::WorkerNode::treeFromStart
Tree treeFromStart
Definition: WorkerNode.h:171
Updater.h
armarx::rrtconnect::WorkerNode::WorkerNode
WorkerNode()=default
Only used when transmitting through ice.
armarx::rrtconnect::WorkerNode::ConfigType
VectorXf ConfigType
Definition: WorkerNode.h:58
armarx::rrtconnect::WorkerNode::abort
void abort(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkerNode.h:88
armarx::rrtconnect::WorkerNode::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: WorkerNode.cpp:29
armarx::rrtconnect::Updater
Definition: Updater.h:64
armarx::rrtconnect::WorkerNode::abortRequest
std::atomic_bool abortRequest
Definition: WorkerNode.h:185
armarx::rrtconnect::WorkerNode::workers
WorkerNodeBasePrxList workers
Definition: WorkerNode.h:179
armarx::rrtconnect::WorkerNode::addNode
NodeId addNode(const ConfigType &cfg, const NodeId &parent, bool addToPrimaryTree)
Definition: WorkerNode.cpp:246
armarx::rrtconnect::WorkerNode::localUpdates
std::deque< Update > localUpdates
Definition: WorkerNode.h:187
Tree.h
armarx::rrtconnect::WorkerNode::updateMutex
std::mutex updateMutex
Definition: WorkerNode.h:175
armarx::rrtconnect::WorkerNode::addNodeToPrimaryTree
NodeId addNodeToPrimaryTree(const ConfigType &cfg, const NodeId &parent)
Definition: WorkerNode.h:131
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::rrtconnect::WorkerNode::sampler
std::unique_ptr< CuboidSampler > sampler
Definition: WorkerNode.h:189
armarx::rrtconnect::WorkerNode::getPrimaryTreeId
Ice::Byte getPrimaryTreeId() const
Definition: WorkerNode.h:157
armarx::rrtconnect::WorkerNode::treeFromGoal
Tree treeFromGoal
Definition: WorkerNode.h:172
ManagedIceObject.h
armarx::rrtconnect::Tree
Definition: Tree.h:48
armarx::rrtconnect::WorkerNode::doApplyUpdates
std::atomic_bool doApplyUpdates
Definition: WorkerNode.h:178
armarx::rrtconnect::WorkerNode::getDimensionality
std::size_t getDimensionality()
Definition: WorkerNode.h:143
armarx::rrtconnect
Definition: Task.cpp:29
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::rrtconnect::WorkerNode::fromStartIsPrimaryTree
bool fromStartIsPrimaryTree
Definition: WorkerNode.h:173
armarx::rrtconnect::WorkerNode::getSecondaryTree
Tree & getSecondaryTree()
Definition: WorkerNode.h:152
armarx::rrtconnect::WorkerNode::sendUpdate
void sendUpdate()
Definition: WorkerNode.cpp:223
armarx::rrtconnect::WorkerNode::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: WorkerNode.cpp:39
armarx::rrtconnect::WorkerNode::getRemoteUpdate
Update getRemoteUpdate(Ice::Long workerNodeId, Ice::Long updateSubId)
Definition: WorkerNode.h:113
armarx::rrtconnect::WorkerNode
Definition: WorkerNode.h:53
armarx::rrtconnect::WorkerNode::workerTask
void workerTask()
The worker task.
Definition: WorkerNode.cpp:117
armarx::rrtconnect::WorkerNode::prepareNextUpdate
void prepareNextUpdate()
Definition: WorkerNode.cpp:240
armarx::rrtconnect::WorkerNode::updateTree
void updateTree(const Update &u, const Ice::Current &=Ice::emptyCurrent) override
Definition: WorkerNode.cpp:87
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::rrtconnect::WorkerNode::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: WorkerNode.cpp:62
armarx::rrtconnect::WorkerNode::applyUpdates
void applyUpdates()
Definition: WorkerNode.cpp:93
armarx::rrtconnect::WorkerNode::addNodeToSecondaryTree
NodeId addNodeToSecondaryTree(const ConfigType &cfg, const NodeId &parent)
Definition: WorkerNode.h:135
armarx::rrtconnect::WorkerNode::getPrimaryTree
Tree & getPrimaryTree()
Definition: WorkerNode.h:148
armarx::rrtconnect::WorkerNode::workerThread
std::thread workerThread
Definition: WorkerNode.h:181
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
armarx::rrtconnect::WorkerNode::globalWorkers
TreeUpdateInterfacePrx globalWorkers
Definition: WorkerNode.h:183
armarx::rrtconnect::WorkerNode::swapTrees
void swapTrees()
Definition: WorkerNode.h:166
armarx::rrtconnect::WorkerNode::~WorkerNode
~WorkerNode() override
dtor.
Definition: WorkerNode.h:66
armarx::GenericFactory
Definition: FactoryCollectionBase.h:51
armarx::rrtconnect::WorkerNode::getUpdate
Update getUpdate(Ice::Long updateId, const Ice::Current &=Ice::emptyCurrent) const override
Definition: WorkerNode.cpp:68
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::rrtconnect::WorkerNode::updater
Updater updater
Definition: WorkerNode.h:176
armarx::rrtconnect::WorkerNode::getSecondaryTreeId
Ice::Byte getSecondaryTreeId() const
Definition: WorkerNode.h:161
armarx::rrtconnect::WorkerNode::ice_postUnmarshal
void ice_postUnmarshal() override
Definition: WorkerNode.cpp:109
armarx::rrtconnect::WorkerNode::setWorkerNodes
void setWorkerNodes(const WorkerNodeBasePrxList &workers, const Ice::Current &=Ice::emptyCurrent) override
Definition: WorkerNode.cpp:76