Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions rocketpy/rocket/parachute.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from inspect import Parameter, signature
from numbers import Real

import numpy as np

Expand Down Expand Up @@ -350,8 +351,8 @@ def wrapper(p, h, y, sensors, u_dot):
self.triggerfunc = _make_wrapper(trigger)
return

# Numeric altitude trigger
if isinstance(trigger, (int, float)):
# Numeric altitude trigger (accept numpy integers/floats; reject bool)
if isinstance(trigger, Real) and not isinstance(trigger, bool):
self._trigger_falling_only = True

def triggerfunc(p, h, y, sensors, u_dot): # pylint: disable=unused-argument
Expand Down Expand Up @@ -379,8 +380,8 @@ def triggerfunc(p, h, y, sensors, u_dot): # pylint: disable=unused-argument
# If we reach this point, the trigger is invalid
raise ValueError(
f"Unable to set the trigger function for parachute '{self.name}'. "
+ "Trigger must be a callable, a float value or one of the strings "
+ "('apogee'). "
+ "Trigger must be a callable, a real number (height in meters), "
+ "or one of the strings ('apogee'). "
+ "See the Parachute class documentation for more information."
)

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/rocket/test_parachute.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,32 @@ def test_callable_trigger_arities_route_arguments(trigger, expects_udot):
result = parachute.triggerfunc(800.0, 500.0, [0.0] * 6, [], [1.0] * 6)
assert result is True
assert parachute.triggerfunc._expects_udot is expects_udot


class TestParachuteNumericTrigger:
"""Numeric height triggers must accept numpy scalar integers/floats and
reject bool (which is a Real subclass in Python)."""

@pytest.mark.parametrize(
"trigger",
[
800,
800.0,
np.int32(800),
np.int64(800),
np.float64(800.0),
],
)
def test_numeric_height_trigger_accepted(self, trigger):
parachute = _make_parachute(trigger=trigger)
assert callable(parachute.triggerfunc)
# Falling below trigger height should fire
y = np.zeros(13)
y[5] = -1.0
assert bool(parachute.triggerfunc(101325.0, 799.0, y, [], None)) is True
assert bool(parachute.triggerfunc(101325.0, 801.0, y, [], None)) is False

@pytest.mark.parametrize("trigger", [True, False])
def test_bool_trigger_rejected(self, trigger):
with pytest.raises(ValueError, match="real number"):
_make_parachute(trigger=trigger)