Tank Control Simulation and Code Generation Using Ladder Logic

This example shows how to do ladder logic simulation and code generation from ladder tank controller model.

Step 1: Create plcdemo_ladder_tankcontrol example model

To start, create a new folder with write permission, copy the files plcdemo_ladder_tankcontrol_template.slx and TankControl.L5X into the new folder.

Step 2: Create plcdemo_ladder_tankcontrol example model

Change the current folder to the new folder, rename plcdemo_ladder_tankcontrol_template.slx to plcdemo_ladder_tankcontrol.slx.

Step 3: Import ladder file and generate ladder controller model

Run plcimportladder command in MATLAB:

plcimportladder TankControl TopAOI TankControl

Step 4: Copy tank controller into plcdemo_ladder_tankcontrol model

Open the generated model TankControl_runner_TankControl.slx, select and copy the TankControl_runner block. Open plcdemo_ladder_tankcontrol, replace Controller/TankControl_runner with the copied block

Step 5: Run simulation

Click Run button to start simulation. Open Tank HMI and use the rotary switch to set controller command input: * Set switch to 'Fill' position to fill tank; * Set switch to 'Hold' position to hold current tank state; * Set switch to 'Empty' position to empty tank; * Set switch to 'Stir' position to activate tank stir state. Note: Tank will enter 'STIR' state only when fluid level is full; otherwise, 'Stir' command has no effect. If tank is in 'STIR' state, the STIR indicator lamp will be on, otherwise off. The numerical value of tank command is the following: 'Fill' - 0 'Hold' - 1 'Empty' - 2 'Stir' -3

Step 6: Ladder code and testbench generation

Open ladder tank control testbench model: