-
Notifications
You must be signed in to change notification settings - Fork 34
Home
This Python package enables modelling and simulation of continuous-time, discrete-time or hybrid dynamic systems. Systems are conceptualized in block diagram form, but represented in terms of Python class and method calls. Unlike Simulink or LabView we write Python code rather than drawing boxes and wires. Wires can communicate any Python type such as scalars, lists, dicts, NumPy arrays, objects, and functions.
We first sketch the dynamic system we want to simulate as a block diagram, for example this simple first-order system
which we can express concisely with bdsim
as (see bdsim/examples/eg1.py
1 #!/usr/bin/env python3
2
3 import bdsim.simulation as sim
4
5 bd = sim.Simulation()
6
7 # define the blocks
8 demand = bd.STEP(T=1, pos=(0,0), name='demand')
9 sum = bd.SUM('+-', pos=(1,0))
10 gain = bd.GAIN(10, pos=(1.5,0))
11 plant = bd.LTI_SISO(0.5, [2, 1], name='plant', pos=(3,0))
12 scope = bd.SCOPE(styles=['k', 'r--'], pos=(4,0))
13
14 # connect the blocks
15 bd.connect(demand, sum[0], scope[1])
16 bd.connect(plant, sum[1])
17 bd.connect(sum, gain)
18 bd.connect(gain, plant)
19 bd.connect(plant, scope[0])
20
21 bd.compile() # check the diagram
22 bd.report() # list all blocks and wires
23
24 out = sim.run(bd, 5) # simulate for 5s
26
27 bd.done()
which is just 16 executable lines of code.
The red block annotations in the diagram are the names of blocks, and have become names of instances of object that represent those blocks. The blocks can also have names which are used in diagnostics and as labels in plots.
In bdsim
all wires are point to point, a one-to-many connection is implemented by many wires.
Ports are designated using Python indexing and slicing notation, for example sum[0]
. Whether it is an input or output port depends on context. Blocks are connected by connect(from, to_1, to_2, ...)
so an index on the first argument refers to an output port, while on the second (or subsequent) arguments refers to an input port. If a port has only a single port then no index is required.
The simulation results are in a container object
>>> out
results:
t | ndarray (67,)
x | ndarray (67, 1)
xnames | list
which contains an array of time values, an array of state values, and a list of the names of the state variables.
More details on this Wiki about:
Copyright (c) Peter Corke 2020-23
- Home
- FAQ
- Changes
- Adding blocks
- Block path
- Connecting blocks
- Subsystems
- Compiling
- Running
- Runtime options
- Discrete-time blocks
- Figures
- Real time control
- PID control
- Coding patterns