37
37
#pragma once
38
38
39
39
#include < functional>
40
+ #include < mutex>
41
+
40
42
#include < moveit/task_constructor/stages/generate_pose.h>
41
43
#include < moveit/task_constructor/stages/action_base.h>
42
44
#include < moveit/task_constructor/storage.h>
@@ -48,20 +50,9 @@ namespace moveit {
48
50
namespace task_constructor {
49
51
namespace stages {
50
52
51
- constexpr char LOGNAME[] = " grasp_provider" ;
52
-
53
- /* *
54
- * @brief Generate grasp candidates using deep learning approaches
55
- * @param ActionSpec - action message (action message name + "ACTION")
56
- * @details Interfaces with a deep learning based grasp library using a action client
57
- */
58
- template <class ActionSpec >
59
- class GraspProvider : public GeneratePose , ActionBase<ActionSpec>
53
+ /* * @brief Generate grasp candidates using deep learning approaches */
54
+ class GraspProvider : public GeneratePose , ActionBase
60
55
{
61
- private:
62
- typedef ActionBase<ActionSpec> ActionBaseT;
63
- ACTION_DEFINITION (ActionSpec);
64
-
65
56
public:
66
57
/* *
67
58
* @brief Constructor
@@ -71,7 +62,7 @@ class GraspProvider : public GeneratePose, ActionBase<ActionSpec>
71
62
* @param server_timeout - connection to server time out (0 is considered infinite timeout)
72
63
* @details Initialize the client and connect to server
73
64
*/
74
- GraspProvider (const std::string& action_name, const std::string& stage_name = " generate grasp pose " ,
65
+ GraspProvider (const std::string& action_name, const std::string& stage_name = " grasp provider " ,
75
66
double goal_timeout = 0.0 , double server_timeout = 0.0 );
76
67
77
68
/* *
@@ -88,8 +79,9 @@ class GraspProvider : public GeneratePose, ActionBase<ActionSpec>
88
79
bool monitorGoal ();
89
80
90
81
void activeCallback () override ;
91
- void feedbackCallback (const FeedbackConstPtr& feedback) override ;
92
- void doneCallback (const actionlib::SimpleClientGoalState& state, const ResultConstPtr& result) override ;
82
+ void feedbackCallback (const grasping_msgs::GraspPlanningFeedbackConstPtr& feedback) override ;
83
+ void doneCallback (const actionlib::SimpleClientGoalState& state,
84
+ const grasping_msgs::GraspPlanningResultConstPtr& result) override ;
93
85
94
86
void init (const core::RobotModelConstPtr& robot_model) override ;
95
87
void compute () override ;
@@ -106,191 +98,10 @@ class GraspProvider : public GeneratePose, ActionBase<ActionSpec>
106
98
void onNewSolution (const SolutionBase& s) override ;
107
99
108
100
private:
101
+ std::mutex grasp_mutex_;
109
102
bool found_candidates_;
110
- std::vector<geometry_msgs::PoseStamped> grasp_candidates_;
111
- std::vector<double > costs_;
103
+ std::vector<moveit_msgs::Grasp> grasp_candidates_;
112
104
};
113
-
114
- template <class ActionSpec >
115
- GraspProvider<ActionSpec>::GraspProvider(const std::string& action_name, const std::string& stage_name,
116
- double goal_timeout, double server_timeout)
117
- : GeneratePose(stage_name), ActionBaseT(action_name, false , goal_timeout, server_timeout), found_candidates_(false ) {
118
- auto & p = properties ();
119
- p.declare <std::string>(" eef" , " name of end-effector" );
120
- p.declare <std::string>(" object" );
121
- p.declare <boost::any>(" pregrasp" , " pregrasp posture" );
122
- p.declare <boost::any>(" grasp" , " grasp posture" );
123
-
124
- ROS_INFO_NAMED (LOGNAME, " Waiting for connection to grasp generation action server..." );
125
- ActionBaseT::clientPtr_->waitForServer (ros::Duration (ActionBaseT::server_timeout_));
126
- ROS_INFO_NAMED (LOGNAME, " Connected to server" );
127
- }
128
-
129
- template <class ActionSpec >
130
- void GraspProvider<ActionSpec>::composeGoal() {
131
- Goal goal;
132
- goal.action_name = ActionBaseT::action_name_;
133
- ActionBaseT::clientPtr_->sendGoal (
134
- goal, std::bind (&GraspProvider<ActionSpec>::doneCallback, this , std::placeholders::_1, std::placeholders::_2),
135
- std::bind (&GraspProvider<ActionSpec>::activeCallback, this ),
136
- std::bind (&GraspProvider<ActionSpec>::feedbackCallback, this , std::placeholders::_1));
137
-
138
- ROS_INFO_NAMED (LOGNAME, " Goal sent to server: %s" , ActionBaseT::action_name_.c_str ());
139
- }
140
-
141
- template <class ActionSpec >
142
- bool GraspProvider<ActionSpec>::monitorGoal() {
143
- // monitor timeout
144
- const bool monitor_timeout = ActionBaseT::goal_timeout_ > std::numeric_limits<double >::epsilon () ? true : false ;
145
- const double timeout_time = ros::Time::now ().toSec () + ActionBaseT::goal_timeout_;
146
-
147
- while (ActionBaseT::nh_.ok ()) {
148
- ros::spinOnce ();
149
-
150
- // timeout reached
151
- if (ros::Time::now ().toSec () > timeout_time && monitor_timeout) {
152
- ActionBaseT::clientPtr_->cancelGoal ();
153
- ROS_ERROR_NAMED (LOGNAME, " Grasp pose generator time out reached" );
154
- return false ;
155
- } else if (found_candidates_) {
156
- // timeout not reached (or not active) and grasps are found
157
- // only way return true
158
- break ;
159
- }
160
- }
161
-
162
- return true ;
163
- }
164
-
165
- template <class ActionSpec >
166
- void GraspProvider<ActionSpec>::activeCallback() {
167
- ROS_INFO_NAMED (LOGNAME, " Generate grasp goal now active" );
168
- found_candidates_ = false ;
169
- }
170
-
171
- template <class ActionSpec >
172
- void GraspProvider<ActionSpec>::feedbackCallback(const FeedbackConstPtr& feedback) {
173
- // each candidate should have a cost
174
- if (feedback->grasp_candidates .size () != feedback->costs .size ()) {
175
- ROS_ERROR_NAMED (LOGNAME, " Invalid input: each grasp candidate needs an associated cost" );
176
- } else {
177
- ROS_INFO_NAMED (LOGNAME, " Grasp generated feedback received %lu candidates: " , feedback->grasp_candidates .size ());
178
-
179
- grasp_candidates_.resize (feedback->grasp_candidates .size ());
180
- costs_.resize (feedback->costs .size ());
181
-
182
- grasp_candidates_ = feedback->grasp_candidates ;
183
- costs_ = feedback->costs ;
184
-
185
- found_candidates_ = true ;
186
- }
187
- }
188
-
189
- template <class ActionSpec >
190
- void GraspProvider<ActionSpec>::doneCallback(const actionlib::SimpleClientGoalState& state,
191
- const ResultConstPtr& result) {
192
- if (state == actionlib::SimpleClientGoalState::SUCCEEDED) {
193
- ROS_INFO_NAMED (LOGNAME, " Found grasp candidates (result): %s" , result->grasp_state .c_str ());
194
- } else {
195
- ROS_ERROR_NAMED (LOGNAME, " No grasp candidates found (state): %s" ,
196
- ActionBaseT::clientPtr_->getState ().toString ().c_str ());
197
- }
198
- }
199
-
200
- template <class ActionSpec >
201
- void GraspProvider<ActionSpec>::init(const core::RobotModelConstPtr& robot_model) {
202
- InitStageException errors;
203
- try {
204
- GeneratePose::init (robot_model);
205
- } catch (InitStageException& e) {
206
- errors.append (e);
207
- }
208
-
209
- const auto & props = properties ();
210
-
211
- // check availability of object
212
- props.get <std::string>(" object" );
213
- // check availability of eef
214
- const std::string& eef = props.get <std::string>(" eef" );
215
- if (!robot_model->hasEndEffector (eef)) {
216
- errors.push_back (*this , " unknown end effector: " + eef);
217
- } else {
218
- // check availability of eef pose
219
- const moveit::core::JointModelGroup* jmg = robot_model->getEndEffector (eef);
220
- const std::string& name = props.get <std::string>(" pregrasp" );
221
- std::map<std::string, double > m;
222
- if (!jmg->getVariableDefaultPositions (name, m)) {
223
- errors.push_back (*this , " unknown end effector pose: " + name);
224
- }
225
- }
226
-
227
- if (errors) {
228
- throw errors;
229
- }
230
- }
231
-
232
- template <class ActionSpec >
233
- void GraspProvider<ActionSpec>::compute() {
234
- if (upstream_solutions_.empty ()) {
235
- return ;
236
- }
237
- planning_scene::PlanningScenePtr scene = upstream_solutions_.pop ()->end ()->scene ()->diff ();
238
-
239
- // set end effector pose
240
- const auto & props = properties ();
241
- const std::string& eef = props.get <std::string>(" eef" );
242
- const moveit::core::JointModelGroup* jmg = scene->getRobotModel ()->getEndEffector (eef);
243
-
244
- robot_state::RobotState& robot_state = scene->getCurrentStateNonConst ();
245
- robot_state.setToDefaultValues (jmg, props.get <std::string>(" pregrasp" ));
246
-
247
- // compose/send goal
248
- composeGoal ();
249
-
250
- // monitor feedback/results
251
- // blocking function untill timeout reached or results received
252
- if (monitorGoal ()) {
253
- // ROS_WARN_NAMED(LOGNAME, "number %lu: ",grasp_candidates_.size());
254
- for (unsigned int i = 0 ; i < grasp_candidates_.size (); i++) {
255
- InterfaceState state (scene);
256
- state.properties ().set (" target_pose" , grasp_candidates_.at (i));
257
- props.exposeTo (state.properties (), { " pregrasp" , " grasp" });
258
-
259
- SubTrajectory trajectory;
260
- trajectory.setCost (costs_.at (i));
261
- trajectory.setComment (std::to_string (i));
262
-
263
- // add frame at target pose
264
- rviz_marker_tools::appendFrame (trajectory.markers (), grasp_candidates_.at (i), 0.1 , " grasp frame" );
265
-
266
- spawn (std::move (state), std::move (trajectory));
267
- }
268
- }
269
- }
270
-
271
- template <class ActionSpec >
272
- void GraspProvider<ActionSpec>::onNewSolution(const SolutionBase& s) {
273
- planning_scene::PlanningSceneConstPtr scene = s.end ()->scene ();
274
-
275
- const auto & props = properties ();
276
- const std::string& object = props.get <std::string>(" object" );
277
- if (!scene->knowsFrameTransform (object)) {
278
- const std::string msg = " object '" + object + " ' not in scene" ;
279
- if (storeFailures ()) {
280
- InterfaceState state (scene);
281
- SubTrajectory solution;
282
- solution.markAsFailure ();
283
- solution.setComment (msg);
284
- spawn (std::move (state), std::move (solution));
285
- } else {
286
- ROS_WARN_STREAM_NAMED (LOGNAME, msg);
287
- }
288
- return ;
289
- }
290
-
291
- upstream_solutions_.push (&s);
292
- }
293
-
294
105
} // namespace stages
295
106
} // namespace task_constructor
296
107
} // namespace moveit
0 commit comments