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


    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

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    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

  3. #3
    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 17:08. Reason: Fixed a math overflow prob
    DT

Similar Threads

  1. Simple RF remote control code
    By Bruce in forum Code Examples
    Replies: 13
    Last Post: - 22nd January 2014, 11:45
  2. Simple Blinking LED - WTF!!
    By johnnylynx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st February 2010, 07:19
  3. Simple LCD code not working!...WHY?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th November 2009, 20:48
  4. what's wrong 16F877A simple code?
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th October 2009, 02: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, 15: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