; updates the wind speed in wind
; units are cm/sec.
; Uses: w0, w1, w2
; if there's a timeout the wind value is 0
; this will happen for wind speeds less than 0.28 m/sec
symbol WIND_FACTOR = 7500 ; 1 rps = 0.75 m/sec.
readWind:
; get the time for 1 rotation in 100s of usec
; takes two rotations to get this.
; read two consecutive high pulses
w1 = 0
pulsin windPin, 1, w0 ; in 10s of usec
#ifdef DEBUG
sertxd ("wind,",#w0)
#endif
if w0 = 0 then endWind
w1 = w0/10 ; in 100s of usec
pulsin windPin, 1, w0
#ifdef DEBUG
sertxd (",",#w0)
#endif
if w0 = 0 then endWind
w1 = w0/10 + w1
; read two consecutive low pulses
pulsin windPin, 0, w0
#ifdef DEBUG
sertxd (",",#w0)
#endif
if w0 = 0 then endWind
w1 = w0/10 + w1
pulsin windPin, 0, w0
#ifdef DEBUG
sertxd (",",#w0)
#endif
if w0 = 0 then endWind
w1 = w0/10 + w1
; w1 contains the time for one rotation in 100s of us
; convert to ms
w1 = w1 / 10
; convert to speed
getSpeed:
w0 = WIND_FACTOR / w1
w0 = w0 * 10 ; wind in cm/sec, units are 0
; get the units
w2 = WIND_FACTOR // w1 * 10 ; remainder * 10
w2 = w2 / w1 ; units
w0 = w0 + w2
; the wind speed, in cm/sec, is in w0,
; this is either converted from the time
; for a revolution or 0 if one of the pulses timed out
endWind:
w2 = w0 - wind
if w2 > 0x7fff then
w2 = -w2
endif
if w2 > 20 then ; change > 0.2 m/s
wind = w0
updateRequired = updateRequired or windChanged
endif
#ifdef DEBUG
sertxd (",TFR: ", #w1, ", Wind: ")
call printF2
sertxd(cr,lf)
#endif
return
#ifdef TEST
testWind:
w1 = 100
call getSpeed ; expect 7.50
w1 = 200
call getSpeed ; expect 3.75
w1 = 400
call getSpeed ; expect 1.87
w1 = 800
call getSpeed ; expect 0.93
w1 = 900
call getSpeed ; expect 0.83
w1 = 1000
call getSpeed ; expect 0.75
w1 = 1100
call getSpeed ; expect 0.68
w1 = 1500
call getSpeed ; expect 0.50
w1 = 2000
call getSpeed ; expect 0.37
w1 = 2600
call getSpeed ; expect 0.28
w1 = 2700
call getSpeed ; expect 0.27
return
#endif