Browse Source

The graph is now shown during the measurement progress.

klaute 2 years ago
parent
commit
59b82fbb30
1 changed files with 169 additions and 88 deletions
  1. 169
    88
      tools/meas.py

+ 169
- 88
tools/meas.py View File

@@ -13,6 +13,7 @@ import serial
13 13
 import copy
14 14
 import binascii
15 15
 import matplotlib.pyplot as plt
16
+from pylab import arange
16 17
 import sys
17 18
 import operator
18 19
 
@@ -117,7 +118,7 @@ msg_type_data_to_read = { MSG_TYPE_ANSWER_OK      : MSG_TYPE_ANSWER_OK_DATA_TO_R
117 118
                           MSG_TYPE_CONFIG         : MSG_TYPE_CONFIG_DATA_TO_RECV,
118 119
                           MSG_TYPE_MEAS_END_INFO  : MSG_TYPE_MEAS_END_INFO_DATA_TO_RECV, }
119 120
 
120
-msg_type       = 0
121
+msg_type = 0
121 122
 
122 123
 cc_data_read   = 0
123 124
 cc_data_buffer = []
@@ -135,6 +136,40 @@ thread_stop    = False
135 136
 
136 137
 ###############################################################################
137 138
 
139
+vswr_marker = []
140
+meas_freq   = []
141
+meas_vswr   = []
142
+meas_imp    = []
143
+meas_a0     = []
144
+meas_a1     = []
145
+meas_vswr_f = {}
146
+min_vswr    = [ 10, 0 ] # the default VSWR is 10 and the default freq is 0]
147
+
148
+###############################################################################
149
+
150
+lvswr = None
151
+limp  = None
152
+la0   = None
153
+la1   = None
154
+
155
+###############################################################################
156
+
157
+config_read  = False
158
+config_lines = False
159
+
160
+###############################################################################
161
+
162
+fig1  = None
163
+axarr = None
164
+
165
+###############################################################################
166
+
167
+start_freq = 0
168
+end_freq   = 0
169
+step_freq  = 0
170
+
171
+###############################################################################
172
+
138 173
 def cc_init():
139 174
 
140 175
   global cc_state_fn
@@ -383,13 +418,116 @@ def gen_progress_bar(n, sf, ef, fs):
383 418
 
384 419
 ###############################################################################
385 420
 
