Analyzing raw analog signal from HC-SR04 triggered by GEX
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
demo-hcsr04/main.py

172 lines
4.6 KiB

#!/usr/bin/env python
6 years ago
import time
from PyQt4 import QtCore, QtGui
6 years ago
from PyQt4.QtGui import QApplication, QMainWindow
6 years ago
import sys, math
6 years ago
import gex
6 years ago
class MyMainScreen(QMainWindow):
6 years ago
def __init__(self, client=None, parent=None):
6 years ago
QtGui.QMainWindow.__init__(self, parent)
6 years ago
self.resize(800, 600)
self.LINEWIDTH = 1
self.CAPLEN = 1400
self.CAPRATE = 115000 # must be > 80 for nyquist
self.samples_trig = None
self.samples_echo = None
6 years ago
self.timer = QtCore.QTimer(self)
self.timer.setInterval(10)
6 years ago
self.timer.timeout.connect(self.measure)
6 years ago
self.timer.start()
#
6 years ago
self.timer2 = QtCore.QTimer(self)
self.timer2.setInterval(25)
6 years ago
self.timer2.timeout.connect(self.updatedisp)
self.timer2.start()
self.pending = False
# init GEX
self.client = client
self.trig = gex.DOut(client, 'trig')
self.adc = gex.ADC(client, 'adc')
self.failcount = 0
self.skipcount = 0
self.wantredraw = False
# self.fcap = gex.FCAP(client, 'fcap')
def closeEvent(self,event):
self.timer.stop()
self.client.close()
self.close()
def measure(self):
if self.pending:
self.failcount += 1
if self.failcount > 2:
self.pending = False
# self.adc.disarm()
6 years ago
self.skipcount = 0
return
6 years ago
6 years ago
if self.skipcount < 5:
self.skipcount += 1
return
else:
self.skipcount = 0
6 years ago
6 years ago
self.pending = True
6 years ago
6 years ago
self.failcount = 0
6 years ago
#self.adc.setup_trigger(1, level=2000, edge='rising', count=1366, handler=self.handle_meas)
#self.adc.arm()
6 years ago
self.adc.set_sample_rate(self.CAPRATE)
self.adc.capture(self.CAPLEN, async=True, lst=self.handle_meas)
6 years ago
self.trig.pulse_us(10)
def handle_meas(self, rawsamp):
self.samples_echo = rawsamp[:,0]
self.samples_trig = rawsamp[:,1]
# crop
cropped = False
for i in range(20, 150):
if self.samples_echo[i] > 2000:
# fail
break
if self.samples_trig[i] > 2500:
self.samples_echo = self.samples_echo[i-20:]
self.samples_trig = self.samples_trig[i-20:]
cropped = True
# crop
break
if not cropped:
pass
else:
self.wantredraw = True
6 years ago
self.pending = False
def updatedisp(self):
if self.wantredraw:
self.repaint()
self.wantredraw = False
6 years ago
def paintEvent(self, event):
if self.samples_trig is None:
6 years ago
return
W = self.width()
H = self.height()
6 years ago
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
6 years ago
painter.fillRect(0,0, W,H, QtCore.Qt.black)
step = round(W / self.CAPLEN)
6 years ago
if step == 0:
step = 1
color_rx = 0xFFFF00
color_tx = 0xFF00FF
# Rx
mult = 0.17*H/500
painter.setPen(QtGui.QPen(QtGui.QColor(color_rx), self.LINEWIDTH))
n = 0
for i in range(0, W, step):
if n == len(self.samples_echo) - 1:
break
y0 = H - self.samples_echo[n] * mult
y1 = H - self.samples_echo[n + 1] * mult
n += 1
painter.drawLine(QtCore.QLine(i, round(y0), i + step, round(y1)))
# Tx
6 years ago
mult = 0.12*H/500
painter.setPen(QtGui.QPen(QtGui.QColor(color_tx), self.LINEWIDTH))
6 years ago
n = 0
for i in range(0, W, step):
if n == len(self.samples_trig)-1:
6 years ago
break
y0 = H - self.samples_trig[n]*mult
y1 = H - self.samples_trig[n + 1]*mult
6 years ago
n += 1
painter.drawLine(QtCore.QLine(i, round(y0), i+step, round(y1)))
font = painter.font()
font.setPointSize(22)
font.setWeight(QtGui.QFont.DemiBold)
painter.setFont(font)
6 years ago
6 years ago
# label - Raw HC-SR04 signal
painter.drawText(W-170, 50, "Vysílač")
painter.setPen(QtGui.QPen(QtGui.QColor(color_rx), 2))
painter.drawText(W-170, 85, "Přijímač")
painter.setPen(QtGui.QPen(QtGui.QColor(0xFFFFFF), 2))
painter.drawText(round(W/2)-220, 50, "Ultrazvukový dálkoměr HC-SR04")
6 years ago
6 years ago
with gex.Client(gex.TrxSerialThread()) as client:
6 years ago
app = QApplication(sys.argv)
6 years ago
mainscreen = MyMainScreen(client)
mainscreen.showFullScreen()
#mainscreen.show()
6 years ago
app.exec_()