'>Hextor10.BSx '============================================================== 'Milford Instruments http://www.milinst.com 'Hextor Design, Bug Commander - Behavioural Operating System 'and the Behavioural Controller programs by David Buckley '================================================================ '{$stamp BS2Sx} '============================================================== 'An improved method of specifying Utrasonic Sensor positions 'and corresonding action in the wander routine by using DATA. 'New test sequences can be used by simply altering the DATA 'statements rather then having to write program statements. 'The rules in the DATA are the same as used in Hextor_9.BSx. 'New '--- 'DATA moved to start of program, makes it easier to find ' 'Required knowledge: ' 'Constants: 'Variables: usRptr,usRrange,usaction 'Routine: uswander rewritten '============================================================== 'Understand the new routine 'uswander' and experiment with 'the tests in the DATA statements at 'usactions'. '============================================================== 'LCD commands. 'For more detailed information see the ILM-216 User Manual. 'Function ASCII '-------- ----- 'Null .................................... 0 'Cursor home ............................. 1 'Hide cursor ............................. 4 'Show underline cursor ................... 5 'Show blinking-block cursor .............. 6 'Bell (not implemented with Hextor LCD) .. 7 'Backspace ............................... 8 'Horezontal tab (4 columns) .............. 9 'Smart linefeed (cursor down one line) ... 10 'Vertical tab (cursor up one line) ....... 11 'Formfeed (clear screen) ................. 12 'Carriage return ......................... 13 'Backlight on ............................ 14 'Backlight off ........................... 15 'Accept cursor position entry ............ 16 ' 64 is the start of the first line ' 80 is the start of the second line 'Format right aligned text ............... 18 'Escape (ESC; start multipart instruction) 27 '==========LCD messages - finish each message with Null ======= dbtxt DATA 12,4,16,80,"back < > ok ",0 dbusy DATA 16,76,"busy",0 dm0 DATA 1,"OPTIONS ",0 dm1 DATA 1,"Wiggle 1 ",0 dm2 DATA 1,"Wiggle 2 ",0 dm3 DATA 1,"Wiggle 3 ",0 dm4 DATA 1,"Setting 1 ",0 dm5 DATA 1,"Setting 2 ",0 dm6 DATA 1,"US wander ",0 '============================================================== 'Sequences in DATA statments '-------------------------- wiggle1 DATA "UDFBCRLOBPMz" wiggle2 DATA "UFFBBz" wiggle3 DATA "UFRRLLBz" setting1 DATA "Qs",4,"r",170,"z" setting2 DATA "Ts",0,"r",200,"z" 'feet drag if not "T" 'Ultrasonic sensor '----------------- 'Sensor position 0=right 127=ahead 255=left 'DATA position1,range1,action1, position2,range2,action2,.. 'at positionX, if usRange high irrx CON 5 'pin infra red comms in, i96n lcdtx CON 6 'pin to send to LCD lcdrx CON 7 'pin to receive from LCD Rled CON 8 'pin high turns on right green led Lled CON 9 'pin high turns on left red led ustx CON 14 'pin ultrasonic commands, i96n usrx CON 15 'pin ultrasonic data in, i96n i96n CON 16624 '9600 baud, 8 bit, no parity, inverted poweruptime CON 1500 'pause before powering up electronics ustout CON 10000 'timeout min 6000 for 0 to 255 travel usmin CON 10 'right,increase if servo hits end stop usahead CON 127 'alter so servo points straight ahead usmax CON 255 'left, decrease if servo hits end stop BOScmnd VAR Byte 'byte to send to BOS processor Sptr VAR Byte 'pointer to DATA sequence string arg1 VAR Byte 'command argument irin VAR Byte 'infra-red command lcdbtns VAR Byte 'button states are in lower nibble btn1state VAR lcdbtns.BIT0 btn2state VAR lcdbtns.BIT1 btn3state VAR lcdbtns.BIT2 btn4state VAR lcdbtns.BIT3 btn VAR lcdbtns.LOWNIB '0 - 4 current button state oldbtn VAR Nib '0 - 4 last button state lcdmsg VAR Word 'pointer to current char in text lcdchar VAR Byte 'character to send to LCD btnc VAR Nib 'current BuTtoN Choice from menu mendo VAR Nib 'selection chosen by OK lcdoptions CON 6 'number of menu options usdir VAR Byte 'us servo direction, 255=left 0=right usRange VAR Byte 'us servo range, 15-255 usRptr VAR Byte 'pointer to DATA us Rules usRrange VAR Byte 'range to trigger action usaction VAR Byte 'action byte if triggered 'during downloading the power to the electronics is turned off, 'if it is turned back on again too soon the electronics, 'especially the ultrasonics do not power up properly, 'so wait poweruptime before turning power on. init: OUTPUT Rled OUTPUT Lled HIGH Rled HIGH Lled PAUSE poweruptime HIGH epower 'turn on electronics init1: GOSUB irget PAUSE 100 'no point in listening too often IF irin<>0 THEN init1 'wait for ir to initialise DEBUG CLS PAUSE 1000 'give time for lcd to get ready 'IR circuit sometimes starts up with a false key of 2 GOSUB irget PAUSE 100 'no point in listening too often IF irin<>0 THEN init1 'wait for false key to end lcdmsg =dbtxt :GOSUB LCd 'write button text 'Program start '------------- start: TOGGLE Rled PAUSE 50 mendo =0 GOSUB lcdmenu 'returns butndo case: IF mendo=1 THEN wig1 'key/mendo 1 IF mendo=2 THEN wig2 'key/mendo 2 IF mendo=3 THEN wig3 'key/mendo 3 IF mendo=4 THEN set1 'key/mendo 4 IF mendo=5 THEN set2 'key/mendo 5 IF mendo=6 THEN uswander 'key/mendo 6 TOGGLE Lled PAUSE 50 GOTO start '------------- leaves -------------------------- wig1: Sptr =wiggle1 lcdmsg =dm1 :GOSUB lcd lcdmsg =dbusy :GOSUB lcd GOSUB dosequence GOTO start wig2: Sptr =wiggle2 lcdmsg =dm2 :GOSUB lcd lcdmsg =dbusy :GOSUB lcd GOSUB dosequence GOTO start wig3: Sptr =wiggle3 lcdmsg =dm3 :GOSUB lcd lcdmsg =dbusy :GOSUB lcd GOSUB dosequence GOTO start set1: Sptr =setting1 lcdmsg =dm4 :GOSUB lcd lcdmsg =dbusy :GOSUB lcd GOSUB dosequence GOTO start set2: Sptr =setting2 lcdmsg =dm5 :GOSUB lcd lcdmsg =dbusy :GOSUB lcd GOSUB dosequence GOTO start uswander: lcdmsg =dm6 :GOSUB lcd lcdmsg =dbusy :GOSUB lcd GOSUB btns 'check pendant buttons IF btn<>0 THEN start 'other command wanted GOSUB irget 'check ir IF irin=6 THEN usdo 'for us IF irin<>0 THEN start 'other ir command received usdo: BOScmnd ="F" 'set default usRptr =usactions 'point to desired actions ustest: READ usRptr,usdir IF usdir=0 THEN usmove 'end of list, do default usRptr =usRptr +1 READ usRptr,usRrange usRptr =usRptr +1 READ usRptr,usaction usRptr =usRptr +1 GOSUB usget IF usRange>usRrange THEN ustest 'do next test BOScmnd =usaction 'action triggered usmove: GOSUB tellBOS 'now do action GOTO uswander '------------- subroutines --------------------- usget:'talk to UltraSonic co-processor usRange =0 'invalid value, used if no repyl usdir =usdir MIN usmin MAX usmax 'don't hit end stops usout: SEROUT ustx,i96n,["T",usdir] SERIN usrx,i96n,ustout,us1,[usRange] 'jump if no reply DEBUG DEC usdir," ",DEC usRange," ",CR RETURN us1: DEBUG "no reply from ultrasonics",CR RETURN '-------------------- lcdmenu: lcdit: LOOKUP btnc,[dm0,dm1,dm2,dm3,dm4,dm5,dm6],lcdmsg GOSUB lcd lcdb: GOSUB irget IF irin=0 THEN mbtns mendo =irin RETURN 'with ir cmdn mbtns: GOSUB btns IF btn=oldbtn THEN lcdb oldbtn =btn IF btn=2 THEN lcd2 IF btn=3 THEN lcd3 IF btn=4 THEN lcd4 GOTO lcdb lcd2: btnc =1 +btnc -1 MIN 1 -1 :GOTO lcdit '+1 -1 => don't go through 0 lcd3: btnc =btnc +1 MAX lcdoptions :GOTO lcdit lcd4: mendo =btnc lcdmsg =dbusy :GOSUB lcd GOSUB nobtns RETURN '-------------------- lcd: READ lcdmsg,lcdchar IF lcdchar=0 THEN lcdret 'if 0 then end of msg SEROUT lcdtx,i96n,[lcdchar] lcdmsg =lcdmsg +1 'point to next character GOTO lcd lcdret: RETURN '-------------------- btns: lcdbtns =0 'set a default value SEROUT lcdtx,i96n,[27,"K0"] 'ask LCD for button status SERIN lcdrx,i96n,200,nolcd,[lcdbtns] 'get byte nolcd: btn =NCD lcdbtns.LOWNIB '0 - 4 RETURN '----------------------- nobtns: lcdbtns =0 'wait until no buttons are pressed nbtn: SEROUT lcdtx,i96n,[27,"K0"] 'ask LCD for button status SERIN lcdrx,i96n,200,nolcd,[lcdbtns] 'get byte IF lcdbtns.LOWNIB<>0 THEN nbtn RETURN '-------------------- irget:'listen to InfraRed co-processor irin =0 'set to idle value SERIN irrx,i96n,100,noir,[irin] 'valid 0 - 8 irin =irin & $F 'mask high nibble timing data noir: RETURN '-------------------- dosequence: 'loop round until BCbyte="z" READ Sptr,BOScmnd IF BOScmnd="z" THEN doneR arg1 =255 LOOKDOWN BOScmnd,["srml"],arg1 IF arg1=255 THEN t1 'BOScmnd not srm or l, jump to t1 Sptr =Sptr+1 'point to argument READ Sptr,arg1 'read it GOSUB tellBOS1d 'send them GOTO nxt 'go onto next operation t1: GOSUB tellBOS nxt: Sptr =Sptr+1 GOTO dosequence doneR: RETURN '-------------------- tellBOS: PAUSE 10 'give the BOS time to go into receive mode IF BOSbusy=1 THEN tellBOS 'wait until BOS not busy SEROUT BOStx,i96n,[BOScmnd] RETURN '-------------------- tellBOS1d: 'send command plus one argument in decimal PAUSE 10 'give the BOS time to go into receive mode IF BOSbusy=1 THEN tellBOS1d 'wait until BOS not busy SEROUT BOStx,i96n,50,[BOScmnd,DEC arg1,CR] '50 pace time RETURN '------------------------ end program -------------------------