'>Hextor22.BSx '============================================================== 'Milford Instruments http://www.milinst.com 'Hextor Design, Bug Commander - Behavioural Operating System 'and the Behavioural Controller programs by David Buckley '================================================================ '{$stamp BS2sx} '============================================================== 'Introduction of a facility to include a sequence in another 'similar to GOSUB, enabled by means of a stack and pointer. ' 'Note this is a moment which has been drawing near for some 'time CTL-M shows that we are running out of room, consequently 'it is time to use the other 7 pages in the Stamp2Sx. We will 'begin another series of programs where each Behaviour 'controller is placed in a separate page. ' 'Because we are running out of room 'the section in BOScon between '**** make space delete start 'and '**** make space delete end 'has been deleted ' 'New '--- 'In a sequence a new meta command i,word 'sequence-name', 'permits 'sequence-name' to be used as a subroutine. ' 'Common RAM section before cons and vars 'A software stack is implemented in Common RAM ' 'Required knowledge: use of stacks, stackpointers, push and pop ' and Common RAM ' 'Meta command: i 'Common RAM: software stack 'Constants: 'Variables: incS, tempb, stkp 'Routine: stkp initialised in init1: 'Subroutine: push,pop,pushword,popword ' Sinc added to dosequence, ' dosequence modified at doneS 'Sequence: wiggle2 included in the middle of wiggle3 'usRules: 'Behaviour: '============================================================== 'Experiment with including sequences in other sequences '============================================================== '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 '--------------------------- 'Meta commands - '"a",word jump to sequence 'name' '"i",word includes sequence 'name' as a subroutine ' 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 "s",15,"RRRRRRRRRRRRs",4,"z" 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 '----------------- 'Meta commands - '"a" if selected by the test then triggers the Action sequence ' The first parameter byte for "Y" or "y" is the index to ' the yawn variable, 0=>usyawn0, 1=>usyawn1. ' The second parameter byte is the threshold '"Y" if selected by the test increments 'usyawnX'. ' If 'usyawnX'> threshold then skip the following rule. '"y" if selected then resets 'usyawnX' to 0 'so select same parameters as rule to be skipped for both Y, y ' '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 'Because the parameters for the next four rules are the same, 'if beg was going to be activated by the second rule then "Y" 'would be selected by the first rule and the Yawn variable 'incremented. When yawn=4 then the beg rule would be skipped 'and the third rule would activate "y" reseting the Yawn 'variable. Then the fourth rule would activate tround. DATA usF, 45,"XY",0,3 'increment usyawn0 'if usyawn0>3 then skip 'ie do beg 3 times before skipping DATA usF, 45,"Xa",Word beg 'Action if <=45 DATA usF, 45,"Xy",0,0 'reset usyawn0 if <=45 DATA usF, 45,"Xa",Word tround '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 '============================================================== 'common RAM '---------- 'Note thERE is no test for the stack being too big, beware of 'the staCK growing down through 0! '60 down Sequence stack '61 SEQuence stack pointer '62 lETTerbox for push and pop '63 aCTIve program number used by BS2Sx '-------------------------------------------------------------- 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 arg VAR Word arg1 VAR arg.LOWBYTE 'command argument arg2 VAR arg.HIGHBYTE 'command argument irin VAR Byte 'infra-red command tempb VAR irin 'temp byte 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 usyawn0 VAR Nib 'counter0 for how bored robot is usyawn1 VAR Nib 'counter1 for how bored robot is incs VAR Nib 'number of sequences included tempw VAR LCDmsg'temp word used at Si stkp VAR Byte 'point to top of CommonRam stack '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 PUT 62,62 'initialise stack pointer 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 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 doBEG:Sptr =beg lcdmsg =dm2 :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 =dm6 :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<>"Y" THEN usy BRANCH Sptrl,[usincY0,usincY1] GOTO ustest 'ignore if usactionl is too big usincY0: usyawn0 =usyawn0 +1 IF usyawn0<=Sptrh THEN ustest usRptr =usRptr +6 'skip next rule GOTO ustest usincY1: usyawn1 =usyawn1 +1 IF usyawn1<=Sptrh THEN ustest usRptr =usRptr +6 'skip next rule GOTO ustest usy: IF BOScmnd<>"y" THEN usm BRANCH Sptrl,[uszeroY0,uszeroY1] GOTO ustest 'ignore if usactionl is too big uszeroY0: usyawn0 =0 GOTO ustest uszeroY1: usyawn1 =0 GOTO ustest usm: 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: GOSUB irget 'check ir IF irin=8 THEN BOSdo 'for us IF irin<>0 THEN start 'other ir command received BOSdo: BOScmnd =0 '**** make space delete start '**** make space delete end GOTO BOScon '------------- subroutines --------------------- push: 'stacks the single byte in letterbox=commonRAM62 GET 61,stkp '61 is stack pointer stkp =stkp -1 'next location GET 62,tempb 'read letterbox PUT stkp,tempb 'push byte PUT 61,stkp 'save pointer RETURN '------------------------------- pop: 'unstacks a single byte into letterbox=commonRAM62 GET 61,stkp '61 is stack pointer GET stkp,tempb 'pop byte stkp =stkp+1 MAX 61 'previous max top+1 PUT 62,tempb 'byte to letter box PUT 61,stkp 'save pointer RETURN '------------------------------- push_arg: 'stacks the word arg PUT 62,arg1 GOSUB push PUT 62,arg2 GOSUB push RETURN '------------------------------- pop_arg: 'unstacks the word arg GOSUB pop GET 62,arg2 GOSUB pop GET 62,arg1 RETURN '------------------------------- 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="a" THEN Sa IF BOScmnd="i" THEN Si 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: GOSUB read_arg 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 Sa: GOSUB read_arg 'read new Sptr Sptr =arg 'make Stpr=new Sptr GOTO dosequence 'do new sequence Si: incS =incS +1 'update includes level GOSUB read_arg 'read new Sptr into arg, Stpr=++2 tempw =arg 'keep it safe arg =Sptr 'save old Sptr GOSUB push_arg Sptr =tempw 'make Stpr=new Sptr GOTO dosequence 'do included sequence doneS: IF incS=0 THEN retS GOSUB pop_arg 'get where we were before include Sptr =arg incS =incS MIN 1 -1 'update includes level GOTO dosequence retS: RETURN '-------------------- read_arg: READ Sptr,arg1 Sptr =Sptr +1 READ Sptr,arg2 Sptr =Sptr +1 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 -------------------------