Data Logging with VEX Brain/Sensors (Python Version)

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.

    Screenshot of Python tutorial interface for VEX IQ Robotics platform, showcasing coding examples and resources for educators and students to enhance their understanding of robotics programming.

  • Next, add the code to declare a variable numOfDataEntries to store the number of data entries for recording, declare a variable polling_delay_msec to store the value of time interval for reading data, and create an empty string variable data_buffer to store the data read from the sensor.

    Diagram illustrating how to store data read from a sensor in VEX IQ robotics programming, part of the Python Tutorials section in the VEX IQ knowledge base.

  • 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.

    Screenshot showing the VEX IQ Timer reset process in Python programming, illustrating how to set the timer value back to 0 seconds before data collection.

  • 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 the data_buffer variable in a specific format.

    Image illustrating the format for defining variables in Python, as part of the VEX IQ Robotics platform tutorials, aimed at educating beginners in programming and robotics concepts.

    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.

    Diagram illustrating the format for defining variables in Python, relevant to VEX IQ Robotics programming tutorials.

    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 a for loop to repeatedly execute the instructions that fetch the data and append the data to the data_buffer variable for a number of iterations. Our strategy is to use the value of the variable numOfDataEntries to determine the number of iterations.

    Flowchart illustrating the process for determining the number of iterations in a VEX IQ robotics programming task, as part of Python tutorials for educational robotics.

  • 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 the for loop to pause the program to add the specific time interval between two readings. We use the value of the variable polling_delay_msec to determine the value of the time interval.

    Diagram illustrating how to determine the value of a time interval in VEX IQ robotics programming, part of Python tutorials for educational purposes.

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.

Screenshot of the VEX IQ Python Tutorials section, showcasing various programming resources and guides for educators and students to learn robotics concepts using the VEX IQ platform.




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.

Screenshot of a Python tutorial for VEX IQ Robotics, showcasing code examples and explanations for programming robots using VEXcode IQ, aimed at educators and students in the VEX IQ community.

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.

    Screenshot of VEX IQ Brain screen displaying a message indicating that the SD card is not inserted, with a prompt to hold the program, illustrating a function in Python tutorials for VEX IQ robotics.


    Note: the brain.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 variable csvHeaderText to store the CSV header text to specify the column names for the CSV file and declare a variable sd_file_name to hold the name of the CSV file to write on the SD card.

    Screenshot of a Python tutorial for VEX IQ robotics, showcasing code examples and explanations for programming VEX IQ robots, aimed at educators and students to enhance their understanding of robotics concepts.

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 the for loop for data collection.

    Screenshot of Python tutorial interface for VEX IQ Robotics platform, showcasing coding examples and resources for beginners in robotics education.

  • Before writing data to a file on the SD card, let’s get to understand how to use the brain.sdcard.savefile() function first.

    Diagram illustrating the VEX IQ Robotics platform components and their connections, designed for educational purposes, featured in the Python Tutorials section of the VEX IQ knowledge base.


    This instruction writes the data stored in the data_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 the brain.sdcard.savefile() function to write the data stored in the data_buffer variable to the CSV file on the SD card. In addition, add the code to check the return value from the brain.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.

    Screenshot of Python programming tutorial for VEX IQ Robotics, showcasing code examples and educational resources to help beginners learn robotics concepts and enhance their programming skills.


    Note: The brain.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 the brain.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. 

Diagram illustrating the VEX IQ Robotics system components and their connections, used in Python tutorials for educational robotics. The image supports learners in understanding the setup and programming of VEX IQ kits.

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.

    Screenshot of a Python tutorial for VEX IQ Robotics, showcasing code examples and explanations to help beginners learn programming concepts related to building and controlling VEX IQ robots.

  • (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).

    Screenshot of a Python tutorial for VEX IQ Robotics, showcasing code examples and explanations to help beginners understand programming concepts within the VEX IQ platform.


    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.

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: