Machine Design Machine Design

Schedule change

ES1 admin  Tues Lecture 0810 Lab 1012  Wedn Lab 1012  Thurs Lecture 0810 (M3)  Thurs Lab 1012

Mechatronics Lab 1 Mechatronics Lab 2

Machine Design Machine Design

Final exam

 Oct 21, 14.0018.00  Rooms V32, V34 ES1 – CAN filtering

 No books, no computers, no papers  Only allowed to use: pen, calculator

 Bring ID, (fruit, snacks)  Allowed to ask clarifying questions  One small supervised break

Mechatronics Lab 3 Mechatronics Lab 4

Machine Design Machine Design

CAN filtering and masks Example

 Mask = 0xF0;  Filter0 = 0x34;  Filter1 = 0xA3;

 Filter0 let’s all messages from 0x30 to 0x3F through  Filter1 let’s all messages from 0xA0 to 0xAF through

Mechatronics Lab 5 Mechatronics Lab 6

1 Machine Design Machine Design

Example again Solution

 Max IDvalue is: 0x1FFFFFFF

 Create a mask and a filter that let’s  Mask = 0x1FFFFF00 (don’t bother messages 0x00123400 to about the last two digits) 0x001235FF through  Filter0 = 0x00123400 (4004FF)  Filter1 = 0x00123500 (5005FF)

Mechatronics Lab 7 Mechatronics Lab 8

Machine Design Machine Design

Again… conclusion

 One filter for all? Need to look at binary code…  Mask = 0x1FFFFE00  …4005FF =  Filter = 0x00123400 0100 0000 0000 to 0101 1111 1111

mask =  Let’s all messages through with ID: 1110 0000 0000 (=0xE00)  0x00123400 to 0x001235FF Filter = 0100 0000 0000 (=0x400)

Mechatronics Lab 9 Mechatronics Lab 10

Machine Design Machine Design

Why filter?

ES1 DSP  If you filter out all irrelevant messages, you don’t need to check ID  Alternatively, accept all Ids and check in program

Mechatronics Lab 11 Mechatronics Lab 12

2 Machine Design Machine Design

Why? Mathematical functions

 Either implemented in software or hardware  HW o Special functions in the processor, co  We need to do calculations fast. processor, DSPcore, MACunit etc. o From 1 cycle (DSP) to several cycles (CISC architecture)  SW o Various (more or less nice) implementations of mathematical functions, typically taylor series

Mechatronics Lab 13 Mechatronics Lab 14

Machine Design Machine Design

Fundamental Data Types (1) Fundamental Data Types in C Computers work in the binary number system. In the “C” language (for AVR microcontrollers) char c; // signed 8 integer. The basic unit is the bit (" BI nary digI T") // range: -128..127 A bit can be either “1” or “0” int i; // signed 16 bit integer. (High or Low, True or False, 5 Volts or 0 Volts) // range: -32768..+32767 long int l; // signed 32 bit integer. The other basic units are: // -2147483648..+2147483647 Nibble 4 0000 - 1111 (Binary), 0 - F (Hex) unsigned char u;// unsigned 8 bit number. 8 bits 0000 0000 - 1111 1111 (Binary), // range: 0 .. 255 00 - FF (Hex) unsigned int v;// unsigned 16 bit number. Word 16 bits 0000 - FFFF (Hex) // range: 0 .. 65535 Longword 32 bits 0000 0000 - FFFF FFFF (Hex) unsigned long w;// unsigned 32 bit number. // range: 0 .. 4294967296 Mechatronics Lab 15 Mechatronics Lab 16

Machine Design Machine Design

Signed number Signed Number representation Representation Hex numbers may be signed or unsigned. Unsigned numbers are positive only.

 Different ways of representing For 8 bits, they range from 0 .. 255 (0..FF hex) negative numbers exists. Signed numbers are positive, negative or zero. The most significant bit is used to represent the sign of a number

For 8 bits, they range from -128 ..127 (80..7F hex)

Mechatronics Lab 17 Mechatronics Lab 18

3 Machine Design Machine Design

Negative Number Decimal values representation Byte numeric representation (8 bits = 1 byte)

Signed Unsigned Binary Hexadecimal Octal +127 127 0111 1111 7F 177  Various definitions and -128 128 1000 0000 80 200 representations… -4 252 1111 1100 FC 374  Float in C: 32 bits -3 253 1111 1101 FD 375  Double: 64 bits -2 254 1111 1110 FE 376 -1 255 1111 1111 FF 377

