Skip to content

Commit b5d6019

Browse files
DepthAI Constants Refactoring (#117)
* bruh * refactoring draft * linting whole repository * additional linting * more linting * praise to copilot bruh
1 parent 5b3df8f commit b5d6019

9 files changed

Lines changed: 47 additions & 32 deletions

File tree

onboard/src/controls/scripts/compute_wrench_matrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_robot_name() -> str:
3939
user_robot_name = input(f"Enter the robot name (press enter for default '{default_robot_name}'): ")
4040

4141
# Use the default value if the user input is empty
42-
return user_robot_name.strip() if user_robot_name.strip() else default_robot_name
42+
return user_robot_name.strip() or default_robot_name
4343

4444
def get_transform(node: Node, tf_buffer: Buffer) -> TransformStamped:
4545
"""

onboard/src/cv/cv/buoy_detector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def image_callback(self, data: CompressedImage) -> None:
9797

9898
# takes contour w/ greatest y-distance
9999
if similar_size_contours:
100-
similar_size_contours.sort(key=lambda x: cv2.contourArea(x))
100+
similar_size_contours.sort(key=cv2.contourArea)
101101
best_cnt = similar_size_contours[0]
102102
for cnt in similar_size_contours:
103103
x, y, w, h = cv2.boundingRect(cnt)

onboard/src/cv/cv/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class Torpedo:
4242
HIGH_BOT = np.array([330, 50, 45])
4343
HIGH_TOP = np.array([360, 95, 95])
4444

45+
TORPEDO_BANNER_X_SCALE = 1.2
46+
TORPEDO_BANNER_Y_SCALE = 0.5
47+
4548
class BlueRect:
4649
"""BlueRect color constants."""
4750
BLUE_BOT = np.array([100, 150, 50])

onboard/src/cv/cv/depthai_spatial_detection.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from std_msgs.msg import String
1515

1616
from cv import depthai_camera_connect
17+
from cv.config import Torpedo
1718
from cv.image_tools import ImageTools
1819
from cv.utils import DetectionVisualizer, calculate_relative_pose
1920

@@ -308,10 +309,17 @@ def detect(self) -> None:
308309

309310
confidence = detection.confidence
310311

311-
# Calculate relative pose
312+
# Calculate relative pose, and pull scalings from config dependent on model
313+
scale_x, scale_y, scale_z = None, None, None
314+
match label:
315+
case 'torpedo_banner':
316+
scale_x = Torpedo.TORPEDO_BANNER_X_SCALE
317+
scale_y = Torpedo.TORPEDO_BANNER_Y_SCALE
318+
312319
det_coords_robot_mm = calculate_relative_pose(bbox, tuple(model['input_size']),
313-
tuple(model['sizes'][label]),
314-
self.focal_length, self.sensor_size, 2)
320+
tuple(model['sizes'][label]),
321+
self.focal_length, self.sensor_size, 2,
322+
scale_x=scale_x, scale_y=scale_y, scale_z=scale_z)
315323

316324
# Find yaw angle offset
317325
left_end_compute = self.compute_angle_from_x_offset(detection.xmin * self.camera_pixel_width)
@@ -341,7 +349,7 @@ def detect(self) -> None:
341349
det_coords_robot_mm[2]) # Maintain original z
342350

343351
self.publish_prediction(
344-
bbox, det_coords_robot_mm, yaw_offset, label, confidence,
352+
bbox, det_coords_robot_mm, -yaw_offset, label, confidence,
345353
(self.camera_pixel_height, self.camera_pixel_width), self.using_sonar)
346354

347355
def publish_prediction(self, bbox: tuple, det_coords: tuple, yaw: float, label: str, confidence: float,
@@ -367,21 +375,16 @@ def publish_prediction(self, bbox: tuple, det_coords: tuple, yaw: float, label:
367375
object_msg.label = label
368376
object_msg.score = confidence
369377

370-
if label == 'torpedo_banner':
371-
object_msg.coords.x = 1.2 * det_coords[0]
372-
object_msg.coords.y = 0.5 * det_coords[1]
373-
object_msg.coords.z = det_coords[2]
374-
else:
375-
object_msg.coords.x = det_coords[0]
376-
object_msg.coords.y = det_coords[1]
377-
object_msg.coords.z = det_coords[2]
378+
object_msg.coords.x = det_coords[0]
379+
object_msg.coords.y = det_coords[1]
380+
object_msg.coords.z = det_coords[2]
378381

379382
object_msg.xmin = bbox[0]
380383
object_msg.ymin = bbox[1]
381384
object_msg.xmax = bbox[2]
382385
object_msg.ymax = bbox[3]
383386

384-
object_msg.yaw = -yaw
387+
object_msg.yaw = yaw
385388

386389
object_msg.height = shape[0]
387390
object_msg.width = shape[1]

onboard/src/cv/cv/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def compute_angle_from_x_offset(x_offset: float, camera_pixel_width: float) -> f
7878

7979
def calculate_relative_pose(bbox_bounds: list[int | float], input_size: tuple[float, float],
8080
label_shape: tuple[float, float], focal_length: float,
81-
sensor_size: tuple[float, float], adjustment_factor: int) -> list[float]:
81+
sensor_size: tuple[float, float], adjustment_factor: int,
82+
scale_x: float = 1.0, scale_y: float = 1.0, scale_z: float = 1.0) -> list[float]:
8283
"""
8384
Return relative pose, to be used as a part of the CVObject.
8485
@@ -89,6 +90,9 @@ def calculate_relative_pose(bbox_bounds: list[int | float], input_size: tuple[fl
8990
focal_length (float): The distance between the lens and the image sensor when the lens is focused on a subject.
9091
sensor_size (tuple[float, float]): The physical size of the camera's image sensor.
9192
adjustment_factor (int): 1 if mono, 2 if depthai.
93+
scale_x (float): Multiplicative scalar for the x position.
94+
scale_y (float): Multiplicative scalar for the y position.
95+
scale_z (float): Multiplicative scalar for the z position.
9296
9397
Returns:
9498
list[float]: The relative pose of the object.
@@ -108,6 +112,10 @@ def calculate_relative_pose(bbox_bounds: list[int | float], input_size: tuple[fl
108112
x_meters = cam_dist_with_obj_height(bbox_height, label_shape[1], focal_length, input_size, sensor_size,
109113
adjustment_factor)
110114

115+
x_meters *= scale_x
116+
y_meters *= scale_y
117+
z_meters *= scale_z
118+
111119
return [x_meters, y_meters, z_meters]
112120

113121

onboard/src/cv/launch/usb_camera_detectors.launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def generate_launch_description() -> LaunchDescription:
3434
ld.add_action(IncludeLaunchDescription(
3535
XMLLaunchDescriptionSource(str(pkg_cv / 'launch' / 'torpedo_target_detector.xml')),
3636
))
37-
elif robot_name in ['crush']:
37+
elif robot_name == 'crush':
3838
ld.add_action(IncludeLaunchDescription(
3939
XMLLaunchDescriptionSource(str(pkg_cv / 'launch' / 'hsv_pink_bin_front.xml')),
4040
))

onboard/src/task_planning/task_planning/interface/ivc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime
33
from enum import Enum
44
from pathlib import Path
5+
from typing import Self
56

67
import pytz
78
from custom_msgs.msg import ModemStatus, StringWithHeader
@@ -86,7 +87,7 @@ class IVC:
8687
MESSAGES_TOPIC = '/sensors/modem/messages'
8788
SEND_MESSAGE_SERVICE = '/sensors/modem/send_message'
8889

89-
def __new__(cls, node: Node | None = None, bypass: bool = False) -> 'IVC': # noqa: ARG004
90+
def __new__(cls, node: Node | None = None, bypass: bool = False) -> Self: # noqa: ARG004
9091
"""Create a new instance of the IVC class or return the existing instance."""
9192
if cls._instance is None:
9293
cls._instance = super().__new__(cls)

onboard/src/task_planning/task_planning/interface/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, node: Node, bypass: bool = False, tf_buffer: Buffer | None =
4545
tf_buffer: The transform buffer for the robot. Defaults to None.
4646
"""
4747
self.bypass = bypass
48-
self._tf_buffer = tf_buffer if tf_buffer else Buffer()
48+
self._tf_buffer = tf_buffer or Buffer()
4949

5050
self._received_state = False
5151
self._received_depth = False

onboard/src/task_planning/task_planning/utils/coroutine_utils.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
from collections.abc import Callable, Coroutine
2-
from typing import TypeVar
32

43
from task_planning.task import Task, Yield
54

6-
SendType = TypeVar('SendType')
7-
TransformedSendType = TypeVar('TransformedSendType')
8-
YieldType = TypeVar('YieldType')
9-
TransformedYieldType = TypeVar('TransformedYieldType')
10-
ReturnType = TypeVar('ReturnType')
11-
TransformedReturnType = TypeVar('TransformedReturnType')
125

13-
14-
async def transform(task: Task[YieldType, TransformedSendType, ReturnType],
15-
send_transformer: Callable[[SendType], TransformedSendType] | None = None,
16-
yield_transformer: Callable[[YieldType], TransformedYieldType] | None = None,
17-
return_transformer: Callable[[ReturnType], TransformedReturnType] | None = None) -> \
18-
Coroutine[TransformedYieldType, SendType, TransformedReturnType]:
6+
async def transform[
7+
SendType,
8+
TransformedSendType,
9+
YieldType,
10+
TransformedYieldType,
11+
ReturnType,
12+
TransformedReturnType,
13+
](
14+
task: Task[YieldType, TransformedSendType, ReturnType],
15+
send_transformer: Callable[[SendType], TransformedSendType] | None = None,
16+
yield_transformer: Callable[[YieldType], TransformedYieldType] | None = None,
17+
return_transformer: Callable[[ReturnType], TransformedReturnType] | None = None,
18+
) -> Coroutine[TransformedYieldType, SendType, TransformedReturnType]:
1919
"""
2020
Transform the input and output of a task.
2121

0 commit comments

Comments
 (0)