#Robotics Programming a Line Following Robot – Part 2

David Meego - Click for blog homepageHere is my second article in a series on robot design and programming techniques. In this article I will be discussing how to make a robot follow a line with two sensors. I will be discussing some of the different methods and explain the pros and cons of each.

Disclaimer: This article will use Robocup Junior Australia‘s Rescue (Line) Challenge with a Lego Mindstorms based robot as an example.

If you have not read the previous article, please do so before continuing:

In the previous article we discussed how you can create a line following program with a single sensor, but we found that there are limitations with what a single sensor is capable of. Adding a second sensor so we have one sensor either side of the line solves the issues we saw before.

Matched Sensor Pairs

Every sensor is different and will give different readings for the same conditions. While this is not always possible, if you have more than two sensors, test the light readings for each sensor and try and find a pair of sensors that give similar values for black and white. Having a matched pair of sensors will make it much simpler to create programs without having to compensate for differences in sensor readings.

Two Sensor Line Follower – Simple

This is the simplest of the two sensor line followers and it is also one of the fastest while still staying reliable. The algorithm for this is very straight forward:

  • If both sensors see white, go forward
  • If left sensor sees black and right sensor sees white, turn left
  • If right sensor sees black and left sensor sees white, turn right
  • If both sensors see black, stop… for now

Below are the examples for this type of robot if built from Lego Mindstorms with the motors plugged into ports B & C and the sensors plugged into ports 1 & 3.

NXT-G

EV3-G

EV3 Classroom

EV3 Basic

'  ** Simple Bot 1 **

'  By David Musgrave

'  http://WinthropDC.com

'  Last Modified: 13-Jul-2017

' ** Setup Starts Here *******************************************************************************************************

' Initialise Variables
Clicks = ""
Finished = "False"

' ** Main Program Starts Here *******************************************************************************************************

Sensor.SetMode(1, 0) '  Color to Reflected Light
Sensor.SetMode(3, 0) '  Color to Reflected Light

' Program Main Loop
Finished = "False"
While Finished = "False"
  If Sensor.ReadPercent(1) >= 30 Then
    If Sensor.ReadPercent(3) >= 30 Then
      Motor.StartSync("BC", 50, 50)
    Else
      Motor.StartSync("BC", -25, 50)
    EndIf
  Else
    If Sensor.ReadPercent(3) >= 30 Then
      Motor.StartSync("BC", 50, -25)
    Else
      Motor.Stop("BC", "True")
    EndIf
  EndIf

  ' Check for Button
  Clicks = Buttons.GetClicks()
  If Text.IsSubText(Clicks, "E") Then
    Finished = "True"
  EndIf
EndWhile ' Finished

' End Program
Motor.Stop("BC", "True")

Spike Prime App

Troubleshooting:

  • If the robot ignores the line and goes straight over it, then your light readings and sensor settings are incorrect. Retake your readings and calculate the trigger point and update the value used by the sensors.
  • If the robot goes backwards, change the motors to the opposite direction. On NXT, change the direction. On EV3 and EV3 Basic, change the sign on the motor speed values.
  • If the robot turns away from the line, swap the motor speeds used for the turn left and turn right to make it turn the other way.
  • If the robot loses the line on 90 degrees corners, slow all the motor speeds down on until it works reliably.
  • If the robot cannot make a hairpin turn sharp enough, increase the speed of the “reversed” motor to make it turn sharper.

This method can work very well, it is reasonably fast, it can handle a gap or break in the line as it will just continue forward when neither sensor see a line and with two sensors it can be used to read the green shortcut markers. All the problems with a single sensor robot have been resolved.

There are some other methods which can be used to get smoother line following, these included stepped, steering and proportional.

Two Sensor Line Follower – Stepped

The Simple Method described above has three steps; left, forward, right.

You could adjust the program to have five steps by breaking the turn left and turn right into two levels of turn. A couple of approaches you can use are shown below:

  1. Use two trigger values, instead of just checking for a value less than 30, you could look for a value less than 20 for a sharp turn, a value between 20 and 40 for a gentle turn and a value higher than 40 to go straight. Note: that you can experiment with the trigger values to find the combination that works the best.
  2. Use a differential value by subtracting the value read by one sensor from the value read by the other sensor other sensor. Based on the example readings with a range of 10 to 50 for each sensor; If one is seeing black and the other is seeing white, the differential can be -40 or 40. So the differential can be a with a value from -40 through 0 to 40. You can now break this range into five steps for turn sharp, turn, straight, turn and turn sharp.

Using a stepped approach will allow for smaller corrections when adjusting for the robot just needs to straighten up.

Two Sensor Line Follower – Steering

The Steering method is like the Stepped method but with many more steps. The idea is that use the NXT Move block or EV3 Move Steering blocks and feed it the differential value calculated above. If you need it to turn sharper, you can multiply the differential by a turn factor to increase the turning values.

Based on the example readings with the range of 10 to 50, the differential can range from -40 to 40. If we multiply this by a turn factor of 2.5, we have a range of -100 to 100. This final value can be used as the input to the block.

Two Sensor Line Follower – Proportional

The Proportional method also uses the differential value with a turn factor and then uses formulas to calculate the motor power to send to each motor. An example starting point for your formulas could be the following algorithm (based on the differential with turn factor range of -100 to 100).

  • If differential is less than zero, Power for B = 50, Power for C = 50 + differential
  • If differential is greater than zero, Power for C = 50, Power for B = 50 – differential

The factors and formulas can be adjusted to change the power values. For example, multiplying the differential by a value of 1.X can make the maximum turning circle sharper.

Conclusions

Please feel free to create programs for other methods mentioned and test them yourself.

The following conclusions can be made from what we know so far:

  • Two sensor robots solve the problems we have a single sensor robots.
  • You can use a variety of methods for line following.
  • Using a differential value calculated by subtracting one sensor value from the other sensor value makes the line following independent of light readings.
  • When both sensors see black you have reached an intersection. You can make changes to the program to handle the green shortcut markers. I suggest moving back a little and switching to colour mode to see if there is a green square and what side it is on.
  • If the robot still loses the line, decrease the overall speed as there will be limits caused by the speed of processing, reaction time for the motors and physical inertia that will limit the maximum reliable speed of the robot.
  • Reliability is more important than speed. In a competition, losing the line will cause penalties and force a restart which will also use up precious time.

More Information

For more information on robotics and the EV3 Basic extensions to Microsoft Small Basic, check out the following links:

In the next article, we will discuss positioning of your sensors on your robot.

David

PS: I know I have not covered PID (Proportional, Integral, Derivative) algorithms. I have found that they are complex to program and still cannot follow a line as fast as some of the simpler methods.

06-Feb-2023: Updated with EV3 Classroom and Spike Prime App screenshots.

This article was originally posted on http://www.winthropdc.com/blog.