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,
Bookmarks