Skip to content

Commit 3c28452

Browse files
author
Malmahrouqi3
committed
BUG: fixed cont ctrl calls integrated into the ODE solver
1 parent 31d80b9 commit 3c28452

3 files changed

Lines changed: 35 additions & 28 deletions

File tree

rocketpy/control/controller.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from inspect import signature
23
from typing import Iterable
34

@@ -17,7 +18,7 @@ def __init__(
1718
self,
1819
interactive_objects,
1920
controller_function,
20-
sampling_rate,
21+
sampling_rate=math.inf,
2122
initial_observed_variables=None,
2223
name="Controller",
2324
):
@@ -72,7 +73,8 @@ def __init__(
7273
relevant information in the `observed_variables` list.
7374
7475
.. note:: The function will be called according to the sampling rate
75-
specified.
76+
specified. If unspecified, the default sampling rate is set to infinity, meaning that the
77+
controller function will be called at every step of the simulation.
7678
sampling_rate : float
7779
The sampling rate of the controller function in Hertz (Hz). This
7880
means that the controller function will be called every

rocketpy/rocket/rocket.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,42 +1952,38 @@ def add_thrust_eccentricity(self, x, y):
19521952
self.thrust_eccentricity_y = y
19531953
return self
19541954

1955-
def add_discrete_controller(
1956-
self,
1957-
controller_function,
1958-
refresh_rate,
1959-
interactive_objects=None,
1960-
initial_observed_variables=None,
1961-
name=None,
1962-
):
1955+
def add_discrete_controller(self,
1956+
controller_function,
1957+
refresh_rate,
1958+
interactive_objects=None,
1959+
initial_observed_variables=None,
1960+
name=None
1961+
):
19631962

19641963
controller = _Controller(
1965-
controller_function=controller_function,
1966-
sampling_rate=refresh_rate,
1967-
interactive_objects=interactive_objects,
1968-
initial_observed_variables=initial_observed_variables,
1969-
name=name,
1970-
)
1964+
controller_function=controller_function,
1965+
sampling_rate=refresh_rate,
1966+
interactive_objects=interactive_objects,
1967+
initial_observed_variables=initial_observed_variables,
1968+
name=name)
19711969

19721970
self._add_controllers(controller)
19731971

1974-
return self
1972+
return None
19751973

1976-
def add_continuous_controller(
1977-
self,
1978-
controller_function,
1979-
interactive_objects=None,
1980-
initial_observed_variables=None,
1981-
name=None,
1982-
):
1974+
def add_continuous_controller(self,
1975+
controller_function,
1976+
interactive_objects=None,
1977+
initial_observed_variables=None,
1978+
name=None
1979+
):
19831980

19841981
controller = _Controller(
19851982
controller_function=controller_function,
1986-
sampling_rate=np.inf,
1983+
sampling_rate=math.inf,
19871984
interactive_objects=interactive_objects,
19881985
initial_observed_variables=initial_observed_variables,
1989-
name=name,
1990-
)
1986+
name=name)
19911987

19921988
self._add_controllers(controller)
19931989
return controller

rocketpy/simulation/flight.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,14 @@ def __simulate(self, verbose):
786786
self.y_sol = phase.solver.y
787787
if verbose:
788788
print(f"Current Simulation Time: {self.t:3.4f} s", end="\r")
789-
789+
for controller in self._continuous_controllers:
790+
controller(
791+
self.t,
792+
self.y_sol,
793+
self.solution[:-1],
794+
self.sensors,
795+
self.env,
796+
)
790797
if self.__check_simulation_events(phase, phase_index, node_index):
791798
break # Stop if simulation termination event occurred
792799

@@ -1585,6 +1592,8 @@ def __init_equations_of_motion(self):
15851592
def __init_controllers(self):
15861593
"""Initialize controllers and sensors"""
15871594
self._controllers = self.rocket._controllers[:]
1595+
self._continuous_controllers = [c for c in self._controllers if math.isinf(c.sampling_rate)]
1596+
self._discrete_controllers = [c for c in self._controllers if not math.isinf(c.sampling_rate)]
15881597
self.sensors = self.rocket.sensors.get_components()
15891598

15901599
# reset controllable object to initial state (only airbrakes for now)

0 commit comments

Comments
 (0)