'>Hextor18.BSx '============================================================== 'Milford Instruments http://www.milinst.com 'Hextor Design, Bug Commander - Behavioural Operating System 'and the Behavioural Controller programs by David Buckley '================================================================ '{$stamp BS2Sx} '============================================================== 'Improvement to the actions in UShello ' 'New '--- ' 'Required knowledge: ' 'Constants: 'Variables: 'Routine: 'Subroutine: 'Sequence: BR BL 'usRules: ushello modified with BR BL 'Behaviour: 'LCD text: '============================================================== 'You will also note that the sequence 'wiggle2' is not used in 'this program, it could be deleted but may be useful in the 'future. Deleting sequences can lead to unpredictable results 'since you have to be absolutely certain that they are not used 'anywhere in the program, if in doubt change them to the single 'byte "z" for a no action sequence. ' 'Writing control sequences illustrates the importance of a good 'education, if the memory is first filled up with junk 'sequences then later it becomes very difficult to assign 'meaningful names to useful sequences. Changing the contents of 'a sequence can lead to all sorts of strange behaviour. '============================================================== 'Direct Control 'turns on servo power and exits with servo power on 'BOSbusy goes low immediately, doesn't wait for move to finish '$speed;$servo - high nibble and low nibble respectively ' BOScmnd,arg1.arg2 ' 0,$speed;$servo,position ' 1,$XX,$servo,X ->P actual servo position ' 2,$XX,$servo,X ->S servo status, bit1-on bit0-done ' 3,X,X ->hiF,then loF two bytes all servo moved status ' 4,X,X ->hiR,then loR two bytes all servo on/off status ' 5,$XX;$servo,X stop pulsing servo ' '-------- Legs & Logical-Servos -------- ' ' Plan view of Hextor S - Servo(0 - 13) L - Legs(1 - 6) ' S L L S ' ( ) gripper 13 ' | | lift 12 ' L---F---R LEG NUMBERS ' 1,0 | 1 2 | 6,7 1=LF 2=RF ,L - left, R - Right ' 3,2 | 3 4 | 8,9 3=LM 4=RM ,M - mid, F - Front ' 5,4 | 5 6 | 10,11 5=LB 6=RB ,B - Back ' \-------/ ' '-------- Servo values and leg positions ------- ' f - forward m - middle b - backward 'Left Lf=255 Lm=127 Lb=0 'Right Rf=0 Rm=127 Rb=255 ' ' d - Down u - Up 'Left Ld=0 Lu=255 'Right Rd=255 Ru=0 ' '-------- Arm and Gripper positions ------- 'Arm up 255 'Arm down 0 'Gripper open 255 'Gripper closed 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" tround DATA "RRRRRRRRRRRRz" beg DATA "s",4,"U" 'speed 4, Up DATA 0,$43,127 'speed 4, servo 3, leg 3 up DATA 0,$49,127 'speed 4, servo 9, leg 4 up DATA 0,$42,170 'speed 4, servo 2, leg 3 forward DATA 0,$48,90 'speed 4, servo 8, leg 4 forward DATA "WU" 'wait for move to finish then Up DATA 0,$25,255 'speed 2, servo 5, leg 5 up DATA 0,$2B,0 'speed 2, servo 11, leg 6 up DATA 0,$F1,255 'speed 15, servo 1, leg 1 up DATA 0,$F7,0 'speed 15, servo 7, leg 2 up DATA 0,$40,200 'speed 4, servo 0, leg 1 forward DATA 0,$46,50 'speed 4, servo 6, leg 2 forward DATA "s",2,"WUz" 'speed 2, wait, Up, end of list Rface DATA "s",15,"RRR","s",2,"z" Lface DATA "s",15,"LLL","s",2,"z" F5 DATA "s",15,"FFFFF","s",2,"z" R5 DATA "s",15,"RRRRR","s",2,"z" L5 DATA "s",15,"LLLLL","s",2,"z" BR DATA "BRz" BL DATA "BLz" 'Ultrasonic sensor '----------------- '"a" - Action sequence 'Rules - 'DATA direction,range,action if >,action if <,sequence pointer 'If the rule doesn't contain "a" then no sequence can be 'activated by it so any value could be used for the pointer 'value. uscorner DATA "S" 'default action DATA usF, 20,"XB",Word 0 'back if <=20 DATA usR2, 20,"XL",Word 0 'left if <=20 DATA usL2, 20,"XR",Word 0 'right if <=20 DATA usF, 25,"Xa",Word tround 'Action if <=25 DATA usR2, 25,"Xa",Word tround 'Action if <=25 DATA usL2, 25,"Xa",Word tround 'Action if <=25 DATA usF, 80,"XF",Word 0 'forward if <80 DATA usR2, 80,"XR",Word 0 'right if <80 DATA usL2, 80,"XL",Word 0 'left if <80 DATA 0 'end of list usbeg DATA "S" DATA usF, 45,"Xa",Word beg 'Action if <=45 DATA usR2, 35,"Xa",Word beg 'Action if <=35 DATA usL2, 35,"Xa",Word beg 'Action if <=35 DATA 0 'end of list ushello DATA "S" DATA usF, 20,"XB",Word 0 'back if <=20 DATA usR2, 20,"Xa",Word BL 'left back if <=20 DATA usL2, 20,"Xa",Word BR 'right back if <=20 DATA usF, 45,"Xa",Word beg 'Action if <=45 DATA usR2, 45,"Xa",Word Rface'Action if <=45 DATA usL2, 45,"Xa",Word Lface'Action if <=45 DATA usF, 100,"Xa",Word F5 'Action if <=100 DATA usR2,100,"Xa",Word R5 'Action if <=100 DATA usL2,100,"Xa",Word L5 'Action if <=100 DATA 0 'end of list '============================================================== '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,"Beg ",0 dm3 DATA 1,"Wiggle 3 ",0 dm4 DATA 1,"Setting 1 ",0 dm5 DATA 1,"Setting 2 ",0 dm6 DATA 1,"US corner ",0 dm7 DATA 1,"US Hello ",0 dm8 DATA 1,"BOS Control ",0 '============================================================== BOStx CON 0 'pin serial to BOS processor BOSrx CON 1 'pin serial from BOS processor epower CON 2 'pin high turns on power to electronics BOSbusy VAR IN4 'pin BOS processor busy => 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 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 i96n CON 16624 '9600 baud, 8 bit, no parity, inverted 'us servo directions, 0 is a reserved direction for end of list usR3 CON 27 'us servo direction Right a lot usR2 CON 77 'us servo direction Right middle usR1 CON 107 'us servo direction Right a little usF CON 127 'us servo direction Forward usL1 CON 147 'us servo direction Left a little usL2 CON 177 'us servo direction Left middle usL3 CON 227 'us servo direction Left a lot BOScmnd VAR Byte 'byte to send to BOS processor Sptr VAR Word 'pointer to DATA sequence string Sptrl VAR Sptr.LOWBYTE Sptrh VAR Sptr.HIGHBYTE arg1 VAR Byte 'command argument arg2 VAR Byte 'command argument irin VAR Byte 'infra-red command oldirin VAR Byte 'old 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 8 'number of menu options usrules VAR Word 'pointer to current rules usdir VAR Byte 'us servo direction, 255=left 0=right usrange VAR Byte 'us servo range, 15-255 usrptr VAR Word 'pointer to DATA us Rules usrrange VAR Byte 'range to trigger action usactiong VAR Byte 'action byte if > triggered usactionl 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: '---- Main BehavioUR COntroller - Infra-Red/Pendant buttons --- irCON: TOGGLE Rled PAUSE 50 mendo =0 GOSUB lcdmenu 'returns butndo case: IF mendo=1 THEN wig1 'key/mendo 1 IF MEndo=2 THEN dobeg '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 DOUSCorner 'key/mendo 6 IF mendo=7 THEN DOUSHello 'key/mendo 7 IF MEndo=8 THEN BOScon 'key/mendo 8 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 dobeg: Sptr =beg 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 '---- Behaviour CONTRoller - Ultrasonic Sensor ---- doUSCORner: usrules =uscorner LCDMsg =dm6 :GOSUB lcd lcdmSG =Dbusy :GOSUB lcd GOSUB irget 'check ir IF irin=6 THEN douscorner 'wait for idle signal GOTO usbehave dousbeg: usrules =usbeg ' lcdmsg =dm7 :gosub lcd lcdmsg =dbusy :GOSUB lcd GOSUB irget 'check ir IF irin=7 THEN dousbeg 'wait for idle signal GOTO usbehave doushello: usrules =ushello lcdmsg =dm7 :GOSUB lcd lcdmsg =dbusy :GOSUB lcd GOSUB irget 'check ir IF irin=7 THEN DOUShello 'wait for idle signal 'AND drop through to usbehave usBEHAVe: GOSUB irget 'check ir IF irin<>0 THEN start 'other ir command received GOSUB btns 'check pendant buttons IF btn<>0 THEN start 'other command wanted usdo: usRptr =usrules READ usRptr,BOScmnd 'read default usRptr =usRptr +1 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,usactiong usRptr =usRptr +1 READ usRptr,usactionl usRptr =usRptr +1 READ usRptr,Sptrl 'read pointer to sequence usRptr =usRptr +1 READ usRptr,Sptrh 'read pointer to sequence usRptr =usRptr +1 'debug dec usdir,dec usRrange,usactiong,usactionl,cr GOSUB usget IF usRange>usRrange THEN usg usl: IF usactionl="X" THEN ustest 'ignore it BOScmnd =usactionl 'action triggered GOTO usmove 'do it usg: IF usactiong="X" THEN ustest 'ignore it BOScmnd =usactiong 'action triggered usmove: IF BOScmnd="a" THEN sact GOSUB tellBOS 'now do action GOTO usbehave 'do it sact: GOSUB dosequence GOTO usbehave '---- Behaviour Controller - Direct to BOS ---- ' turns on servo power and exits with servo power on ' $speed;$servo - high nibble and low nibble respectively ' 0,$speed;$servo,position ' 1,$XX,$servo,X ->P actual servo position ' 2,$XX,$servo,X ->S servo status, bit1-on bit0-done ' 3,X,X ->hiF,then loF two bytes all servo moved status ' 4,X,X ->hiR,then loR two bytes all servo on/off status ' 5,$XX;$servo,X stop pulsing servo BOScon: lcdmsg =dm8 :GOSUB lcd GOSUB irget 'check ir GOSUB btns 'check pendant buttons GOSUB irget 'check ir IF irin=8 THEN BOSdo 'for us IF irin<>0 THEN start 'other ir command received IF btn<>0 THEN start 'other command wanted BOSdo: BOScmnd =0 arg1 =$90 arg2 =200 GOSUB tellBOS3 'move 0 FOR arg2 =0 TO 9 'use arg2 as a temp variable BOScmnd =1 arg1 =0 GOSUB tellBOS3 'ask position GOSUB BOSreply1 DEBUG"pulsing", BIN8 BOScmnd,BIN8 arg1,CR PAUSE 20 NEXT BOScmnd =4 GOSUB tellBOS3 'ask pulsing GOSUB BOSreply2 DEBUG"pulsing", BIN8 BOScmnd,BIN8 arg1,CR BOScmnd =5 arg1 =0 GOSUB tellBOS3 'stop pulsing PAUSE 1000 BOScmnd =4 GOSUB tellBOS3 'ask pulsing GOSUB BOSreply2 DEBUG"pulsing", BIN8 BOScmnd,BIN8 arg1,CR BOScmnd =0 arg1 =$30 arg2 =50 GOSUB tellBOS3 'move 0 PAUSE 150 BOScmnd =3 GOSUB tellBOS3 'ask finished GOSUB BOSreply2 DEBUG"finished", BIN8 BOScmnd,BIN8 arg1,CR PAUSE 2000 BOScmnd =3 GOSUB tellBOS3 'ask finished GOSUB BOSreply2 DEBUG"finished", BIN8 BOScmnd,BIN8 arg1,CR GOTO BOScon '------------- 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,dm7,dm8],lcdmsg GOSUB lcd lcdb: GOSUB irget IF irin=oldirin THEN mbtns oldirin =irin 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 doneS Sptr =Sptr+1 'point to command IF BOScmnd<6 THEN Scon arg1 =255 LOOKDOWN BOScmnd,["srml"],arg1 IF arg1=255 THEN t1 'BOScmnd not srm or l, jump to t1 READ Sptr,arg1 'read it Sptr =Sptr +1 GOSUB tellBOS1d 'send them GOTO dosequence t1: GOSUB tellBOS GOTO dosequence Scon: READ Sptr,arg1 Sptr =Sptr +1 READ Sptr,arg2 Sptr =Sptr +1 GOSUB tellBOS3 'wait until not busy then send 3 bytes IF BOScmnd=0 THEN dosequence IF BOScmnd>2 THEN rep2 rep1: GOSUB BOSreply1 'reply not used GOTO dosequence rep2: GOSUB BOSreply2 'reply not used GOTO dosequence doneS: 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 '-------------------- tellBOS3: IF BOSbusy=1 THEN tellBOS3 SEROUT BOStx,i96n,1,[BOScmnd,arg1,arg2] 'pace min 1 RETURN '-------------------- BOSreply1: 'wait min 40 SERIN BOSrx,i96n,50,BOSr1,[arg1] 'get byte BOSr1: RETURN '-------------------- BOSreply2: 'wait min 210 'get highbyte,lowbyte of flags SERIN BOSrx,i96n,250,BOSr2,[arg1,arg2] BOSr2: RETURN '------------------------ end program -------------------------