Mercurial > ~darius > hgwebdir.cgi > pyinst
comparison logpps.py @ 62:ffc9292eb00b
Use edge detection to log time differences between edges
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Fri, 08 Jan 2021 14:06:52 +1030 |
parents | |
children | 7386f2888508 |
comparison
equal
deleted
inserted
replaced
61:b6dc43e6f6f0 | 62:ffc9292eb00b |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 # Copyright (c) 2021 | |
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved. | |
5 # | |
6 # Redistribution and use in source and binary forms, with or without | |
7 # modification, are permitted provided that the following conditions | |
8 # are met: | |
9 # 1. Redistributions of source code must retain the above copyright | |
10 # notice, this list of conditions and the following disclaimer. | |
11 # 2. Redistributions in binary form must reproduce the above copyright | |
12 # notice, this list of conditions and the following disclaimer in the | |
13 # documentation and/or other materials provided with the distribution. | |
14 # | |
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE | |
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
25 # SUCH DAMAGE. | |
26 # | |
27 # Expected DB schema | |
28 # CREATE TABLE ppslog ( | |
29 # name TEXT, | |
30 # time TIMESTAMP WITH TIME ZONE, | |
31 # delta NUMERIC(15, 12) | |
32 # ); | |
33 | |
34 import datetime | |
35 import matplotlib.pylab as pylab | |
36 import numpy | |
37 import psycopg2 | |
38 #import sqlite3 | |
39 import sys | |
40 import time | |
41 # Should try this code instead: https://github.com/python-ivi/python-usbtmc | |
42 import usb488 | |
43 | |
44 def main(): | |
45 u = usb488.USB488Device() | |
46 print('Found device') | |
47 | |
48 # See "TDS2000 Programmer.pdf" | |
49 res = u.ask('*IDN?') | |
50 print('IDN reports ' + res) | |
51 | |
52 #dbh = sqlite3.connect('logpps.db') | |
53 dbh = psycopg2.connect('host=vm11 user=ppslog dbname=ppslog') | |
54 test(u, dbh, 'ncu-iono2-tx') | |
55 | |
56 def test(u, dbh = None, name = None): | |
57 if dbh != None: | |
58 cur = dbh.cursor() | |
59 | |
60 u.write('ACQ:MODE SAMPLE') | |
61 u.write('ACQ:STATE STOP') | |
62 | |
63 #u.write('DATA:ENC RIB') # Big endian signed | |
64 #u.write('DATA:WIDTH 2') # 2 bytes wide | |
65 | |
66 vscale1 = float(u.ask('CH1:SCALE?').split()[1]) | |
67 print(('Channel 1 scale is %.2f volts/div' % (vscale1))) | |
68 | |
69 vscale2 = float(u.ask('CH2:SCALE?').split()[1]) | |
70 print(('Channel 2 scale is %.2f volts/div' % (vscale2))) | |
71 | |
72 hscale = float(u.ask('HOR:MAIN:SCALE?').split()[1]) | |
73 print(('Horizontal scale is %.5f nsec/div' % (hscale * 1e9))) | |
74 # TEK2024B doesn't grok HOR:DIV? so hard code 10 (has 8 vertically) | |
75 acqwindow = hscale * 10.0 | |
76 | |
77 while True: | |
78 ary1, ary2 = acquire(u, vscale1, vscale2) | |
79 #pylab.plot(ary1) | |
80 #pylab.plot(ary2) | |
81 #pylab.show() | |
82 sampletime = acqwindow / len(ary1) | |
83 d = getpdiffedge(ary1, ary2) * sampletime | |
84 | |
85 print('Delta is %.1f nsec' % (d * 1e9)) | |
86 if dbh != None: | |
87 now = datetime.datetime.now() | |
88 cur.execute('INSERT INTO ppslog(name, time, delta) VALUES(%s, %s, %s)', (name, now, d)) | |
89 dbh.commit() | |
90 | |
91 def getchannel(u, ch, vscale): | |
92 u.write('DAT:SOU CH%d' % (ch)) # Set the curve source to desired channel | |
93 result = u.ask('CURVE?', 1.0) # Ask for the curve data | |
94 data1 = result[13:] # Chop off the header (should verify this really..) | |
95 ary = numpy.frombuffer(data1, dtype = '>h') | |
96 ary = ary / 32768.0 * vscale # Scale to volts | |
97 return ary | |
98 | |
99 def acquire(u, vscale1, vscale2): | |
100 u.write('ACQ:STATE 1') # Do a single acquisition | |
101 u.write('*OPC?') | |
102 u.read(2.0) # Wait for it to complete | |
103 | |
104 ary1 = getchannel(u, 1, vscale1) | |
105 ary2 = getchannel(u, 2, vscale2) | |
106 | |
107 return ary1, ary2 | |
108 | |
109 def getpdiffedge(ary1, ary2): | |
110 '''Return phase difference in samples between two signals by edge detection''' | |
111 | |
112 # Rescale to 0-1 | |
113 ary1 = ary1 - ary1.min() | |
114 ary1 = ary1 / ary1.max() | |
115 ary2 = ary2 - ary2.min() | |
116 ary2 = ary2 / ary2.max() | |
117 | |
118 # Find rising edge of each | |
119 ary1pos = numpy.argmax(ary1 > 0.2) | |
120 ary2pos = numpy.argmax(ary2 > 0.2) | |
121 | |
122 return ary1pos - ary2pos | |
123 | |
124 if __name__ == '__main__': | |
125 main() |