Skip to content

Watching a simulation variable

Peter Corke edited this page Aug 20, 2026 · 2 revisions

After a bdsim model has been created you may want to monitor some additional signals and have them appear in the simulation results. In bdsim we want to watch the signal, or put the signal into the watch list. Watched signals appear in the simulation results structure as a column of .y, and the name of the signal is the corresponding element of the list .ynames which is a list of the repr() generated output port names.

Note

Early versions of bdsim put the watch variables in different fields, eg. .y0, .y1, .y2 etc. This is difficult to index programmatically, so the this notation is now deprecated.

There are a few different ways we watch a signal.

At simulation time

We simply set the watch parameter to sim.run(). No modification to the model is required. For the program eg1.py

# define the blocks
demand = bd.STEP(T=1, name="demand")
sum = bd.SUM("+-")
gain = bd.GAIN(10)
plant = bd.LTI_SISO(0.5, [2, 1], name="myplant")
scope = bd.SCOPE(styles=["k", "r--"], loc="lower right")

# connect the blocks
bd.connect(demand, sum[0], scope[1])
bd.connect(plant, sum[1])
bd.connect(sum, gain)
bd.connect(gain, plant)
bd.connect(plant, scope[0])

bd.compile()  # check the diagram

out = sim.run(bd, watch=[gain, plant[0], "myplant[0]")  # <==== watch list set

The watch argument is a list of one or more signals whose value during simulation will be recorded. Each element can be:

  • a Block reference, interpreted as output port 0. eg. gain in the example above
  • a Plug reference (block with port index) eg. plan[0] in the example above
  • a string of the form "blockname[i]" — output port i of the named block. eg. "myplant[0]" in the example above. This is particularly useful for watching a signal that is embedded deep in some nested SUBSYSTEM block for which you do not have an object reference. Signals in these nested blocks all have unique names which can be seen in any report about the system.

Warning

We can only watch block outputs, not block inputs.

Scope inputs

Adding the watch option to any graphics block will cause its inputs to be watched. The time series data will appear in the simulation results, not just on screen in a scope.

scope = bd.SCOPE(styles=["k", "r--"], loc="lower right", watch=True)

WATCH block

Any signal that is input to WATCH block is added to the watch list. The block itself does nothing during simulation.

Clone this wiki locally