LEGO Mindstorms Class: Lesson 5
Line Following with the Light Sensor:
In the last class we used the Light Sensor to detect when our robot encountered a dark line. In this class, we're going to use the Light Sensor to help our robots follow along a dark line. Specifically, we will follow the edge of the line.
Let's say we want our robot to follow a dark line on white paper. Remember, if we calibrated it, the light sensor will output a value from 0 to 100 depending on how much of the sensor is over the dark line. 0 means the sensor is totally over the line. 100 means the sensor is only over the white paper. Imagine what the sensor would see if it moved from left to right over a dark line (see picture below):
Before we work with the line sensor, we'll need to calibrate it for the lighting conditions in the class today. Last week you loaded a program onto your robot called "Calibrate.rbt". Run it by holding the light sensor completely over the black line, and pressing the orange button on the brick, then holding the robot over the white paper and pressing the orange button again. You can test the calibration by viewing the output of the light sensor in a Light Sensor Block in the NXT-G window. Ask your instructors for help if you don't remember how to do this.
Exercise 1: Write a line following program
We're going to tell our robot to follow the left edge of the line. In the picture above, we see that the sensor reading is high (> 50) if the sensor is mostly over the paper, and the sensor reading is low (< 50) if the sensor is mostly over the line. We can use this fact to keep the sensor lined up with the left edge of the line while the robot moves forward. Let's write some pseudocode that does this:
Now let's turn this pseudocode into an actual NXT-G program. Create a program called linefollow.rbt, and save it to your folder.
That's the whole program! Your instructor has made a course out of paper and electrical tape. Try placing the robot near the left edge of the line and seeing if it can follow the course.
Challenge 1: The need for speed!
Your instructors have set up a course mapped out in electrical tape. Time how long it takes your robot to complete the course. Can you think of ways to make it go faster without losing its way? Hint: Try completing Challenge 2 below for one method to make your robot a speedier line follower.
Challenge 2: Write a 3-state line following program
The line following program we just wrote works best following a line where the curves aren't too sharp. If you try to get the robot to follow a sharply angled turn, it may lose the line entirely. The robot also moves pretty slowly, since it has to spend so much time turning left and right to find the edge of the line. Let's try a method to make the robot move faster and more accurately.
Instead of telling the robot what to do based on whether it is left or right of the line (two different states), let's make it more sensitive to its position. We will let the robot travel straight if it is close to the edge of the line, and only turn left or right when it moves away from the edge it is trying to follow. Since it reacts differently to three different conditions (left of edge, right of edge, close to edge) this is called a 3-State Program. Here is the pseudocode we want to follow:
The code may look tricky because it has an "if" statement inside another "if" statement, but if you think carefully, you will see how to write the program. You will need to use more than one switch statement to get it to work. Ask your instructors if you need help.
An example of a 3-state line following program.