|
| 1 | +// Copyright 2024 TIER IV, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "freespace_planning_algorithms/abstract_algorithm.hpp" |
| 16 | +#include "freespace_planning_algorithms/astar_search.hpp" |
| 17 | + |
| 18 | +#include <rclcpp/rclcpp.hpp> |
| 19 | +#include <rclcpp/serialization.hpp> |
| 20 | + |
| 21 | +#include <geometry_msgs/msg/pose_array.hpp> |
| 22 | +#include <nav_msgs/msg/occupancy_grid.hpp> |
| 23 | + |
| 24 | +#include <pybind11/pybind11.h> |
| 25 | +#include <pybind11/stl.h> |
| 26 | + |
| 27 | +struct PlannerWaypointsVector |
| 28 | +{ |
| 29 | + std::vector<std::vector<double>> waypoints; |
| 30 | + double length; |
| 31 | +}; |
| 32 | + |
| 33 | +class AstarSearchPython : public freespace_planning_algorithms::AstarSearch |
| 34 | +{ |
| 35 | + using freespace_planning_algorithms::AstarSearch::AstarSearch; |
| 36 | + |
| 37 | +public: |
| 38 | + void setMapByte(const std::string & costmap_byte) |
| 39 | + { |
| 40 | + rclcpp::SerializedMessage serialized_msg; |
| 41 | + static constexpr size_t message_header_length = 8u; |
| 42 | + serialized_msg.reserve(message_header_length + costmap_byte.size()); |
| 43 | + serialized_msg.get_rcl_serialized_message().buffer_length = costmap_byte.size(); |
| 44 | + for (size_t i = 0; i < costmap_byte.size(); ++i) { |
| 45 | + serialized_msg.get_rcl_serialized_message().buffer[i] = costmap_byte[i]; |
| 46 | + } |
| 47 | + nav_msgs::msg::OccupancyGrid costmap; |
| 48 | + static rclcpp::Serialization<nav_msgs::msg::OccupancyGrid> serializer; |
| 49 | + serializer.deserialize_message(&serialized_msg, &costmap); |
| 50 | + |
| 51 | + freespace_planning_algorithms::AstarSearch::setMap(costmap); |
| 52 | + } |
| 53 | + |
| 54 | + bool makePlanByte(const std::string & start_pose_byte, const std::string & goal_pose_byte) |
| 55 | + { |
| 56 | + rclcpp::SerializedMessage serialized_start_msg; |
| 57 | + static constexpr size_t message_header_length = 8u; |
| 58 | + serialized_start_msg.reserve(message_header_length + start_pose_byte.size()); |
| 59 | + serialized_start_msg.get_rcl_serialized_message().buffer_length = start_pose_byte.size(); |
| 60 | + for (size_t i = 0; i < start_pose_byte.size(); ++i) { |
| 61 | + serialized_start_msg.get_rcl_serialized_message().buffer[i] = start_pose_byte[i]; |
| 62 | + } |
| 63 | + geometry_msgs::msg::Pose start_pose; |
| 64 | + static rclcpp::Serialization<geometry_msgs::msg::Pose> start_serializer; |
| 65 | + start_serializer.deserialize_message(&serialized_start_msg, &start_pose); |
| 66 | + |
| 67 | + rclcpp::SerializedMessage serialized_goal_msg; |
| 68 | + serialized_goal_msg.reserve(message_header_length + goal_pose_byte.size()); |
| 69 | + serialized_goal_msg.get_rcl_serialized_message().buffer_length = goal_pose_byte.size(); |
| 70 | + for (size_t i = 0; i < goal_pose_byte.size(); ++i) { |
| 71 | + serialized_goal_msg.get_rcl_serialized_message().buffer[i] = goal_pose_byte[i]; |
| 72 | + } |
| 73 | + geometry_msgs::msg::Pose goal_pose; |
| 74 | + static rclcpp::Serialization<geometry_msgs::msg::Pose> goal_serializer; |
| 75 | + goal_serializer.deserialize_message(&serialized_goal_msg, &goal_pose); |
| 76 | + |
| 77 | + return freespace_planning_algorithms::AstarSearch::makePlan(start_pose, goal_pose); |
| 78 | + } |
| 79 | + |
| 80 | + PlannerWaypointsVector getWaypointsAsVector() |
| 81 | + { |
| 82 | + freespace_planning_algorithms::PlannerWaypoints waypoints = |
| 83 | + freespace_planning_algorithms::AstarSearch::getWaypoints(); |
| 84 | + PlannerWaypointsVector waypoints_vector; |
| 85 | + for (const auto & waypoint : waypoints.waypoints) { |
| 86 | + // NOTE: it was difficult to return the deserialized pose_byte and serialize the pose_byte on |
| 87 | + // python-side. So this function returns [*position, *quaternion] as double array |
| 88 | + const auto & xyz = waypoint.pose.pose.position; |
| 89 | + const auto & quat = waypoint.pose.pose.orientation; |
| 90 | + waypoints_vector.waypoints.push_back( |
| 91 | + std::vector<double>({xyz.x, xyz.y, xyz.z, quat.x, quat.y, quat.z, quat.w})); |
| 92 | + } |
| 93 | + waypoints_vector.length = waypoints.compute_length(); |
| 94 | + return waypoints_vector; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +namespace py = pybind11; |
| 99 | + |
| 100 | +PYBIND11_MODULE(freespace_planning_algorithms_pybind, p) |
| 101 | +{ |
| 102 | + auto pyPlannerWaypointsVector = |
| 103 | + py::class_<PlannerWaypointsVector>(p, "PlannerWaypointsVector", py::dynamic_attr()) |
| 104 | + .def(py::init<>()) |
| 105 | + .def_readwrite("waypoints", &PlannerWaypointsVector::waypoints) |
| 106 | + .def_readwrite("length", &PlannerWaypointsVector::length); |
| 107 | + auto pyAstarParam = |
| 108 | + py::class_<freespace_planning_algorithms::AstarParam>(p, "AstarParam", py::dynamic_attr()) |
| 109 | + .def(py::init<>()) |
| 110 | + .def_readwrite( |
| 111 | + "only_behind_solutions", &freespace_planning_algorithms::AstarParam::only_behind_solutions) |
| 112 | + .def_readwrite("use_back", &freespace_planning_algorithms::AstarParam::use_back) |
| 113 | + .def_readwrite( |
| 114 | + "distance_heuristic_weight", |
| 115 | + &freespace_planning_algorithms::AstarParam::distance_heuristic_weight); |
| 116 | + auto pyPlannerCommonParam = |
| 117 | + py::class_<freespace_planning_algorithms::PlannerCommonParam>( |
| 118 | + p, "PlannerCommonParam", py::dynamic_attr()) |
| 119 | + .def(py::init<>()) |
| 120 | + .def_readwrite("time_limit", &freespace_planning_algorithms::PlannerCommonParam::time_limit) |
| 121 | + .def_readwrite( |
| 122 | + "minimum_turning_radius", |
| 123 | + &freespace_planning_algorithms::PlannerCommonParam::minimum_turning_radius) |
| 124 | + .def_readwrite( |
| 125 | + "maximum_turning_radius", |
| 126 | + &freespace_planning_algorithms::PlannerCommonParam::maximum_turning_radius) |
| 127 | + .def_readwrite( |
| 128 | + "turning_radius_size", |
| 129 | + &freespace_planning_algorithms::PlannerCommonParam::turning_radius_size) |
| 130 | + .def_readwrite("theta_size", &freespace_planning_algorithms::PlannerCommonParam::theta_size) |
| 131 | + .def_readwrite( |
| 132 | + "curve_weight", &freespace_planning_algorithms::PlannerCommonParam::curve_weight) |
| 133 | + .def_readwrite( |
| 134 | + "reverse_weight", &freespace_planning_algorithms::PlannerCommonParam::reverse_weight) |
| 135 | + .def_readwrite( |
| 136 | + "lateral_goal_range", |
| 137 | + &freespace_planning_algorithms::PlannerCommonParam::lateral_goal_range) |
| 138 | + .def_readwrite( |
| 139 | + "longitudinal_goal_range", |
| 140 | + &freespace_planning_algorithms::PlannerCommonParam::longitudinal_goal_range) |
| 141 | + .def_readwrite( |
| 142 | + "angle_goal_range", &freespace_planning_algorithms::PlannerCommonParam::angle_goal_range) |
| 143 | + .def_readwrite( |
| 144 | + "obstacle_threshold", |
| 145 | + &freespace_planning_algorithms::PlannerCommonParam::obstacle_threshold); |
| 146 | + auto pyVehicleShape = |
| 147 | + py::class_<freespace_planning_algorithms::VehicleShape>(p, "VehicleShape", py::dynamic_attr()) |
| 148 | + .def(py::init<>()) |
| 149 | + .def(py::init<double, double, double>()) |
| 150 | + .def_readwrite("length", &freespace_planning_algorithms::VehicleShape::length) |
| 151 | + .def_readwrite("width", &freespace_planning_algorithms::VehicleShape::width) |
| 152 | + .def_readwrite("base2back", &freespace_planning_algorithms::VehicleShape::base2back); |
| 153 | + |
| 154 | + py::class_<freespace_planning_algorithms::AbstractPlanningAlgorithm>( |
| 155 | + p, "AbstractPlanningAlgorithm"); |
| 156 | + py::class_< |
| 157 | + freespace_planning_algorithms::AstarSearch, |
| 158 | + freespace_planning_algorithms::AbstractPlanningAlgorithm>(p, "AstarSearchCpp"); |
| 159 | + py::class_<AstarSearchPython, freespace_planning_algorithms::AstarSearch>(p, "AstarSearch") |
| 160 | + .def(py::init< |
| 161 | + freespace_planning_algorithms::PlannerCommonParam &, |
| 162 | + freespace_planning_algorithms::VehicleShape &, |
| 163 | + freespace_planning_algorithms::AstarParam &>()) |
| 164 | + .def("setMap", &AstarSearchPython::setMapByte) |
| 165 | + .def("makePlan", &AstarSearchPython::makePlanByte) |
| 166 | + .def("getWaypoints", &AstarSearchPython::getWaypointsAsVector); |
| 167 | +} |
0 commit comments