Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use acceleration spike to switch to LandedState #118

Merged
merged 6 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions airbrakes/data_handling/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def max_vertical_velocity(self) -> float:
"""The maximum vertical velocity the rocket has attained during the flight, in m/s."""
return float(self._max_vertical_velocity)

@property
harshil21 marked this conversation as resolved.
Show resolved Hide resolved
def average_vertical_acceleration(self) -> float:
"""The average vertical acceleration of the rocket in m/s^2."""
return float(np.mean(self._rotated_accelerations))

@property
def current_timestamp(self) -> int:
"""The timestamp of the last data packet in nanoseconds."""
Expand Down
6 changes: 3 additions & 3 deletions airbrakes/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from constants import (
GROUND_ALTITUDE_METERS,
LANDED_SPEED_METERS_PER_SECOND,
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED,
MAX_FREE_FALL_SECONDS,
MAX_VELOCITY_THRESHOLD,
TAKEOFF_HEIGHT_METERS,
Expand Down Expand Up @@ -180,10 +180,10 @@ def update(self):

data = self.context.data_processor

# If our altitude and speed are around 0, we have landed
# If our altitude is around 0, and we have an acceleration spike, we have landed
if (
data.current_altitude <= GROUND_ALTITUDE_METERS
and abs(data.vertical_velocity) <= LANDED_SPEED_METERS_PER_SECOND
and data.average_vertical_acceleration >= LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED
):
self.next_state()

Expand Down
5 changes: 2 additions & 3 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ class DisplayEndingType(StrEnum):

GROUND_ALTITUDE_METERS = 10.0
"""The altitude in meters that the rocket must be under before we consider it to have landed."""
LANDED_SPEED_METERS_PER_SECOND = 5.0
"""The speed in meters per second that the rocket must be under before we consider it to have
landed."""
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED = 70.0
"""The acceleration in m/s^2 that the rocket must be above before we consider it to have landed."""

# -------------------------------------------------------
# Apogee Prediction Configuration
Expand Down
3 changes: 3 additions & 0 deletions tests/test_data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def test_init(self, data_processor):
assert d.vertical_velocity == 0.0
assert d.max_vertical_velocity == 0.0
assert d.current_timestamp == 0
assert d.average_vertical_acceleration == 0.0

def test_str(self, data_processor):
data_str = (
Expand Down Expand Up @@ -362,6 +363,8 @@ def test_first_update(self, data_processor, data_packets, init_alt, max_alt, rot
assert data.vertical_acceleration == d._rotated_accelerations[idx]
assert data.time_since_last_data_packet == d._time_differences[idx]

assert d.average_vertical_acceleration == np.mean(d._rotated_accelerations)

@pytest.mark.parametrize(
# altitude reading - list of altitudes passed to the data processor (estPressureAlt)
# current_altitude - calculated current altitude of the rocket, zeroed out.
Expand Down
47 changes: 17 additions & 30 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from airbrakes.data_handling.logged_data_packet import LoggedDataPacket
from constants import (
GROUND_ALTITUDE_METERS,
LANDED_SPEED_METERS_PER_SECOND,
TAKEOFF_HEIGHT_METERS,
TAKEOFF_VELOCITY_METERS_PER_SECOND,
ServoExtension,
Expand Down Expand Up @@ -124,25 +123,15 @@ def test_update(
# Let's validate our data!

# Check we have all the states:
if launch_name == "purple_launch":
# Our Launches don't properly log/reach the LandedState, so we will ignore it for now.
assert len(states_dict) == 4
assert list(states_dict.keys()) == [
"StandbyState",
"MotorBurnState",
"CoastState",
"FreeFallState",
]
else:
assert len(states_dict) == 5
# Order of states matters!
assert list(states_dict.keys()) == [
"StandbyState",
"MotorBurnState",
"CoastState",
"FreeFallState",
"LandedState",
]
assert len(states_dict) == 5
# Order of states matters!
assert list(states_dict.keys()) == [
"StandbyState",
"MotorBurnState",
"CoastState",
"FreeFallState",
"LandedState",
]
states_dict = types.SimpleNamespace(**states_dict)
stand_by_state = states_dict.StandbyState
motor_burn_state = states_dict.MotorBurnState
Expand Down Expand Up @@ -194,14 +183,15 @@ def test_update(
assert free_fall_state.min_altitude <= GROUND_ALTITUDE_METERS + 10.0
assert all(ext == ServoExtension.MIN_EXTENSION for ext in free_fall_state.extensions)

# Check landed state values:
landed_state = states_dict.LandedState
if launch_name != "purple_launch":
# Generated data for simulated landing for interest launcH:
landed_state = states_dict.LandedState
assert landed_state.min_velocity >= -LANDED_SPEED_METERS_PER_SECOND
# Generated data for simulated landing for interest launch:
assert landed_state.min_velocity >= -15.0 # high velocity impact
harshil21 marked this conversation as resolved.
Show resolved Hide resolved
assert landed_state.max_velocity <= 0.1
assert landed_state.min_altitude <= GROUND_ALTITUDE_METERS
assert landed_state.max_altitude <= GROUND_ALTITUDE_METERS + 10.0
assert all(ext == ServoExtension.MIN_EXTENSION for ext in landed_state.extensions)
assert landed_state.min_altitude <= GROUND_ALTITUDE_METERS
assert landed_state.max_altitude <= GROUND_ALTITUDE_METERS + 10.0
assert all(ext == ServoExtension.MIN_EXTENSION for ext in landed_state.extensions)

# Now let's check if everything was logged correctly:
# some what of a duplicate of test_logger.py:
Expand Down Expand Up @@ -258,7 +248,4 @@ def test_update(
assert line_number > 80_000

# Check if all states were logged:
if launch_name == "purple_launch":
assert state_list == ["S", "M", "C", "F"]
else:
assert state_list == ["S", "M", "C", "F", "L"]
assert state_list == ["S", "M", "C", "F", "L"]
45 changes: 35 additions & 10 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
from constants import (
GROUND_ALTITUDE_METERS,
LANDED_SPEED_METERS_PER_SECOND,
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED,
LOG_BUFFER_SIZE,
MAX_FREE_FALL_SECONDS,
MAX_VELOCITY_THRESHOLD,
Expand Down Expand Up @@ -307,21 +307,41 @@ def test_name(self, free_fall_state):
assert free_fall_state.name == "FreeFallState"

@pytest.mark.parametrize(
("current_altitude", "vertical_velocity", "expected_state", "time_length"),
("current_altitude", "vertical_accel", "expected_state", "time_length"),
[
(GROUND_ALTITUDE_METERS * 4, -(LANDED_SPEED_METERS_PER_SECOND * 4), FreeFallState, 1.0),
(GROUND_ALTITUDE_METERS * 2, -(LANDED_SPEED_METERS_PER_SECOND * 2), FreeFallState, 1.0),
(GROUND_ALTITUDE_METERS - 5, -(LANDED_SPEED_METERS_PER_SECOND * 2), FreeFallState, 1.0),
(GROUND_ALTITUDE_METERS - 5, LANDED_SPEED_METERS_PER_SECOND - 1.0, LandedState, 1.0),
(
GROUND_ALTITUDE_METERS * 4,
-(LANDED_SPEED_METERS_PER_SECOND * 4),
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED / 4,
FreeFallState,
1.0,
),
(
GROUND_ALTITUDE_METERS * 2,
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED / 2,
FreeFallState,
1.0,
),
(
GROUND_ALTITUDE_METERS - 5,
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED / 2,
FreeFallState,
1.0,
),
(
GROUND_ALTITUDE_METERS - 5,
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED,
LandedState,
1.0,
),
(
GROUND_ALTITUDE_METERS * 4,
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED / 4,
FreeFallState,
MAX_FREE_FALL_SECONDS - 1.0,
),
(
GROUND_ALTITUDE_METERS * 4,
-(LANDED_SPEED_METERS_PER_SECOND * 4),
LANDED_ACCELERATION_METERS_PER_SECOND_SQUARED * 4,
LandedState,
MAX_FREE_FALL_SECONDS,
),
Expand All @@ -336,10 +356,15 @@ def test_name(self, free_fall_state):
],
)
def test_update(
self, free_fall_state, current_altitude, vertical_velocity, expected_state, time_length
self,
free_fall_state,
current_altitude,
vertical_accel,
expected_state,
time_length,
):
free_fall_state.context.data_processor._current_altitudes = [current_altitude]
free_fall_state.context.data_processor._vertical_velocities = [vertical_velocity]
free_fall_state.context.data_processor._rotated_accelerations = [vertical_accel]
free_fall_state.start_time_ns = 0
free_fall_state.context.data_processor._last_data_packet = EstimatedDataPacket(
time_length * 1e9
Expand Down
Loading