Download presentation

Presentation is loading. Please wait.

Published byAidan Harrington Modified over 3 years ago

1
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 1 NUM 1019 Numerical Methods on the PIC Micro

2
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 2 Class Objective When you finish this class you will: Know the basics of numerical methods as applied to embedded systems Know what the PIC micro can really do See a complex mechatronics demo Know where to find additional design resources

3
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 3 Agenda l What is numerical methods? l The basics l Trigonometry l Finding the square root l Dynamic systemsIntroduction The Basics Trigonometry Finding the square root Dynamic systems

4
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 4 What is Numerical Methods?

5
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 5 Introduction l Numerical methods use numbers to simulate mathematical processes (The world around us)Introduction The Basics Trigonometry Finding the square root Dynamic systems

6
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 6 Introduction l The Purpose of Computing Is Insight, Not Numbers –R.W. Hamming l Computers are for ComputingIntroduction The Basics Trigonometry Finding the square root Dynamic systems

7
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 7 The Basics

8
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 8 Numbers l The three number systems Counting numbers l 0,1,2,…,32,767 Fixed point l , , Floating point l x 10 4Introduction The Basics Trigonometry Finding the square root Dynamic systems

9
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 9 Fixed Point l l Fixed point numbers represent whole numbers and fractions l l 10.4 can also be represented as 104 when scaled by 10 (decimal shifted once right) l l Computations can be done on whole numbers as long as we remember where to put the decimal in the end (called the radix point)Introduction The Basics Trigonometry Finding the square root Dynamic systems

10
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 10 Fixed Point l l The same applies to binary numbers often referred to as the Qx.x format or Qi.j l l i and j are the number of bits to the left and right of the decimal point respectively. Ex) Q8.4Introduction The Basics Trigonometry Finding the square root Dynamic systems

11
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 11 Fixed Point l l Signed-magnitude format for negative numbers First bit is used as a sign bit ex) = -19 l l 2s complement is most often used for fixed point negative numbers Complement +1 ex) 9 = = -9Introduction The Basics Trigonometry Finding the square root Dynamic systems

12
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 12 l Floating Point Notation: Sign * (Mantissa) * 2 exponent = l l Fractional component stored in signed-magnitude format and is in binary form Floating PointIntroduction The Basics Trigonometry Finding the square root Dynamic systems

13
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 13 Floating Point l l Exponent stored in biased format This makes number comparisons easier l l Floating point number stored as:Introduction The Basics Trigonometry Finding the square root Dynamic systems

14
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 14 l l Good for large and small numbers (10 38, ) l l Number system is not linear, distance between numbers jumps l l Using Floating point numbers for counting and/or logical decision making is generally a bad idea Floating PointIntroduction The Basics Trigonometry Finding the square root Dynamic systems

15
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 15 Fixed Float l l Example: 1) Find exponent: 2) Find fractional component :Introduction The Basics Trigonometry Finding the square root Dynamic systems

16
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 16 Fixed Float 3) Find binary number:Introduction The Basics Trigonometry Finding the square root Dynamic systems

17
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 17 Fixed Float 4) Convert to Microchip float format eb= Biased Exponent l l Microchip Float Format:Introduction The Basics Trigonometry Finding the square root Dynamic systems

18
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 18 Bases l l We typically do calculations in decimal (base 10) 130 ten = (1 * 10 2 ) + (3 * 10 1 ) + (0 * 10 0 ) l l Machines only understand 1s and 0s so they use binary (base 2) 130 ten = (1*2 7 )+(0*2 6 )+(0*2 5 )+(0*2 4 )+(0*2 3 )+(0*2 2 )+(1*2 1 )+(0*2 0 ) = two l l Hexadecimal (base 16) serves as a shorthand for binary numbers two = (8*16 1 ) + (2 * 16 0 ) = 82 sixteen (0x82)Introduction The Basics Trigonometry Finding the square root Dynamic systems

19
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 19 Bases l l Dec BinIntroduction The Basics Trigonometry Finding the square root Dynamic systems l l Fraction Bin

20
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 20 Bases l l Watch out for non- terminating numbers l l Never enough precision l l Try it:Introduction The Basics Trigonometry Finding the square root Dynamic systems

21
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 21 Bases l l Making the Dec Bin Dec conversion doesnt always return the same result l l Example:Introduction The Basics Trigonometry Finding the square root Dynamic systems

22
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 22 Bases l l Do not confuse the kind of number system used (Counting, Fixed Point, Float) with the form of the number representation (Dec, Bin, Hex) l l (they are the same number)Introduction The Basics Trigonometry Finding the square root Dynamic systems

23
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 23 BCD l l Binary Coded Decimal (BCD) Counting to ten in binary l l Solves 0.1 approximation problem l l BCD arithmetic is sometimes required by law for financial applicationsIntroduction The Basics Trigonometry Finding the square root Dynamic systems

24
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 24 Four Functions l l Addition:Introduction The Basics Trigonometry Finding the square root Dynamic systems l l Subtraction:

25
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 25 Four FunctionsIntroduction The Basics Trigonometry Finding the square root Dynamic systems l l Division: l l Multiplication:

26
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 26 Multiplication l A trick that can speed up multiplication in software Round by inspection to allow shift and add instead of multiply l Use two or three shift and add routines instead of a multiply routine Need to multiply x(n) by (dec 149) (dec 144) => error = (dec 160) => error = +11x x(n)* x(n)<<7 + x(n)<<4 = x(n)*2 7 +x(n)*2 4 l To multiply y(n) by (dec 11) (dec 12) => error = (dec 10) => error = -1 In general round down for y(n)Introduction The Basics Trigonometry Finding the square root Dynamic systems

