PROGRAMMING VARIABLES

PickUP AND LEARN SERIES T E C H N O L O G Y PROGRAMMING VARIABLES Trio Motion Technology Pick-up And Learn Series First Edition • 2012 Revision : ...
Author: Jasmin Charles
20 downloads 2 Views 2MB Size
PickUP AND LEARN SERIES

T E C H N O L O G Y

PROGRAMMING VARIABLES

Trio Motion Technology Pick-up And Learn Series First Edition • 2012 Revision : 1.0 Trio Programming Guides are designed to aid learning of the TrioBASIC language through description and examples. Each one will cover a particular topic and discuss which commands and parameters in the TrioBASIC are required to complete the task. A general understanding of TrioBASIC is required and it is recommended to attend an introduction to TrioBASIC training course. The programming guides are not a replacement for the TrioBASIC help files which can be found in Motion Perfect as well as the manual which cover each command and parameter in more detail and should be referenced when required. Any examples given in the programming guide will work and have been tested on an isolated controller. If you choose to use these examples on a machine please take care that it will not cause damage or injury and that they are correctly included in the project changing parameters and values where required. .

All goods supplied by Trio are subject to Trio’s standard terms and conditions of sale. The material in this manual is subject to change without notice. Despite every effort, in a document of this scope errors and omissions may occur. Therefore Trio cannot be held responsible for any malfunctions or loss of data as a result. Copyright (C) 2000-2013 Trio Motion Technology Ltd. All Rights Reserved

UK | USA | CHINA | INDIA www.triomotion.com SAFETY WARNING

During the installation or use of a control system, users of Trio products must ensure there is no possibility of injury to any person, or damage to machinery. Control systems, especially during installation, can malfunction or behave unexpectedly. Bearing this in mind, users must ensure that even in the event of a malfunction or unexpected behaviour the safety of an operator or programmer is never compromised. This doucment uses the following icons for your reference:

M Information that Information to relates to safety issues highlight key and critical software features or methods. information





Useful tips and techinques.

Example programs

Programming Guides

Contents Variables and Data storage...................................................... 1 Introduction...................................................................... 1 Local variables..................................................................... 1 Description....................................................................... 1 Declaration....................................................................... 1 Resolution........................................................................ 2 STRING variables.................................................................. 3 Description....................................................................... 3 Declaration....................................................................... 3 String handling................................................................... 4 Arrays of local variables......................................................... 5 Declaration....................................................................... 5 Table................................................................................. 6 Description....................................................................... 6 Table Commands................................................................ 6 Declaration....................................................................... 7 Retaining TABLE values......................................................... 7 Saving in a BASIC program..................................................... 7 Saving into the STARTUP program............................................ 7 Storing into the SD card........................................................ 7 Saving into controller FLASH memory........................................ 7 Flash data preservation scheme.............................................. 8 VR values........................................................................... 9 Description....................................................................... 9 Declaration....................................................................... 9 Retaining VR values............................................................. 9 Saving in a BASIC program..................................................... 9 Saving into the STARTUP program............................................ 9 Storing into the SD card........................................................ 9 Flash data preservation scheme............................................. 10 FLASH_DATA preservation scheme........................................... 10

CONTENTS 

Programming Guides

Variables and Data storage INTRODUCTION TrioBASIC supports various local and global variables to aid programming. They can be used in many ways including making the program more flexible, increase readability and to transfer data from one program to another. Some variables are volatile and so are lost when the program stops executing, some are held in memory so are lost when the controller powers down or performs an EX, finally some are held in either flash memory or are battery backed so are retained. This document will go through the different types of variables and how they can be used in a program.

Local variables DESCRIPTION Local variables are numerical values that are specific (local) to one program. This means that the same named local variable can have different values in separate programs. Local variables are simple to identify in a program as they are always in lower case. Motion Perfect will color local variables in black. Labels are also in lower case and can be confused with local variables, though Motion Perfect 3 will color these green.

local_variable = 1 label: GOTO label

DECLARATION By default declaring local variables is optional, when first used they have a start value of 0. COMPILE_MODE can be set in the MC_CONFIG program to force declaration of local variables, this can be useful as it helps to reduce problems associated with uninitialised variables or typing errors. When required to declare a local variable then DIM should be used, four different variable types are possible: Keyword

