I'm moving on to other ideas of racing with Ringos and thought I should post my line following code. This I'm confident is robust to work. There is room for tweaking speed for your own tracks and find your own improvements. Hope this help someone out there, I'm just learning like many of you.
Code: Select all
Line Follow by Steve Bolton
if you base your code on mine please leave my name in place and share on the plumgeek forum for others to learn from.
A simple but robust line follow code runs well on an oval circuit using black electricians tape on a white topped desk.
Its meant to run on a surface straddling a line made of black tape though it can work with a 15mm line of black paint.
it runs straight but if it drifts the eyes detect the line and it readjusts.
Work best on a 50cm wide circular track, hard corners are a problem
/* set up hardware */
HardwareBegin(); //initialize Ringo's brain to work with his circuitry
PlayStartChirp(); //Play startup chirp and blink eyes
SwitchMotorsToSerial(); //Call "SwitchMotorsToSerial()" before using Serial.print functions as motors & serial share a line
delay(2000); // 2 sec delay before start
/* declare variables */
int leftOn, leftOff, rightOn, rightOff, rearOn, rearOff; int leftDiff,rightDiff, rearDiff; int speedleft, speedright;
digitalWrite(Source_Select, LOW); //select underside light sensors
digitalWrite(Edge_Lights, HIGH); //turn on IR light sources
delayMicroseconds(500); //let sensors stabilise
leftOn = analogRead(LightSense_Left);
rightOn = analogRead(LightSense_Right); rearOn = analogRead(LightSense_Rear);
SetAllPixelsRGB(0, 0, 0); //all lights off
//set default speed of 50
speedleft = 50;
speedright = 50;
// use Motors() to turn using multiplier
if (leftOn < 250)
SetPixelRGB( 5, 0, 50, 0);
if (rightOn < 250)
SetPixelRGB( 4, 0, 50, 0);
if (rearOn < 250)
SetPixelRGB( 0, 0, 50, 0);