|
| 1 | +#include "freespace_planning_algorithms/astar_search.hpp" |
| 2 | +#include "freespace_planning_algorithms/abstract_algorithm.hpp" |
| 3 | +#include <rclcpp/rclcpp.hpp> |
| 4 | +#include <rclcpp/serialization.hpp> |
| 5 | + |
| 6 | +#include <geometry_msgs/msg/pose_array.hpp> |
| 7 | +#include <nav_msgs/msg/occupancy_grid.hpp> |
| 8 | + |
| 9 | + |
| 10 | +#include <pybind11/pybind11.h> |
| 11 | +// #include <pybind11/stl.h> |
| 12 | +// #include <pybind11/stl_bind.h> |
| 13 | +// #include <pybind11/eigen.h> |
| 14 | +// #include <pybind11/chrono.h> |
| 15 | +// #include <pybind11/complex.h> |
| 16 | +// #include <pybind11/functional.h> |
| 17 | + |
| 18 | +using namespace freespace_planning_algorithms; |
| 19 | + |
| 20 | +class AstarSearchPython : public AstarSearch |
| 21 | +{ |
| 22 | + using AstarSearch::AstarSearch; |
| 23 | + public: |
| 24 | + void setMapByte(const std::string & costmap_byte) |
| 25 | + { |
| 26 | + rclcpp::SerializedMessage serialized_msg; |
| 27 | + static constexpr size_t message_header_length = 8u; |
| 28 | + serialized_msg.reserve(message_header_length + costmap_byte.size()); |
| 29 | + serialized_msg.get_rcl_serialized_message().buffer_length = costmap_byte.size(); |
| 30 | + for (size_t i = 0; i < costmap_byte.size(); ++i) { |
| 31 | + serialized_msg.get_rcl_serialized_message().buffer[i] = costmap_byte[i]; |
| 32 | + } |
| 33 | + nav_msgs::msg::OccupancyGrid costmap; |
| 34 | + static rclcpp::Serialization<nav_msgs::msg::OccupancyGrid> serializer; |
| 35 | + serializer.deserialize_message(&serialized_msg, &costmap); |
| 36 | + |
| 37 | + AstarSearch::setMap(costmap); |
| 38 | + } |
| 39 | + |
| 40 | + bool makePlanByte(const std::string & start_pose_byte, const std::string & goal_pose_byte) |
| 41 | + { |
| 42 | + rclcpp::SerializedMessage serialized_start_msg; |
| 43 | + static constexpr size_t message_header_length = 8u; |
| 44 | + serialized_start_msg.reserve(message_header_length + start_pose_byte.size()); |
| 45 | + serialized_start_msg.get_rcl_serialized_message().buffer_length = start_pose_byte.size(); |
| 46 | + for (size_t i = 0; i < start_pose_byte.size(); ++i) { |
| 47 | + serialized_start_msg.get_rcl_serialized_message().buffer[i] = start_pose_byte[i]; |
| 48 | + } |
| 49 | + geometry_msgs::msg::Pose start_pose; |
| 50 | + static rclcpp::Serialization<geometry_msgs::msg::Pose> start_serializer; |
| 51 | + start_serializer.deserialize_message(&serialized_start_msg, &start_pose); |
| 52 | + |
| 53 | + rclcpp::SerializedMessage serialized_goal_msg; |
| 54 | + serialized_goal_msg.reserve(message_header_length + goal_pose_byte.size()); |
| 55 | + serialized_goal_msg.get_rcl_serialized_message().buffer_length = goal_pose_byte.size(); |
| 56 | + for (size_t i = 0; i < goal_pose_byte.size(); ++i) { |
| 57 | + serialized_goal_msg.get_rcl_serialized_message().buffer[i] = goal_pose_byte[i]; |
| 58 | + } |
| 59 | + geometry_msgs::msg::Pose goal_pose; |
| 60 | + static rclcpp::Serialization<geometry_msgs::msg::Pose> goal_serializer; |
| 61 | + goal_serializer.deserialize_message(&serialized_goal_msg, &goal_pose); |
| 62 | + |
| 63 | + return AstarSearch::makePlan(start_pose, goal_pose); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | + |
| 68 | +namespace py = pybind11; |
| 69 | + |
| 70 | +PYBIND11_MODULE(freespace_planning_algorithms_python, p) |
| 71 | +{ |
| 72 | + auto pyAstarParam = py::class_<AstarParam>(p, "AstarParam", py::dynamic_attr()) |
| 73 | + .def(py::init<>()) |
| 74 | + .def_readwrite("only_behind_solutions", &AstarParam::only_behind_solutions) |
| 75 | + .def_readwrite("use_back", &AstarParam::use_back) |
| 76 | + .def_readwrite("distance_heuristic_weight", &AstarParam::distance_heuristic_weight); |
| 77 | + auto pyPlannerCommonParam = py::class_<PlannerCommonParam>(p, "PlannerCommonParam", py::dynamic_attr()) |
| 78 | + .def(py::init<>()) |
| 79 | + .def_readwrite("time_limit", &PlannerCommonParam::time_limit) |
| 80 | + .def_readwrite("minimum_turning_radius", &PlannerCommonParam::minimum_turning_radius) |
| 81 | + .def_readwrite("maximum_turning_radius", &PlannerCommonParam::maximum_turning_radius) |
| 82 | + .def_readwrite("turning_radius_size", &PlannerCommonParam::turning_radius_size) |
| 83 | + .def_readwrite("theta_size", &PlannerCommonParam::theta_size) |
| 84 | + .def_readwrite("curve_weight", &PlannerCommonParam::curve_weight) |
| 85 | + .def_readwrite("reverse_weight", &PlannerCommonParam::reverse_weight) |
| 86 | + .def_readwrite("lateral_goal_range", &PlannerCommonParam::lateral_goal_range) |
| 87 | + .def_readwrite("longitudinal_goal_range", &PlannerCommonParam::longitudinal_goal_range) |
| 88 | + .def_readwrite("angle_goal_range", &PlannerCommonParam::angle_goal_range) |
| 89 | + .def_readwrite("obstacle_threshold", &PlannerCommonParam::obstacle_threshold); |
| 90 | + auto pyVSehicleShape = py::class_<VehicleShape>(p, "VehicleShape", py::dynamic_attr()) |
| 91 | + .def(py::init<>()) |
| 92 | + .def(py::init<double, double, double>()) |
| 93 | + .def_readwrite("length", &VehicleShape::length) |
| 94 | + .def_readwrite("width", &VehicleShape::width) |
| 95 | + .def_readwrite("base2back", &VehicleShape::base2back); |
| 96 | + |
| 97 | + py::class_<AbstractPlanningAlgorithm>(p, "AbstractPlanningAlgorithm"); |
| 98 | + py::class_<AstarSearch, AbstractPlanningAlgorithm>(p, "AstarSearchCpp"); |
| 99 | + py::class_<AstarSearchPython, AstarSearch>(p, "AstarSearch") |
| 100 | + .def(py::init<PlannerCommonParam &, VehicleShape &, AstarParam &>()) |
| 101 | + .def("setMap", &AstarSearchPython::setMapByte) |
| 102 | + .def("makePlan", &AstarSearchPython::makePlanByte); |
| 103 | +} |
0 commit comments