forked from Stormholt/DWL5500XY-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclinometer_gui.py
142 lines (116 loc) · 4.25 KB
/
inclinometer_gui.py
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
134
135
136
137
138
139
140
141
142
import os
import csv
import datetime
import DWL5500XY
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button, RadioButtons
import matplotlib
import time
import sys
matplotlib.use("tkAgg")
# set the port name
portname = input("Enter the serial port name that the sensor is connected to (e.g. COM5):")
sc = DWL5500XY.Sensor(True)
sc.open_connection(portname)
sc.initialize_sensor()
sc.set_location_code(0x17, 0x0E)
sc.set_mode(sc.LOCATION_MODE)
sc.set_mode(sc.DUAL_MODE)
sc.reset_alternate_zero_dualaxis()
if sc.conn == None:
print("Could not connect to sensor. Ensure the Serial port is correct.")
exit(2)
plot_window = 600
# create buffer for plotting two values
x_var = np.zeros([plot_window])
y_var = np.zeros([plot_window])
g_var = np.zeros([plot_window])
plt.ion()
fig, ax = plt.subplots()
xline, = ax.step(list(range(plot_window)), x_var)
yline, = ax.step(list(range(plot_window)), y_var)
# y axis label in degrees
ax.set_ylabel('Angle [$^\circ$]')
ax.set_xlabel('Sample Number [$0.1 s$]')
ax.set_title("XY Inclination")
def reset_zero(event):
print("Setting alternate zero for DUAL_MODE\n")
sc.set_alternate_zero_dualaxis()
axnext = plt.axes([0.85, 0.9, 0.1, 0.05])
bnext = Button(axnext, 'Set Zero')
bnext.on_clicked(reset_zero)
def toggle_mode(e):
if e == "Inclination":
sc.set_mode(sc.DUAL_MODE)
ax.legend(["x [$^\circ$]", "y [$^\circ$]"])
else:
sc.set_mode(sc.VIBRO_MODE)
ax.legend(["vibration [$g$]"])
axtoggle = plt.axes([0.7, 0.9, 0.1, 0.08])
btggl = RadioButtons(axtoggle, ['Inclination', 'Vibration'])
btggl.on_clicked(toggle_mode)
toggle_mode("Inclination")
ax.grid(True, which='major', axis='y')
ax.grid(True, which='minor', axis='y', alpha=0.2)
fps = 0
fps_text = axnext.text(0, 1.1, "FPS:"+str(fps), fontsize=12)
def update_plot(x, y):
xline.set_ydata(x[-plot_window:])
yline.set_ydata(y[-plot_window:])
minimum_limit = 0.001 # the resolution of the sensor
value_max = max(np.amax(x[-plot_window:]),
np.amax(y[-plot_window:]))
value_min = min(np.amin(x[-plot_window:]),
np.amin(y[-plot_window:]))
ax.set_ylim([value_min-minimum_limit, value_max+minimum_limit])
fig.canvas.draw()
fig.canvas.flush_events()
now = datetime.datetime.now()
# Format the date and time as a string
timestamp = now.strftime("%Y%m%d_%H%M%S")
vibration_filename = "vibration_" + timestamp + ".csv"
inclination_filename = "inclination_" + timestamp + ".csv"
print("Saving data to " + vibration_filename + " and " + inclination_filename)
try:
csv_writer = csv.DictWriter(open(inclination_filename, 'w'), fieldnames=[
"x", "y", "time"], lineterminator="\n")
csv_writer.writeheader()
csv_writerv = csv.DictWriter(open(vibration_filename, 'w'), fieldnames=[
"vibration[g]", "time"], lineterminator="\n")
csv_writerv.writeheader()
except:
sys.exit("Could not open the given file.")
fps_timer = time.perf_counter()
while True:
try:
fps = 1/(time.perf_counter() - fps_timer)
fps_timer = time.perf_counter()
fps_text.set_text("FPS: %.1f" % fps)
angles = sc.read_response()
if type(angles) == dict and btggl.value_selected == "Inclination":
x_var = np.append(x_var, angles["x"])
y_var = np.append(y_var, angles["y"])
update_plot(x_var, y_var)
angles["time"] = time.time()
csv_writer.writerow(angles)
elif type(angles) == float and btggl.value_selected == "Vibration":
g_var = np.append(g_var, angles)
update_plot(g_var, g_var)
csv_writerv.writerow({"vibration[g]":angles, "time":time.time()})
else:
print("Invalid response from sensor")
fig.canvas.draw()
fig.canvas.flush_events()
except KeyboardInterrupt:
plt.savefig(inclination_filename.replace(".csv", ".png"))
matplotlib.use("pgf")
matplotlib.rcParams.update({
"pgf.texsystem": "pdflatex",
'font.family': 'serif',
'text.usetex': True,
'pgf.rcfonts': False,
})
plt.savefig(inclination_filename.replace(".csv", "")+".pgf")
plt.close("all")
exit(0)