Home | About | Projects | Blog | Contact | Useful Links

 


Robotics Resource

 



Programming the Basic Stamp 2

for Simple Line Following

There are many different microcontrollers used for line following. The Basic Stamp 2 is only one of many. Others would be Arduino, PIC, NXT, Motorola, and more. As you can see with the following program, the basic idea is to adjust the speed and direction of the motors while the robot is sensing the line.

On the straight part of a track, the robot would go at diferent speeds than when the robot senses a curve. Sending pulses to the motors at specific speeds on specific sides of the robot for specific conditions on the line following track is the basic premis of the program.

To successfully complete the line following track, the robot must be able to rotate right, pivot right, curve right, go straight ahead, curve left, pivot left, rotate left, and do nothing.

As an example of what the program is doing is that when the robot senses a condition in which it needs to curve left, is: the motor attached to pin 13 is pulsed with 850ms pulses while the motor attached to pin 12 is pulsed in 700ms pulses. This will cause the motor attached to pin 13 to move faster than the motor attached to pin 12, thus causing the robot to curve left.



' {$STAMP BS2}
' {$PBASIC 2.5}

' LineFollowWithCheckQtis.bs2

' Navigates based on values acquired with the Check_Qtis subroutine.

qtis VAR Nib ' qti black/white states
OUTB = %1111 ' Set OUTB bits to 1

DO ' Main DO...LOOP
GOSUB Check_Qtis ' Get QTI states

SELECT qtis ' Control servo speeds/directions
CASE %1000 ' Rotate right
PULSOUT 13, 650
PULSOUT 12, 650
CASE %1100 ' Pivot right
PULSOUT 13, 750
PULSOUT 12, 650
CASE %0100 ' Curve right
PULSOUT 13, 800
PULSOUT 12, 650
CASE %0110 ' Straight ahead
PULSOUT 13, 850
PULSOUT 12, 650
CASE %0010 ' Curve left
PULSOUT 13, 850
PULSOUT 12, 700
CASE %0011 ' Pivot left
PULSOUT 13, 850
PULSOUT 12, 750
CASE %0001 ' Rotate left
PULSOUT 13, 850
PULSOUT 12, 850
CASE ELSE ' Do nothing
PAUSE 3
ENDSELECT
LOOP

Check_Qtis:
' Result -> qtis variable. 0 means white surface, 1 means black
' surface.

DIRB = %1111 ' P7..P4 -> output
PAUSE 0 ' Delay = 230 us
DIRB = %0000 ' P7..P4 -> input
PAUSE 0 ' Delay = 230 us
' PULSOUT UnusedPin, 0 ' Delays = 208 + (Duration*2) us
qtis = INB ' Store QTI outputs in INB

RETURN