Introduction
In this article, we will discuss how to create a data logging project that instructs the VEX Robot to collect data with the VEX Brain and save it in a CSV file on the SD card for data analysis.
Data logging is a powerful tool that we can use to assist scientific research, conduct our own experiments, and solve specific problems. There are various fun and exciting tasks that we can drive the VEX Robot to accomplish with data logging. The following are some examples:
- Gathering data with the Distance Sensor to calculate the speed of the robot
- Driving the robot around the classroom and collecting data with the Optical Sensor to observe light changes in various locations.
- Taking the robot on an adventure and recording GPS coordinates data to create a map.
Coding skills needed for developing a Data Logging Project
- Use various commands to collect data with VEX Brain/Sensors and add it to a data structure.
- Write the data to a Comma-Separated Values (CSV) file on the SD card.
Hardware Required for Data Logging:
- A VEX Brain (IQ, V5, EXP)
- A SD card
- A Sensor or multiple Sensors (Optional, according to the required data)
Getting Data From The Sensor
We can program the VEX Robot (IQ, V5, EXP) to capture the data from the following sensors:
- Inertial Sensor
- Distance Sensor
- Optical Sensor
- Vision Sensor
- Rotation Sensor (V5, EXP)
- GPS Sensor (V5)
Note: the VEX IQ (2nd generation) Brain and the VEX EXP Brain have an Inertial Sensor built in. We can collect the Inertial Sensor data with these VEX Brians.
In this article, we will develop a data logging project that collects the acceleration data with the VEX IQ Brain (2nd) and stores it into an SD card.
First, we need to write a short program to get readings from the Timer and the Inertial Sensor built in the VEX IQ Brain (2nd).
- At the beginning of this program, we will need to import the Python modules to control VEX IQ Brain (2nd) and fetch the data from it. Thus, start by adding these instructions to the project.
- Next, add the code to declare a variable
numOfDataEntries
to store the number of data entries for recording, declare a variablepolling_delay_msec
to store the value of time interval for reading data, and create an empty string variabledata_buffer
to store the data read from the sensor. - As we will use the current value of the Timer to add the Timestamp to the data, we need to reset the timer value back to 0 seconds before starting collecting data.
- Before using the instruction that fetches the data from the VEX IQ Brain (2nd) and records the data in the buffer, we need to understand it first.
The following instruction appends the current timer value to thedata_buffer
variable in a specific format.Let’s break down the format string
“%1.3f”
:- “%”: Introduces the conversion specifier.
- “1”: Indicates the minimum number of digits to be present in the string
- “.3”: Specifies the number of decimal places to include in the formatted number.
- “f”: Indicates that the value to be formatted is a floating-point number.
Thus, we can use the format string “%1.3f” to format a floating-point number with a minimum width of 1 digit and a precision of 3 decimal places.
- Now, we can read the Timer and the acceleration value of the Inertial Sensor built in the VEX IQ Brain (2nd) and then append the data to the
data_buffer
variable in the specified format.Note: “\n” is the New Line character. It indicates that the line ends here and the beginning of a new line.
- Then, to record a certain number of data entries into the
data_buffer
variable, we can use afor loop
to repeatedly execute the instructions that fetch the data and append the data to thedata_buffer
variable for a number of iterations. Our strategy is to use the value of the variablenumOfDataEntries
to determine the number of iterations.
- So far, our program records data as quickly as it possibly can. However, we want to record data at specific time intervals. Therefore, we can use a
wait()
function in thefor loop
to pause the program to add the specific time interval between two readings. We use the value of the variablepolling_delay_msec
to determine the value of the time interval.
Congratulations! We've finished the program to get readings (the acceleration data) from the Inertial Sensor built in the VEX Brain. Next, we will explore how to write the data to a CSV file on the SD card.
Attaching the SD Card to the VEX Brain
Before writing the data to a file on an SD card, insert the SD card into the SD card slot of the VEX Brain first.
Note: Format the SD card to FAT32 for data writing. We recommend using SD cards that are 32GB or less in size.
To ensure that writing data to a file on the SD card can be performed under appropriate conditions, we will need to add code to check if the SD card is properly inserted into the VEX IQ Brain (2nd).
- Use the
brain.sdcard.is_inserted()
function to check if the SD card is inserted. If the SD card is not inserted, display the corresponding message on the VEX IQ Brain Screen and hold the program.
Note: thebrain.sdcard.is_inserted()
function returns True if an SD card is inserted into the Brain.
Writing the Data to a CSV file on the SD Card
So far, our program can collect data with the VEX IQ Brain (2nd). To complete the data logging project, we need to write the data to a Comma-Separated Values (CSV) file on the SD card for future examination and analysis.
- When writing different types of data to a CSV file, we want to know which type of data each column contains. To do this, we can append the CSV header text to the
data_buffer
variable before recording data.
Thus, add the code to declare a variablecsvHeaderText
to store the CSV header text to specify the column names for the CSV file and declare a variablesd_file_name
to hold the name of the CSV file to write on the SD card.
Note: Make sure the headers are in the same order as the data stored in the data_buffer
variable.
- Next, append the CSV header text to the
data_buffer
string before thefor loop
for data collection.
- Before writing data to a file on the SD card, let’s get to understand how to use the
brain.sdcard.savefile()
function first.
This instruction writes the data stored in thedata_buffer
variable to a named CSV file on the SD card.
Let’s break it down:-
brain.sdcard.savefile(): The function saves a bytearray into a named file on the SD card. The function returns the number of bytes written to the file.
-
sd_file_name: The first parameter of the function. Indicates the name of the file to write. In this project, the file name is stored in the variable
sd_file_name.
-
bytearray(datat_buffer,’utf-8’): The second parameter of the function. Represents the bytearray to be written into the file.
-
- bytearray(): The method creates a mutable bytearray. In this instruction, we use it to convert a string to a bytearray by specifying the encoding.
- data_buffer: The first parameter of the method. Indicates the source to be converted to a bytearray. In this project, the source is the data stored in the variable
data_buffer.
- ‘utf-8’: The second parameter of the method. Indicates the specified encoding used to encode the string. The encoding is ‘utf-8’ in this instruction.
-
-
- After the
for loop
for data collection, use thebrain.sdcard.savefile()
function to write the data stored in thedata_buffer
variable to the CSV file on the SD card. In addition, add the code to check the return value from thebrain.sdcard.savefile()
function to verify if the data has been written to the file successfully and display the corresponding message on the VEX IQ Brain Screen to get real-time feedback.
Note: Thebrain.sdcard.savefile()
function returns the number of bytes written to the file. In this project, we use it to write the collected data to a CSV file, so the number of bytes written to the file must be greater than zero. In other words, if thebrain.sdcard.savefile()
function returns 0, we can conclude that the data hasn't been written to the file successfully. Otherwise, the data has been written in the file.
Congratulations! We've developed a data logging project that collects the acceleration data with the VEX IQ Brain (2nd) and stores it into a CSV file on the SD card. Next, we will explore how to open the CSV file for data analysis.
Opening the CSV File for Data Analysis
Once the data has been written to the CSV file on the SD card, we can use a spreadsheet application to open the file for reading and analyzing the data.
Note: Two of the most common spreadsheet applications are Google Sheets and Microsoft Excel. In this article, we will use Google Sheets (Web Based) to open the CSV file on the SD card. The process of using other applications is similar.
- Remove the SD card from the VEX IQ Brain’s SD card slot. If the computer has a built-in Micro SD card slot, we can insert the SD card directly into that slot. Otherwise, insert the SD card into an SD card adapter and then connect the adapter to the computer.
- Log in to our Google account and open Google Sheets. Create a new spreadsheet.
- In the spreadsheet, open the “File” menu, select “Import” -> “Upload” -> “Browse” button, then choose the CSV file on the computer. After uploading the CSV file, click the “Import Data” button. After Importing data, click “Open now” to see the collected data in the CSV file.
- (Optional) One easy way to analyze the data is to draw a graph, and then look for the trends in the data. In the spreadsheet, open the “Insert” menu and select “Chart” to draw a graph using the data in the CSV file. The following graph is the result of the acceleration data collected with the VEX IQ Brain (2nd).
Note: We can use the Chart editor to select a different Chart Type or edit the chart based on our needs.
Up to this point, we've finished the data logging project that collects data with the VEX IQ Brain (2nd) and saves it in a CSV file on the SD card. Also, we have learned how to read the CSV file on the SD card using Google Sheets and even create a graph for further analysis. What next? Let’s try to plan more exciting experiments using the VEX Robot for data logging, to observe how the changing environmental factors affect various types of data and get a deeper understanding of the world around us.