27
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 27 Evaluating l Horners method for a polynomial Evaluated: Used to convert thermocouple voltage to temperature (oC) over a wide range of temperatures.Introduction The Basics Trigonometry Finding the square root Dynamic systems

28
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 28 Evaluating l l Using unsigned coefficients is sometimes beneficial l l Subtract values with negative coefficients in algorithm l l For scaled coefficients between -128 and 127 convert these to 0 to 255 l l IIR digital filter example: Start with difference equation:Introduction The Basics Trigonometry Finding the square root Dynamic systems

29
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 29 Error analysis l Things to watch out for: Loss of significance when subtracting two nearly equal numbers Example with algebraically equivalent expressions:Introduction The Basics Trigonometry Finding the square root Dynamic systems

30
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 30 Error analysis l Two kinds of error, Round-Off and Truncation l Round-Off is a result of a fixed precision (machine dependant)Introduction The Basics Trigonometry Finding the square root Dynamic systems

31
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 31 Error analysis l Round-off example:Introduction The Basics Trigonometry Finding the square root Dynamic systems

32
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 32 Error analysis l Round-off example:Introduction The Basics Trigonometry Finding the square root Dynamic systems

33
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 33 Error analysis l What is the difference between Chop-off and Round-off? l Chop-off will introduce unnecessary bias errors l Round-off minimizes biasIntroduction The Basics Trigonometry Finding the square root Dynamic systems

34
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 34 Error analysis l Truncation error is the result of implementing a finite number of steps of an infinite step sequence (algorithm dependant) l For example, using a fixed number of terms in a Taylor expansion See hand out for further information on Taylor approximationsIntroduction The Basics Trigonometry Finding the square root Dynamic systems

35
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 35 Trigonometry

36
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 36 Trigonometry l Taylor approximation for sin(x) of degree 9, P 9 (x) about x 0 = 0Introduction The Basics Trigonometry Finding the square root Dynamic systems

37
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 37 CORDIC Transform l Coordinate Rotation Digital Computer An iterative algorithm used for calculating trig functions l The CORDIC Transform only works for angles between ±90 oIntroduction The Basics Trigonometry Finding the square root Dynamic systems

38
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 38 Trigonometry l How does it work? Rotates the phase of a complex number, by multiplying it by a succession of constant values The multiplier used is in 2, so in binary arithmetic we will use the shift command See handout for more detail on the derivationIntroduction The Basics Trigonometry Finding the square root Dynamic systems

39
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 39 Trigonometry l Range finders have been around for a long time l They used right triangles to find distancesIntroduction The Basics Trigonometry Finding the square root Dynamic systems

40
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 40 Trigonometry l Lets make a laser range finder! l Our setup: l Use law of Sines to find L:Introduction The Basics Trigonometry Finding the square root Dynamic systems Line up the laser pointers! AB c L b a C

41
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 41 Trigonometry l What is the error using CORDIC SIN(X)? l What is the error using Taylor approximation to SIN(X)?Introduction The Basics Trigonometry Finding the square root Dynamic systems

42
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 42 Finding the square root

43
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 43 Square Root l Fixed point Newton-Raphson method (AN526)Introduction The Basics Trigonometry Finding the square root Dynamic systems

44
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 44 Square Root l MINIMAX floating point approximation 1) Limit your input rangeIntroduction The Basics Trigonometry Finding the square root Dynamic systems

45
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 45 Square Root l MINIMAX floating point approximation 2) Find the best linear solutionIntroduction The Basics Trigonometry Finding the square root Dynamic systems Sqr(2) 12 Same maximum error at mid-point and both end-points

46
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 46 Square Root l Fast integer square root (TB040)Introduction The Basics Trigonometry Finding the square root Dynamic systems

47
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 47 Dynamic Systems

48
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 48 Differentiation l Backward difference three-point formula M = maximum value for the 4 th derivative Find a step size h to minimize error E 3Introduction The Basics Trigonometry Finding the square root Dynamic systems

49
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 49 Integration l Simpsons 3/8 th ruleIntroduction The Basics Trigonometry Finding the square root Dynamic systems

50
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 50 Dynamic simulation l World of differential equationsIntroduction The Basics Trigonometry Finding the square root Dynamic systems

51
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 51 PIC Ballistics l Lets play the PIC Ballistics demo! Calculate a trajectory for a given initial velocity and angle Calculate the acceleration along trajectory using differentiation methodsIntroduction The Basics Trigonometry Finding the square root Dynamic systems

52
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 52 Summary l What is numerical methods? l The basics l Trigonometry l Finding the square root l Dynamic systems

53
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 53 References l AN670 Floating Point to ASCII conversion l TB040 Fast Integer Square root l AN526 PIC16 Math Utility Routines l AN544 Math Utility Routines l AN617 Fixed Point Routines l AN660 Floating Point Math Functions

54
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 54 References l German Optical Rangefinders by Peter Lienau l Fixed-Point Trigonometry With CORDIC Iterations By Ken Turkowski l A survey of CORDIC algorithms for FPGA based computers By Ray Andraka l Math Toolkit for Real-Time programming by Jack Crenshaw l Numerical Methods Using Matlab by Kurtis D. Fink l Numerical Methods for Scientists and Engineers by Richard Hamming

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google