MotionPlanningServer.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 <atomic>
27 #include <condition_variable>
28 #include <mutex>
29 #include <thread>
30 #include <unordered_map>
31 
36 
38 #include <RobotComponents/interface/components/MotionPlanning/MotionPlanningServer.h>
39 
41 
42 namespace armarx
43 {
44  class MotionPlanningTask;
46  class DynamicLibrary;
47  using DynamicLibraryPtr = std::shared_ptr<DynamicLibrary>;
48 
50  /**
51  * @brief A ice handle for a MotionPlanningServerComponent
52  */
54 
55  /**
56  * @brief Properties for a MotionPlanningServerComponent
57  */
59  {
60  public:
61  /**
62  * @brief ctor
63  * @param prefix the used prefix for properties
64  */
67  {
68  defineOptionalProperty<std::string>(
69  "RemoteObjectNodes", "", "CSV of RemoteObjectNode names");
70  defineOptionalProperty<bool>(
71  "LocalRONStart",
72  true,
73  "start a local remote object node if the CSV RemoteObjectNodes is empty");
74  defineOptionalProperty<float>(
75  "LocalRONCorePerc",
76  0.5,
77  "Local remote object node core count percentage. (has to be >0; 0.5 => 50%");
78  }
79  };
80 
81  /**
82  * @defgroup Component-MotionPlanningServer MotionPlanningServer
83  * @ingroup RobotComponents-Components
84  * @brief Implementation of the slice interface MotionPlanningServerInterface
85  * @see \ref RobotComponents-Tutorials-MotionPlanning
86  * @see \ref RobotComponents-Tutorials-SimoxCSpace
87  * @see \ref RobotComponents-GuiPlugins-SimoxCSpaceVisualizer
88  */
90  virtual public MotionPlanningServerInterface,
91  virtual public armarx::Component
92  {
93  public:
94  /**
95  * @brief ctor
96  */
97  MotionPlanningServer() = default;
98 
99  /**
100  * @brief dtor
101  */
103  {
104 #pragma GCC diagnostic push
105 #pragma GCC diagnostic ignored "-Wterminate"
107  ARMARX_VERBOSE_S << "dtor";
109 #pragma GCC diagnostic pop
110  }
111 
112  // inherited from Component
113  /**
114  * @brief Returns the server's default name.
115  * @return The server's default name.
116  */
117  std::string
118  getDefaultName() const override
119  {
120  return "MotionPlanningServer";
121  }
122 
123  /**
124  * @brief Initializes the server and starts the dispatcher thread
125  */
126  void onInitComponent() override;
127  /**
128  * @brief Connects to the used RemoteObjectNodes
129  */
130  void onConnectComponent() override;
131 
132  /**
133  * @brief noop
134  */
135  void
137  {
138  }
139 
140  /**
141  * @brief cleans up and joins the dispatcher
142  */
143  void onExitComponent() override;
144 
145  /**
146  */
147  /**
148  * @see PropertyUser::createPropertyDefinitions()
149  * @return The planning server's PropertyDefinitions
150  */
153  {
156  }
157 
158  //from MotionPlanningServerInterface
159  /**
160  * @brief Returns a task's cspace and all found paths.
161  * @param id The id.
162  * @return The task's cspace and all found paths.
163  */
164  CSpaceAndPaths getTaskCSpaceAndPathsById(Ice::Long id,
165  const Ice::Current& = Ice::emptyCurrent) override;
166 
167  /**
168  * @brief Enqueues a task
169  * @param task The task.
170  * @return The task's proxy.
171  */
172  armarx::ClientSideRemoteHandleControlBlockBasePtr
173  enqueueTask(const MotionPlanningTaskBasePtr& task,
174  const Ice::Current& = Ice::emptyCurrent) override;
175 
176  /**
177  * @brief Returns the number of tasks.
178  * @return The number of tasks.
179  */
180  Ice::Long getNumberOfTasks(const Ice::Current& = Ice::emptyCurrent) const override;
181  /**
182  * @brief Returns the number of queued tasks.
183  * @return The number of queued tasks.
184  */
185  Ice::Long getNumberOfQueuedTasks(const Ice::Current&) const override;
186 
187  /**
188  * @brief Returns information about all tasks.
189  * @return Information about all tasks.
190  */
191  TaskInfoSeq getAllTaskInfo(const Ice::Current& = Ice::emptyCurrent) const override;
192 
193  ClientSideRemoteHandleControlBlockBasePtr
194  getCurrentTaskHandle(const Ice::Current&) override;
195 
196  bool loadLibFromPath(const std::string& path,
197  const Ice::Current& = Ice::emptyCurrent) override;
198  bool loadLibFromPackage(const std::string& package,
199  const std::string& lib,
200  const Ice::Current& = Ice::emptyCurrent) override;
201 
202  protected:
203  bool deleteTaskById(Ice::Long id);
204  /**
205  * @brief the dispatcher task. (executed by the dispatcherThread)
206  */
207  void dispatcherTask();
208 
209  /**
210  * @brief the next task id.
211  */
213 
214  /**
215  * @brief Returns a new task id.
216  * @return A new task id.
217  */
218  Ice::Long
220  {
221  return nextTaskId++;
222  }
223 
224  /**
225  * @brief The used remote object nodes' names
226  */
227  std::vector<std::string> remoteObjectNodeNames;
228  /**
229  * @brief The used remote object nodes
230  */
231  std::vector<armarx::RemoteObjectNodeInterfacePrx> remoteObjectNodes;
232 
233  /**
234  * @brief Whether a local node should be started, if no rons were provided.
235  */
237  /**
238  * @brief Percentage of cores used by the local ron (if started).
239  */
241 
243 
245  {
248  };
249 
250  /**
251  * @brief The type of the map of tasks
252  */
253  using TaskMap = std::unordered_map<Ice::Long, TaskAndRemoteHandle>;
254  /**
255  * @brief The map of tasks.
256  */
258 
259  /**
260  * @brief currentTask stores a handle to the currently dispatched planning task.
261  */
263 
264  /**
265  * @brief Contains the queue of task ids to execute.
266  *
267  * If not empty the first id is the next task to execute
268  * A set is used since the queue lacks an erase function and ids are
269  * added in ascending order.
270  */
271  std::set<Ice::Long> taskQueue;
272 
273  /**
274  * @brief mutex protecting the task queue
275  */
276  mutable std::recursive_mutex queueMutex;
277 
278  //we have to ensure that the waiting thread has only locked the recursive mutex once,
279  //since the condition variable only will use the unlock method on the unique_lock once during the wait.
280  //http://stackoverflow.com/questions/14323340/can-you-combine-stdrecursive-mutex-with-stdcondition-variable
281  //
282  //this is given for dispatcherThread
283  /**
284  * @brief The dispatcher's cv
285  */
286  std::condition_variable_any waitForTaskOrDispatcherKill;
287  /**
288  * @brief The dispatcher's thread
289  */
290  std::thread dispatcherThread;
291  /**
292  * @brief Whether the dispatcher should shut down
293  */
294  std::atomic_bool dispatcherKillRequest{false};
295 
296  std::map<std::string, DynamicLibraryPtr> loadedLibs;
297 
299 
300 
301  private:
302  bool loadLibFromAbsolutePath(const std::string& path);
303  };
304 
305 } // namespace armarx
armarx::MotionPlanningServer::dispatcherKillRequest
std::atomic_bool dispatcherKillRequest
Whether the dispatcher should shut down.
Definition: MotionPlanningServer.h:294
armarx::MotionPlanningServer::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: MotionPlanningServer.h:152
armarx::MotionPlanningServer::onDisconnectComponent
void onDisconnectComponent() override
noop
Definition: MotionPlanningServer.h:136
armarx::MotionPlanningServer::dispatcherThread
std::thread dispatcherThread
The dispatcher's thread.
Definition: MotionPlanningServer.h:290
armarx::DynamicLibraryPtr
std::shared_ptr< DynamicLibrary > DynamicLibraryPtr
Definition: DynamicLibrary.h:124
armarx::MotionPlanningServer::waitForTaskOrDispatcherKill
std::condition_variable_any waitForTaskOrDispatcherKill
The dispatcher's cv.
Definition: MotionPlanningServer.h:286
armarx::MotionPlanningServer::remoteObjectNodeNames
std::vector< std::string > remoteObjectNodeNames
The used remote object nodes' names.
Definition: MotionPlanningServer.h:227
armarx::MotionPlanningServer::TaskAndRemoteHandle::rh
armarx::RemoteHandleControlBlockPtr rh
Definition: MotionPlanningServer.h:246
armarx::MotionPlanningServer::MotionPlanningServer
MotionPlanningServer()=default
ctor
armarx::MotionPlanningServer::tasks
TaskMap tasks
The map of tasks.
Definition: MotionPlanningServer.h:257
armarx::MotionPlanningServer::onConnectComponent
void onConnectComponent() override
Connects to the used RemoteObjectNodes.
Definition: MotionPlanningServer.cpp:72
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
armarx::MotionPlanningServer::currentTask
TaskAndRemoteHandle currentTask
currentTask stores a handle to the currently dispatched planning task.
Definition: MotionPlanningServer.h:262
armarx::MotionPlanningServer::getTaskCSpaceAndPathsById
CSpaceAndPaths getTaskCSpaceAndPathsById(Ice::Long id, const Ice::Current &=Ice::emptyCurrent) override
Returns a task's cspace and all found paths.
Definition: MotionPlanningServer.cpp:379
armarx::MotionPlanningServer::remoteObjectNodes
std::vector< armarx::RemoteObjectNodeInterfacePrx > remoteObjectNodes
The used remote object nodes.
Definition: MotionPlanningServer.h:231
armarx::MotionPlanningServer::loadLibFromPackage
bool loadLibFromPackage(const std::string &package, const std::string &lib, const Ice::Current &=Ice::emptyCurrent) override
Definition: MotionPlanningServer.cpp:455
armarx::MotionPlanningServer
Definition: MotionPlanningServer.h:89
armarx::MotionPlanningServerPropertyDefinitions
Properties for a MotionPlanningServerComponent.
Definition: MotionPlanningServer.h:58
RemoteObjectNode.h
armarx::MotionPlanningServer::getDefaultName
std::string getDefaultName() const override
Returns the server's default name.
Definition: MotionPlanningServer.h:118
armarx::MotionPlanningServer::onInitComponent
void onInitComponent() override
Initializes the server and starts the dispatcher thread.
Definition: MotionPlanningServer.cpp:45
IceInternal::Handle< MotionPlanningTask >
armarx::MotionPlanningServerPropertyDefinitions::MotionPlanningServerPropertyDefinitions
MotionPlanningServerPropertyDefinitions(std::string prefix)
ctor
Definition: MotionPlanningServer.h:65
armarx::MotionPlanningServer::localNodePercent
float localNodePercent
Percentage of cores used by the local ron (if started).
Definition: MotionPlanningServer.h:240
armarx::MotionPlanningServer::onExitComponent
void onExitComponent() override
cleans up and joins the dispatcher
Definition: MotionPlanningServer.cpp:110
armarx::MotionPlanningServer::getNumberOfQueuedTasks
Ice::Long getNumberOfQueuedTasks(const Ice::Current &) const override
Returns the number of queued tasks.
Definition: MotionPlanningServer.cpp:339
armarx::MotionPlanningServer::TaskMap
std::unordered_map< Ice::Long, TaskAndRemoteHandle > TaskMap
The type of the map of tasks.
Definition: MotionPlanningServer.h:253
armarx::MotionPlanningServer::getAllTaskInfo
TaskInfoSeq getAllTaskInfo(const Ice::Current &=Ice::emptyCurrent) const override
Returns information about all tasks.
Definition: MotionPlanningServer.cpp:346
RemoteHandleControlBlock.h
armarx::MotionPlanningServer::localNode
RemoteObjectNodePtr localNode
Definition: MotionPlanningServer.h:242
MotionPlanningTask.h
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
armarx::MotionPlanningServer::enqueueTask
armarx::ClientSideRemoteHandleControlBlockBasePtr enqueueTask(const MotionPlanningTaskBasePtr &task, const Ice::Current &=Ice::emptyCurrent) override
Enqueues a task.
Definition: MotionPlanningServer.cpp:288
armarx::MotionPlanningServer::getCurrentTaskHandle
ClientSideRemoteHandleControlBlockBasePtr getCurrentTaskHandle(const Ice::Current &) override
Definition: MotionPlanningServer.cpp:368
armarx::MotionPlanningServer::~MotionPlanningServer
~MotionPlanningServer() override
dtor
Definition: MotionPlanningServer.h:102
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:91
armarx::MotionPlanningServer::startLocalNode
bool startLocalNode
Whether a local node should be started, if no rons were provided.
Definition: MotionPlanningServer.h:236
ExpressionException.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::MotionPlanningServer::taskQueue
std::set< Ice::Long > taskQueue
Contains the queue of task ids to execute.
Definition: MotionPlanningServer.h:271
SimoxCSpace.h
armarx::MotionPlanningServer::loadedLibs
std::map< std::string, DynamicLibraryPtr > loadedLibs
Definition: MotionPlanningServer.h:296
armarx::MotionPlanningServer::loadLibFromPath
bool loadLibFromPath(const std::string &path, const Ice::Current &=Ice::emptyCurrent) override
Definition: MotionPlanningServer.cpp:440
armarx::MotionPlanningServer::TaskAndRemoteHandle::task
MotionPlanningTaskPtr task
Definition: MotionPlanningServer.h:247
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::MotionPlanningServer::getNumberOfTasks
Ice::Long getNumberOfTasks(const Ice::Current &=Ice::emptyCurrent) const override
Returns the number of tasks.
Definition: MotionPlanningServer.cpp:332
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
armarx::MotionPlanningServer::getNewTaskId
Ice::Long getNewTaskId()
Returns a new task id.
Definition: MotionPlanningServer.h:219
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::MotionPlanningServer::TaskAndRemoteHandle
Definition: MotionPlanningServer.h:244
armarx::MotionPlanningServer::dispatcherTask
void dispatcherTask()
the dispatcher task.
Definition: MotionPlanningServer.cpp:145
armarx::MotionPlanningServer::nextTaskId
std::atomic< Ice::Long > nextTaskId
the next task id.
Definition: MotionPlanningServer.h:212
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:207
armarx::MotionPlanningServer::cacheCSpace
SimoxCSpacePtr cacheCSpace
Definition: MotionPlanningServer.h:298
armarx::MotionPlanningServer::queueMutex
std::recursive_mutex queueMutex
mutex protecting the task queue
Definition: MotionPlanningServer.h:276
armarx::DynamicLibrary
The DynamicLibrary class provides a mechanism to load libraries at runtime.
Definition: DynamicLibrary.h:49
min
T min(T t1, T t2)
Definition: gdiam.h:44
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::MotionPlanningServer::deleteTaskById
bool deleteTaskById(Ice::Long id)
Definition: MotionPlanningServer.cpp:265