Yah, the root routine is brute force. But in its defense I have to say it works. It usually converges in 6 or 7 iterations. Maybe someone on the list will extend Scott's algorithm to 24 and 32 bits. What I ment to post, and where my interest is, is the conversion from A/D pitot pressure counts to velocity. Somewhere I'm getting this 20% error. It looks like pure physics to me - the calculation is trivial once you've looked up all the constants. So where is the error comming from? Andy can avoid the square root if he doesn't have to display airspeed. Just square the target speed and do the speed loop comparisons using the squared numbers. Regards, Jim Ham At 09:10 PM 5/10/2000 -0500, you wrote: >On Wed, 10 May 2000, Jim Ham wrote: > >> The routine below works mostly OK - it seems to have about a 20% error. If >> anyone sees the problem please drop me a note. > >Yeah the problem is that the PIC is getting bored taking such a long time >calculating the square root and is forgetting the answer before the algorithm >completes :). But seriously, if you need a square root routine I'd suggest >looking at James' pic pages (what's the latest URL James?) or > >http://www.dattalo.com/technical/software/pic/picsqrt.html > >I've haven't been paying attention to the thread so I'm not sure what your >equation looks like. > >Scott > >> >> >> /***********************************************************************\ >> * >> * return indicated airspeed (kts)(assume std density at sea level >> * also assume 80mv FS in A2D >> * returns airspeed in 10ths of knots >> * >> \***********************************************************************/ >> int SpdLookup( ulong raw ) >> { >> /* >> return (int)sqrt( raw * 200 )/10 ; >> */ >> >> byte iteration ; >> unsigned long guess ; >> unsigned long last_guess ; >> >> // 1 psi = 2.13e6 counts from A/D >> // rho 2.378e-3 slugs/cu.ft - density of air at sea level >> // p(psf) = 1/2 rho(slugs/cu.ft) V^2(ft/sec) >> // p(psf) = raw / 14789 ; >> // V(ft/sec) = sqrt( 2 * p /rho ) >> // 1 kt = 0.5925 ft/sec >> // V(kts) = 0.5925*sqrt( 2 * p /rho ) >> // V(kts*100) = 59.25*sqrt( 2 * p /rho ) >> // V(kts*100) = sqrt( (59.25^2*2*raw) /(2.378e-3*14.789e3) ) >> raw *= 200ul ; >> //*********************************************************************** >> //* >> //* Take a square root - Newton's method >> //* >> //*********************************************************************** >> iteration = 20 ; >> last_guess = guess = raw / 128 ; >> do { >> if (guess == 0l ) guess = 1l ; >> guess += (raw / guess) ; >> guess /= 2 ; >> if (last_guess == guess) break ; >> last_guess = guess ; >> } while ( --iteration ) ; >> return MakeInt(guess/10l) ; >> } >> Jim Ham, Porcine Associates (650)326-2669 fax(650)326-1071 "http://www.porcine.com"