Compare commits

...

2 Commits

  1. 2
      gex
  2. 97
      main.py

2
gex

@ -1 +1 @@
Subproject commit 3a5ac05d9db965f86188929f8f56a68ee994827d Subproject commit 47e1d5b14a3b66a30914d203ce0f787e36c69e3c

@ -1,7 +1,9 @@
#!/usr/bin/env python
import time import time
from PyQt4 import QtCore, QtGui, Qt from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication, QMainWindow from PyQt4.QtGui import QApplication, QMainWindow
import sys, math import sys, math
import gex import gex
@ -10,15 +12,21 @@ class MyMainScreen(QMainWindow):
QtGui.QMainWindow.__init__(self, parent) QtGui.QMainWindow.__init__(self, parent)
self.resize(800, 600) self.resize(800, 600)
self.samples = None self.LINEWIDTH = 1
self.CAPLEN = 1400
self.CAPRATE = 115000 # must be > 80 for nyquist
self.samples_trig = None
self.samples_echo = None
self.timer = QtCore.QTimer(self) self.timer = QtCore.QTimer(self)
self.timer.setInterval(20) self.timer.setInterval(10)
self.timer.timeout.connect(self.measure) self.timer.timeout.connect(self.measure)
self.timer.start() self.timer.start()
#
self.timer2 = QtCore.QTimer(self) self.timer2 = QtCore.QTimer(self)
self.timer2.setInterval(30) self.timer2.setInterval(25)
self.timer2.timeout.connect(self.updatedisp) self.timer2.timeout.connect(self.updatedisp)
self.timer2.start() self.timer2.start()
@ -29,8 +37,6 @@ class MyMainScreen(QMainWindow):
self.trig = gex.DOut(client, 'trig') self.trig = gex.DOut(client, 'trig')
self.adc = gex.ADC(client, 'adc') self.adc = gex.ADC(client, 'adc')
self.adc.set_sample_rate(80000)
self.failcount = 0 self.failcount = 0
self.skipcount = 0 self.skipcount = 0
self.wantredraw = False self.wantredraw = False
@ -47,7 +53,7 @@ class MyMainScreen(QMainWindow):
self.failcount += 1 self.failcount += 1
if self.failcount > 2: if self.failcount > 2:
self.pending = False self.pending = False
self.adc.disarm() # self.adc.disarm()
self.skipcount = 0 self.skipcount = 0
return return
@ -61,15 +67,37 @@ class MyMainScreen(QMainWindow):
self.failcount = 0 self.failcount = 0
self.adc.setup_trigger(1, level=1000, edge='rising', count=1366, handler=self.handle_meas) #self.adc.setup_trigger(1, level=2000, edge='rising', count=1366, handler=self.handle_meas)
self.adc.arm() #self.adc.arm()
self.adc.set_sample_rate(self.CAPRATE)
self.adc.capture(self.CAPLEN, async=True, lst=self.handle_meas)
self.trig.pulse_us(10) self.trig.pulse_us(10)
def handle_meas(self, report): def handle_meas(self, rawsamp):
self.samples = report.data[50:] 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
self.pending = False self.pending = False
self.wantredraw = True
def updatedisp(self): def updatedisp(self):
if self.wantredraw: if self.wantredraw:
@ -77,7 +105,7 @@ class MyMainScreen(QMainWindow):
self.wantredraw = False self.wantredraw = False
def paintEvent(self, event): def paintEvent(self, event):
if self.samples is None: if self.samples_trig is None:
return return
W = self.width() W = self.width()
@ -87,24 +115,37 @@ class MyMainScreen(QMainWindow):
painter.setRenderHint(QtGui.QPainter.Antialiasing) painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing) painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
painter.fillRect(0,0, W,H, QtCore.Qt.black) painter.fillRect(0,0, W,H, QtCore.Qt.black)
painter.setPen(QtGui.QPen(QtCore.Qt.green, 1))
step = round(W / len(self.samples)) step = round(W / self.CAPLEN)
if step == 0: if step == 0:
step = 1 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
mult = 0.12*H/500 mult = 0.12*H/500
painter.setPen(QtGui.QPen(QtGui.QColor(color_tx), self.LINEWIDTH))
n = 0 n = 0
for i in range(0, W, step): for i in range(0, W, step):
if n == len(self.samples)-1: if n == len(self.samples_trig)-1:
break break
v = self.samples[n][0] y0 = H - self.samples_trig[n]*mult
w = self.samples[n+1][0] y1 = H - self.samples_trig[n + 1]*mult
if v <= 0: v = 0.000000001
if w <= 0: w = 0.000000001
y0 = H - v*mult
y1 = H - w*mult
n += 1 n += 1
painter.drawLine(QtCore.QLine(i, round(y0), i+step, round(y1))) painter.drawLine(QtCore.QLine(i, round(y0), i+step, round(y1)))
@ -114,11 +155,17 @@ class MyMainScreen(QMainWindow):
painter.setFont(font) painter.setFont(font)
# label - Raw HC-SR04 signal # label - Raw HC-SR04 signal
painter.drawText(W-370, 50, "Surový signál přijímače") painter.drawText(W-170, 50, "Vysílač")
painter.drawText(W-370, 85, "HC-SR04")
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")
with gex.Client(gex.TrxSerialThread()) as client: with gex.Client(gex.TrxSerialThread()) as client:
app = QApplication(sys.argv) app = QApplication(sys.argv)
mainscreen = MyMainScreen(client) mainscreen = MyMainScreen(client)
mainscreen.showFullScreen() mainscreen.showFullScreen()
#mainscreen.show()
app.exec_() app.exec_()

Loading…
Cancel
Save