Course 8 / Lecture 10:

Use a VR headset to control your camera

In Lecture 9 you have built a PTZ camera. In this lecture you will learn how to control it from a VR headset. You will learn who to use the gyroscope in an Android mobile phone. You will learn how to use this information to control the motors to align the camera with the mobile phone. If you put this mobile phone into a VR headset your camera can track the movement of your head.

Figure 1 - Control the Spycam robot with the VR headset


Task #1:

Setup the orientation sensor connection!

Step 1: Create the orientation connection

To make the camera to follow the movement of the mobile phone you need to create a connection that can handle the position information coming from the phone. That connection will be the Orientation connection, that you can create in Control Panel. So, just click on Create new Connection, and like in Figure 2, select the Sensor method from the connection methods.

Figure 2 - Connection methods in Ozeki 10

From the Sensor connections, next you have to choose the Browser sensor connection, and lastly select the Orientation sensor as you can see in Figure 3. Last, you can modify the details of the connection, like the name of the connection, and you can set a threshold for the orientation.

Figure 3 - Browser sensors in Ozeki 10

Step 2: Create a route to SNAP

Next, you need to make sure that the information from the sensor will arrive to the SNAP application. So, for that you need to create a route in Control Panel. After you selected the Routes menu, click on Create new Route, and here you need to select the Orientation sensor from the list to the From part (Figure 4), and the SNAP application to the To part.

Figure 4 - Route creation from the sensor to SNAP

Task #2:

Open Ozeki 10 on your mobile phone's browser!

The orientation sensor will get information from the mobile phone's position, but for that you need to open Ozeki 10 on the phone. For that you just need to open a browser on your phone, and enter the IP address of the PC, and the 9505 port like in Figure 5. It is important that both your phone and PC needs to be on the same network, and you have to turn off the firewalls on the PC.

Figure 5 - Address entered in the phone's browser

Task #3:

Write code to store the initial orientation of the phone!

Now, with your phone is connected to the sensor connection, you can start writing the code in SNAP. So, first drag the 'When green flag clicked' entry point. Then, you have to subscribe to the orientation sensor with a 'Subscribe' block and here select the orientation sensor. Next, add the 'Get next message' block to get the first information from the connection. And lastly, like in Figure 6 create a variable with the name 'azi_start', and with the 'Set 'azi_start' to numeral 1 in message' block set this variable to the initial azimuth value. The message contains several data, but with this block you can select the information that you need without splitting the message.

Figure 6 - Initial heading of the phone stored in 'azi_start' variable

Task #4:

Extend code to read orientation data continuously!

After you stored the starting orientation, you need to read the data of the z and y axises. For that, first add the 'Forever' block. In that block, first place the 'Get next message' block. Next, you need to create two variables with the names 'azimuth' and 'roll'. These will store information from the z and y axises. First as you can see in Figure 7 with the 'Set 'azimuth' to numeral 1 in message(int)' block, save the value of Z axis to the 'azimuth' variable. Lastly store the value of Y axis into the 'roll' by dragging the 'Set 'roll' to numeral 3 in message(int)'.

Figure 7 - First numeral value of the message saved in 'azimuth'

Task #5:

Create block rotate the camera using the Y axis!

To make the appearance of the program a lot more nicer, next you need to create a block that will do all the actions in case you move the camera up or down with your head. So, just click on Make a Block in Control menu, name the block 'Follow roll', select the Command option and click OK. Then click on the + button before the 'Follow roll' text and type '$arrowUp'. This will draw an arrow icon on the block's head as you can see in Figure 8 and make it look better. You can do it on the other side, but that time type the text '$arrowDown'.

Figure 8 - Head of the 'Follow roll' block

Task #6:

Extend 'Follow roll' block to set the motor speed to 5%!

Next, in Block Editor, you need to add some blocks into the created 'Follow roll' block. First, you need to set the motor speed. This can be done with the 'Set motor speed to 5%'. Next, you need to create a variable that will have got the name: 'roll_boolean'. This variable will show you that the motor needs to be rotated or not. You need to set that variable to true like in Figure 9 with the 'Set' block, and here select the 'roll_boolean' variable.

Figure 9 - The value of 'roll_boolean' variable set to 'true'

Task #7:

Upgrade 'Follow roll' block to make the camera look down!

The controlling of the camera will have three cases in this block. If the camera looks up, if the camera looks down or it looks completely straight ahead. So, first of all place three 'if' blocks below the head of 'Follow roll' block. In the first one the condition needs to be that if the 'roll' variable is between 0 an -70 and the 'roll_boolean' is true like in Figure 10. If all these conditions are true, set the angle of the motor B o 340° by adding the 'Set motor B to 340°'. Here, you need to also add the 'Wait 1 secs' block and set the 'roll_boolean' to false by adding a 'Set' block.

Figure 10 - The 'if' checks if the phone is looking down

Task #8:

Expand 'Follow roll' block to make the camera look up and straight ahead!

The second 'if' of the block needs to have the conditions that the value of 'roll' is between 0 and 75 and the 'roll_boolean' is true. If these conditions become true, first in that 'if', drag the 'Set motor B to 30°'. That will make the camera look up. Next, add the 'Wait 1 secs' block, and set the 'roll_boolean' variable to false as you can see in Figure 11. In the last if, place the condition that if the 'roll_boolean' is true. If it stays true till that 'if' block, that means the camera needs to be look straight ahead. For that add the 'Set motor B to 0°' and also add here the 'Wait 1 secs' block.

Figure 11 - The full 'Follow Roll' block

Task #9:

Create block to rotate the camera to left and right!

The next block, that you have to create will be responsible to look left or right with the camera. So, click on Make a block, and type the name 'Follow azimuth', and select the Command type. Next, in Block Editor, you can give the same look to this block as you did with the 'Follow roll' block, by adding the '$arrowLeft' and '$arrowRight' to the head of it as Figure 12 demonstrates. Next, set the motor speed to 5% by adding the 'Set motor speed to 5%' block. Lastly create a variable with the name 'azi_boolean' and with the 'Set' block set the value of it to true.

Figure 12 - Head of the 'Follow azimuth' block modified

Task #10:

Upgrade 'Follow azimuth' block to look left and right with the camera!

The method of moving the camera to left or right will be the same as it you did with moving it up or down, so first, place three 'if' blocks. In the first one, set the condition, that the 'azimuth' variable is bigger than the value of 'azi_start' + 20 and the 'azi_boolean' is true. If that happens you need to rotate the camera to left by adding the 'Set motor A' block, and type 310 degrees. Also add the 'Wait 2 secs' block and set the 'azi_boolean' to false (Figure 13). In the next 'if' add the condition, that the value of 'azimuth' is smaller than the 'azi_start' - 20 and the 'azi_boolean' is true. If that condition turns true, you need to rotate the camera to right. So, do that with the 'Set motor A to 50°' block. Please add the 'Wait 2 secs' and 'Set 'azi_boolean' to false' blocks. In the last 'if' the condition is that if the 'azi_boolean' is still true. So in that case set the motor A angle to zero degrees with the 'Set motor A to 0°' and also drag the 'Wait 2 secs' block.

Figure 13 - Actions in 'Follow azimuth' block

Task #11:

Upgrade code to run both 'Follow roll' and 'Follow azimuth' blocks simultaneously!

To run the created blocks side by side you have to place these two blocks after different entry points. So, for that drag two 'When I receive' entry points, and add the 'Follow roll' to the one, and 'Follow azimuth' to the other entry point as you can see in Figure 14. But what these entry points will receive? For that drag the 'Broadcast' block into the main program, and from the drop-down menu select 'add new'. Here just type 'move', and after you clicked OK, select it for the 'Broadcast' block. The last thing that you have to do is to select 'move' for both 'When I receive' blocks.

Figure 14 - Both block can run at the same time

Task #12:

Insert your phone into the VR headset and check the results!

Figure 15 - Phone inserted into the VR headset

Program code

Figure 16 - The program can control the Spycam

More information


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