annotate logpps.py @ 68:f95db5ea2fe1

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