Course 9 / Lecture 1:

Collect sensor data into Text files

In this lecture you will learn, how to collect sensor data into text files. Your task will be to write code, that will receive events from an IR remote, and will create a text file in a directory for each event. The text will contain data from the event. In this case it will store which button on the IR remote was pressed.

C9 L1 Figure 1
Figure 1 - You will see how to use Ozeki 10 for storing IR sensor data

Requirements

Task #1:

Create a Verbose text file connection from the Control Panel!

If you want to collect data from the sensors, you will need a connection that can store the incoming data from them. In this lecture the 'TXT File' connection will be used (Figure 2). You can create it quite easily, like the other ones in the Control Panel. After you clicked on 'Create new Connection', select the 'Application' connection method and choose the 'TXT File' connection (Figure 2).


Figure 2 - Add 'TXT File' connection from the Control Panel

Before you can finish the creation of the text connection, you should do some modifications on the connection details. First you can modify the connection name or just leave it as default. Then you have to choose Verbose (Figure 3) from the available File formats. This format provides not only the message itself, but the originator and the recipient connection's name to. Figure 1 on the top of the lecture is a great example for this. Finally you can set the folders for different type of messages.


Figure 3 - Configure the 'TXT File' connection and click 'OK' to save settings

Task #2:

Select the Remote mode for the infrared sensor in Control Panel!

Before you can write the SNAP program to collect data from the infrared sensor, please switch the mode of the infrared sensor to Remote. Do it by selecting your infrared sensor connection from the connection list to open it. Find the 'Configure' tab (Figure 4) and in the 'Infrared mode' option please select 'Remote' from the list of available modes. In this mode the IR beacon can send number 1, 2, 3, 4 depending on the button you press. The robot's sensor will detect the value.


Figure 4 - Use the infrared sensor in Remote mode

Task #3:

Write code to collect button presses on the infrared beacon and collect them into Verbose text files!


Step 1: Create a code to detect sensor data

Now everything is set to receive sensor data except the program that gathers and stores data in a text file. So it's time to create this program in SNAP. After you opened it, you should add an entry point with the first block add an entry point which activates when the green flag is clicked (Figure 5). Then drag a 'Forever' loop under it, which ensures that you can collect as many keypress information as you wish until you stop the program. Finally add a 'wait until' block and drag the '>' operator to it. To the higher side add the received value and to the lower side type zero because that means no button was pressed, and all the other buttons have got higher number codes than that.


Figure 5 - Wait for the button presses

Step 2: Send data to 'TXT File' connection

Add the last block to the Snap program, which sends remote controller numbers in separate messages to the TXT file connection, which stores them in multiple files. You should use the 'To [connection], message: [text]' (Figure 6) block for this purpose. This block contains the connection you would like to send the message to. In this case choose your 'TXT File' [connection] you have added in Task #1. In the [text] part you usually have to type a text, but now you will drag the 'Remote button' block to it, which is the number arriving from the IR beacon and detected by the IR sensor.


Figure 6 - Data sent to the 'TXT File' connection

Step 3: Check the results

When you have finished collecting data and stopped the program, you will be able to see all stored data in the connection folder you have configured in Task #1. As you can see, the messages are separately stored in different files. If you open one of these with a simple notepad, you can see three lines in the file (Figure 7). The first line shows the originator's address, the second line shows the recipient's address and in the last line you can see the message itself. To figure out the timestamp, you can always check when the file was created. Now you also know when the remote was pressed.


Figure 7 - The incoming message opened with the notepad

Program code


Figure 8 - The program forwards data from the sensor to the text connection


More information


Next


Copyright © 2000- |Ozeki Ltd | info@ozekirobot.com |
Page: 6147 | 79.99.42.43 | Login
Thank you for visiting this page