Skip to content

Real time control

Peter Corke edited this page Aug 6, 2024 · 2 revisions

One interesting use case for bdsim is for real-time control of physical processes using something like a RaspberryPi. This opens up interesting possibilities for low-cost controls education. This is only experimental at this stage.

User code changes

The idea is to use a different "run time" which is selected at line 3.

1  import bdsim
2
3  sim = bdsim.BDRealTime(debug='g', graphics=False)  # create real-time system
4  bd = sim.blockdiagram()  # create an empty block diagram
5
6  out = sim.run(bd, T=20)  # simulate for 20s

Everything else behaves the same, but when it comes to line 6 the block diagram is run without numerical integration. Instead the diagram is evaluated at every clock tick. The diagram must contain at least one clock and one discrete-time block such as a ZOH or DINTEGRATOR.

Under the hood

This is a soft real-time system, the run time uses time.sleep to pause until the next clock event. This mechanism avoids clock drift but not clock jitter.

To control a physical plant you will need to create blocks to perform the operating system and hardware specific i/o operations. These could be encoded as new blocks, modelled on the blocks/source.py and blocks/sinks.py.

Connecting to the physical world

A serial or network connected Arduino or RaspberryPi is an easy way to go. For Arduino the popular package Firmata is one option, or its successor Telemetrix. I've been experimenting with a simpler, but more control oriented, package called ArduIO.

Need to create I/O blocks to support these, perhaps one input block whose argument is Firmata|Telemetrix|arduIO or different block libraries: from arduio import Input, Output