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(), SLOT(updateAutoDeactivate()),
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_ = context_->getRosNodeAbstraction().lock()->get_raw_node();
48 node_->create_client<nav2_msgs::srv::GetCost>(
"local_costmap/get_cost_local_costmap");
50 node_->create_client<nav2_msgs::srv::GetCost>(
"global_costmap/get_cost_global_costmap");
53 void CostmapCostTool::activate() {}
54 void CostmapCostTool::deactivate() {}
56 int CostmapCostTool::processMouseEvent(rviz_common::ViewportMouseEvent & event)
60 Ogre::Vector3 position;
61 bool success = context_->getViewPicker()->get3DPoint(event.panel, event.x, event.y, position);
62 setCursor(success ? hit_cursor_ : std_cursor_);
66 s <<
"<b>Left-Click:</b> Select this point.";
68 s <<
" [" << position.x <<
"," << position.y <<
"," << position.z <<
"]";
69 setStatus(s.str().c_str());
72 auto point = rviz_common::pointOgreToMsg(position);
74 callCostService(point.x, point.y);
76 if (auto_deactivate_property_->getBool()) {
81 setStatus(
"Move over an object to select the target point.");
86 void CostmapCostTool::callCostService(
float x,
float y)
89 auto request = std::make_shared<nav2_msgs::srv::GetCost::Request>();
94 if (local_cost_client_->wait_for_service(std::chrono::seconds(1))) {
95 local_cost_client_->async_send_request(request,
96 std::bind(&CostmapCostTool::handleLocalCostResponse,
this, std::placeholders::_1));
100 if (global_cost_client_->wait_for_service(std::chrono::seconds(1))) {
101 global_cost_client_->async_send_request(request,
102 std::bind(&CostmapCostTool::handleGlobalCostResponse,
this, std::placeholders::_1));
106 void CostmapCostTool::handleLocalCostResponse(
107 rclcpp::Client<nav2_msgs::srv::GetCost>::SharedFuture future)
109 auto response = future.get();
110 if (response->cost != -1) {
111 RCLCPP_INFO(node_->get_logger(),
"Local costmap cost: %.1f", response->cost);
113 RCLCPP_ERROR(node_->get_logger(),
"Failed to get local costmap cost");
117 void CostmapCostTool::handleGlobalCostResponse(
118 rclcpp::Client<nav2_msgs::srv::GetCost>::SharedFuture future)
120 auto response = future.get();
121 if (response->cost != -1) {
122 RCLCPP_INFO(node_->get_logger(),
"Global costmap cost: %.1f", response->cost);
124 RCLCPP_ERROR(node_->get_logger(),
"Failed to get global costmap cost");
129 #include <pluginlib/class_list_macros.hpp>