Introduction to LEGO Mindstorms EV3

Line Follower

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

Make your robot follow a line with the color sensor.

Keywords

  • color
  • sensor
  • line
  • reflected
  • light
  • intensity

About this video

Author(s)
Kevin Briggs
First online
03 July 2019
DOI
https://doi.org/10.1007/978-1-4842-5179-9_13
Online ISBN
978-1-4842-5179-9
Publisher
Apress
Copyright information
© Kevin Briggs 2019

Video Transcript

In this video, we are going to learn how to make our robot follow a line, as a really useful feature that will really help the accuracy of what you’re trying to do with your robot. So our color sensor that I’ve attached to here– oops– you could say that it goes there. And it shines down onto this line.

So it shines a light. And then it measures how much light is reflected back up. If it’s over a dark surface, it will take less light in. So, for example, this might be too. If it’s over a light surface, it’s going to take more. So, for example, this might be 70.

What we are going to try to do is we are going to try to get our robot to follow the middle of this line so that it knows if it starts to get too low, because it’s going over dark, we’re going to have it turn left. If it starts to get too high because it’s going over white, we’re going to have it turn right.

So there’s a simple formula when we’re going on the line from the left side, again, for now, we will do both sides. But for now, this is just from the left side. So the value of our reading– whatever that happens to be– minus the target value. So in this case, our target value would be 30 because it’s halfway between that line.

That is going to equal our steering value. And the cool thing about the EV3 software is that your steering value– you could if you take it out, you can notice that you could have it go either way. But it all has a value. So right now, that value is 28. That value is negative 38. That value’s negative 50, 69.

All right, so we are going to use that in order to help us make some steering. Now, if I go here, right now the target value is 30 because we want to on that how fine– oops– the actual value is going to be 30 because it’s perfect. So our steering would be zero. It would just keep on going straight.

However, if we want a little bit to the left– so the value is going to increase because now there’s more light going over it. And the target value is still 30. So 40 minus 30 equals 10. And 10 is going to be right. So as it goes right, the actual value of the sensor is going to decrease. So that will be 39.

So this goes to 9, 8, 7, 6 and so on. So the steering gets less sort of violent as it goes in there. It gets calmer and calmer until it’s right back on the line again being zero going straight. Conversely, if it goes over the dark area, then the value, like it’s going to go lower.

So the value is going to be 20. Target value is 30 that equals negative 10. And that will take it to the left. So let’s see how we can do this here. I’m going to a new program. I’ll call it line. So if I look at my value, I put it right over the green– see it’s seven.

If I put it over there, it’s 33. So my value is different than the one that I showed you in the presentation. And your value is probably going to be totally different than mine. That’s all right. Make sure you’re doing this for your own value and not necessarily just taking what I have.

So if I wanted to kind of split the difference there, that would be about 20. This doesn’t have to be precise. Just put it kind of in the middle. So now, what we’re going to do– I’ll kind of set this up. And then I’ll explain after how everything is working here.

So I’m going to get my loop. Then we’re going to go into this yellow block that we haven’t used yet. I’m going to get the color sensor out of there. Then I’m going to get this math block. And then I’m going to get the move steering. So if you remember, our value minus our target value is going to equal our steering.

So our target value is going to be 20. And now what I’m going to do is I’m going to take this– this data wire and plug it into here. And this reading will go into A. And then A– whatever that is minus 20– is going to equal the steering. Let me just change a couple of things here.

Instead of measuring color, we want to measure reflected light intensity. Op, that takes it out. And then we just want to turn this on– It’s going to take this here– reflected light intensity. So that minus 20 is going to equal our steering value. Let’s go here.

[CHIMES]

[WHIRRING]

Whoa. That was too quick. Let me slow it down. I think that was working. But you’ll get a better example of it if I slow it down.

[CHIMES]

Perfect. You can see it kind of goes out. And then it completely follows that line. If I were to go more over– I’m going to start over the center. Now, watch it. It’s going to go out and come back in. And also, if I were to start way out here– it’s going to go back in and straighten itself out eventually– cool.

It keeps them going here straight. Well, we do want to curve. But we will take care of that eventually. So now we have our left side working. However, what happens if I put on the right side? Say, put it over here.

[CHIMES]

It’s going the wrong way. That’s no good. We need to do something to change this. So our formula for the right size can be a little bit different. We need to kind of change the direction that it’s steering in. Instead of when it goes too bright, we want to turn right. Instead of that, we want to turn left.

So what we’re going to do is we’re going to multiply everything by negative 1. So here, again, 30 minus 30 is going to equal 0. This is going to work exactly the same. However, for this, we’re going to take 40 minus 30. And now, 10 would be going to the right. We don’t want that.

So we’re going to take 40 minus 30, which equals 10. Then we’re going to multiply that value by negative 1. And that is going to give us negative 10. And also, if we do 20 minus 30, that’s negative 10. Negative 10 minus 1 equals positive 10. It’s going to go back here. And instead of doing just a simple minus, they have this advanced math feature.

So what I’m going to do is, I am going to go a minus b times c. All right. So d is going to be nothing. C will be negative 1. Then I just put it in there. Now, this should work.

[CHIMES]

Awesome. If I bring it up here, comes back in, straightens out. Sweet– cool. And if you wanted to go on the left side, all you got to do is switch that to positive 1. And then it will work just the same as the other one, because any value times 1 is going to equal itself.

Now, we have a problem where we have this curved line. And we wanted to keep on following it. But it’s kind of, like, it doesn’t react in time. So it just kind of keeps on going. Now, what we’re going to do is we have this multiplication feature.

So what I can do is I can just– instead of having it be one, I can switch this to two. And now it’s going to take whatever that steering value is and multiply it by 2. So let’s say our actual one got up to 24. Normally that would steer back to the right at four.

However, what we can do is if we multiply by 2, then it’s going to steer back to the right at eight. And that’s going to make everything much sharper. You can’t go too high with this, because eventually, it’s going to just be really, really jittery. And it’s not going to work. But at two, it’s going to follow the line much more sharply. So let’s try this– see if it works the left side.

[CHIMES]

All right, didn’t quite get there. So let’s try three instead. This going to start to get pretty sharp here.

[CHIMES]

There you go. You could see it follows it much more smoothly. And even if I were to bring it off here, it’s going to come back much sharper than what it was before. So play with what you’re doing is just straight line. Then multiplying by 1 may be the perfect thing for you. If not, you might need to go higher.

So play around with your values and get what you want. But you should be able to follow just about any line with this method. So hope that was useful. Come back for one more video where I’m going to give you a final challenge to see if you could use this sort of methodology in programming to make your robot go in perfectly straight line–