Skip to content

Commit 641c0b8

Browse files
tylerjwstephanie-engabake48
authored
✨ Visualizing in RViz Tutorial (#365)
Co-authored-by: Stephanie Eng <[email protected]> Co-authored-by: Anthony Baker <[email protected]>
1 parent 3709269 commit 641c0b8

14 files changed

+383
-1
lines changed

doc/tutorials/tutorials.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ In these tutorials, the Franka Emika Panda robot is used as a quick-start demo.
1414
getting_started/getting_started
1515
quickstart_in_rviz/quickstart_in_rviz_tutorial
1616
your_first_project/your_first_project
17+
visualizing_in_rviz/visualizing_in_rviz
1718
pick_and_place_with_moveit_task_constructor/pick_and_place_with_moveit_task_constructor
117 KB
Loading
40.5 KB
Loading
115 KB
Loading
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <moveit/move_group_interface/move_group_interface.h>
2+
#include <moveit_visual_tools/moveit_visual_tools.h>
3+
4+
#include <memory>
5+
#include <rclcpp/rclcpp.hpp>
6+
#include <thread>
7+
8+
int main(int argc, char* argv[])
9+
{
10+
// Initialize ROS and create the Node
11+
rclcpp::init(argc, argv);
12+
auto const node = std::make_shared<rclcpp::Node>(
13+
"hello_moveit", rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true));
14+
15+
// Create a ROS logger
16+
auto const logger = rclcpp::get_logger("hello_moveit");
17+
18+
// We spin up a SingleThreadedExecutor for the current state monitor to get
19+
// information about the robot's state.
20+
rclcpp::executors::SingleThreadedExecutor executor;
21+
executor.add_node(node);
22+
auto spinner = std::thread([&executor]() { executor.spin(); });
23+
24+
// Create the MoveIt MoveGroup Interface
25+
using moveit::planning_interface::MoveGroupInterface;
26+
auto move_group_interface = MoveGroupInterface(node, "panda_arm");
27+
28+
// Construct and initialize MoveItVisualTools
29+
auto moveit_visual_tools =
30+
moveit_visual_tools::MoveItVisualTools{ node, "panda_link0", rviz_visual_tools::RVIZ_MARKER_TOPIC,
31+
move_group_interface.getRobotModel() };
32+
moveit_visual_tools.deleteAllMarkers();
33+
moveit_visual_tools.loadRemoteControl();
34+
35+
// Create a closure for updating the text in rviz
36+
auto const draw_title = [&moveit_visual_tools](auto text) {
37+
auto const text_pose = [] {
38+
auto msg = Eigen::Isometry3d::Identity();
39+
msg.translation().z() = 1.0;
40+
return msg;
41+
}();
42+
moveit_visual_tools.publishText(text_pose, text, rviz_visual_tools::WHITE, rviz_visual_tools::XLARGE);
43+
};
44+
auto const prompt = [&moveit_visual_tools](auto text) { moveit_visual_tools.prompt(text); };
45+
auto const draw_trajectory_tool_path =
46+
[&moveit_visual_tools, jmg = move_group_interface.getRobotModel()->getJointModelGroup("panda_arm")](
47+
auto const trajectory) { moveit_visual_tools.publishTrajectoryLine(trajectory, jmg); };
48+
49+
// Set a target Pose
50+
auto const target_pose = [] {
51+
geometry_msgs::msg::Pose msg;
52+
msg.orientation.w = 1.0;
53+
msg.position.x = 0.28;
54+
msg.position.y = -0.2;
55+
msg.position.z = 0.5;
56+
return msg;
57+
}();
58+
move_group_interface.setPoseTarget(target_pose);
59+
60+
// Create a plan to that target pose
61+
prompt("Press 'next' in the RvizVisualToolsGui window to plan");
62+
draw_title("Planning");
63+
moveit_visual_tools.trigger();
64+
auto const [success, plan] = [&move_group_interface] {
65+
moveit::planning_interface::MoveGroupInterface::Plan msg;
66+
auto const ok = static_cast<bool>(move_group_interface.plan(msg));
67+
return std::make_pair(ok, msg);
68+
}();
69+
70+
// Execute the plan
71+
if (success)
72+
{
73+
draw_trajectory_tool_path(plan.trajectory_);
74+
moveit_visual_tools.trigger();
75+
prompt("Press 'next' in the RvizVisualToolsGui window to execute");
76+
draw_title("Executing");
77+
moveit_visual_tools.trigger();
78+
move_group_interface.execute(plan);
79+
}
80+
else
81+
{
82+
draw_title("Planning Failed!");
83+
moveit_visual_tools.trigger();
84+
RCLCPP_ERROR(logger, "Planing failed!");
85+
}
86+
87+
// Shutdown ROS
88+
rclcpp::shutdown();
89+
spinner.join();
90+
return 0;
91+
}
47.2 KB
Loading
116 KB
Loading
118 KB
Loading
117 KB
Loading
122 KB
Loading

0 commit comments

Comments
 (0)