Arriving at a final solution. There is still room for improvement on using the Mindstorms Ultrasonic Sensor for aligning to walls, but we hope the whole five videos could give you a good idea of how to start with a problem and reach a solution.
- 17 Nov 2015
The same principles could be applied for nearly every program. For Touch, for Color and for other sensors. The process of improving the program without modifying its behaviour is called Refactoring. This videos do not present show the whole process of refactoring, but would give us a starting point to explore the subject in some of the next videos.
Previous video tutorials:
Finally in this video we'll build a program for aligning to the border with the 2 Mindstorms ultrasonic sensors. In the previous video we did a number of mistake and this was the process that I wanted to walk you trough, so that you know how to think when implementing such a program. Let's now go the program and finish the alignment to the border.
This is where we stopped and the program was working in the following way. First we move forward, then we detect the border with one of the sensors, then we stop the motor and if we have detected the border with the sensor on port 4, we align right
and if we have detected the border with the sensor on port 1 then we align left and you can, of course, find the programs below the video, you can also check out the previous video of the series to know when we stopped, but what we have to implement now in this program is the initial detection of the border. Currently we detect the border only with port 4, we are moving forward until we detect the border with port 4, but we want to detect the border with either port 1 or port 4, with one of the 2 sensors and for this we'll have to use the following logic.
We'll take one sensor block and another sensor block and we would compare the distance to the border to be less than or equal to 4 inches or 10 cm with the sensor on port 4 and the same with the sensor on port 1.
Now this block, the compare block, we'll return a result that's true if the sensor detects a value of less than 4 inches or 10 cm and than we use a logic block, that's one of the advanced blocks in the EV3 software and this here is the logic, we want
either one of the sensors to return result of true. So we need port 4 or port 1 to return true and if one of them returns true, we know that one of them has detected the border. We add this in a loop, so we are doing it constantly, constantly checking and we finish the loop only when the logic block returns true. That's the program. It was quite fast as an implementation. We are constantly detecting with port 4 and port 1 when one of the sensor sees the border it will return true and from there this whole block, the logic block will return true and the whole loop will finish. When the loop is finished we stop the motor and then if it was port 4 to detect the border we align to the right, if it was port 1 to align to the left. It's a pretty straight forward program. The most complex logic is right here with the logic block and the end of the loop. Let's see if the program works as expected.
It tries to align and then with the other.
It again tries to align. From here.
Now there are some other configurations that could be done with this program and you must have in mind that the sensor might not be very accurate. It has about a cm of accuracy sometimes. It might require some modifications with the values of the turning and the speed, but that's the general principle for aligning your robot to a border with the 2 ultrasonic sensors.
Now if you find some situations in which the program does not work and you would like to improve it, leave us a comment below and we'll be happy to work on other solutions but I prefer for the moment not to enter into more complex solutions.