TwinCAT 3 Project Development Steps Motor Control 17/10/2014 This tutorial describes all necessary steps in the development of a PLC application for control of a standard ESO Motor device. Creating New Project 1. Create new solution (File / New / Project…) and select TwinCAT Project / TwinCAT XAE Project (XML format) and give it a name (e.g. DemoMotor). Select the location where to save the solution and then press OK to continue. 2. Right-click on PLC and select Add New Item… 3. From Plc Templates select Standard PLC Project and give it a name (e.g. again DemoMotor). Press Add to continue. 4. Under PLC/DemoMotor expand DemoMotor Project and then expand the References. 5. 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, in this case it is ESO TC3 Motor 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. 6. Save the project. Connecting to Target PLC 1. 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. 2. 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. 3. 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) 1. 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 lower right corner of the screen should become in the . 2. In the project tree, under I/O , right-click on Devices and select Scan. Confirm with OK. 3. Under found devices select only Device 1 (EtherCAT) and Device 4 (NOV-DP-RAM). Press OK to continue. 4. Press Yes when asked to Scan for boxes. 5. Select Yes when asked about the NC-Configuration. 6. Select No when asked to Activate Free Run. 7. Save the project. Importing Existing Axis (Motor) and Motor Control I/O Terminal configurations Motor configuration includes two parts: the motor I/O terminal configuration and the configuration of the NC Axis. The motor I/O terminal configuration specifies settings like feedback type, encoder resolution, nominal and reduce current, while the configuration of the NC Axis specifies terms like scale factor, in-position window, following error, motor dynamics, PID coefficients, etc. Normally, once a new motor is properly configured and tested, the two configurations would be exported and made available for the same motors used in the very same project of in some future projects. In this exercise, for safety reasons, we will use existing and already tested configurations. They are included in the config directory of the Motor_TC3 module in the SVN repository. The files are called: EL7041_CANRMC_Stepper.xti XML file with the configuration of the EL7041 terminal Axis_CANRMC_Stepper.xti XML file with the configuration of the demo motor axis Prior to importing these configurations, the existing ones will have to be removed. 1. Under MOTION / NC-Task 1 SAF / Axis right-click on Axis 1 and select Remove. 2. Right-click on Axis and select Add Existing Item... Navigate to the location of the file Axis_CANRMC_Stepper.xti and select it and then click on the Open button to exit. Select No when asked to Reconnect imported links. In this case, the name of the axis is OPTI1. Keep it as it is. 3. Under I/O / Devices / Device 1 (EtherCAT) locate the EL7041 terminal and right-click on it and then select Remove. 4. Right-click on the terminal that use to come after the EL7041 (e.g. EL9011) and select Insert Existing Item... Navigate to the location of the file EL7041_CANRMC_Stepper.xti and select it and then click on the Open button to exit. Select No when asked to Reconnect imported links. The newly inserted terminal will probably have a different index from the original one. Correct it (just for cosmetic reasons). 5. Save the project Writing PLC Program 1. In the project tree, under PLC / DemoMotor / DemoMotor Project / POUs, double-click on MAIN (PRG). The program MAIN will be open for editing. 2. Under VAR add an instance of a Motor, e.g. “OPTI1: FB_MOTOR;”. The correct FB name, i.e. FB_MOTOR, can be found by pressing F2 (Input Selector) and then under Structured Types / ESO_TC3_Motor_Library / POUs select FB_MOTOR. 3. Under the code part in the editor, write OPTI1(nNOVRAM_DevId:=4, nNOVRAM_Offset:=0); and save the project. The complete program looks like this: The two input parameters of OPTI1 define the usage of the non-volatile memory that permanently keeps last motor configuration. In this case the ID of the NOV-DP-RAM device is 4. It must be ensured that the two numbers match! Motor configuration takes about 360 bytes. This means that the offset for multiple motors in the system should be incremented at least by this value. It is recommended to use round values and increment offsets by 1000. For example: OPTI1(nNOVRAM_DevId:=4, nNOVRAM_Offset:=0); OPTI2(nNOVRAM_DevId:=4, nNOVRAM_Offset:=1000); OPTI3(nNOVRAM_DevId:=4, nNOVRAM_Offset:=2000); … 4. Under the Build pull-down menu, select Rebuild DemoMotor and ensure that there are no errors or warnings in the Output window. 5. Save the project. Mapping of Program Variables to Physical I/Os In this section we link the program variables with the physical I/O signals. In the case of motion control applications there are two groups of variables to be mapped: Firstly, the Axis (i.e. OPTI1) has to be linked to the EL7041 terminal as well as to the Axis variable of the OPTI1 instance in the PLC program (i.e. MAIN.OPTI1.Axis). Secondly, the switches have to be linked to the corresponding digital input signals. Any digital input signal can be used for this purpose. It is only the matter of how the switches are wired. The EL7041 terminal has two digital input signals that we will use for the Lower and Upper HW limits respectively. The Z-pulse of the EL7041 terminal is used for high precision homing. The expanded view of motor input and output variables is shown below. In this exercise we will use only lower and upper limit switches (bLHW and bUHW) and the Index switch (Z-pulse). Brakes are not used. 1. Double-click on the OPTI1 Axis and select the Settings Tab. Click on the Link To I/O button and select Stepper Drive (MDP703) connected to EL7041. Press OK to complete. 2. Click on the Link To PLC button and select MAIN.OPTI1.Axis. Press OK to complete. We have completed linking of the axis. In the following step we will map the limit switches. 3. Under DemoMotor Instance expand both PlcTask Inputs (yellow) and Outputs (red). Expand the switches. 4. Right-click on bLHW and select Change Link… In the popup, under Show Variables, select Used and unused . Some variables get linked multiple times and if the Unused option is selected they would not be visible. Scroll down to EL7041 and below it find and select Digital input 1. Press OK to exit. Repeat the same procedure for bUHW that is connected to Digital input 2. 5. Right-click on bINDEX. Scroll down and find Status of input C (Z-pulse) under the EL7041 module. Select it and press OK. 6. Save the project. 7. Activate the configuration and then confirm switching to Run Mode. Configuring Operational Mode on PLC Boot and OPC UA Server 1. Double-click on PLC / DemoMotor. In the DemoMotor window, under the Project tab, check the Autostart Boot Project check-box. With this setting the PLC will reboot in Run Mode. 2. 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. 3. Save the project and then Activate Configuration . 4. Confirm with OK and once again to Restart TwinCAT System in Run Mode. 5. Login again. Instantiating Device GUIs from the GUI Templates ESO TC3 Motor Library provides two GUI templates, one for the control and the other one for the configuration of motors. Instances of both GUIs should be created. We will firstly start with the control GUI and name it GUI, and then create a Configuration GUI called GUI_Config. The process is identical for both GUIs. The GUIs are shown below. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 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 GUI_MOTOR_CONTROL_TEMPLATE and then click on Add under Selected Visualisations. Click OK to complete. An instance of the Motor GUI will appear. Click on the Properties tab in the lower right corner of the screen. Under Position, set X and Y values to zero. Click on the lower right corner of the inserted frame and enlarge it enough to cover the complete GUI. Under References / ESO_TC3_Motor_Library. GUI_MOTOR_CONTROL_TEMPLATE / m_DeviceName enter ‘OPTI1’ (note the single quotes!) and press enter. Under m_Instance enter MAIN.OPTI1 and press enter. The References now look like this: 11. Repeat the same process for the instance of the GUI_MOTOR_CONFIG_TEMPLATE and name the GUI GUI_Config. 12. Save the project. Activating Configuration and Logging in 1. 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. 2. 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 the screen indicating the Run Mode. will appear in the lower right corner of 3. 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 1. After Login is complete, switch to the GUI_Config tab in order to switch to the configuration GUI. 2. Ensure that LHW and UHW limit switch signals are configured as Active Low and that the brake related checkboxes are not selected. 3. Switch to the GUI Tab. 4. Select Local checkbox to enable buttons and entry fields on the GUI. 5. To reset the motor, press the following buttons in given order: DISABLE, RESET, ENABLE. 6. Press the INIT button to initialize the device. The LED next to it will become green once the device is initialized. 7. Test the motor in Position and Velocity mode. Note that there are separate speed entries for the two modes (VEL Position & VEL Velocity). 8. Try different INIT sequences, axis type, limits and backlash. Configuring Operational Mode on PLC Boot and OPC UA Server 1. Double-click on PLC / DemoMotor. In the DemoMotor window, under the Project tab, check the Autostart Boot Project check-box. With this setting the PLC will reboot in Run Mode. 2. 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. 3. Save the project and then Activate Configuration . 4. Confirm with OK and once again to Restart TwinCAT System in Run Mode. 5. Login again . Controlling Device from OPC UA Clients 1. 2. 3. 4. 5. 6. 7. 8. 9. Start an OPC UA Client, e.g. UaExpert. Select the Target OPC UA Server and Connect. Find the instance of OPTI1 under Root/Objects/PLC1/MAIN. Expand OPTI1 in order to display cfg, ctrl, info and stat folders. Drag and drop at least the following stat attributes to the Default DA View: bEnabled, bInitialised, lrPosActual, lrVelActual, nAxisStatus, sErrorText and sStatus. Drag and drop all ctrl attributes to the Default DA View. Initialise the Motor by setting nCommand to 1 and then bExecute to TRUE (1). To move the Motor, for example, to 250 deg with the speed of 40 deg/sec, set nCommand = 3, lrPosition = 250, lrVelocity = 40 and bExecute = TRUE (1). Test different speeds and positions.
© Copyright 2026 Paperzz