The graph is now shown during the measurement progress.

This commit is contained in:
klaute 2016-10-07 17:39:14 +02:00
parent f1ef87a429
commit 59b82fbb30

View file

@ -13,6 +13,7 @@ import serial
import copy import copy
import binascii import binascii
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from pylab import arange
import sys import sys
import operator import operator
@ -135,6 +136,40 @@ thread_stop = False
############################################################################### ###############################################################################
vswr_marker = []
meas_freq = []
meas_vswr = []
meas_imp = []
meas_a0 = []
meas_a1 = []
meas_vswr_f = {}
min_vswr = [ 10, 0 ] # the default VSWR is 10 and the default freq is 0]
###############################################################################
lvswr = None
limp = None
la0 = None
la1 = None
###############################################################################
config_read = False
config_lines = False
###############################################################################
fig1 = None
axarr = None
###############################################################################
start_freq = 0
end_freq = 0
step_freq = 0
###############################################################################
def cc_init(): def cc_init():
global cc_state_fn global cc_state_fn
@ -383,14 +418,117 @@ def gen_progress_bar(n, sf, ef, fs):
############################################################################### ###############################################################################
def calc_data():
global vswr_marker
global meas_data
global meas_freq
global meas_vswr
global meas_imp
global meas_a0
global meas_a1
global meas_vswr_f
global min_vswr
i = ((drive_str + 1) * 2.0) / 1000.0
##### calculate the results
#for m in meas_data:
m = meas_data[-1]
meas_freq.append(m[0])
vswr = 0
meas_a0.append(m[1])
meas_a1.append(m[2])
if m[1] > 0 and m[2] > 0:
if m[1] > m[2]:
vswr = (1.0 * m[1] / m[2])
meas_vswr.append(vswr)
elif m[1] < m[2]:
vswr = (1.0 * m[2] / m[1])
meas_vswr.append(vswr)
else:
vswr = 1
meas_vswr.append(1)
else:
vswr = 1
meas_vswr.append(1)
meas_vswr_f[m[0]] = vswr
if vswr < min_vswr[0]:
min_vswr[0] = vswr
min_vswr[1] = m[0] # the frequency
# impedance
r = 50.0 * vswr
meas_imp.append(r)
# generate the lowest 5 vswr marker
i = 0
old = 1000
for r in sorted(meas_vswr_f.items(), key=operator.itemgetter(1)):
vswr_marker.append(meas_freq.index(r[0]))
if r > old:
i += 1
old = r
if i == 5:
break
#####
def update_graph():
global vswr_marker
global meas_data
global meas_freq
global meas_vswr
global meas_imp
global meas_a0
global meas_a1
global meas_vswr_f
global axarr
global config_read
global config_lines
global lvswr
global limp
global la0
global la1
if config_read == True and config_lines == False:
x = arange(start_freq, end_freq, step_freq)
lvswr, = axarr[0].plot(x[0], meas_vswr[-1], label='VSWR', markevery=[vswr_marker[0]], markersize=4, marker="o", markerfacecolor="r")
limp, = axarr[1].plot(x[0], meas_imp[-1], label='impedance', markevery=[vswr_marker[0]], markersize=4, marker="o", markerfacecolor="r")
la0, = axarr[2].plot(x[0], meas_a0[-1], label='a0')#, markersize=4, marker="o", markerfacecolor="r")
la1, = axarr[2].plot(x[0], meas_a1[-1], label='a1')#, markersize=4, marker="o", markerfacecolor="r")
axarr[0].legend(handles=[lvswr])
axarr[0].set_xlim([start_freq, end_freq])
axarr[0].set_ylim([0, max(meas_vswr)+1])
axarr[1].legend(handles=[limp])
axarr[1].set_xlim([start_freq, end_freq])
axarr[1].set_ylim([20, 150])
axarr[2].legend(handles=[la0, la1])
axarr[2].set_xlim([start_freq, end_freq])
axarr[2].set_ylim([0, 1024])
config_lines = True
elif config_read == True and config_lines == True:
lvswr.set_data(meas_freq, meas_vswr)
limp.set_data(meas_freq, meas_imp)
la0.set_data(meas_freq, meas_a0)
la1.set_data(meas_freq, meas_a1)
#print "Please close the mathplot window to exit..."
plt.draw()
plt.pause(0.00000001)
###############################################################################
if __name__ == "__main__": if __name__ == "__main__":
start_freq = 0
end_freq = 0
step_freq = 0
config_read = False
meas_data = [] meas_data = []
cc_init() cc_init()
@ -484,6 +622,14 @@ if __name__ == "__main__":
sendSerialData([CC_CMD_SAV_DFLT]) sendSerialData([CC_CMD_SAV_DFLT])
dataSend = dataSend + 1 dataSend = dataSend + 1
if args.show_graph == True and args.start_meas == True:
plt.ion()
fig1, axarr = plt.subplots(3, sharex=True)
fig1.canvas.set_window_title("SWR meter measurement results")
update_graph()
# 4. start main loop # 4. start main loop
while dataSend > 0 and timeout < TIMEOUT_CNT_MAX: while dataSend > 0 and timeout < TIMEOUT_CNT_MAX:
@ -513,12 +659,19 @@ if __name__ == "__main__":
a0 += e[3][5] a0 += e[3][5]
a1 = e[3][6] << 8 a1 = e[3][6] << 8
a1 += e[3][7] a1 += e[3][7]
#print "freq: " + user_friendly_freq(freq) #print "freq: " + user_friendly_freq(freq)
#print "a0: " + str(a0) #print "a0: " + str(a0)
#print "a1: " + str(a1) #print "a1: " + str(a1)
sys.stdout.write("\r" + gen_progress_bar(dataSend, start_freq, end_freq, step_freq)) sys.stdout.write("\r" + gen_progress_bar(dataSend, start_freq, end_freq, step_freq))
meas_data.append([ freq, a0, a1 ]) meas_data.append([ freq, a0, a1 ])
if args.show_graph == True:
# for file output no recalculationis required
calc_data()
update_graph()
elif e[1] == MSG_TYPE_CONFIG: elif e[1] == MSG_TYPE_CONFIG:
#print "recv: CONFIG" #print "recv: CONFIG"
if args.start_meas == True: if args.start_meas == True:
@ -557,58 +710,16 @@ if __name__ == "__main__":
sys.stdout.write("\r100.00 % done \n") sys.stdout.write("\r100.00 % done \n")
print "" print ""
meas_freq = [] if (args.output_file != None or args.show_graph == True) and args.start_meas == True:
meas_ratio = [] calc_data()
meas_r = []
meas_a0 = []
meas_a1 = []
meas_ratio_f = {}
min_vswr = [ 10, 0 ] # the default VSWR is 10 and the default freq is 0]
i = ((drive_str + 1) * 2.0) / 1000.0
##### calculate the results
if args.output_file != None or args.show_graph == True:
for m in meas_data:
meas_freq.append(m[0])
vswr = 0
meas_a0.append(m[1])
meas_a1.append(m[2])
if m[1] > 0 and m[2] > 0:
if m[1] > m[2]:
vswr = (1.0 * m[1] / m[2])
meas_ratio.append(vswr)
elif m[1] < m[2]:
vswr = (1.0 * m[2] / m[1])
meas_ratio.append(vswr)
else:
vswr = 1
meas_ratio.append(1)
else:
vswr = 1
meas_ratio.append(1)
meas_ratio_f[m[0]] = vswr
if vswr < min_vswr[0]:
min_vswr[0] = vswr
min_vswr[1] = m[0] # the frequency
# impedance
r = 50.0 * vswr
meas_r.append(r)
##### generate the output CSV file ##### generate the output CSV file
if args.output_file != None: if args.output_file != None and args.start_meas == True:
FILE = open(args.output_file, "w") FILE = open(args.output_file, "w")
FILE.write("freqency;ratio;impedance;drive;a0;a1\n") FILE.write("freqency;ratio;impedance;drive;a0;a1\n")
j = 0 j = 0
for m in meas_ratio: for m in meas_vswr:
FILE.write("%f;%f;%f;%f;%d;%d\n" % (meas_freq[j], m, meas_r[j], i, meas_data[j][1], meas_data[j][2])) FILE.write("%f;%f;%f;%f;%d;%d\n" % (meas_freq[j], m, meas_imp[j], i, meas_data[j][1], meas_data[j][2]))
j = j + 1 j = j + 1
FILE.close() FILE.close()
print "Output file " + args.output_file + " written." print "Output file " + args.output_file + " written."
@ -616,41 +727,11 @@ if __name__ == "__main__":
print "First minimum VSWR %0.6f found at freqency %s" % (min_vswr[0], user_friendly_freq(min_vswr[1])) print "First minimum VSWR %0.6f found at freqency %s" % (min_vswr[0], user_friendly_freq(min_vswr[1]))
##### show the graph ##### show the graph
if args.show_graph == True: if args.show_graph == True and args.start_meas == True:
# TODO wait for close
f, axarr = plt.subplots(3, sharex=True) update_graph()
print "Please close the window to exit the program."
f.canvas.set_window_title("SWR meter measurement results") plt.show(block=True)
vswr_marker = []
i = 0
old = 1000
for r in sorted(meas_ratio_f.items(), key=operator.itemgetter(1)):
#for r in sorted(meas_ratio_f):
vswr_marker.append(meas_freq.index(r[0]))
if r > old:
i += 1
old = r
if i == 5:
break
lv, = axarr[0].plot(meas_freq, meas_ratio, label='VSWR', markevery=vswr_marker, markersize=4, marker="o", markerfacecolor="r")
lr, = axarr[1].plot(meas_freq, meas_r, label='impedance', markevery=[vswr_marker[0]], markersize=4, marker="o", markerfacecolor="r")
la0, = axarr[2].plot(meas_freq, meas_a0, label='a0')
la1, = axarr[2].plot(meas_freq, meas_a1, label='a1')
axarr[0].legend(handles=[lv])
#axarr[0].scatter(meas_freq, meas_ratio, 1)
axarr[0].set_ylim([0, 5])
axarr[1].legend(handles=[lr])
#axarr[1].scatter(meas_freq, meas_r, 1)
axarr[1].set_ylim([20, 150])
axarr[2].legend(handles=[la0, la1])
axarr[2].set_ylim([0, 1024])
print "Please close the mathplot window to exit..."
plt.show()
else: else:
print "err: unknown type 0x%02x" % (e[1]) print "err: unknown type 0x%02x" % (e[1])