|
| 1 | +#include <ros/ros.h> |
| 2 | + |
| 3 | +#include <moveit/planning_scene/planning_scene.h> |
| 4 | + |
| 5 | +#include <moveit/task_constructor/task.h> |
| 6 | +#include <moveit/task_constructor/container.h> |
| 7 | + |
| 8 | +#include <moveit/task_constructor/solvers/pipeline_planner.h> |
| 9 | + |
| 10 | +#include <moveit/task_constructor/stages/connect.h> |
| 11 | +#include <moveit/task_constructor/stages/fixed_state.h> |
| 12 | + |
| 13 | +#include <moveit/task_constructor/cost_terms.h> |
| 14 | + |
| 15 | +using namespace moveit::task_constructor; |
| 16 | + |
| 17 | +/* FixedState - Connect - FixedState */ |
| 18 | +int main(int argc, char** argv) { |
| 19 | + ros::init(argc, argv, "mtc_tutorial"); |
| 20 | + |
| 21 | + Task t; |
| 22 | + t.stages()->setName("alternative path costs"); |
| 23 | + t.loadRobotModel(); |
| 24 | + |
| 25 | + assert(t.getRobotModel()->getName() == "panda"); |
| 26 | + |
| 27 | + auto scene{ std::make_shared<planning_scene::PlanningScene>(t.getRobotModel()) }; |
| 28 | + auto& robot_state{ scene->getCurrentStateNonConst() }; |
| 29 | + robot_state.setToDefaultValues(); |
| 30 | + robot_state.setToDefaultValues(robot_state.getJointModelGroup("panda_arm"), "extended"); |
| 31 | + |
| 32 | + auto initial{ std::make_unique<stages::FixedState>("start") }; |
| 33 | + initial->setState(scene); |
| 34 | + t.add(std::move(initial)); |
| 35 | + |
| 36 | + auto pipeline{ std::make_shared<solvers::PipelinePlanner>() }; |
| 37 | + |
| 38 | + auto alternatives{ std::make_unique<Alternatives>("connect") }; |
| 39 | + { |
| 40 | + auto connect{ std::make_unique<stages::Connect>( |
| 41 | + "path length", stages::Connect::GroupPlannerVector{ { "panda_arm", pipeline } }) }; |
| 42 | + connect->setCostTerm(std::make_unique<cost::PathLength>()); // This is the default for Connect, specified for |
| 43 | + // demonstration purposes |
| 44 | + alternatives->add(std::move(connect)); |
| 45 | + } |
| 46 | + { |
| 47 | + auto connect{ std::make_unique<stages::Connect>( |
| 48 | + "trajectory duration", stages::Connect::GroupPlannerVector{ { "panda_arm", pipeline } }) }; |
| 49 | + connect->setCostTerm(std::make_unique<cost::TrajectoryDuration>()); |
| 50 | + alternatives->add(std::move(connect)); |
| 51 | + } |
| 52 | + { |
| 53 | + auto connect{ std::make_unique<stages::Connect>( |
| 54 | + "eef motion", stages::Connect::GroupPlannerVector{ { "panda_arm", pipeline } }) }; |
| 55 | + connect->setCostTerm(std::make_unique<cost::LinkMotion>("panda_hand")); |
| 56 | + alternatives->add(std::move(connect)); |
| 57 | + } |
| 58 | + { |
| 59 | + auto connect{ std::make_unique<stages::Connect>( |
| 60 | + "elbow motion", stages::Connect::GroupPlannerVector{ { "panda_arm", pipeline } }) }; |
| 61 | + connect->setCostTerm(std::make_unique<cost::LinkMotion>("panda_link4")); |
| 62 | + alternatives->add(std::move(connect)); |
| 63 | + } |
| 64 | + |
| 65 | + t.add(std::move(alternatives)); |
| 66 | + |
| 67 | + auto goal_scene{ scene->diff() }; |
| 68 | + goal_scene->getCurrentStateNonConst().setToDefaultValues(robot_state.getJointModelGroup("panda_arm"), "ready"); |
| 69 | + auto goal = std::make_unique<stages::FixedState>("goal"); |
| 70 | + goal->setState(goal_scene); |
| 71 | + t.add(std::move(goal)); |
| 72 | + |
| 73 | + try { |
| 74 | + t.plan(0); |
| 75 | + } catch (const InitStageException& e) { |
| 76 | + std::cout << e << std::endl; |
| 77 | + } |
| 78 | + |
| 79 | + ros::spin(); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
0 commit comments