You can create user-defined instructions for your ladder models by using the Custom Instruction block. You can store
these blocks containing custom instructions in a user-defined library named
plcuserlib.slx
. You can also import, simulate, and export your ladder
instructions by using your custom blocks.
To create a user-defined instruction, use a Custom Instruction block added to the Simulink® PLC Coder™ Ladder Library.
To open the Ladder Library, at the MATLAB® command line, enter:
plcladderlib
The Ladder Library opens all the blocks required for building the Ladder Diagram in Simulink.
To create a new Simulink library, in the Library tab click New > Library. From the Simulink start page, select Blank Library and click Create Library.
Drag a Custom Instruction block from the Ladder Library to the new library that you created.
To build your own ladder logic model, double-click your Custom Instruction block to see the block parameters. Use the Help menu to view their descriptions.
In Instruction Name text field, give a name to your instruction. Specify the inputs and outputs required for your instruction block. Click Apply, and then click OK.
To look inside the mask, click in the Custom Instruction block. The blocks
inside the mask enable the instruction to simulate with other PLC Ladder instructions. The
user-defined logic is included in the Instruction_Enable block.
Save the library as plcuserlib.slx
. You can add multiple instruction
blocks to this library.
This example shows how to calculate square root of an input signal by using a Custom Instruction block.
To open the Simulink Start Page, on the MATLAB Home tab, click Simulink.
Select Blank Library and click Create Library.
Save the library as plcuserlib.slx
to a folder on the MATLAB path.
To open the PLC Ladder Library, at the MATLAB command line, enter:
plcladderlib
Drag the Custom Instruction
block from plcladderlib
to your user-defined library
plcuserlib.slx
.
Double-click the Custom Instruction block to open the Block Parameters.
Specify the Instruction Name as SQR
. Check that
the Number of Inputs is 1
and Input
Types is specified as a cell array of allowed data types. Similarly, check that
the Number of Outputs is 1
and Output
Types is specified as a cell array of allowed data types. Click
OK.
Click in the SQR block and double-click the
Instruction_Enable subsystem.
Inside the Instruction_Enable subsystem, add a Sqrt block
from the Simulink / Math Operations Library. Double-click this block and select
signedSqrt
from
Main>Function, and then click
OK.
Connect the input and output ports to the input and output ports of Sqrt block by using Data Type Conversion blocks.
Navigate to the top level of the library. Click Lock Links and
Unlock Library in the Library tab, and then save
the library. Simulink
PLC Coder can now use the SQR instruction when
plcuserlib.slx
is on the MATLAB path. You can drag this instruction to your models from the library that you
have created and saved.
To verify if Simulink PLC Coder has identified the newly created instruction, at the MATLAB command line, enter:
plcladderinstructions
The example in the image shows the use of the SQR instruction inside an Add-On Instruction block.
The Custom Instruction block does not support instructions:
With data type array
and struct
(composite) as
arguments.
That require internal data storage (states).
Custom Instruction | plcimportladder
| plcladderinstructions
| plcladderlib