Robowflex  v0.1
Making MoveIt Easy
ur5_custom_planning.cpp
Go to the documentation of this file.
1 /* Author: Carlos Quintero Pena */
2 
10 #include <robowflex_library/util.h>
13 
14 /* \file ur5_custom_planning.cpp
15  * Simple demonstration of how to use the TrajOptPlanner with the UR5 with custom terms in the optimization.
16  * This customized trajopt planner has end-effector pose constraints at specific timesteps of the trajectory.
17  */
18 
19 using namespace robowflex;
20 static const std::string GROUP = "manipulator";
21 
23 {
24 public:
25  struct CartCnt
26  {
28  int timestep;
30  double pos_coeffs;
31  double rot_coeffs;
32  };
33 
34  CustomTrajOptPlanner(const RobotPtr &robot, const std::string &group_name)
35  : TrajOptPlanner(robot, group_name, "custom_trajopt")
36  {
37  }
38 
39  PlannerResult plan(const SceneConstPtr &scene, const robot_state::RobotStatePtr &start_state)
40  {
41  // This is required by any TrajOptPlanner::plan() method.
42  ref_state_ = std::make_shared<robot_state::RobotState>(*start_state);
43 
44  // Transform robowflex scene to tesseract environment.
46 
47  // Fill in the problem construction info and initialization.
48  auto pci = std::make_shared<trajopt::ProblemConstructionInfo>(env_);
49  problemConstructionInfo(pci);
50 
51  // Add velocity cost.
52  addVelocityCost(pci);
53 
54  // Add start state.
55  addStartState(start_state, pci);
56 
57  // Add cartesian terms in constraints_.
58  addCartTerms(pci);
59 
60  return solve(scene, pci);
61  }
62 
63  std::vector<CartCnt> constraints_{};
64 
65 private:
67  {
68  for (const auto &term : constraints_)
69  {
70  Eigen::Quaterniond rotation(term.pose.linear());
71  auto pose_constraint = std::make_shared<trajopt::CartPoseTermInfo>();
72  pose_constraint->term_type = trajopt::TT_CNT;
73  pose_constraint->link = term.link;
74  pose_constraint->timestep = term.timestep;
75  pose_constraint->xyz = term.pose.translation();
76  pose_constraint->wxyz = Eigen::Vector4d(rotation.w(), rotation.x(), rotation.y(), rotation.z());
77  pose_constraint->pos_coeffs = Eigen::Vector3d::Constant(term.pos_coeffs);
78  pose_constraint->rot_coeffs = Eigen::Vector3d::Constant(term.rot_coeffs);
79  pose_constraint->name = "pose_cnt_link_" + term.link + std::to_string(term.timestep);
80 
81  pci->cnt_infos.push_back(pose_constraint);
82  }
83  }
84 };
85 
86 int main(int argc, char **argv)
87 {
88  // Startup ROS
89  ROS ros(argc, argv);
90 
91  // Create the default UR5 robot.
92  auto ur5 = std::make_shared<UR5Robot>();
93  ur5->initialize();
94 
95  // Set start state and get the ee pose.
96  ur5->setGroupState(GROUP, {0.0677, -0.8235, 0.9860, -0.1624, 0.0678, 0.0});
97  const auto &ee = ur5->getModel()->getEndEffectors()[0]->getLinkModelNames()[0];
98  RobotPose pose = ur5->getLinkTF(ee);
99 
100  // Create an RViz visualization helper.
101  IO::RVIZHelper rviz(ur5);
102  RBX_INFO("RViz Initialized! Press enter to continue (after your RViz is setup)...");
103  std::cin.get();
104 
105  // Create and visualize scene.
106  auto scene = std::make_shared<Scene>(ur5);
107  scene->getCurrentState() = *ur5->getScratchState();
108  rviz.updateScene(scene);
109 
110  // Create a custom TrajOpt planner
111  auto planner = std::make_shared<CustomTrajOptPlanner>(ur5, GROUP);
112  planner->initialize(GROUP);
113  planner->options.num_waypoints = 17; // Set number of waypoints in trajectory.
114 
115  // Define desired motion of the end-effector.
116  EigenSTL::vector_Vector3d directions = {
117  {0.0, -0.3, 0.0}, {0.0, 0.0, -0.2}, {0.0, 0.3, 0.0}, {0.0, 0.0, 0.2}};
118 
119  // Define a Cartesian constraint for each location.
120  for (size_t i = 0; i < directions.size(); ++i)
121  {
123  pose.translate(directions[i]);
124  term.pose = pose;
125  term.timestep = (i + 1) * 4;
126  term.link = ee;
127  term.pos_coeffs = 1.0;
128  term.rot_coeffs = 1.0;
129 
130  planner->constraints_.push_back(term);
131  }
132 
133  // Do planning with cartesian constraints.
134  auto response = planner->plan(scene, ur5->getScratchState());
135  if (!response.first)
136  {
137  RBX_INFO("Optimization did not converge");
138  return 0;
139  }
140 
141  // Publish the trajectory to a topic to display in RViz
142  Trajectory trajectory(planner->getTrajectory());
144 
145  // Visualize the final pose.
146  scene->getCurrentState() = *ur5->getScratchState();
147  rviz.updateScene(scene);
148 
149  RBX_INFO("Press enter to exit.");
150  std::cin.get();
151 
152  return 0;
153 }
PlannerResult plan(const SceneConstPtr &scene, const robot_state::RobotStatePtr &start_state)
Plan a motion using custom terms specified by the user.
void addCartTerms(std::shared_ptr< trajopt::ProblemConstructionInfo > pci) const
CustomTrajOptPlanner(const RobotPtr &robot, const std::string &group_name)
RVIZ visualization helper. See Live Visualization with RViz for more information.
Definition: visualization.h:38
void updateScene(const SceneConstPtr &scene)
Updates the scene being visualized.
void updateTrajectory(const planning_interface::MotionPlanResponse &response)
Updates the trajectory being visualized.
RAII-pattern for starting up ROS.
Definition: util.h:52
A shared pointer wrapper for robowflex::Robot.
A const shared pointer wrapper for robowflex::Scene.
Robowflex Tesseract TrajOpt Planner.
The Trajectory class is a wrapper around MoveIt!'s robot_trajectory::RobotTrajectory,...
Definition: trajectory.h:43
#define RBX_INFO(fmt,...)
Output a info logging message.
Definition: log.h:118
Functions for loading and animating robots in Blender.
bool sceneToTesseractEnv(const robowflex::SceneConstPtr &scene, tesseract::tesseract_ros::KDLEnvPtr env)
Add scene collision objects to a (previously initialized) KDL environment.
Definition: conversions.cpp:17
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Definition: adapter.h:24
Functions for loading and animating scenes in Blender.
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")
T to_string(T... args)
int main(int argc, char **argv)
static const std::string GROUP