Robowflex  v0.1
Making MoveIt Easy
cob4_visualization.cpp
Go to the documentation of this file.
1 /* Author: Juan D. Hernandez */
2 
13 #include <robowflex_library/util.h>
14 
15 using namespace robowflex;
16 
17 /* \file cob4_visualization.cpp
18  * A simple script that demonstrates how to use RViz with Robowflex with the
19  * COB4 robot. See https://kavrakilab.github.io/robowflex/rviz.html for how to
20  * use RViz visualization. Here, the scene, the pose goal, and motion plan
21  * displayed in RViz.
22  */
23 
24 static const std::string LEFT_ARM = "arm_left";
25 static const std::string RIGHT_ARM = "arm_right";
26 static const std::string LEFT_EE = "arm_left_7_link";
27 static const std::string RIGHT_EE = "arm_right_7_link";
28 
29 int main(int argc, char **argv)
30 {
31  // Startup ROS
32  ROS ros(argc, argv);
33 
34  // Create the default Care-O-Bot4 robot.
35  auto cob4 = std::make_shared<Cob4Robot>();
36  cob4->initialize();
37 
38  // Create an RViz visualization helper. Publishes all topics and parameter under `/robowflex` by default.
39  IO::RVIZHelper rviz(cob4);
40  IO::RobotBroadcaster bc(cob4);
41  bc.start();
42 
43  RBX_INFO("RViz Initialized! Press enter to continue (after your RViz is setup)...");
44  std::cin.get();
45 
46  // Load kinematics for the COB4 robot.
47  cob4->loadKinematics(RIGHT_ARM);
48  cob4->loadKinematics(LEFT_ARM);
49 
50  // Fold the arms.
51  cob4->setGroupState(RIGHT_ARM, {2.69, 1.70, -0.91, 1.50, -2.14, -2.35, 1.06});
52  cob4->setGroupState(LEFT_ARM, {-1.14, -1.50, 0.34, -1.50, 0.43, -1.56, -1.20});
53 
54  // Load a scene from a YAML file.
55  auto scene = std::make_shared<Scene>(cob4);
56  scene->fromYAMLFile("package://robowflex_library/yaml/test_cob4.yml");
57 
58  // Visualize the scene.
59  rviz.visualizeCurrentState();
60  rviz.updateScene(scene);
61 
62  // Create the default planner for the COB4.
63  auto planner = std::make_shared<OMPL::Cob4OMPLPipelinePlanner>(cob4);
64  planner->initialize();
65 
66  // Sets the Cob4's base pose.
67  cob4->pointHead({0.6, -0.26, 0.95});
68 
69  // Create a motion planning request with a joint position goal for the right arm.
70  MotionRequestBuilder request_right_arm(planner, RIGHT_ARM);
71  request_right_arm.setStartConfiguration(cob4->getScratchState());
72 
73  // Create a motion planning request with a pose goal for the right arm.
74  const auto &goal_pose_right = TF::createPoseQ(0.6, -0.26, 0.95, 1.0, 0.0, 0.0, 0.0);
75  request_right_arm.setGoalPose(RIGHT_EE, "base_link", goal_pose_right);
76 
77  // Visualizing the target poses for the right arm.
78  rviz.addGoalMarker("goal", request_right_arm);
79  rviz.addTransformMarker("pose_right", "base_link", goal_pose_right);
80  rviz.updateMarkers();
81 
82  RBX_INFO("Scene and Goal displayed for the right arm! Press enter to plan...");
83  std::cin.get();
84 
85  // Do motion planning!
86  request_right_arm.setConfig("RRTConnect");
87  planning_interface::MotionPlanResponse res = planner->plan(scene, request_right_arm.getRequest());
88  if (res.error_code_.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
89  return 1;
90 
91  // Publish the trajectory to a topic to display in RViz
92  rviz.updateTrajectory(res);
93 
94  // Visualize resulting state.
95  // Create a trajectory object for better manipulation.
96  auto right_arm_trajectory = std::make_shared<Trajectory>(res.trajectory_);
97  cob4->setGroupState(RIGHT_ARM, right_arm_trajectory->vectorize().back());
98 
99  rviz.visualizeCurrentState();
100 
101  RBX_INFO("Press enter to continue with the left arm.");
102 
103  // Sets the Cob4's base pose.
104  cob4->pointHead({0.4, 0.26, 0.79});
105 
106  // Create a motion planning request with a joint position goal for the right arm.
107  MotionRequestBuilder request_left_arm(planner, LEFT_ARM);
108  request_left_arm.setStartConfiguration(cob4->getScratchState());
109 
110  // Create a motion planning request with a pose goal for the left arm.
111  const auto &goal_pose_left = TF::createPoseQ(0.38, 0.26, 0.76, 0.0, 0.707, 0.0, 0.707);
112  request_left_arm.setGoalPose(LEFT_EE, "base_link", goal_pose_left);
113 
114  // Visualizing the target poses for the left arm.
115  rviz.addGoalMarker("goal_left", request_left_arm);
116  rviz.addTransformMarker("pose_left", "base_link", goal_pose_left);
117  rviz.updateMarkers();
118 
119  RBX_INFO("Scene and Goal displayed for the left arm! Press enter to plan...");
120  std::cin.get();
121 
122  // Do motion planning!
123  request_left_arm.setConfig("RRTConnect");
124  res = planner->plan(scene, request_left_arm.getRequest());
125  if (res.error_code_.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
126  return 1;
127 
128  // Publish the trajectory to a topic to display in RViz
129  rviz.updateTrajectory(res);
130 
131  // Visualize resulting state.
132  auto left_arm_trajectory = std::make_shared<Trajectory>(res.trajectory_);
133  cob4->setGroupState(LEFT_ARM, left_arm_trajectory->vectorize().back());
134 
135  rviz.visualizeCurrentState();
136 
137  RBX_INFO("Press enter to remove goal and scene.");
138  std::cin.get();
139 
140  // Clean up RViz.
141  rviz.removeMarker("goal");
142  rviz.updateMarkers();
143  rviz.removeScene();
144 
145  RBX_INFO("Press enter to exit.");
146  std::cin.get();
147 
148  return 0;
149 }
T back(T... args)
RVIZ visualization helper. See Live Visualization with RViz for more information.
Definition: visualization.h:38
void removeMarker(const std::string &name)
Removes a marker that was added through addMarker().
void updateMarkers()
Displays the managed list of markers. Keeps track of whether markers have already been displayed and ...
void removeScene()
Removes the scene being visualized.
void visualizeCurrentState()
Visualize the current state of the robot.
void updateScene(const SceneConstPtr &scene)
Updates the scene being visualized.
void addGoalMarker(const std::string &name, const MotionRequestBuilder &request)
Adds the current goal of the motion request builder as a set of markers in the marker array.
void updateTrajectory(const planning_interface::MotionPlanResponse &response)
Updates the trajectory being visualized.
void addTransformMarker(const std::string &name, const std::string &base_frame, const RobotPose &pose, double scale=1)
Add a transform marker to the managed list of markers. Displayed after an updateMarkers() call.
Helper class to broadcast transform information on TF and joint states.
Definition: broadcaster.h:24
void start()
Begin publishing TF and joint information.
Definition: broadcaster.cpp:37
A helper class to build motion planning requests for a robowflex::Planner.
Definition: builder.h:34
bool setConfig(const std::string &requested_config)
Set the planning configuration to use for the motion planning request. Attempts to match requested_co...
Definition: builder.cpp:114
void setStartConfiguration(const std::vector< double > &joints)
Set the start configuration from a vector joints. All joints are assumed to be specified and in the d...
Definition: builder.cpp:189
void setGoalPose(const std::string &ee_name, const std::string &base_name, const RobotPose &pose, double tolerance=0.001)
Set a goal pose for the end-effector ee_name. Generates a sphere with radius tolerance as well as ori...
Definition: builder.cpp:396
planning_interface::MotionPlanRequest & getRequest()
Get a reference to the currently built motion planning request.
Definition: builder.cpp:493
RAII-pattern for starting up ROS.
Definition: util.h:52
int main(int argc, char **argv)
static const std::string LEFT_EE
static const std::string LEFT_ARM
static const std::string RIGHT_EE
static const std::string RIGHT_ARM
#define RBX_INFO(fmt,...)
Output a info logging message.
Definition: log.h:118
RobotPose createPoseQ(double x, double y, double z, double W, double X, double Y, double Z)
Creates a robot pose from a linear component and a Quaternion.
Definition: tf.cpp:47
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
Functions for loading and animating scenes in Blender.
moveit_msgs::MoveItErrorCodes error_code_
robot_trajectory::RobotTrajectoryPtr trajectory_