fallbacks_move_to.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
5 
11 
12 constexpr double TAU = 2 * M_PI;
13 
14 using namespace moveit::task_constructor;
15 
22 int main(int argc, char** argv) {
23  ros::init(argc, argv, "mtc_tutorial");
24 
26  spinner.start();
27 
28  // setup Task
29  Task t;
30  t.setName("fallback strategies in MoveTo");
31  t.loadRobotModel();
32  const moveit::core::RobotModelConstPtr robot{ t.getRobotModel() };
33 
34  assert(robot->getName() == "panda");
35 
36  // setup solvers
37  auto cartesian = std::make_shared<solvers::CartesianPath>();
38 
39  auto ptp = std::make_shared<solvers::PipelinePlanner>("pilz_industrial_motion_planner");
40  ptp->setPlannerId("PTP");
41 
42  auto rrtconnect = std::make_shared<solvers::PipelinePlanner>("ompl");
43  rrtconnect->setPlannerId("RRTConnect");
44 
45  // target end state for all Task plans
46  std::map<std::string, double> target_state;
47  robot->getJointModelGroup("panda_arm")->getVariableDefaultPositions("ready", target_state);
48  target_state["panda_joint1"] = +TAU / 8;
49 
50  // define initial scenes
51  auto initial_scene{ std::make_shared<planning_scene::PlanningScene>(robot) };
52  initial_scene->getCurrentStateNonConst().setToDefaultValues(robot->getJointModelGroup("panda_arm"), "ready");
53 
54  auto initial_alternatives = std::make_unique<Alternatives>("initial states");
55 
56  {
57  // can reach target with Cartesian motion
58  auto fixed{ std::make_unique<stages::FixedState>("close to target state in workspace") };
59  auto scene{ initial_scene->diff() };
60  scene->getCurrentStateNonConst().setVariablePositions({ { "panda_joint1", -TAU / 8 } });
61  fixed->setState(scene);
62  initial_alternatives->add(std::move(fixed));
63  }
64  {
65  // Cartesian motion to target is impossible, but PTP is collision-free
66  auto fixed{ std::make_unique<stages::FixedState>("directly reachable without collision") };
67  auto scene{ initial_scene->diff() };
68  scene->getCurrentStateNonConst().setVariablePositions({
69  { "panda_joint1", +TAU / 8 },
70  { "panda_joint4", 0 },
71  });
72  fixed->setState(scene);
73  initial_alternatives->add(std::move(fixed));
74  }
75  {
76  // Cartesian and PTP motion to target would be in collision
77  auto fixed{ std::make_unique<stages::FixedState>("getting to target requires collision avoidance") };
78  auto scene{ initial_scene->diff() };
79  scene->getCurrentStateNonConst().setVariablePositions({ { "panda_joint1", -TAU / 8 } });
80  scene->processCollisionObjectMsg([]() {
81  moveit_msgs::CollisionObject co;
82  co.id = "box";
83  co.header.frame_id = "panda_link0";
84  co.operation = co.ADD;
85  auto& pose{ co.pose };
86  pose = []() {
87  geometry_msgs::Pose p;
88  p.position.x = 0.3;
89  p.position.y = 0.0;
90  p.position.z = 0.64 / 2;
91  p.orientation.w = 1.0;
92  return p;
93  }();
94  co.primitives.push_back([]() {
95  shape_msgs::SolidPrimitive sp;
96  sp.type = sp.BOX;
97  sp.dimensions = { 0.2, 0.05, 0.64 };
98  return sp;
99  }());
100  return co;
101  }());
102  fixed->setState(scene);
103  initial_alternatives->add(std::move(fixed));
104  }
105 
106  t.add(std::move(initial_alternatives));
107 
108  // fallbacks to reach target_state
109  auto fallbacks = std::make_unique<Fallbacks>("move to other side");
110 
111  auto add_to_fallbacks{ [&](auto& solver, auto& name) {
112  auto move_to = std::make_unique<stages::MoveTo>(name, solver);
113  move_to->setGroup("panda_arm");
114  move_to->setGoal(target_state);
115  fallbacks->add(std::move(move_to));
116  } };
117  add_to_fallbacks(cartesian, "Cartesian path");
118  add_to_fallbacks(ptp, "PTP path");
119  add_to_fallbacks(rrtconnect, "RRT path");
120 
121  t.add(std::move(fallbacks));
122 
123  try {
124  t.plan();
125  } catch (const InitStageException& e) {
126  std::cout << e << '\n';
127  }
128 
130 
131  return 0;
132 }
robot_model.h
pipeline_planner.h
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
alternatives.name
name
Definition: alternatives.py:15
ros.h
moveit::task_constructor::InitStageException
fallbacks
Definition: fallbacks.py:1
ros::AsyncSpinner
stages.h
TAU
constexpr double TAU
Definition: fallbacks_move_to.cpp:12
constrained.co
co
Definition: constrained.py:23
compute_ik.pose
pose
Definition: compute_ik.py:36
moveit::task_constructor::Task
container.h
spinner
void spinner()
properties.p
p
Definition: properties.py:14
moveit::task_constructor
task.h
main
int main(int argc, char **argv)
Definition: fallbacks_move_to.cpp:22
planning_scene.h
cartesian_path.h
cartesian.move
move
Definition: cartesian.py:34
ros::waitForShutdown
ROSCPP_DECL void waitForShutdown()
M_PI
#define M_PI
cartesian
Definition: cartesian.py:1
t
dictionary t


demo
Author(s): Robert Haschke , Simon Goldstein , Henning Kayser
autogenerated on Sat May 3 2025 02:40:30