|
| 1 | +""" |
| 2 | +rrt_star_bidirectional_path_planning.py |
| 3 | +
|
| 4 | +Author: Auto-generated |
| 5 | +""" |
| 6 | + |
| 7 | +# import path setting |
| 8 | +import numpy as np |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +abs_dir_path = str(Path(__file__).absolute().parent) |
| 13 | +relative_path = "/../../../components/" |
| 14 | +relative_simulations = "/../../../simulations/" |
| 15 | + |
| 16 | + |
| 17 | +sys.path.append(abs_dir_path + relative_path + "visualization") |
| 18 | +sys.path.append(abs_dir_path + relative_path + "state") |
| 19 | +sys.path.append(abs_dir_path + relative_path + "vehicle") |
| 20 | +sys.path.append(abs_dir_path + relative_path + "obstacle") |
| 21 | +sys.path.append(abs_dir_path + relative_path + "mapping/grid") |
| 22 | +sys.path.append(abs_dir_path + relative_path + "course/cubic_spline_course") |
| 23 | +sys.path.append(abs_dir_path + relative_path + "control/pure_pursuit") |
| 24 | +sys.path.append(abs_dir_path + relative_path + "plan/rrt_star_bidirectional") |
| 25 | + |
| 26 | + |
| 27 | +# import component modules |
| 28 | +from global_xy_visualizer import GlobalXYVisualizer |
| 29 | +from min_max import MinMax |
| 30 | +from time_parameters import TimeParameters |
| 31 | +from vehicle_specification import VehicleSpecification |
| 32 | +from state import State |
| 33 | +from four_wheels_vehicle import FourWheelsVehicle |
| 34 | +from obstacle import Obstacle |
| 35 | +from obstacle_list import ObstacleList |
| 36 | +from cubic_spline_course import CubicSplineCourse |
| 37 | +from pure_pursuit_controller import PurePursuitController |
| 38 | +from rrt_star_bidirectional_path_planner import RrtStarBidirectionalPathPlanner |
| 39 | +from binary_occupancy_grid import BinaryOccupancyGrid |
| 40 | +import json |
| 41 | + |
| 42 | + |
| 43 | +# flag to show plot figure |
| 44 | +# when executed as unit test, this flag is set as false |
| 45 | +show_plot = True |
| 46 | + |
| 47 | + |
| 48 | +def main(): |
| 49 | + """ |
| 50 | + Main process function |
| 51 | + """ |
| 52 | + |
| 53 | + # set simulation parameters |
| 54 | + x_lim, y_lim = MinMax(-5, 55), MinMax(-20, 25) |
| 55 | + navigation_gif_path = abs_dir_path + relative_simulations + "path_planning/rrt_star_bidirectional_path_planning/rrt_star_bidirectional_navigate.gif" |
| 56 | + map_path = abs_dir_path + relative_simulations + "path_planning/rrt_star_bidirectional_path_planning/map.json" |
| 57 | + path_filename = abs_dir_path + relative_simulations + "path_planning/rrt_star_bidirectional_path_planning/path.json" |
| 58 | + search_gif_path = abs_dir_path + relative_simulations + "path_planning/rrt_star_bidirectional_path_planning/rrt_star_bidirectional_search.gif" |
| 59 | + |
| 60 | + |
| 61 | + vis = GlobalXYVisualizer(x_lim, y_lim, TimeParameters(span_sec=25), show_zoom=False, gif_name=navigation_gif_path) |
| 62 | + occ_grid = BinaryOccupancyGrid(x_lim, y_lim, resolution=0.5, clearance=1.5, map_path=map_path) |
| 63 | + |
| 64 | + obst_list = ObstacleList() |
| 65 | + obst_list.add_obstacle(Obstacle(State(x_m=10.0, y_m=15.0), length_m=10, width_m=8)) |
| 66 | + obst_list.add_obstacle(Obstacle(State(x_m=40.0, y_m=0.0), length_m=2, width_m=10)) |
| 67 | + obst_list.add_obstacle(Obstacle(State(x_m=10.0, y_m=-10.0, yaw_rad=np.rad2deg(45)), length_m=5, width_m=5)) |
| 68 | + obst_list.add_obstacle(Obstacle(State(x_m=30.0, y_m=15.0, yaw_rad=np.rad2deg(10)), length_m=5, width_m=2)) |
| 69 | + obst_list.add_obstacle(Obstacle(State(x_m=50.0, y_m=15.0, yaw_rad=np.rad2deg(15)), length_m=5, width_m=2)) |
| 70 | + obst_list.add_obstacle(Obstacle(State(x_m=25.0, y_m=0.0), length_m=2, width_m=2)) |
| 71 | + obst_list.add_obstacle(Obstacle(State(x_m=35.0, y_m=-15.0), length_m=7, width_m=2)) |
| 72 | + |
| 73 | + vis.add_object(obst_list) |
| 74 | + occ_grid.add_object(obst_list) |
| 75 | + occ_grid.save_map() |
| 76 | + # Easy Goal = (50,22) |
| 77 | + # Hard Goal = (50,-10) |
| 78 | + planner = RrtStarBidirectionalPathPlanner((0, 0), (50, -10), map_path, x_lim=x_lim, y_lim=y_lim, path_filename=path_filename, gif_name=search_gif_path, |
| 79 | + max_iterations=5000, step_size=0.5, goal_sample_rate=0.05) |
| 80 | + |
| 81 | + # Load sparse path from json file |
| 82 | + with open(path_filename, 'r') as f: |
| 83 | + sparse_path = json.load(f) |
| 84 | + |
| 85 | + # Extract x and y coordinates |
| 86 | + sparse_x = [point[0] for point in sparse_path] |
| 87 | + sparse_y = [point[1] for point in sparse_path] |
| 88 | + |
| 89 | + # Use with CubicSplineCourse |
| 90 | + course = CubicSplineCourse(sparse_x, sparse_y, 20) |
| 91 | + vis.add_object(course) |
| 92 | + |
| 93 | + # create vehicle instance |
| 94 | + spec = VehicleSpecification() |
| 95 | + pure_pursuit = PurePursuitController(spec, course) |
| 96 | + vehicle = FourWheelsVehicle(State(color=spec.color), spec, |
| 97 | + controller=pure_pursuit, |
| 98 | + show_zoom=False) |
| 99 | + |
| 100 | + vis.add_object(vehicle) |
| 101 | + |
| 102 | + # plot figure is not shown when executed as unit test |
| 103 | + if not show_plot: vis.not_show_plot() |
| 104 | + |
| 105 | + # show plot figure |
| 106 | + vis.draw() |
| 107 | + |
| 108 | + |
| 109 | +# execute main process |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments