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
42namespace armarx
43{
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 {
69 "RemoteObjectNodes", "", "CSV of RemoteObjectNode names");
71 "LocalRONStart",
72 true,
73 "start a local remote object node if the CSV RemoteObjectNodes is empty");
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 */
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 */
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 */
212 std::atomic<Ice::Long> nextTaskId{std::numeric_limits<Ice::Long>::min()};
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
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
Default component property definition container.
Definition Component.h:70
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition Component.h:94
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
The DynamicLibrary class provides a mechanism to load libraries at runtime.
Properties for a MotionPlanningServerComponent.
TaskAndRemoteHandle currentTask
currentTask stores a handle to the currently dispatched planning task.
std::thread dispatcherThread
The dispatcher's thread.
ClientSideRemoteHandleControlBlockBasePtr getCurrentTaskHandle(const Ice::Current &) override
void onInitComponent() override
Initializes the server and starts the dispatcher thread.
std::vector< armarx::RemoteObjectNodeInterfacePrx > remoteObjectNodes
The used remote object nodes.
std::recursive_mutex queueMutex
mutex protecting the task queue
bool startLocalNode
Whether a local node should be started, if no rons were provided.
Ice::Long getNumberOfTasks(const Ice::Current &=Ice::emptyCurrent) const override
Returns the number of tasks.
float localNodePercent
Percentage of cores used by the local ron (if started).
std::atomic< Ice::Long > nextTaskId
the next task id.
bool loadLibFromPackage(const std::string &package, const std::string &lib, const Ice::Current &=Ice::emptyCurrent) override
void onDisconnectComponent() override
noop
armarx::ClientSideRemoteHandleControlBlockBasePtr enqueueTask(const MotionPlanningTaskBasePtr &task, const Ice::Current &=Ice::emptyCurrent) override
Enqueues a task.
std::vector< std::string > remoteObjectNodeNames
The used remote object nodes' names.
TaskMap tasks
The map of tasks.
MotionPlanningServer()=default
ctor
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
TaskInfoSeq getAllTaskInfo(const Ice::Current &=Ice::emptyCurrent) const override
Returns information about all tasks.
std::condition_variable_any waitForTaskOrDispatcherKill
The dispatcher's cv.
std::map< std::string, DynamicLibraryPtr > loadedLibs
std::unordered_map< Ice::Long, TaskAndRemoteHandle > TaskMap
The type of the map of tasks.
std::set< Ice::Long > taskQueue
Contains the queue of task ids to execute.
std::atomic_bool dispatcherKillRequest
Whether the dispatcher should shut down.
void dispatcherTask()
the dispatcher task.
bool loadLibFromPath(const std::string &path, const Ice::Current &=Ice::emptyCurrent) override
void onConnectComponent() override
Connects to the used RemoteObjectNodes.
void onExitComponent() override
cleans up and joins the dispatcher
Ice::Long getNewTaskId()
Returns a new task id.
CSpaceAndPaths getTaskCSpaceAndPathsById(Ice::Long id, const Ice::Current &=Ice::emptyCurrent) override
Returns a task's cspace and all found paths.
Ice::Long getNumberOfQueuedTasks(const Ice::Current &) const override
Returns the number of queued tasks.
std::string getDefaultName() const override
Returns the server's default name.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
#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
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< RemoteHandleControlBlock > RemoteHandleControlBlockPtr
IceInternal::Handle< MotionPlanningServer > MotionPlanningServerComponentPtr
A ice handle for a MotionPlanningServerComponent.
IceInternal::Handle< MotionPlanningTask > MotionPlanningTaskPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< SimoxCSpace > SimoxCSpacePtr
An ice handle for a SimoxCSpace.
Definition SimoxCSpace.h:56
std::shared_ptr< DynamicLibrary > DynamicLibraryPtr
IceInternal::Handle< RemoteObjectNode > RemoteObjectNodePtr
An ice handle for a RemoteObjectNodeComponent.