In this tutorial we introduce the basic algorithm for following a line with one sensor and review the algorithm for quickly followwing a black line with one sensor, implementing both with the Mindstorms EV3 robotics kit.
- 10 Dec 2013
We have several inquiries about following a black line with the EV3 Kit. In this episode we suggest two ways, both using only one EV3 color sensor. The first is the most basic way for following a black line - oscillating between the black line and the whitespace, advancing with each oscillation further forward. With this the robot actually follows the edge of the black line.
The second way is the algorithm from Episode 31, where we have introduced a faster way for following a black line. In this case both motors are moving forward, so the robot advances faster forward. Again the aim is to follow the edge of the black line, but this time, if we get far from it, we just increase the power of the motor that is further away from the edge.
Note: In this tutorial we have used the EV3 Color Sensor, but both programs can be performed using the NXT Color Sensor or the NXT Light Sensor. More on the topic could be found in Episode 48.
Download the whole Mindstorms EV3 project created in the tutorial with the myBlocks included:
The First Lego League Competition is all about positioning and having a good orientation on the field. A common way to do this is by following the black lines. We have devoted previous tutorials on how to follow a black line using the NXT kit. In this tutorial I will focus on the same thing but this time I'll be using the EV3 kit.
First of all, we should start by putting the EV3 color sensor on the robot. In our case I have chosen to put it right here and now let's see what the consequences are. OK. We start the program and the robot will do something like this and then it will go away. However, the problem is not with the program but with the robot. As you can see, the red dot, this is the EV3 color sensor, is positioned exactly between the two wheels. When the robot turns, the sensor is almost static. That's because the sensor is on the same line as the point of the turn. If we want to detect the black line, we should make a huge turn which is very difficult to achieve. But if we position the sensor on the place of the gyro sensor, the robot will detect the black line only with a slight turn. Now I will change the position of the two sensors and see the difference. OK. Several minutes later the position of the two sensors is switched and the EV3 color sensor is in front of the robot. I will run the program and as you can see it's working just fine. In conclusion, the EV3 color sensor should not be on the same line as the two motors but it should be in front of the robot which means the sensor should follow the direction in which the robot is moving. So, if the robot is moving backwards, it'd be better if the sensor is at the back of the robot. Our strategy of following the black line will be to follow the line between the black line and the rest of the field. So, first we'll move the right motor until the sensor detects the black line and the left motor until the sensor detects a white space. Then the right motor again, etc. Now, let's take a look at the program. First, we have a loop that repeats itself forever. We have a motor block in it which turns on motor B. Motor B is on until the EV3 color sensor detects a black line or in our case detects a value less than 15. Then motor B stops and we turn on motor C which is on until the EV3 color sensor is no longer on a black line or the value is greater than 15 and finally motor C stops. 15 is a good threshold that works well in our case and will work well in most of the cases. Still, in some cases it may not be the correct value. So, we should go to Port View where we can see the value of the EV3 color sensor. As you can see, when the sensor is on white space, the value is 40, 42, 43. When we move the sensor on the black line, it's 4. So, 15 is a good threshold value. Let's run the program.
I have packed the program I have just created into a My Block. Here you can set the threshold value which is an input parameter for the My Block. I enter 15 as in our case. And now you are ready to download the program. You can find this My Block under the materials tab below the video. After we have downloaded the program from the website, we should go to the tools menu, Block import, go to the path where you have downloaded the My Block and then click Import. After you have imported the My Block, it will be available for you under the My Blocks tab in your project. Next, I'd like to implement another faster algorithm. For this purpose, I'll delete this block and make a simple program which will show the current value of the EV3 color sensor on the display. So, I put on loop; I take an EV3 color sensor inside to measure the reflected light intensity. Then I need a Display Block which will be set to text. Pixels; it will be wired; I connect the two blocks and I download the program. When the EV3 color sensor is exactly on the black line, it's value is 4. When I start moving it slowly towards the white space, the value is getting greater and greater until we reach a value of 39 to 40. Again, our aim will be to follow the line between the black line and the white space where the value is about 24. When we are more on the black line, we'll make the right motor go faster and the robot will make a slight turn. If we are exactly on the black line, the right motor will go even faster and it will make a greater turn. Respectively, when we are on white space, the left motor will go faster and will get back to the wanted value of 24. When we are around the value of 24-25, the two motors will simultaneously go at the same speed, so that the robot will go in straight line. Let's now see how this algorithm is implemented in the program. As I said, we first take the value of the EV3 color sensor. Then make this calculation using it and then we'll have several cases depending on the results from the calculation. What is the calculation? Well, as we saw, the range of the values of the EV3 color sensor is from 4 to 40. That's why we subtract 4 from the value of the color sensor so that the minimum value will be 0 and the maximum value will be 36. Then we divide the result by 6, so that we have seven different ranges. These are the cases from 0 to 6. For instance, in the first case, we have the values from 4 to 10. In the third case, which is our perfect case, we have the values from 22 to 28 where the value of 24-25 is, which is the value we want. As you can see, in this case we move with both motors at power 30 so that the robot is moving forward. If we are on black, we'll make a turn only with motor C. It is exactly the same when we are on white. Here we make a turn only with motor B. In the middle cases we move with slightly more power of the appropriate motor. You can download this program as a My Block under the materials tab below this video. Let's download the program and try it. Let's see what the difference between the two programs is. I will run the first one which looks like this and is rather slow.
Now I will run the second one.
It is a way faster. You can download both programs from the materials tab under this tutorial. If you have any thoughts about the video, please, leave a comment under it.