Skip to content

Commit 5f81928

Browse files
committed
Modify function to bounding box coordinates from server
1 parent 9f91f18 commit 5f81928

1 file changed

Lines changed: 111 additions & 54 deletions

File tree

GEMstack/onboard/planning/route_planning_component.py

Lines changed: 111 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@
1212
from typing import List
1313
from ..component import Component
1414
from ...utils import serialization
15-
from ...state import Route,ObjectFrameEnum
15+
from ...state import Route, ObjectFrameEnum
1616
import math
1717
import requests
1818

1919

2020
class RoutePlanningComponent(Component):
2121
"""Reads a route from disk and returns it as the desired route."""
22+
2223
def __init__(self):
2324
print("Route Planning Component init")
2425
self.planner = None
2526
self.route = None
26-
27+
2728
def state_inputs(self):
2829
return ["all"]
2930

3031
def state_outputs(self) -> List[str]:
31-
return ['route']
32+
return ["route"]
3233

3334
def rate(self):
3435
return 10.0
@@ -46,29 +47,43 @@ def update(self, state: AllState):
4647
print("I am in PARKING mode")
4748
# Return a route after doing some processing based on mission plan REMOVE ONCE OTHER PLANNERS ARE IMPLEMENTED
4849
base_path = os.path.dirname(__file__)
49-
file_path = os.path.join(base_path, "../../knowledge/routes/forward_15m_extra.csv")
50-
51-
waypoints = np.loadtxt(file_path, delimiter=',', dtype=float)
50+
file_path = os.path.join(
51+
base_path, "../../knowledge/routes/forward_15m_extra.csv"
52+
)
53+
54+
waypoints = np.loadtxt(file_path, delimiter=",", dtype=float)
5255
if waypoints.shape[1] == 3:
53-
waypoints = waypoints[:,:2]
56+
waypoints = waypoints[:, :2]
5457
print("waypoints", waypoints)
55-
self.route = Route(frame=ObjectFrameEnum.START,points=waypoints.tolist())
58+
self.route = Route(frame=ObjectFrameEnum.START, points=waypoints.tolist())
5659
elif state.mission_plan.planner_type.value == PlannerEnum.RRT_STAR.value:
5760
print("I am in RRT mode")
58-
start = (state.vehicle.pose.x+1, state.vehicle.pose.y+1)
59-
goal = (state.mission_plan.goal_x, state.mission_plan.goal_y) #When we implement kinodynamic, we need to include target_yaw also
60-
x_bounds = (0,20)
61-
y_bounds = (0,20)
61+
start = (state.vehicle.pose.x + 1, state.vehicle.pose.y + 1)
62+
goal = (
63+
state.mission_plan.goal_x,
64+
state.mission_plan.goal_y,
65+
) # When we implement kinodynamic, we need to include target_yaw also
66+
x_bounds = (0, 20)
67+
y_bounds = (0, 20)
6268
step_size = 1.0
6369
max_iter = 2000
64-
occupancy_grid = np.zeros((20, 20), dtype=int)
70+
occupancy_grid = np.zeros((20, 20), dtype=int)
6571
occupancy_grid[5:10, 5:10] = 1
66-
self.planner = RRTStar(start, goal, x_bounds, y_bounds, max_iter=max_iter, step_size=step_size, vehicle_width=1, occupancy_grid=occupancy_grid)
72+
self.planner = RRTStar(
73+
start,
74+
goal,
75+
x_bounds,
76+
y_bounds,
77+
max_iter=max_iter,
78+
step_size=step_size,
79+
vehicle_width=1,
80+
occupancy_grid=occupancy_grid,
81+
)
6782
rrt_resp = self.planner.plan()
6883
self.route = Route(frame=ObjectFrameEnum.START, points=rrt_resp)
6984
else:
7085
print("Unknown mode")
71-
86+
7287
return self.route
7388

7489

@@ -119,9 +134,10 @@ def max_visible_arc(circle_center, radius, geofence):
119134
for i in range(len(max_arc)):
120135
# max_arc[i].append(heading_on_circle(xc,yc,max_arc[0], max_arc[1]))
121136
max_arc[i] = np.array(max_arc[i])
122-
np.append(max_arc[i],0)
137+
np.append(max_arc[i], 0)
123138
return max_arc
124139

