PSoC6 Data Collection to CSV log file

1. Collect dataset using Eclipse ModusToolbox

1.1 Create Project through ModusToolbox

Create application project

1.2 Steps to Configure the Code:

  1. Navigate to the gesture.h File:

    • Locate the gesture.h file within the source folder of the project directory.

  2. Modify the Macro for Data Collection Mode:

    • Open the gesture.h file.

    • Find the line defining GESTURE_DATA_COLLECTION_MODE.

    • Change its value from 0u to 1u.

      #define GESTURE_DATA_COLLECTION_MODE 1u
    • This change enables printing sensor data to the terminal instead of running the inference engine.

  1. Navigate to the Makefile:

    • Locate the Makefile in the root directory of the project.

  2. Update the Shield:

    • Open the Makefile.

    • Look for the shield configuration line:

    SHIELD_DATA_COLLECTION = CY_028_SENSE_SHIELD_v1
    • Change this to:

    SHIELD_DATA_COLLECTION = CY_028_SENSE_SHIELD_v2
    • This updates the configuration to use the CY_028_SENSE_SHIELD_v2 for gathering IMU data.

1.3 Build and Launch the application

1.4 Let's collect dataset by Eclipse

  • Open modus-shell

  • Navigate to the folder and install the library.

pip install -r requirements.txt
install library: pip install -r requirements.txt
  • To collect data from the sensor, use the command line as below:

./collect.sh <COM PORT> <GESTURE NAME> <PERSON

ตัวอย่าง:

./collect.sh COM6 circle Chhun
Press 'c' to start collect data and 's' to stop and exit

2. Convert .txt dataset to .csv dataset

After data collection, the dataset is stored in the Machine_Learning_Gesture_Classification\train\gesture_data\<your gesture name> directory as a .txt file

2.1 Plot graph

  • Using below code for plot the graph for each sensor dataset:

import matplotlib.pyplot as plt

# Define the file name
filename = '<your .txt file location'

# Initialize lists to store data
gyro_x, gyro_y, gyro_z, accel_x, accel_y, accel_z = [], [], [], [], [], []

# Read data from the file
with open(filename, 'r') as file:
    for line in file:
        # Split the line by comma
        data = line.strip().split(',')
        # Check if the line has 6 values
        if len(data) == 6:
            # Check if all values in the line are not '-'
            if all(value != '-' for value in data):
                # Append data to respective lists
                gyro_x.append(float(data[0]))
                gyro_y.append(float(data[1]))
                gyro_z.append(float(data[2]))
                accel_x.append(float(data[3]))
                accel_y.append(float(data[4]))
                accel_z.append(float(data[5]))

# Create time values (assuming each line represents a time step)
time = list(range(len(accel_x)))

# Plot accelerometer and gyroscope data with a wider figure
plt.figure(figsize=(16, 8))  # Increase width for a longer x-axis

# Set a main title for the entire figure
plt.suptitle('Square Dataset', fontsize=20)

# Plot accelerometer data
plt.subplot(2, 1, 1)
plt.plot(time, accel_x, label='Accel X')
plt.plot(time, accel_y, label='Accel Y')
plt.plot(time, accel_z, label='Accel Z')
plt.title('Accelerometer Data', fontsize=16)  # Title for this subplot
plt.xlabel('Time', fontsize=12)
plt.ylabel('Acceleration', fontsize=12)
plt.legend()

# Plot gyroscope data
plt.subplot(2, 1, 2)
plt.plot(time, gyro_x, label='Gyro X')
plt.plot(time, gyro_y, label='Gyro Y')
plt.plot(time, gyro_z, label='Gyro Z')
plt.title('Gyroscope Data', fontsize=16)  # Title for this subplot
plt.xlabel('Time', fontsize=12)
plt.ylabel('Angular Velocity', fontsize=12)
plt.legend()

# Adjust layout to prevent overlap
plt.tight_layout(rect=[0, 0, 1, 0.95])  # Leave space for main title
plt.show()
  • Sample Graph Outputs from the Dataset

2.2 Covert to .csv file

Colab: https://colab.research.google.com/drive/1wdsgtEFlWhmXoo3XLQOzVhtO8u_F96Nt?usp=sharing

Open Colab and create your own copy before running the code:

This session in Colab is used to convert a .txt dataset to a .csv dataset.

Last updated

Was this helpful?