Skip to content

Commit 02f5e83

Browse files
committed
developing impl using action client
1 parent 17452e2 commit 02f5e83

File tree

3 files changed

+153
-8
lines changed

3 files changed

+153
-8
lines changed

core/include/moveit/task_constructor/stages/gpd_generator.h

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,40 @@
3737
#pragma once
3838

3939
#include <moveit/task_constructor/stages/generate_pose.h>
40+
#include <moveit/task_constructor/stages/action_base.h>
41+
#include <moveit/task_constructor/storage.h>
42+
#include <moveit/planning_scene/planning_scene.h>
43+
44+
#include <Eigen/Geometry>
45+
#include <eigen_conversions/eigen_msg.h>
4046

4147
#include <memory>
4248

4349
#include <moveit_task_constructor_msgs/GenerateDeepGraspPoseAction.h>
44-
#include <actionlib/client/simple_action_client.h>
45-
46-
#include <moveit/task_constructor/action_base.h>
4750

4851

4952
namespace moveit {
5053
namespace task_constructor {
5154
namespace stages {
5255

5356

54-
class DeepGraspPose : public GeneratePose
57+
template<class ActionSpec>
58+
class DeepGraspPose : public GeneratePose, ActionBase<ActionSpec>
5559
{
60+
private:
61+
typedef ActionBase<ActionSpec> ActionBaseT;
62+
ACTION_DEFINITION(ActionSpec);
63+
5664
public:
57-
DeepGraspPose(const std::string& name = "generate grasp pose");
65+
DeepGraspPose(const std::string& action_name,
66+
const std::string& stage_name = "generate grasp pose");
67+
68+
void activeCallback() override;
69+
70+
void feedbackCallback(const FeedbackConstPtr &feedback) override;
71+
72+
void doneCallback(const actionlib::SimpleClientGoalState& state,
73+
const ResultConstPtr &result) override;
5874

5975
void init(const core::RobotModelConstPtr& robot_model) override;
6076
void compute() override;
@@ -70,8 +86,138 @@ class DeepGraspPose : public GeneratePose
7086
protected:
7187
void onNewSolution(const SolutionBase& s) override;
7288

73-
actionlib::SimpleActionClient<moveit_task_constructor_msgs::GenerateDeepGraspPoseAction> client_;
89+
private:
90+
bool found_candidates_;
91+
std::vector<geometry_msgs::PoseStamped> grasp_candidates_;
92+
std::vector<double> costs_;
7493
};
94+
95+
template<class ActionSpec>
96+
DeepGraspPose<ActionSpec>::DeepGraspPose(const std::string& action_name,
97+
const std::string& stage_name)
98+
: GeneratePose(stage_name), ActionBaseT(action_name), found_candidates_(false) {
99+
auto& p = properties();
100+
p.declare<std::string>("eef", "name of end-effector");
101+
p.declare<std::string>("object");
102+
p.declare<double>("angle_delta", 0.1, "angular steps (rad)");
103+
104+
p.declare<boost::any>("pregrasp", "pregrasp posture");
105+
p.declare<boost::any>("grasp", "grasp posture");
106+
107+
ROS_INFO("Waiting for connection to grasp generation action server...");
108+
ActionBaseT::clientPtr_->waitForServer(ros::Duration(ActionBaseT::server_timeout_));
109+
}
110+
111+
112+
template<class ActionSpec>
113+
void DeepGraspPose<ActionSpec>::activeCallback(){
114+
ROS_INFO("Generate grasp goal now active ");
115+
// found_candidates_ = false;
116+
}
117+
118+
119+
template<class ActionSpec>
120+
void DeepGraspPose<ActionSpec>::feedbackCallback(const FeedbackConstPtr &feedback){
121+
// TODO: Check if costs are positive ?
122+
123+
// each candidate should have a cost
124+
if (feedback->grasp_candidates.size() != feedback->costs.size()){
125+
ROS_ERROR("Invalid input: each grasp candidate needs an associated cost");
126+
}
127+
else{
128+
grasp_candidates_.resize(feedback->grasp_candidates.size());
129+
costs_.resize(feedback->costs.size());
130+
131+
grasp_candidates_ = feedback->grasp_candidates;
132+
costs_ = feedback->costs;
133+
}
134+
}
135+
136+
137+
template<class ActionSpec>
138+
void DeepGraspPose<ActionSpec>::doneCallback(const actionlib::SimpleClientGoalState& state,
139+
const ResultConstPtr &result){
140+
// TODO: add completion timeout
141+
142+
if (state == actionlib::SimpleClientGoalState::SUCCEEDED){
143+
found_candidates_ = true;
144+
ROS_INFO("Successfully found grasp candidates (result): %s", result->grasp_state.c_str());
145+
}
146+
else{
147+
ROS_WARN("No grasp candidates found (state): %s", ActionBaseT::clientPtr_->getState().toString().c_str());
148+
}
149+
}
150+
151+
152+
template<class ActionSpec>
153+
void DeepGraspPose<ActionSpec>::init(const core::RobotModelConstPtr& robot_model){
154+
InitStageException errors;
155+
try {
156+
GeneratePose::init(robot_model);
157+
} catch (InitStageException& e) {
158+
errors.append(e);
159+
}
160+
161+
const auto& props = properties();
162+
163+
// check angle_delta
164+
if (props.get<double>("angle_delta") == 0.){
165+
errors.push_back(*this, "angle_delta must be non-zero");
166+
}
167+
168+
// check availability of object
169+
props.get<std::string>("object");
170+
// check availability of eef
171+
const std::string& eef = props.get<std::string>("eef");
172+
if (!robot_model->hasEndEffector(eef)){
173+
errors.push_back(*this, "unknown end effector: " + eef);
174+
}
175+
else {
176+
// check availability of eef pose
177+
const moveit::core::JointModelGroup* jmg = robot_model->getEndEffector(eef);
178+
const std::string& name = props.get<std::string>("pregrasp");
179+
std::map<std::string, double> m;
180+
if (!jmg->getVariableDefaultPositions(name, m)){
181+
errors.push_back(*this, "unknown end effector pose: " + name);
182+
}
183+
}
184+
185+
if (errors){
186+
throw errors;
187+
}
188+
}
189+
190+
191+
template<class ActionSpec>
192+
void DeepGraspPose<ActionSpec>::compute(){
193+
194+
195+
}
196+
197+
198+
template<class ActionSpec>
199+
void DeepGraspPose<ActionSpec>::onNewSolution(const SolutionBase& s){
200+
planning_scene::PlanningSceneConstPtr scene = s.end()->scene();
201+
202+
const auto& props = properties();
203+
const std::string& object = props.get<std::string>("object");
204+
if (!scene->knowsFrameTransform(object)) {
205+
const std::string msg = "object '" + object + "' not in scene";
206+
if (storeFailures()) {
207+
InterfaceState state(scene);
208+
SubTrajectory solution;
209+
solution.markAsFailure();
210+
solution.setComment(msg);
211+
spawn(std::move(state), std::move(solution));
212+
} else{
213+
ROS_WARN_STREAM_NAMED("GenerateGraspPose", msg);
214+
}
215+
return;
216+
}
217+
218+
upstream_solutions_.push(&s);
219+
}
220+
75221
} // namespace stages
76222
} // namespace task_constructor
77223
} // namespace moveit

core/src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
add_library(${PROJECT_NAME}
2-
${PROJECT_INCLUDE}/action_base.h
32
${PROJECT_INCLUDE}/container.h
43
${PROJECT_INCLUDE}/container_p.h
54
${PROJECT_INCLUDE}/cost_queue.h

core/src/stages/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_library(${PROJECT_NAME}_stages
22
${PROJECT_INCLUDE}/stages/modify_planning_scene.h
33
${PROJECT_INCLUDE}/stages/fix_collision_objects.h
44

5+
${PROJECT_INCLUDE}/stages/action_base.h
56
${PROJECT_INCLUDE}/stages/current_state.h
67
${PROJECT_INCLUDE}/stages/gpd_generator.h
78
${PROJECT_INCLUDE}/stages/fixed_state.h
@@ -28,7 +29,6 @@ add_library(${PROJECT_NAME}_stages
2829
generate_pose.cpp
2930
generate_grasp_pose.cpp
3031
generate_place_pose.cpp
31-
gpd_generator.cpp
3232
compute_ik.cpp
3333
predicate_filter.cpp
3434

0 commit comments

Comments
 (0)