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