diff options
author | Sven Wegener <sven.wegener@stealer.net> | 2015-12-20 22:21:34 +0100 |
---|---|---|
committer | Sven Wegener <sven.wegener@stealer.net> | 2016-01-21 22:46:28 +0100 |
commit | f1700d8cb1cf9ec7136b29578f9caf159ac70e90 (patch) | |
tree | 7eb897b5d127d5341361708f5806ac362c22c930 | |
parent | ff7a42a2700115a61eeabf8eba4341bb500cfeca (diff) |
Add calibration for COUT and VIN to calibrate.pycalibration
Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
-rwxr-xr-x | stm8/calibrate.py | 155 |
1 files changed, 154 insertions, 1 deletions
diff --git a/stm8/calibrate.py b/stm8/calibrate.py index 6410d6b..b5c9085 100755 --- a/stm8/calibrate.py +++ b/stm8/calibrate.py @@ -139,7 +139,15 @@ class B3603(object): return (pwm_vout, pwm_cout) def current(self, c): - return self.command("CURRENT %.3f" % c) + lines = self.command("CURRENT %.3f" % c) + pwm_vout = None + pwm_cout = None + for line in lines: + word = line.split(' ') + if word[0] != 'PWM' and word[0] != 'aWM': continue + if word[1] == 'VOLTAGE': pwm_vout = float(word[2]) + if word[1] == 'CURRENT': pwm_cout = float(word[2]) + return (pwm_vout, pwm_cout) class Multimeter(object): def __init__(self, portname, model): @@ -295,6 +303,151 @@ def auto_calibration(): psu.close() +def auto_calibration_cout(): + psu = B3603(sys.argv[2]) + if not psu.open(): + print 'Failed to open serial port to B3603 on serial %s' % sys.argv[2] + return + + dmm = Multimeter(sys.argv[3], sys.argv[4]) + if not dmm.open(): + print 'Failed to open serial port to multimeter on serial %s model %s' % (sys.argv[3], sys.argv[4]) + psu.close() + return + + vin = psu.status()['vin'] + psu.voltage(vin) + + NUM_STEPS = 20 + MIN_CURRENT = 0.1 + MAX_CURRENT = 3 + STEP_SIZE_INT = int(100 * (MAX_CURRENT - MIN_CURRENT) / NUM_STEPS) + STEP_SIZE = STEP_SIZE_INT / 100.0 + print 'Will use %d steps between %s and %s' % (NUM_STEPS, MIN_CURRENT, MAX_CURRENT) + + if STEP_SIZE < 0.01: + print 'Step size is below 0.1, cannot test' + return + + psu.output_on() + psu.current(MIN_CURRENT) + + pwm_data = [] + adc_data = [] + cout_data = [] + valid = True + + for step in xrange(NUM_STEPS): + current = MIN_CURRENT + step * STEP_SIZE + print 'Setting current to', current + (pwm_vout, pwm_cout) = psu.current(current) + # Wait 1 second for things to stabilize + time.sleep(1) + cout = dmm.sample3(3) # Use three samples + if cout == None: + print 'Failed to get cout' + valid = False + break + if cout < 0.01: + print 'Cout is too low (%s), something broke, try to reset the B3603 to defaults with RESTORE command first' % cout + #valid = False + #break + rstatus = psu.rstatus() + adc_cout = rstatus['cout_adc'] + cout_calc = rstatus['cout_calc'] + + pwm_data.append(pwm_cout) + adc_data.append(adc_cout) + cout_data.append(int(cout*1000)) + print 'Step %d Set current %f Read current %f PWM %s ADC %s (%s)' % (step, current, cout, pwm_vout, adc_cout, cout_calc) + + print psu.output_off() + + if not valid: + print 'Test is invalid, calibration cancelled' + return + + print 'ADC' + val = lse(adc_data, cout_data) + adc_a = int(val[0]*65536) + adc_b_tmp = val[1] + if adc_b_tmp < 0: + adc_b_tmp = -adc_b_tmp + else: + print 'Expected ADC_B to be negative... for some reason it\'ts not' + adc_b_tmp = 0 + adc_b = int(adc_b_tmp*65536) + print val, adc_a, adc_b + print psu.command('CALCOUTADCA %d' % adc_a) + print psu.command('CALCOUTADCB %d' % adc_b) + print + print 'PWM' + val = lse(cout_data, pwm_data) + pwm_a = int(val[0]*65536) + pwm_b = int(val[1]*65536) + print val, pwm_a, pwm_b + print psu.command('CALCOUTPWMA %d' % pwm_a) + print psu.command('CALCOUTPWMB %d' % pwm_b) + + psu.close() + +def auto_calibration_vin(): + psu = B3603(sys.argv[2]) + if not psu.open(): + print 'Failed to open serial port to B3603 on serial %s' % sys.argv[2] + return + + dmm = Multimeter(sys.argv[3], sys.argv[4]) + if not dmm.open(): + print 'Failed to open serial port to multimeter on serial %s model %s' % (sys.argv[3], sys.argv[4]) + psu.close() + return + + NUM_STEPS = 10 + print 'Will use %d steps' % (NUM_STEPS,) + + adc_data = [] + vin_data = [] + valid = True + + for step in xrange(NUM_STEPS): + print 'Waiting for voltage, step %d' % (step,) + raw_input() + vin = dmm.sample3(3) # Use three samples + if vin == None: + print 'Failed to get vin' + valid = False + break + rstatus = psu.rstatus() + adc_vin = rstatus['vin_adc'] + vin_calc = rstatus['vin_calc'] + + adc_data.append(adc_vin) + vin_data.append(int(vin*1000)) + print 'Step %d Read voltage %f ADC %s (%s)' % (step, vin, adc_vin, vin_calc) + + print psu.output_off() + + if not valid: + print 'Test is invalid, calibration cancelled' + return + + print 'ADC' + val = lse(adc_data, vin_data) + adc_a = int(val[0]*65536) + adc_b_tmp = val[1] + if adc_b_tmp < 0: + adc_b_tmp = -adc_b_tmp + else: + print 'Expected ADC_B to be negative... for some reason it\'ts not' + adc_b_tmp = 0 + adc_b = int(adc_b_tmp*65536) + print val, adc_a, adc_b + print psu.command('CALVINADCA %d' % adc_a) + print psu.command('CALVINADCB %d' % adc_b) + + psu.close() + def manual_calibration(): print 'Not implemented' |