The motion of a robot inside a field with obstacles

Line follower

To solve the problem of robot motion inside a field with obstacles, two different philosophies can be followed. The first is to trace a predefined path to avoid obstacles, in case you know beforehand the object to avoid, the second is to probe the surrounding space through appropriate sensors and to define the better path that allows the robot to avoid the obstacles. These two different philosophies lead to two different methodologies:

  • Line follower

  • Interpolated curves.

But why should a robot follow a line? Surely because in this way it could be made to move with a certain security within a space, without having to resort to other engineering concepts

Some examples from the real world of robots that work by exploiting this solution:

  • Restaurants

  • Industrial world

  • World health care

Preparing For This Tutorial:

Preparing For This Tutorial:

  • Instruments

  • Computers.

  • LEGO® Mindstorms EV3 Education Kit.

  • LEGO® Mindstorms EV3 Home Edition software and Python.

  • A white sheet on which a path is prepared using, for example, 2 and 5 cm black insulation tapes. Remember that the more tight the curves, the more complex it will be to program the robot and find suitable algorithms for tracking the line.

Time constraints: - 12 hrs



Meeting 1- Introduction to robotics, construction of the robot
Meeting 2- First programming tests (engine ignition, ultrasonic sensor use test to avoid obstacles)
Meeting 3 - Find a black line and stop (Read the values measured by the sensor on black and white)
Meeting 4 - ON-OFF algorithm
Meeting 5 - Proportional algorithm (P controller)
Meeting 6 - Final Challenge


Part 1

We have a white cloth, and on it a line of black scotch that forms a path. If the robot starts from a random point from the cloth, can we keep it going until it finds the black line?
The color sensor, in fact, positioned a few mm from the floor, detects the level of light reflected within a range from 0 to 100:
0 - No reflection -> sensor pointed on black
100 - Maximum reflection -> sensor focused on white
Example 1.

If we know that the robot is positioned on a white area, and must arrive on the black and stop the algorithm could be of this type:

I turn on the engines
I measure with the sensor and wait for a value <20

#(the initial values will be high -> the robot is on the white)

I turn off the motors
Why don’t you use a value closer to zero to detect black?

Part 2

Try to chase the line!


  1. If we start from the hypothesis that the robot is above the black line, what is the desired value, what the robot would like to measure continuously (with the sensor light) during its operation?

  2. If the robot is precisely located with the sensor above the line, and at some point the value of reflected light detected by the sensor increases, which way do I bend the robot to correct the position and return to black? Right or left?

The solution that we propose is to choose as desired value 50% of white and 50% of black (typical ideal situation with white = 100 and black = 0 among which we average obtaining the value 50), with the sensor positioned on the right end of the belt, This solution is called ON-OFF because the robot either turns to the right or left, always with the same curvature parameters.There are no other possibilities!


Repeat indefinitely
I acquire a value from the sensor
If the value read> 50 (average value) the robot is moving towards white (which is on its right)
The robot turns to the left
If the value read <50 (average value) the robot is moving towards the black (which is on its left)
The robot turns to the right
And therefore the average value can not be 50. Measure these two values and then calculate the real average value between the two, before constructing the algorithm. In the scheme as average value is found 38.5 because in real conditions (in which there are variables to take into account as ambient light, the cloth of a not perfect white color), we will never have as values:
- Light reflected on black = 0
- Light reflected on white = 100

Part 3

Improve the control algorithm! Can the robot avoid (or decrease) oscillation? What is due, and why does the robot behave in this way?


If the brightness of the right sensor is reduced, reduce the speed of the right motor proportionally, if the brightness of the left sensor is increased, increase proportionally the speed of the left motor, if the brightness is equal, proceed with the same speed of the two motors. Another way to control the movement of the robot is to introduce a control on the system that assesses the error compared to the optimum trajectory. The error indicated with E is nothing more than the difference between the value acquired by the sensor and the desired value (average between white and black). The correction of the robot (i.e. how much steer should be proportional to this error where Kp represents the coefficient of proportionality and is to be found empirically. To find the best value it is worth to initially set 1, and then proceed to modify about 0.2 the constant, until you get to the value that convinces them the most.

Part 4

Move an object within a security area or warehouse (as if you were to program an industrial robot) in the shortest time possible.

Short help on programming

Commands/functions needed for the exercise

Commands necessary to move

#!/usr/bin/env python3

from ev3dev2.motor import MoveTank, OUTPUT_B, OUTPUT_C



ms= MoveSteering(OUTPUT_B, OUTPUT_C)

ms.on(steering=0, speed=50)

MoveSteering functions control two motors based on a steering value and a speed value The steering value controls the path of the robot and can be any value between -100 and +100.

  • -100 means turn left on the spot

  • -50 means turn left with the left wheel not turning

  • 0 means go straight

  • +50 means turn right with the right wheel not turning

  • +100 means turn right on the spot

The speed value refers to the speed of the faster wheel. The speed of the slower wheel is automatically calculated based on the steering and speed values.

Import necessary library and create a new instance of the class and assigns this object to the local variable color from import ColorSensor

color = ColorSensor()

Read the value from color sensor


Reflected light intensity as an integer percentage 0-100. Light on the sensor is red.


Returns an integer in the range 0-100 that represents the ambient light intensity. Light on the sensor is dimly lit blue.


Color detected by the sensor, represented by an integer:

0: No color 1: Black 2: Blue 3: Green 4: Yellow 5: Red 6: White 7: Brown

Next Section - Stirling motors