Course 6 / Lecture 6:

Build a speed camera

In this lecture you will learn to build a speed camera. You will learn different methods to determine the speed of the robot. You will do this by measuring the time, and the travelled distance. To build the speed camera you will use the ultrasonic distance sensor, and the time measurement code you have learnt about in lecture 3. You will take two measurements, just like real life speed cameras do.

Figure 1
Figure 1 - Bulid your own Lego policeman with a speed camera


Instruction video

Lecture video - How you can calculate the speed of the robot

Task #1:

Measure the distance from the robot twice using the Ultrasonic Sensor and store the results into variables!

Step 1: Understanding the method of the Speedcam

The main task of the speed cameras is to make two distance measurements from an approaching object with a given time interval in order to calculate the speed of the object. From the two measurements the covered distance can be easily calculated. The covered distance means the distance the robot traveled between the two measurement. By knowing the covered distance and the time interval, calculating the speed either in meter per second or in kilometer per hour is just a few operations. The formula for calculating the speed is shown in Figure 2.

Figure 2 - The formula of velocity calculation

Step 2: Determination of time interval

After you understood how the speed cameras work, it is time to create your own one. As always, SNAP is a great assistant for that. So, open it, and let's start by adding of 'when green flag clicked' entry point. Then you need to create a 'timeInterval' variable where you will store the value of the interval you want to wait between the two measurements. In the example below (Figure 3), the interval is half a second. Finally, attach a 'wait [secs] secs' block to it and set a value for its 'secs' field to have enough time to point your sensor to the direction of the robot.

Figure 3 - Time interval between measurements

Step 3: Measure the distance twice

Now, you can start measuring the distance. For that, create two variables called 'distance1' and 'distance2'. After that, with a 'set' block store the first measurement into the 'distance1' variable. You need to use the 'Distance in cm [connection]' block for measuring the distance. So select the connection of the Ultrasonic Sensor for its connection field and drag the block into the 'set' block. After that, attach a 'wait [secs] secs' block to it, and drag the 'timeInterval' variable into it. Due to this, the program will wait until the time interval expires before the second measurement begins.

Figure 4 - Distance values stored

Task #2:

Calculate the covered distance of the robot!

Now you have done all the measurements that you need to calculate the covered distance and the speed. Let's start with calculating the covered distance (Figure 5). You just need to subtract the value of the second measurement from the first one and take the absolute value of the result. Due to this you can calculate the covered distance of an approaching robot and also of a receding robot.

Figure 5 - Calculation of covered distance

Task #3:

Convert centimeter to meter and calculate the speed in meter per second!

Step 1: Theory of speed calculation

Since the Ultrasonic Sensor measures the distance from the robot in centimeters, you need to do a conversion to calculate a standard unit of speed which will be meter per second. For that you only just have to divide the covered distance by one hundred, and now you have got this distance in meters. So you can calculate the speed (Figure 6). This is also a simple calculation, you just need to divide the covered distance (which is now in meters) by the time interval (which is in seconds).

Figure 6 - Calculation of the robot's speed in meter per second

Step 2: Implement the calculations

To store the speed of the robot, first create a variable called 'm/s'. Then attach a 'set' block to save the result of the calculation to the 'm/s' variable. First, insert a division operator into it, and drag the 'timeInterval' variable to the right hand side. To the left hand side, drag another division operator. Here you will do the conversion from centimeter to meter. So, you should type 100 to the right hand side of it. To the left hand side of the inner division, add the '[sqrt] of [value]' found in Operators and select the 'abs' option from the list. Lastly in the 'abs of' block, add a subtraction operator and insert the two variables that store the measurements. And if you have done everything right, you have just successfully implemented the code that calculates the speed of your robot. The required steps are demonstrated on Figure 7.

Figure 7 - Speed of the robot stored into a variable

Task #4:

Convert meter per second to kilometer per hour!

