-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectAngleData.py
More file actions
133 lines (109 loc) · 4.3 KB
/
Copy pathCollectAngleData.py
File metadata and controls
133 lines (109 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#https://stackoverflow.com/questions/42339876/error-unicodedecodeerror-utf-8-codec-cant-decode-byte-0xff-in-position-0-in
#https://www.youtube.com/watch?v=VN3HJm3spRE
#https://www.geeksforgeeks.org/writing-csv-files-in-python/
import serial
import time
import csv
import chardet
import os
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
arduino = serial.Serial(port='COM4', baudrate=115200, timeout=None)
time.sleep(1)
data = []
print("After this message, you will be prompted to type 'start' to start collecting Dynamometer data. After that, you have 10 seconds to get the dynamometer to full speed before data stops being collected.")
input("Type 'start' to begin collecting data: ")
start_time = time.time()
while True:
#while there no data at the serial port, don't try to read data(as readline is a hanging operation)
while(arduino.in_waiting == 0):
pass
data_pt = arduino.readline()
try:
encoding = chardet.detect(data_pt)['encoding']
decoded_data = data_pt.decode(encoding)
decoded_data = decoded_data.rstrip("\n")
decoded_data = decoded_data.split(',')
except:
pass
try:
decoded_data = [float(number) for number in decoded_data]
print(decoded_data)
data.append(decoded_data)
except:
pass
if(time.time()-start_time >=20):
break
fields = ["timestamp", "total_angle"]
# Define the base name for the CSV files
base_filename = 'run'
file_extension = '.csv'
# Function to find the next available file name
def get_next_filename():
i = 0
while True:
filename = f"{base_filename}{i}{file_extension}"
if not os.path.exists(filename):
return filename
i += 1
# Get the next available CSV filename
csv_file = get_next_filename()
# Create a new CSV file and write to it
with open(csv_file, mode='w', newline='') as file:
writer = csv.writer(file)
# Write headers (optional)
writer.writerow(fields)
writer.writerows(data)
# df = pd.read_csv(csv_file)
# df = df[df >= 0].dropna() #https://www.geeksforgeeks.org/how-to-drop-negative-values-in-pandas-dataframe/
# df.head()
# degree = 5
# coeffs = np.polyfit(df['Timestamp'], df['RPM'], degree)
# polynomial = np.poly1d(coeffs)
# polynomial_derivative = np.polyder(polynomial)
# # # # Step 4: Generate smooth values for plotting the polynomial and its derivative
# x_plotting_vals = np.linspace(df["Timestamp"].iloc[0], df["Timestamp"].iloc[-1], 100)
# y_plotting_vals = polynomial(x_plotting_vals)
# y_plotting_vals_deriv = polynomial_derivative(x_plotting_vals)
# # # # Step 5: Plot the data, polynomial fit, and its derivative
# plt.figure(figsize=(10, 6))
# plt.scatter(df['RPM'], df['Timestamp'], color='red', label='Data points', zorder=5) # Plot the data points
# plt.plot(x_plotting_vals, y_plotting_vals, label=f'Polynomial Fit (degree {degree})', color='blue') # Polynomial fit
# plt.plot(x_plotting_vals, y_plotting_vals_deriv, label='Derivative of Polynomial', color='green', linestyle='--') # Derivative
# plt.legend()
# plt.xlabel('x')
# plt.ylabel('y')
# plt.title(f'Polynomial Fit and its Derivative (degree {degree})')
# plt.grid(True)
# plt.show()
# # # Step 6: Print out the polynomial equation and its derivative
# # equation = f"Polynomial Equation (degree {degree}):\n"
# # equation += "y = "
# # terms = []
# # for i, coeff in enumerate(coeffs):
# # power = degree - i
# # if power == 0:
# # terms.append(f"{coeff:.4f}")
# # elif power == 1:
# # terms.append(f"{coeff:.4f}x")
# # else:
# # terms.append(f"{coeff:.4f}x^{power}")
# # equation += " + ".join(terms)
# # # Print the polynomial equation
# # print(equation)
# # # Print the derivative of the polynomial
# # derivative_terms = []
# # for i, coeff in enumerate(polynomial_derivative.coefficients):
# # power = len(polynomial_derivative.coefficients) - i - 1
# # if power == 0:
# # derivative_terms.append(f"{coeff:.4f}")
# # elif power == 1:
# # derivative_terms.append(f"{coeff:.4f}x")
# # else:
# # derivative_terms.append(f"{coeff:.4f}x^{power}")
# # derivative_equation = "Derivative of Polynomial:\n"
# # derivative_equation += "dy/dx = "
# # derivative_equation += " + ".join(derivative_terms)
# # print(derivative_equation)