Nico and Robots

Line tracking for a moving robot

Why line tracking ?

Here we are going to learn how to use a camera to follow a line drawn on the floor. Let’s say that you want to conceive an autonomous robot but you’re not ready to use mapping technologies to guide it yet. Then line tracking would be a nice and simple way to test your robot. To track lines, you will need a camera and a numeric software such as Matlab or Labview.

See the line

  • Step 1 : Get a picture
  • You can either take a picture or directly establish streaming, it’s up to you.
    Get a picture of your line
  • Step 2 : Get access to the RGB data
  • RGB (Red Green Blue) are 3 indexes of a pixel’s colour, usually each is defined from 0 to 255. You will need to get the RGB code of every pixel of your screen and read the average RGB value of the line you want to track. For example, perfect red is [255 , 0 , 0], perfect green is [0 , 255 , 0] and a mix would look like [125 , 125 , 0].
    “Read” your line’s RGB code
  • Step 3 : Get the coordinates of the pixels
  • Now that you know the colour indexes you’re looking for, you can get the coordinates of all the pixels whose RGB values match with your line.
    Consider each pixel as its coordinates Unfortunately, it is very rare that a single RGB measure matches the entire line so you can introduce a threshold admission to be sure of it. For example if two pixels of your red line are respectively corresponding to RGB = [240 , 5 , 2] and RGB = [251 , 15 , 1], you may choose all the pixels whose RGBs are defined between [245±5 , 10±5 , 1].
    Select the coordinates
  • Step 4 : See the line
  • Compute the coefficients of the least square line associated to the pixel coordinates you’ve found.

    Track the line

  • Step 1 : Define what you are looking for
  • Usually, you need to follow the line straight, which means you want to keep the line centered and vertical. In order to regulate it you will need : The distance between the line and the center of the screen D. D=(|a*xcenter + b*ycenter +c|)/((a2+b2)^(1/2)) The orientation of the line α. α = atan(-a/b)
  • Step 2 : Implement automatic control
Depending on your robot/vehicle, you will need to implement different automatic controls to monitor your motors. For “classical” applications such as wheeled robots you can configure it at low speed by implementing a Full State Feedback if you can model your system and get the following State space equations. The LQ controller fits particularly well here.Measurement equation Where x is a state vector, y is the output vector and u is the control vector. In this case y represents our motors inputs, u includes D and α, and x may correspond to y and other variables you want to estimate in your model. However if you are not familiar with State Feedbacks, you can also implement a simple feedback loop with an adjusted proportional gain, it will already work just fine but you will still have to regulate the weighting between the distance factor D and the orientation factor α depending on your system performances against it. Here is an example of a line tracking establishment I achieved at AKEOPLUS: