TwinCAT 3 Project Development Steps
Lamp Control
17/10/2014
This tutorial describes all necessary steps in the development of a PLC application for control of a standard ESO Lamp device. The same steps, with minor differences, are applicable for other ESO Standard PLC devices, e.g. Shutter devices.
Creating New Project
- Create new solution (File / New / Project…) and select TwinCAT Project / TwinCAT XAE Project (XML format) and give it a name (e.g. DemoLamp). Select the location where to save the solution and then press OK to continue.
- Right-click on PLC and select Add New Item…
- From Plc Templates select Standard PLC Project and give it a name (e.g. again DemoLamp). Press Add to continue.
- Under PLC/DemoLamp expand DemoLamp Project and then expand the References.
- Right-click on References and select Add library… On the pop-up dialog, under Company, select ESO. Expand (Miscellaneous) and then select the libraries to include. Every project requires the library called ESO TC3 Common Library. In addition, a specific library for the device has to be included, e.g. ESO TC3 Lamp Library. Include all libraries needed for the project (hold the Ctrl key and make multiple selections) and then press OK to continue. The libraries will appear under References.
- Save the project.
Connecting to Target PLC
- Click on the current Target on the Toolbar. The target can be <Local> or any other PLC target. Ensure that <Local> is selected . Click on <Local> and then select Choose Target System… and on the pop-up click on the Search (Ethernet) button.
- On the Add Route Dialog under Enter Host Name / IP input field enter the PLC name or its IP address. Make sure that the corresponding radio button selection (Host Name or IP Address) in the lower left corner corresponds to the type of data to be entered.
- Press Enter and wait until the list of hosts is displayed. Find the target PLC to be used in the list and select it . If there is no an X under the Connected column, press the Add Route button and wait until the X appears. Press the Close button to continue. Note, if an X is already there, just press the Close button to continue. Press OK again. The target PLC is now displayed on the Toolbar.
Discovering PLC I/O Terminals (Scan)
- Switch the PLC to Config Mode, i.e. click on the Restart TwinCAT (Config Mode) Icon . This can also be done from the top menu by clicking on TwinCAT and selecting Restart TwinCAT (Config Mode). Confirm the action with OK. After a couple of seconds the Run icon in the lower right corner of the screen should become .
- In the project tree, under I/O, right-click on Devices and select Scan. Confirm with OK.
- Under found devices select only Device 1 (EtherCAT) and Device 4 (NOV-DP-RAM). Press OK to continue.
- Press Yes when asked to Scan for boxes.
- If the PLC application involves motor control, select Yes when (and if) asked about the NC-Configuration. Otherwise select No.
- Select No when asked to Activate Free Run.
- Save the project.
Writing PLC Program
- In the project tree, under PLC / DemoLamp / DemoLamp Project / POUs, double-click on MAIN (PRG). The program MAIN will be open for editing.
- Under VAR add an instance of a lamp, e.g. “Lamp1: FB_LAMP;”. The correct FB name, i.e. FB_LAMP, can be found by pressing F2 (Input Selector) and then under Structured Types / ESO_TC3_Lamp_Library / POUs select FB_LAMP.
- Under the code part in the editor, write “Lamp1();” and save the project. The complete program looks like this:
- Under the Build pull-down menu, select Rebuild DemoLamp and ensure that there are no errors or warnings in the Output window.
Mapping of Program Variables to Physical I/Os
- Under DemoLamp Instance expand both PlcTask Inputs (yellow) and Outputs (red), as shown below. These attributes are available for linking/mapping to the physical I/Os. Depending on the hardware configuration, e.g. if there is intensity control or fault feedback signal, not all of them have to be mapped. Signals i_bOn (feedback status), i_nCouplerState (operational state of the system) and q_bSwitch (lamp control output signal) have to be mapped for every lamp.
- Under I/O / Devices / Device 1 (EtherCAT) expand the terminal whose I/O signal is to be linked to the program variable and right-click on it. For example, right-click on Term 8 (EL2008) / Channel 1 / Output and select Change Link… Select MAIN.Lamp1.q_bSwitch and press OK to continue. Do the same for all other links.
- Link MAIN.Lamp1.i_nCouplerState to the State of the coupler that hosts the I/O terminal (e.g. EK1100) or the State variables of any other I/O terminals. This link is mandatory and it is used as a check if the system is OPERATIONAL. If the link is not established, it will not be possible to initialize the device.
Instantiating Device GUI from the GUI Template
- Right-click on VISUs and select Add and then select Visualisation…
- Give the name, e.g. GUI and press OK.
- A new tab, called GUI will appear next to the MAIN tab.
- In the lower right corner of the screen click on Toolbox and then expand the Basic list. Drag and drop the Frame onto GUI.
- On the Frame Configuration popup, under Available Visualisations, select LAMP_GUI_TEMPLATE and then click on Add under Selected Visualisations. Click OK to complete.
- An instance of the Lamp GUI will appear. Click on the lower right corner of the inserted frame and enlarge it enough to cover the complete GUI.
- Click on the Properties tab in the lower right corner of the screen.
- Under Position, set X and Y values to zero.
- Under References / ESO_TC3_Lamp_Library.LAMP_GUI_TEMPLATE / m_DeviceName enter ‘Lamp1’ (note the single quotes!) and press enter.
- Under m_Instance enter MAIN.Lamp1 and press enter. The References now look like this:
- Save the project.
Activating Configuration and Logging in
- Activate the Configuration, i.e. click on the Activate Configuration Icon. This can also be done from the top menu by clicking on TwinCAT and selecting Activate Configuration.
- When asked, confirm the action with Yes or OK. Confirm that the system should be Restarted in Run Mode. After a couple of seconds the Run icon should appear in the lower right corner of the screen indicating the Run Mode.
- Click on the green Login icon to log into the PLC and automatically download the program. This can also be done from the top menu by clicking on PLC and selecting Login.
Controlling Device from the GUI
The figure on the left shows how the lamp GUI appears when it is just started and the lamp is not initialised yet. The one on the right shows the status when the lamp is in Local Control and ON.
- After Login is complete, switch to the GUI tab.
- Select Local Control to enable buttons and entry fields on the GUI.
- Press the INIT button to initialize the device. The LED next to it will become green once the device is initialized.
- Set Intensity, if used, and turn the lamp ON/OFF with the corresponding buttons. Monitor the status of the device.
- Enter some values for Warmup and Cooldown and repeat the control exercise.
- Save the project.
Configuring Operational Mode on PLC Boot and OPC UA Server
- Double-click on PLC / DemoLamp. In the DemoLamp window, under the Project tab, check the Autostart Boot Project check-box. With this setting the PLC will reboot in Run Mode.
- Under the Settings tab, under the Target Files section, check both TMC File and TPY File. These files are needed for the OPC UA Server.
- Save the project and then Activate Configuration.
- Confirm with OK and once again to Restart TwinCAT System in Run Mode.
- Login again.
Controlling Device from OPC UA Clients
- Start an OPC UA Client, e.g. UaExpert.
- Select the Target OPC UA Server and Connect.
- Find the instance of Lamp1 under Root/Objects/PLC1/MAIN.
- Expand Lamp1 in order to display cfg, ctrl, info and stat folders.
- Drag and drop all stat attributes to the Default DA View. Do the same for the ctrl attributes.
- Initialise the lamp by setting nCommand to 1 and then bExecute to TRUE (1).
- To turn the lamp ON, nCommand = 3 and bExecute = TRUE (1).
- To turn the lamp OFF, nCommand = 2 and bExecute = TRUE (1).
- Control and monitor the status of the device from both environments OPC UA Client and TwinCAT 3.