This video lesson is a revised version of Episode #1. Matt Gipson requested it in a comment. Using the EV3-G software we have developed a very simple program for following a line with two LEGO MINDSTORMS color/light sensors.
- 11 Jan 2015
This program is usefull as a starting point. It is not in any way a complete and efficient solution. But if you understand how this EV3-G program works you would have far less trouble understanding more complex programs.
Why two light/color sensors
The use of two light/color sensors allows for a faster line following. The line stays between the sensors and if none of the sensors detects the line, the robot just moves forward.
More efficient solution
You could find a more efficient solution in How to follow a black line (EV3 version)
In this Mindstorms video lesson we use the constructions from:
The original program for NXT-G can be found in the materials section of video lesson 1:
Recently, we've received a comment from Matt Gipson on how we can translate an old NXT program for developing Episode 1 into the EV3 software and the question was especially for newbies. Episode 1 is all about lining and following a line and in this episode we showed a very simple approach to how we can follow a line with two sensors. In this episode I'd like to develop the same program translated from NXT into an EV3 version. And we will get something like this.
Let's start from the idea. This robot is an EV3 competition robot and you can find instructions for constructing it below the video. This robot has two color sensors but it can also have a color and a light sensor or two light sensors. This robot will follow the line. The basic idea of using two color sensors is to make the robot follow the line faster. And today I'm going to just rework a very simple solution that we've used in one of the videos but, again, I will provide you with links on how you can make this solution more efficient. And we've done this in one of the previous episodes. Now, let's start with the program. This is the EV3 software and I'm trying to transfer this program, developed on NXT, into an EV3 version. First, we should inspect the robot. I have two color sensors attached to this robot. One is attached to port 2 and the other - to port 3. I also have two motors. The first one that I'll use is port A and the second one is port C. I have a medium motor on port B but it won't be used in this specific lesson. Let's start with the program. We have two sensors and, basically, the program works like this. We have sensor 2 and we have sensor 3. We have one motor here. It's motor A. And one motor here. It's motor C. The line goes like this. It's a black line. The robot is moving forward. At a certain point the sensor on port 2 will detect the black line. When this happens, we should align the robot to the line. This means that we should turn motor C forward and motor A backwards. In this way, the whole robot will change its direction. The same principle applies to port 3 and the other color sensor. If the line goes like this, as the color sensor in port 3 will detect the line first, we should turn motor A forward and motor C backwards. Let's see how this happens on the program. First, we need a switch and with this switch we will check. What are we going to check? We'll check the color sensor, we'll compare colors. Are we over a black line? This is our comparison. If we are over a black line on port 2, then we should move motor A backwards and motor C forward. Which is the appropriate way to do this? For example, we can use the Move Tank Block and we select A and C. So, we are moving them at a number of degrees, rotating them, let's say, by 30 degrees. Motor A should go backwards and motor C should go forward. You'll see that in a moment when I run the program on the robot. This will be minus 50, so that we can have it backward, and the other one is forward. But if the color sensor attached to port 2 is not over a black line, we should do another check. We should do the check whether the other sensor is over a black line. How do we do this? Again, Compare, Color Sensor, and we compare with Black. Only black. I'll just zoom out the program, so that you can see it more clearly.
What should happen with the sensor attached to port 3 if we are over a black line? Think about this for a moment. If the sensor detects a black line, we should align the robot to the line and move forward. How do you do this? In the opposite way to the other case. Again we'll take a Tank Block and have motors A and C. We'll again turn them by 30 degrees.
And we should turn motor A forward and motor C backwards - minus 50.
Let's again draw our robot. We have sensor 2, sensor 3, motor A and motor C.
If the line goes like this, then we'll enter the following data - right here. The sensor on port 2 will detect a black line and then we should move motor A backwards and motor C forward and we will make the robot turn. If, on the other hand, the sensor on port 2 does not detect a black line, and the sensor on port 3 detects a black line, we should do the opposite. We should move C backwards and A forward. If both sensors are over white, then we should just move forward - endlessly. To do this, we will again use the Tank.
It's on, with power of, let's say, 30.
So, we have our switch ready. Our next step is to repeat this switch in loop.
OK. Here's our program. Let's run it and see what happens.
Here I'm starting the program.
The robot is following a line using two sensors.
It's a very simple algorithm and it's very useful to get your head round following lines. Especially for newbies. I hope that I've answered to the comment from Matt. In some of the next episodes we'll present other improvements on this program. And below the video you can find links to other video lessons where we develop more efficient algoritms on how you can follow a line using two sensors.