Skip to content

Commit 226f81c

Browse files
authored
Add documentation and cleanups for PlanningRequestAdapter and PlanningRequestAdapterChain classes (#2142)
* Cleanups * Add documentation and more cleanups * Revert size_t change
1 parent f807733 commit 226f81c

File tree

2 files changed

+103
-56
lines changed

2 files changed

+103
-56
lines changed

moveit_core/planning_request_adapter/include/moveit/planning_request_adapter/planning_request_adapter.h

+64-19
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,61 @@ namespace planning_request_adapter
4848
{
4949
MOVEIT_CLASS_FORWARD(PlanningRequestAdapter); // Defines PlanningRequestAdapterPtr, ConstPtr, WeakPtr... etc
5050

51+
/** @brief Concept in MoveIt which can be used to modify the planning problem and resulting trajectory (pre-processing
52+
* and/or post-processing) for a motion planner. PlanningRequestAdapter enable using multiple motion planning and
53+
* trajectory generation algorithms in sequence to produce robust motion plans.
54+
*/
5155
class PlanningRequestAdapter
5256
{
5357
public:
58+
/// \brief Functional interface to call a planning function
5459
using PlannerFn =
5560
std::function<bool(const planning_scene::PlanningSceneConstPtr&, const planning_interface::MotionPlanRequest&,
5661
planning_interface::MotionPlanResponse&)>;
5762

58-
PlanningRequestAdapter()
59-
{
60-
}
61-
62-
virtual ~PlanningRequestAdapter()
63-
{
64-
}
63+
virtual ~PlanningRequestAdapter() = default;
6564

6665
/** \brief Initialize parameters using the passed Node and parameter namespace.
66+
* @param node Node instance used by the adapter
67+
* @param parameter_namespace Parameter namespace for adapter
6768
If no initialization is needed, simply implement as empty */
6869
virtual void initialize(const rclcpp::Node::SharedPtr& node, const std::string& parameter_namespace) = 0;
6970

70-
/// Get a short string that identifies the planning request adapter
71+
/** \brief Get a description of this adapter
72+
* @return Returns a short string that identifies the planning request adapter
73+
*/
7174
virtual std::string getDescription() const
7275
{
7376
return "";
7477
}
7578

79+
/** \brief Adapt the planning request if needed, call the planner
80+
function \e planner and update the planning response if
81+
needed.
82+
* @param planner Pointer to the planner used to solve the passed problem
83+
* @param planning_scene Representation of the environment for the planning
84+
* @param req Motion planning request with a set of constraints
85+
* @param res Reference to which the generated motion plan response is written to
86+
* @return True if response got generated correctly */
7687
bool adaptAndPlan(const planning_interface::PlannerManagerPtr& planner,
7788
const planning_scene::PlanningSceneConstPtr& planning_scene,
7889
const planning_interface::MotionPlanRequest& req,
7990
planning_interface::MotionPlanResponse& res) const;
8091

92+
/** \brief Adapt the planning request if needed, call the planner
93+
function \e planner and update the planning response if
94+
needed. If the response is changed, the index values of the
95+
states added without planning are added to \e
96+
added_path_index
97+
* @param planner Pointer to the planner used to solve the passed problem
98+
* @param planning_scene Representation of the environment for the planning
99+
* @param req Motion planning request with a set of constraints
100+
* @param res Reference to which the generated motion plan response is written to
101+
* @param added_path_index Sometimes planning request adapters may add states on the solution path (e.g.,
102+
add the current state of the robot as prefix, when the robot started to plan only from near that state, as the
103+
current state itself appears to touch obstacles). This is helpful because the added states should not be considered
104+
invalid in all situations.
105+
* @return True if response got generated correctly */
81106
bool adaptAndPlan(const planning_interface::PlannerManagerPtr& planner,
82107
const planning_scene::PlanningSceneConstPtr& planning_scene,
83108
const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res,
@@ -87,7 +112,16 @@ class PlanningRequestAdapter
87112
function \e planner and update the planning response if
88113
needed. If the response is changed, the index values of the
89114
states added without planning are added to \e
90-
added_path_index */
115+
added_path_index
116+
* @param planner Pointer to the planner used to solve the passed problem
117+
* @param planning_scene Representation of the environment for the planning
118+
* @param req Motion planning request with a set of constraints
119+
* @param res Reference to which the generated motion plan response is written to
120+
* @param added_path_index Sometimes planning request adapters may add states on the solution path (e.g.,
121+
add the current state of the robot as prefix, when the robot started to plan only from near that state, as the
122+
current state itself appears to touch obstacles). This is helpful because the added states should not be
123+
considered invalid in all situations.
124+
* @return True if response got generated correctly */
91125
virtual bool adaptAndPlan(const PlannerFn& planner, const planning_scene::PlanningSceneConstPtr& planning_scene,
92126
const planning_interface::MotionPlanRequest& req,
93127
planning_interface::MotionPlanResponse& res,
@@ -115,24 +149,35 @@ class PlanningRequestAdapter
115149
}
116150
};
117151

118-
/// Apply a sequence of adapters to a motion plan
152+
/** \brief Apply a sequence of adapters to a motion plan */
119153
class PlanningRequestAdapterChain
120154
{
121155
public:
122-
PlanningRequestAdapterChain()
123-
{
124-
}
125-
126-
void addAdapter(const PlanningRequestAdapterConstPtr& adapter)
127-
{
128-
adapters_.push_back(adapter);
129-
}
130-
156+
/** \brief Add new adapter to the back of the chain
157+
* @param adapter Adapter to be added */
158+
void addAdapter(const PlanningRequestAdapterConstPtr& adapter);
159+
160+
/** \brief Iterate through the chain and call all adapters and planners in the correct order
161+
* @param planner Pointer to the planner used to solve the passed problem
162+
* @param planning_scene Representation of the environment for the planning
163+
* @param req Motion planning request with a set of constraints
164+
* @param res Reference to which the generated motion plan response is written to
165+
* @return True if response got generated correctly */
131166
bool adaptAndPlan(const planning_interface::PlannerManagerPtr& planner,
132167
const planning_scene::PlanningSceneConstPtr& planning_scene,
133168
const planning_interface::MotionPlanRequest& req,
134169
planning_interface::MotionPlanResponse& res) const;
135170

171+
/** \brief Iterate through the chain and call all adapters and planners in the correct order
172+
* @param planner Pointer to the planner used to solve the passed problem
173+
* @param planning_scene Representation of the environment for the planning
174+
* @param req Motion planning request with a set of constraints
175+
* @param res Reference to which the generated motion plan response is written to
176+
* @param added_path_index Sometimes planning request adapters may add states on the solution path (e.g.,
177+
add the current state of the robot as prefix, when the robot started to plan only from near that state, as the
178+
current state itself appears to touch obstacles). This is helpful because the added states should not be
179+
considered invalid in all situations.
180+
* @return True if response got generated correctly */
136181
bool adaptAndPlan(const planning_interface::PlannerManagerPtr& planner,
137182
const planning_scene::PlanningSceneConstPtr& planning_scene,
138183
const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res,

moveit_core/planning_request_adapter/src/planning_request_adapter.cpp

+39-37
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,22 @@ bool PlanningRequestAdapter::adaptAndPlan(const planning_interface::PlannerManag
103103
const planning_interface::MotionPlanRequest& req,
104104
planning_interface::MotionPlanResponse& res) const
105105
{
106-
std::vector<std::size_t> dummy;
107-
return adaptAndPlan(planner, planning_scene, req, res, dummy);
106+
std::vector<std::size_t> empty_added_path_index;
107+
return adaptAndPlan(planner, planning_scene, req, res, empty_added_path_index);
108+
}
109+
110+
void PlanningRequestAdapterChain::addAdapter(const PlanningRequestAdapterConstPtr& adapter)
111+
{
112+
adapters_.push_back(adapter);
108113
}
109114

110115
bool PlanningRequestAdapterChain::adaptAndPlan(const planning_interface::PlannerManagerPtr& planner,
111116
const planning_scene::PlanningSceneConstPtr& planning_scene,
112117
const planning_interface::MotionPlanRequest& req,
113118
planning_interface::MotionPlanResponse& res) const
114119
{
115-
std::vector<std::size_t> dummy;
116-
return adaptAndPlan(planner, planning_scene, req, res, dummy);
120+
std::vector<std::size_t> empty_added_path_index;
121+
return adaptAndPlan(planner, planning_scene, req, res, empty_added_path_index);
117122
}
118123

119124
bool PlanningRequestAdapterChain::adaptAndPlan(const planning_interface::PlannerManagerPtr& planner,
@@ -128,48 +133,45 @@ bool PlanningRequestAdapterChain::adaptAndPlan(const planning_interface::Planner
128133
added_path_index.clear();
129134
return callPlannerInterfaceSolve(*planner, planning_scene, req, res);
130135
}
131-
else
136+
// the index values added by each adapter
137+
std::vector<std::vector<std::size_t>> added_path_index_each(adapters_.size());
138+
139+
// if there are adapters, construct a function for each, in order,
140+
// so that in the end we have a nested sequence of functions that calls all adapters
141+
// and eventually the planner in the correct order.
142+
PlanningRequestAdapter::PlannerFn fn = [&planner = *planner](const planning_scene::PlanningSceneConstPtr& scene,
143+
const planning_interface::MotionPlanRequest& req,
144+
planning_interface::MotionPlanResponse& res) {
145+
return callPlannerInterfaceSolve(planner, scene, req, res);
146+
};
147+
148+
for (int i = adapters_.size() - 1; i >= 0; --i)
132149
{
133-
// the index values added by each adapter
134-
std::vector<std::vector<std::size_t> > added_path_index_each(adapters_.size());
135-
136-
// if there are adapters, construct a function for each, in order,
137-
// so that in the end we have a nested sequence of functions that calls all adapters
138-
// and eventually the planner in the correct order.
139-
PlanningRequestAdapter::PlannerFn fn = [&planner = *planner](const planning_scene::PlanningSceneConstPtr& scene,
140-
const planning_interface::MotionPlanRequest& req,
141-
planning_interface::MotionPlanResponse& res) {
142-
return callPlannerInterfaceSolve(planner, scene, req, res);
150+
fn = [&adapter = *adapters_[i], fn, &added_path_index = added_path_index_each[i]](
151+
const planning_scene::PlanningSceneConstPtr& scene, const planning_interface::MotionPlanRequest& req,
152+
planning_interface::MotionPlanResponse& res) {
153+
return callAdapter(adapter, fn, scene, req, res, added_path_index);
143154
};
155+
}
144156

145-
for (int i = adapters_.size() - 1; i >= 0; --i)
146-
{
147-
fn = [&adapter = *adapters_[i], fn, &added_path_index = added_path_index_each[i]](
148-
const planning_scene::PlanningSceneConstPtr& scene, const planning_interface::MotionPlanRequest& req,
149-
planning_interface::MotionPlanResponse& res) {
150-
return callAdapter(adapter, fn, scene, req, res, added_path_index);
151-
};
152-
}
153-
154-
bool result = fn(planning_scene, req, res);
155-
added_path_index.clear();
157+
bool result = fn(planning_scene, req, res);
158+
added_path_index.clear();
156159

157-
// merge the index values from each adapter
158-
for (std::vector<std::size_t>& added_states_by_each_adapter : added_path_index_each)
160+
// merge the index values from each adapter
161+
for (std::vector<std::size_t>& added_states_by_each_adapter : added_path_index_each)
162+
{
163+
for (std::size_t& added_index : added_states_by_each_adapter)
159164
{
160-
for (std::size_t& added_index : added_states_by_each_adapter)
165+
for (std::size_t& index_in_path : added_path_index)
161166
{
162-
for (std::size_t& index_in_path : added_path_index)
163-
{
164-
if (added_index <= index_in_path)
165-
index_in_path++;
166-
}
167-
added_path_index.push_back(added_index);
167+
if (added_index <= index_in_path)
168+
index_in_path++;
168169
}
170+
added_path_index.push_back(added_index);
169171
}
170-
std::sort(added_path_index.begin(), added_path_index.end());
171-
return result;
172172
}
173+
std::sort(added_path_index.begin(), added_path_index.end());
174+
return result;
173175
}
174176

175177
} // end of namespace planning_request_adapter

0 commit comments

Comments
 (0)