-
Notifications
You must be signed in to change notification settings - Fork 52
/
deep_pick_place_task.cpp
468 lines (417 loc) · 19.7 KB
/
deep_pick_place_task.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*********************************************************************
* BSD 3-Clause License
*
* Copyright (c) 2020 PickNik Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/* Author: Henning Kayser, Simon Goldstein, Boston Cleek
Desc: A demo to show MoveIt Task Constructor using a deep learning based
grasp generator
*/
#include <deep_grasp_task/deep_pick_place_task.h>
#include <rosparam_shortcuts/rosparam_shortcuts.h>
namespace deep_grasp_task
{
constexpr char LOGNAME[] = "pick_place_task";
DeepPickPlaceTask::DeepPickPlaceTask(const std::string& task_name, const ros::NodeHandle& nh)
: nh_(nh), task_name_(task_name), execute_("execute_task_solution", true)
{
}
void DeepPickPlaceTask::loadParameters()
{
/****************************************************
* *
* Load Parameters *
* *
***************************************************/
ROS_INFO_NAMED(LOGNAME, "Loading task parameters");
ros::NodeHandle pnh("~");
// Planning group properties
size_t errors = 0;
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "arm_group_name", arm_group_name_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "hand_group_name", hand_group_name_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "eef_name", eef_name_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "hand_frame", hand_frame_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "world_frame", world_frame_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "grasp_frame_transform", grasp_frame_transform_);
// Predefined pose targets
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "hand_open_pose", hand_open_pose_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "hand_close_pose", hand_close_pose_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "arm_home_pose", arm_home_pose_);
// Target object
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "object_name", object_name_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "object_dimensions", object_dimensions_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "object_reference_frame", object_reference_frame_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "surface_link", surface_link_);
support_surfaces_ = { surface_link_ };
// Deep grasp properties
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "action_name", action_name_);
// Pick/Place metrics
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "approach_object_min_dist", approach_object_min_dist_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "approach_object_max_dist", approach_object_max_dist_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "lift_object_min_dist", lift_object_min_dist_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "lift_object_max_dist", lift_object_max_dist_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "place_surface_offset", place_surface_offset_);
errors += !rosparam_shortcuts::get(LOGNAME, pnh, "place_pose", place_pose_);
rosparam_shortcuts::shutdownIfError(LOGNAME, errors);
}
void DeepPickPlaceTask::init()
{
ROS_INFO_NAMED(LOGNAME, "Initializing task pipeline");
const std::string object = object_name_;
// Reset ROS introspection before constructing the new object
// TODO(henningkayser): verify this is a bug, fix if possible
task_.reset();
task_.reset(new moveit::task_constructor::Task());
Task& t = *task_;
t.stages()->setName(task_name_);
t.loadRobotModel();
// Sampling planner
auto sampling_planner = std::make_shared<solvers::PipelinePlanner>();
sampling_planner->setProperty("goal_joint_tolerance", 1e-5);
// Cartesian planner
auto cartesian_planner = std::make_shared<solvers::CartesianPath>();
cartesian_planner->setMaxVelocityScaling(1.0);
cartesian_planner->setMaxAccelerationScaling(1.0);
cartesian_planner->setStepSize(.001);
// Set task properties
t.setProperty("group", arm_group_name_);
t.setProperty("eef", eef_name_);
t.setProperty("hand", hand_group_name_);
t.setProperty("hand_grasping_frame", hand_frame_);
t.setProperty("ik_frame", hand_frame_);
/****************************************************
* *
* Current State *
* *
***************************************************/
Stage* current_state_ptr = nullptr; // Forward current_state on to grasp pose generator
{
auto current_state = std::make_unique<stages::CurrentState>("current state");
// Verify that object is not attached
auto applicability_filter =
std::make_unique<stages::PredicateFilter>("applicability test", std::move(current_state));
applicability_filter->setPredicate([object](const SolutionBase& s, std::string& comment) {
if (s.start()->scene()->getCurrentState().hasAttachedBody(object))
{
comment = "object with id '" + object + "' is already attached and cannot be picked";
return false;
}
return true;
});
current_state_ptr = applicability_filter.get();
t.add(std::move(applicability_filter));
}
/****************************************************
* *
* Open Hand *
* *
***************************************************/
{ // Open Hand
auto stage = std::make_unique<stages::MoveTo>("open hand", sampling_planner);
stage->setGroup(hand_group_name_);
stage->setGoal(hand_open_pose_);
t.add(std::move(stage));
}
/****************************************************
* *
* Move to Pick *
* *
***************************************************/
{ // Move-to pre-grasp
auto stage = std::make_unique<stages::Connect>(
"move to pick", stages::Connect::GroupPlannerVector{ { arm_group_name_, sampling_planner } });
stage->setTimeout(5.0);
stage->properties().configureInitFrom(Stage::PARENT);
t.add(std::move(stage));
}
/****************************************************
* *
* Pick Object *
* *
***************************************************/
Stage* attach_object_stage = nullptr; // Forward attach_object_stage to place pose generator
{
auto grasp = std::make_unique<SerialContainer>("pick object");
t.properties().exposeTo(grasp->properties(), { "eef", "hand", "group", "ik_frame" });
grasp->properties().configureInitFrom(Stage::PARENT, { "eef", "hand", "group", "ik_frame" });
/****************************************************
---- * Approach Object *
***************************************************/
{
auto stage = std::make_unique<stages::MoveRelative>("approach object", cartesian_planner);
stage->properties().set("marker_ns", "approach_object");
stage->properties().set("link", hand_frame_);
stage->properties().configureInitFrom(Stage::PARENT, { "group" });
stage->setMinMaxDistance(approach_object_min_dist_, approach_object_max_dist_);
// Set hand forward direction
geometry_msgs::Vector3Stamped vec;
vec.header.frame_id = hand_frame_;
vec.vector.z = 1.0;
stage->setDirection(vec);
grasp->insert(std::move(stage));
}
/****************************************************
---- * Generate Grasp Pose *
***************************************************/
{
auto stage = std::make_unique<stages::DeepGraspPose<moveit_task_constructor_msgs::SampleGraspPosesAction>>(
action_name_, "generate grasp pose");
stage->properties().configureInitFrom(Stage::PARENT);
stage->properties().set("marker_ns", "grasp_pose");
stage->setPreGraspPose(hand_open_pose_);
stage->setObject(object);
stage->setMonitoredStage(current_state_ptr); // Hook into current state
// Compute IK
auto wrapper = std::make_unique<stages::ComputeIK>("grasp pose IK", std::move(stage));
wrapper->setMaxIKSolutions(8);
wrapper->setMinSolutionDistance(1.0);
wrapper->setIKFrame(grasp_frame_transform_, hand_frame_);
wrapper->properties().configureInitFrom(Stage::PARENT, { "eef", "group" });
wrapper->properties().configureInitFrom(Stage::INTERFACE, { "target_pose" });
grasp->insert(std::move(wrapper));
}
/****************************************************
---- * Allow Collision (hand object) *
***************************************************/
{
auto stage = std::make_unique<stages::ModifyPlanningScene>("allow collision (hand,object)");
stage->allowCollisions(
object, t.getRobotModel()->getJointModelGroup(hand_group_name_)->getLinkModelNamesWithCollisionGeometry(),
true);
grasp->insert(std::move(stage));
}
/****************************************************
---- * Close Hand *
***************************************************/
{
auto stage = std::make_unique<stages::MoveTo>("close hand", sampling_planner);
stage->properties().property("group").configureInitFrom(Stage::PARENT, hand_group_name_);
stage->setGoal(hand_close_pose_);
grasp->insert(std::move(stage));
}
/****************************************************
.... * Attach Object *
***************************************************/
{
auto stage = std::make_unique<stages::ModifyPlanningScene>("attach object");
stage->attachObject(object, hand_frame_);
attach_object_stage = stage.get();
grasp->insert(std::move(stage));
}
/****************************************************
.... * Allow collision (object support) *
***************************************************/
{
auto stage = std::make_unique<stages::ModifyPlanningScene>("allow collision (object,support)");
stage->allowCollisions({ object }, support_surfaces_, true);
grasp->insert(std::move(stage));
}
/****************************************************
.... * Lift object *
***************************************************/
{
auto stage = std::make_unique<stages::MoveRelative>("lift object", cartesian_planner);
stage->properties().configureInitFrom(Stage::PARENT, { "group" });
stage->setMinMaxDistance(lift_object_min_dist_, lift_object_max_dist_);
stage->setIKFrame(hand_frame_);
stage->properties().set("marker_ns", "lift_object");
// Set upward direction
geometry_msgs::Vector3Stamped vec;
vec.header.frame_id = world_frame_;
vec.vector.z = 1.0;
stage->setDirection(vec);
grasp->insert(std::move(stage));
}
/****************************************************
.... * Forbid collision (object support) *
***************************************************/
{
auto stage = std::make_unique<stages::ModifyPlanningScene>("forbid collision (object,surface)");
stage->allowCollisions({ object }, support_surfaces_, false);
grasp->insert(std::move(stage));
}
// Add grasp container to task
t.add(std::move(grasp));
}
/******************************************************
* *
* Move to Place *
* *
*****************************************************/
{
auto stage = std::make_unique<stages::Connect>(
"move to place", stages::Connect::GroupPlannerVector{ { arm_group_name_, sampling_planner } });
stage->setTimeout(5.0);
stage->properties().configureInitFrom(Stage::PARENT);
t.add(std::move(stage));
}
/******************************************************
* *
* Place Object *
* *
*****************************************************/
{
auto place = std::make_unique<SerialContainer>("place object");
t.properties().exposeTo(place->properties(), { "eef", "hand", "group" });
place->properties().configureInitFrom(Stage::PARENT, { "eef", "hand", "group" });
/******************************************************
---- * Lower Object *
*****************************************************/
{
auto stage = std::make_unique<stages::MoveRelative>("lower object", cartesian_planner);
stage->properties().set("marker_ns", "lower_object");
stage->properties().set("link", hand_frame_);
stage->properties().configureInitFrom(Stage::PARENT, { "group" });
stage->setMinMaxDistance(.03, .13);
// Set downward direction
geometry_msgs::Vector3Stamped vec;
vec.header.frame_id = world_frame_;
vec.vector.z = -1.0;
stage->setDirection(vec);
place->insert(std::move(stage));
}
/******************************************************
---- * Generate Place Pose *
*****************************************************/
{
// Generate Place Pose
auto stage = std::make_unique<stages::GeneratePlacePose>("generate place pose");
stage->properties().configureInitFrom(Stage::PARENT, { "ik_frame" });
stage->properties().set("marker_ns", "place_pose");
stage->setObject(object);
// Set target pose
geometry_msgs::PoseStamped p;
p.header.frame_id = object_reference_frame_;
p.pose = place_pose_;
p.pose.position.z += 0.5 * object_dimensions_.at(0) + place_surface_offset_;
stage->setPose(p);
stage->setMonitoredStage(attach_object_stage); // Hook into attach_object_stage
// Compute IK
auto wrapper = std::make_unique<stages::ComputeIK>("place pose IK", std::move(stage));
wrapper->setMaxIKSolutions(2);
wrapper->setIKFrame(grasp_frame_transform_, hand_frame_);
wrapper->properties().configureInitFrom(Stage::PARENT, { "eef", "group" });
wrapper->properties().configureInitFrom(Stage::INTERFACE, { "target_pose" });
place->insert(std::move(wrapper));
}
/******************************************************
---- * Open Hand *
*****************************************************/
{
auto stage = std::make_unique<stages::MoveTo>("open hand", sampling_planner);
stage->properties().property("group").configureInitFrom(Stage::PARENT, hand_group_name_);
stage->setGoal(hand_open_pose_);
place->insert(std::move(stage));
}
/******************************************************
---- * Forbid collision (hand, object) *
*****************************************************/
{
auto stage = std::make_unique<stages::ModifyPlanningScene>("forbid collision (hand,object)");
stage->allowCollisions(
object_name_,
t.getRobotModel()->getJointModelGroup(hand_group_name_)->getLinkModelNamesWithCollisionGeometry(), false);
place->insert(std::move(stage));
}
/******************************************************
---- * Detach Object *
*****************************************************/
{
auto stage = std::make_unique<stages::ModifyPlanningScene>("detach object");
stage->detachObject(object_name_, hand_frame_);
place->insert(std::move(stage));
}
/******************************************************
---- * Retreat Motion *
*****************************************************/
{
auto stage = std::make_unique<stages::MoveRelative>("retreat after place", cartesian_planner);
stage->properties().configureInitFrom(Stage::PARENT, { "group" });
stage->setMinMaxDistance(.12, .25);
stage->setIKFrame(hand_frame_);
stage->properties().set("marker_ns", "retreat");
geometry_msgs::Vector3Stamped vec;
vec.header.frame_id = hand_frame_;
vec.vector.z = -1.0;
stage->setDirection(vec);
place->insert(std::move(stage));
}
// Add place container to task
t.add(std::move(place));
}
/******************************************************
* *
* Move to Home *
* *
*****************************************************/
{
auto stage = std::make_unique<stages::MoveTo>("move home", sampling_planner);
stage->properties().configureInitFrom(Stage::PARENT, { "group" });
stage->setGoal(arm_home_pose_);
stage->restrictDirection(stages::MoveTo::FORWARD);
t.add(std::move(stage));
}
}
bool DeepPickPlaceTask::plan()
{
ROS_INFO_NAMED(LOGNAME, "Start searching for task solutions");
ros::NodeHandle pnh("~");
int max_solutions = pnh.param<int>("max_solutions", 10);
try
{
task_->plan(max_solutions);
}
catch (InitStageException& e)
{
ROS_ERROR_STREAM_NAMED(LOGNAME, "Initialization failed: " << e);
return false;
}
if (task_->numSolutions() == 0)
{
ROS_ERROR_NAMED(LOGNAME, "Planning failed");
return false;
}
return true;
}
bool DeepPickPlaceTask::execute()
{
ROS_INFO_NAMED(LOGNAME, "Executing solution trajectory");
moveit_task_constructor_msgs::ExecuteTaskSolutionGoal execute_goal;
task_->solutions().front()->fillMessage(execute_goal.solution);
execute_.sendGoal(execute_goal);
execute_.waitForResult();
moveit_msgs::MoveItErrorCodes execute_result = execute_.getResult()->error_code;
if (execute_result.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
{
ROS_ERROR_STREAM_NAMED(LOGNAME, "Task execution failed and returned: " << execute_.getState().toString());
return false;
}
return true;
}
} // namespace deep_grasp_task