Skip to content

Commit

Permalink
Merge branch 'main' into detect_target-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan118 committed Feb 8, 2024
2 parents 5117781 + 8d5eca5 commit dea615a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 15 deletions.
9 changes: 6 additions & 3 deletions modules/data_merge/data_merge_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ def data_merge_worker(timeout: float,
if ((detections.timestamp - previous_odometry.timestamp)
< (current_odometry.timestamp - detections.timestamp)):
# Required for separation
value = merged_odometry_detections.MergedOdometryDetections(
result, merged = merged_odometry_detections.MergedOdometryDetections.create(
previous_odometry.odometry_data,
detections.detections,
)
else:
value = merged_odometry_detections.MergedOdometryDetections(
result, merged = merged_odometry_detections.MergedOdometryDetections.create(
current_odometry.odometry_data,
detections.detections,
)

output_queue.queue.put(value)
if not result:
continue

output_queue.queue.put(merged)
7 changes: 6 additions & 1 deletion modules/detect_target/detect_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ def run(self, data: image_and_time.ImageAndTime) -> "tuple[bool, detections_and_

# Make a copy of bounding boxes in CPU space
objects_bounds = boxes.xyxy.detach().cpu().numpy()
detections = detections_and_time.DetectionsAndTime(data.timestamp)
result, detections = detections_and_time.DetectionsAndTime.create(data.timestamp)
if not result:
return False, None

assert detections is not None

for i in range(0, boxes.shape[0]):
bounds = objects_bounds[i]
label = int(boxes.cls[i])
Expand Down
20 changes: 19 additions & 1 deletion modules/detections_and_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,25 @@ class DetectionsAndTime:
"""
Contains detected object and timestamp.
"""
def __init__(self, timestamp: float):
__create_key = object()

@classmethod
def create(cls, timestamp: float) -> "tuple[bool, DetectionsAndTime | None]":
"""
Sets timestamp to current time.
"""
# Check if timestamp is positive
if timestamp < 0.0:
return False, None

return True, DetectionsAndTime(cls.__create_key, timestamp)

def __init__(self, class_private_create_key, timestamp: float):
"""
Private constructor, use create() method.
"""
assert class_private_create_key is DetectionsAndTime.__create_key, "Use create() method"

self.detections = []
self.timestamp = timestamp

Expand Down
22 changes: 21 additions & 1 deletion modules/merged_odometry_detections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,32 @@ class MergedOdometryDetections:
"""
Contains odometry/telemetry and detections merged by closest timestamp.
"""
__create_key = object()

@classmethod
def create(cls,
odometry_local: drone_odometry_local.DroneOdometryLocal,
detections: "list[detections_and_time.Detection]") \
-> "tuple[bool, MergedOdometryDetections | None]":
"""
odometry_local: Drone position and orientation in local space.
detections: List of Detections from detections_and_time.
"""
if len(detections) == 0:
return False, None

return True, MergedOdometryDetections(cls.__create_key, odometry_local, detections)

def __init__(self,
class_private_create_key,
odometry_local: drone_odometry_local.DroneOdometryLocal,
detections: "list[detections_and_time.Detection]"):
"""
Required for separation.
Private constructor, use create() method.
"""
assert class_private_create_key is MergedOdometryDetections.__create_key, \
"Use create() method"

self.odometry_local = odometry_local
self.detections = detections

Expand Down
4 changes: 3 additions & 1 deletion tests/test_data_merge_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def simulate_detect_target_worker(timestamp: float,
"""
Place the detection into the queue.
"""
detections = detections_and_time.DetectionsAndTime(timestamp)
result, detections = detections_and_time.DetectionsAndTime.create(timestamp)
assert result
assert detections is not None
detections_queue.queue.put(detections)

def simulate_flight_input_worker(timestamp: float,
Expand Down
12 changes: 9 additions & 3 deletions tests/test_geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,15 @@ def test_basic(self,
assert result
assert drone_odometry is not None

merged_detections = merged_odometry_detections.MergedOdometryDetections(
result, merged_detections = merged_odometry_detections.MergedOdometryDetections.create(
drone_odometry,
[
detection1,
detection2,
],
)
assert result
assert merged_detections is not None

result, expected_detection1 = detection_in_world.DetectionInWorld.create(
np.array(
Expand Down Expand Up @@ -859,13 +861,15 @@ def test_advanced(self,
assert result
assert drone_odometry is not None

merged_detections = merged_odometry_detections.MergedOdometryDetections(
result, merged_detections = merged_odometry_detections.MergedOdometryDetections.create(
drone_odometry,
[
detection_bottom_right_point,
detection_centre_left_point,
],
)
assert result
assert merged_detections is not None

result, expected_bottom_right = detection_in_world.DetectionInWorld.create(
np.array(
Expand Down Expand Up @@ -964,10 +968,12 @@ def test_bad_direction(self,
assert result
assert drone_odometry is not None

merged_detections = merged_odometry_detections.MergedOdometryDetections(
result, merged_detections = merged_odometry_detections.MergedOdometryDetections.create(
drone_odometry,
[detection1],
)
assert result
assert merged_detections is not None

# Run
result, actual_list = basic_locator.run(merged_detections)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_geolocation_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def simulate_previous_worker(in_queue: queue_proxy_wrapper.QueueProxyWrapper):
assert result_simulate
assert detection is not None

value = merged_odometry_detections.MergedOdometryDetections(
result_simulate, merged = merged_odometry_detections.MergedOdometryDetections.create(
drone_odometry,
[detection],
)

in_queue.queue.put(value)
assert result_simulate
assert merged is not None
in_queue.queue.put(merged)


if __name__ == "__main__":
Expand Down
7 changes: 5 additions & 2 deletions utilities/workers/worker_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ def check_pause(self):
def request_exit(self):
"""
Requests worker processes to exit.
Does nothing if already requested.
"""
time.sleep(self.__QUEUE_DELAY)
self.__exit_queue.put(None)
if self.__exit_queue.empty():
self.__exit_queue.put(None)

def clear_exit(self):
"""
Clears the exit request condition.
Does nothing if already cleared.
"""
time.sleep(self.__QUEUE_DELAY)
if not self.__exit_queue.empty():
self.__exit_queue.get()
_ = self.__exit_queue.get()

def is_exit_requested(self) -> bool:
"""
Expand Down

0 comments on commit dea615a

Please sign in to comment.