MIN_GAIN, MAX_GAIN = 0.5, 1.1 DECREMENT_STEP = 0.02 INCREMENT_FACTOR = 0.5 SCAN_RANGE_FACTOR = 6 MAX_RANGE_STEP = 200 new_gain, new_voltage = None, None desired_loss=get_blm_saturation(blms[i]) current_voltage = get_blm_ws_gain(blms[i]) #read_ws_gain (blms[i], wire) #current_loss = float(get_blm_loss(blms[i])) current_loss = 1700 current_gain = get_gain_from_voltage(current_voltage) if False: #current_loss > desired_loss: #Gain must decrease. Fixed steps down if current_voltage > MIN_GAIN: new_gain = max(current_voltage - DECREMENT_STEP, MIN_GAIN) else: #Gain must be increasede. Step proportional to the difference dg = current_gain * (desired_loss/ current_loss -1) new_gain = current_gain + dg * INCREMENT_FACTOR new_voltage = get_voltage_from_gain(new_gain) new_voltage = max(min(new_voltage, MAX_GAIN),MIN_GAIN) print "dg=", dg, " ng=", new_gain if (new_voltage is not None) and (new_voltage != current_voltage): logstr= "Adapting " , blms[i], " - ", wire, "gain: ", new_voltage print logstr log(logstr) write_ws_gain(blms[i], wire, new_voltage)