Model and Simulate Ladder Diagrams in Simulink
Simulate and validate your ladder diagrams by modeling them in Simulink® PLC Coder™.
You can then simulate and generate code for the ladder diagram models from within the Simulink environment.
To create a ladder diagram, open the Simulink PLC Coder ladder diagram library. At the MATLAB® command line, enter:
The ladder library opens containing all the blocks required for building the ladder diagram in Simulink.
Create a blank Simulink model. You can drag appropriate blocks from the library to build your ladder logic model in Simulink. For each block, double-click the block to see the block parameters. Use the
helpmenu to view the block parameter description. For more information on the ladder diagram instructions and these blocks, refer to the LOGIX 5000 Controllers General Instructions Reference Manual. Go to https://literature.rockwellautomation.com/idc/groups/literature/documents/rm/1756-rm003_-en-p.pdf.
The Simulink PLC Coder ladder diagram library contains top-level ladder logic blocks such as PLCControllerSuite, PLC Controller, Task, Ladder Diagram Program, Ladder Diagram Subroutine, Ladder Diagram add-on instruction block (AOI), and AOI Runner. All these blocks are organization blocks (ladder diagram containers) that you cannot put on ladder diagram rungs. Other blocks from the library cannot be top-level ladder logic blocks for simulation.
The PLCControllerSuite block can hold controller tags that are visible for all ladder logic blocks in this controller and contain the Task block.
The PLCController block enables you to build ladder logic directly. All the tags in the controller level ladder diagram are controller tags (global variables or I/O symbols)
The Task block contains ladder diagram programs that are using the same sample time and priority.
Code generation for empty Task blocks is not supported. If a Task block is empty, the software does not issue warnings or errors during code generation, but the generated code produces errors in Rockwell Automation® IDEs.
The Ladder Diagram Program block enables you to build ladder logic directly. Program-level ladder diagrams can have program scope variables and can access controller tags if defined.
The Ladder Diagram Subroutine block enables you to create and define a named ladder routine. You can edit the logic implemented by the subroutine by clicking the
Routine Logicbutton under the block parameters menu of this block.
The Ladder Diagram Function Block (AOI) block enables you to create the ladder diagram function block. You can edit the parameters and specifications of this block by using the various options available in the block parameters menu of this block.
The AOI Runner block is a program block that can contain only one Ladder Diagram Function Block designed for add-on instruction (AOI) testing (test bench generation and verification).
Drag a PLCControllerSuite block into the blank model that you created. You can double-click each organizational unit to traverse to the lower-level ladder logic semantics and build your ladder diagram. This image shows the empty ladder logic diagram.
Use the XIC and OTE blocks from the library to construct a simple ladder diagram. Use the Add Rungs or Add 1 Rung buttons in the ladder logic semantic to add a new rung. All added blocks must be on the rung. Use the Junction block to merge rung branches.
Double-click each new block added to the rung and specify the tags. In ladder diagrams, tags (variables) represent all inputs, outputs, and internal memory. The tag can be a variable name or an expression, such as:
Variable Name: Start, Stop, Switch
Bit Access: MyInt.0, MyInt.31
Array Element: A, B[2,3], C[idx], D[i, j]. Use of braces for indexing is not allowed in a tag expression. For example, A(2) is illegal.
Structure: A.B, C.D, E.F.G
Expressions: A.B > C.D; A+B.C
To change the attributes of the tag, open the Program Variables table within the Ladder Diagram Program block. The tags can have attributes such as Data Type, Initial Value, and Size. You can delete the unused variables in the variable table by selecting the Delete option. Select Apply for the changes to take effect. Go to the Controller Level block and double-click the Controller Tags table to specify the global variable and I/O symbol attributes.
To add rung comments to your model in Simulink, create a connected annotation (see
Motor Control Logicin image) to the rung terminal block. See Associate Annotations with Blocks and Areas.
Ctrl+Dto update the ladder logic model to reflect changes. You have now created a simple ladder model in Simulink.
Ladder Model Simulation
To perform ladder diagram simulation in Simulink, you must connect input and output blocks to the ladder model that match the actions that the ladder diagram inputs and outputs perform.
For simulation, to enable animation, use the
plcladderoptionfunction. At the MATLAB command line, enter:
Connect input and output ports to the PLCControllerSuite block to provide inputs for simulation and to read the outputs. You must modify the attributes of the switch and motor tags. To change the attributes of the tag, open the Program Variables table within the Ladder Diagram Program block and set them to the values shown.
Go to the Controller Level block and double-click the Controller Tags table to specify the global variable and I/O symbol attributes.
The software adds input and output ports to the PLCControllerSuite block. You can use Simulink blocks to add inputs to the ladder model. For example, you can use the Constant block to add Boolean inputs to mimic switch behavior.
Navigate to the Ladder Diagram Program block of the ladder model and click Step Forward to step through the simulation. The software uses the inputs provided, runs a behavioral simulation, and animates the ladder rungs and blocks based on the execution state.
You can continue stepping forward or run a continuous simulation until the simulation stop time.