From 89eede241b68643b9b76b35adaab7172cfb46f55 Mon Sep 17 00:00:00 2001 From: RexBerry <59031902+RexBerry@users.noreply.github.com> Date: Thu, 20 Oct 2022 18:39:57 -0500 Subject: [PATCH] Change type of attribute time of Point from Optional[float] to float #16 --- flight/avoidance/obstacle_avoidance.py | 9 ++------- flight/avoidance/point.py | 10 +++++++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/flight/avoidance/obstacle_avoidance.py b/flight/avoidance/obstacle_avoidance.py index 6724e74d..d8fb84b1 100644 --- a/flight/avoidance/obstacle_avoidance.py +++ b/flight/avoidance/obstacle_avoidance.py @@ -69,17 +69,12 @@ async def calculate_avoidance_path( # Units don't change, only the type of the object drone_velocity: Velocity = Velocity.from_mavsdk_velocityned(raw_drone_velocity) - obstacle_positions.sort(key=lambda p: p.time or 0.0) + obstacle_positions.sort(key=lambda p: p.time) # Degree of polynomial used in polynomial regression polynomial_degree: int = 3 # Create list of times - obstacle_times: list[float] = [ - point.time for point in obstacle_positions if point.time is not None - ] - - # Should hopefully never fail, otherwise I will have a mental breakdown - assert len(obstacle_times) == len(obstacle_positions) + obstacle_times: list[float] = [point.time for point in obstacle_positions] # Get weights for polynomial regression # The most recent point should have the highest weight diff --git a/flight/avoidance/point.py b/flight/avoidance/point.py index acfe4b33..83d483b2 100644 --- a/flight/avoidance/point.py +++ b/flight/avoidance/point.py @@ -2,6 +2,7 @@ Defines and implements the Point class used in obstacle_avoidance.py """ +import time from dataclasses import dataclass import mavsdk.telemetry @@ -39,7 +40,7 @@ class Point: utm_zone_number: int utm_zone_letter: str altitude: float - time: float | None + time: float @classmethod def from_dict(cls, position_data: InputPoint) -> "Point": @@ -85,9 +86,12 @@ def from_mavsdk_position(cls, position: mavsdk.telemetry.Position) -> "Point": northing: float zone_number: int zone_letter: str + # Can't unpack tuple because mypy complains easting, northing, zone_number, zone_letter = utm.from_latlon( position.latitude_deg, position.longitude_deg ) - # Can't directly unpack function return value because mypy complains - return cls(easting, northing, zone_number, zone_letter, position.absolute_altitude_m, None) + # Use time.time() as the time for the point + return cls( + easting, northing, zone_number, zone_letter, position.absolute_altitude_m, time.time() + )