Browse Source

firmware: Analog read was to shaky, the number of read per millisecond is now increased to 20. meas.py: Some modifications to get the output more userfriendly. CSV output is implemented now.

klaute 1 year ago
parent
commit
7e9c8b086e
2 changed files with 56 additions and 19 deletions
  1. 10
    2
      firmware/command_ctrl.ino
  2. 46
    17
      tools/meas.py

+ 10
- 2
firmware/command_ctrl.ino View File

@@ -222,8 +222,16 @@ void cc_startMeasurement()
222 222
       {
223 223
         // 3. on every loop read the analog input A0 and A1 for the in intervall (milliseconds)
224 224
         //    and generate the average value, read the ADC value every milli second.
225
-        a0_sum += analogRead(A0);
226
-        a1_sum += analogRead(A1);
225
+        uint8_t t = 0;
226
+        uint16_t ta0 = 0;
227
+        uint16_t ta1 = 0;
228
+        for (t = 0; t < 20; t++)
229
+        {
230
+          ta0 += analogRead(A0);
231
+          ta1 += analogRead(A1);
232
+        }
233
+        a0_sum += (ta0 / 20);
234
+        a1_sum += (ta1 / 20);
227 235
 
228 236
         delay(1);
229 237
       }

+ 46
- 17
tools/meas.py View File

@@ -68,11 +68,6 @@ THREAD_LOOP_DELAY_S = 0.01
68 68
 
69 69
 ###############################################################################
70 70
 
71
-MUX_MIN_VAL = 0
72
-MUX_MAX_VAL = 7
73
-
74
-###############################################################################
75
-
76 71
 ser    = None
77 72
 device = "/dev/ttyUSB0"
78 73
 
@@ -339,6 +334,18 @@ def sendSerialData(data):
339 334
 
340 335
 ###############################################################################
341 336
 
337
+def user_friendly_freq(f):
338
+
339
+  if f >= 1000000:
340
+    return str(f / 1000000.0) + " MHz"
341
+
342
+  elif f >= 1000:
343
+    return str(f / 1000.0) + " kHz"
344
+
345
+  return str(f) + " Hz"
346
+
347
+###############################################################################
348
+
342 349
 if __name__ == "__main__":
343 350
 
344 351
   start_freq = 0
@@ -447,7 +454,7 @@ if __name__ == "__main__":
447 454
           a0   += e[3][5]
448 455
           a1    = e[3][6] << 8
449 456
           a1   += e[3][7]
450
-          print "freq: " + str(freq)
457
+          print "freq: " + user_friendly_freq(freq)
451 458
           print "a0: " + str(a0)
452 459
           print "a1: " + str(a1)
453 460
 
@@ -471,11 +478,11 @@ if __name__ == "__main__":
471 478
           intervall += e[3][13]
472 479
           drive_str = e[3][14]
473 480
 
474
-          print "start_freq = " + str(start_freq)
475
-          print "end_freq   = " + str(end_freq)
476
-          print "step_freq  = " + str(step_freq)
477
-          print "intervall  = " + str(intervall)
478
-          print "drive_str  = " + str(drive_str)
481
+          print "start_freq = " + user_friendly_freq(start_freq)
482
+          print "end_freq   = " + user_friendly_freq(end_freq)
483
+          print "step_freq  = " + user_friendly_freq(step_freq)
484
+          print "intervall  = " + str(intervall) + " ms"
485
+          print "drive_str  = " + str((drive_str + 1) * 2) + " mA"
479 486
 
480 487
           if args.start_meas == True and config_read == False:
481 488
             dataSend = dataSend + 1 + ((end_freq - start_freq) / step_freq)
@@ -489,20 +496,34 @@ if __name__ == "__main__":
489 496
           meas_r = []
490 497
           meas_p = []
491 498
 
499
+          min_vswr = [ 10, 0 ] # the default VSWR is 10 and the default freq is 0]
500
+
492 501
           i = ((drive_str + 1) * 2.0) / 1000.0
493 502
 
494 503
           if args.output_file != None or args.show_graph == True:
495 504
             for m in meas_data:
496 505
 
497 506
               meas_freq.append(m[0])
498
-
499
-              if m[1] > m[2]:
500
-                meas_ratio.append(m[1] / m[2])
501
-              elif m[1] < m[2]:
502
-                meas_ratio.append(m[2] / m[1])
507
+              vswr = 0
508
+
509
+              if m[1] > 0 and m[2] > 0:
510
+                if m[1] > m[2]:
511
+                  vswr = (m[1] / m[2])
512
+                  meas_ratio.append(vswr)
513
+                elif m[1] < m[2]:
514
+                  vswr = (m[2] / m[1])
515
+                  meas_ratio.append(vswr)
516
+                else:
517
+                  vswr = 1
518
+                  meas_ratio.append(1)
503 519
               else:
520
+                vswr = 1
504 521
                 meas_ratio.append(1)
505 522
 
523
+              if vswr < min_vswr[0]:
524
+                min_vswr[0] = vswr
525
+                min_vswr[1] = m[0] # the frequency
526
+
506 527
               # R = meas_data[0] * 5V / 1023 * I
507 528
               r = (m[1] * 5.0 / 1023.0) / i
508 529
               meas_r.append(r)
@@ -514,7 +535,13 @@ if __name__ == "__main__":
514 535
                 meas_p.append(0)
515 536
 
516 537
           if args.output_file != None:
517
-            pass # TODO impl
538
+            FILE = open(args.output_file, "w")
539
+            j = 0
540
+            for m in meas_ratio:
541
+              FILE.write("%f;%f;%f;%f;%f\n" % (meas_freq[j], m, meas_r[j], meas_p[j], i))
542
+              j = j + 1
543
+            FILE.close()
544
+            print "Output file " + args.output_file + " written."
518 545
 
519 546
           if args.show_graph == True:
520 547
             plt.plot(meas_freq, meas_ratio)
@@ -524,6 +551,8 @@ if __name__ == "__main__":
524 551
             print "Please close the mathplot window to exit..."
525 552
             plt.show()
526 553
 
554
+          print "First minimum VSWR " + str(min_vswr[0]) + " found at freqency " + user_friendly_freq(min_vswr[1])
555
+
527 556
         else:
528 557
           print "err: unknown type 0x%02x" % (e[1])
529 558
           break