1 #include "rclcpp/rclcpp.hpp"
2 #include "nav_msgs/srv/get_plan.hpp"
6 plan_path(const std::shared_ptr<nav_msgs::srv::GetPlan::Request> req,
7 std::shared_ptr<nav_msgs::srv::GetPlan::Response> res)
10 double sx = req->start.pose.position.x;
11 double sy = req->start.pose.position.y;
12 double sh = 0.0; // FIXME
13 p.set_start(sx, sy, sh);
14 double gx = req->goal.pose.position.x;
15 double gy = req->goal.pose.position.y;
16 double gb = 0.0; // FIXME
17 double ge = 0.0; // FIXME
18 p.set_goal(gx, gy, gb, ge);
19 // TODO make 1000 settable over ROS (max iters before reset)
20 p.set_imax_reset(1000);
24 double best_cost = 0.0;
25 auto best_path = p.get_path();
26 // TODO make 1000 settable over ROS (max iters total)
31 if (best_cost == 0.0 || p.get_path_cost() < best_cost) {
32 // TODO make 0.75 settable over ROS
33 if (p.get_path_cost() < 0.75 * best_cost) {
36 best_path = p.get_path();
37 best_cost = p.get_path_cost();
41 // TODO make 5 settable over ROS
47 RCLCPP_INFO(rclcpp::get_logger("rclcpp"),
48 "path planned in %lf s, path cost %lf m",
50 res->plan.poses.clear();
51 for (auto pp: best_path) {
52 geometry_msgs::msg::PoseStamped p;
53 p.pose.position.x = pp.x();
54 p.pose.position.y = pp.y();
55 p.pose.position.z = 0.0;
56 // TODO use pp.h() to compute orientation
57 p.pose.orientation.x = 0.0;
58 p.pose.orientation.y = 0.0;
59 p.pose.orientation.z = 0.0;
60 p.pose.orientation.w = 0.0;
61 res->plan.poses.push_back(p);
66 main(int argc, char **argv)
68 rclcpp::init(argc, argv);
69 auto node = rclcpp::Node::make_shared("rrts");
70 auto srv = node->create_service<nav_msgs::srv::GetPlan>("rrts/planner",
72 RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to plan paths.");