15 #include "nav2_rviz_plugins/costmap_cost_tool.hpp"
16 #include <rviz_common/viewport_mouse_event.hpp>
17 #include "rviz_common/display_context.hpp"
18 #include "rviz_common/load_resource.hpp"
19 #include "rviz_common/interaction/view_picker_iface.hpp"
20 #include "rviz_common/msg_conversions.hpp"
21 #include "rviz_common/properties/bool_property.hpp"
23 namespace nav2_rviz_plugins
25 CostmapCostTool::CostmapCostTool()
30 auto_deactivate_property_ =
new rviz_common::properties::BoolProperty(
32 "Switch away from this tool after one click.",
33 getPropertyContainer(),
nullptr,
this);
36 CostmapCostTool::~CostmapCostTool() {}
38 void CostmapCostTool::onInitialize()
40 hit_cursor_ = cursor_;
41 std_cursor_ = rviz_common::getDefaultCursor();
43 setName(
"Costmap Cost");
44 setIcon(rviz_common::loadPixmap(
"package://rviz_default_plugins/icons/classes/PointStamped.png"));
46 node_ptr_ = context_->getRosNodeAbstraction().lock();
47 if (node_ptr_ ==
nullptr) {
50 rclcpp::get_logger(
"costmap_cost_tool"),
51 "Underlying ROS node no longer exists, initialization failed");
54 rclcpp::Node::SharedPtr node = node_ptr_->get_raw_node();
56 std::make_shared<nav2_util::ServiceClient<nav2_msgs::srv::GetCosts>>(
57 "local_costmap/get_cost_local_costmap",
61 std::make_shared<nav2_util::ServiceClient<nav2_msgs::srv::GetCosts>>(
62 "global_costmap/get_cost_global_costmap",
67 void CostmapCostTool::activate() {}
68 void CostmapCostTool::deactivate() {}
70 int CostmapCostTool::processMouseEvent(rviz_common::ViewportMouseEvent & event)
74 Ogre::Vector3 position;
75 bool success = context_->getViewPicker()->get3DPoint(event.panel, event.x, event.y, position);
76 setCursor(success ? hit_cursor_ : std_cursor_);
80 s <<
"<b>Left-Click:</b> Select this point.";
82 s <<
" [" << position.x <<
"," << position.y <<
"," << position.z <<
"]";
83 setStatus(s.str().c_str());
86 auto point = rviz_common::pointOgreToMsg(position);
88 callCostService(point.x, point.y);
90 if (auto_deactivate_property_->getBool()) {
95 setStatus(
"Move over an object to select the target point.");
100 void CostmapCostTool::callCostService(
float x,
float y)
102 rclcpp::Node::SharedPtr node = node_ptr_->get_raw_node();
104 auto request = std::make_shared<nav2_msgs::srv::GetCosts::Request>();
105 geometry_msgs::msg::PoseStamped pose;
106 pose.header.frame_id = context_->getFixedFrame().toStdString();
107 pose.header.stamp = node->now();
108 pose.pose.position.x = x;
109 pose.pose.position.y = y;
110 request->poses.push_back(pose);
111 request->use_footprint =
false;
114 if (local_cost_client_->wait_for_service(std::chrono::seconds(1))) {
115 local_cost_client_->async_call(
117 std::bind(&CostmapCostTool::handleLocalCostResponse,
this, std::placeholders::_1));
121 if (global_cost_client_->wait_for_service(std::chrono::seconds(1))) {
122 global_cost_client_->async_call(
124 std::bind(&CostmapCostTool::handleGlobalCostResponse,
this, std::placeholders::_1));
128 void CostmapCostTool::handleLocalCostResponse(
129 rclcpp::Client<nav2_msgs::srv::GetCosts>::SharedFuture future)
131 rclcpp::Node::SharedPtr node = node_ptr_->get_raw_node();
132 auto response = future.get();
133 RCLCPP_INFO(node->get_logger(),
"Local costmap cost: %.1f", response->costs[0]);
136 void CostmapCostTool::handleGlobalCostResponse(
137 rclcpp::Client<nav2_msgs::srv::GetCosts>::SharedFuture future)
139 rclcpp::Node::SharedPtr node = node_ptr_->get_raw_node();
140 auto response = future.get();
141 RCLCPP_INFO(node->get_logger(),
"Global costmap cost: %.1f", response->costs[0]);
145 #include <pluginlib/class_list_macros.hpp>