Advertisement
martinrobotika

basic competition code

Apr 12th, 2014
2,929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' SumoBot-5.1-Basic-Competition-Program.BS2
  2. ' {$STAMP BS2}
  3. ' {$PBASIC 2.5}
  4.  
  5. ' -----[ I/O Definitions ]-------------------------------------------------
  6.  
  7. LMotor       PIN        13                      ' left servo motor
  8. RMotor       PIN        12                      ' right servo motor
  9.  
  10. LLinePwr     PIN        10                      ' left line sensor power
  11. LLineIn      PIN        9                       ' left line sensor input
  12. RLinePwr     PIN        7                       ' right line sensor power
  13. RLineIn      PIN        8                       ' right line sensor input
  14.  
  15. LfIrOut      PIN        4                       ' left IR LED output
  16. LfIrIn       PIN        11                      ' left IR sensor input
  17. RtIrOut      PIN        15                      ' right IR LED output
  18. RtIrIn       PIN        14                      ' right IR sensor input
  19.  
  20. Speaker      PIN        1                       ' piezo speaker
  21. StartLED     PIN        0                       ' display start delay
  22.  
  23. ' -----[ Constants ]-------------------------------------------------------
  24.  
  25. LFwdFast     CON        1000                    ' left motor fwd; fast
  26. LFwdSlow     CON        800                     ' left motor fwd; slow
  27. LStop        CON        750                     ' left motor stop
  28. LRevSlow     CON        700                     ' left motor rev; slow
  29. LRevFast     CON        500                     ' left motor rev; fast
  30.  
  31. RFwdFast     CON        500                     ' right motor fwd; fast
  32. RFwdSlow     CON        700                     ' right motor fwd; slow
  33. RStop        CON        750                     ' right motor stop
  34. RRevSlow     CON        800                     ' right motor rev; slow
  35. RRevFast     CON        1000                    ' right motor rev; fast
  36.  
  37. ' -----[ Variables ]-------------------------------------------------------
  38.  
  39. lLine        VAR        Word                    ' left sensor raw reading
  40. rLine        VAR        Word                    ' right sensor raw reading
  41. blackThresh  VAR        Word                    ' QTI black threshold
  42. lineBits     VAR        Nib                     ' decoded sensors value
  43. lbLeft       VAR        lineBits.BIT1
  44. lbRight      VAR        lineBits.BIT0
  45.  
  46. irBits       VAR        Nib                     ' IR readings (l & r)
  47. irLeft       VAR        irBits.BIT1
  48. irRight      VAR        irBits.BIT0
  49. lastIr       VAR        Nib                     ' info from last reading
  50.  
  51. pulses       VAR        Byte                    ' counter for motor control
  52. temp         VAR        Byte
  53.  
  54. ' -----[ EEPROM Data ]-----------------------------------------------------
  55.  
  56. RunStatus    DATA       $00                     ' run status
  57.  
  58. ' -----[ Initialization ]--------------------------------------------------
  59.  
  60. Reset:
  61.   READ RunStatus, temp                          ' read current status
  62.   temp = ~temp                                  ' invert status
  63.   WRITE RunStatus, temp                         ' save for next reset
  64.   IF (temp > 0) THEN END                        ' okay to run?
  65.  
  66. ' Sets black threshold to 1/4 the average of the two sensor readings.
  67. ' SumoBot must be placed over black playing surface before this code runs.
  68.  
  69. Set_Threshold:                                  ' set QTI black threshold
  70.   GOSUB Read_Line_Sensors
  71.   blackThresh = (lLine / 10) + (rLine / 10)
  72.  
  73.   LOW LMotor                                    ' make more pins outputs
  74.   LOW RMotor
  75.  
  76. Start_Delay:                                    ' five second delay
  77.   FOR temp = 1 TO 5
  78.     HIGH StartLED                               ' show active
  79.     PAUSE 900
  80.     INPUT StartLED                              ' blink each second
  81.     FREQOUT Speaker, 100, 2500, 3000            ' beep each second
  82.   NEXT
  83.  
  84.   GOTO Lunge                                    ' start aggressive!
  85.  
  86. ' -----[ Program Code ]----------------------------------------------------
  87.  
  88. Main:
  89.   GOSUB Read_Line_Sensors
  90.  
  91.   ' If not on the Shikiri line (border), continue to look for opponent,
  92.   ' otherwise, spin back toward center and resume search
  93.  
  94.   BRANCH lineBits, [Search_For_Opponent, Spin_Left, Spin_Right, About_Face]
  95.  
  96. ' --[ Border Avoidance ]--
  97.  
  98. Spin_Left:                                      ' right sensor was active
  99.   FOR pulses = 1 TO 20
  100.     PULSOUT LMotor, LRevFast
  101.     PULSOUT RMotor, RFwdFast
  102.     PAUSE 20
  103.   NEXT
  104.   lastIr = %00                                  ' clear scan direction
  105.   GOTO Lunge
  106.  
  107. Spin_Right:                                     ' left sensor was active
  108.   FOR pulses = 1 TO 20
  109.     PULSOUT LMotor, LFwdFast
  110.     PULSOUT RMotor, RRevFast
  111.     PAUSE 20
  112.   NEXT
  113.   lastIr = %00
  114.   GOTO Lunge
  115.  
  116. About_Face:                                     ' both sensors on Shikiri
  117.   FOR pulses = 1 TO 10                          ' back up from edge
  118.     PULSOUT LMotor, LRevFast
  119.     PULSOUT RMotor, RRevFast
  120.     PAUSE 20
  121.   NEXT
  122.   FOR pulses = 1 TO 30                          ' turn around
  123.     PULSOUT LMotor, LFwdFast
  124.     PULSOUT RMotor, RRevFast
  125.     PAUSE 20
  126.   NEXT
  127.   lastIr = %00
  128.   GOTO Lunge
  129.  
  130. ' --[ IR Processing ]--
  131.  
  132. Search_For_Opponent:
  133.   GOSUB Read_IR_Sensors
  134.  
  135.   ' If opponent is not in view, scan last known direction. Turn toward
  136.   ' opponent if seen by one "eye" -- if both, lunge forward
  137.  
  138. BRANCH irBits, [Scan, Follow_Right, Follow_Left, Lunge]
  139.  
  140. Scan:
  141.   BRANCH lastIR, [Move_Fwd, Scan_Right, Scan_Left]
  142.  
  143. Move_Fwd:
  144.   GOSUB Creep_Forward
  145.   GOTO Main
  146.  
  147. Scan_Right:                                     ' spin right, slow
  148.   FOR pulses = 1 TO 5
  149.     PULSOUT LMotor, LFwdSlow
  150.     PULSOUT RMotor, RRevSlow
  151.     PAUSE 20
  152.   NEXT
  153.   GOSUB Creep_Forward                           ' keep moving
  154.   GOTO Main
  155.  
  156. Scan_Left:                                      ' spin left, slow
  157.   FOR pulses = 1 TO 5
  158.     PULSOUT LMotor, LRevSlow
  159.     PULSOUT RMotor, RFwdSlow
  160.     PAUSE 20
  161.   NEXT
  162.   GOSUB Creep_Forward
  163.   GOTO Main
  164.  
  165. Follow_Right:                                   ' spin right, fast
  166.   PULSOUT LMotor, LFwdFast
  167.   PULSOUT RMotor, RRevSlow
  168.   lastIR = irBits                               ' save last direction found
  169.   GOTO Main
  170.  
  171. Follow_Left:                                    ' spin left, fast
  172.   PULSOUT LMotor, LRevSlow
  173.   PULSOUT RMotor, RFwdFast
  174.   lastIR = irBits
  175.   GOTO Main
  176.  
  177. Lunge:                                          ' locked on -- go get him!
  178.   FOR pulses = 1 TO 25
  179.     PULSOUT LMotor, LFwdFast
  180.     PULSOUT RMotor, RFwdFast
  181.     GOSUB Read_Line_Sensors
  182.     IF (lineBits = %11) THEN Match_Over         ' in sight, we're on the line
  183.   NEXT
  184.   GOTO Main
  185.  
  186. ' If SumoBot can see the opponent with both "eyes" and both QTIs are
  187. ' detecting the border, we must have pushed the opponent out.
  188.  
  189. Match_Over:
  190.   FOR pulses = 1 TO 10                          ' stop motors
  191.     PULSOUT LMotor, LStop
  192.     PULSOUT RMotor, RStop
  193.     PAUSE 20
  194.   NEXT
  195.   INPUT LMotor
  196.   INPUT RMotor
  197.  
  198.   FOR temp = 1 TO 10                            ' make some noise
  199.     HIGH StartLED
  200.     FREQOUT Speaker, 100, 2500, 3000            ' beep
  201.     INPUT StartLED                              ' blink LED
  202.     PAUSE 100
  203.   NEXT
  204.  
  205.   DIRS = $0000                                  ' disable all outputs
  206.   GOTO Reset                                    ' reset for next round
  207.  
  208. ' -----[ Subroutines ]-----------------------------------------------------
  209.  
  210. Read_Line_Sensors:
  211.   HIGH LLinePwr                                 ' activate sensors
  212.   HIGH RLinePwr
  213.   HIGH LLineIn                                  ' discharge caps
  214.   HIGH RLineIn
  215.   PAUSE 1
  216.   RCTIME LLineIn, 1, lLine                      ' read left sensor
  217.   RCTIME RLineIn, 1, rLine                      ' read right sensor
  218.   LOW LLinePwr                                  ' deactivate sensors
  219.   LOW RLinePwr
  220.  
  221.   ' convert readings to bits
  222.   LOOKDOWN lLine, >=[1000, 0], lbLeft           ' 0 = black, 1 = line
  223.   LOOKDOWN rLine, >=[1000, 0], lbRight
  224.   RETURN
  225.  
  226. Read_IR_Sensors:
  227.   FREQOUT LfIrOut, 1, 38500                     ' modulate left IR LED
  228.   irLeft = ~LfIrIn                              ' read input (1 = target)
  229.   FREQOUT RtIrOut, 1, 38500                     ' modulate right IR LED
  230.   irRight = ~RtIrIn                             ' read input (1 = target)
  231.   RETURN
  232.  
  233. Creep_Forward:
  234.   FOR pulses = 1 TO 20
  235.     PULSOUT LMotor, LFwdSlow
  236.     PULSOUT RMotor, RFwdSlow
  237.     PAUSE 20
  238.   NEXT
  239.   RETURN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement