2010-08-20

Line Following Robot

Everyone with a Lego Mindstorms set must at some point build a line following robot. Here's mine.



The trick to following a line when all you have is one light sensor is that you don't follow the line, you follow the line's right (or left) edge. You try to stay exactly on the border between the dark area and the light area. The light sensor reports shades of gray when it passes from black to white. You choose a value that you think corresponds to the edge and then when the sensor reports a value brighter than the threshold value, you turn left, and when it reports a value darker than the threshold value, you turn right (assuming you chose to follow the right edge of a black line).



For smooth movement we don't just turn left when the value from the sensor is greater than the chosen threshold. Since the data we get is not binary, we can make the robot turn sharper when we think it's off the line entirely and only adjust its course slightly when we think it has left the edge of the line, but by just a little.



Here's the NXC program that the robot is running. It's heavily inspired by the NXT-G program of this robot. I initially used OnFwdSync(OUT_BC, ...) to turn the robot, but it sometimes resulted in very jerky movement. When trying to figure out whether it's a problem with my hardware or software I looked at that robot's program and saw that they control each motor independently. When I replaced calls to OnFwdSync(OUT_BC, ...) with OutFwd(OUT_B, ...) and OutFwd(OUT_C, ...) (with power values based on whether we think we should be turning right or left), the jerkiness problem was solved.

I also used their idea of calibrating the sensor at the beginning (the robot rotates a bit to the left and then to the right, noting the maximum and minimum light levels and chooses the threshold value as the average of the two).



You will note that I used a faster version of the program for the NXT Test Pad and a slower version for following the line that I made on the floor with electrical tape. I had to do this because the robot kept losing the line (probably because the tape was too narrow, it was also blue, not black, and some of the turns I made may have been too sharp).

15 comments:

  1. Thanks for documenting your design and sharing, quite helpful. :)

    ReplyDelete
  2. pretty cool i wish u got a tutorial on HOW to get the robot to follow the line like the details of it

    ReplyDelete
  3. Replies
    1. There's a link to the code right there. Here it is again:

      http://www.jfedor.org/lego/linefollower.nxc

      Delete
  4. Can you please upload the program in .rbt file because I want in the software

    ReplyDelete
  5. what kind of sensor u used?
    Plz give the name....

    thanks

    ReplyDelete
    Replies
    1. It is the color sensor that comes with the NXT 2.0 set. But I'm not using it for detecting colors, just light intensity.

      Delete
  6. what type of software did you used to complete the programming?

    ReplyDelete
    Replies
    1. I used the NXC programming language and Bricx Command Center: http://bricxcc.sourceforge.net/

      Delete
  7. do you have similar code in java nxj ?

    ReplyDelete
  8. Please can you give me the program im goin to compeate on a nacional competition and i can try your program

    ReplyDelete
  9. thanks for sharing, very useful for me :) could you provide, the bricks and pieces you used please

    ReplyDelete
  10. Hi jacek...I have seen your video when i was searching for smooth line following program using nxt-G programing.Our team members trying to do this program these days and we are not getting exact and smooth following.Could u plz send me the code or share the code in nxt-G programing with .rbt extension

    ReplyDelete
  11. Could you maybe give us the block code?

    ReplyDelete