# How to make algorithm for an artificial intelligence line follower.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
hi guys,

I have decided to make a line follower robot which sound easier but viewing the arena of it.
makes me confuse how to make control over robot for deciding the right path asgive here in end.

any help will be appreciated,
thanks

#### Attachments

• 45.4 KB Views: 184
Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
Many line following algorithms are out there as open source.

To get a start from scratch on it, simply "think like the robot", e.g. assume you only have the inputs and outputs your uC has. What do you store to know which way to turn if you run off a line? What do you "see" when the line is curving?

5 sensors are often used for line following, though you can get by with 2 or even 1. The extra sensors will help decide what to do when the bot crosses a line at a 90 degree angle. Going back to "thinking like the bot", what would the sensors give for input in such a case? What should the motors do?

Generally, you need the robot to figure out the following:
1) Find a line, either light on dark or dark on light, you can start with it hardcoded for dark on light

2) how to follow the line, keep the sensor over the line, when it hits an edge, other sensors will let you know which way the line went, but with a single sensor, you need to stop and pivot back and forth to find an edge again to continue.

3) Deal with curves in line, part of #2

4) Deal with perpendicular intersection

5) Obstacle detection at all times in forward direction so if off course, it won't ram into something you didn't intend to.

6) Advanced: How to handle oblique intersections, the contests usually state how they should be handled, so that needs to be an option that can be set if you are entering contests.

7) Keep track of any indecision points so you can work through the code and give it a better answer.

There really isn't any "Artificial Intelligence". An autonomous robot is only as smart as the person who programmed it, and what he included in foresight.

Lastly, though this is a big list, don't try to bite off too much at once, since it'll get you disgruntled. I'd suggest starting with a platform with motors, 5 sensors for lines, 1 sensor at rear to sense edge of table (if running on table), and collision detection switches. Then start with very basic patterns, like a large circle, then throw in harder stuff and rewrite the code to deal with the new situations seen.

Try to avoid "spaghetti code", instead of adding more if/then statements, try to integrate new solutions into the overall algorithm.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi again,

Will LDR will be better than IR/Photo diode??
and the 5 sensors should be straight or in vee shape??
But how T junction will be sensed?

#### Georacer

Joined Nov 25, 2009
5,182
Sloppy picture of the arena, too bad for the organizing committee.

Other than that, good for you, that you enter such a contest; it can give you lots of experience.

If I were you, I would use one light or colour sensor in each side of the robot. In the cases where both sensors will trigger (the T-intersections) I would have it preprogrammed to right-left-right-right-left-left. It's always good to build the course in your house to test it beforehand.

Do you know what the dynamic strips are? if they're just extended black areas, you will have to add some more commands to your routine, and make it right-left-straight-right-right-straight-left-left.

Also the checkpoints confuse me. The rules state that they will be laid at 50% and 80% of the track, but the map clearly shows different.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi again,

Also the checkpoints confuse me. The rules state that they will be laid at 50% and 80% of the track, but the map clearly shows different.
There are just for marks..!!
Do you know what the dynamic strips are? if they're just extended black areas, you will have to add some more commands to your routine, and make it right-left-straight-right-right-straight-left-left.
But i think there should be time limit in command or with the help of sensors??

I would use one light or color sensor in each side of the robot.Please explain more, will IR are bad for this task??

#### Georacer

Joined Nov 25, 2009
5,182
Hi again,
But i think there should be time limit in command or with the help of sensors??
I didn't get what you said here. Please explain with more words. Sensors will help you follow your line, but in case the line breaks in a predetermined spot, you will have to tell your robot to keep going straight.

Please explain more, will IR are bad for this task??
It seems that other people have used IR LEDs and sensors with success, so you could go with it. I have never done it before.

Just Google Line Following Robot and go with the flow. There are ready schematics and code to begin with.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829

Hi again,
But i think there should be time limit in command or with the help of sensors??

I didn't get what you said here. Please explain with more words. Sensors will help you follow your line, but in case the line breaks in a predetermined spot, you will have to tell your robot to keep going straight.
OK, as per your logic right-left-straight-right-right-straight-left-left the robot will move but how it will decide to move in left right direction??

#### RiJoRI

Joined Aug 15, 2007
536
... OK, as per your logic right-left-straight-right-right-straight-left-left the robot will move but how it will decide to move in left right direction??
This is the "cheater" method -- every time the robot comes to an intersection, it looks up the next direction to take from your list. Of course, if the course gets changed a little, your robot is in trouble!

An AI bot would need to keep track of its decisions so when it backtracks to an intersection, it can take the opposite direction.

An idiot bot would track the EDGE of the mark, assuming the mark is wide enough. When it comes to an intersection, it ALWAYS goes right or left, as you have programmed it. This is the equivalent of a human going through a maze by keeping one hand against the left (or right) wall.

--Rich

#### Georacer

Joined Nov 25, 2009
5,182
Usually in such contests the track is set and well specified from before.

If you arrive at the contest and notice a changed track or altered rules, that means that some team with strings to pull didn't make it to the final stage and tried to bring the course to fit its robot.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
if the course gets changed a little, your robot is in trouble!
exactly ........what i am thinking of ...
here is the solution...

If you arrive at the contest and notice a changed track or altered rules

#### thatoneguy

