' ========================================================================= ' File...... bs2_joystick_rover ' Purpose... Joystick Testing application ' Author.... Firestorm_v1 ' E-mail.... firestorm.v1@gmail.com ' Website... http://www.yourwarrantyisvoid.com ' Date...... 01/27/2010 ' Version... 1.0 ' {$STAMP BS2} ' {$PBASIC 2.5} ' -----[ Program Description ]--------------------------------------------- ' This application is designed to let you use the analog joystick to ' control your BOE-bot. It uses RCTIME to create the proper pulses to ' send to the servos in order to get them to perform as expected. The ' trigger button (Button 0) will honk the "horn" (piezo speaker) and the ' thumb button (Button 1) will turn on or turn off an LED. ' ' On the BOE-BOT, the "front" of the bot will be the side that has the ' breadboard, with the serial port or USB port at the rear of the bot. ' ' The joystick is set up as follows: ' ' Left servo forward only ' | ' Turn Left | Both servos forward ' \ | / ' \ | / ' Right servo backwards only --- [=] --- Right servo forward only ' / | \ ' / | \ ' Both servos backwards | Turn Right ' | ' Left servo backwards only ' ' The same DEBUG statements that were used for BS2_joystick_diagnostics ' are used for on-bench testing and fine tuning of the BOE-BOT. I have ' also added speed indicators(H=High, M=Medium, L=Low) and direction ' indicators (F=Forward, R=Reverse) for additional information. ' ' ' -----[ I/O Definitions ]------------------------------------------------- '{Joystick} Xaxispin PIN 0 'Pin of X axis pot on joystick Yaxispin PIN 1 'pin of Y axis pot on joystick trigger PIN 2 'Pin for joystick trigger button (Button 1) thumb PIN 3 'Pin for thumb button (Button 2) '{Onboard Peripherials} lightbulb PIN 4 'Pin for the headlight (when button 2 is activated) horn PIN 5 'Pin for the piezo speaker (when button 1 is activated) '{Servos} ServoL PIN 13 'Left Servo ServoR PIN 12 'Right Servo ' -----[ Variables ]------------------------------------------------------- time VAR Word 'Generic timer variable (GOSUBs only) pulse VAR Word 'Generic pulse variable (GOSUBs only) rcXtime VAR Word 'Time of cap decay on X axis pot rcXpulse VAR Word 'Pulse timing out to left servo, (for testing only) rcXspd VAR Byte 'X speed H M L - rcXdir VAR Byte 'X direction F R - rcYtime VAR Word 'Time of cap decay on Y axis pot rcYpulse VAR Word 'Pulse timing out to right servo, (for testing only) rcYspd VAR Byte 'Y speed H M L - rcYdir VAR Byte 'Y direction F R - ' -----[ EEPROM Data ]----------------------------------------------------- 'None ' -----[ Initialization ]-------------------------------------------------- 'None ' -----[ Program Code ]---------------------------------------------------- DO GOSUB Xaxis GOSUB Yaxis GOSUB Blinky GOSUB Beeper DEBUG CRSRXY,0,0 DEBUG "X time: ",DEC5 rcXtime,CR DEBUG "X pulse: ",DEC5 rcXpulse," ",rcXspd," ",rcXdir,CR DEBUG "Y time: ",DEC5 rcYtime,CR DEBUG "Y pulse: ",DEC5 rcYpulse," ",rcYspd," ",rcYdir,CR DEBUG "Button 0:",BIN1 trigger,CR DEBUG "Button 1:",BIN1 thumb,CR LOOP END ' -----[ Subroutines ]----------------------------------------------------- Xaxis: rcXtime=0 'Do some basic setup and set rcXpulse=200 'variables to defaults. rcXspd="-" rcXdir="-" HIGH Xaxispin 'Charge X axis cap PAUSE 20 ' Wait a few ms RCTIME Xaxispin,1,rcXtime 'Time it's decay and set to rcXtime IF rcXtime=0 THEN 'In case stick is unplugged rcXpulse=200 'don't want to damage servos. ELSEIF (rcXtime <= 1050 AND rcXtime >=1) THEN 'Extreme far left rcXpulse=rcXpulse + 45 rcXspd="H" rcXdir="F" ELSEIF (rcXtime <=2055 AND rcXtime >=1051 ) THEN ' Mostly left rcXpulse=rcXpulse + 30 rcXspd="M" rcXdir="F" ELSEIF (rcXtime <=2950 AND rcXtime >=2055) THEN ' slightly left rcXpulse=rcXpulse + 15 rcXspd="L" rcXdir="F" ELSEIF (rcXtime <=4050 AND rcXtime >=3100) THEN ' slightly right rcXpulse=rcXpulse - 15 rcXspd="L" rcXdir="R" ELSEIF (rcXtime <=5100 AND rcXtime >=4050) THEN 'Mostly right rcXpulse=rcXpulse - 30 rcXspd="M" rcXdir="R" ELSEIF rcXtime >=5100 THEN 'Extremely far right rcXpulse=rcXpulse - 45 rcXspd="H" rcXdir="R" ELSE 'Capture all other values, set to rcXpulse=200 'a "safe" value. rcXspd="-" rcXdir="-" ENDIF rcXpulse= rcXpulse+550 'Add constant 550 to the pulse differential PULSOUT ServoL,rcXpulse 'Pulse it out. RETURN Yaxis: rcYtime=0 'Do some basic setup and set rcYpulse=200 'variables to defaults. rcYspd="-" rcYdir="-" HIGH Yaxispin 'Charge Y axis cap PAUSE 20 ' Wait a few ms RCTIME Yaxispin,1,rcYtime 'Time it's decay and set to rcXtime IF rcYtime=0 THEN 'In case stick is unplugged rcYpulse=200 'don't want to damage servos. ELSEIF (rcYtime <= 800 AND rcYtime >=1) THEN 'Extreme far forward rcYpulse=rcYpulse + 45 rcYspd="H" rcYdir="F" ELSEIF (rcYtime <=1299 AND rcYtime >=801 ) THEN ' Mostly forward rcYpulse=rcYpulse + 30 rcYspd="M" rcYdir="F" ELSEIF (rcYtime <=1600 AND rcYtime >=1300) THEN ' slightly forward rcYpulse=rcYpulse + 15 rcYspd="L" rcYdir="F" ELSEIF (rcYtime <=2200 AND rcYtime >=1700) THEN ' slightly back rcYpulse=rcYpulse - 15 rcYspd="L" rcYdir="R" ELSEIF (rcYtime <=2500 AND rcYtime >=2201) THEN 'Mostly back rcYpulse=rcYpulse - 30 rcYspd="M" rcYdir="R" ELSEIF rcYtime >=2500 THEN 'Extremely far back rcYpulse=rcYpulse - 45 rcYspd="H" rcYdir="R" ELSE 'Capture all other values, set to rcYpulse=200 'a "safe" value rcYspd="-" rcYdir="-" ENDIF rcYpulse= rcYpulse+550 'Add constant 550 to the pulse differential PULSOUT ServoR,rcYpulse 'Pulse it out. RETURN Blinky: 'Check our headlight. IF thumb=%1 THEN IF lightbulb=0 THEN HIGH lightbulb PAUSE 200 ELSE LOW lightbulb PAUSE 200 ENDIF ENDIF RETURN Beeper: 'Beep Beep! IF trigger=%1 THEN FREQOUT horn, 500,1500 PAUSE 100 FREQOUT horn, 500,1500 ENDIF RETURN ' -------------------------------------------------------------------------