From 59b82fbb303168ce08e6bc26879382e338684496 Mon Sep 17 00:00:00 2001 From: klaute Date: Fri, 7 Oct 2016 17:39:14 +0200 Subject: [PATCH] The graph is now shown during the measurement progress. --- tools/meas.py | 259 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 170 insertions(+), 89 deletions(-) diff --git a/tools/meas.py b/tools/meas.py index 6b1f928..9b357eb 100644 --- a/tools/meas.py +++ b/tools/meas.py @@ -13,6 +13,7 @@ import serial import copy import binascii import matplotlib.pyplot as plt +from pylab import arange import sys import operator @@ -117,7 +118,7 @@ msg_type_data_to_read = { MSG_TYPE_ANSWER_OK : MSG_TYPE_ANSWER_OK_DATA_TO_R MSG_TYPE_CONFIG : MSG_TYPE_CONFIG_DATA_TO_RECV, MSG_TYPE_MEAS_END_INFO : MSG_TYPE_MEAS_END_INFO_DATA_TO_RECV, } -msg_type = 0 +msg_type = 0 cc_data_read = 0 cc_data_buffer = [] @@ -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(): 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__": - start_freq = 0 - end_freq = 0 - step_freq = 0 - - config_read = False - meas_data = [] cc_init() @@ -484,6 +622,14 @@ if __name__ == "__main__": sendSerialData([CC_CMD_SAV_DFLT]) 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 while dataSend > 0 and timeout < TIMEOUT_CNT_MAX: @@ -513,12 +659,19 @@ if __name__ == "__main__": a0 += e[3][5] a1 = e[3][6] << 8 a1 += e[3][7] + #print "freq: " + user_friendly_freq(freq) #print "a0: " + str(a0) #print "a1: " + str(a1) + sys.stdout.write("\r" + gen_progress_bar(dataSend, start_freq, end_freq, step_freq)) 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: #print "recv: CONFIG" if args.start_meas == True: @@ -557,58 +710,16 @@ if __name__ == "__main__": sys.stdout.write("\r100.00 % done \n") print "" - meas_freq = [] - meas_ratio = [] - 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) + if (args.output_file != None or args.show_graph == True) and args.start_meas == True: + calc_data() ##### 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.write("freqency;ratio;impedance;drive;a0;a1\n") j = 0 - for m in meas_ratio: - 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])) + for m in meas_vswr: + 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 FILE.close() 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])) ##### show the graph - if args.show_graph == True: - - f, axarr = plt.subplots(3, sharex=True) - - f.canvas.set_window_title("SWR meter measurement results") - - - 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() + if args.show_graph == True and args.start_meas == True: + # TODO wait for close + update_graph() + print "Please close the window to exit the program." + plt.show(block=True) else: print "err: unknown type 0x%02x" % (e[1])