How to measure the speed of the robot Pro Preview

A human being walks with an average speed of about 5 km/h (3 miles/h). In this video tutorial we will develop a block for LEGO MINDSTORMS EV3, that measures the speed of the robot in distance/time units where time will be configurable and could be 1 second or 0.5 seconds or any other number of seconds you pass as a param to the block. Measuring the speed of the robot is very powerful if you start building an Artificial Inteligence for your robot and is quite fun :)

  • #39
  • 09 Sep 2013
  • 12:54

For this program we use an LEGO Mindstorms EV3-G version of the software and the robot, but the same principle could be applied to NXT robots as well. Share with us if you think we should build a new movie about measuring speed in NXT. 

While developing the speed measure block you will learn about the use of the Ultrasonic Sensor, Integer Variables and Display block. It is also "tricky" to display three different numbers on the screen since you would have to change the coordinates for each one. Find out how all of this is done in the video. 

Mentioned Episodes

Here is a list of episodes that might be useful to watch. 

  • Episode 38 - We start with the project developed in episode 38 where we build a block for moving a specific distance in mm.

Speedometer designed by Ayesha Rana from The Noun Project

Current speed block

- a project containing the current speed block. The block measures and displays on the screen the current speed of the robot.

Current speed block

- a project containing the current speed block. The block measures and displays on the screen the current speed of the robot.

Try to align Ultrasonic program

English

In this episode I would like to start from episode 38 where we developed a Distance Move Block. And continue further to develop a Current Speed Block. This Block will measure the speed of your robot in this case millimeters per second. And it will display this value on the Display. And let's see how it works.

First I would like to start with the project from episode 38 where we have a DistanceMove Block. In this Distance Move Block we can specify the number of millimeters for example 200. That we would like our robot to move and the power of the motors. For example 30. What I would like to do is to measure the speed of the robot while the robot is moving. So because this is WHILE the robot is moving we should do this in a parallel thread. And in this parallel thread we can measure the speed of the robot in several different ways. Today I will stop at measuring the speed of the robot with a Ultrasonic Sensor. So I would like to measure the distance from the robot to, for example to the border of the field or to a wall. Then do some calculations depending on the measured value and then display these calculations. With the Display Block on the robot. This is our logic for today and we should now somehow connect all these Blocks and do the program. Measuring the distance is done with the Ultrasonic Sensor and after measuring we can diplay the value on the Display. What exactly is the distance between the robot and the border. Let's start a new thread. Right here. Okay I will leave the mathematical Block for a moment later. Then we would like to Display a wired value. And we are displaying the value of the Ultrasonic Sensor on the Ev3 screen. And we should do this in a Loop. So we should constantly measure and do this in a Loop. We take a new Loop, right here and put the two Blocks inside.

And one important thing is that after displaying the value we should wait for about a second. So that we could actually see the displayed value. Okay, let's see how it works.

As you probably know Velocity or Speed is actually measured. What is Velocity? Velocity is change of distance over time. So V is actually change of distance over time. Now what I would like to do is to measure the distance to the wall at a second moment. So I'm again adding a new Sensor - Ultrasonic Sensor. I will again display the value of the Ultrasonic Sensor on the Display. This will be a Text and it will be a wired Text. And then I will again wait for another second.

To see both values. These two Display Blocks they will display the value in the same position. And this is not very convenient. So we would like to display the second value at the position of Y for example: 30 That is right 30, this will display one of the values below the other. What we currently have is the distance to an object in moment One. And the distance to the object just one second later. So let me write this in a comment. We have the distance in moment one. Then we have the distance in moment one + one second. And this is actually our Velocity. The difference between the different distances. The distance in the first moment and the second moment will give us the speed of the robot for one second. And since we are measuring the distance in millimeters, then we know that the difference between S1 - S2 Will give us the Velocity of the robot measured in millimeters. And this Velocity will be in millimeters per second. So let't display this value. I will take the Mathematical Block like this, then I will take the fist measurement put it in the Mathematical Block. Then the second measurement put it in the Mathematical Block. Then I will choose Subtract since I would like to subtract the first distance from the second distance. And then I would like to display this value on the Display. I will chose Text it will be a wired text and the result will be connected here. One important thing is that I would like again to change the Y of the result. The Y coordinate it will be let's say 60 or 70.

70. Now what we will see on the Display is the fist measurement, then the second measurement. And then the difference between the to measurements. And this difference is the speed of the robot in millimeters per second. Let's see how it works.

Generally I will consider a bad programming practice to have the measurement of the fist Sensor And the measurement of the second Sensor that are made in a different moment. To have them both used in a single Mathematical Block. Because when the program reaches the Mathematical Block it's kind of illogical which value exactly is taken from the Sensors, the value in the first moment or the value in the second moment. That is why it is much more clear for someone reading your program, to save these values in variables. And then to use those variables in the different Mathematical Blocks. For example after we display the fist value on the Display we can take this value and write it in a variable. Right here and we will call this variable Point1. This is our Point1. Then we can of course remove this and remove this and we can do the same for the second moment. Take a new variable Block chose Point2 and write the measurement in the second moment in variable named Point2. And as a result we can do the subtraction between Point1 and Point2 and then display the result value. Again that is a nicer way to implement your program and it is much more clear which values are taken in which moments. Let's do the subtraction, here, first Block second Block we take Point1, we take Point2 and we take Read. We read the values and then we subtract Point1 and Point2. Okay let's see how it works.

As you can see this program is becoming quite large it is not very easy to see the whole logic on one screen. And that is the moment when you like to build a new MyBlock. So let's select all these Blocks here, then move to the right. Use the CTR key. Oh, I deselected them. Okay, so select. Move left, use the CTR key to select the others and use the MyBlock builder. Our Block will be called CurrentSpeed. Let's choose an icon for this Block, and we need to add two Parameters. The first Parameter will be an output Parameter, the whole Block will give us the CurrentSpeed 'CS'. And it has an Icon like, for example this one. And we need one more Parameter, this will be the input Block that we specify the time interval. In which we would like to take different measurements. And we will call this Parameter - Time. And it will be input Parameter, a number and it will have, for example this icon here. Finish!

Great! We have a new Block that is called CurrentSpeed. And as a result we would like to return the difference between the two measurements. This is our current speed, so we take this result from the Mathematical Block and return it. And the other thing we like to do is to pass this input Parameter which is the time to the two Wait Blocks. For example if we passed one second here, we automatically wait one second here and we will automatically wait one second at the last. Oh, my screen is not enough, let me.. Great! It worked! So we pass the input Parameter to the Wait Blocks. Let's see how the program looks like. We have a Current speed here as a Block. I will choose the Loop, it is quite large.

And add the CurrentSpeed here.

And one thing we should do now is to specify weather we would like to measure the speed for one second or for two second, what is our time interval. And that is why we will use a constant. This is our Constant, our Constant has a value of one and we are passing this Value to the 'CS' input Parameter. And that is for our program. We can now measure the speed of our robot in units of millimeters or centimeters in the metric system for seconds. Let's see how it works.

Measuring the speed of the robot can be very useful if we are trying to implement a smarter robot that has some initial form of artificial intelligent so feel free to use this CurrentSpeed Block and DistanceMove Block in your programs. And get back to us to tell us how it works.