mergedemo.mdl | Open this Model |
Simulink Merge Block Demonstration
This interactive demonstration introduces you to the following Simulink concepts.
If you have not opened or have made changes to the Merge Block
Demonstration Simulink model, please reinitialize the
Simulink model mergedemo.mdl.
The left side of this model contains two blocks, for example the Repeating Sequence block, from the Simulink Sources library. Each block generates a scalar output signal of the form displayed on the block's icon. These two signals are fed into different Enabled Subsystems, which are outlined with thick lines. The enabled (or conditionally executed) subsystems are controlled by the signal generated by a Discrete Pulse Generator block. This block's output toggles between the values of zero and one every five seconds. When the output of the Discrete Pulse Generator block is positive, the top enabled subsystem is simulating. When the output of the Discrete Pulse Generator block is zero, the bottom enabled subsystem is simulating. To force the bottom enabled subsystem to simulate when the Discrete Pulse Generator block's output is zero, the output is passed through a Logical Operator block set to the NOT operator. The NOT block outputs a value of one when its input is zero.
The outputs of the two enabled subsystems are fed into a Merge block. The output of the Merge block is, finally, displayed on a Simulink Scope.
You can run the simulation to see the output of the Merge block on the Scope. Notice that the type of waveform shown on the Scope changes every five seconds. This is due to the behavior of the Merge block.
The Merge block demonstration contains two conditionally executed subsystems. A conditionally executed subsystem is a subsystem that is only executed at particular times during the simulation, i.e., when certain conditions are met. In this case, the subsystems are enabled subsystems, distinguished by their icons and the additional input port on the top or bottom of the block, as shown below.
The signal feeding into the additional port is called the control signal. The value of this signal provides the condition that determines whether the subsystem is executed, or not, at any given time step. Enabled subsystems are executed when the control signal is strictly positive.
In this demonstration, you want to execute one of the enabled subsystems when the control signal is positive, and the other when the control signal is zero. You can do this by using the Logical Operator block in Simulink.
The Logical Operator block in Simulink gives you a method for incorporating logical operators and boolean signals into your Simulink diagram. You can open the Logical Operator block to see the different operators that are listed in the drop down menu. The current setting of NOT returns 1 (or TRUE) when the input signal is non-zero, or 0 (FALSE) when the input signal is strictly zero.
In this example, you can use this feature to generate a positive value when the control signal goes to zero by maintaining the current operator setting of NOT and closing the dialog.
Note: Even though you are using a Logical Operator block, the output of the block is not a boolean in Simulink. By default, all signals in Simulink are of the data type double. To see this, you can display the data type of each signal by checking the Port data types command in the Simulink diagram's Format menu.
You can force Simulink to use boolean values for the inputs and outputs of the Logical Operator block, using the following steps.
If you run the simulation, you'll see an error indicating that the Logical Operator block expects a boolean input signal. While the error dialog is open, notice that the input and output data type of the NOT block is labeled boolean on the Simulink diagram.
You can use the following steps to remedy this error.
You can use the Merge block to create a single signal whose value is equal to the output of whichever enabled subsystem is currently executing. The following steps.
As before, when you run the simulation, the output of the Merge block is the value of whatever conditionally executed subsystem is executing at any point in time.
When you ran the simulation, you should have noticed that the color of the currently executing conditionally executing subsystems changed. This does not happen automatically for all conditionally executing subsystems. It was achieved using an S-function!
If you open one of the enabled subsystems by double clicking on the block in the Simulink diagram, you'll see it contains the following.
At each time step, the S-function's mdlUpdate subfunction executes. It sets the BackgroundColor property of the Subsystems based on whether it is currently executing, or not, with the following command.
For more information on how to write M-file S-functions, see the documentation.
Close the Merge block demonstration Simulink diagram.