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 - Bulid your own Lego policeman with a speed camera
- Ozeki Speedcam: http://www.ozeki.hu/index.php?owpn=6277
- Ozeki 10 installed: http://www.ozeki.hu/index.php?owpn=6066
- Lego connection installed: http://www.ozeki.hu/index.php?owpn=6067
- Snap basics: http://www.ozeki.hu/index.php?owpn=6180
Lecture video - How you can calculate the speed of the robot
Measure the distance from the robot twice using the Ultrasonic Sensor and store the results into variables!
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
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
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
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
Convert centimeter to meter and calculate the speed in meter per second!
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
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
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
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:  y: ' 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
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
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!
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
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  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  secs' block. These steps are demonstrated on Figure 12.
Figure 12 - Actions when the robot is speeding
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
Figure 14 - Program code that calculates the speed of the robot
|Download the SNAP program|
This code shows how to calculate the speed of the robot using the distance data measured by the Ultrasonic Sensor.
It is easier to follow the guide if you have the codes in front of you:
Download the SNAP program code