LEGO MINDSTORMS Smooth Line Follower

Due to public request on my youtube channel here is the code for my smooth line follower.

Download Mindstorms Project File

Smooth Line Follower Code

Smooth Line Follower Code

If you try the code with your robot the result might not be as good as in the video.

This can be caused by:
– Different position of the light sensor in relation to the wheels
– Different distance from wheel to wheel

The basic idea behind the code: My formula to calculate the speed is a/100*80-25=”speed value” with “a” as light value. One wheel is set to this speed value. The other is set to 30 – “speed value”. Huh? Let´s see what happens when the robot is driving on the line.

Case A – light sensor reads 100 (on white area)
100/100*80-25=55 Now one wheel gets the 55 as speed value. The other gets 30 – 55 = -25 As result the robot turns!

Case B – light sensor reads 0 (on dark area)
0/100*80-25=-25 Now the wheel that previously was turning forward turns backwards since it´s speed is set to: -25 The other wheel turn forward because it is set to 30 – (-25) = 55 As result the robot turns in the other direction!

Case C – light sensor reads 50 (somewhere on the edge of the line between white and black)
50/100*80-25=15 One wheel gets the value 15 and the other gets the value 30 – 15 = 15. As result the robot drives straight ahead!

If the robot is on the edge (or a little off) of the line both wheels will be set to positive speed values.

LineFollowerTableDriving through narrow corners is possible because one wheel (the inner one) is set to a negative speed if the light sensor detects that the robot is either in the white or black area. This enables the robot to nearly turn on the spot.

 

22 thoughts on “LEGO MINDSTORMS Smooth Line Follower

    1. wordpress Post author

      Hi Aten!
      It was a bit of trial and error. I even tried formulas with x³ because I thought this would make the movement smoother but in the end for me a simple linear formula had the best results.
      It´s important that the two formulas have the same value at a sensor reading of 50. This value is your maximum speed for going straight ahead and both graphs need to intersect (Remember: have the same value)

      One has a positve slope – the other has the same slope but negative.

      Calculate formula 1 for a given slope (the value for the steepness and maximum speed you want and that defines how sensitive the robot regarding to light-sensor readings – this is the trial and error part):
      slope * “sensor reading” + y-intercept = “maximum speed”

      I tried a slope of 0,8 and maximum speed of 15 when the sensor reads 50.

      0,8 * 50 + y-intercept = 15
      40 + y-intercept = 15 | -40
      40-40 + y-intercept = 15-40
      y-intercept = -25

      So one formula is speed = 0,8 * “sensor reading” – 25

      -0,8 * 50 + y-intercept = 15
      -40 + y-intercept = 15 | +40
      -40+40 + y-intercept = 15+40
      y-intercept = 55

      So the second formula is speed = -0,8 * “sensor reading” + 55 (the 30-“result of formula 1” I used is just a “short cut” for the same calculation)

      If you want to try out alternative formulas (or different formulas for sub-ranges) maybe it´s a good idea to have a table in MS Excel / LibreOffice Calc and visualize the output for all sensor readings.
      That way it´s easier to understand how the robot will react once it´s off the line and if you stay within valid values without having to do more calculations. I wanted to have a linear formula where the outer wheel moves faster forward than the inner wheel in order to have a tight turn.
      Another observation I made is that you need to adjust your formula to the layout of your track. If you do not have 90° turns or interrupted tracks you can go much faster.

      More info on linear functions:
      https://www.mathplanet.com/education/pre-algebra/graphing-and-functions/the-slope-of-a-linear-function
      https://www.mathplanet.com/education/pre-algebra/graphing-and-functions/linear-equations-in-the-coordinate-plane

      Reply
    1. wordpress Post author

      I assume that if the gap is large enough the robot will start to turn until he find a line again…

      Reply
  1. E.St

    I just added the light sensor to the basic educator bot, made a runway with black tape and A4 papers and it ran PERFECTLY. It even followed exactly the line AFTER the end, when the bot moved to the edge of the paper on a green floor, and it detected the edge, got back at the starting black line and continued. Thanks a lot!!!

    Reply
  2. alaa touni

    what is this function (abs) what (s)?
    i download the program but robot turn it self ?
    can you help ?

    Reply
  3. sharecool

    Mr.Bjoern I copied your programming but it could not work cause i had build a tankbot or is it something else.Please help me

    Reply
  4. Mihir R

    My robot is designed to move backwards i.e motor are placed in the negative direction. I am not getting how to tune the program on how to make the robot move backward. Can you tell me how urgently ?

    Regards,
    Mihir

    Reply
    1. Bjoern Post author

      Hi Mihir, I fear I need some more information to help you… Can you upload a picture of your robot and/or your code somewhere?

      Reply
  5. atie

    when i use a light sensor i dont know why but i have a lot of output that are not true for example the whit output is 60 and the gray is 32 but i dont know why while robot is moving the sensor see white same gray

    Reply
  6. MinWoo Cho

    Could you please explain the first part (Helligkeit) please? I do not understand why you take divide by 80 and multiply by 100! Why is this necessary?

    Reply
  7. Maria

    how could make the robot were reversed ?
    you see, it is that I put egranajes the robot and because of this going backwards in all that makes for you .
    I could do to make this not happen in this program?

    PS: sorry if misspelled somewhere but I’m Spanish and, I don’t know English , so I have to use the translator

    Reply
    1. Bjoern Post author

      Hi Maria, because of hte translator I am not sure if I understand what you are trying to say… Can you re-ost your question in spanish?

      Reply
  8. Ephraim

    Hello Bjoern,

    We have tried using your code and see the potential, but we have trouble figuring out what each of the numbers mean. It would be helpful if you could give us a copy of the code that was annotated. Thanks!

    Reply
  9. Master Q

    How do i adjust the code so that it just detects a green line only and ignores any black line

    Reply
    1. Bjoern Post author

      Hhmmm… Good question. In fact the sensor is in light reflection mode and does not care for the color of the line. The programm follows along the edge of a line and not the line itself. You could add a block for color detection but that will not help in case the robot is in the white part of the track where it has no chance to detect the color of the line…

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *