aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <franklin@rockbox.org>2019-11-08 14:10:49 -0500
committerFranklin Wei <franklin@rockbox.org>2019-11-08 14:10:49 -0500
commit2a3c741db02f19323272b11072fbf2e7cd033910 (patch)
tree3310a0a8120907b52d2adbe310ab08ccd71286b8
parent409ddec8c53a147a6c6e80e6d5a33f714f38b215 (diff)
downloadregentester-2a3c741db02f19323272b11072fbf2e7cd033910.zip
regentester-2a3c741db02f19323272b11072fbf2e7cd033910.tar.gz
regentester-2a3c741db02f19323272b11072fbf2e7cd033910.tar.bz2
regentester-2a3c741db02f19323272b11072fbf2e7cd033910.tar.xz
Fixup
-rwxr-xr-xtachometer.py41
1 files changed, 27 insertions, 14 deletions
diff --git a/tachometer.py b/tachometer.py
index ae0b11e..9ae5ab5 100755
--- a/tachometer.py
+++ b/tachometer.py
@@ -1,8 +1,8 @@
#!/usr/bin/env python
"""
-Simply display the contents of the webcam with optional mirroring using OpenCV
-via the new Pythonic cv2 interface. Press <esc> to quit.
+A video tachometer. Run as ./tachometer.py and select the target
+region on the wheel.
"""
import cv2
@@ -22,6 +22,19 @@ HEIGHT=180
# number of stripes on wheel
TICS_PER_REV = 6
+#desired RPM range
+RPM_LOW = 6
+RPM_HIGH = 180
+
+ZERO_RPM_THRES = 10
+
+def update_plot(ax, fig, x, y):
+ ax.lines[0].set_data( x, y ) # set plot data
+ ax.relim() # recompute the data limits
+ ax.set_ylim(ymin=0, ymax=max(y) * 1.5)
+ ax.autoscale_view() # automatic axis scaling
+ fig.canvas.flush_events() # update the plot and take care of window eve
+
def show_webcam(mirror=False):
cam = cv2.VideoCapture(CAMERA_IDX)
@@ -60,8 +73,6 @@ def getRPM(x_data, y_data):
rate = nsamples * supersample / delta_t
freq, fft = getFFT(eval_y, rate)
- RPM_LOW = 5
- RPM_HIGH = 180
# get low and high freq bins
lo = get_first_higher(freq, RPM_LOW / 60 * TICS_PER_REV)
@@ -98,7 +109,7 @@ def getRPM(x_data, y_data):
peakRPM = np.where(interpolated_freqs == np.amax(interpolated_freqs))
peakRPM=freqs[peakRPM[0]][0]
print(peakRPM)
- return peakRPM
+ return peakRPM if peakRPM >= ZERO_RPM_THRES else 0
def main():
cam = cv2.VideoCapture(CAMERA_IDX)
@@ -112,14 +123,16 @@ def main():
plt.ion()
fig = plt.figure()
ax = plt.subplot(1,1,1)
- ax.set_xlabel('RPM')
- ax.set_ylabel('Intensity')
+ ax.set_xlabel('time')
+ ax.set_ylabel('measured rpm')
x_data, y_data = [], []
+ rpm_times, rpm_data = [], []
ax.plot(x_data , y_data , 'ko-' , markersize = 10 ) # add an empty line to the plot
fig.show() # show the window (figure will be in foreground, but the user may move it to background)
while True:
+ # sample.
ret_val, img = cam.read()
cv2.imshow('my webcam', img)
extracted_region = img[y:y+h, x:x+w]
@@ -135,14 +148,14 @@ def main():
x_data.pop(0)
y_data.pop(0)
-# ax.lines[0].set_data( x_data,y_data ) # set plot data
-# ax.relim() # recompute the data limits
-# ax.autoscale_view() # automatic axis scaling
-# fig.canvas.flush_events() # update the plot and take care of window events (like resizing etc.)
if len(x_data) >= WINDOW:
- print(getRPM(x_data, y_data))
-
- # print(avg)
+ rpm = getRPM(x_data, y_data)
+ rpm_times.append(t)
+ rpm_data.append(rpm)
+ if(len(rpm_times) > WINDOW):
+ rpm_times.pop(0)
+ rpm_data.pop(0)
+ update_plot(ax, fig, rpm_times, rpm_data)
if cv2.waitKey(1) == 27:
break # esc to quit