Skip to content

Commit b3f5a0c

Browse files
ENH: add Flight post-step callback across all phases (#758)
1 parent 25271f4 commit b3f5a0c

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎rocketpy/simulation/flight.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ class Flight:
7979
Name of the flight.
8080
Flight._controllers : list
8181
List of controllers to be used during simulation.
82+
Flight.post_step_callback : callable, optional
83+
Optional callback invoked once after every successful ODE solver
84+
step for the entire simulation, including parachute descent.
85+
Receives the ``Flight`` instance (``callback(flight)``). Use
86+
``flight.t`` and ``flight.y_sol`` for the current time and state.
87+
Controllers stop being useful after parachute deployment; this
88+
callback is the extension point for full-lifecycle observers
89+
(e.g. ground-station / radio update simulation).
8290
Flight.max_time : int, float
8391
Maximum simulation time allowed. Refers to physical time
8492
being simulated, not time taken to run simulation.
@@ -506,6 +514,7 @@ def __init__( # pylint: disable=too-many-arguments,too-many-statements
506514
equations_of_motion="standard",
507515
ode_solver="LSODA",
508516
simulation_mode="6 DOF",
517+
post_step_callback=None,
509518
):
510519
"""Run a trajectory simulation.
511520
@@ -592,6 +601,12 @@ def __init__( # pylint: disable=too-many-arguments,too-many-statements
592601
simulation_mode : str, optional
593602
Simulation mode to use. Can be "6 DOF" for 6 degrees of freedom or
594603
"3 DOF" for 3 degrees of freedom. Default is "6 DOF".
604+
post_step_callback : callable, optional
605+
Callback invoked once after every successful ODE solver step for
606+
the entire simulation, including parachute phases. Signature is
607+
``callback(flight)``, matching phase/node callbacks. Access the
608+
current time and state via ``flight.t`` and ``flight.y_sol``.
609+
Default is None.
595610
Returns
596611
-------
597612
None
@@ -600,6 +615,9 @@ def __init__( # pylint: disable=too-many-arguments,too-many-statements
600615
----------
601616
.. [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html
602617
"""
618+
if post_step_callback is not None and not callable(post_step_callback):
619+
raise TypeError("post_step_callback must be callable or None")
620+
603621
# Save arguments
604622
self.env = environment
605623
self.rocket = rocket
@@ -626,6 +644,7 @@ def __init__( # pylint: disable=too-many-arguments,too-many-statements
626644
self.equations_of_motion = equations_of_motion
627645
self.simulation_mode = simulation_mode
628646
self.ode_solver = ode_solver
647+
self.post_step_callback = post_step_callback
629648

630649
# Controller initialization
631650
self.__init_controllers()
@@ -800,6 +819,9 @@ def __simulate(self, verbose):
800819
self.sensors,
801820
self.env,
802821
)
822+
# Full-lifecycle observer (all phases, including parachute)
823+
if self.post_step_callback is not None:
824+
self.post_step_callback(self)
803825
if self.__check_simulation_events(phase, phase_index, node_index):
804826
break # Stop if simulation termination event occurred
805827

‎tests/unit/simulation/test_flight.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,47 @@ def test_get_controller_observed_variables(flight_calisto_air_brakes):
139139
assert len(obs_vars) == 0
140140

141141

142+
def test_post_step_callback_runs_before_and_after_apogee(
143+
calisto_robust, example_plain_env
144+
):
145+
"""post_step_callback must fire across the full flight, including descent.
146+
147+
Controllers are not a substitute: air-brake fixtures often terminate at
148+
apogee, and parachute phases are not meant to keep feeding actuators.
149+
This callback is the full-lifecycle observer hook (issue #758).
150+
"""
151+
callback_times = []
152+
153+
def record_step(flight):
154+
callback_times.append(flight.t)
155+
156+
flight = Flight(
157+
rocket=calisto_robust,
158+
environment=example_plain_env,
159+
rail_length=5.2,
160+
inclination=85,
161+
heading=0,
162+
terminate_on_apogee=False,
163+
post_step_callback=record_step,
164+
)
165+
166+
assert callback_times, "post_step_callback was never invoked"
167+
assert min(callback_times) < flight.apogee_time
168+
assert max(callback_times) > flight.apogee_time
169+
assert flight.t_final > flight.apogee_time
170+
171+
172+
def test_post_step_callback_must_be_callable(calisto, example_plain_env):
173+
"""Non-callable post_step_callback values are rejected at construction."""
174+
with pytest.raises(TypeError, match="post_step_callback"):
175+
Flight(
176+
rocket=calisto,
177+
environment=example_plain_env,
178+
rail_length=5.2,
179+
post_step_callback="not-callable",
180+
)
181+
182+
142183
def test_initial_stability_margin(flight_calisto_custom_wind):
143184
"""Test the initial_stability_margin method of the Flight class.
144185

0 commit comments

Comments
 (0)