Joined Feb 19, 2009
6,359
exactly ........what i am thinking of ...
here is the solution...
What you should do is make a mockup of the track at home on poster board, using electrical tape for the line.

Build your robot, just make sure to put a shield around the receivers at front so they don't get interference from either the transmitter or other bots running at the same time. Doing so will make using IR no problem. I think robotroom.com has a ton of good info for line followers, but he goes a bit overboard on his, having LCD displays and whatnot. He does describe a great 5 emitter/receiver pickup and how to build it.

If speed isn't an issue, and only completing the course is, then you can use servos with the electronic guts ripped out as drive motors. Just wire up + and - to the motor, and the gears in the servo head will slow it down, maybe too much if speed is needed, but you can use 6" wheels to get the speed back. Otherwise, faster gearhead motors will cost a bit more.

Build it basic, so that when you start it on a line it follows it straight. Then add curves/corners, and change code to fit, keep doing so until you have a duplicate of their track so your robot can go through and find everything. It is a bunch of steps and a bunch of try / fail to get it right, but the results are well worth it.

Trying to build a bot and taking it to a competition or show without testing will often result in embarrassment and depression.

Trying it at home while building with a few different track layouts once you have it built will give you a huge leg up and a lot less fretting over the event. Preparation and testing are key. Expect to spend a few weeks writing and debugging the code if you haven't done this before and can use only spare time to work on it. i.e. Don't wait until the last minute to write the code! Building the mechanics of the robot is the easy part!

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Should i use a line array or Vee shape for LDR sensors??

#### Georacer

Joined Nov 25, 2009
5,182
Don't wait until the last minute to write the code! Building the mechanics of the robot is the easy part!
Funny you would say that. I 'd say the exact opposite.

Ah... that hardware/software discrimination...

#### thatoneguy

Joined Feb 19, 2009
6,359
Funny you would say that. I 'd say the exact opposite.

Ah... that hardware/software discrimination...
I was referring to the base/physical robot, he'll need that to test his code, and writing code for this one may be a tad difficult, once he has a moving platform to work with, additional hardware can be decided. Though I'd suggest comparators on the sensors instead of ADC reads by the controller, so I guess that's "hardware" in advance to save on software.

After looking over the map, it may be useful to have a "flip down" sensor that extends 3-4" so the thin gap with no line isn't accidentally crossed. This would simply be a reflective sensor, like the others, but on a servo so it is sitting straight up to fit in the 10" box at the start, and when you press "go", it flips down to give a "warning" about gaps if it comes to a "capacitor looking" trap.

Also, a turn 180 degrees in place so the route can be retraced without going off a line if you took a dead end, this is where memory of turn history is needed, so the bot doesn't make the same mistake twice.

If you are certain the layout will be like that, you can code it to turn right first, but it may be an example only. Try to find out if that will for sure be the layout, save for the wide moveable strips, which can be "defeated" with a forward sensor and the array at the front. They sometimes aren't exactly the same, it would make winning too easy, they may do something as simple as swapping start and end, which would mess up any pre-plotted courses.

--ETA: That's probably the track. The turns are in the same order and number from start to end or end to start:
R R L L R R L L So maybe they are looking for a bot that can detect/ignore the moveable fat fake intersections.

Last edited:

#### Georacer

Joined Nov 25, 2009
5,182
The thick lines are actually a good thing. You can use them to align your robot, as long as they are perpendicular to the main path.

With a sensor in each side of the line, you can advance the robot until the first "sees" black, and then pivot the robot over that sensor so that the other "sees" black too. Then both your sensors are set over the edge of the thick line and you can go straight to overcome it.

#### Markd77

Joined Sep 7, 2009
2,806
Another option for the dynamic blocks is to drive around one edge of them. A line follower should easily be able to follow the edge, but guaranteeing that it can drive in a straight line across the block could be harder (but maybe faster).

#### thatoneguy

Joined Feb 19, 2009
6,359
That sounds like a workable solution Mark.

Since this is his first contest, I'm focusing on how to keep him from getting Disqualified by going over a spot where there is no line. If he makes it through, the time is the time. If his software is complete and speedy, he can increase the wheel size (up to 10") for more speed, except then traction is an issue in order stop on a dime and turn around or reverse.

If he gets one going that takes every "wrong" path, but eventually hits both checkpoints and the end, I'd most assuredly call that a "Win" for him, since this is his first time. Winning would be nicer, though having a bot built that can at least do that at home is an excellent start. For the contest, just tweak the code for hints on which way to go at intersections and hope the layout is the same.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829

#### Markd77

Joined Sep 7, 2009
2,806
Maybe the 250 or even 150RPM version would be better, 375RPM is pretty fast, with a 2cm diameter wheel:
375/60 = 6.25 revolutions per second
With a 2cm diameter wheel the circumference is 2 X ∏ = 6.3 cm
Speed = 6.25 x 6.3 = 39 cm per second which is about 1.4km/h

Looking at it again you probably need a 4cm wheel for this motor so double everything.

Don't forget a charger for the battery.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
with a 2cm diameter wheel:
Hi again,
It is 6cm diameter and .5cm width...

Don't forget a charger for the battery.
I am using a home made charger of 12V and 500mA ...
which gives 11.30V DC and 800mA max.
is this OK..I am thinking to have LM317 To have 12.6V DC output.

Similar threads