# Black and white line following

i am going to program 3pi for the compition in which the line change to black to white and background white to black and vica versa

i cant think how can i do it
any ideas?
I m thinking of adding one more sensor.

one more question
when 3pi calibrate does it make values if it is on black background and white line ?

Hello,

That is an interesting contest! Are your competitors all using 3pi robots, or do they get to use whatever they want?

I am not sure what you mean when you ask “does it make values”. The calibration routine reads the maximum and minimum values as it turns left and right, independently for each sensor. If I were you, I would think about whether you can do a simple check based on the actual values of the sensors to determine whether you are on a black line or a white line - with 5 sensors you should almost always be able to tell. The Maze Solving Example Project demonstrates a lot of similar decisions.

-Paul

i wanted to say that
when 3pi calibrate he takes high value as line (black), so if i am on black course with white line then does it atomatically start considering that low values of white as the line or he will just confuse

if he can distinguish then i will add a sensor ahead which tell me what type of course it is and change the calibrated value according to upcoming course

in pid code there is line

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

i may change this line according to course but what will be the values i will get on white on black ?

It is not 3pi exclusive contest and i dont think in whole India any body got 3pi

There is not going to be anything automatic about it until you program it! In the documentation you can see that there is a read_line_white() function that you can use when you are on a white line instead of a black line. But if you want to be really smart about it, you will write your own version of that function, that first determines what type of line you are on, and then applies the appropriate formula. Five sensors should be plenty, but you can definitely try to add another sensor if you think that will help.

-Paul

OK

assume i need to change algorithm from white to black TO black on white

what sensors reading should i get ?
if i use simple algorithm instead of PID then it will be slow.

Hello,
Have you looked at any of the decisions made in the maze solving code? For example, look at how it determines the end of the maze.

I have no idea what you mean by “if i use simple algorithm instead of PID then it will be slow.” PID is a simple algorithm, and you should not replace it with anything. I was suggesting that you replace the read_line() function with something better - and if you do not have a solution to the problem yet, why would you think that solution will be slow?

-Paul

read line gives numbers from 0-4000 right ?

i just want to ask you what are the changes for code for white on black ?

i think i have to make 2 read_line function one for black (the normal one) and other for white.

In normal word what should i do to get same 0-4000 values for white on black and how should i switch them, i will determine when change is by front 3 sensors reading (that what you meant by studying the line maze code ?)

thanks

The read_line() function returns the position of a black line, as a value from 0 to 4000. The read_line_white() function returns the position of a white line, as a value from 0 to 4000. They both also put values into the sensors array that are from 0 to 1000, where 0 is the whitest white and 1000 is the blackest black. The line position is just computed from those values according to the formula given in the command reference.

What I mean by studying the maze solving code is that you should look at what it does, understand how it works, and use something similar (but not exactly the same!) for your situation. Do you understand how it works?

-Paul

Hello.

The read_line() function in the 3pi library already supports detecting black lines or white lines. What paul is suggesting is that you modify the library code to intelligently figure out which version of that function to call based on what your sensors are seeing. For example:

``````unsigned int smart_read_line(unsigned int *sensorValues)
{
// look at the numbers in the sensorValues array to determine if you're on a black background
// or white background (e.g. maybe sum them together and see if the sum is greater than
// a certain threshold).
if (sensorValues[0] + sensorValues[1] + ... + sensorValues[4] > 2500)

}``````

- Ben

when 3pi calibrate what it does and there is other function i can use to get calibrated data

i want a function that give me 0 - 4000 number.
what is formula for black and white ?
I need to use this custom function instead of auto calibration.

Please tell code example or formula .

I don’t understand what you’re asking, but the library source code is include with the library download, so you can see for yourself how everything is done.

- Ben

thanks i didnt read the liberty resource

pololu.com/docs/0J18/17

now i got it

thank you again for help,

Ben

the change in liberty you are suggesting should be done inside main code right ?
i have trouble during transition period when bot will be half way on black and half on white side
should i ignore this condition or just make 3pi to go straight at slow rate until it reaches the other part completely because transition park have a straight line.

for ex
on black to white
senor no 1 , 3, 5 are black
and others are white

so should i change it there it self or wait until the bot is inside the other part of maze ( different color)

PID is cotinueus loop then where should i put the code for going straight for period where it overrides PID ? Its basic programming issue ,

I wasn’t suggesting you edit the library code itself, although you could. I was suggesting that you write a function in your own program that calls the appropriate library functions based on the situation. The library already has code for finding a white line on a black surface and a black line on a white surface. See the smart_read_line() function I wrote in an earlier post.

The transition from white to black (and vice versa) is certainly capable of causing a simple PID line-following algorithm to mess up. I recommend that you try to detect the transition as soon as possible and just have the robot drive in a straight line until you have fully made the transition. The code for doing this can just go inside your PID loop:

``````while (1)
{
while (transitioning())
{
set_motors(150, 150);  // drive straight
delay_ms(100);  // for 100 ms
}

// the rest of the PID code goes here...
}``````

You will need to write the function transitioning(), which returns 1 when you are between black and white and 0 when you are not.

- Ben

the smart line code given by ben checks some thresh hold value. But that value is from read_line or read_line_white. Because that threshold value will change its meaning according to what read line function we are using.

Plus i am thinking of using another line sensor module just to check it is on white or not.

And what to do with the breaks.This year arena is bit different.
Say ur opinion. BTW i won indian maze solving competition and many other competition do you want videos to post in video section ?

Hello.

I don’t understand the point you’re trying to make here:

Why do you want to use another sensor rather than making use of the built-in sensors on the 3pi?

The line-break in your course picture doesn’t seem like it will be as difficult to deal with as the acute angle or the angled transition from black to white. As long as you’re going straight and centered over the line when you hit the break, the 3pi should be able to detect the loss of the line while being pointed in the proper direction to pick the line up again across the gap; just program the 3pi to keep driving straight when the line suddenly disappears (but don’t do this when you lose the line to the right or the left). Note that you might need to drive the two motors at slightly different speeds in order to make the 3pi drive straight across the gap due to random variations in your 3pi’s motors. I suggest you conduct some tests to find left and right speeds close to the max speed of your line-following algorithm that cause your particular robot to drive in a straight line, and use these two speeds when the line abruptly disappears. You might also want to include code that shuts down the motors if you don’t find the line again after a certain period of time to prevent your 3pi from driving uncontrollably into a wall or spectators if things go unexpectedly wrong.

Note that you have to be careful not to confuse the sharp kink in the line with the gap, because it seems like you’re also likely to abruptly lose the line if you hit that acute angle at a high speed. You should be able to use the outer sensors on the 3pi to see the sharp-angle turn coming, but it might take some clever coding.

I’d love to see any videos you have of your 3pi competing!

- Ben

A post was merged into an existing topic: Doubts in programming