© 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

21
© 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1

Transcript of © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

Page 1: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

SPiiPlus Training Class

Mathematical and Signal Processing Functions

1

Page 2: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Mathematical and Signal Processing Functions

ACSPL+ includes a large command set of built-in mathematical and signal processing functions. These functions allow for powerful algorithms to be written in minimal lines of code, including:

o I/O debouncingo Digital controllers and filterso Teach-and-goo Array processingo Error mappingo Kinematic transformations (forward and inverse)

2

Page 3: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Assignment Command

Assignment Command:o Assigning a value to a variable using an expressiono ‘=‘ is used to separate the variable from the expressiono An assignment command can only assign a value to a scalar variable, a

single element of an array, or a specific bit of a scalar variable or single element of an array.

Syntax:variable = expressionvariable.(bit) = expression1d_array(index) = expression2d_array(index1)(index2) = expression

3

Page 4: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Assignment Command

Examples:

GLOBAL INT tiVarGLOBAL REAL trVarGLOBAL INT tiArr(5)GLOBAL REAL trArr(7)GLOBAL INT tiArr2(10)(3)GLOBAL REAL trArr2(12)(7)

tiVar = 1trVar = 1 * 2tiArr(0) = tiVar + trVartrArr(3) = tiArr(0) / 2tiArr2(9)(0) = tiArr(0) + trArr(3)trArr2(0)(3) = 1 + tiArr2(9)(0) * 0.4

4

Name Value

tiVar 1

trVar 2.0

tiArr(0) 3

trArr(3) 1.5

tiArr2(9)(0) 4

trArr2(0)(3) 2.6

Page 5: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Unary Operators

Unary operators are functions that act on one operando - (unary minus)

Can be used with INT or REAL variables

o ~ (inversion) Can only be used with INT variables REAL variables are converted to INT

o ^ (logical not) Can only be used with Boolean (0,1) INT variables REAL variables and non-Boolean INT variables are converted to Boolean INT

variables

5

Page 6: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Unary Operators

Examples:

GLOBAL INT tiVarGLOBAL REAL trVarGLOBAL INT tiArr(3)GLOBAL REAL trArr(3)

tiVar = 1tiArr(0) = -tiVartiArr(1) = ~tiVartiArr(2) = ^tiVar

trVar = 2.5trArr(0) = -trVartrArr(1) = ~trVartrArr(2) = ^trVar

6

Name Decimal Hex (rounded)

tiArr(0) -1 0xFFFFFFFF

tiArr(1) -2 0xFFFFFFFE

tiArr(2) 0 0x00000000

Name Decimal Hex (rounded)

trArr(0) -2.5 0xFFFFFFFE

trArr(1) -2 0xFFFFFFFD

trArr(2) 0 0x00000000

Page 7: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Binary Operators

Binary operators are functions that act on two operands

7

+ (addition) - (subtraction) * (multiplication) / (division) & (and) | (or) ~ (xor) = (equal to)

<> (not equal) > (greater than) >= (greater than or equal to) < (less than) <= (less than or equal to)

Page 8: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Binary Operators

Examples:GLOBAL INT tiArr(5)GLOBAL REAL trArr(5)

tiArr(0) = 0b00001010tiArr(1) = 0b11100100tiArr(2) = tiArr(0) & tiArr(1)tiArr(3) = tiArr(0) | tiArr(1)tiArr(4) = tiArr(0) ~ tiArr(1)

trArr(3) = 5trArr(0) = trArr(3) * 4trArr(1) = trArr(3) / 0.1IF ( trArr(0) > trArr(1) ) trArr(2) = 1ELSE trArr(2) = 0END

8

Name Decimal Hex (rounded)

tiArr(0) 10 0x0000000A

tiArr(1) 228 0x000000E4

tiArr(2) 0 0x00000000

tiArr(3) 238 0x000000EE

tiArr(4) 238 0x000000EE

Name Decimal Hex (rounded)

trArr(0) 20 0x00000014

trArr(1) 50 0x00000032

trArr(2) 0 0x00000000

trArr(3) 5 0x00000005

Page 9: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Order of Operations

ACSPL+ uses a standard order of operations. The following table shows the operator precedence from highest to lowest. Operators with the same precedence are evaluated from left to right.

9

Operator Operation

() Brackets the expression

. Bit selection

- ~ ^ Unary minus, Inversion, Logical not

* / Multiplication, Division

+ - Addition, Subtraction

= <> < > <= >= Comparison

& | ~ Logical and bitwise AND, OR, XOR

Page 10: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Order of Operations

Examples:GLOBAL REAL trArr(6)

trArr(0) = 1 + 2 * 3trArr(1) = 1 * 2 + 3trArr(2) = (1 + 2) * 3trArr(3) = 5 * 6 / 7trArr(4) = 5 / 6 * 7

IF ( trArr(4) > trArr(3) * 1 + 2 - 5 ) trArr(5) = -1ELSE trArr(5) = 1END

10

Name Decimal

trArr(0) 7

trArr(1) 5

trArr(2) 9

trArr(3) 4.28571

trArr(4) 5.83333

trArr(5) -1

Page 11: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

General Mathematical Functions

ACSPL+ supports a set of standard mathematical functions.

11

abs( in )= Absolute value

ceil( in )= Ceiling of a value

floor( in )= Floor of a value

hypot( x, y )=

pow( x, y )=

sqrt( in ) =

sign( in )= Sign of a value

roll( in, const )= Modulus of a value

Page 12: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Trigonometric Functions

ACSPL+ supports the standard list of trigonometric functions. All trigonometric function use radians (not degrees).

12

sin( angle ) cos( angle ) tan( angle )

asin( x ) acos( y ) atan( x / y ) atan2( x, y )

Page 13: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Exponential and Logarithmic Functions

ACSPL+ supports the standard exponential and logarithmic functions.

13

exp( in )=

ldexp( x, y )=

log( in )= Natural logarithm

log10( in )= Base 10 logarithm

Page 14: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Array Processing Functions

ACSPL+ support some basic array processing functions

14

avg( )= average of array elements

copy( )Copies elements from one array to another

fill( )Fills in an array with a constant value

max( )= maximum entry in an array

maxi( )= index of maximum entry in an array

min( )= minimum entry in an array

mini( )= index of minimum entry in an array

Page 15: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Signal Processing Functions

ACSPL+ supports some standard signal processing functions.

15

deadzone( )= signal with deadzone

dsign( )= sign of signal with delay and ramp time

edge( )= edge detection of signal

intgr( )= integration of signal

lag( )= state of signal (high or low) with positive and negative edge delays

sat( )= signal with saturation range

Page 16: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Mapping / Interpolation Functions

ACSPL+ supports some standard 1D mapping and interpolation functions.

16

map( )1D linear interpolation with uniformly spaced points

mapb( )1D B-spline interpolation with uniformly spaced points

maps( )1D Catmull-Rom spline interpolation with uniformly spaced points

mapn( )1D linear interpolation with non-uniformly spaced points

mapnb( )1D B-spline interpolation with non-uniformly spaced points

mapns( )1D Catmull-Rom spline interpolation with non-uniformly spaced points

Page 17: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

Mapping / Interpolation Functions

ACSPL+ supports some standard 2D mapping and interpolation functions.

17

map2( )2D linear interpolation with uniformly spaced points

map2b( )2D B-spline interpolation with uniformly spaced points

map2s( )2D Catmull-Rom spline interpolation with uniformly spaced points

map2n( )2D linear interpolation with non-uniformly spaced points

map2nb( )2D B-spline interpolation with non-uniformly spaced points

map2ns( )2D Catmull-Rom spline interpolation with non-uniformly spaced points

Page 18: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

ACSPL+ Programming Example: 1

Digital Input Debouncing:

A digital input from a mechanical pushbutton is used to tell a rotary stage to advance 2 rotations. In order to remove false triggers when the pushbutton is pressed and released, a debouncing mechanism is required.

1. Load program “Programming 08 – DIODebouncing.prg” to the controller.o Should populate buffer 11

2. Open communication terminal and set it up to show DISP messages3. Plot the variables di_signal and di_debounced on the scope

o Set the time for 0.1 sec/div and the trigger on Auto

4. From the communication terminal start buffer 11 at line 1 (“START 11, 1”). Follow the instructions on the screen

18

Page 19: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

ACSPL+ Programming Example: 2

Analog Input Filtering:

An analog input is being used as accelerometer feedback for increasing the performance of a stage. The accelerometer feedback should be low frequency (< 5 Hz), so it is desired to put a single-pole low pass filter at 50 Hz.

1. Load program “Programming 08 – AnalogFiltering.prg” to the controller.o Should populate buffer 12

2. Open communication terminal and set it up to show DISP messages3. Plot the variables ai_in and ai_filt on the scope

o Set the time for 0.1 sec/div and the trigger on Auto

4. From the communication terminal start buffer 12 at line 1 (“START 12, 1”). Follow the instructions on the screen

19

Page 20: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

ACSPL+ Programming Example: 3

X-Y Plane Rotation:

1. Load program “Programming 08 – XYRotation.prg” to the controller.o Should populate buffer 13

2. Modify the program in order to allow the user to specify a rotation in degrees.3. Test the program by rotating the plane 45 degrees, and 90 degrees, and running

test X and Y axis moves.

Hint: A 2-D rotation can be described by the following rotation matrix:

20

Page 21: © 2013 SPiiPlus Training Class Mathematical and Signal Processing Functions 1.

© 2013

ACSPL+ Programming Example: 4

Simple Digital Controller:

An application requires an EtherCAT drive to be run in torque mode in order to give a constant desired torque on a load. A simple PI controller is used with a torque sensor in order to give a steady effective torque.

1. Open buffer 142. Write a program to give the desired results. You can create user-defined variables

for the drive output command and the torque sensor input.

21