FLOAT INTEGER BOOLEAN STRING

Description 64bit floating point number (default) 64bit integer value 1bit binary value

Text value (see STRING variables)

When a local variable is not declared it will be a 64bit floating point number by default. ›› If no value has been assigned then it will take the value ‘0’ Local variables can be declared and assigned a value in the ‘INCLUDE’ program.

RESOLUTION TrioBASIC allows mixed use of variable types, the programmer must consider the impact this may have on the resolution of the result. The following rules are used by the controller when working between number types:

Text Files Variables and Data storage

1

Trio Motion Technology

• If a float is assigned to an integer it will be truncated. • If a float is operated on by an integer the result will be a float, unless assigned to an integer. • If an integer number exceeds the maximum integer range then it will revert to a float to store the result. • If you try to assign a number that is too big for an INTEGER variable then you will get an out of range error. • Any non-zero number assigned to a Boolean will give it the value -1 (TRUE in TrioBASIC)

Example 1 ÂÂ Local variables can be used in the program to store a value. They can be operated on by any mathematical function. In this simple example a local variable has its value set by multiplying two other local variables. The result is then printed to the terminal.

DIM length AS FLOAT   DIM width AS FLOAT   length = 10   width = 5   area = length * width   PRINT “Area = “;area

Example 2 ÂÂ Local variables can be used in FOR NEXT structures. There is no need to set an initial value as this is done by the FOR. In the below example output 8 is flashed 10 times.

  DIM warning_lamp AS INTEGER   DIM count AS INTEGER   warning_lamp = 8   FOR count = 1 TO 10     OP(warning_lamp,ON)     WA(200)     OP(warning_lamp,OFF)     WA(200)   NEXT count

Example 3 ÂÂ Local variables can be used to make code simpler to maintain and easier to read. In this example local variables are used to number inputs, outputs and VRs. If a switch were connected to a different input it is easy to map the change by changing the value in the local variable. The initializing of the local variables can be performed using INCLUDE so one definition can be added to multiple programs.

  ‘Local variables used for inputs   machine_start = 1 ‘input 1 for machine start button   machine_stop = 2 ‘input 2 for machine stop button   machine_estop = 3 ‘input 3 for emergency stop button      ‘Local variables used for outputs   enable_knife = 8 ‘output 8 for knife enable   warning_lamp = 9 ‘output 9 for the warning lamp      ‘Local variables used for VR’s   error_count = 0 ‘VR(0) for the error count     WHILE IN(machine_stop) = OFF

2

Text Files Local variables

Programming Guides

    IF IN(machine_start) = ON THEN       OP(enable_knife,ON)     ELSEIF IN(machine_esto) THEN       GOSUB estop_routine     ENDIF   WEND   STOP    estop_routine:   RAPIDSTOP(2)   OP(enable_knife,OFF)   OP(warning_lamp,ON)   VR(error_count) = VR(error_count) + 1 RETURN

STRING variables DESCRIPTION String variables are a special type of local variables which are declared to hold text (strings). They are local variables so are specific (local) to one program. This means that the same named local variable can have different values in separate programs. If you need to transfer a string from one program to another then they can be written to ›› VRs using ASC, then read back using VR_STRING. String variables have many uses but can be used to store HMI messages as well as in the decoding of a text file such as G-Code or HGPL. As STRING variables are a special type of local variables they are also in lower case and Motion Perfect colors them black. If you confuse them with a local variable and try to assign them a numerical value the controller will return a run time error.

DECLARATION

STRING variables must be declared at the start of a program using the DIM command. It is important to declare the name of the string as well as the maximum length.

THAT IF THE STRING EXCEEDS THE DEFINED MAXIMUM LENGTH THEN IT WILL 00REMEMBER BE TRUNCATED WITH NO WARNING.

DIM string_local AS STRING(100) The length of the string must be declared using a hard coded numerical value. You cannot use local variables, VR or other numerical parameters to declare the length of a STRING.

STRING HANDLING To assist with strings there are a selection of commands that can be used to process the values. The table below lists the commands and their functionality. Please see the Technical Reference Manual, or Motion Perfect 3 TrioBASIC help file for more information. If any STRING command uses a parameter outside the length of the string then the return value will either be truncated or return a false.

Text Files STRING variables

3

Trio Motion Technology

Command ASC CHR

INSTR LCASE LEFT

Function

Converts a string character to an ASCII number Returns as ACSII number as the string value

Returns the position of a string within another string (string search) Returns the string in lower case Returns the specified number of characters from the left side of the string

LEN

Returns the length of the string

MID

Returns the specified number of characters from the specified section of the string

RIGHT STR

UCASE VAL

VR_STRING

Returns the specified number of characters from the right side of the string Returns a string from a number Returns the string in upper case Returns a number from a string Read a sequence of characters from the VRs into a string variable

Example 1 ÂÂ Messages can be stored in the STRING variables then printed to the terminal when required. This example looks at various AXISSTATUS bits and displays the appropriate message.

  DIM error_fe AS STRING(100)   DIM error_limit AS STRING(100)   DIM error_communications AS STRING(100) DIM error_other AS STRING(100)      error_fe = “Following Error exceeds limit”   error_limit = “Software or hardware limit reached”   error_communications = “Drive communications error”   error_other = “Other AXISSTATUS error”      IF AXISSTATUS.2 THEN     PRINT error_communications   ELSEIF AXISSTATUS.8 THEN     PRINT error_limit   ELSEIF AXISSTATUS.4 THEN     PRINT error_limit   ELSEIF AXISSTATUS.5 THEN     PRINT error_limit   ELSEIF AXISSTATUS.9 THEN     PRINT error_limit   ELSEIF AXISSTATUS.10 THEN     PRINT error_limit   ELSE     PRINT error_other   ENDIF

Example 2 ÂÂ Strings can be built by adding together sections of characters. This routine reads in characters from one of the communication channels then appends the character to the string. Remember the GET will return the ASCII code of the character so when appending it to the string you must use the CHR to convert the number to the character.

  DIM recieved_characters AS STRING(100)

4

Text Files STRING variables

Programming Guides

  WHILE KEY#5     GET#5, character     recieved_characters = recieved_characters + CHR(character)   WEND

Example 3 ÂÂ STRINGS can be manipulated so that various sections can be removed or searched. In this

example a block of G-Code has the first 5 characters removed then the remainder is searched for the index of the first G-Code. Then the G-Code word is removed using the MID command.

  DIM gcode_block AS STRING(100)   DIM gcode_word AS STRING(3)      gcode_block = LEFT(gcode_block,5)   index = INSTR(gcode_block, “G**”,”*”)   gcode_word = MID(gcode_block,index,3)

Arrays of local variables DESCRIPTION It is possible to declare an array of local variables. This means that you have a group of values by the same name which can be indexed. An array can be up to 3 dimensions and up to 1024 in size in each dimension.

DECLARATION Arrays must be declared and this is done using the DIM command. They are declared in the same way as local variables but have an additional bracketed parameter indicating the size of the array.

DIM local_integer AS INTEGER(10)  DIM local_float AS FLOAT(10,10)  DIM local_boolean AS BOOLEAN(10,10,10) The length of the array must be declared using a constant numerical value. You cannot use local variables, VR or other numerical parameter to declare the length of a STRING.

Example 1 ÂÂ Declare an array of floating point numbers which can store a set of parameters for each axis, then later in the program load these values to axis parameters.

DIM axis_config AS FLOAT(3,3) axis_config(axis_0, max_speed) = 100 axis_config(axis_0, max_accel) = 2000 axis_config(axis_0, max_decel) = 2500 axis_config(axis_1, max_speed) = 200 axis_config(axis_1, max_accel) = 4000 axis_config(axis_1, max_decel) = 4000 axis_config(axis_2, max_speed) = 100 axis_config(axis_2, max_accel) = 2000 axis_config(axis_2, max_decel) = 2000 Text Files Arrays of local variables

5

Trio Motion Technology

‘Setup maximum speeds BASE(axis_0) SPEED = axis_config(axis_0, max_speed) ACCEL = axis_config(axis_0, max_accel) DECEL = axis_config(axis_0, max_decel)

