import mathutils import plotutils X_MIN = scx.minValue / 2 X_MAX = scx.maxValue / 2 Y_MIN = scy.minValue / 2 Y_MAX = scy.maxValue / 2 SIMULATION = True Y_STEP = 0.125 sensor = scd_sim if SIMULATION else scd X_MIN, X_MAX, Y_MIN, Y_MAX = float(X_MIN), float(X_MAX), float(Y_MIN), float(Y_MAX) step=0 for y in frange(Y_MIN, Y_MAX, Y_STEP): scy.move(y) scan_range = [X_MIN, X_MAX] if (step % 2==0) else [X_MAX, X_MIN] r = cscan(scx, sensor, scan_range[0], scan_range[1], 0.01, latency = DIODE_SETTLING_TIME) try: xdata = enforce_monotonic(r.getPositions(0)) ydata = r.getReadable(0) (offset, normalization, mean_val, sigma) = mathutils.fit_gaussian_offset(ydata, xdata) if (normalization-offset) < offset * 1.20: raise Exception("Invalid x fit") if sigma > 0.5: raise Exception("Invalid x fit") #Plotting the gaussian gaussian = mathutils.Gaussian(normalization, mean_val, sigma) plotutils.plot_function(get_plots(None)[0], gaussian, "Fit", xdata) print "Found x center at ", mean_val scx.move(mean_val) break except Exception as e: print "Invalid x fit for y=", y, str(e) step += 1 r = cscan(scy, sensor, Y_MIN, Y_MAX, 0.01, latency = DIODE_SETTLING_TIME) xdata = enforce_monotonic(r.getPositions(0)) ydata = r.getReadable(0) (offset, normalization, mean_val, sigma) = mathutils.fit_gaussian_offset(ydata, xdata) if (normalization-offset) < offset * 1.20: raise Exception("Invalid y fit") if sigma > 0.5: raise Exception("Invalid y fit") print "Found y center at ", mean_val scy.move(mean_val) print "Success finding center at x=", scx.read(), " y=", scy.read()