Analyzing raw analog signal from HC-SR04 triggered by GEX

main.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import time
  2. from PyQt4 import QtCore, QtGui, Qt
  3. from PyQt4.QtGui import QApplication, QMainWindow
  4. import sys, math
  5. import gex
  6. class MyMainScreen(QMainWindow):
  7. def __init__(self, client=None, parent=None):
  8. QtGui.QMainWindow.__init__(self, parent)
  9. self.resize(800, 600)
  10. self.samples = None
  11. self.timer = QtCore.QTimer(self)
  12. self.timer.setInterval(20)
  13. self.timer.timeout.connect(self.measure)
  14. self.timer.start()
  15. self.timer2 = QtCore.QTimer(self)
  16. self.timer2.setInterval(30)
  17. self.timer2.timeout.connect(self.updatedisp)
  18. self.timer2.start()
  19. self.pending = False
  20. # init GEX
  21. self.client = client
  22. self.trig = gex.DOut(client, 'trig')
  23. self.adc = gex.ADC(client, 'adc')
  24. self.adc.set_sample_rate(80000)
  25. self.failcount = 0
  26. self.skipcount = 0
  27. self.wantredraw = False
  28. # self.fcap = gex.FCAP(client, 'fcap')
  29. def closeEvent(self,event):
  30. self.timer.stop()
  31. self.client.close()
  32. self.close()
  33. def measure(self):
  34. if self.pending:
  35. self.failcount += 1
  36. if self.failcount > 2:
  37. self.pending = False
  38. self.adc.disarm()
  39. self.skipcount = 0
  40. return
  41. if self.skipcount < 5:
  42. self.skipcount += 1
  43. return
  44. else:
  45. self.skipcount = 0
  46. self.pending = True
  47. self.failcount = 0
  48. self.adc.setup_trigger(1, level=1000, edge='rising', count=1366, handler=self.handle_meas)
  49. self.adc.arm()
  50. self.trig.pulse_us(10)
  51. def handle_meas(self, report):
  52. self.samples = report.data[50:]
  53. self.pending = False
  54. self.wantredraw = True
  55. def updatedisp(self):
  56. if self.wantredraw:
  57. self.repaint()
  58. self.wantredraw = False
  59. def paintEvent(self, event):
  60. if self.samples is None:
  61. return
  62. W = self.width()
  63. H = self.height()
  64. painter = QtGui.QPainter(self)
  65. painter.setRenderHint(QtGui.QPainter.Antialiasing)
  66. painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
  67. painter.fillRect(0,0, W,H, QtCore.Qt.black)
  68. painter.setPen(QtGui.QPen(QtCore.Qt.green, 1))
  69. step = round(W / len(self.samples))
  70. if step == 0:
  71. step = 1
  72. mult = 0.12*H/500
  73. n = 0
  74. for i in range(0, W, step):
  75. if n == len(self.samples)-1:
  76. break
  77. v = self.samples[n][0]
  78. w = self.samples[n+1][0]
  79. if v <= 0: v = 0.000000001
  80. if w <= 0: w = 0.000000001
  81. y0 = H - v*mult
  82. y1 = H - w*mult
  83. n += 1
  84. painter.drawLine(QtCore.QLine(i, round(y0), i+step, round(y1)))
  85. font = painter.font()
  86. font.setPointSize(22)
  87. font.setWeight(QtGui.QFont.DemiBold)
  88. painter.setFont(font)
  89. # label - Raw HC-SR04 signal
  90. painter.drawText(W-370, 50, "Surový signál přijímače")
  91. painter.drawText(W-370, 85, "HC-SR04")
  92. with gex.Client(gex.TrxSerialThread()) as client:
  93. app = QApplication(sys.argv)
  94. mainscreen = MyMainScreen(client)
  95. mainscreen.showFullScreen()
  96. app.exec_()