mirror of
https://github.com/moveit/moveit_task_constructor.git
synced 2025-11-04 14:49:57 +08:00
Introspection: private implementation to speedup compilation
This commit is contained in:
parent
9dae631a1d
commit
9aba70fd59
@ -1,13 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <moveit/macros/class_forward.h>
|
#include <moveit/macros/class_forward.h>
|
||||||
#include <ros/publisher.h>
|
|
||||||
#include <ros/service.h>
|
|
||||||
#include <moveit_task_constructor/TaskDescription.h>
|
#include <moveit_task_constructor/TaskDescription.h>
|
||||||
#include <moveit_task_constructor/TaskStatistics.h>
|
#include <moveit_task_constructor/TaskStatistics.h>
|
||||||
#include <moveit_task_constructor/Solution.h>
|
#include <moveit_task_constructor/Solution.h>
|
||||||
#include <moveit_task_constructor/GetSolution.h>
|
#include <moveit_task_constructor/GetSolution.h>
|
||||||
#include <boost/bimap.hpp>
|
|
||||||
|
|
||||||
#define DESCRIPTION_TOPIC "description"
|
#define DESCRIPTION_TOPIC "description"
|
||||||
#define STATISTICS_TOPIC "statistics"
|
#define STATISTICS_TOPIC "statistics"
|
||||||
@ -19,30 +16,19 @@ MOVEIT_CLASS_FORWARD(Stage)
|
|||||||
MOVEIT_CLASS_FORWARD(Task)
|
MOVEIT_CLASS_FORWARD(Task)
|
||||||
MOVEIT_CLASS_FORWARD(SolutionBase)
|
MOVEIT_CLASS_FORWARD(SolutionBase)
|
||||||
|
|
||||||
|
class IntrospectionPrivate;
|
||||||
|
|
||||||
/** The Introspection class provides publishing of task state and solutions.
|
/** The Introspection class provides publishing of task state and solutions.
|
||||||
*
|
*
|
||||||
* It is interlinked to its task.
|
* It is interlinked to its task.
|
||||||
*/
|
*/
|
||||||
class Introspection {
|
class Introspection {
|
||||||
ros::NodeHandle nh_;
|
IntrospectionPrivate *impl;
|
||||||
/// associated task
|
|
||||||
const Task &task_;
|
|
||||||
|
|
||||||
/// publish task detailed description and current state
|
|
||||||
ros::Publisher task_description_publisher_;
|
|
||||||
ros::Publisher task_statistics_publisher_;
|
|
||||||
/// publish new solutions
|
|
||||||
ros::Publisher solution_publisher_;
|
|
||||||
/// services to provide an individual Solution
|
|
||||||
ros::ServiceServer get_solution_service_;
|
|
||||||
|
|
||||||
/// mapping from stages to their id
|
|
||||||
std::map<const void*, moveit_task_constructor::StageStatistics::_id_type> stage_to_id_map_;
|
|
||||||
boost::bimap<uint32_t, const SolutionBase*> id_solution_bimap_;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Introspection(const Task &task);
|
Introspection(const Task &task);
|
||||||
Introspection(const Introspection &other) = delete;
|
Introspection(const Introspection &other) = delete;
|
||||||
|
~Introspection();
|
||||||
|
|
||||||
/// fill task description message for publishing the task configuration
|
/// fill task description message for publishing the task configuration
|
||||||
moveit_task_constructor::TaskDescription& fillTaskDescription(moveit_task_constructor::TaskDescription& msg);
|
moveit_task_constructor::TaskDescription& fillTaskDescription(moveit_task_constructor::TaskDescription& msg);
|
||||||
|
|||||||
@ -3,43 +3,27 @@
|
|||||||
#include <moveit_task_constructor/task.h>
|
#include <moveit_task_constructor/task.h>
|
||||||
#include <moveit_task_constructor/storage.h>
|
#include <moveit_task_constructor/storage.h>
|
||||||
|
|
||||||
#include <ros/ros.h>
|
#include <ros/node_handle.h>
|
||||||
|
#include <ros/publisher.h>
|
||||||
|
#include <ros/service.h>
|
||||||
#include <moveit/planning_scene/planning_scene.h>
|
#include <moveit/planning_scene/planning_scene.h>
|
||||||
|
|
||||||
|
#include <boost/bimap.hpp>
|
||||||
|
|
||||||
namespace moveit { namespace task_constructor {
|
namespace moveit { namespace task_constructor {
|
||||||
|
|
||||||
Introspection::Introspection(const Task &task)
|
class IntrospectionPrivate {
|
||||||
|
public:
|
||||||
|
IntrospectionPrivate(const Task &task)
|
||||||
: nh_(std::string("~/") + task.id()) // topics + services are advertised in private namespace
|
: nh_(std::string("~/") + task.id()) // topics + services are advertised in private namespace
|
||||||
, task_(task)
|
, task_(task)
|
||||||
{
|
{
|
||||||
stage_to_id_map_[&task_] = 0; // root is task having ID = 0
|
resetMaps();
|
||||||
|
|
||||||
task_description_publisher_ = nh_.advertise<moveit_task_constructor::TaskDescription>(DESCRIPTION_TOPIC, 1, true);
|
task_description_publisher_ = nh_.advertise<moveit_task_constructor::TaskDescription>(DESCRIPTION_TOPIC, 1, true);
|
||||||
task_statistics_publisher_ = nh_.advertise<moveit_task_constructor::TaskStatistics>(STATISTICS_TOPIC, 1);
|
task_statistics_publisher_ = nh_.advertise<moveit_task_constructor::TaskStatistics>(STATISTICS_TOPIC, 1);
|
||||||
solution_publisher_ = nh_.advertise<::moveit_task_constructor::Solution>(SOLUTION_TOPIC, 1, true);
|
solution_publisher_ = nh_.advertise<::moveit_task_constructor::Solution>(SOLUTION_TOPIC, 1, true);
|
||||||
|
|
||||||
get_solution_service_ = nh_.advertiseService("get_solution", &Introspection::getSolution, this);
|
|
||||||
}
|
}
|
||||||
|
void resetMaps () {
|
||||||
void Introspection::publishTaskDescription()
|
|
||||||
{
|
|
||||||
::moveit_task_constructor::TaskDescription msg;
|
|
||||||
task_description_publisher_.publish(fillTaskDescription(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Introspection::publishTaskState()
|
|
||||||
{
|
|
||||||
::moveit_task_constructor::TaskStatistics msg;
|
|
||||||
task_statistics_publisher_.publish(fillTaskStatistics(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Introspection::reset()
|
|
||||||
{
|
|
||||||
// send empty task description message to indicate reset
|
|
||||||
::moveit_task_constructor::TaskDescription msg;
|
|
||||||
msg.id = task_.id();
|
|
||||||
task_description_publisher_.publish(msg);
|
|
||||||
|
|
||||||
// reset maps
|
// reset maps
|
||||||
stage_to_id_map_.clear();
|
stage_to_id_map_.clear();
|
||||||
stage_to_id_map_[&task_] = 0; // root is task having ID = 0
|
stage_to_id_map_[&task_] = 0; // root is task having ID = 0
|
||||||
@ -47,13 +31,63 @@ void Introspection::reset()
|
|||||||
id_solution_bimap_.clear();
|
id_solution_bimap_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ros::NodeHandle nh_;
|
||||||
|
/// associated task
|
||||||
|
const Task &task_;
|
||||||
|
|
||||||
|
/// publish task detailed description and current state
|
||||||
|
ros::Publisher task_description_publisher_;
|
||||||
|
ros::Publisher task_statistics_publisher_;
|
||||||
|
/// publish new solutions
|
||||||
|
ros::Publisher solution_publisher_;
|
||||||
|
/// services to provide an individual Solution
|
||||||
|
ros::ServiceServer get_solution_service_;
|
||||||
|
|
||||||
|
/// mapping from stages to their id
|
||||||
|
std::map<const void*, moveit_task_constructor::StageStatistics::_id_type> stage_to_id_map_;
|
||||||
|
boost::bimap<uint32_t, const SolutionBase*> id_solution_bimap_;
|
||||||
|
};
|
||||||
|
|
||||||
|
Introspection::Introspection(const Task &task)
|
||||||
|
: impl(new IntrospectionPrivate(task))
|
||||||
|
{
|
||||||
|
impl->get_solution_service_ = impl->nh_.advertiseService("get_solution", &Introspection::getSolution, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
Introspection::~Introspection()
|
||||||
|
{
|
||||||
|
delete impl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Introspection::publishTaskDescription()
|
||||||
|
{
|
||||||
|
::moveit_task_constructor::TaskDescription msg;
|
||||||
|
impl->task_description_publisher_.publish(fillTaskDescription(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Introspection::publishTaskState()
|
||||||
|
{
|
||||||
|
::moveit_task_constructor::TaskStatistics msg;
|
||||||
|
impl->task_statistics_publisher_.publish(fillTaskStatistics(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Introspection::reset()
|
||||||
|
{
|
||||||
|
// send empty task description message to indicate reset
|
||||||
|
::moveit_task_constructor::TaskDescription msg;
|
||||||
|
msg.id = impl->task_.id();
|
||||||
|
impl->task_description_publisher_.publish(msg);
|
||||||
|
|
||||||
|
impl->resetMaps();
|
||||||
|
}
|
||||||
|
|
||||||
void Introspection::publishSolution(const SolutionBase &s)
|
void Introspection::publishSolution(const SolutionBase &s)
|
||||||
{
|
{
|
||||||
::moveit_task_constructor::Solution msg;
|
::moveit_task_constructor::Solution msg;
|
||||||
s.fillMessage(msg, this);
|
s.fillMessage(msg, this);
|
||||||
msg.task_id = task_.id();
|
msg.task_id = impl->task_.id();
|
||||||
s.start()->scene()->getPlanningSceneMsg(msg.start_scene);
|
s.start()->scene()->getPlanningSceneMsg(msg.start_scene);
|
||||||
solution_publisher_.publish(msg);
|
impl->solution_publisher_.publish(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Introspection::publishAllSolutions(bool wait) const
|
void Introspection::publishAllSolutions(bool wait) const
|
||||||
@ -61,7 +95,7 @@ void Introspection::publishAllSolutions(bool wait) const
|
|||||||
Task::SolutionProcessor processor
|
Task::SolutionProcessor processor
|
||||||
= [this, wait](const ::moveit_task_constructor::Solution& msg, double cost) {
|
= [this, wait](const ::moveit_task_constructor::Solution& msg, double cost) {
|
||||||
std::cout << "publishing solution with cost: " << cost << std::endl;
|
std::cout << "publishing solution with cost: " << cost << std::endl;
|
||||||
solution_publisher_.publish(msg);
|
impl->solution_publisher_.publish(msg);
|
||||||
if (wait) {
|
if (wait) {
|
||||||
std::cout << "Press <Enter> to continue ..." << std::endl;
|
std::cout << "Press <Enter> to continue ..." << std::endl;
|
||||||
int ch = getchar();
|
int ch = getchar();
|
||||||
@ -71,14 +105,14 @@ void Introspection::publishAllSolutions(bool wait) const
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
task_.processSolutions(processor);
|
impl->task_.processSolutions(processor);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Introspection::getSolution(moveit_task_constructor::GetSolution::Request &req,
|
bool Introspection::getSolution(moveit_task_constructor::GetSolution::Request &req,
|
||||||
moveit_task_constructor::GetSolution::Response &res)
|
moveit_task_constructor::GetSolution::Response &res)
|
||||||
{
|
{
|
||||||
auto it = id_solution_bimap_.left.find(req.solution_id);
|
auto it = impl->id_solution_bimap_.left.find(req.solution_id);
|
||||||
if (it == id_solution_bimap_.left.end())
|
if (it == impl->id_solution_bimap_.left.end())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const SolutionBase& solution = *it->second;
|
const SolutionBase& solution = *it->second;
|
||||||
@ -88,19 +122,19 @@ bool Introspection::getSolution(moveit_task_constructor::GetSolution::Request &
|
|||||||
|
|
||||||
uint32_t Introspection::stageId(const Stage* const s)
|
uint32_t Introspection::stageId(const Stage* const s)
|
||||||
{
|
{
|
||||||
return stage_to_id_map_.insert(std::make_pair(s, stage_to_id_map_.size())).first->second;
|
return impl->stage_to_id_map_.insert(std::make_pair(s, impl->stage_to_id_map_.size())).first->second;
|
||||||
}
|
}
|
||||||
uint32_t Introspection::stageId(const Stage* const s) const
|
uint32_t Introspection::stageId(const Stage* const s) const
|
||||||
{
|
{
|
||||||
auto it = stage_to_id_map_.find(s);
|
auto it = impl->stage_to_id_map_.find(s);
|
||||||
if (it == stage_to_id_map_.end())
|
if (it == impl->stage_to_id_map_.end())
|
||||||
throw std::runtime_error("unknown stage");
|
throw std::runtime_error("unknown stage");
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Introspection::solutionId(const SolutionBase& s)
|
uint32_t Introspection::solutionId(const SolutionBase& s)
|
||||||
{
|
{
|
||||||
auto result = id_solution_bimap_.left.insert(std::make_pair(id_solution_bimap_.size(), &s));
|
auto result = impl->id_solution_bimap_.left.insert(std::make_pair(impl->id_solution_bimap_.size(), &s));
|
||||||
return result.first->first;
|
return result.first->first;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,8 +168,8 @@ moveit_task_constructor::TaskDescription& Introspection::fillTaskDescription(mov
|
|||||||
desc.flags = stage.pimpl()->interfaceFlags();
|
desc.flags = stage.pimpl()->interfaceFlags();
|
||||||
// TODO fill stage properties
|
// TODO fill stage properties
|
||||||
|
|
||||||
auto it = stage_to_id_map_.find(stage.pimpl()->parent());
|
auto it = impl->stage_to_id_map_.find(stage.pimpl()->parent());
|
||||||
assert (it != stage_to_id_map_.cend());
|
assert (it != impl->stage_to_id_map_.cend());
|
||||||
desc.parent_id = stat.parent_id = it->second;
|
desc.parent_id = stat.parent_id = it->second;
|
||||||
|
|
||||||
fillStageStatistics(stage, stat);
|
fillStageStatistics(stage, stat);
|
||||||
@ -148,9 +182,9 @@ moveit_task_constructor::TaskDescription& Introspection::fillTaskDescription(mov
|
|||||||
|
|
||||||
msg.description.clear();
|
msg.description.clear();
|
||||||
msg.statistics.clear();
|
msg.statistics.clear();
|
||||||
task_.stages()->traverseRecursively(stageProcessor);
|
impl->task_.stages()->traverseRecursively(stageProcessor);
|
||||||
|
|
||||||
msg.id = task_.id();
|
msg.id = impl->task_.id();
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,8 +196,8 @@ moveit_task_constructor::TaskStatistics& Introspection::fillTaskStatistics(movei
|
|||||||
moveit_task_constructor::StageStatistics stat; // create new Stage msg
|
moveit_task_constructor::StageStatistics stat; // create new Stage msg
|
||||||
stat.id = stageId(&stage);
|
stat.id = stageId(&stage);
|
||||||
|
|
||||||
auto it = stage_to_id_map_.find(stage.pimpl()->parent());
|
auto it = impl->stage_to_id_map_.find(stage.pimpl()->parent());
|
||||||
assert (it != stage_to_id_map_.cend());
|
assert (it != impl->stage_to_id_map_.cend());
|
||||||
stat.parent_id = it->second;
|
stat.parent_id = it->second;
|
||||||
|
|
||||||
fillStageStatistics(stage, stat);
|
fillStageStatistics(stage, stat);
|
||||||
@ -174,9 +208,9 @@ moveit_task_constructor::TaskStatistics& Introspection::fillTaskStatistics(movei
|
|||||||
};
|
};
|
||||||
|
|
||||||
msg.stages.clear();
|
msg.stages.clear();
|
||||||
task_.stages()->traverseRecursively(stageProcessor);
|
impl->task_.stages()->traverseRecursively(stageProcessor);
|
||||||
|
|
||||||
msg.id = task_.id();
|
msg.id = impl->task_.id();
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user