140+
125141
def heading_on_circle(cx, cy, px, py):
126142
dx = px - cx
127143
dy = py - cy
@@ -135,13 +151,23 @@ def check_point_exists(vehicle, server_url="http://localhost:8000"):
135151
response = requests.get(f"{server_url}/api/inspect")
136152
response.raise_for_status()
137153
points = response.json().get("coords", [])
138-
139-
for point in points:
140-
pt1 = ObjectPose(frame=ObjectFrameEnum.GLOBAL, t=0, x=point[0]["lat"], y=point[0]["lng"])
141-
pt2 = ObjectPose(frame=ObjectFrameEnum.GLOBAL, t=0, x=point[1]["lat"], y=point[1]["lng"])
154+
155+
if points:
156+
pt1 = ObjectPose(
157+
frame=ObjectFrameEnum.GLOBAL,
158+
t=0,
159+
x=points[0]["lng"],
160+
y=points[0]["lat"],
161+
)
162+
pt2 = ObjectPose(
163+
frame=ObjectFrameEnum.GLOBAL,
164+
t=0,
165+
x=points[1]["lng"],
166+
y=points[1]["lat"],
167+
)
142168
pt1.to_frame(ObjectFrameEnum.START)
143169
pt2.to_frame(ObjectFrameEnum.START)
144-
return True, [[pt1.x, pt1.y],[pt2.x, pt2.y]]
170+
return True, [[pt1.x, pt1.y], [pt2.x, pt2.y]]
145171
return False, []
146172

147173
except requests.exceptions.RequestException as e:
@@ -151,21 +177,24 @@ def check_point_exists(vehicle, server_url="http://localhost:8000"):
151177

152178
class InspectRoutePlanner(Component):
153179
"""Reads a route from disk and returns it as the desired route."""
154-
def __init__(self, state_machine, frame : str = 'start'):
155-
self.geofence_area = [[0,0],[40,40]]
180+
181+
def __init__(self, state_machine, frame: str = "start"):
182+
self.geofence_area = [[0, 0], [40, 40]]
156183
self.state_list = state_machine
157184
self.index = 1
158185
self.mission = self.state_list[self.index]
159-
self.circle_center = [30,30]
186+
self.circle_center = [30, 30]
160187
self.radius = 20
161-
self.inspection_route = max_visible_arc(self.circle_center, self.radius, self.geofence_area)
162-
self.start = [0,0]
188+
self.inspection_route = max_visible_arc(
189+
self.circle_center, self.radius, self.geofence_area
190+
)
191+
self.start = [0, 0]
163192

164193
def state_inputs(self):
165-
return ['all']
194+
return ["all"]
166195

167196
def state_outputs(self) -> List[str]:
168-
return ['route']
197+
return ["route"]
169198

