LBL logo

Buildings.Utilities.IO.Python27.Examples

Collection of models that illustrate model use and test models

Information

This package contains examples for the use of models that can be found in Buildings.Utilities.IO.Python27.

Extends from Modelica.Icons.ExamplesPackage (Icon for packages containing runnable examples).

Package Content

NameDescription
Buildings.Utilities.IO.Python27.Examples.KalmanFilter KalmanFilter Kalman filter implemented in Python and called from Modelica

Buildings.Utilities.IO.Python27.Examples.KalmanFilter Buildings.Utilities.IO.Python27.Examples.KalmanFilter

Kalman filter implemented in Python and called from Modelica

Buildings.Utilities.IO.Python27.Examples.KalmanFilter

Information

This example demonstrates the implementation of a Kalman filter in Python. The model generates a uniform random number, which is computed in the Python file KalmanFilter.py by the function random(seed). This random number is added to a sine wave and then sent to the function filter(u) in the above Python file. The function filter(u) implements a Kalman filter that estimates and returns the state. The function saves its temporary variables to a file called tmp-kalman.pkl.

When simulating this model, the figure below will be generated which shows the sine wave, the sine wave plus noise, which is input to the Kalman filter, and the estimated state which is the output of the Kalman filter.

image

Implementation

The code is based on http://www.scipy.org/Cookbook/KalmanFiltering.

Extends from Modelica.Icons.Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
TimesamplePeriod0.001Sample period of component [s]

Modelica definition

model KalmanFilter 
  "Kalman filter implemented in Python and called from Modelica"
  extends Modelica.Icons.Example;
  Real_Real ran(
    nDblWri=1,
    nDblRea=1,
    functionName="random",
    moduleName="KalmanFilter",
    samplePeriod=samplePeriod) "Generate a random number in Python";
  Modelica.Blocks.Sources.Clock clock;
  Real_Real kalFil(
    moduleName="KalmanFilter",
    functionName="filter",
    nDblWri=1,
    nDblRea=1,
    samplePeriod=samplePeriod) "Kalman filter in Python";

  parameter Modelica.SIunits.Time samplePeriod=0.001 
    "Sample period of component";
  Modelica.Blocks.Sources.Sine sine(freqHz=1) "Sine wave";
  Modelica.Blocks.Math.Add add;
equation 
 // Delete the temporary file generated by the Python file
 // at the start and end of the simulation.
 when {initial(), terminal()} then
  Modelica.Utilities.Files.removeFile("tmp-kalman.pkl");
end when;

  connect(clock.y, ran.uR[1]);

  connect(add.y, kalFil.uR[1]);
  connect(ran.yR[1], add.u2);
  connect(sine.y, add.u1);
end KalmanFilter;

Automatically generated Thu Oct 24 15:13:43 2013.