From 7e9c8b086e7e9e74db431902827887dc75bada95 Mon Sep 17 00:00:00 2001 From: klaute Date: Wed, 21 Sep 2016 20:02:58 +0200 Subject: [PATCH] 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. --- firmware/command_ctrl.ino | 12 ++++++-- tools/meas.py | 61 +++++++++++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/firmware/command_ctrl.ino b/firmware/command_ctrl.ino index f51478f..b52d0b7 100644 --- a/firmware/command_ctrl.ino +++ b/firmware/command_ctrl.ino @@ -222,8 +222,16 @@ void cc_startMeasurement() { // 3. on every loop read the analog input A0 and A1 for the in intervall (milliseconds) // and generate the average value, read the ADC value every milli second. - a0_sum += analogRead(A0); - a1_sum += analogRead(A1); + uint8_t t = 0; + uint16_t ta0 = 0; + uint16_t ta1 = 0; + for (t = 0; t < 20; t++) + { + ta0 += analogRead(A0); + ta1 += analogRead(A1); + } + a0_sum += (ta0 / 20); + a1_sum += (ta1 / 20); delay(1); } diff --git a/tools/meas.py b/tools/meas.py index ed0bbf9..829b29f 100644 --- a/tools/meas.py +++ b/tools/meas.py @@ -68,11 +68,6 @@ THREAD_LOOP_DELAY_S = 0.01 ############################################################################### -MUX_MIN_VAL = 0 -MUX_MAX_VAL = 7 - -############################################################################### - ser = None device = "/dev/ttyUSB0" @@ -339,6 +334,18 @@ def sendSerialData(data): ############################################################################### +def user_friendly_freq(f): + + if f >= 1000000: + return str(f / 1000000.0) + " MHz" + + elif f >= 1000: + return str(f / 1000.0) + " kHz" + + return str(f) + " Hz" + +############################################################################### + if __name__ == "__main__": start_freq = 0 @@ -447,7 +454,7 @@ if __name__ == "__main__": a0 += e[3][5] a1 = e[3][6] << 8 a1 += e[3][7] - print "freq: " + str(freq) + print "freq: " + user_friendly_freq(freq) print "a0: " + str(a0) print "a1: " + str(a1) @@ -471,11 +478,11 @@ if __name__ == "__main__": intervall += e[3][13] drive_str = e[3][14] - print "start_freq = " + str(start_freq) - print "end_freq = " + str(end_freq) - print "step_freq = " + str(step_freq) - print "intervall = " + str(intervall) - print "drive_str = " + str(drive_str) + print "start_freq = " + user_friendly_freq(start_freq) + print "end_freq = " + user_friendly_freq(end_freq) + print "step_freq = " + user_friendly_freq(step_freq) + print "intervall = " + str(intervall) + " ms" + print "drive_str = " + str((drive_str + 1) * 2) + " mA" if args.start_meas == True and config_read == False: dataSend = dataSend + 1 + ((end_freq - start_freq) / step_freq) @@ -489,20 +496,34 @@ if __name__ == "__main__": meas_r = [] meas_p = [] + min_vswr = [ 10, 0 ] # the default VSWR is 10 and the default freq is 0] + i = ((drive_str + 1) * 2.0) / 1000.0 if args.output_file != None or args.show_graph == True: for m in meas_data: meas_freq.append(m[0]) + vswr = 0 - if m[1] > m[2]: - meas_ratio.append(m[1] / m[2]) - elif m[1] < m[2]: - meas_ratio.append(m[2] / m[1]) + if m[1] > 0 and m[2] > 0: + if m[1] > m[2]: + vswr = (m[1] / m[2]) + meas_ratio.append(vswr) + elif m[1] < m[2]: + vswr = (m[2] / m[1]) + meas_ratio.append(vswr) + else: + vswr = 1 + meas_ratio.append(1) else: + vswr = 1 meas_ratio.append(1) + if vswr < min_vswr[0]: + min_vswr[0] = vswr + min_vswr[1] = m[0] # the frequency + # R = meas_data[0] * 5V / 1023 * I r = (m[1] * 5.0 / 1023.0) / i meas_r.append(r) @@ -514,7 +535,13 @@ if __name__ == "__main__": meas_p.append(0) if args.output_file != None: - pass # TODO impl + FILE = open(args.output_file, "w") + j = 0 + for m in meas_ratio: + FILE.write("%f;%f;%f;%f;%f\n" % (meas_freq[j], m, meas_r[j], meas_p[j], i)) + j = j + 1 + FILE.close() + print "Output file " + args.output_file + " written." if args.show_graph == True: plt.plot(meas_freq, meas_ratio) @@ -524,6 +551,8 @@ if __name__ == "__main__": print "Please close the mathplot window to exit..." plt.show() + print "First minimum VSWR " + str(min_vswr[0]) + " found at freqency " + user_friendly_freq(min_vswr[1]) + else: print "err: unknown type 0x%02x" % (e[1]) break