Example 2 ÂÂ Read the AXISSTATUS into a Boolean array.

DIM status_array AS BOOLEAN(19) FOR x = 0 TO 18   status_array(x) = AXISSTATUS.x NEXT x

Table DESCRIPTION The table is an area of memory that can be accessed in large blocks. This makes it very useful for storing recipes and profiles. The table memory is global so if a value is changed in one program then the value is also available in another program. Table can be mapped to communication protocols such as Modbus so the values can be sent to and read from other devices. Many commands take values from the TABLE such as CAM, FRAME and many more.

TABLE COMMANDS

The following commands are available to assist reading and writing values to the TABLE. Please see the Technical Reference Manual, or Motion Perfect 3 TrioBASIC help file for more information. Command

TABLE STICK_READ STICK_WRITE FLASH_TABLE TSIZE NEW “TABLE”

Function

Read/ write values to/ from the TABLE Write to the TABLE from the SD card Save TABLE data to the SD card

Read/ write TABLE values from the controller flash memory Returns the maximum size of the TABLE Set all the TABLE values to ‘0’

DECLARATION

There is no need to declare the TABLE as it is always configured on power up.

RETAINING TABLE VALUES

There are 512000 TABLE locations available on the MC4xx range of controllers. When a controller has a battery the first 196608 values are retained the rest are volatile. On controllers with no battery all the TABLE values are volatile so lost on a power cycle or EX. There are various methods of storing TABLE values some in the controller and some externally:

SAVING IN A BASIC PROGRAM

The TABLE can be initialized in a BASIC program using the TABLE command.

6

Text Files Table

Programming Guides

SAVING INTO THE STARTUP PROGRAM

The Motion Perfect v3 tool can save the TABLE to the STARTUP program, this can then be run to initialize the TABLE.

STORING INTO THE SD CARD

TABLE data can be saved to and read from the SD card using STICK_READ and STICK_ WRITE. The whole table can be saved into one file on the SD card in wither a binary or comma separated variable (CSV) format. The functions allow for up to 999999 binary and 999999 CSV files. Advantages of saving to the SD card is that it can be done programmatically and very large quantities of values can be stored.

SAVING INTO CONTROLLER FLASH MEMORY

The TABLE can be split into pages of 16000 values. Up to 32 pages can be stored into the controller FLASH memory using the FLASHTABLE function. All the TABLE values can be saved into the FLASH memory using the 32 pages. It is quicker to save the page rather than the whole TABLE in one. Each page of flash memory is specified to have 100000 erase cycles, each write to a page uses one erase cycle. Therefore it is important not to write to a page too frequently otherwise there is a risk of shortening the life of the motion controller. An alternative method is to save the whole table in one go and have it reloaded on power up. This can be done using FLASHVR(-1) and writes to all 32 pages in one go. (-1) SAVES ALL THE TABLE TO FLASH, THIS CAN TAKE MANY SECONDS TO 00FLASHVR EXECUTE.

FLASH DATA PRESERVATION SCHEME

By setting FLASH_DATA the user can decide if they wish to retain the first 4096 TABLE values or VR values (VR’s are default).

Example 1 ÂÂ A CAM profile is loaded to the TABLE in a TrioBASIC program then executed using the CAM command.

  table_start = 0   points = 40   ‘Store the cam profile in TABLE   TABLE(table_sta rt,0,0.0111,0.0883,0.296,0.6953,1.3422,2.2864,3.5699,5.2257)   TABLE(table_start + 9,7.2775,9.7384,12.6113,15.888,19.5501,23.5692,27.9076)   TABLE(table_start + 16,32.5194,37.3514,42.345,47.4373,52.5627,57.655,62.6486)   TABLE(table_start + 23,67.4806,72.0924,76.4308,80.4499,84.112,87.3887,90.2616)   TABLE(table_start + 30,92.7225,94.7743,96.4301,97.7136,98.6578,99.3047,99.704) Text Files Table

7

