LineSync-Based Async Scan Parameter Sweep

PowerScript

This script changes specific parameters (e.g., tip bias, setpoint) in synchronization with the line sync signal during image scanning, and is expected to be useful for efficiently identifying optimal measurement parameters. To use this script, the following conditions must be met: The line sync signal must be connected to Aux2 In on the controller back panel. The scan rate must be 0.4 Hz or lower. The overscan at each end must be set to 50%.

main 0 files
LineSync-Based Async Scan …..js
LineSync-Based Async Scan …..js 2944 bytes
var EdgeDetector = {
    prev: null,
    threshold: 0.3,
    detect: function(current) {
        var result = { rising: false, falling: false };
 
        if (this.prev === null) {
            this.prev = current;
            return result;
        }
 
        if (this.prev < this.threshold && current > this.threshold) {
            result.rising = true;
        } else if (this.prev > this.threshold && current <this.threshold) {
            result.falling = true;
        }
 
        this.prev = current;
        return result;
    },
 
    reset: function() {
        this.prev = null;
    }
};
 
 
var $AsyncScan = {
	parameters : {
		"channels"  : ["ZHeight", "TipBias", {"name":"SetPoint", "unit":"volt"}],
		"samplingRate" : 0.5, 			// unit: kHz
		"initTipBias" : 0.0,  		// unit: V
		"initSetPoint" : 1.0, 		// unit: V
		"finiTipBias" : 1.0, 		// unit: V
		"finiSetPoint" : 2.0, 		// unit: V
		"numLine" : 128
	},
 
	addChannels : function (chArray){
		for (var i in chArray){
			print('Add Ch: ' + chArray[i])
			spm.scan.channels.add(chArray[i])	
		}
		spm.scan.triggerLineScan()
		spm.sleep(50)
		spm.scan.stop()
		spm.sleep(50)
		spm.scan.stop()
	},
 
	clearChannels : function (){
		var chs = spm.scan.channels.names() 
		for (var i=0; i < chs.length; i++){
            		print('Remove Ch: '+chs[i])
            		spm.scan.channels.remove(chs[i])
            	}
	},
	calStep : function (init, fini) {
		return (fini - init) / this.parameters.numLine
	},
 
	update : function (aTipBias, aSetPoint) {
		spm.bias.tip = aTipBias;
		spm.zservo.setpoint.normValue = aSetPoint;
		print("Set Params. >> Bias (V): " + aTipBias + ", SetPoint (V): " + aSetPoint)
	},
	init: function () {
		app.setPrintOutputVisible(false)
		this.clearChannels();
		this.addChannels(this.parameters.channels);
		spm.sleep(2000)
	},
 
	fini: function () {
		this.update(this.parameters.initTipBias, this.parameters.initSetPoint)
	},
	run : function (Detector){
		this.init();
		var tipBias = this.parameters.initTipBias;
		var setPoint = this.parameters.initSetPoint;
		var stepTipBias = this.calStep(tipBias, this.parameters.finiTipBias);
		var stepSetPoint = this.calStep(setPoint, this.parameters.finiSetPoint);		
		print("Tip Bias Step: " + stepTipBias);
		print("SetPoint Step: " + stepSetPoint);
 
		this.update(tipBias, setPoint);
		spm.addChannel("aux2 in")
		spm.scan.triggerImageScan();
		spm.sleep(60);
		while (spm.scan.isImageScanning){
			var LineSync = spm.readChannel("aux2 in") 
			result = Detector.detect(LineSync.value)
			if (result.falling){
				tipBias += stepTipBias;
				setPoint += stepSetPoint;
				this.update(tipBias, setPoint);
				print("Line change")
			}
			spm.sleep(1/this.parameters.samplingRate);
		}
		this.fini()
		print("Done Image Scan")
	}
};
 
$AsyncScan.parameters.numLine = 256;
$AsyncScan.run(EdgeDetector)
Comments (0)

No comments yet. Be the first to comment!

Snippet Information
Author: jungyu.lee
Language: PowerScript
Created: Jan 08, 2026
Updated: 0 minutes ago
Views: 16
Stars: 0