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.
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 pairs16 to 23 Stores the next pairing. 4 pairs24 to 27 Stores the next pairing. 2 pairs28 to 29 Stores the last pair. 1 pair30 Stores the result2. 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
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
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
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
WOW! I really got that interpretation wrong didn't I?
OK, I think I understand what you're doing.
I'll play with that a bit and see what happens.
Thanks for explaining.
<br>
DT
I'm thinking you might have a winner there Bill.
But I have a question or two.
I've had another go at your code and am getting some promising results.
It's hard to follow with a calculator, so I made an interface to help test the routines.
Also, instead of reading/writing to EEPROM, I've modified your routine to use an array, so that I can pass it data from the PC.
The array is a circular buffer that overwrites the oldest value with the new sample, and on each iteration runs your code to average all 16 samples in the array.
I'm thinking that the circular buffer might be a problem.
Maybe if it only took 16 samples then averaged ???
The modified routine looks like this ...The interface simply sends the Wind Vane readings to the PIC where it runs both of our averaging codes, and sends the results back for display.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 BillsData VAR BYTE[32] DataPos VAR BYTE Bill_Avg: DataPos = DataPos + 1 IF DataPos = 16 THEN DataPos = 0 BillsData(DataPos) = DIR ;DATA 250,230,240,34,200,10,10,23,23,21,125,0,34,255,123 ' 16 vectors FOR J = 0 TO 28 STEP 2 ' Read pairs of vectors ; READ J, X X = BillsData(J) ; READ J+1,Y Y = BillsData(J+1) 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 BillsData(J/2 + 16) = Average NEXT J Bill_Value = Average ; LCDOUT $fe,$80,"Average: ",DEC3 Average RETURN
Yours is on the right. Any thoughts on the sample buffer?
It works great unless there are large fast changes. (click image to play)
<object id='stUkhdQ01IR11XRFVbWlleU19R' width='711' height='444' type='application/x-shockwave-flash' data='http://www.screentoaster.com/swf/STPlayer.swf' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0'><param name='movie' value='http://www.screentoaster.com/swf/STPlayer.swf'/><param name='allowFullScreen' value='true'/><param name='allowScriptAccess' value='always'/><param name='flashvars' value='video=stUkhdQ01IR11XRFVbWlleU19R'/></object>
Sorry Internet Explorer only.![]()
DT
Darrel,
Thanks again for your investigation. The display 'InstruMental' is really good.
I guess the issue of:
1. Adding one new vector and then running the 'Averaging Tree' with 15 older vectors. Or...
2. Waiting till 16 new vectors are available and then running the 'Averaging Tree'
Is one of latency. And the selection should be based on:
1. The importance of immediate results.
2. The time interval between the availability of new vectors.
3. The need for the MCU to do other things.
I have attached some code that I used to send the averages to my PC with the screen presentation laid out so one can easily see, and check, the averages. The vectors come from a RANDOM function that loads up the PIC EEPROM. RS232 at 19,200 Baud on pin C.6. Loops continuosely.
Two questions for you please:
1. How does one post PBP code here so that it looks neat/tidy. My code looks OK on my PC but when I post it here it is a mess?
2. Is your 'InstruMental' writtern in Visual Basic? I want to learn this and recently got the book 'VISUAL BASIC FOR ELECTRONICS ENGINEERING APPLICATIONS' by Vincent Himpe. For me (An ex-electronics engineer) it's no good - starts off too advanced. Any advice on a good 'entry level' book?
Regards Bill Legge
Code:' -----[ Title ]------------------------------------------------- ' ' File...... WVL 877 VERTOR AVERAGE ' Purpose... Experiment with PIC16F877A ' Date...... July 2009 ' ' -----[ Program Description ]--------------------------- ' ' 1. Devices used: PIC16F877A on EasyPIC5 ' ' ----[ Includes / Defines ]----------------------------------------- ' define OSC 20 ' Define Xtal as 20 MHz. Use "HS" for programming ' include "modedefs.bas" ' Include serout defines ' ' -----[ Initialization ]-------------------------------------------- ' ADCON1 = %00001110 ' A0 is analog, remainder digital CMCON = %00000111 ' Comparators OFF INTCON = $80 ' Turns off interrupts. See PBP page 187 TRISA = %00000001 ' A0 is A/D input TRISB=0 : TRISC=0 : TRISD=0 : TRISE=0 PORTA=0 : PORTB=0 : PORTC=0 : PORTD=0 : PORTE=0 ' ' ----[ Variables ]-------------------------------------------------- ' X VAR BYTE ' First vector READ Y VAR byte ' Second vector READ Vector var byte[31] ' Stores 16 A/D readings of wind direction and averages Difference var byte ' Difference between vectors, always + Average var word ' Average of two vectors J var byte ' EEPROM memory address K var word Memory var byte ' Contents of EEPROM address J TxPin var PORTC.6 Heartbeat var PORTC.0 ' '-----[ Main ]------------------------------------------------------- ' Main: for J = 0 to 15 ' Create 16 random numbers 1 to 255 random K write J, K.lowbyte ' Load EEPROM with random numbers next J 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 for J = 0 to 30 ' Send to PC at 19,200 Baud read J, Vector[J] next J serout2 TxPin,32,["VECTOR ADDITION",13,13] serout2 TxPin,32,[dec3 Vector[0],9,dec3 Vector[16],9,dec3 Vector[24],9,dec3 Vector[28],9,dec3 Vector[30],13] serout2 TxPin,32,[dec3 Vector[1],13] serout2 TxPin,32,[dec3 Vector[2],9,dec3 Vector[17],13] serout2 TxPin,32,[dec3 Vector[3],13] serout2 TxPin,32,[dec3 Vector[4],9,dec3 Vector[18],9,dec3 Vector[25],13] serout2 TxPin,32,[dec3 Vector[5],13] serout2 TxPin,32,[dec3 Vector[6],9,dec3 Vector[19],13] serout2 TxPin,32,[dec3 Vector[7],13] serout2 TxPin,32,[dec3 Vector[8],9,dec3 Vector[20],9,dec3 Vector[26],9,dec3 Vector[29],13] serout2 TxPin,32,[dec3 Vector[9],13] serout2 TxPin,32,[dec3 Vector[10],9,dec3 Vector[21],13] serout2 TxPin,32,[dec3 Vector[11],13] serout2 TxPin,32,[dec3 Vector[12],9,dec3 Vector[22],9,dec3 Vector[27],13] serout2 TxPin,32,[dec3 Vector[13],13] serout2 TxPin,32,[dec3 Vector[14],9,dec3 Vector[23],13] serout2 TxPin,32,[dec3 Vector[15],13,13,13] goto Main END
Bookmarks