386
-if __name__ == "__main__":
421
+def calc_data():
422
+
423
+  global vswr_marker
424
+  global meas_data
425
+  global meas_freq
426
+  global meas_vswr
427
+  global meas_imp
428
+  global meas_a0
429
+  global meas_a1
430
+  global meas_vswr_f
431
+  global min_vswr
432
+
433
+  i = ((drive_str + 1) * 2.0) / 1000.0
434
+
435
+  ##### calculate the results
436
+  #for m in meas_data:
437
+  m = meas_data[-1]
438
+
439
+  meas_freq.append(m[0])
440
+  vswr = 0
441
+
442
+  meas_a0.append(m[1])
443
+  meas_a1.append(m[2])
444
+
445
+  if m[1] > 0 and m[2] > 0:
446
+    if m[1] > m[2]:
447
+      vswr = (1.0 * m[1] / m[2])
448
+      meas_vswr.append(vswr)
449
+    elif m[1] < m[2]:
450
+      vswr = (1.0 * m[2] / m[1])
451
+      meas_vswr.append(vswr)
452
+    else:
453
+      vswr = 1
454
+      meas_vswr.append(1)
455
+  else:
456
+    vswr = 1
457
+    meas_vswr.append(1)
458
+
459
+  meas_vswr_f[m[0]] = vswr
460
+
461
+  if vswr < min_vswr[0]:
462
+    min_vswr[0] = vswr
463
+    min_vswr[1] = m[0] # the frequency
464
+
465
+  # impedance
466
+  r = 50.0 * vswr
467
+  meas_imp.append(r)
468
+
469
+  # generate the lowest 5 vswr marker
470
+  i = 0
471
+  old = 1000
472
+  for r in sorted(meas_vswr_f.items(), key=operator.itemgetter(1)):
473
+    vswr_marker.append(meas_freq.index(r[0]))
474
+    if r > old:
475
+      i += 1
476
+    old = r
477
+    if i == 5:
478
+      break
479
+
480
+#####
481
+def update_graph():
482
+
483
+  global vswr_marker
484
+  global meas_data
485
+  global meas_freq
486
+  global meas_vswr
487
+  global meas_imp
488
+  global meas_a0
489
+  global meas_a1
490
+  global meas_vswr_f
491
+  global axarr
492
+  global config_read
493
+  global config_lines
494
+  global lvswr
495
+  global limp
496
+  global la0
497
+  global la1
498
+
499
+  if config_read == True and config_lines == False:
500
+    x = arange(start_freq, end_freq, step_freq)
501
+    lvswr, = axarr[0].plot(x[0], meas_vswr[-1], label='VSWR', markevery=[vswr_marker[0]], markersize=4, marker="o", markerfacecolor="r")
502
+    limp,  = axarr[1].plot(x[0], meas_imp[-1],  label='impedance', markevery=[vswr_marker[0]], markersize=4, marker="o", markerfacecolor="r")
503
+    la0,   = axarr[2].plot(x[0], meas_a0[-1], label='a0')#, markersize=4, marker="o", markerfacecolor="r")
504
+    la1,   = axarr[2].plot(x[0], meas_a1[-1], label='a1')#, markersize=4, marker="o", markerfacecolor="r")
505
+
506
+    axarr[0].legend(handles=[lvswr])
507
+    axarr[0].set_xlim([start_freq, end_freq])
508
+    axarr[0].set_ylim([0, max(meas_vswr)+1])
509
+    axarr[1].legend(handles=[limp])
510
+    axarr[1].set_xlim([start_freq, end_freq])
511
+    axarr[1].set_ylim([20, 150])
512
+    axarr[2].legend(handles=[la0, la1])
513
+    axarr[2].set_xlim([start_freq, end_freq])
514
+    axarr[2].set_ylim([0, 1024])
515
+
516
+    config_lines = True
517
+
518
+  elif config_read == True and config_lines == True:
519
+    lvswr.set_data(meas_freq, meas_vswr)
520
+    limp.set_data(meas_freq, meas_imp)
521
+    la0.set_data(meas_freq, meas_a0)
522
+    la1.set_data(meas_freq, meas_a1)
523
+
524
+  #print "Please close the mathplot window to exit..."
525
+  plt.draw()
526
+  plt.pause(0.00000001)
387 527
 
388
-  start_freq = 0
389
-  end_freq   = 0
390
-  step_freq  = 0
528
+###############################################################################
391 529
 
392
-  config_read = False
530
+if __name__ == "__main__":
393 531
 
394 532
   meas_data = []
395 533
 
@@ -484,6 +622,14 @@ if __name__ == "__main__":
484 622
     sendSerialData([CC_CMD_SAV_DFLT])
485 623
     dataSend = dataSend + 1
486 624
 
625
+  if args.show_graph == True and args.start_meas == True:
626
+    plt.ion()
627
+
628
+    fig1, axarr = plt.subplots(3, sharex=True)
629
+    fig1.canvas.set_window_title("SWR meter measurement results")
630
+
631
+    update_graph()
632
+
487 633
   # 4. start main loop
488 634
   while dataSend > 0 and timeout < TIMEOUT_CNT_MAX:
489 635
 
@@ -513,12 +659,19 @@ if __name__ == "__main__":
513 659
           a0   += e[3][5]
514 660
           a1    = e[3][6] << 8
515 661
           a1   += e[3][7]
662
+
516 663
           #print "freq: " + user_friendly_freq(freq)
517 664
           #print "a0: " + str(a0)
518 665
           #print "a1: " + str(a1)
666
+
519 667
           sys.stdout.write("\r" + gen_progress_bar(dataSend, start_freq, end_freq, step_freq))
520 668
           meas_data.append([ freq, a0, a1 ])
521 669
 
670
+          if args.show_graph == True:
671
+            # for file output no recalculationis required
672
+            calc_data()
673
+            update_graph()
674
+
522 675
         elif e[1] == MSG_TYPE_CONFIG:
523 676
           #print "recv: CONFIG"
524 677
           if args.start_meas == True:
@@ -557,58 +710,16 @@ if __name__ == "__main__":
557 710
           sys.stdout.write("\r100.00 % done                                       \n")
558 711
           print ""
559 712
 
