# Teaching Age Appropriate Line Follower Algorithms ## The challenge

Line followers is a must-do for a robotics device. It had become the benchmark for evaluating the efficiency of a platform. Unfortunately, there are many factors that will influence the behavior of a robot and the efficiency of an algorithm.

In addition, while designing age-appropriate experiences, how far can we go before we induce anxiety in our users because of complex programming structures, ultimately causing them to leave the product aside.

In short,

what is the right balance between performance and usability, when using line follower examples in a robotics product.

## The basic principle

There is no sensor in the world that is called a line follower sensor. A line is not something that is detectable as a thing. What is detectable is dark areas on light areas (or the inverse!)

When we apply this thought to a moving robot on two wheels, with a light sensor place on it pointing downwards, we get a fabulous learning tool. So, the basic principle for a line follower is:

###### “Hey, this is black, let’s turn to find where is white; hey, this is white, let’s turn to find where is black… and keep going!”

This is it!

This little but very important sentence is called Pseudocode, and it is crucial to be able to write it down before starting to code. Without this, we would be all writing in the blank.

Now we need to find a clever way to “find black” and “find white”. ## Color mode (light sensor)

• Some light sensors have a “black” and “white” color detection available. This is great to start with but we need to acknowledge something, this mode has most likely been “optimized” by a programmer for you. This means that:
1. We need to be confident that the programmer could optimize his algorithm.
2. The algorithm has been optimized for a specific condition (that might be different from your reality)

With this mode, the sensor can only return and of the 2 values:

• Black or White

## Reflected Light mode (light sensor)

Most light sensors will detect the quantity of light that is reflected to it, and will compare it the the amount of light it had emitted. More simplified: With this mode, the sensor can only return any of the 100 values:

• at 0% of reflected light, the sensor detects something dark (although 0% reflected light sounds like a blackhole!)
• at 100% of reflected light, the sensor detects something light  (although 100% reflected light sounds like a mirror!)
In short,

we can see already that following lines requires the comprehension of percentage and conditions. This should give us a clue of the age appropriate type of algorithm we will have to create.

## Making fast code (part1)

Coding with the goal of having optimized behaviours can be a real art. It requires not only to know what to code but also how to code it. Most of the graphical languages, block based for example, simplifies the logic for users, but at a cost of “loosing” some flexibility in the code.

Blocks are a group of functions designed by the team of engineers that worked on the block. We therefore rely on their style and efficient code. But more then that, a block is an “extra layer” that the machine has to understand. The more layers, the slowest, even if all the layers are optimized (i mean, we are talking milliseconds here, but in machine-world, that is what counts). ## The algorithms

There are different strategies for this nice problem:

1. a very linear algorithm, using binary logic
2. a conditional algorithm, using binary logic
3. a conditional algorithm, using a more complex binary logic
4. a conditional algorithm, using Proportional logic
5. a conditional algorithm, using PID logic

## Linear-Binary

Alright, this is where we start. This is the less effective line follower algorithm, but it is the easiest to understand.

If we start here, the robot should do only do two little moves: one motion toward black, one motion toward white. So if we loop this, the robot should go along the line. #### Tweak the parameters

Things you can look after if something doesn't go well:

1. Reduce the speed
2. Change the "amount of steer" of each turn.

## Conditional-Binary

Using a “Wait for” function as the advantage that it is simple to understand, but it is known to be to the fastest.

A simple step to optimize the code is to combine the 2 “Wait for” into 1 “if-else” structure. Even better, at this point, changing the “do you detect black” for “do you detect a dark color that has a reflected light index bellow 50%” will give better results. At this point, we should have a “regular” line follower robot, driving kind of fast along the line, not missing too many turns under normal lightning conditions.

In a way, we have created a binary switch. This means that there is a condition set up, with 2 possible actions: one action (action A) if the condition is true, one action (action B) if the action is false. ## Conditional-Binary +

From this point, we are not changing anything from the structure of the logic. But we will add more conditions to make it more accurate.

When the robot moves, it brings the sensor above the black area and the white area. So there is actually a transition moment where the sensor is half/half over white and black, giving a different reading the if it was fully on white or black.

Therefore, we can use the “moment” to do something else with the robot.  ## Proportional

Alright, this is where things get tricky and fun!

There was a significant improvement moving from the binary algorithm and the binary+ algorithm.

What if now we could introduce so many steps that we would make the robot react more proportionally to the change of reflected light. So this hole game is to deal with the variation of the reading. We want to keep the robot on target, which is the edge of line. In other words, the target should be a sensor reading of 50, and the “error” should indicate how far the sensor’s value is from a reading of 50

Then we make a correction. The robot will take an action that is proportional to the error. The more “away” for the edge of the line the sensor is, the more sharp the turn it has to make. To do this, we must adjust steering input on move block.

So the pseudocode (remember this 🙂 )

What is my distance from line, Create a correcting amount, depending on the Error Use the correction value to adjust the robot’s turn towards the line.  ## PID

What is PID?

• Proportional [Error] : How bad is the situation now?
• Integral à Have my past fixes helped fix things?
• Derivative à How is the situation changing?

PID control combine the error, integral and derivative values to decide how to steer the robot ## Best Use

Type of algorithm

Concepts

Use

Age appropriate*

Linear - Binary

Wait For

Exploring the concept of line detection

any age

Conditional - Binary

If-else, greater then/less then

Exploring the concept of line detection, while optimizing the robot behaviour

Conditional - Binary +

Multiple If-else

Optimizing further robot behaviour

Proportional

Error

Maximize robot performance