Andy Lindsay (Parallax)
12-08-2005, 03:57 AM
This circuit below and the attached programs provide distance measurements that are better than the examples in Robotics with the Boe-Bot, Chapter 8.· The measurements split a·one foot (approx) distance into eight zones that are pretty evenly spaced, and there's next to no jitter in the measurements.·
···· http://forums.parallax.com/attachment.php?attachmentid=39689
The example programs make the distance measurement by placing different resistances in series with the IR LEDs.· The different series resistances are set by manipulating the input/output states of P4, P5, and P6.· In the initialization of the program, P4 to P7 are all set LOW (connected to Vss), but left as inputs with the command OUTB = %0000.· After that, the program manipulates which of those pins are connected to VSS and which of them are inputs with by setting DIRB equal to different values.· For example, DIRB = %0101 sets P6 and P4 to outputs, but leaves P5 an input.· Since·earlier in the program, the OUTB was set to %0000, any I/O pin that gets changed to an output will send the·LOW signal.· If DIRB = %0001,·that makes P6 and P5 inputs, but P4 is·output-low.· That means·P4 is connected·to Vss.· Whenever anything is connected to Vss, it is commonly referred to as connected to ground, or "grounded".·
The example programs use FOR DIRB = 1 to 7...NEXT to take P4, P5, and P6 through the following input/output combinations: %0001, %0010, %0011, %0100, %0101, %0110, %0111.· Each different OUTB setting causes different combinations of parallel resistors to be in series with the IR LEDs.· The thing about different parallel combinations of resistors is that they provide different levels of series resistance with the IR LEDs.··From·Robotics with the Boe-Bot, Chapter 7, Activity #3, we know that different series resistances equate·to different IR LED headlight brightnesses.··The brighter the IR LED, the further·the IR·receiver·can see to detect an object.· Inside the FOR...NEXT loop, the code counts the number of detections each side saw, and equates that to a distance.
Here is the equation for calculating the equivalent resistance of one or more parallel resistors:
· Req = (R1-1 + R2-1 + R3-1 + ... + RN-1)-1
Here is another form of the same equation:
· Req = 1 / (1/R1 + 1/R2 + 1/R3 + ... + 1/RN)
Let's say DIRB = %0111.· That means that all three resistors are connected to Vss and are part of the circuit.· To calculate the equivalent resistance in series with the IR LED, it's:
· Req = 1 / (1/2000 + 1/4700 + 1/10000) = 1230 ohms
Here's another example with DIRB = %0101.· With this, configuration, P6 and P4 are grounded, but P5 is an input.· So the 4.7 k resistor is not part of the circuit, and we only want to calculate the parallel resistance for the 10 k and 2 k resistors.· The notation for parallel resistance is R1 || R2, etc.· So, this calculation is for 2 k || 10 k:
· Req·= 1 / (1/2000 + 1/10000) = 1667 ohms.
The different·series resistances you can get from the seven different combinations of OUTB are: 1230, 1402, 1667, 2000, 3197, 4700, and 10,000.
Both programs below are also attached to this post.
This example program tests the distance for both IR Detectors and displays it in the Debug Terminal.· You should be able to get values from 0 to 7, wich corresponds to between 1 inch and 1 foot.·
' -----[ Title ]--------------------------------------------------------------
' TestDistnaceWithNewCircuit.bs2
' Test sensitivity of new IR distance circuit.
' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
' -----[ Variables ]----------------------------------------------------------
irDetectLeft VAR Bit
irDetectRight VAR Bit
distanceLeft VAR Nib
distanceRight VAR Nib
' -----[ Initialization ]-----------------------------------------------------
DEBUG CLS, "Left Right", CR
OUTB = %0000
' -----[ Main Routine ]-------------------------------------------------------
DO
GOSUB Get_Ir_Distances
DEBUG " ", DEC1 distanceLeft, " ", DEC1 distanceRight, CRSRX, 0
PAUSE 100
LOOP
' -----[ Subroutine - Get IR Distances ]--------------------------------------
Get_Ir_Distances:
distanceLeft = 0
distanceRight = 0
FOR DIRB = 1 TO 7
FREQOUT 7,1,38500
irDetectLeft = IN8
distanceLeft = distanceLeft + irDetectLeft
FREQOUT 3,1,38500
irDetectRight = IN2
distanceRight = distanceRight + irDetectRight
NEXT
RETURN
This example program is the following Boe-Bot program from Robotics with the Boe-Bot, Chapter 8, Activity #2.· With this program, the Boe-Bot should be able to lock onto and follow an object with much less jitter.
·
' -----[ Title ]--------------------------------------------------------------
' FollowingBoeBotWithNewDistanceCircuit.bs2
' Boe-Bot adjusts its position to keep objects it detects in zone 4.
' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
DEBUG "Program Running!"
' -----[ Constants ]----------------------------------------------------------
Kpl CON -15
Kpr CON 15
SetPoint CON 4
CenterPulse CON 750
' -----[ Variables ]----------------------------------------------------------
irDetectLeft VAR Bit
irDetectRight VAR Bit
distanceLeft VAR Nib
distanceRight VAR Nib
pulseLeft VAR Word
pulseRight VAR Word
' -----[ Initialization ]-----------------------------------------------------
FREQOUT 4, 2000, 3000
' -----[ Main Routine ]-------------------------------------------------------
DO
GOSUB Get_Ir_Distances
' Calculate proportional output.
pulseLeft = SetPoint - distanceLeft * Kpl + CenterPulse
pulseRight = SetPoint - distanceRight * Kpr + CenterPulse
GOSUB Send_Pulse
LOOP
' -----[ Subroutine - Get IR Distances ]--------------------------------------
Get_Ir_Distances:
distanceLeft = 0
distanceRight = 0
FOR DIRB = 1 TO 7
FREQOUT 7,1,38500
irDetectLeft = IN8
distanceLeft = distanceLeft + irDetectLeft
FREQOUT 3,1,38500
irDetectRight = IN2
distanceRight = distanceRight + irDetectRight
NEXT
RETURN
' -----[ Subroutine - Get Pulse ]---------------------------------------------
Send_Pulse:
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 5
RETURN
Post Edited By Moderator (Jessica Uelmen (Parallax)) : 8/25/2010 5:55:58 PM GMT
···· http://forums.parallax.com/attachment.php?attachmentid=39689
The example programs make the distance measurement by placing different resistances in series with the IR LEDs.· The different series resistances are set by manipulating the input/output states of P4, P5, and P6.· In the initialization of the program, P4 to P7 are all set LOW (connected to Vss), but left as inputs with the command OUTB = %0000.· After that, the program manipulates which of those pins are connected to VSS and which of them are inputs with by setting DIRB equal to different values.· For example, DIRB = %0101 sets P6 and P4 to outputs, but leaves P5 an input.· Since·earlier in the program, the OUTB was set to %0000, any I/O pin that gets changed to an output will send the·LOW signal.· If DIRB = %0001,·that makes P6 and P5 inputs, but P4 is·output-low.· That means·P4 is connected·to Vss.· Whenever anything is connected to Vss, it is commonly referred to as connected to ground, or "grounded".·
The example programs use FOR DIRB = 1 to 7...NEXT to take P4, P5, and P6 through the following input/output combinations: %0001, %0010, %0011, %0100, %0101, %0110, %0111.· Each different OUTB setting causes different combinations of parallel resistors to be in series with the IR LEDs.· The thing about different parallel combinations of resistors is that they provide different levels of series resistance with the IR LEDs.··From·Robotics with the Boe-Bot, Chapter 7, Activity #3, we know that different series resistances equate·to different IR LED headlight brightnesses.··The brighter the IR LED, the further·the IR·receiver·can see to detect an object.· Inside the FOR...NEXT loop, the code counts the number of detections each side saw, and equates that to a distance.
Here is the equation for calculating the equivalent resistance of one or more parallel resistors:
· Req = (R1-1 + R2-1 + R3-1 + ... + RN-1)-1
Here is another form of the same equation:
· Req = 1 / (1/R1 + 1/R2 + 1/R3 + ... + 1/RN)
Let's say DIRB = %0111.· That means that all three resistors are connected to Vss and are part of the circuit.· To calculate the equivalent resistance in series with the IR LED, it's:
· Req = 1 / (1/2000 + 1/4700 + 1/10000) = 1230 ohms
Here's another example with DIRB = %0101.· With this, configuration, P6 and P4 are grounded, but P5 is an input.· So the 4.7 k resistor is not part of the circuit, and we only want to calculate the parallel resistance for the 10 k and 2 k resistors.· The notation for parallel resistance is R1 || R2, etc.· So, this calculation is for 2 k || 10 k:
· Req·= 1 / (1/2000 + 1/10000) = 1667 ohms.
The different·series resistances you can get from the seven different combinations of OUTB are: 1230, 1402, 1667, 2000, 3197, 4700, and 10,000.
Both programs below are also attached to this post.
This example program tests the distance for both IR Detectors and displays it in the Debug Terminal.· You should be able to get values from 0 to 7, wich corresponds to between 1 inch and 1 foot.·
' -----[ Title ]--------------------------------------------------------------
' TestDistnaceWithNewCircuit.bs2
' Test sensitivity of new IR distance circuit.
' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
' -----[ Variables ]----------------------------------------------------------
irDetectLeft VAR Bit
irDetectRight VAR Bit
distanceLeft VAR Nib
distanceRight VAR Nib
' -----[ Initialization ]-----------------------------------------------------
DEBUG CLS, "Left Right", CR
OUTB = %0000
' -----[ Main Routine ]-------------------------------------------------------
DO
GOSUB Get_Ir_Distances
DEBUG " ", DEC1 distanceLeft, " ", DEC1 distanceRight, CRSRX, 0
PAUSE 100
LOOP
' -----[ Subroutine - Get IR Distances ]--------------------------------------
Get_Ir_Distances:
distanceLeft = 0
distanceRight = 0
FOR DIRB = 1 TO 7
FREQOUT 7,1,38500
irDetectLeft = IN8
distanceLeft = distanceLeft + irDetectLeft
FREQOUT 3,1,38500
irDetectRight = IN2
distanceRight = distanceRight + irDetectRight
NEXT
RETURN
This example program is the following Boe-Bot program from Robotics with the Boe-Bot, Chapter 8, Activity #2.· With this program, the Boe-Bot should be able to lock onto and follow an object with much less jitter.
·
' -----[ Title ]--------------------------------------------------------------
' FollowingBoeBotWithNewDistanceCircuit.bs2
' Boe-Bot adjusts its position to keep objects it detects in zone 4.
' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
DEBUG "Program Running!"
' -----[ Constants ]----------------------------------------------------------
Kpl CON -15
Kpr CON 15
SetPoint CON 4
CenterPulse CON 750
' -----[ Variables ]----------------------------------------------------------
irDetectLeft VAR Bit
irDetectRight VAR Bit
distanceLeft VAR Nib
distanceRight VAR Nib
pulseLeft VAR Word
pulseRight VAR Word
' -----[ Initialization ]-----------------------------------------------------
FREQOUT 4, 2000, 3000
' -----[ Main Routine ]-------------------------------------------------------
DO
GOSUB Get_Ir_Distances
' Calculate proportional output.
pulseLeft = SetPoint - distanceLeft * Kpl + CenterPulse
pulseRight = SetPoint - distanceRight * Kpr + CenterPulse
GOSUB Send_Pulse
LOOP
' -----[ Subroutine - Get IR Distances ]--------------------------------------
Get_Ir_Distances:
distanceLeft = 0
distanceRight = 0
FOR DIRB = 1 TO 7
FREQOUT 7,1,38500
irDetectLeft = IN8
distanceLeft = distanceLeft + irDetectLeft
FREQOUT 3,1,38500
irDetectRight = IN2
distanceRight = distanceRight + irDetectRight
NEXT
RETURN
' -----[ Subroutine - Get Pulse ]---------------------------------------------
Send_Pulse:
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 5
RETURN
Post Edited By Moderator (Jessica Uelmen (Parallax)) : 8/25/2010 5:55:58 PM GMT