560
-          meas_freq = []
561
-          meas_ratio = []
562
-          meas_r = []
563
-          meas_a0 = []
564
-          meas_a1 = []
565
-          meas_ratio_f = {}
566
-
567
-          min_vswr = [ 10, 0 ] # the default VSWR is 10 and the default freq is 0]
568
-
569
-          i = ((drive_str + 1) * 2.0) / 1000.0
570
-
571
-          ##### calculate the results
572
-          if args.output_file != None or args.show_graph == True:
573
-            for m in meas_data:
574
-
575
-              meas_freq.append(m[0])
576
-              vswr = 0
577
-
578
-              meas_a0.append(m[1])
579
-              meas_a1.append(m[2])
580
-
581
-              if m[1] > 0 and m[2] > 0:
582
-                if m[1] > m[2]:
583
-                  vswr = (1.0 * m[1] / m[2])
584
-                  meas_ratio.append(vswr)
585
-                elif m[1] < m[2]:
586
-                  vswr = (1.0 * m[2] / m[1])
587
-                  meas_ratio.append(vswr)
588
-                else:
589
-                  vswr = 1
590
-                  meas_ratio.append(1)
591
-              else:
592
-                vswr = 1
593
-                meas_ratio.append(1)
594
-
595
-              meas_ratio_f[m[0]] = vswr
596
-
597
-              if vswr < min_vswr[0]:
598
-                min_vswr[0] = vswr
599
-                min_vswr[1] = m[0] # the frequency
600
-
601
-              # impedance
602
-              r = 50.0 * vswr
603
-              meas_r.append(r)
713
+          if (args.output_file != None or args.show_graph == True) and args.start_meas == True:
714
+            calc_data()
604 715
 
605 716
           ##### generate the output CSV file
606
-          if args.output_file != None:
717
+          if args.output_file != None and args.start_meas == True:
607 718
             FILE = open(args.output_file, "w")
608 719
             FILE.write("freqency;ratio;impedance;drive;a0;a1\n")
609 720
             j = 0
610
-            for m in meas_ratio:
611
-              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]))
721
+            for m in meas_vswr:
722
+              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]))
612 723
               j = j + 1
613 724
             FILE.close()
614 725
             print "Output file " + args.output_file + " written."
@@ -616,41 +727,11 @@ if __name__ == "__main__":
616 727
           print "First minimum VSWR %0.6f found at freqency %s" % (min_vswr[0], user_friendly_freq(min_vswr[1]))
617 728
 
618 729
           ##### show the graph
619
-          if args.show_graph == True:
620
-
621
-            f, axarr = plt.subplots(3, sharex=True)
622
-
623
-            f.canvas.set_window_title("SWR meter measurement results")
624
-
625
-
626
-            vswr_marker = []
627
-            i = 0
628
-            old = 1000
629
-            for r in sorted(meas_ratio_f.items(), key=operator.itemgetter(1)):
630
-            #for r in sorted(meas_ratio_f):
631
-              vswr_marker.append(meas_freq.index(r[0]))
632
-              if r > old:
633
-                i += 1
634
-              old = r
635
-              if i == 5:
636
-                break
637
-
638
-            lv, = axarr[0].plot(meas_freq, meas_ratio, label='VSWR', markevery=vswr_marker, markersize=4, marker="o", markerfacecolor="r")
639
-            lr, = axarr[1].plot(meas_freq, meas_r, label='impedance', markevery=[vswr_marker[0]], markersize=4, marker="o", markerfacecolor="r")
640
-            la0, = axarr[2].plot(meas_freq, meas_a0, label='a0')
641
-            la1, = axarr[2].plot(meas_freq, meas_a1, label='a1')
642
-
643
-            axarr[0].legend(handles=[lv])
644
-            #axarr[0].scatter(meas_freq, meas_ratio, 1)
645
-            axarr[0].set_ylim([0, 5])
646
-            axarr[1].legend(handles=[lr])
647
-            #axarr[1].scatter(meas_freq, meas_r, 1)
648
-            axarr[1].set_ylim([20, 150])
649
-            axarr[2].legend(handles=[la0, la1])
650
-            axarr[2].set_ylim([0, 1024])
651
-
652
-            print "Please close the mathplot window to exit..."
653
-            plt.show()
730
+          if args.show_graph == True and args.start_meas == True:
731
+            # TODO wait for close
732
+            update_graph()
733
+            print "Please close the window to exit the program."
734
+            plt.show(block=True)
654 735
 
655 736
         else:
656 737
           print "err: unknown type 0x%02x" % (e[1])