Mechatronics Lab 19 Mechatronics Lab 20

Machine Design Machine Design

Decimal values cont.d The AVR family members (3) New high performance 32 bit MCU/DSP AVRs :

The AVR32 UC3B devices deliver 72 Dhrystone MIPS (DMIPS) at 60 MHz, and include true single-  Two major alternatives: cycle MACs and DSP arithmetic and consume only 23 mA at 3.3V.  Floating point  Fixed point DSP - Digital Signal Processor. MAC - Multiply & AC cumulate - A typical function performed by DSPs to implement Filters, Discrete/ Fast Fourier Transforms (butterfly) etc...

Mechatronics Lab 21 Mechatronics Lab 22

Machine Design Machine Design

From the manual Types of Computers • RISC - Reduced Instruction Computer • The instruction set is small, and most instructions complete in one cycle (100 or less instruction types,  High Performance, Low Power smaller range of addressing modes). AVR®32 UC 32Bit Microcontroller • Multiply & Divide performed using add/subtract & shift  Compact Singlecycle RISC Instruction • DSP - Digital Signal Processor Set Including DSP Instruction Set • The instruction set is specialized for common fixed  ReadModifyWrite Instructions and Atomic Bit Manipulation or floating point arithmetic. DSP performance is measured in MFLOPS - Millions of Floating/Fixed Point Operations Per Second.

Mechatronics Lab 23 Mechatronics Lab 24

4 Machine Design Machine Design

Types of Computers Basically… • CISC - Complex Instruction Set Computer • The instruction set is large, and offers great variety of instructions (100 or more instruction types, many addressing modes). • Few instructions complete in one cycle  1 cycle = 1/f (1/60MHz) • Typically includes multiply & divide operations that may take many cycles to complete. • EG the 68HC11 takes- 10 Cycles for (8x8) MUL • 41 cycles for 16 / 16 Divide. 200K mul/sec, 50K div / sec

Mechatronics Lab 25 Mechatronics Lab 26

Machine Design Machine Design

Mathematical functions Taylor series…

 In mathematics , the Taylor series is a representation of a function as an infinite sum of  Either implemented in software or terms calculated from the values of its hardware derivatives at a single point. The Taylor series  HW was formally introduced by the English mathematician Brook Taylor in 1715. If the series o Special functions in the processor, co processor, DSPcore, MACunit etc. is centered at zero, the series is also called a Maclaurin series , named after the Scottish o From 1 cycle (DSP) to several cycles (CISC architecture) mathematician Colin Maclaurin who made extensive use of this special case of Taylor's  SW series in the 18th century. It is common practice o Various (more or less nice) implementations of to use a finite number of terms of the series to mathematical functions, typically taylor series approximate a function. The Taylor series may be regarded as the limit of the Taylor polynomials .

Mechatronics Lab 27 Mechatronics Lab 28

Machine Design Machine Design

Mathematical functions AVR32 Fixed point support cont.d  Difficult and expensive to include full DSPfunctionality (complete one operation in one cycle)  Hardware support, some  Various combinations and breeds of mathematical instructions are HW and SW implemented in HW  Only for fixed point variables (not floats)  AVR32: Some fixed point DSP support  Requires the user to use a special library, special set of functions

Mechatronics Lab 29 Mechatronics Lab 30

5 Machine Design Machine Design

DSPLib …from the manual…

 If the DSPLib library is used, the user does not have to bother how it’s implemented in HW, doesn’t  The Atmel AVR32 DSPLib is a library even have to know if it’s which provides a useful set of digital implemented or not. signal processing functions  The result can be seen after compilation, in the number of cycles it requires.

Mechatronics Lab 31 Mechatronics Lab 32

Machine Design Machine Design Benchmarking experiments – math.h First – consider the alternatives

float x, z = 12.3; float x, z = 12.3;  math.h (standard C library) while(1){ while(1){  Implements, in SW, functions such LED_Toggle(1); LED_Toggle(1); as for(i=0;i<=10;i++){ for(i=0;i<=1000;i++){  Sin() x = sqrt(z); x = sqrt(12.3);  Pow(x, y) } } } }

Mechatronics Lab 33 Mechatronics Lab 34

Machine Design Machine Design Benchmarking experiments – math.h Math.h

