Deprecated: Function eregi() is deprecated in /public/sites/ on line 51
NXT Line follower using a light or color sensor | My NXT projects
Home My NXT projects NXT Line follower using a light or color sensor
Bookmark and Share

Who is online?

We have 36 guests online
Where do visitors come from?
Locations of visitors to 

this page

Visitors in 2009:

Visitors in 2008:

page counter:

PDF Print E-mail
Written by   
Thursday, 18 February 2010 00:00

NXT Line follower using a color or light sensor and PID controller

Until now I had never tried it: building a Line Follower with LEGO NXT Mindstorms. But I got a question from someone whether it was possible to build a line follower and write a NXC program for the NXT 2.0 color sensor (while many Line Followers have been build with a NXT 1.0 light sensor) . That is why I decided that I should try it.
Many people have already builded a Line Follower. It all looks rather simple, but it was still quite tricky. The robot itself should maneuver easy, so that it can smoothly follow the line. The light or color sensor must not be too high nor too low above the surface. And the sensor must be far enough in front of the wheels (but not to far) so that it is able to discover a deviation in the line soon enough, so that the robot can adapt in time.
I haven't invent a new algerithm to control the robot, but I based my Line Follower program on the PID (proportional–integral–derivative) controller.

A PID controller is a generic control loop feedback mechanism (controller) that calculates an "error" value as the difference between a measured process variable and a desired "setpoint". The controller attempts to minimize the "error" by adjusting the process control inputs.
The Line Follower uses the (color or light) sensor to scan the ground. He reads the color or the reflection value of the floor. The value that the sensor shows when it's on the line is called the "setpoint" value. Ones the robot follows the line and the sensor goes on and off the line,  the sensor value (called "actual_position") changes. As a result of deviations from the line the PID calculates an "output" that is used to adjust the motors, allowing the Line Follower to go back to the line.

I have tested my NXC Line follower program with tree different sensors:

The LEGO Mindstorms NXT 2.0 solor sensor, the Hitechnic color sensor and the NXT 1.0 light sensor. Each sensor has it own characteristics and must be initialized and read out in a different way. Here the statements for NXC:
Sensor type Sensor values Initializing sensor read sensor
NXT 2.0 Color 1 (black) .... 6 (white) SetSensorColorFull(S2) SENSOR_2
Hitechnic Color 0 (black) .... 17 (white) SetSensorLowspeed(S2) ReadSensorHTColor(S2, setpoint,
r, g, b)
NXT 1.0 Light 0 (black) .... 100 (white) SetSensorLight(S2) Sensor(IN_2)

The PID controller contains basically 3 different controllers in one: proportional, integral and derivative controller. Each controller must be individually adjust by a gain to set: Kproportional, Kintegral and Kderivative.
Here is a simple software loop ( a non exsisting program language) that shows the PID algorithm:
previous_error = 0
integral = 0
  error = setpoint - actual_position
  integral = integral + (error*dt)
  derivative = (error - previous_error)/dt
  output = (Kproportional*error) + (Kintegral*integral) + (Kderivative*derivative)
  previous_error = error
  goto start
The value "dt" determines the responsiveness of the PID controller.

Proportional controller.
The proportional term makes a change in the "output" that is proportional to the current "error" value (when the sensor is not on the line). The proportional response can be adjusted by multiplying the "error" by a constant Kproportional, the proportional gain.

A high proportional gain results in a large change in "output" for a given change in the "error". If the proportional gain is too high, the Line Follower will be unstable. In contrast, a small gain results in a small "output" response to a large input "error", and a less responsive (or sensitive) controller. If the proportional gain is too low, the control action is too small for the Line Follower to go back on the line. In the absence of disturbances, pure proportional control will not settle at its target value, but will retain a steady state error that is a function of the proportional gain and the process gain. Despite the steady-state offset, both tuning theory and industrial practice indicate that it is the proportional term that should contribute the bulk of the "output" change.

Larger Kproportional values typically mean faster response since the larger the "error", the larger the proportional term compensation. An excessively large proportional gain will lead to process instability and oscillation.

Intergral or integrator controller.
The contribution from the integral term is proportional to both the magnitude of the "error" and the duration of the "error". Summing the instantaneous "error" over time (integrating the error) gives the accumulated offset that should have been corrected previously. The accumulated "error" is then multiplied by the integral gain and added to the controller output. The magnitude of the contribution of the integral term to the overall control action is determined by the integral gain, Kintegral.

The integral term (when added to the proportional term) accelerates the movement of the Line Follower to the "setpoint" (line) and eliminates the residual steady-state error that occurs with a proportional controller alone. However, since the integral term response to accumulated past "errors", it can cause the present value to overshoot the "setpoint" value (cross over the setpoint and then create a deviation in the other direction, so the line goes on Line Follower).

Larger Kintegral values imply steady state "errors" are eliminated more quickly. The trade-off is larger overshoot: any negative "error" integrated during transient response must be integrated away by positive "error" before reaching steady state.

Derivative, differentiator or differential controller.
The rate of change of the process "error" is calculated by determining the slope of the "error" over time (i.e., its first derivative with respect to time) and multiplying this rate of change by the derivative gain Kderivative.

The magnitude of the contribution of the derivative term (sometimes called rate) to the overall control action is termed the derivative gain, Kderivative.The derivative term slows the rate of change of the controller "output" and this effect is most noticeable close to the controller "setpoint" (the line). Hence, derivative control is used to reduce the magnitude of the overshoot produced by the integral component (to prevent the Follower Line shot way over the line) and improve the combined controller-process stability. However, differentiation of a signal amplifies noise and thus this term in the controller is highly sensitive to noise in the error term, and can cause a process to become unstable if the noise and the derivative gain are sufficiently large.

Larger Kderivative values decrease overshoot, but slow down transient response and may lead to instability due to signal noise amplification in the differentiation of the "error".

The NXC Line follower program
The program is really smart but simple.
line    statement                                                           comment
 1 setpoint = SENSOR                            // read the sensor value when the bot is on the line.
 2 while (true)                                 // never ending loop   
 3 {
 4  actual_position = SENSOR;                   // read the sensor value to check if the sensor on the line or not.
 5  error = setpoint - actual_position;
 6  proportional = Kproportional * error;
 7  integral = integral + error;
 8  derivative = (error - previous_error) / dt;
 9  output = proportional + Kintegral * dt * integral + Kderivative * derivative; // result of PID calculation
10  previous_error = error;
11  left = speed - output                       // adjust left wheel
12  if (left < 0 )                              // check direction (OnFwd statement cann't have negative input)
13     {
14       OnFwd(OUT_A,-left);
15     }
16   else
17     {
18       OnRev(OUT_A,left);
19     }
20   right = speed + output
                      // adjust right wheel
21   if (right < 0 )                              // check direction (OnFwd statement cann't have negative input)
22     {
23       OnFwd(OUT_B,-right);
25     }
26    else
27      {
28        OnRev(OUT_B,right);
29      }
30   Wait(dt);
31 }

You can download the NXC code (made with NXC 1.28 firmware) for my Line follower here:
NXT 2.0 Color sensor: line follower color sensor NXT2.nxc
NXT 1.0 Light sensor: line follower light sensor NXT1.nxc
NXT Hitechnic Color sensor: line follower color sensor Hitechnic.nxc

Tuning the Line Follower PID controller Kproportional, Kintegral and Kderivative gains.
The behavior of the Line Follower depends on the contruction. Therefore you always have to tune your Line Follower.
To set the gains (Kproportional, Kintegral and Kderivative) of the PID controller manually, you must initially set the Kintegral and Kderivative values to zero. Increase Kproportional until the Line Follower knows to find the line after it run of the line.

Then Kproportional should be set at about half that value. Then increase Kintegral until the Line Follower goes back on the line. The more Kintegral, the quicker the Line Follower will go to the line. However, too much Kintegral will lead to instability and the Line Follower will go over the line.

Finally Kderivative must be increased, if necessary, so the Line Follower will quickly go back to normal driving, after it has returned to the line. This compensates the Kproportional and Kintegral correction and the Line Follower less will go less quick over the line. However, too much Kderivative will cause excessive response and the Line Follower will shake.

I have made a video about my NXT Line Follower. You can see the Line Follower in action with the 3 different sensors. You can also see how to tuning the PID gains. I have made several test runs with different Kproportional, Kintegral en Kderivative values:

I have not jet finished a full building instruction, but here is the base of the Line Follower made with LDD (LEGO Digital Designer):

Click on the picture to download the .lxf file.

Check the complete NXT Mindstorms Line Follower high resolution photo gallery at Google Picasa...

Copyright © 2014 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
Powered by WebRing.