In the first LEGO tutorial we started following a wall with the Ultrasonic Sensor. In this tutorial we would improve the program, remove the zig zag movement and make it very smooth.
- 21 Oct 2015
Again, it is a good idea to start with the EV3 basics course. Ultrasonic Sensor to get an idea of the use of the sensor and how to program it.
We make the wall following program much smoother and reliable.
Previous videos from this series
Previously we finished at the very simple program for using the LEGO Mindstorms EV3 ultrasonic sensor, to follow the border on a field. The program worked like this.
It was a simple zig-zag following of a line. In today's video I would like to improve this program or even to implement a completely different one, so that the program behaves like this.
More smoother following of a border. Let's see how we can do this.
This was the previous program, the zig-zag program. Now, I'll delete everything in this program and we'll use the same blocks but in a slightly different way. There is a block that we discussed previously. It's called the Move Steering block and I would like to move motors A and D and I would like to move them not very fast so you can see it on the camera but actually it doesn't matter if you do it very fast it will and can work for the program, but it's better to use a lower value so that you can see it on the camera. I'll just turn the motor on. There is this steering option and this steering option tells the robot in which direction to steer. This here is the positive direction, and this here is the negative direction. If we set a steering of let's say -51 this means that the robot will steer to motor A and since our motor, motor A is the left motor, the whole robot will steer left. If we add +51 it will steer to motor D this means right. We can use this option in the block. How can we use this? We take the sensor block for the ultrasonic sensor but not the Wait block, but the sensor block, that gives us the exact distance at which we are from the border. We'll take this distance, let's say in measure in inches and we are measuring the distance in inches. What we want to do is the following. We want to keep the robot at about 4 inches from the border. I'll take one more Math block. This whole program we'll use a Math block, Move block and Sensor block. With the Math block we'll do the following logic: we take the measurement from the sensor and we subtract this with let's say 4 because we want to keep the whole robot at 4 inches from the border. We subtract 4. Now if the sensor returns a value of 1 then 1 minus 4 will give us -3 and if the sensor returns a value of 10 let's say then 10 minus 4 will give us a value of 6. First value is -3 and second value is 6 and we would set this as the value for steering and this would make the robot steer and we add all this in a loop.
So, again the logic, if we are too far away from the border this means that we are for example at 10 inches as a distance, we subtract 4 because we want to keep the robot at 4 inches.
We are left with a positive value. The positive value will make us move slightly to the border. A negative value will make us move slightly away from the border. Let's see how this program behaves. I'll just place the robot like this, as you can see it's not aligned to the border it is slightly tilted to the border. We now press run. The robot starts, it moves and as you can see it's slightly turning to the border and it would start following the border now outside the view of the camera. Again. Place the robot, it slightly turning and tries to align.
What we should do now, the problem is that there's a difference and we pass this difference to the block for steering and if there's a 1 inch difference we pass to this block 1 as a steering, so 1% and we want one of the motors to move 1% faster than the other. What we need to do in order to catch the border faster and to follow it is when there is, let's say one inch difference to have this motor move 10% faster. So to multiply this difference. How can we do this? In the software, what we need to do is to take this difference that's the difference between the detected value and the value that we want to have as a distance between the robot and the border. We take that difference and we multiply it by something. Let's multiply this difference by 10. And pass the result as a steering value. Download and run. Place the robot here. It is not aligned to the border and now run the program.
The robot catches the border and it starts following the border.
Again, it's very near to the border it moves away and then it tuns and starts to follow the border and it will now keep a distance of about 4 inches. Let's change this value of 10 to 20 to see what happens. I've now changed the value that we multiply in the program to 30 and look at the program now. It is not aligned to the border, start, it moves, it finds the border and it keeps a distance of about 4 inches to the border and continues following the line. Again. Not aligned, I'll place it even like this so it's not aligned in the other direction. Start, it finds the border, it aligns to the border and it starts following the border. Very simple, very elegant solution. One more modification that we must do to this program is when do we stop following the border. We can stop following the border by setting the number of times or the number of seconds that we want this loop to be executed. If we want to execute this loop for let's say 5 seconds, 5 is too much, let's make it 3 seconds, we want to execute this loop for 3 second, this will make the whole program work for 3 seconds. Again, the program, we take the distance between the sensor and the border, then from this distance we subtract the distance that we want to keep our robot away from the border, in this case 4 inches, because we detect the distance in inches. Or if we detect the distance in centimeters we must subtract 10 cm. Then if the result from this operation is negative, this means that we are close to the border, because if we have 1 inch here and 4 inches here we have -3 it's negative, that means we are close to the border and if we are away from the border, the result from this whole operation will be a positive number. So negative, positive. Then we multiply this number by 30 and we pass the result to the steering block. If the steering block receive a positive number it will steer to one of the directions and if it receive a negative number it will steer to the other direction. In this way we keep the robot constantly moving near the border at a distance of 4 inches.
Running and counting. 1, 2, 3 So the whole program continues for 3 seconds. Now for how much seconds you would like this program to work, depends entirely on you. It depends on the program that you're using, the competition, the mission model, so it's entirely on you. Again it works in the following way. Start
It moves for 3 seconds and then we know that we are probably aligned to the border we have followed the border long enough to reach our mission model and do something with this mission model.
This was the program for following a border with the ultrasonic sensor. It's not very difficult, I think it's understandable, it's clear, it's an easy program and if there is a need for more complex program or a program that solves a specific challenge, give us a comment below, leave us a comment below and we'll try to build a lesson for it. Of course there are other ways to make an even more precise following of a border but this will be a subject of some of the next videos.