# PSoC6 Data Collection to CSV log file

## 1. Collect dataset using Eclipse ModusToolbox

1.1 Create Project through ModusToolbox

<figure><img src="/files/VHWkqS6bodzwp6r1F0yk" alt=""><figcaption><p>Create application project</p></figcaption></figure>

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

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

<figure><img src="/files/H0bu6leZvYoShvVXUwMC" alt=""><figcaption></figcaption></figure>

3. **Navigate to the `Makefile`:**
   * Locate the `Makefile` in the root directory of the project.
4. **Update the Shield:**

   * Open the `Makefile`.
   * Look for the shield configuration line:&#x20;

   <pre><code><strong>SHIELD_DATA_COLLECTION = CY_028_SENSE_SHIELD_v1
   </strong></code></pre>

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

<figure><img src="/files/8cpbkFOGrGaiSYgZd5Uw" alt=""><figcaption></figcaption></figure>

1.3 Build and Launch the application

1.4 Let's collect dataset by Eclipse

* Open modus-shell

<figure><img src="/files/dC1RizejFeVv4GFIEJSh" alt=""><figcaption></figcaption></figure>

* Navigate to the folder and install the library.

```cfscript
pip install -r requirements.txt
```

<figure><img src="/files/UlfHreL63hWxiO6015cU" alt=""><figcaption><p>install library: pip install -r requirements.txt</p></figcaption></figure>

* To collect data from the sensor, use the command line as below:

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

ตัวอย่าง:

```
./collect.sh COM6 circle Chhun
```

<figure><img src="/files/XffK8GEp6J2YwNhhmv2y" alt=""><figcaption><p>Press 'c' to start collect data and 's' to stop and exit</p></figcaption></figure>

## 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

* Create project in google colab: <https://colab.research.google.com/drive/1zkqpE8NGNUJv6uIUV-4ItKIbkxr1n4qj?usp=sharing>
* Install library

  ```notebook-python
  !pip install tsaug
  ```

<figure><img src="/files/l0em4sr0sWGiccNL3lf6" alt=""><figcaption></figcaption></figure>

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

```python
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

<figure><img src="/files/378V2nuBZ3Mv5QBypFgb" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Pwr3vZsJ1kKWQ8bOXd8j" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/UoMSHKcmx1atI4IwQMls" alt=""><figcaption></figcaption></figure>

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:

<figure><img src="/files/9riOyfNxhgLNpFxmH6Nf" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/g8YTugZgricfaXZloTfq" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/iot-development-with-infineon-psoc-tm-6-and-bdh-platform/bdh-x-brain-data-analytics/psoc6-data-collection-to-csv-log-file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
