3pi following white line on black blackground

Hi, I am trying to use 3pi to run on a straight white track with speed and stop after T-junction on black. The background is black. Any idea how to do it. I tried line-follower coding but it will turn at T-junction but not go straight to finish and stop on black. Another thing is that it keeps to the side of the line and not run in the middle. Pls help

Hello.

Have you looked at the maze solving code example? It has some code for detecting intersections and stopping points. You might be able to adapt it to your needs. As for detecting white on black background, it is a matter of inverting your expectations of the reflectance sensor readings.

- Ryan

Thanks Ryan. I did try that but I could not incorporate into the line follower program coding due to my poor knowledge of C++. After going through the maze solver program, isn’t there a simple straight forward coding since we are not testing the course at all. We know it will come to a T-junction so all sensors see white at the same time. I don’t quite get you about the reflectance part. Is 255 the max speed of the motor? Is it advisable to run on the straight track on max speed?

Hello.

I don’t understand what you mean when you ask:

My point about inverting the reflectance sensor values was that the maze solver code is designed for a black line on a white background, so you would need to invert the values of the code that checks for intersections to get it to work for you. Here is the relevant code for the maze solver:

``````void follow_segment()
{
int last_proportional = 0;
long integral=0;

while(1)
{
// Normally, we will be following a line.  The code below is
// similar to the 3pi-linefollower-pid example, but the maximum
// speed is turned down to 60 for reliability.

// Get the position of the line.
unsigned int sensors[5];

// The "proportional" term should be 0 when we are on the line.
int proportional = ((int)position) - 2000;

// Compute the derivative (change) and integral (sum) of the
// position.
int derivative = proportional - last_proportional;
integral += proportional;

// Remember the last position.
last_proportional = proportional;

// Compute the difference between the two motor power settings,
// m1 - m2.  If this is a positive number the robot will turn
// to the left.  If it is a negative number, the robot will
// turn to the right, and the magnitude of the number determines
// the sharpness of the turn.
int power_difference = proportional/20 + integral/10000 + derivative*3/2;

// Compute the actual motor settings.  We never set either motor
// to a negative value.
const int max = 60; // the maximum speed
if(power_difference > max)
power_difference = max;
if(power_difference < -max)
power_difference = -max;

if(power_difference < 0)
set_motors(max+power_difference,max);
else
set_motors(max,max-power_difference);

// We use the inner three sensors (1, 2, and 3) for
// determining whether there is a line straight ahead, and the
// sensors 0 and 4 for detecting lines going to the left and
// right.

if(sensors[1] < 100 && sensors[2] < 100 && sensors[3] < 100)
{
// There is no line visible ahead, and we didn't see any
// intersection.  Must be a dead end.
return;
}
else if(sensors[0] > 200 || sensors[4] > 200)
{
// Found an intersection.
return;
}

}
}
``````

The last if statement shows where it detects an intersection. However, since you are doing white on black, you need to change the if statement (and other statements that rely on the sensor values, like read_line) to something like this:

``````		else if(sensors[0] < 800|| sensors[4] < 800)
{
// Found an intersection.
return;
}
``````

255 is the max speed of the motors. It is okay to run a max speed, if your PID algorithm is good enough to stay on the line, and you have some safety code that turns off the 3pi’s motors, if it doesn’t see any lines for a little bit.

- Ryan

Thanks Bryan,
I was reading the maze solver codes and they suggested putting it on test run and avoid U-turns and on the real run it will not hit the U-turn. Hence, if that is possible then I thought we could avoid the testing and go straight for the T-junction.
The track is very long, more than 5 metre.
I will try using your code and see how it goes.