170199
def rate(self):
171200
return 1.0
@@ -179,58 +208,86 @@ def update(self, state):
179208
points_found, pts = check_point_exists(state.vehicle)
180209
if points_found:
181210
self.inspection_area = pts
182-
print(self.state_list[self.index+1])
183-
self.mission = self.state_list[self.index+1]
211+
print(self.state_list[self.index + 1])
212+
self.mission = self.state_list[self.index + 1]
184213
self.index += 1
185214
print("CHANGING STATES", self.mission)
186215
self.start = [state.vehicle.pose.x, state.vehicle.pose.y]
187-
self.circle_center = [(self.inspection_area[0][0]+self.inspection_area[1][0])/2, (self.inspection_area[0][1]+self.inspection_area[1][1])/2]
188-
self.radius = ((self.inspection_area[0][0]+self.inspection_area[1][0])**2 + (self.inspection_area[0][1]+self.inspection_area[1][1])**2)**0.5/2
189-
self.inspection_route = max_visible_arc(self.circle_center, self.radius, self.geofence_area)
216+
self.circle_center = [
217+
(self.inspection_area[0][0] + self.inspection_area[1][0]) / 2,
218+
(self.inspection_area[0][1] + self.inspection_area[1][1]) / 2,
219+
]
220+
self.radius = (
221+
(self.inspection_area[0][0] + self.inspection_area[1][0]) ** 2
222+
+ (self.inspection_area[0][1] + self.inspection_area[1][1]) ** 2
223+
) ** 0.5 / 2
224+
self.inspection_route = max_visible_arc(
225+
self.circle_center, self.radius, self.geofence_area
226+
)
190227
elif self.mission == "NAV":
191228
state.mission.type = MissionEnum.DRIVE
192-
start = (state.vehicle.pose.x+1, state.vehicle.pose.y+1)
193-
goal = (self.inspection_route[0][0]-3, self.inspection_route[0][1]-3)
194-
if(abs(start[0]-goal[0]) <= 1 and abs(start[1]-goal[1]) <= 1):
195-
print(self.state_list[self.index+1])
196-
self.mission = self.state_list[self.index+1]
229+
start = (state.vehicle.pose.x + 1, state.vehicle.pose.y + 1)
230+
goal = (self.inspection_route[0][0] - 3, self.inspection_route[0][1] - 3)
231+
if abs(start[0] - goal[0]) <= 1 and abs(start[1] - goal[1]) <= 1:
232+
print(self.state_list[self.index + 1])
233+
self.mission = self.state_list[self.index + 1]
197234
self.index += 1
198235
print("CHANGING STATES", self.mission)
199-
x_bounds = (0,50)
200-
y_bounds = (0,50)
236+
x_bounds = (0, 50)
237+
y_bounds = (0, 50)
201238
step_size = 1.0
202239
max_iter = 2000
203-
occupancy_grid = np.zeros((50, 50), dtype=int)
240+
occupancy_grid = np.zeros((50, 50), dtype=int)
204241
# occupancy_grid[5:10, 5:10] = 1
205-
self.planner = RRTStar(start, goal, x_bounds, y_bounds, max_iter=max_iter, step_size=step_size, vehicle_width=1, occupancy_grid=occupancy_grid)
242+
self.planner = RRTStar(
243+
start,
244+
goal,
245+
x_bounds,
246+
y_bounds,
247+
max_iter=max_iter,
248+
step_size=step_size,
249+
vehicle_width=1,
250+
occupancy_grid=occupancy_grid,
251+
)
206252
rrt_resp = self.planner.plan()
207253
self.route = Route(frame=ObjectFrameEnum.START, points=rrt_resp)
208254
elif self.mission == "INSPECT":
209255
state.mission.type = MissionEnum.INSPECT
210-
start = (state.vehicle.pose.x+1, state.vehicle.pose.y+1)
256+
start = (state.vehicle.pose.x + 1, state.vehicle.pose.y + 1)
211257
goal = (self.inspection_route[-1][0], self.inspection_route[-1][1])
212-
if(abs(start[0]-goal[0]) <= 1 and abs(start[1]-goal[1]) <= 1):
213-
print(self.state_list[self.index+1])
214-
self.mission = self.state_list[self.index+1]
258+
if abs(start[0] - goal[0]) <= 1 and abs(start[1] - goal[1]) <= 1:
259+
print(self.state_list[self.index + 1])
260+
self.mission = self.state_list[self.index + 1]
215261
self.index += 1
216262
print("CHANGING STATES", self.mission)
217263
self.flag += 0.1
218-
self.route = Route(frame=ObjectFrameEnum.START, points=self.inspection_route)
264+
self.route = Route(
265+
frame=ObjectFrameEnum.START, points=self.inspection_route
266+
)
219267
elif self.mission == "FINISH":
220268
state.mission.type = MissionEnum.INSPECT_UPLOAD
221-
start = (state.vehicle.pose.x+1, state.vehicle.pose.y+1)
269+
start = (state.vehicle.pose.x + 1, state.vehicle.pose.y + 1)
222270
goal = (self.start[0], self.start[1])
223-
if(abs(start[0]-goal[0]) <= 1 and abs(start[1]-goal[1]) <= 1):
224-
print(self.state_list[self.index+1])
225-
self.mission = self.state_list[self.index+1]
271+
if abs(start[0] - goal[0]) <= 1 and abs(start[1] - goal[1]) <= 1:
272+
print(self.state_list[self.index + 1])
273+
self.mission = self.state_list[self.index + 1]
226274
self.index += 1
227275
print("CHANGING STATES", self.mission)
228-
x_bounds = (0,50)
229-
y_bounds = (0,50)
276+
x_bounds = (0, 50)
277+
y_bounds = (0, 50)
230278
step_size = 1.0
231279
max_iter = 2000
232280
occupancy_grid = np.zeros((50, 50), dtype=int)
233-
self.planner = RRTStar(start, goal, x_bounds, y_bounds, max_iter=max_iter, step_size=step_size, vehicle_width=1, occupancy_grid=occupancy_grid)
281+
self.planner = RRTStar(
282+
start,
283+
goal,
284+
x_bounds,
285+
y_bounds,
286+
max_iter=max_iter,
287+
step_size=step_size,
288+
vehicle_width=1,
289+
occupancy_grid=occupancy_grid,
290+
)
234291
rrt_resp = self.planner.plan()
235292
self.route = Route(frame=ObjectFrameEnum.START, points=rrt_resp)
236293

0 commit comments

Comments
 (0)