• No results found

Machine Control with Top-Down Programming

In document TwinCAT 2 Manual v2_1_0 (Page 98-171)

IV. PLC Programming “The Inspection Conveyor”

21. Machine Control with Top-Down Programming

Intro

This section is going to cover the design and programming of a modular conveyor. With each module a new concept and/or topic will be introduced. As the saying goes “Prior proper planning, prevents poor

performance”. With that in mind the first topic will cover the overall machine control and the use of a state machine for automatic or manual operation. The first conveyor module will be for adding product to the system using only digital Input and Outputs. The second module will use an analog input to measure the size of the product. Next an analog output will be added to control the conveyor speed using a Variable Frequency Drive (VFD).

Machine Control/State Machine

There are many ways to do overall machine control and to implement a state machine, both of which are outside the scope of this document. For the purpose of this document I have chosen to use a state machine that best serves the purpose of learning to use the TwinCAT software. The overall machine control will be handled in a CASE statement. The machine will have the following States:

Undefined: When no State is defined by the PLC this will be the default State. This state is not allowed to be set by the operator. This State will be used when the machine is first powered on and when a problem in the PLC code occurs.

Maintenance: Used for making adjustments to the machine or for troubleshooting individual components.

Operations will be allowed in this mode that could be harmful to the equipment. Access to this mode will be restricted.

Manual: Used to start up the machine and prepare for operation, or to shut down the machine after Automatic operation. Requires operator intervention for all functions of the machine.

Automatic: Used for routine production. The machine will process product based on the conditions of the I/O with minimal operator intervention.

Modular Conveyor System

Each conveyor module will need to work as a standalone piece and also in conjunction with other modules in front of and/or after it. Using a photo eye at each end of the conveyor will aid in this process. For the programming of this system each conveyor module will be a Function Block; therefore, if multiples of a module are needed they can be easily instantiated. Additionally, standard data Structures for all conveyor modules will be used by each of the Function Blocks to aid in communication between modules.

Machine data Structure: contains status information for the overall machine including the current State.

Module data Structure: contains information about the configuration of the module, including information about the previous and following module.

Creating the program

Open the PLC Control by selecting the TwinCAT icon in the Windows System Tray and the select PLC Control

From the ‘File’ menu, select ‘New’

For most of this project we will not be connecting to hardware. Therefore everything will run in simulation on the computer you are using.

Note: The TwinCAT 2 Run-Time is only available on Windows 32-bit Operating Systems In the ‘Choose Target System Type’ window, select ‘PC or CX (x86), then click on ‘OK’

In the ‘New POU’ window the Type of POU should be a ‘Program’

The ‘Name of the new POU:’ should be ‘MAIN’

The ‘Language of the POU’ should be set to ‘ST’ for Structured Text

Note: Even if you are an experienced programmer in one of the other languages, it is my recommendation that when starting a new project the MAIN program should always be done in ST. This will allow the programmer to easily call other programs and also easily comment out large parts of the program.

Additionally I would advise that the MAIN program never be done in SFC, doing so will make using the special SFC flags much more difficult, if not impossible.

You should now have the following

Place a semicolon on Line 1 of MAIN.

From the ‘Project Menu’, select ‘Rebuild All’

In the ‘Message Window’ at the bottom, you should receive 0 Errors and 1 Warning.

The Warning is because we have not saved the file with a name.

From the ‘File Menu’, select ‘Save As’

The file can be saved anywhere you would like. I would recommend against saving it on the desktop, the PLC Control will also create other supporting files that will clutter your desktop quickly. I have created a folder called ‘TwinCAT 2 Manual Samples’ directly on the root of my C:\ drive.

Give your project a name and press the ‘Save’ button. I would recommend that you use the same file name that I have used.

Adding a version number to your project name is an easy way to have multiple versions of the program, so that you can go back to a previous version later on.

You will now see that the file name of the project is placed across the top of the PLC Control

Before writing any real code we will first declare all known variables that will later be connected to hardware.

Select the ‘Resources’ tab at the bottom of the left column

Expand the ‘Global Variables’ folder by clicking on the + sign

In the ‘Global Variables’ folder there are two lists by default.

The ‘Global_Variables’ and the ‘Variable_Configuration’

The ‘Variable_Configuration’ list is only used for the BC line of controllers.

The ‘Global_Variables’ list is included by default; however, on large machines it is good practice to create multiple lists to help organize the variables into smaller more manageable lists. Therefore, we are going to start by creating a couple of Global Variable Lists.

Right Click on the ‘Global Variables’ folder, select ‘Add Object’

Change the name of the list by adding ‘_IO’ to the end of the name, and then click on ‘OK’

Note: The name of this list must follow the IEC 61131-3 naming rules, the same as variable names. If it does not, or the name has already been used the ‘OK’ button will be grayed out.

Double-Click on ‘Global_Variables_IO’

This will open the Global Variable list.

Place the cursor at the end of line 1 and press the enter key a couple of times.

Between the key words ‘VAR_GLOBAL’ and ‘END_VAR’ is where we will declare our variables.

Note: Please refer to the Appendix “Variable Naming Convention” for a better understanding of the variable names used throughout this project.

Comments can be added to the code by placing (* at the beginning of the comment, and *) at the end of the comment. All comments will turn green.

(*Machine Control*)

The following will be Boolean inputs of type BOOL, therefore when the hardware detects 24 Volts DC the PLC will represent this with TRUE, when the hardware detects 0 Volts DC the PLC will represent this with FALSE.

gati_xMan_Auto_SS will be a two position Selector Switch between Manual and Auto. When the switch is in the ‘Manual’ position the input will be off, when the switch is in the ‘Auto’ position the input will be on.

gati_xMaintenance will be a push button to request the State Machine to go into ‘Maintenance’

gati_xReset will be a push button for resetting faults (*Stack Lights*)

The use of Stack Lights allows everyone in the area of the machine to easily know the status of the machine.

The definitions of what the colors represent vary between industries and countries.

For this project the following colors will be used as defined here:

‘Yellow’ will be used when the machine is in Automatic

‘Green’ will be used when the machine is in Manual

‘Red’ will be used when a Fault is active

‘Blue’ will indicate that the machine is in Maintenance

Between ‘Yellow’, ‘Green’, and ‘Blue’ only one of the can be on at any given time.

‘Red’ can be on in addition to any of the others

gatq_xAutoLight will be written with a value of TRUE when the machine is in Auto, thereby applying 24 Volts DC to the output and turning on the light.

gatq_xManualLight will be written with a value of TRUE when the machine is in Auto, thereby applying 24 Volts DC to the output and turning on the light.

gatq_xFaultLight will be written with a value of TRUE when the machine is in Auto, thereby applying 24 Volts DC to the output and turning on the light.

gatq_xMaintenanceLight will be written with a value of TRUE when the machine is in Auto, thereby applying 24 Volts DC to the output and turning on the light.

Next we will create an Enumeration that will be used to represent the possible States of the State Machine.

Select the ‘Data Types’ tab at the bottom of the left column

Right-Click on the ‘Data Types’ folder and select ‘Add Object’

Type in ‘E_MachineState’ as the ‘Name of the new data type’, and click ‘OK’

You should now have the following

Data Types always default to a ‘STRUCT’, this can be changed to an Enumeration by simply removing

‘STRUCT’ and ‘END_STRUCT’ from lines 2 and 3 and replacing them with ();

When creating an Enumeration the first Variable will receive a value of zero by default, each value after that will be incremented by one. The variables of the Enumeration must be placed between ( and ) and each one separated by a comma ‘,’.

Now would be a good time to save the changes that have been made. From the ‘File’ menu, select ‘Save’. Or simply press and hold the ‘Ctrl’ key and the press the ‘S’ key.

Notice that the asterisk * and the end of the file name disappears. The asterisk is there to indicate that changes have been made but not saved.

To check the Enumeration and the Global Variables for any possible typing errors go to the ‘Project’ menu, and select ‘Rebuild All’

If you have any errors, they should be fixed before moving on.

As an example I have removed the semicolon from the end of one of the Global Variable declarations.

When preforming a ‘Rebuild All’ this generates 2 errors, which can be seen in the ‘Message Window’

Place your mouse at the top of the ‘Message Window’

Click and Drag the bar upwards to see more of the ‘Message Window’

It is best to start with the first error in the list, many times one problem will create others for the compiler.

The easiest way to find the first error in the list is to press the ‘F4’ key. Repeatedly pressing ‘F4’ will go to the next error in the list. You could also scroll through the error list and Double-Click on the error.

When the error is selected, the location of the error will be shown, and the line of code that has the problem will be highlighted. Repeatedly pressing ‘F4’ will go to the next error in the list.

The full error message states that the complier is ‘Expecting the end of line character or an assignment before seeing a new variable name. To the compiler the problem occurred on line 9, the appropriate way to fix the problem is to find the variable declared before line 9 and place the semicolon at the end of the line.

After fixing any errors you may have had, preform another ‘Rebuild All’ from the ‘Project’ menu.

Once, you have zero errors, you will get 7 warnings. Generally speaking warnings can be ignored. These 7 warnings are created by the use of %I* and %Q* variables. The warning simply states that the VAR_CONFIG file has not been created for these variables. ‘F4’ will scroll through the warnings. Ignore them for now and continue on.

Now would be a good time to ‘Save’ your project.

Finally, it is time to write some code. Click on the POUs tab at the bottom of the left column.

Double-Click on the MAIN program.

Note: You may or may not have the small blue arrow next to the icon for MAIN. This blue arrow simply indicates that changes have been made to this POU that have not been downloaded into the running PLC.

In the local declaration section of MAIN define a variable called eStep as of type E_MachineState

The best way to do this is to first type in the new variable name eStep, then place a colon after it, then press the F2 key which opens the Input assistant.

