banner I have been a Lego League coach since 2007. This year, I wanted to document the season to give rookie coaches a resource to help them through to competition. The process can be intense, but it can also be a lot of fun for you and your team.

I hope to cover enough through my posts, but if I leave anything out, please feel free to leave a comment, or contact me.
Nov
4th

Following a Line – The Program

Author: fllCoach | Files under Programming

Hopefully you’re team has taken my last post and tried to program a line following program themselves. It’s not that difficult if you take some time to think about it.

I’ll outline the program here and you can see if your team’s program is the same, or even better.

Remember that when following a line, you’re actually following the edge of the line. We’ll say we want to follow the right edge. When the robot sees white, it should turn left and when it sees black, it should turn right. You should have realized that this needs a switch statement.

The condition of the switch statement is the mid-way point of what your robot determines is white and black. If you have calibrated your light sensor, this should be pretty close to 100 and 0, respectively. So for the sake of this discussion, we’ll use 50.

Now for the top branch, or white branch, we want to turn left. If you’ve designed your robot using my earlier tip, your left motor will be attached to port B and your right motor will be attached to port C. To turn left, you want to make your right wheel go, while not moving your left motor.

For the bottom branch, or black branch, you want to do the opposite.

Remember that if you do not explicitly turn off a motor, it will keep going. So if the light sensor sees white when it first starts, turns on the right motor to turn left, and then sees black and turns the left motor on to turn right, both motors will be on and it will go straight. So you need to make sure you include a stop motor block to stop the motor you don’t want running.

Now that you have the wheels programmed to do the right things depending on what the light sensor detects, you have to do it more than once. That is accomplished with a loop. Add a loop to the beginning of the program and pull the entire switch statement into it. The loop will expand to accommodate the switch.

By default, the loop goes forever. You can now test your program to see if it follows a line like you expect it to.

To be useful, the loop must be terminated in some way, usually using some kind of sensor. Change the loop’s end condition to a rotation sensor and set the loop to continue until one of the robot’s wheels has traveled 1000 degrees. It should now stop on its own.

Note that any line follower program will only work with the light sensor(s) in front of the drive wheels. If it’s in line with the drive wheels, the light sensor can’t lead the robot enough to direct it.

As I mentioned in my previous post (and as Dean Hystad has aptly pointed out), there are many more ways to follow a line that are much smoother and more robust. But I hope this at least gets your team started. You can just use this line follower as is to complete some missions, or spend some time tweaking it to work better. My teams have used this algorithm before and it’s worked well enough. But our mission accuracy certainly could have been much better with a better algorithm.


