' QBASIC sonar scanner. Steps motor through 180 degrees and plots ' points corresponding to echos in a DOS box. RUN THIS IN DOS MODE, ' not in a Windoze DOS BOX! This version stores up to 200 echos ' (about 16 max for each pulse packet) ' the timing loops will have to be adjusted for processor speed. ' the present version runs well on a 466 MHz machine DIM r(200): maxn = 200 'multiple echo counter DIM s%(8)'bit pattern for stepper driver REM Choose from the following motor options: Two-phase (default) REM Half-step doubles resolution but lowers motor force REM Wave lowers power requirements and lowers motor force s%(1) = 12: s%(2) = 6: s%(3) = 3: s%(4) = 9: REM two-phase s%(5) = 12: s%(6) = 6: s%(7) = 3: s%(8) = 9: REM 1.8 degrees/step REM s%(1) = 9: s%(2) = 8: s%(3) = 12: s%(4) = 4: REM half-step REM s%(5) = 6: s%(6) = 2: s%(7) = 3: s%(8) = 1: REM 0.9 degrees/step REM s%(1) = 8: s%(2) = 4: s%(3) = 2: s%(4) = 1: REM wave pattern REM s%(5) = 8: s%(6) = 4: s%(7) = 2: s%(8) = 1: REM saves power h% = 128: p% = &H378: st% = &H379: nstep = 1 'port addresses FOR i = 1 TO 8: s%(i) = s%(i) * 16: NEXT i 'move motor bits to top nibble SCREEN 11 LINE (0, 0)-(639, 479), 1, B 'frame cx = 320: cy = 440 'scanner screen position REM set some constants that depend on CPU speed. 1 speed = 20000 ' on a 466 Mhz PC. this sets sample rate scalep = 7 'screen radial distance scale factor maxd = 4000 'ECHO wait loop timeout: max distance possible ' cx,cy = scanner position on graphics screen. Plot a box LINE (cx - 1, cy - 1)-(cx + 1, cy + 1), 1, B FOR js = -50 TO 50 'step through 180 degrees in 1.8 degree steps GOSUB 100 'step GOSUB 300 'wait till scanner stops FOR n = 1 TO maxn: r(n) = 0: NEXT n 'clear echo counters n = 0 OUT p%, ps% + 1 'turn on INIT FOR i = 1 TO maxd 'loop waiting for echo 'maxd is the timeout default IF (INP(st%) AND h%) = 0 THEN 'got ECHO, store counter OUT p%, ps% + 3 'BLNK echo n = n + 1: r(n) = i: IF n = maxn THEN 35 OUT p%, ps% + 1 'release BLNK and listen again END IF NEXT i 35 maxj = n: OUT p%, ps% ' turn off INIT ' ' Plot echos on screen at the proper distance from (cx,cy) along ' the line corresponding to the current detector angle. ' The scale factor scalep depends on processor speed and sets the screen ' scale, i.e. radial distance from origin is "scalep" loop increments/pixel ' FOR j = 1 TO maxj rad = INT(r(j) / scalep + .5) 'scale radial range to fit screen th = js * 3.14159 / 100 ' -pi/2 to pi/2 wrt vertical rx = rad * COS(th): ry = rad * SIN(th) xx = cx + ry: yy = cy - rx LINE (xx - 1, yy - 1)-(xx + 1, yy + 1), 1, B 'small box at echo loc NEXT j NEXT js 'continue scan REM rewind scanner speed = speed / 4 ' motor skips steps if faster FOR js = -50 TO 50 GOSUB 200 GOSUB 300 NEXT js REM wait for key input 95 IF INKEY$ = "" THEN 95 CLS 1 'clear screen GOTO 1 'repeat REM step cw 100 OUT p%, s%(nstep): REM cw ps% = s%(nstep) nstep = nstep + 1: IF nstep > 8 THEN nstep = 1 RETURN REM step ccw 200 nstep = nstep - 1: IF nstep < 1 THEN nstep = 8 ps% = s%(nstep) OUT p%, s%(nstep) RETURN REM wait for step to complete 300 FOR nn = 1 TO speed NEXT nn RETURN