Integration into TwinCAT (CX9020)
This example explains how to write a simple PLC program for SMI in TwinCAT and how to link it with the hardware. The task is to control an SMI motor and to change the direction of rotation with a button.
Hardware
Setting up the components
The following hardware is required:
- 1x Embedded PC CX9020
- 1x digital 2-channel input terminal KL1002 (for the step up/down function)
- 1x SMI terminal KL6831
- 1x end terminal KL9010
Set up the hardware and the SMI components as described in the associated documentation.
This example assumes that an Up button was connected to the first KL1002 input and a Down button to the second, and that an SMI motor is connected to Address 1.
Software
Creation of the PLC program
Create a new PLC project for PC-based systems (ARM) and add the TcSMI.lib library.
Next, generate the following global variables:
VAR_GLOBAL
bUp AT %I* : BOOL;
bDown AT %I* : BOOL;
stInData AT %I* : ST_KL6831KL6841InData;
stOutData AT %Q* : ST_KL6831KL6841OutData;
stCommandBuffer : ST_SMICommandBuffer;
END_VAR
bUp : Input variable for the Up button.
bDown : Input variable for the Down button.
stInData : Input variable for the SMI terminal.
stOutData : Output variable for the SMI terminal.
stCommandBuffer : required for communication with SMI.
Then create a program (CFC) for background communication with SMI. The FB_KL6831KL6841Communication block is called in this program. Make sure to link the communication block with stInData, stOutData and stCommandBuffer.
Create a MAIN program (CFC) in which the blocks FB_SMIUpStep and FB_SMIDownStep can be called up. Connect the input bStart of the StepUp block with the global variable bUp and stCommandBuffer with the global variable stCommandBuffer. Proceed accordingly for the StepDown block.
Go to the task configuration and give the task a lower interval time. More detailed information can be found in the FB_KL6831KL6841Communication block description.
Load the project to the CX as the boot project and save it.
Configuration in the System Manager
Create a new System Manager project, select the CX as the target system, and search for the associated hardware.
Add the PLC program created above under PLC configuration. The two tasks are listed when the PLC project is expanded in the tree view. Expand the tasks – all global input and output variables should be allocated to the standard task. However, since the variables stInData and stOutData are to be processed faster, move them to the background communication task via drag & drop.
Now link the global variables of the PLC program with the Bus Terminal inputs and outputs, create the allocations, and activate the configuration. Then start the device in run mode.
Your CX is now ready for use.
By pressing one of the direction buttons the SMI motor selects the direction for the angle specified under uiAngle.