|
| 1 | +import math |
| 2 | +import numpy as np |
| 3 | +from shapely.geometry import Point, Polygon |
| 4 | + |
| 5 | + |
| 6 | +def motion(state, v, w, dt): |
| 7 | + x, y, theta, _, _ = state |
| 8 | + # print(f'Current state x = {x} | y = {y} | theta = {theta} | v = {v} | w = {w} | dt = {dt}') |
| 9 | + |
| 10 | + x += v * math.cos(theta) * dt |
| 11 | + y += v * math.sin(theta) * dt |
| 12 | + theta += w * dt |
| 13 | + |
| 14 | + # print(f'NEW state x = {x} | y = {y} | theta = {theta} | v = {v} | w = {w} | dt = {dt}') |
| 15 | + # print(f'-' * 50) |
| 16 | + |
| 17 | + return x, y, theta, v, w |
| 18 | + |
| 19 | + |
| 20 | +def calc_trajectory(state, v, w, config): |
| 21 | + trajectory = [] |
| 22 | + t = 0.0 |
| 23 | + new_state = state |
| 24 | + while t <= config["predict_time"]: |
| 25 | + new_state = motion(new_state, v, w, config["dt"]) |
| 26 | + trajectory.append(new_state) |
| 27 | + t += config["dt"] |
| 28 | + |
| 29 | + return trajectory |
| 30 | + |
| 31 | + |
| 32 | +def calc_to_goal_cost(trajectory, goal, cost_gain): |
| 33 | + x, y, _, _, _ = trajectory[-1] |
| 34 | + distance = math.hypot(goal[0] - x, goal[1] - y) |
| 35 | + return cost_gain * distance |
| 36 | + |
| 37 | + |
| 38 | +def calc_turn_cost(state, local_goal, cost_gain): |
| 39 | + x, y, theta, _, _ = state |
| 40 | + dx = local_goal[0] - x |
| 41 | + dy = local_goal[1] - y |
| 42 | + desired_theta = math.atan2(dy, dx) |
| 43 | + turn_angle = abs(desired_theta - theta) |
| 44 | + turn_angle = min(turn_angle, 2 * math.pi - turn_angle) |
| 45 | + return cost_gain * turn_angle |
| 46 | + |
| 47 | + |
| 48 | +def calc_speed_cost(v, config): |
| 49 | + return config["speed_cost_gain"] * (config["max_speed"] - v) |
| 50 | + |
| 51 | + |
| 52 | +def calc_obstacle_cost_v2(trajectory, obstacle_tree, buffered_obstacles, config): |
| 53 | + min_distance = float("inf") |
| 54 | + for state in trajectory: |
| 55 | + x, y, _, _, _ = state |
| 56 | + point = Point(x, y) |
| 57 | + |
| 58 | + nearby_indices = obstacle_tree.query(point) |
| 59 | + for idx in nearby_indices: |
| 60 | + buffered_obs = buffered_obstacles[idx] |
| 61 | + if buffered_obs.contains(point): |
| 62 | + return float("inf") |
| 63 | + distance = buffered_obs.distance(point) |
| 64 | + if distance < min_distance: |
| 65 | + min_distance = distance |
| 66 | + return config["obstacle_cost_gain"] / (min_distance + 1e-6) |
| 67 | + |
| 68 | + |
| 69 | +def calc_obstacle_cost(trajectory, buffered_obstacles, config): |
| 70 | + min_distance = float("inf") |
| 71 | + for state in trajectory: |
| 72 | + x, y, _, _, _ = state |
| 73 | + point = Point(x, y) |
| 74 | + for buffered_obs in buffered_obstacles: |
| 75 | + if buffered_obs.contains(point): |
| 76 | + return float("inf") |
| 77 | + distance = buffered_obs.distance(point) |
| 78 | + if distance < min_distance: |
| 79 | + min_distance = distance |
| 80 | + return config["obstacle_cost_gain"] / (min_distance + 1e-6) |
| 81 | + |
| 82 | + |
| 83 | +def calc_combined_turn_speed_cost(state, local_goal, v, cost_gain): |
| 84 | + x, y, theta, _, _ = state |
| 85 | + dx = local_goal[0] - x |
| 86 | + dy = local_goal[1] - y |
| 87 | + desired_theta = math.atan2(dy, dx) |
| 88 | + turn_angle = abs(desired_theta - theta) |
| 89 | + turn_angle = min(turn_angle, 2 * math.pi - turn_angle) |
| 90 | + # If the robot is moving fast and needs to turn a lot, the cost increases. |
| 91 | + return cost_gain * turn_angle * v |
| 92 | + |
| 93 | + |
| 94 | +def dwa_control(state, config, buffered_obstacles, local_goal): |
| 95 | + best_cost = float("inf") |
| 96 | + best_control = (0.0, 0.0) |
| 97 | + best_trajectory = [] |
| 98 | + best_cost_info = {} |
| 99 | + |
| 100 | + v_min = max(config["min_speed"], state[3] - config["max_acceleration"] * config["dt"]) |
| 101 | + v_max = min(config["max_speed"], state[3] + config["max_acceleration"] * config["dt"]) |
| 102 | + w_min = -config["max_yaw_rate"] |
| 103 | + w_max = config["max_yaw_rate"] |
| 104 | + |
| 105 | + v_samples = np.linspace(v_min, v_max, num=5) |
| 106 | + w_samples = np.linspace(w_min, w_max, num=5) |
| 107 | + |
| 108 | + for v in v_samples: |
| 109 | + for w in w_samples: |
| 110 | + trajectory = calc_trajectory(state, v, w, config) |
| 111 | + to_goal_cost = calc_to_goal_cost(trajectory, local_goal, config["to_goal_cost_gain"]) |
| 112 | + speed_cost = calc_speed_cost(v, config) |
| 113 | + obstacle_cost = calc_obstacle_cost(trajectory, buffered_obstacles, config) |
| 114 | + turn_cost = calc_turn_cost(state, local_goal, config["turn_cost_gain"]) |
| 115 | + |
| 116 | + total_cost = to_goal_cost + speed_cost + obstacle_cost + turn_cost |
| 117 | + if total_cost < best_cost: |
| 118 | + best_cost = total_cost |
| 119 | + best_control = (v, w) |
| 120 | + best_trajectory = trajectory |
| 121 | + best_cost_info = { |
| 122 | + "total_cost": total_cost, |
| 123 | + "to_goal_cost": to_goal_cost, |
| 124 | + "speed_cost": speed_cost, |
| 125 | + "obstacle_cost": obstacle_cost, |
| 126 | + "turn_cost": turn_cost |
| 127 | + } |
| 128 | + return best_control, best_trajectory, best_cost_info |
| 129 | + |
| 130 | + |
| 131 | +def dwa_control_v2(state, config, obstacle_tree, buffered_obstacles, local_goal): |
| 132 | + best_cost = float("inf") |
| 133 | + best_control = (0.0, 0.0) |
| 134 | + best_trajectory = [] |
| 135 | + best_cost_info = {} |
| 136 | + |
| 137 | + # v_min = max(config["min_speed"], state[3] - config["max_acceleration"] * config["dt"]) |
| 138 | + # v_max = min(config["max_speed"], state[3] + config["max_acceleration"] * config["dt"]) |
| 139 | + |
| 140 | + v_lower = config["min_speed"] |
| 141 | + v_upper = min(config["max_speed"], state[3] + config["max_acceleration"] * config["dt"]) |
| 142 | + v_samples = np.linspace(v_lower, v_upper, num=5) |
| 143 | + |
| 144 | + w_min = -config["max_yaw_rate"] |
| 145 | + w_max = config["max_yaw_rate"] |
| 146 | + |
| 147 | + # v_samples = np.linspace(v_min, v_max, num=5) |
| 148 | + w_samples = np.linspace(w_min, w_max, num=5) |
| 149 | + |
| 150 | + for v in v_samples: |
| 151 | + for w in w_samples: |
| 152 | + trajectory = calc_trajectory(state, v, w, config) |
| 153 | + to_goal_cost = calc_to_goal_cost(trajectory, local_goal, config["to_goal_cost_gain"]) |
| 154 | + speed_cost = calc_speed_cost(v, config) |
| 155 | + obstacle_cost = calc_obstacle_cost_v2(trajectory, obstacle_tree, buffered_obstacles, config) |
| 156 | + # turn_cost = calc_turn_cost(state, local_goal, config["turn_cost_gain"]) |
| 157 | + turn_cost = calc_combined_turn_speed_cost(state, local_goal, v, config["turn_cost_gain"]) |
| 158 | + total_cost = to_goal_cost + speed_cost + obstacle_cost + turn_cost |
| 159 | + if total_cost < best_cost: |
| 160 | + best_cost = total_cost |
| 161 | + best_control = (v, w) |
| 162 | + best_trajectory = trajectory |
| 163 | + best_cost_info = { |
| 164 | + "total_cost": total_cost, |
| 165 | + "to_goal_cost": to_goal_cost, |
| 166 | + "speed_cost": speed_cost, |
| 167 | + "obstacle_cost": obstacle_cost, |
| 168 | + "turn_cost": turn_cost |
| 169 | + } |
| 170 | + return best_control, best_trajectory, best_cost_info |
0 commit comments