Skip to content

Instantly share code, notes, and snippets.

@PyroAVR
Last active December 4, 2021 19:14
Show Gist options
  • Save PyroAVR/b774d8a1b4284ac7f0d64f74dd1d7735 to your computer and use it in GitHub Desktop.
Save PyroAVR/b774d8a1b4284ac7f0d64f74dd1d7735 to your computer and use it in GitHub Desktop.
bogus plotting script for IDE / Freescale cup car (replaces matlab script)
import numpy as np
import matplotlib.pyplot as plt
import serial
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(3, 1, 1)
ax2 = fig.add_subplot(3, 1, 2)
ax3 = fig.add_subplot(3, 1, 3)
ax1.set_title("Raw Output")
ax2.set_title("5-Value Average Filter Output")
ax3.set_title("Binary Edge-Detector Output")
ax1.set_ylim([0, 65535])
ax2.set_ylim([0, 65535])
ax3.set_ylim([0, 2])
port = serial.Serial('/dev/ttyACM0', 9600)
scanline = [0]*128
avg_filter_scanlines = np.zeros(shape=(128, 5))
index = 0
raw, = ax1.plot(scanline)
average, = ax2.plot(scanline)
edge_detect, = ax3.plot(np.zeros(128))
idx = 0
def update_average(scan_array, idx):
# avg_scanline = np.average(scan_array, axis=1)
# np.roll(scan_array, idx, 1)
n = 2
h_n = [1/n]*n
avg_scanline = np.convolve(scan_array[:, 0], h_n)
return avg_scanline[0:128]
def update_edge_detect(average_scanline):
output = np.abs(np.convolve(np.convolve(average_scanline, [-1, 0, 1]), [-1, 3, -1]))
return output[0:128]/max(output)
# THRESH = 4500
# output = np.zeros(average_scanline.shape)
# for i in range(0, average_scanline.shape[0]):
# output[i] = 1 if average_scanline[i] > THRESH else 0
# return output
def update_plots(scan_array, raw, average):
raw.set_ydata(scan_array[:, 0])
global idx
# idx = (idx + 1) % avg_filter_scanlines.shape[1]
avg = update_average(scan_array, idx)
average.set_ydata(avg)
edge_detect.set_ydata(update_edge_detect(avg))
fig.canvas.draw()
fig.canvas.flush_events()
port.reset_input_buffer()
port.flushInput()
while(True):
a = port.readline().decode('utf-8').strip()
try:
a = int(a)
except ValueError:
print('conversion error')
continue
if a == -1:
print(avg_filter_scanlines[:, 0])
update_plots(avg_filter_scanlines, raw, average)
index = 0
elif index < 128:
avg_filter_scanlines[index, 0] = a if a > 0 else 0
index += 1
else:
print('extra data')
port.flushInput()
# plt.pause(0.000000000001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment