Linefollowing Robot Using the BS2px?

Hello, I am creating a linefollowing robot using the BS2px, utilizing its new COMPARE command. I am using five Fairchild QRD1114 IR sensors, connected together and then to one of the Stamp’s inputs. The other input required for the COMPARE function is connected to a 10k trimpot that acts as a variable voltage divider, to adjust the threshold of when the sensors activate.

The sensors are read by turning on their corresponding LEDs one at a time, and the values are COMPAREd in sequence to the threshold set by the pot.

I am having trouble getting a reading from these sensors, as they will only output a value of 0 (I have tried adjusting the potentiometer, moving the sensors closer to the ground, etc.) and nothing seems to be working. I am wondering if anyone else has experience using the BS2px, or the COMPARE command?

One thing I should mention, is that my Stamp appears to be slightly broken :blush: , but only slightly. Pins 8 and 12 do not output (although they are not used in the sensing circuit), and I plan on getting a new Stamp ASAP. This may be the source of my problem, however, I would still like to know if there is a more fundamental issue at work here. Thanks for any help.

Unfortunately, I can’t post any pictures right now as this is my first post. I will try to get them up as soon as possible

Once again, Thanks for any responses.

-Quarky

Mmmmm, The BS2px is the top of the line Basic Stamp! I envy you! I plan to get the BS2px as my next MCU.

Can you post your code? Lets have look at the section you are having problems with. If the entire program is not to long, post the whole thing.

When you compose a reply message use the Code button then paste your code and then click the code button again to close the tags.

paste code between [code] and [/code]

It’s sort of long, so I’ll leave out the turning subroutines and such.
Here it is:

' {$STAMP BS2px}
' {$PBASIC 2.5}

'This linefollower program uses the voltage comparator of the BS2px microcontroller to read the inputs of five sensors.
'The program turns on one LED at a time and reads the value of the sensor that it is connected to.
'The value is compared to a constant threshold which is used to determine the presence of a black line which the robot is
'to follow.
'If the value is greater than the threshold on the right sensors then it will cause the robot to activate its left drive
'motor, turning the robot right.
'If the value is greater than the threshold on the left sensors then it will cause the robot to activate its right drive
'motor, turning the robot left.


Sensor              PIN 1
Thresh              PIN 2
Sensor_LED1         PIN 3
Sensor_LED2         PIN 4
Sensor_LED3         PIN 5
Sensor_LED4         PIN 6
Sensor_LED5         PIN 7
F_Dir_MR            PIN 8           'Forward Direction Right Motor
R_Dir_MR            PIN 9           'Reverse Direction Right Motor
F_Dir_ML            PIN 10          'Forward Direction Left Motor
R_Dir_ML            PIN 11          'Reverse Direction Left Motor
LED2                PIN 12
LED3                PIN 13
LED4                PIN 14
LED5                PIN 15
LED1                PIN 0

SensorValue      VAR Bit            'Stores the value of the sensor as compared to the threshold voltage
RSensor          VAR Bit            'Right Sensor value variable
RCSensor         VAR Bit            'Right Center Sensor value variable
CSensor          VAR Bit            'Center Sensor value variable
LCSensor         VAR Bit            'Left Center Sensor value variable
LSensor          VAR Bit            'Left Sensor value variable
PinCommand       VAR Byte           'LED Sensor pin commanding variable
Temp             VAR Word           'General variable for counters, timers etc. (optional)

Start:
  HIGH LED1
  PAUSE 200
  LOW LED1
  FOR PinCommand = 12 TO 15         'Do a BLiFNAR sequence while waiting to make sure sensors will register properly
    HIGH PinCommand
    PAUSE 200
    LOW PinCommand
  NEXT
  FOR PinCommand = 14 TO 12
    HIGH PinCommand
    PAUSE 200
    LOW PinCommand
  NEXT
  HIGH LED1
  PAUSE 200
  LOW LED1
  CONFIGPIN PULLUP, %0000000000000010     'Set pullup resistor for sensor input
  CONFIGPIN THRESHOLD, %0000000000000110  'Set logic threshold to CMOS, for easier tuning purposes

Linefollower:
DO
  LOW LED1                          'Turn off all LEDs
  LOW LED2
  LOW LED3
  LOW LED4
  LOW LED5
  GOSUB Forward                     'Go forward
  GOSUB Line                        'Read the value of the line sensors
  IF RSensor = 1 THEN Sharp_Right   'Choose which direction to go based on sensor values
  IF LSensor = 1 THEN Sharp_Left
  IF RCSensor = 1 THEN Soft_Right
  IF LCSensor = 1 THEN Soft_Left
  IF CSensor = 1 THEN Linefollower
LOOP                                'Loop again

Line:
  PinCommand = 3                    'Set PinCommand variable to initial state to read sensor values
  FOR PinCommand = 3 TO 7           'FOR... NEXT loop that uses PinCommand as the incremental variable
    HIGH PinCommand                 'Activate the Sensor LED that PinCommand is equal to
    COMPARE 2, SensorValue          'Compare Threshold voltage to sensor voltage as set by the trimpot - store the result in SensorValue
    IF PinCommand = 3 THEN SensorValue = RSensor   'Stores SensorValue for right sensor in RSensor
    IF PinCommand = 4 THEN SensorValue = RCSensor  'Stores SensorValue for right center sensor in RCSensor
    IF PinCommand = 5 THEN SensorValue = CSensor   'Stores SesnorValue for center sensor in CSensor
    IF PinCommand = 6 THEN SensorValue = LCSensor  'Stores SensorValue for left center sensor in LCSensor
    IF PinCommand = 7 THEN SensorValue = LSensor   'Stores SensorValue for left sensor in LSensor
    LOW PinCommand
  NEXT
  RETURN                            'Go back to LINEFOLLOWER subroutine

The parts I appear to be having trouble with are the Linefollower and Line subroutines. I tried debugging the sensor values, and it only gave me 0s for all the sensors.

You did a fine job with the code writing as I was able to follow along well but it is still always hard to understand someone elese code.

I dont uderstand why you have the LEDs turn on and off in the beginning unless you are just checking to make sure everything is wired up ok?

also, this will not help if you are getting a value of 0 but you might want to try using this:

IF RSensor > 1 THEN Sharp_Right 'Choose which direction to go based on sensor values IF LSensor > 1 THEN Sharp_Left IF RCSensor > 1 THEN Soft_Right IF LCSensor > 1 THEN Soft_Left IF CSensor > 1 THEN Linefollower

The greater than will allow for the motors to work if the value is greater than one else nothing will happen if the value is say 2 or three. This is probably not an issue if the only two values returned are either a one or a zero, then the " = " operator is fine.

Perhaps you should break it down to just one sensor and try to get that comunicating rather than try all at once. Once you get one working, you can expand on that.

Thanks for the reply! It is well appreciated. :slight_smile:

As to the LED sequence at the beginning: BLiFNAR = Blinky Lights For No Apparent Reason. I love flashy things! (It doesn’t serve any actual purpose, but it gives it a “cool” factor 8) )

Thank you for the suggestion for the code. The COMPARE command stores a result in a Bit Variable as either a 1 or a 0: so I guess I can’t use your method :frowning: .

However, I will try checking one sensor at a time to find the problem. Thanks once again for the suggestions! :smiley:

-Quarky

Let me know what happens with just one sensor. I would also try to have the debug widow display real time results so that you know right away if the sensors are working.

Ok, so I’ve done little work over the past few days (what with homework and all), but I have managed to get to checking the sensors. I tried checking the code, putting debugs after every new subroutine, and to show the values of the sensors, and still nothing. I’m wondering if I’ve wired the sensors incorrectly, so here is a schematic of what I’m doing:

http://img123.imageshack.us/img123/2434/12838895rz0.png

PIN 1 and PIN 2 are comparator input pins. The potentiometer acts as a voltage divider to produce an adjustable threshold (I’ve tried adjusting it all the way from limit to limit, still doesn’t work). The T(x) and D(x) pairs are the QRD1114s.

I’m still getting a zero value fr every sensor, however when using my video camera I did notice that the IR LEDs of the QRD1114s were flashing when they were supposed to, so I don’t think the sensors are damaged.

Thanks for any input!
-Quarky

You need a current limiting resistor for each QRD1114.

I use 330 ohms, typically.

  • Jon

What’s the voltage on Pin 1 when the LED’s are off?

When one is on?

When more than ome is on?

You must be turning on the sensors individually, and reading them back in multiplexed on a single line. Thru me for a while! I’d have left 'em all on, and watched the 5 outputs individually, but that’s just me.

Jon picked up the individual series resistors for the LEDs already.

uP port configuration OK?

Alan KM6VV

With the QRD1114, and the appropriate pull-up resistor, you can turn the sensor into a digital sensor, which can be read using any digital I/O line.

I don’t remember the value, but I think it was around 6.8 K ohms…

  • Jon

Sorry Jon, :blush: I forgot to draw in the current limiting resistors i already have on the IR LEDs. Thanks for pointing that out though.

The BS2px automatically outputs either a 1 or a 0, depending on if input voltage 1 is greater or less than input voltage 2. I’m using a CMOS logic threshold level for this project, so 1 would be 2.5 V or greater and 0 would be less than 2.5V. As such, I don’t think I can directly read the voltage on the input of the stamp, sorry. :frowning:

You’re absolutely right, KM6VV, and the only reason I didn’t choose to do them separately is that the BS2px only has one comparator input pin. Maybe if I make another one, and use some voltage comparator chips… :wink:

Actually, that’s what the 10K resistor in the schematic is for. It creates a voltage divider between itself and the QRD1114, which can then be fed to the comparator.
parallax.com/Portals/0/Downl … /nv116.pdf
Follow this link, and on page 4 you’ll see the schematic I based my robot on.

Thanks,
-Quarky

Um, what… :open_mouth: You put the black lead on the ground and the red lead on pin one in your posted schematic. You really need to know the voltages present if you want to troubleshoot your problem. Hope this helps, Jim

:open_mouth: How could I have been so stupid??? That just blew right over my head.

Okay, now that I’ve had my senses knocked into me, the voltages are:

All LEDs off: 6.25V

All LEDs on: 0.16V

One LED on: 0.23V

Thanks for bringing me back down to earth, Jim. :slight_smile:

-Quarky

I understand, you’re using a comparator instead of an A/D. What I’m telling you is with the appropriate pullup resistor, the output from the sensor itself is a digital signal, at least as far as line follower/mini sumo usage goes.

The values end up being about 0.2 volts and 4.6 volts (for black and white, don’t remember which is which). Thus the output from the sensor can go directly to the input pin on your stamp.

  • Jon

Thanks, Jon!

Would that mean replacing the current resistor in the schematic with the 6.8 K one you suggest?

And also, would this method work for multiplexed sensors on a single line?

Again, thank you for the suggestion.

UPDATE:

I tested the voltages again with the sensors against black and white surfaces:

WHITE:
All LEDs off: 4.35V
All LEDs on: 0.17V
One LED on: 0.27V

BLACK:
All LEDs off: 6.75V
All LEDs on: 3.45V
One LED on: 6.40V

-Quarky

I’ve never seen a set of sensors like this hooked up to one input, and I can’t really say I understand why you would want to do it this way for a line follower.

Typically, with a 5-sensor line follower, you have each sensor hooked up independently, and thus can read each sensor independently, so you can see if the robot is starting to veer off being centered on the line.

To do it that way, you need one pullup resistor and one current limited resistor for each sensor.

You should be feeding the LED with 5 volts, and the pullup should be connected to 5 volts as well.

  • Jon

it’s no real difference. I mean it takes the same number of I/O piuns either way…

The V+ must be connected directly to a 6vdc battery? You should have it connected to 5vdc. What’s the comparitor supply voltage?

From what I am seeing the sensors are working great, but the comparitor is not. If the comparitor is powered up at 5vdc then there is a problem with putting 6vdc + on one of the inputs.

oops I just reread the original post. I need to read up on the compare command. It actually works on analog voltages? Looking…