1 #include "nav_msgs/srv/get_plan.hpp"
2 #include "rclcpp/rclcpp.hpp"
3 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
7 plan_path(const std::shared_ptr<nav_msgs::srv::GetPlan::Request> req,
8 std::shared_ptr<nav_msgs::srv::GetPlan::Response> res)
11 double sx = req->start.pose.position.x;
12 double sy = req->start.pose.position.y;
14 tf2::fromMsg(req->start.pose.orientation, qh);
16 double sh = qh.getAngle();
17 p.set_start(sx, sy, sh);
18 double gx = req->goal.pose.position.x;
19 double gy = req->goal.pose.position.y;
20 tf2::fromMsg(req->goal.pose.orientation, qh);
22 double gb = qh.getAngle();
23 double ge = gb + std::abs(req->tolerance);
24 p.set_goal(gx, gy, gb, ge);
25 // TODO make 1000 settable over ROS (max iters before reset)
26 p.set_imax_reset(1000);
30 double best_cost = 0.0;
31 auto best_path = p.get_path();
32 // TODO make 1000 settable over ROS (max iters total)
37 if (best_cost == 0.0 || p.get_path_cost() < best_cost) {
38 // TODO make 0.75 settable over ROS
39 if (p.get_path_cost() < 0.75 * best_cost) {
42 best_path = p.get_path();
43 best_cost = p.get_path_cost();
47 // TODO make 5 settable over ROS
53 RCLCPP_INFO(rclcpp::get_logger("rclcpp"),
54 "path planned in %lf s, path cost %lf m",
56 res->plan.poses.clear();
57 for (auto pp: best_path) {
58 geometry_msgs::msg::PoseStamped p;
59 p.pose.position.x = pp.x();
60 p.pose.position.y = pp.y();
61 p.pose.position.z = 0.0;
63 qh.setRPY(0, 0, pp.h());
64 p.pose.orientation = tf2::toMsg(qh);
65 res->plan.poses.push_back(p);
70 main(int argc, char **argv)
72 rclcpp::init(argc, argv);
73 auto node = rclcpp::Node::make_shared("rrts");
74 auto srv = node->create_service<nav_msgs::srv::GetPlan>("rrts/planner",
76 RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to plan paths.");