Simple Maths Going Wrong


Closed Thread
Results 1 to 40 of 44

Hybrid View

  1. #1
    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

  2. #2
    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

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


    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

  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 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.

  5. #5
    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

    I think I've solved it for an array of 16 readings? The readings

    1. A/D from a potentiometer
    2. Range is 0 to 255
    3. 0 is North, 64 East, 128 South,192 West

    The rules:

    1. Always work in pairs of vectors. Using EEPROM to store the vectors:
    0 to 15 Stores the vectors. 8 pairs
    16 to 23 Stores the next pairing. 4 pairs
    24 to 27 Stores the next pairing. 2 pairs
    28 to 29 Stores the last pair. 1 pair
    30 Stores the result
    2. If the difference between the vector pairs if <128 then average the vectors normally
    3. If the difference between the vectors >128 then average normally, add 128, check for whole revolutions

    I've run a fair number of tests and all seems to be OK?

    Regards Bill Legge

    Code:
    X  VAR BYTE                ' First vector
    Y  VAR BYTE                ' Second vector
    Difference  VAR BYTE     ' Difference between vectors, always +
    Average  VAR WORD      ' Average of two vectors
    J  VAR BYTE                ' EEPROM memory address
    
    DATA 250,230,240,34,200,10,10,23,23,21,125,0,34,255,123  ' 16 vectors	
    
    Main:
    FOR J = 0 TO 28 STEP 2	' Read pairs of vectors
    	READ J,  X
    	READ J+1,Y
    	if Y>X THEN SWAP X,Y	' X is always the larger variable
    				
    	Difference	= X-Y		
    	Average	= (Difference/2) + Y' Half difference and add to smaller vector
    		 	
    	IF Difference>128 then					
    	Average=Average+128	' Reverse direction of vector
    	IF Average>255 THEN Average=Average-255	' Whole revolution? Subtract 255 if necessary
    	ENDIF
    	
    	WRITE J/2 + 16,Average		
    	NEXT J
    	
    	LCDOUT $fe,$80,"Average: ",DEC3 Average	
    END

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


    Did you find this post helpful? Yes | No

    Default

    Darell, I think that the problem given in post one still remain even with adding 1/10 of the variation (with your smooting system) because the variation are so big (1 to 255) that will making swinging also your reading if not filtered.

    As far this new release of PBP 2.6 is concerned can you please clearify this trigonometric manipulation:

    Wind = (C-127) ATN (S-127) = cos(a) (no math sign ?) ATN sin(a)

    I know that tan(a) = sin(a)/cos(a) so (a) = atn (sin(a)/cos(a))

    Is this the way that the new PBP use to extract the angle?

    Thank you for your help in understanding.

    Al.
    Last edited by aratti; - 23rd July 2009 at 07:41.
    All progress began with an idea

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


    Did you find this post helpful? Yes | No

    Default

    Hmm, I don't know Bill.
    I've had a play with your code, and I'm not sure about it.

    It does seem to pick a valid point in-between two samples. But it only averages that point, and the previous point found between two other samples. It's not averaging all the samples, so it's still very jumpy.

    Your explanation describes EEPROM locations up to 29, but there are only 15 DATA values, although I don't think that affects the problem.

    I've modified the program a bit to use the Average as the X value, then read the samples one at a time, also connected a POT to simulate the wind vane, but I get the same results.

    Another hiccup, if the average is currently 10, then it receives a string of 255 samples, it stops at 1, and never crosses 0. And for many other samples, it stops 1 number away from the actual reading. 250 might stop at 249 or 251.

    I think finding a point in-between 2 samples is fairly easy. But averaging a large number of samples is something altogether different, and a 2-sample average just doesn't do it.

    Aratti,
    The ATN function is actually an ATAN2 type since it works for all four quadrants. An arctangent would only work with 1 quadrant.

    The format of the statement is ...

    Result = X ATN Y

    There aren't any other "math signs" involved.
    <br>
    DT

  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

    DT. Thanks for taking time to look into the code.

    First, I wrote it specifically to average 16 samples - it won't work with any other number because of the counter:

    J/2 + 16

    This counter takes the average (or average+128 if the two sample are more that 128 apart - and checks for a 'roll-over' above 255) and stores them:

    Samples 0 and 1 average stored in address 16
    Samples 2 and 3 average stored in address 17
    Samples 4 and 5 average stored in address 18
    Samples 6 and 7 average stored in address 19
    Samples 8 and 9 average stored in address 20
    Samples 10 and 11 average stored in address 21
    Samples 12 and 13 average stored in address 22
    Samples 14 and 15 average stored in address 23

    Then, the same J/2 + 16 deals with the next round of averages:

    Averages of 16 and 17 stored in address 24
    Averages of 18 and 19 stored in address 25
    Averages of 20 and 21 stored in address 26
    Avergaes of 22 and 23 stored in address 27

    And again

    Averages of 24 and 25 stored in address 28
    Averages of 26 and 27 stored in address 29

    And finally

    Averages of 28 and 29 stored in address 30

    I guess the same could be done for other numbers of samples but 16 seems a reasonable number for wind direction and the code is short and simple. I have spent a laborious few hours with a pocket calculator checking the results - and so far it is Ok. If you, or anyone else spots an error please let me know.

    Regards Bill Legge

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