Trio Motion Technology

  TABLE(table_start + 37,99.9117,99.9889,100)      tablestop = tablestart + points - 1   linkspeed = 1000   linkdist = 500   ‘Set the speed on the axis to be moved   SPEED AXIS(moveax) = linkspeed      ‘Perform the move   CAM(tablestart,tablestop,1,linkdist)

Example 2 ÂÂ Load the table with the configuration parameters for a robot then enable the FRAME, which uses these parameters.

  link1 = 225000   link2 = 125000   link3 = 50000   t1_encoder = 4*8192   t1_edges_per_radian = t1_encoder / (2 * PI)   t2_encoder = 4*8192   t2_edges_per_radian = t2_encoder / (2 * PI)   t3_encoder = 4*8192   t3_edges_per_radian = t3_encoder / (2 * PI)   z_encoder = 4*8192   z_mm_per_rev = 5   z_edges_per_mm = z_encoder / z_mm_per_rev   mech_config = 0 ‘mechanicial configuration   ratio = 1   TABLE(0, link1, link2, t1_edges_per_radian, t2_edges_per_ radian, 0, mech_config)   TABLE(9, t3_edges_per_radian, link3, ratio, z_edges_per_mm)      FRAME = 15

VR values DESCRIPTION VRs are global variables so they are common values across all programs. If they are changed in one program the value is also available in another value. The VR values can be mapped to communication protocols such as Modbus so the values can be sent to and read from other devices. By default VRs are always backed up, so retain their value even when the power is removed.

DECLARATION VR values are addressed starting at 0 to the maximum number available on the controller.

8

Text Files VR values

Programming Guides

All VR values can be set to the default value ‘0’ using the CLEAR command

RETAINING VR VALUES There are 4096 VR locations available on the MC4xx range of controllers. On controllers with a battery they are retained in battery backed memory. If the battery is flat or removed then the VR values are lost. On controllers that do not have a battery they are stored into flash memory automatically using the VR preservation scheme unless FLASH_DATA has been set to save the TABLE. In addition to the retained values there are other methods available to store VR values, some in the controller and some externally:

SAVING IN A BASIC PROGRAM

The VRs can be initialized in a BASIC program using the VR command.

SAVING INTO THE STARTUP PROGRAM

The Motion Perfect v3 tool can save the VRs to the STARTUP program, this can then be run to initialize the VRs.

STORING INTO THE SD CARD

VR data can be saved to and read from the SD card using STICK_READVR and STICK_ WRITEVR. All the VRs can be saved into one file on the SD card in wither a binary or comma separated variable (CSV) format. The functions allow for up to 999999 binary and 999999 CSV files. Advantages of saving to the SD card is that it can be done programmatically and very large quantities of values can be stored.

FLASH DATA PRESERVATION SCHEME

By setting FLASH_DATA the user can decide if they wish to retain the first 4096 TABLE values or VR values (VR’s are default).

Example 1 ÂÂ VR values can be set from an HMI using a communication method such as Modus to initialize machine or product values. Then used in a program to set the machine configuration.

  ‘product_length = VR(100)   ‘machine_speed = VR(101)      SPEED AXIS(0) = VR(101)   FORWARD AXIS(0)   CAM(0,99, VR(100), 100)

FLASH_DATA PRESERVATION SCHEME

When a controller has a battery it will preserve all VR and some TABLE. If a controller does not have a battery then it uses a preservation scheme to store the TABLE or VR. The preservation scheme is a background process will store any changed VRs or TABLE into flash memory for recovery on the next power-cycle/EX. This background process will schedule a maximum of 4 changed values every 100ms.

Text Files VR values

9

Trio Motion Technology

FLASH_DATA by default is set to store the first 4096 VR values on controllers that do not have a backup battery. It is possible to change the value in the FLASH_DATA parameter so that it will store the first 4096 TABLE values. The flash memory is specified to have 100000 erase cycles. If a program continuously changed VR values then it would take over 48 years to reach this value. The worst case is if all 4096 values were changed very quickly then it would take the background process 102 seconds to save all the values. It is possible to force the controller to save all changed values by issuing FLASHVR(-100), this can take up to 0.5 second to complete. If a software controller reset is issued (EX), any changed values are automatically saved to flash.

10

Text Files VR values

Programming Guides

Text Files 

11