-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_data.py
More file actions
54 lines (43 loc) · 1.58 KB
/
Copy pathplot_data.py
File metadata and controls
54 lines (43 loc) · 1.58 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
import serial # pip install pyserial
import csv
import numpy as np
import pandas as pd
from datetime import datetime
import time
import matplotlib.pyplot as plt
ser = serial.Serial('/dev/ttyACM0', 115200)
timestamp = datetime.now().strftime("%m-%d-%H-%M-%S")
filename = f"data/data_log_{timestamp}.csv"
headers = ["SETPOINT_RPM", "SETPOINT_POS", "MEASUREMENT_RPM", "MEASUREMENT_POS"]
file_1 = open(filename, mode='w', newline='')
writer_1 = csv.writer(file_1, delimiter=';')
writer_1.writerow(headers)
try:
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').strip()
data_fields = line.split(';')
if len(data_fields) == len(headers):
writer_1.writerow(data_fields)
except KeyboardInterrupt:
file_1.close()
df1 = pd.read_csv(filename, sep=';')
timestamp = datetime.now().strftime("%m-%d-%H-%M-%S")
filename = f"plot/plot_{timestamp}.png"
_, axs = plt.subplots(2, 1, figsize=(8, 8))
Kp_speed = 0.6
Ki_speed = 1.0
Kp_pos = 0.4
Ki_pos = 0.0
plt.sca(axs[0])
ax = df1['MEASUREMENT_POS'].plot()
df1['SETPOINT_POS'].plot(ax=ax, linestyle = '--')
plt.title(f'Position Kp={Kp_pos}, Ki={Ki_pos}')
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.sca(axs[1])
plt.title(f'Velocity Kp={Kp_speed}, Ki={Ki_speed}')
ax = df1['MEASUREMENT_RPM'].plot()
df1['SETPOINT_RPM'].plot(ax=ax, linestyle = '--')
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.savefig(filename)
print('Save csv and png!')