В момента ресурсът е наличен само на английски

How to align to a wall with Ultrasonic Sensors. Remove complexity (part 4) Pro Preview

We explore some common mistakes when a program is developed and becomes complex. We then try to remove part of this complexity. 

  • #166
  • 16 Nov 2015
  • 16:23

English

In the previous video we tried to build a program for aligning to a border but the program got quite complex and it included some nested switch blocks and it was not very readable so in this video we'll try to finish the program and as a final result have a program for aligning and to make the program more readable, more clear. Of course, we'll use some of the features of the EV3 software that we haven't used before. This will make the whole program much more clear. Let's go to the software.

This here is the program. We start with the Tank block that is moving motor A and D forward with the power of 50 until the sensor on port 4, our left sensor so motor D is our left motor and motor A is our right motor, sensor 4 is our left sensor. Compare, distance, inches, less than or equal to 4 inches. We wait for the sensor to detect a border on about 10 cm or 4 inches and we stop with both motors. Up until now nothing new because we've already done this program. Now the next step as we have detected the wall with the sensor on port 4, and this is our left sensor. Then we must turn our right motor. How does that look? It looks like this. This here is the border and this here is our robot and on this side we have sensor number 4 and this here is sensor 1 and this here the motor near sensor 4 is motor D and this here is motor A. We've just detected the border to about 4 inches or 10 cm with the sensor 4, now we must move the whole robot forward but only with motor A and then move a little back with motor D to make sure that the sensor on port 1 detects the border at 4 inches or 10 cm and the sensor on port 4 also detects the border on 4 inches or 10 cm. The next thing is to move forward with motor A. How do we move forward with motor A? We select motor A and we just turn it on with not much power, like 10% and add it to the program. Then we wait until the ultrasonic sensor detects a distance of less than or equal to 4 inches and this will be the sensor on port 1 and then we stop with motor A.

Let's run the program to see if it works. Positioning the robot, running the program.

Move and then with a small move with motor A after we stop. Again.

This is how our program works. The next thing we should is just move motor D a little back so we can align perfectly. Let's implement this in the program. Implementing this into the program will require us to move with motor D but backward. Let's remind ourselves.

This here is the border, we are approaching the border with the robot and we have the sensor on port 4 right here and the sensor on port 1 here. First we detect the sensor on port 4 and then we move motor A forward until the sensor on port 1 sees the border at about 4 inches or 10 cm and now we must move the motor on port D a little back, so that it sees the whole

border, again on 10 cm. How do we do this? We take motor D, we start it on but with a negative power of -10 which means in reverse until the sensor on port 4 detects a distance of exactly this time we want exactly 4 inches and then we stop with motor D.

Let's run the program and see if it works.

As you can see the robot aligns almost perfectly. We've done this in previous videos. Now we must also add the logic for the other sensor when we approach the border from this side. Before this we would like to make the program more clear. What I will do is extract part of this behavior in the program into a new block. Let's just zoom out.

I would like to extract a behavior of our robot.

What is the behavior of our robot. After it detects with the sensor on port 4 that we have a border in front. The behavior is right here and this behavior is that we align to the left so we are slightly moving the right motor and we align to the left and what we can do is extract the whole behavior. This behavior and from tools, my block builder, build a new block and this new block will be called: align_right

and I'll choose some icon, finish. This is our align right block.

Is the program still running, let's check to make sure we haven't broken anything. Running the program.

So we've extracted part of the blocks into a new block and we know the behavior of the new block align right because we're aligning to the right.

Let's just start the program again.

Because we are aligning to the right we have this new block with the new behavior. Now we'll implement the same for the other part, when we are aligning to the left. We must align to the right only if the sensor on port 1,

the left sensor detects the border. So we must do a check and this check is right here. It's a switch and we check if the ultrasonic sensor on port 4 detects something on less than or greater than 4 inches only then we align to the right.

And we execute this align to the right block only if we detect the border with the sensor on port 4. Now similarly if we detect the border with the ultrasonic sensor on port 1

4 inches or 10 cm, we must do exactly the same but to align to the left. What would be the program to align to the left, let's again draw this. We have our border here and we have our robot and it is approaching the border from here and this here is the sensor on port 4, this here is on port 1, we detected right here the border, so we must turn the motor like this and then turn it a little bit like this and since the left motor is on port D and this one is on port A we must turn port D forward and port A backward. Let's do this. It's quite straight forward. We start port A forward not very fast with the power of let's say 10

until the sensor on port 4 detects something at exactly 4 inches and when it detects something at exactly 4 inches we stop with the motor.

Then we must turn the motor on port

Did I make a mistake here? Let's draw this again.

We have the robot and the robot has motor D here and motor A here and this is the sensor on port 1 and this is the sensor on port 4.

We must move motor on port D forward and motor on port A backward. So D is forward, A is backward. This is D forward and then A backward. Motor on port A on with -10, slightly back and we wait until the sensor on port 1 detects a distance of exactly 4 inches. We want exactly 4 inches and we stop with motor A.

Now we extract this whole behavior into a new block and we call it align_left.

Again I'll add it some icon.

We have a program that's more clear, because if we detect something with one of the sensors we then align right or with the other sensor we then align left. We can even go inside these blocks and if we need we can change some of the properties in the blocks. The basic principle is this. Now one more this we should do is that currently when we are approaching the border we are only waiting for one of the sensors to detect the border, in this case the sensor on port 4, but it might be the other sensor detecting the border. So we must wait until one of the sensors either the onе on port 1 or the sensor on port 4 detects the border. For this we'll have to use a switch block with some logic but we'll do this in the next video.