Skip to content

Commit 46b84ad

Browse files
committed
ENH: Refactor parachute trigger function and improve controller observed variables retrieval
1 parent 73a9985 commit 46b84ad

4 files changed

Lines changed: 118 additions & 69 deletions

File tree

docs/examples/camoes_flight_sim.ipynb

Lines changed: 85 additions & 61 deletions
Large diffs are not rendered by default.

rocketpy/rocket/parachute.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,28 @@ def __evaluate_trigger_function(self, trigger):
298298
)
299299

300300
def triggerfunc(p, h, y, sensors, **kwargs):
301+
if not positional_param_count and not accepts_var_positional:
302+
# New-style kwargs-only trigger: no positional args accepted.
303+
# Restore sensors (removed by trigger_event_wrapper) and
304+
# override pressure with the noisy value before forwarding.
305+
merged = {**kwargs, "pressure": p, "sensors": sensors}
306+
if accepts_var_keyword:
307+
return trigger(**merged)
308+
return trigger(
309+
**{k: v for k, v in merged.items() if k in keyword_only_names}
310+
)
311+
301312
positional_args = [p, h, y]
302313
if accepts_sensors_positional:
303314
positional_args.append(sensors)
304315

305316
if accepts_var_keyword:
306317
return trigger(*positional_args, **kwargs)
307318

308-
forwarded_kwargs = {
309-
key: value
310-
for key, value in kwargs.items()
311-
if key in keyword_only_names
312-
}
313-
return trigger(*positional_args, **forwarded_kwargs)
319+
return trigger(
320+
*positional_args,
321+
**{k: v for k, v in kwargs.items() if k in keyword_only_names},
322+
)
314323

315324
self.triggerfunc = triggerfunc
316325

rocketpy/rocket/rocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1877,7 +1877,7 @@ def add_air_brakes(
18771877
"instead. Support for this argument will be removed in v1.13.",
18781878
DeprecationWarning,
18791879
)
1880-
controller_context["observed_variables"] = initial_observed_variables.copy()
1880+
controller_context["observed_variables"] = initial_observed_variables
18811881

18821882
orig_controller = controller_function
18831883
signature = inspect.signature(orig_controller)

rocketpy/simulation/flight.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,23 @@ def from_dict(cls, data):
27232723
alternative="Access the desired variables via controller.log",
27242724
)
27252725
def get_controller_observed_variables(self):
2726-
return None
2726+
"""Retrieve the observed variables from each controller.
2727+
2728+
If there is only one controller, its log is returned directly. If
2729+
there are multiple controllers, a list of logs is returned.
2730+
2731+
Returns
2732+
-------
2733+
list
2734+
Controller log(s) containing the return values of each
2735+
controller function call.
2736+
"""
2737+
observed_variables = [controller.log for controller in self._controllers]
2738+
return (
2739+
observed_variables[0]
2740+
if len(observed_variables) == 1
2741+
else observed_variables
2742+
)
27272743

27282744
@deprecated(
27292745
reason="Prefer direct pair iteration (for example zip(seq, seq[1:]))",

0 commit comments

Comments
 (0)