'------------------------------------------------------------------------------- 'BX-24 Mobile Robot Code 'This program contains Servo, Piezo, IR out and IR detector primitive subroutines for BX-24 system. 'Copyright 1999, Peter C. Charles 'BX24 chip is socketted into Parallax Board of Education, and mounted on Parallax BOEBot platform 'Pins are as follows: 'Pin5 (BX24 nomenclature)-right servo control 'Pin6-left servo control 'Pin7-Piezo speaker 'Pin8-right IR control, 555 ic timer pin 4. 555 is tuned to 38KHz and outputs to high intensity IR LED 'Pin9-left IR control, 555 ic timer pin 4. 555 is tuned to 38KHz and outputs to high intensity IR LED 'Pin10-IR receiver unit 'Note: This configuration uses switched IR emitters and only one receiver (my cat removed the other IR receiver. 'Ironically, the cat also only has one eye- hence his name "One-Eyed Jack"). This setup differs significantly 'from the BOEBot configuration, and is very finicky about the exact geometry of the emitters and the receiver. 'It has to be empirically adjusted for optimal sensitivity. The pin usage is also radically different from the 'BOEBot setup, as I wanted to conserve the ADC pins for other interesting sensors. '------------------------------------------------------------------------------- Public Const L_Servo as byte = 6 'Motor controller declaration Public Const R_Servo as byte = 5 'Motor controller declaration Sub Main() 'Define some local variables and constants Const C As Integer = 785 'Musical notes so the robot can sing Const D As Integer = 881 Const E As Integer = 989 Const G As Integer = 1176 Const r As Integer = 0 Dim Count As Long, TimeInterval As Long Dim R_IR As Byte 'Variables for the IR sensor data Dim L_IR As Byte 'Sing a little song to start the day! Call FreqOut(7, C, C, 2.0E-1) Call FreqOut(7, E, E, 2.0E-1) Call FreqOut(7, G, G, 2.0E-1) Call FreqOut(7, C, C, 4.0E-1) Call FreqOut(7, r, r, 1.0E-1) Call FreqOut(7, G, G, 2.0E-1) Call FreqOut(7, C, C, 1.0) Do Call PutPin(10,2) 'IR receiver in tristate Call PutPin(9,1) 'Left IR transmiter pin on Call Delay (0.001) L_IR = GetPin(10) 'read IR signal from receiver into left variable Call PutPin(9,0) 'Turn off left emitter Call PutPin(8,1) 'Right IR transmitter pin on Call Delay (0.001) R_IR = GetPin(10) 'read IR signal from receiver into right variable Call PutPin(8,0) 'Turn off right emitter 'Responses to IR proximity detectors If (L_IR=0) and (R_IR=1) Then 'something in the way on left Call Move_BackLeft End If If (L_IR=1) and (R_IR=0) Then 'something in the way on right Call Move_BackRight End If If (L_IR=0) and (R_IR=0) Then 'something in the way straight ahead Call Move_Backward End If 'The single most significant obstacle that my robot runs into are my pets. As they tend to advance 'on the bot when it backs up, I have it check twice prior to moving forward. Note some changes in the 'avoidance behavior. Call PutPin(9,1) 'Left IR transmiter pin on Call Delay (0.001) L_IR = GetPin(10) 'read IR signal from receiver Call PutPin(9,0) 'Turn off left emitter Call PutPin(8,1) 'Right IR transmitter pin on Call Delay (0.001) R_IR = GetPin(10) 'read IR signal from receiver into right variable Call PutPin(8,0) 'Turn off right emitter 'More responses to IR proximity detectors If (L_IR=0) and (R_IR=1) Then 'Still something to the right Call Move_Backward End If If (L_IR=1) and (R_IR=0) Then 'Still something to the left Call Move_Backward End If If (L_IR=0) and (R_IR=0) Then 'Still something straight ahead Call TurnAround_Right End If 'Let's go somewhere! Call Move_Forward Loop End Sub '------------------------------------------------------------------------------- Sub BlinkLEDs() 'Flash the Red and Green LEDS- code from NetMedia Const GreenLED As Byte = 26 Const RedLED As Byte = 25 Const LEDon As Byte = 0 Const LEDoff As Byte = 1 ' Red pulse. Call PutPin(RedLED, LEDon) Call Delay(0.07) Call PutPin(RedLED, LEDoff) Call Delay(0.07) ' Green pulse. Call PutPin(GreenLED, LEDon) Call Delay(0.07) Call PutPin(GreenLED, LEDoff) Call Delay(0.07) End Sub '--------------------------------------------------------------------------- Sub Noise() 'Uses the BX24-specific FreqOut comand to squeek pin 7 Call FreqOut(7, 4000, 1000, 500) End Sub '------------------------------------------------------------------------------- Sub Move_Forward() 'Uses PWM to control Futaba servos for forward motion Const GreenLED As Byte = 26 Const LEDon As Byte = 0 Dim L_Counter as Integer Call PutPin(GreenLED, LEDon) 'Turn on the green LED For L_Counter = 1 to 75 Call PulseOut(L_Servo, 2.0E-3, 1) 'Left drive shaft- 2 mSec interval Call PulseOut(R_Servo, 1.0E-3, 1) 'Right drive shaft- 1 mSec interval Next End Sub 'These values work pretty well for moderate forward speed '------------------------------------------------------------------------------- Sub Move_Backward() 'Uses PWM to control Futaba servos for backward motion Dim L_Counter as Integer Call BlinkLEDs Call Noise 'Makes a little backing-up noise Call Noise Call BlinkLEDs For L_Counter = 1 to 220 Call PulseOut(L_Servo, 1.0E-3, 1) 'Left drive shaft- 1mSec interval Call PulseOut(R_Servo, 2.0E-3, 1) 'Right drive shaft- 2mSec interval Next End Sub '------------------------------------------------------------------------------- Sub TurnAround_Right() 'Servos turning same direction, makes bot pivot right Dim L_Counter as Integer Call Noise 'Makes a little backing up noise For L_Counter = 1 to 220 'Gets it all the way around Call PulseOut(L_Servo, 2.0E-3, 1) 'Right drive shaft- 2mSec interval Call PulseOut(R_Servo, 2.0E-3, 1) 'Right drive shaft- 2mSec interval Next End Sub '------------------------------------------------------------------------------- Sub TurnAround_Left() 'Same trick, turn 180 deg to left Dim L_Counter as Integer Call BlinkLEDs Call Noise 'Makes a little backing-up noise For L_Counter = 1 to 440 'when turning this way, needs 440- why? Call PulseOut(L_Servo, 1.0E-3, 1) 'Right drive shaft- 1mSec interval Call PulseOut(R_Servo, 1.0E-3, 1) 'Right drive shaft- 1mSec interval Next End Sub '------------------------------------------------------------------------------- Sub Move_BackRight() 'Same trick, backward motion with twist to right Dim L_Counter as Integer Call BlinkLEDs Call Noise 'Makes a little backing-up noise For L_Counter = 1 to 200 Call PulseOut(L_Servo, 1.5E-3, 1) 'Left drive shaft- 1.5mSec interval Call PulseOut(R_Servo, 2.0E-3, 1) 'Right drive shaft- 2mSec interval Next End Sub '------------------------------------------------------------------------------- Sub Move_BackLeft() 'Same trick, backward motion with twist to left Dim L_Counter as Integer Call BlinkLEDs Call Noise 'Makes a little backing-up noise For L_Counter = 1 to 200 Call PulseOut(L_Servo, 2.0E-3, 1) 'Left drive shaft- 1.5mSec interval Call PulseOut(R_Servo, 1.5E-3, 1) 'Right drive shaft- 2mSec interval Next End Sub '-------------------------------------------------------------------------------