Select ‘User defined Types’ from the left column and then E_MachineState, press ‘OK’

This will bring you back to the declaration section; place a semicolon at the end of the line.

By declaring eStep to be of type E_MachineState the value of the variable eStep will be the text in the Enumeration. eStep will be used as the condition variable of the CASE statement that will control the State Machine.

The general layout of a CASE statement is as follows:

The variable between CASE and OF must use an integer value (INT,DINT,SINT,USINT). Enumerations hold an integer value.

The numbers that follow on the next lines represent the possible values of that variable. Therefore when iStep is equal to 0 the code between 0 and the next number will be run. The code following all other numbers will not be run. For example if iStep is equal to 10 then the code on lines 5 and 6 would be run the next line of code would be line 12, or the first line after the END_CASE command.

The ELSE case is a safeguard, if iStep is ever set to a value that is not defined the code in the ELSE command will be run.

The values of the condition variable are limited to integer values. In the above picture values are skipped to allow for the possibility to easily add steps in between. It is generally a good practice to do this, otherwise when a step has to be added then all following steps must be changed.

The value of iStep is set by conditions in the PLC code. The value can be changed from within the CASE statement or from outside the CASE statement.

For our project the Enumeration is declared as having 4 possible values, therefore the need for skipping numbers is not necessary. However the ELSE command should always be included.

Note: when using an Enumeration both of the above are valid; however, the use of the variable name from within the Enumeration makes the code easier to read.

By default when the PLC starts all values are 0, unless given an initial value. To ensure that our state machine starts at zero, an initial value will be placed on the variable eStep.

To do this double-click on the variable eStep in the declaration section of MAIN. This will highlight the variable name

Press and hold the ‘Shift’ key then press ‘F2’. The ‘Declare Variable’ window will open

Place a zero in the ‘Initial Value’ box, and click ‘OK’

The declaration of eStep now has an initial value of 0.

The first thing we will setup is the control of the value of eStep.

We have a Selector Switch for Manual or Auto and a pushbutton for Maintenance.

With the initial value of eStep being 0 the case statement will be in E_Undefined. Inside this step we will set eStep to go to E_Manual.

However, we should also look at the condition of the Selector Switch. The input is negated with the NOT command because the switch being in the ON position is for Auto.

Remember to use ‘F2’ for the Input Assistant to select gati_xMan_Auto_ss from the Global Variables

We now are able to put the machine into Manual Operation. From Manual, there needs to be a way to go into either Maintenance or Auto.

Now would be a good time to ‘Save’ your project.

It would also be a good time to check for errors. Go to the ‘Project’ Menu and select ‘Rebuild All’

You should get one error. Press ‘F4’ to go to the error.

Each value for the CASE statement must have some code in it. When the complier sees two values for eStep with no code in between them it causes an error. The easy way to avoid this is to place a semicolon after the colon. It should be removed later, but if you forget it won’t hurt anything.

You will also need to do this after E_Auto and ELSE

If you have any other errors, please address them before moving on.

As the programmer it is your duty to ensure that all possible conditions are accounted for. Currently it is possible to get into the Auto state but it is not possible to get out of it.

The selector switch will place the machine into the Manual state.

This is the same code that was used to go from E_Undefined to E_Manual.

From the Maintenance mode, pressing the Maintenance push button will place the machine back into Manual.

In the ELSE command eStep will be set to E_Undefined.

The entire body of MAIN, now looks like the following

Now would be a good time to check for errors using ‘Rebuild All’ from the ‘Project’ menu, and save your project.

At this point the control for the state machine is finished. Later in the section on ‘Fault Handling’ we will add to the state machine for what needs to be done when a fault occurs.

The plan is to have a function block for each conveyor module. Each function block will be capable of controlling the conveyor module in each possible machine state. Therefore the machine state will be passed into the function block. Before creating the function blocks, we will create the code that is going to call the function blocks.

In the ‘POU’ column right click and select ‘Add Object’.

Name the POU ‘P_MachineControl’. Leave the Type as a Program and set the Language to ‘ST’ for Structured Text. Then click on ‘OK’

Before writing any code in the new program we should call the program from ‘MAIN’. Double-click on

‘MAIN’ to open it. Below the code for the CASE statement, place the cursor and press ‘F2’.

In the ‘Input Assistant’ select ‘User defined Programs’ from the column on the left, select

‘P_MachineControl’ from the window on the right, then press ‘OK’.

This will call the new program every PLC scan. The open and close parenthesis are not required but should be used to indicate that it is a POU call.

Now double-click on ‘P_MachineControl’ in the ‘POU’ column. Later we will add code to this program for calling the Function Blocks that will control the machine. For now add a semicolon to prevent any build

Now double-click on ‘P_MachineControl’ in the ‘POU’ column. Later we will add code to this program for calling the Function Blocks that will control the machine. For now add a semicolon to prevent any build

In document TwinCAT 2 Manual v2_1_0 (Page 98-171)

Related documents