16 responses. Wanna say something?

  1. Philip Martin
    Nov 28, 2010 at 12:41:38
    #1

    Are you able to develop a double light sensor line follower? One light sensor oneach side of the line. I’ve tried it but it’s not working. I’m trying a switch within a switch. Any thoughts?

  2. fllCoach
    Nov 28, 2010 at 13:51:25
    #2

    My team used a two light sensor follow this year. You’re on the right track with the switch within a switch. You have four possible conditions with two light sensors. Consider each condition and what you want the robot to do in each condition.

    If you are still having trouble, send the program (.rbt file) to ‘thecoach at legoleaguecoaching.org’ and I’ll take a look at it.

  3. Dean Hystad
    Nov 30, 2010 at 17:10:33
    #3

    The shortest two sensor light follower uses math instead of logic.

    BEGIN Loop
    Motor steering = (Left Light – Right Light) x Gain
    END Loop

    This assumes a robot that drives forward when the motor direction is set to Up Arrow.

    When the Left light sensor reads higher than the Right light sensor the robot turns to the Right. The bigger the difference between the two sensors the sharper the robot turns.

    An NXT-G program for this algorithm is 5 blocks inside a loop. Two sensor blocks, a subtraction, a multiplication, and a Move block. The idea is so simple that it was implemented with analog electronics long before robots where a possibility.

  4. Tim David
    Dec 10, 2010 at 09:02:34
    #4

    Our team uses the two sensor line follower algorithm Dean describes. You can see a picture of it here:

    http://hightechkidsblog.blogspot.com/search/label/NXT-G

    This year, the team adapted the basic error correction technique for wall following using the ultrasonic sensor.

  5. gennie
    Jan 5, 2011 at 15:40:23
    #5

    hello, i am on a team for ffl robotics and we are using a light sensor but the problem we are having is getting the light sensor to stop after following the line. we did the program above but once it gets to the end of the line, it doesnt find the black sensor so it starts turning in cirles. so could you help us out on how to stop the light sensor??

  6. fllCoach
    Jan 5, 2011 at 16:15:45
    #6

    Gennie,

    I assume you are using the loop above with the rotation sensor to stop the robot? If your robot is not stopping, check the settings for the termination of the loop. Start with a distance that is really small, like 100 degrees, to see if the robot stops almost right away. If that works, then increase the condition to where you want it. If that doesn’t work, make sure the termination condition is what you expect it to be.

    If that doesn’t work, let me know and I will contact you with my email address so that you can send me your program for me to look at. I’m sure it’s just something simple.

  7. Dean Hystad
    Jan 6, 2011 at 11:48:43
    #7

    In the example you never turn off the motors. The loop should be followed by a move block to stop the motors.

    The example is also missing a rotation sensor reset prior to the loop. If you include this code as part of a mission program you’ll need to reset the “B” rotation sensor before you start line following. Failure to do this can result in the loop never ending because the B rotation sensor is already greater than the trigger value.

    Another potential gotcha is the loop termination condition. Depending on how you build your robot you may have to use the down arrow direction setting in the move block. This will require different settings in the loop termination block.

    I don’t understand why you use two move blocks for steering. The move block has a steering setting. I would modify the program to have one move block that steers to the right if the light is too bright and left if the light is too dark. You can then use the steering setting to fine tune the tracking performance. If the robot swings violently left and right adjust the steering closer to center. If the robot loses the line on curves adjust the steering away from center.

  8. fllCoach
    Jan 8, 2011 at 23:37:01
    #8

    Dean’s right. I forgot to add a motor stop to the example above. Even without the motor stop block, the motors should still stop when the loop is terminated, it just might go further than you expect and possibly do a circular turn at the end depending on what the light sensor sees last.

    I didn’t add a rotation sensor reset at the beginning because I was assuming the program would stand on its own. Therefore, the rotation sensor would already start at 0. I was also assuming the motors were attached “correctly” so that Up was Forward.

    I didn’t use the steering setting because this is a basic program that is easier to understand with multiple motor blocks. After this program is understood, the program can evolve to use more advanced methods.

    Thanx, Dean, for pointing out the missing motor stop block. I’ll add that to the example when I have some time.

  9. Dean Hystad
    Jan 17, 2011 at 15:28:02
    #9

    When posting code it is important to document any assumptions made about initial and ending conditions. This year there were several posts on the FLL forum about software that worked fine stand alone, but failed when included as part of a larger program.

    To many it is not obvious that you need to stop both motors when you are done line following (or the robot will spin around with one wheel stopped). It is even less obvious that you should reset rotation sensors prior to using them to control how long a loop executes. Including these two blocks, with a brief comment on their purpose, greatly enhances the value of the example.

  10. john
    Jan 3, 2012 at 00:31:30
    #10

    dear fllcoach can you teach me how to program 2 light sensor for better line fallow

  11. fllCoach
    Jan 7, 2012 at 07:39:36
    #11

    This is on my list of things to write about. I hope to do get it out before next season.

  12. Erin Howarth
    Nov 12, 2012 at 16:18:51
    #12

    Alright so this was working for me, and now it is not, so I think I need to reset the sensor before the loop starts. How do I do that?

  13. Erin Howarth
    Nov 12, 2012 at 16:21:44
    #13

    Oh, and how do I set the termination condition? Which block is that in? and what should i expect it to be?

  14. Dean Hystad
    Nov 13, 2012 at 14:24:12
    #14

    Line following runs as long as the loop continues to run. In the bottom example the loop termination condition is a rotation sensor block. The loop runs until the rotation sensor is greater than some value.

    When you run a program all the rotation sensors and timers start at zero. As the program runs the timers count up. As the robot moves, the rotation sensor values change. If you want to use a rotation sensor to terminate line following you may need to reset the sensor before the loop. The example program works because line following is the first thing that happens in the program and the rotation sensor is zero. If you modify this program and preceed the line following with some Move or Motor blocks, you need to reset the sensor to zero yourself.

    In addition to a reset rotation sensor block before the line following, you may need to add a Move Stop block after the loop to stop the motors.

  15. fllCoach
    Nov 15, 2012 at 07:36:24
    #15

    Thanx, Dean, for answering this question in my absence.

1 Trackback(s)

  1. Aug 31, 2012: How to Use this Site | Lego League Coaching

Post a Comment