The meter/second unit is also conventional to measure speed, but the speed cameras in the real life display the speed of the car in kilometer/hour. To be able to dispaly the speed in kilometer/hour, you just need to multiply the value of the 'm/s' variable by 3.6. So create a variable called 'km/hour', insert a 'set' block into the Scripts field then select that variable for it. Finally, insert a multiplication block into the previous one and put the 'm/s' variable into one of its side, then write contant 3.6 to the other side like Figure 8 demonstrates.

Figure 8 - Speed converted into kilometer per hour

Task #5:

Create a block to display the speed of the robot on LCD screen of the Brick!

With the creation of your own block you can make your SNAP codes simpler. Now you need to create a block, that can display the speed of the robot on the LCD. For that, just select the Lego category and click on 'Make a block'. Here give the name 'Print', and select the Command option. After you clicked OK, expand the head of the block with an input field and name it 'text'. Next, you need to add a 'Clean LCD' block for cleaning the current content of the screen. To set the font size to large, attach an 'LCD font size [large]' block to the previous one. Then you need to insert an 'LCD text [text] position x: [5] y: [55]' to display the text on middle of the Brick LCD. Finally, there is an 'Update LCD' block to commit the previous changes.

Figure 9 - Block for displaying text on the LCD screen

Task #6:

Create a block to round the given number to three decimals!

To display the speed in pretty format, you should create a block to round it to 3 decimals. Here you can do a little trick. In case of the robot, the whole number of the speed value always be 1 digit. So, to round it to 3 decimals, you need to keep the first 4 digits. So, click on 'Make a Block' and now name this block 'round to three decimals', select the Reporter option and click OK. In the Block Editor first expand the head of the block with an input field and name it 'number'. Then you don't need to add any additional block, you have to just expand the 'Report' block. In that block, drag the 'join' block. Here you need to join the first five letters (1 whole digit, the fraction sign and 3 fraction digits) of the initial number. So expand the 'join' block to have five members, and drag the 'letter of' block to each part. Lastly add the 'number' variable to each 'letter of' block like on Figure 10.

Figure 10 - Block to round the speed to 3 decimals

Task #7:

If the measured speed is more than 1 km/h, flash the LED in red light and warn the police officer. If not, flash it in green light. In both case, print the measured speed!

Step 1: Make a decision

At this point, your program can measure the speed of the robot, but it has to make difference between a robot that goes at normal speed and a robot that goes over the speed limit. It maybe sounds difficult, but the implementation of it is not a hard task. To do that you just need to add an 'if-else' block, and in the condition of the if, place a '>' operator. To the left hand side of the operator, drag the 'km/h' variable. To the other side, type number 1 as can be seen on Figure 11. This is a condition about what to do if the robot's speed is greater than 1.

Figure 11 - The robot is speeding if its speed is above one kilometer per hour

Step 2: Warn if the speed is too high

First you should fill the case when the robot goes too fast. So, first drag a 'Set LED' block and select the 'red flashing' option here. Then add a 'repeat' block and type 5. This is used to display the speed and the warning label 'Too fast!!' five times. So, in the 'repeat' block first add the 'Print' block you created before, and type 'Too fast!!'. After that drag a 'wait [1] secs' to wait one second before displaying the speed, so you can read the text properly. Next, you need to attach a 'Print' block and insert a 'join' one to it to concatenate the 'km/h' variable's value with the text 'km/h'. But before doing this, please place the variable into the 'round to three decimals' block. Lastly add another 'wait [1] secs' block. These steps are demonstrated on Figure 12.

Figure 12 - Actions when the robot is speeding

Step 3: Flash the LED in green ligh if the speed is normal

To the other case, when the robot goes at normal speed, you only need to add two blocks. The first one is the 'Set LED' block, where now you need to select the 'green flashing' option to sign that the robot does not speeding. Next drag the 'Print' block, and as it was in the other case, with a 'join' block join the 'km/h' variable that rounded to three decimals with the 'km/h' text. If you have done everything right till this point, your program should look the same like on Figure 13. Time to try it out!

Figure 13 - Actions when the robot goes at normal speed

Program code

Figure 14 - Program code that calculates the speed of the robot

More information


Copyright © 2000- |Ozeki Ltd | |
Page: 630 | | Login
Thank you for visiting this page