From fa430f17e783262154bf7d8153639dbbb6dbe021 Mon Sep 17 00:00:00 2001 From: Stefan Ecklebe Date: Mon, 21 Oct 2024 17:54:22 +0200 Subject: [PATCH] Fix problem with disturbance module if no sensor is present --- pymoskito/examples/car/model.py | 2 +- pymoskito/examples/tanksystem/model.py | 2 +- pymoskito/generic_simulation_modules.py | 6 +++--- pymoskito/simulation_modules.py | 9 ++++++--- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pymoskito/examples/car/model.py b/pymoskito/examples/car/model.py index 6ac767fc..3f7655fa 100644 --- a/pymoskito/examples/car/model.py +++ b/pymoskito/examples/car/model.py @@ -46,7 +46,7 @@ def state_function(self, t, x, args): """ x1, x2, x3, x4, x5 = x - v, w = 1, 1 + v, w = args[0] return np.array([v * np.cos(x3), v * np.sin(x3), diff --git a/pymoskito/examples/tanksystem/model.py b/pymoskito/examples/tanksystem/model.py index f917f9bf..775fd97d 100644 --- a/pymoskito/examples/tanksystem/model.py +++ b/pymoskito/examples/tanksystem/model.py @@ -79,7 +79,7 @@ def event_level_2_high(self, t, x): return x[1] - self.settings["hT"] def calc_output(self, input_vector): - return [input_vector[0], input_vector[1] + np.random.random(1)[0] / 100] + return input_vector pm.register_simulation_module(pm.Model, TwoTankSystem) diff --git a/pymoskito/generic_simulation_modules.py b/pymoskito/generic_simulation_modules.py index 4cdb9db4..ead3d536 100644 --- a/pymoskito/generic_simulation_modules.py +++ b/pymoskito/generic_simulation_modules.py @@ -627,10 +627,10 @@ class GaussianNoise(Disturbance): ("mean", 0)]) def __init__(self, settings): - settings.update([("input signal", "Sensor")]) + settings.update([("input signal", "Model_Output")]) Disturbance.__init__(self, settings) - def _disturb(self, value): + def _disturb(self, t, signal): return np.random.normal(self._settings['mean'], self._settings['sigma'], - value.output_dim) + signal.shape) diff --git a/pymoskito/simulation_modules.py b/pymoskito/simulation_modules.py index 773f3d46..ed6c63ea 100644 --- a/pymoskito/simulation_modules.py +++ b/pymoskito/simulation_modules.py @@ -593,10 +593,12 @@ def __init__(self, settings): SimulationModule.__init__(self, settings) def calc_output(self, input_dict): - return self._disturb(input_dict[self._settings["input signal"]]) + return self._disturb( + input_dict["time"], + input_dict.get(self._settings["input signal"], None)) @abstractmethod - def _disturb(self, value): + def _disturb(self, t, signal): """ Placeholder for disturbance calculations. @@ -604,7 +606,8 @@ def _disturb(self, value): to create the noise. Args: - value (array-like float): Values from the source selected by the + t (float): Current simulation time. + signal (array-like float): Values from the source selected by the ``input_signal`` property. Returns: array-like float: Noise that will be mixed with a signal later on.