float x, z = 12.3; float x, z = 12.3; while(1){ while(1){ LED_Toggle(1); LED_Toggle(1);  With math.h, SQRT() takes about 180 cycles for(i=0;i<=10;i++){ for(i=0;i<=1500;i++){ x = sin(z); x = sin(12.3); } } } }

Mechatronics Lab 35 Mechatronics Lab 36

6 Machine Design Machine Design

Math.h DSPLib (instead of math.h)

 With math.h, SIN() takes about 150

cycles

Mechatronics Lab 37 Mechatronics Lab 38

Machine Design Machine Design

Fixed Point Arithmetics DSPLib definition

 Allow any fixedpoint QFormatted numbers.  16bit and 32bit data are supported.  In computing , a fixed-point number  Is compatible with any platforms that are representation is a real for a supported by GCC and IAR. number that has a fixed number of digits  Can be customized with several algorithm after (and sometimes also before) the radix optimization options according to your needs. point (e.g. , after the decimal point '.' in  The DSPLib architecture is composed of 6 sub English decimal notation). Fixedpoint modules: filtering , operators , signal generation , number representation can be compared to transforms , vectors and windowing . the more complicated (and more  Lays out an advanced library with more specific computationally demanding) floating point applications. number representation.  Provides a useful set of debugging functions.

Mechatronics Lab 39 Mechatronics Lab 40

Machine Design Machine Design Benchmarking Representation With DSPlib With math.h

 A value of a fixedpoint data type is essentially an integer that is scaled by a  Sine/cosine: 48 cycles specific factor determined by the type. For (Q16) example, the value 1.23 can be represented  Sine/cosine: 150 as 1230 in a fixedpoint data type with  Absolute: 1 cycle cycles scaling factor of 1/1000, and the value  Square root: 61 cycles 1230000 can be represented as 1230 with a  Square root: 180 (Q16) scaling factor of 1000. Unlike floatingpoint cycles data types, the scaling factor is the same for  Exponential: 70 cycles all values of the same type, and does not (Q16) change during the entire computation.

Mechatronics Lab 41 Mechatronics Lab 42

7 Machine Design Machine Design

Mathematical operations Qformat

 Q16: 1 integer bit, 15 fractional bits 1. Scale value and convert to Qformat  Resolution = 2 15 approx 0.00003 (from float)

2. Use Fixed Point DSPoperator  Value from 1 to +0.99997 1. Sine(), square root(), multiplication() 3. Convert back from Qformat to float  Q16 sometimes referred to as Q1.15

Mechatronics Lab 43 Mechatronics Lab 44

Machine Design Machine Design

Conversion Conversion

Float to Q Q to Float To convert a number from floating To convert a number from Qm.n point to Qm.n format: format to floating point: Multiply the floating point number by Convert the number to floating point 2n as if it were an integer Round to the nearest integer Multiply by 2 −n

Mechatronics Lab 45 Mechatronics Lab 46

Machine Design Machine Design

Fixedpoint format Convert to Qformat

 All DSP types in the DSPLib includes the notion of 16bit and 32bit fixed point formats. It is important to dsp16_t a, b, c; understand this format in order to b=DSP16_Q(0.91); fastest and/or debug an application. See Q Format for more details on this format.

Mechatronics Lab 47 Mechatronics Lab 48

8 Machine Design Machine Design

Multiplication Multiplication

static dsp16_t dsp16_op_mul (dsp16_t num1, dsp16_t num2)

16bit fixed point version of the multiplication function.

Mechatronics Lab 49 Mechatronics Lab 50

Machine Design Machine Design

More advanced arithmetics

 Benchmarking  Addition addition of two 256point vectors: 1,252 cycles (41.7us @ 30MHz) for the 16bit version and 1,638 cycles (54.6us @ 30MHz) for the 32bit version.  Power raised to the power of one 256point vectors with a real number: 51,927 cycles (1.73ms @ 30MHz) for the 16bit version and 167,276 cycles (5.58ms @ 30MHz) for the 32bit version.

Mechatronics Lab 51 Mechatronics Lab 52

Machine Design

How to use math.h

Question How to use math.h in an AVR32 Studio Project? Answer •Here is the step by step procedure to apply for linking the math library to your project: Open the project properties. •Select C/C++ Build item > Settings > Tool Settings tab. •Select AVR32/GNU C Linker > Libraries. •Add a library: m as shown in attached picture; m is standind for math. •Do not forget to include math.h in your C file. •Build your project.

Mechatronics Lab 53

9