Skip to content

Commit 080e316

Browse files
committed
ENH: first structure
1 parent df52c15 commit 080e316

2 files changed

Lines changed: 118 additions & 6 deletions

File tree

rocketpy/simulation/events.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
class Event:
2+
def __init__(self, trigger, action, name):
3+
"""Initializes an Event object.
4+
5+
Parameters
6+
----------
7+
trigger : function
8+
A function that takes the current state of the simulation as input and returns a boolean value. The event will be triggered when this function returns True.
9+
action : function
10+
A function that takes the current state of the simulation as input and performs some action when the event is triggered.
11+
name : str
12+
A name for the event, used for identification purposes.
13+
14+
"""
15+
self.trigger = trigger
16+
self.action = action
17+
self.name = name
18+
19+
# TODO: check_trigger does note receive enough arguments to substitute parachute events
20+
def check_trigger(self, state):
21+
"""Checks if the event should be triggered based on the current state of the simulation.
22+
23+
Parameters
24+
----------
25+
state : dict
26+
The current state of the simulation, containing information such as altitude, velocity, and time.
27+
28+
Returns
29+
-------
30+
bool
31+
True if the event should be triggered, False otherwise.
32+
33+
"""
34+
return self.trigger(state)
35+
36+
def execute_action(self, state):
37+
"""Executes the action associated with the event.
38+
39+
Parameters
40+
----------
41+
state : dict
42+
The current state of the simulation, containing information such as altitude, velocity, and time.
43+
44+
"""
45+
return self.action(state)
46+
47+
def __repr__(self):
48+
# TODO: Implement a more informative string representation of the Event object.
49+
pass
50+
51+
def __str__(self):
52+
# TODO: Implement a more informative string representation of the Event object.
53+
pass
54+
55+
def __call__(self, *args, **kwds):
56+
# TODO: This should call the action (or the trigger?)
57+
pass
58+
59+
60+
# TODO: Implement functions which are standard types of events, such as motor burnout events, landing events, etc.
61+
# def motor_burnout_trigger(state):
62+
# """A trigger function that returns
63+
64+
# # TODO: think about this
65+
# class ParachuteEvent(Event):
66+
67+
68+
# def __init__(self, trigger, action, name, parachute):
69+
# """Initializes a ParachuteEvent object.
70+
71+
# Parameters
72+
# ----------
73+
# trigger : function
74+
# A function that takes the current state of the simulation as input and returns a boolean value. The event will be triggered when this function returns True.
75+
# action : function
76+
# A function that takes the current state of the simulation as input and performs some action when the event is triggered.
77+
# name : str
78+
# A name for the event, used for identification purposes.
79+
# parachute : Parachute
80+
# The parachute associated with this event.
81+
82+
# """
83+
# super().__init__(trigger, action, name)
84+
# self.parachute = parachute
85+
86+
# def apogee_trigger(state):
87+
# """A trigger function that returns True when the rocket reaches apogee.
88+
89+
# Parameters
90+
# ----------
91+
# state : dict
92+
# The current state of the simulation, containing information such as altitude, velocity, and time.
93+
94+
# Returns
95+
# -------
96+
# bool
97+
# True if the rocket has reached apogee, False otherwise.
98+
99+
# """
100+
# return state['velocity'] <= 0

rocketpy/simulation/flight.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
quaternions_to_precession,
2727
quaternions_to_spin,
2828
)
29+
from .events import Event
2930

3031
ODE_SOLVER_MAP = {
3132
"RK23": RK23,
@@ -617,6 +618,19 @@ def __init__( # pylint: disable=too-many-arguments,too-many-statements
617618
self.simulation_mode = simulation_mode
618619
self.ode_solver = ode_solver
619620

621+
# Events
622+
def out_of_rail_trigger(state):
623+
return (
624+
state[0] ** 2 + state[1] ** 2 + (state[2] - self.env.elevation) ** 2
625+
>= self.effective_1rl**2
626+
)
627+
628+
self.out_of_rail_event = Event(
629+
trigger=out_of_rail_trigger,
630+
action=self.__handle_out_of_rail_event,
631+
name="Out of Rail Event",
632+
)
633+
620634
# Controller initialization
621635
self.__init_controllers()
622636

@@ -1014,14 +1028,12 @@ def __check_simulation_events(self, phase, phase_index, node_index):
10141028
bool
10151029
True if an event occurred and the simulation should break.
10161030
"""
1031+
# TODO: make all these 3 events be handled with the Events class
10171032
# Check for first out of rail event
1018-
if len(self.out_of_rail_state) == 1 and (
1019-
self.y_sol[0] ** 2
1020-
+ self.y_sol[1] ** 2
1021-
+ (self.y_sol[2] - self.env.elevation) ** 2
1022-
>= self.effective_1rl**2
1033+
if len(self.out_of_rail_state) == 1 and self.out_of_rail_event.trigger(
1034+
self.y_sol
10231035
):
1024-
return self.__handle_out_of_rail_event(phase, phase_index, node_index)
1036+
return self.out_of_rail_event.action(phase, phase_index, node_index)
10251037

10261038
# Check for apogee event
10271039
# TODO: negative vz doesn't really mean apogee. Improve this.

0 commit comments

Comments
 (0)