|
import time |
|
import numpy as np |
|
import pyarrow as pa |
|
from dora import DoraStatus |
|
from constants import LOCATION |
|
|
|
CAMERA_WIDTH = 1280 |
|
CAMERA_HEIGHT = 720 |
|
|
|
|
|
def check_clear_road(bboxes, image_width, goal_x): |
|
""" |
|
Find the x-coordinate of the midpoint of the largest gap along the x-axis where no bounding boxes overlap. |
|
|
|
Parameters: |
|
- bboxes (np.array): A numpy array where each row represents a bounding box with |
|
the format [min_x, min_y, max_x, max_y, confidence, label]. |
|
- image_width (int): The width of the image in pixels. |
|
|
|
Returns: |
|
- int: The x-coordinate of the midpoint of the largest gap where no bounding boxes overlap. |
|
""" |
|
if bboxes.size == 0: |
|
|
|
return goal_x |
|
|
|
events = [] |
|
for bbox in bboxes: |
|
min_x, max_x = bbox[0], bbox[2] |
|
events.append((min_x, "enter")) |
|
events.append((max_x, "exit")) |
|
|
|
|
|
events.append( |
|
(0, "exit") |
|
) |
|
events.append( |
|
(image_width, "enter") |
|
) |
|
|
|
|
|
events.sort(key=lambda x: (x[0], x[1] == "enter")) |
|
|
|
|
|
current_boxes = 1 |
|
last_x = 0 |
|
largest_gap = 0 |
|
gap_start_x = None |
|
largest_gap_mid = None |
|
|
|
for x, event_type in events: |
|
if current_boxes == 0 and gap_start_x is not None: |
|
|
|
gap = x - gap_start_x |
|
gap_end_x = gap_start_x + x |
|
if goal_x < gap_end_x and goal_x > gap_start_x: |
|
return True |
|
elif goal_x < gap_start_x: |
|
return False |
|
if event_type == "enter": |
|
current_boxes += 1 |
|
if current_boxes == 1: |
|
gap_start_x = None |
|
elif event_type == "exit": |
|
current_boxes -= 1 |
|
if current_boxes == 0: |
|
gap_start_x = x |
|
|
|
return False |
|
|
|
|
|
class Operator: |
|
def __init__(self): |
|
self.bboxs = None |
|
self.time = time.time() |
|
self.position = [0, 0, 0] |
|
self.waypoints = None |
|
self.tf = np.array([[1, 0], [0, 1]]) |
|
self.count = 0 |
|
self.completed = True |
|
self.image = None |
|
self.goal = "" |
|
self.current_location = "HOME" |
|
|
|
def on_event( |
|
self, |
|
dora_event: dict, |
|
send_output, |
|
) -> DoraStatus: |
|
if dora_event["type"] == "INPUT": |
|
id = dora_event["id"] |
|
if id == "image": |
|
value = dora_event["value"].to_numpy() |
|
|
|
self.image = value.reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3)) |
|
elif id == "control_reply": |
|
value = dora_event["value"].to_numpy()[0] |
|
if value == self.count: |
|
self.completed = True |
|
elif id == "set_goal": |
|
self.goal = dora_event["value"][0].as_py() |
|
print("got goal:", self.goal, flush=True) |
|
|
|
if len(dora_event["value"]) > 0: |
|
if self.goal != "": |
|
self.waypoints = LOCATION[self.current_location][self.goal] |
|
|
|
elif id == "position": |
|
print("got position:", dora_event["value"], flush=True) |
|
value = dora_event["value"].to_numpy() |
|
[x, y, z] = value |
|
self.position = [x, y, z] |
|
if self.image is None: |
|
print("no image", flush=True) |
|
return DoraStatus.CONTINUE |
|
|
|
if self.completed == False: |
|
print("not completed", flush=True) |
|
return DoraStatus.CONTINUE |
|
|
|
if self.waypoints is None: |
|
print("no waypoint", flush=True) |
|
return DoraStatus.CONTINUE |
|
|
|
|
|
elif ( |
|
self.waypoints.shape[0] == 1 |
|
and np.linalg.norm(self.waypoints[0] - np.array([x, y])) < 0.2 |
|
): |
|
print(f"goal {self.goal} reached", flush=True) |
|
self.current_location = self.goal |
|
send_output( |
|
f"reached_{self.goal.lower()}", pa.array(self.image.ravel()) |
|
) |
|
self.waypoints = None |
|
return DoraStatus.CONTINUE |
|
elif ( |
|
self.waypoints.size > 0 |
|
and np.linalg.norm(self.waypoints[0] - np.array([x, y])) < 0.1 |
|
): |
|
self.waypoints = self.waypoints[1:] |
|
print("removing waypoints", flush=True) |
|
|
|
z = np.deg2rad(z) |
|
self.tf = np.array([[np.cos(z), -np.sin(z)], [np.sin(z), np.cos(z)]]) |
|
goal = self.tf.dot(self.waypoints[0] - np.array([x, y])) |
|
goal_camera_x = ( |
|
CAMERA_WIDTH * np.arctan2(goal[1], goal[0]) / np.pi |
|
) + CAMERA_WIDTH / 2 |
|
goal_angle = np.arctan2(goal[1], goal[0]) * 180 / np.pi |
|
print( |
|
"position", |
|
[x, y], |
|
"goal:", |
|
goal, |
|
"Goal angle: ", |
|
np.arctan2(goal[1], goal[0]) * 180 / np.pi, |
|
"z: ", |
|
np.rad2deg(z), |
|
"x: ", |
|
goal_camera_x, |
|
"count: ", |
|
self.count, |
|
flush=True, |
|
) |
|
|
|
self.count += 1 |
|
self.completed = False |
|
|
|
message = pa.array( |
|
[ |
|
self.waypoints[0][0] - x, |
|
self.waypoints[0][1] - y, |
|
0.0, |
|
0.8, |
|
0.0, |
|
10.0, |
|
float(int(goal_angle)), |
|
self.count, |
|
] |
|
) |
|
print("sending:", message, flush=True) |
|
send_output( |
|
"control", |
|
message, |
|
dora_event["metadata"], |
|
) |
|
|
|
return DoraStatus.CONTINUE |
|
|