Skip to content

Feature/normalize data #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void pvGyroDataReading()
for (;;)
{
MPU_Read_Gyro(GyroDataArr); // Reading Gyroscoped Data
vTaskDelay(pdMS_TO_TICKS(500)); // delay for 500ms
vTaskDelay(pdMS_TO_TICKS(10)); // delay for 500ms
}
}

Expand All @@ -122,21 +122,22 @@ void pvAccelDataReading()
for (;;)
{
MPU_Read_Accel(AccelDataArr); // Reading Accelerometer Data
vTaskDelay(pdMS_TO_TICKS(500)); // delay for 500ms
vTaskDelay(pdMS_TO_TICKS(10)); // delay for 500ms
}
}

void pvDataLogging()
{
for (;;)
{
printf("Ax: %.4f\n\rYy: %.4f\n\rAz: %.4f\n\r", AccelDataArr[0], AccelDataArr[1], AccelDataArr[2]); // Log Accelerometer Data
printf("Gx: %.4f\n\rGy: %.4f\n\rGz: %.4f\n\n\r", GyroDataArr[0], GyroDataArr[1], GyroDataArr[2]); // Log Gyroscope Data
vTaskDelay(pdMS_TO_TICKS(500)); // Delay for 500ms
printf("%.4f %.4f %.4f %.4f %.4f %.4f\n",
AccelDataArr[0], AccelDataArr[1], AccelDataArr[2],
GyroDataArr[0], GyroDataArr[1], GyroDataArr[2]); // Log Accelerometer Data[Ax Ay Az, Gx Gy Gz]
vTaskDelay(pdMS_TO_TICKS(10)); // Delay for 500ms
}
}

int __io_putchar(int ch) //Overwriting for use printf via UART
int __io_putchar(int ch) // Overwriting for use printf via UART
{
UART_SendChar(USART2, ch);
return ch;
Expand Down
72 changes: 72 additions & 0 deletions data_normailzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import serial
import csv
import statistics

# Initialize containers to store data for calculations
data_storage = {
"Ax": [],
"Ay": [],
"Az": [],
"Gx": [],
"Gy": [],
"Gz": []
}

final_data = ['Circle ACW']

# Open/Create an output .csv file and add headers
with open('output.csv', 'a+', newline='') as csvfile:
output = csv.writer(csvfile)
# output.writerow(['Gesture', 'Ax', 'Ay', 'Az', 'Gx', 'Gy', 'Gz']) # Add appropriate headers

# Configure and open the serial port
try:
ser = serial.Serial('/dev/ttyACM0', baudrate=115200, timeout=1) # Added timeout for readline
print(f"Connected to: {ser.name}") # Print the name of the serial port

while True:
# Read a line of data from the serial port
data = ser.readline().decode('utf-8').strip() # Strip whitespace/newlines
if data: # Check if data is not empty
values = data.split(' ') # Split data assuming space-separated values

# Ensure the data has the correct number of elements before writing
if len(values) == 6: # Expecting Ax, Ay, Az, Gx, Gy, Gz
# Convert strings to floats for calculations
float_values = [float(v) for v in values]

# Write to CSV
# output.writerow(float_values)
print(float_values) # Optionally print to console for debugging

# Add data to storage for calculations
data_storage["Ax"].append(float_values[0])
data_storage["Ay"].append(float_values[1])
data_storage["Az"].append(float_values[2])
data_storage["Gx"].append(float_values[3])
data_storage["Gy"].append(float_values[4])
data_storage["Gz"].append(float_values[5])
else:
print(f"Invalid data received: {data}") # Debugging information for invalid data


except serial.SerialException as e:
print(f"Serial port error: {e}")

except KeyboardInterrupt:
print("\nExiting...")
# Calculate mean and SD when exiting
print("\nCalculating statistics...")
for key, values in data_storage.items():
if values: # Ensure there is data to calculate
mean = statistics.mean(values)
stdev = statistics.stdev(values) if len(values) > 1 else 0 # SD needs at least 2 data points
final_data.append(stdev/mean) # adding final calcualte data into the list so that we can write into csv
print(f"{key}: Mean = {mean:.4f}, SD = {stdev:.4f}")
else:
print(f"{key}: No data available.")
output.writerow(final_data) #Writing Data into CSV
print("Data is Written into .csv")

finally:
ser.close() # Close the serial port when done
Loading