Main Content

Model and Simulate Ladder Diagrams in Simulink

Simulate and validate your ladder diagrams by modeling them in金宝app®PLC Coder™.

You can then simulate and generate code for the ladder diagram models from within the Simulink environment.

  1. To create a ladder diagram, open theSimulink PLC Coderladder diagram library. At the MATLAB®command line, enter:

    plcladderlib

    The ladder library opens containing all the blocks required for building the ladder diagram in Simulink.

  2. 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 thehelpmenu 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 tohttps://literature.rockwellautomation.com/idc/groups/literature/documents/rm/1756-rm003_-en-p.pdf.

  3. TheSimulink PLC Coderladder diagram library contains top-level ladder logic blocks such asPLCControllerSuite,PLC Controller,Task,Ladder Diagram Program,Ladder Diagram Subroutine,Ladder Diagram add-on instruction block (AOI), andAOI 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.

    • ThePLCControllerSuiteblock can hold controller tags that are visible for all ladder logic blocks in this controller and contain theTaskblock.

    • ThePLCControllerblock 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)

    • TheTaskblock contains ladder diagram programs that are using the same sample time and priority.

      Code generation for emptyTaskblocks is not supported. If aTaskblock is empty, the software does not issue warnings or errors during code generation, but the generated code produces errors in Rockwell Automation®ide。

    • TheLadder Diagram Programblock enables you to build ladder logic directly. Program-level ladder diagrams can have program scope variables and can access controller tags if defined.

    • TheLadder Diagram Subroutineblock enables you to create and define a named ladder routine. You can edit the logic implemented by the subroutine by clicking theRoutine Logicbutton under the block parameters menu of this block.

    • TheLadder 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.

    • TheAOI Runnerblock 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).

  4. Drag aPLCControllerSuiteblock 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.

  5. 使用theXICandOTEblocks from the library to construct a simple ladder diagram. Use theAdd RungsorAdd 1 Rungbuttons in the ladder logic semantic to add a new rung. All added blocks must be on the rung. Use theJunctionblock to merge rung branches.

  6. 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

    • 数组元素:[1],[2、3],C [idx], D (i, j)。使用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

    • Mixture: A[1].B[i,j].C[3].D

    • Expressions: A[3].B > C.D; A[3]+B[4].C

  7. To change the attributes of the tag, open theProgram Variablestable within theLadder Diagram Programblock. 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删除option. SelectApplyfor the changes to take effect. Go to the Controller Level block and double-click theController Tagstable to specify the global variable and I/O symbol attributes.

  8. To add rung comments to your model in Simulink, create a connected annotation (seeMotor Control Logicin image) to the rung terminal block. SeeAssociate Annotations with Blocks and Areas.

  9. 使用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.

  1. For simulation, to enable animation, use theplcladderoptionfunction. At the MATLAB command line, enter:

    plcladderoption('simpleController','Animation','on')

  2. Connect input and output ports to thePLCControllerSuiteblock 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 theProgram Variablestable within theLadder Diagram Programblock and set them to the values shown.

  3. Go to the Controller Level block and double-click theController Tagstable to specify the global variable and I/O symbol attributes.

  4. The software adds input and output ports to thePLCControllerSuiteblock. You can use Simulink blocks to add inputs to the ladder model. For example, you can use theConstantblock to add Boolean inputs to mimic switch behavior.

  5. Navigate to theLadder Diagram Programblock of the ladder model and clickStep Forwardto 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.

  6. You can continue stepping forward or run a continuous simulation until the simulation stop time.

See Also

||||||

Related Topics