Simple Maths Going Wrong


Closed Thread
Results 1 to 40 of 44

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136

    Default Simple Maths Going Wrong

    I suspect the answer to my problem will be embarrasingly simple?

    I am using a rotary pot fixed to a wind vein to record wind direction.
    Assume that NORTH is 0 on the A/D reading [Maximum 255]

    I want to average, say two readings, to smooth the results:

    Reading 1 = 255, Reading 2 = 1, Average = 128 [ i.e SOUTH]
    Reading 1 = 128, Reading 2 = 128, Average = 128 [SOUTH again]

    Any ideas please? Sorry if this is an idiot question!

    Regards Bill legge

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bill Legge View Post
    I suspect the answer to my problem will be embarrasingly simple?

    I am using a rotary pot fixed to a wind vein to record wind direction.
    Assume that NORTH is 0 on the A/D reading [Maximum 255]

    I want to average, say two readings, to smooth the results:

    Reading 1 = 255, Reading 2 = 1, Average = 128 [ i.e SOUTH]
    Reading 1 = 128, Reading 2 = 128, Average = 128 [SOUTH again]

    Any ideas please? Sorry if this is an idiot question!

    Regards Bill legge
    Not an idiot question, the answer might be . . . .
    store the values in a word variable, i e reading = (reading + reading) / 2
    so reading1 = 255 + reading2 =256/2 = 128 . You can oversample several times to increase accuracy.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    Not an idiot question, the answer might be . . . .
    store the values in a word variable, i e reading = (reading + reading) / 2
    so reading1 = 255 + reading2 =256/2 = 128 . You can oversample several times to increase accuracy.
    That is the same thing he has? I think.
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    That is the same thing he has? I think.
    OK, except I think, thought. . . he was not adding them together in a <b>WORD</b> variable since byte only holds 255
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Take a peek at this thread, kind of interesting.
    http://www.picbasic.co.uk/forum/showthread.php?t=6650
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Hi Bill, the simple solution I can suggest is:

    A1=Reading1*100/average
    A2=Reading2*100/average

    If A1>A2 then
    If A1-A2>10 then discard
    else
    If A2-A1>10 then discard
    endif

    Naturally you can change the 10% threshold and lower it or increase it.

    Al.
    All progress began with an idea

  7. #7
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    654


    Did you find this post helpful? Yes | No

    Default

    Bill,

    If I understood your question correctly, when the A/D converter gives you a value of 0 that means pointing North and when you get a value of 128 that means pointing South (range is 0-255). Well, averaging a reading equal to 1 with a reading equal to 255 won't work. These two readings are close to 0 (North) but if you average then out you will get a value of 128 which is pointing South, the opposite direction.

    This problem is harder than what it looks, so don't think you are asking an idiot question. Right now I can't think of a way of averaging these two values using simple algebra or a simple algorithm. However, by using trigonometry these kinds of problems are very easy to do. Unfortunately, PBP does not offer too many trigonometric functions except sine and cosine.

    My suggestion,
    - Treat each reading as an angle (that's what they really are).
    - Use the PBP cosine function to add the X components of the angles (they can be as many as you want).
    - Use the PBP sine function to add the Y components of the angles.
    - Determine the new value of the new angle (new reading)

    I am very sorry if I confused you more than what I helped you, but if you need help let me know.

    Robert

  8. #8
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default Simple Maths

    First of all - a bit of relief that my difficulty is not in the 'daft' category.

    1. Thanks for pointing me to the earlier discussion. I see M's solution and agree that it works fine with two readings - but I can't see an easy way of doing it with an array; say 16 readings? And wind direction needs a good bit of averaging to get a good result.

    2. Effect of wind speed. I agree that wind direction is meaningless unless the wind is blowing. I have a threshold that discards direction reading unless there is a reasonable wind speed.

    3. Trignometric solution. thanks, a neat idea but how to get the an angle once all the SIN and COS have been summed/differenced? I don't think inverse trig functions are available?

    Overall, the task seems so visually simple that I still feel that some addition/subtraction of readings will do the trick. Perhaps it's as simple as:

    1. Use, say, the first vector as a reference (zero)
    2. Always measuring clockwise, add the angle to the next vector
    3. divide by the number of vectors
    4. Add this to the first vector
    5. Subtract 256 if necssary (i.e one revolution)

    The trick is to always measure angles in the same direction, say clockwise?

    Regards Bill Legge

  9. #9
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    654


    Did you find this post helpful? Yes | No

    Default

    Bill,

    Yes, you might be in the right track by measuring angles in the same direction. This might work for two angles, but if you need more than two readings it can get quite complex. Still you need to work out the algorithm and tested with different values.

    I know that in order to get the angle you need inverse trigonometric functions, inverse tangent to be specific which is not available in PBP (maybe in future versions). But, I didn't want to throw that in here just in case you were not familiar with these functions. However if you know the signs of the X and Y resultant components that will tell you in which quadrant the angle is and that is a big help.

    Note that the method of summing two readings and dividing by two (averaging) works most of the times. The only times that it doesn't work is when one of the readings value range is (0 < reading1 < 64) and the other value range is (192 < reading2 <= 255). When this happens you get bad results. Then, you might need to add or substract 128. Based on this info you should be able to write your algorithm.


    Robert

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Here's one that works real well.
    But it requires PBP 2.60 for the ATN function.

    The measured wind direction should be in the variable DIR.
    Then GOSUB Avg_Wind

    It works by averaging the SIN and COS separately for each sample.
    Then converts those averages back to an angle with ATN.
    Angles range from 0-255, representing 0-359 degrees.
    Code:
    CLEAR
    
    DIR   VAR BYTE   ; Current sample of wind direction (0-255)
    S     VAR BYTE   ; averaged SIN
    C     VAR BYTE   ; averaged COS
    Wind  VAR BYTE   ; final averaged wind direction
    Temp  VAR BYTE   ; temporary variable
    
    AvgCount  CON 10 ; amount of averaging to apply
    
    ;---------------------------------------------------------------
    Avg_Wind:    ; average in one DIR sample
      Temp = SIN DIR+127
      S=(S*(AvgCount-1)+Temp)/AvgCount
      Temp = COS DIR+127
      C=(C*(AvgCount-1)+Temp)/AvgCount
      Wind = (C-127) ATN (S-127)
    RETURN
    
    ;---------------------------------------------------------------
    Set_Wind:    ; Sets average wind direction without averaging
      S = SIN DIR + 127
      C = COS DIR + 127
    GOTO Avg_Wind
    If you don't have 2.60, the same thing can probably be done with scalerobotics cordic trig routines.

    HTH,
    Last edited by Darrel Taylor; - 22nd July 2009 at 16:08. Reason: Fixed a math overflow prob
    DT

  11. #11
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Not so simple math...

    edit, I had a convoluted solution, but I have deleted it after seeing Darrel's elegant solution. Nice!
    Last edited by ScaleRobotics; - 22nd July 2009 at 16:35.
    http://www.scalerobotics.com

  12. #12
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Hi Darell, could you explain me how you get the average?

    Temp = SIN DIR+127 here variable Temp is equal to S since S=SIN DIR+127

    Than

    S=(S*(AvgCount-1)+Temp)/AvgCount

    which is:

    S=(S*9+S)/10

    So S is still S

    I must have lost something.

    Al.
    Last edited by aratti; - 22nd July 2009 at 18:46.
    All progress began with an idea

  13. #13
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    The results of the SIN and COS functions are a Signed number ranging from -127 to +127.
    PBPW can't multiply or divide negative numbers, so they have to be scaled up to be worked with.

    Since Temp is a Byte sized variable, adding 127 brings it up to a range of 0 to 254, which can then be averaged easily.

    Then after averaging, 127 is subtracted to restore the signed numbers for use with ATN.
    <hr>
    For the averaging ...

    Code:
    S=(S*9+Temp)/10  ; Temp is the new corrected sample
    The current average is multiplied by one less than the AvgCount, the new sample is added in for a total of 10 samples. Then it divides by 10 (AvgCount) to get the new average. No WORDs needed.
    <br>
    DT

  14. #14
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    654


    Did you find this post helpful? Yes | No

    Default

    The new ATN function that Darrel mentions most likely stands for arctangent, which is the inverse tangent. It looks like this new PBP version is going to be loaded with a bunch of new goodies.

    Robert

  15. #15
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rsocor01 View Post
    The new ATN function that Darrel mentions most likely stands for arctangent, which is the inverse tangent. It looks like this new PBP version is going to be loaded with a bunch of new goodies.

    Robert
    Yeah, I think it's more than fresh paint and new chrome bumpers, that's why I ordered mine yesterday. It's got some new array handling routines in there I am chompin' at the bit to try out.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  16. #16
    Join Date
    Jun 2005
    Location
    West Australia
    Posts
    116


    Did you find this post helpful? Yes | No

    Default DT routine - Direction better than 8 bit?

    Hi Folks,

    Mainly for Darrel but happy to have input from all!

    DT's example of averaging the direction (thanks Darrel) makes use of ATN from an 8 bit direction (0-255 = 0-359 degrees) and works well when I use this method with PBPro 2.60.

    However this gives some inaccuracies and low resolution. I've acquired my direction with a 10 bit AD and converted down to 0-359 but no matter how I manipulate the numbers in DT's formula I get to about 270 deg's and then it rolls over with false values.

    Is there a way to achieve the above or am I stuck with coarse direction? I'd like to have 1 degree changes if possible not the jumps 8 bit has.

    Kind regards to all and thanks in advance,
    Bill

  17. #17
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,942


    Did you find this post helpful? Yes | No

    Default

    As you have no code posted I can only guess that your vaariable are byte and not word.

    Ioannis

  18. #18
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by wjsmarine View Post
    ... makes use of ATN from an 8 bit direction (0-255 = 0-359 degrees) ...
    ... with a 10 bit AD and converted down to 0-359
    Shouldn't it be converted to 0-255 (representing 0-359)?

    ATN SIN and COS all use binary radians.
    <br>
    DT

  19. #19
    Join Date
    Jun 2005
    Location
    West Australia
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Better Direction resolution?

    Hi Darrel and Ioannis,

    Thanks for the help. I replied earlier but it didn't seem to make it, here goes again...

    Ioannis the first thing I did when trying changes was to move to Words for the variables so was surprised when the numbers didn't crunch.

    Darrel your code works well but gives 360/256 = 1.4 degree accuracy/resolution and I was hoping for better than this. I'm not strong at math so your comment about binary radians probably means I'm stuck with the original limits, which is probably okay for wind direction - but what do I do if I need greater accuracy for Tilt, etc?

    Any suggestions and help is appreciated, thanks guys.
    Bill

  20. #20
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    If you need more resolution, you'll need to pull out the Big Guns ...

    Cordic trig assembly code for PIC18f (scalerobotics)
    http://www.picbasic.co.uk/forum/showthread.php?t=10528
    <br>
    DT

Similar Threads

  1. Simple RF remote control code
    By Bruce in forum Code Examples
    Replies: 13
    Last Post: - 22nd January 2014, 10:45
  2. Simple Blinking LED - WTF!!
    By johnnylynx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st February 2010, 06:19
  3. Simple LCD code not working!...WHY?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th November 2009, 19:48
  4. what's wrong 16F877A simple code?
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th October 2009, 01:11
  5. Doing Simple Math - getting the wrong answer
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2005, 14:27

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts