# Applied Scientific Instrumentation

### Products

 Custom Optics diSPIM FTP FW1000: Filter Wheel MPPI MS2000, MFC2000 and RM2000 Controller Piezo Systems RAMM, MIM and VTS Portal Serial Command Software TG-1000/Tiger Controller Tracking & Stabilization XY Stage Z-Drive and Focus Stage

video_autofocus

# Video AUTO-FOCUS

The ASI auto-focus system uses an electronic circuit to detect the high spatial frequency information present in a video signal coming from an analog camera, and converts it to a focus value for each frame – the better the focus, the larger the focus value. Depending on the sample and the type and power of the objective, the conditions to achieve the best focus value may change. To compensate, the system incorporates an auto-calibration routine that automatically sets various internal parameters to get a good focus value. The system also allows you to highlight any subset of the video field and focus on an object in it.

Relevant Auto-focus Connections
Screenshot of Video Monitor signal from the Auto Focus controller showing Sampled/Highlighted Area

When the controller is given an auto-focus command, it moves from a starting position to a stop position, constantly checking the focus signal. The focus signal goes through a filtering process to remove unwanted false focuses caused by noise and other disturbances. In “Normal” mode, the controller takes the filtered signal and watches for the maximum value. When a new maximum value is detected, the controller keeps track of the position where it was found. When the end of the scan is reached, the controller goes back to the position that had the highest value.

Fig 2: A “Normal” auto-focus scan showing a stage’s focus-axis position in tenths of microns, and the focus signal and filtered focus signal (focus-value) extracted from input video signal.

There is also a “Hill Detect” mode available. In this mode, the auto-focus controller watches for the focus value to increase in value and then start to decrease, thus detecting a hill. At this point, the auto-focus controller stops the scan and goes back to the position that had the highest focus value found on the hill. This is somewhat subject to false focusing and should be used only on high contrast single-layer samples with sharp edges.

## AUTO-FOCUS ALGORITHM

An actual auto-focus is initiated with the AFOCUS (or AF) command without any arguments; however, the system should first be set up. Auto-focus is configured by the AFOCUS (or AF) command with arguments specifying the speed at which to scan, the travel distance to be scanned, and the scan type. All or none of these arguments can be entered when issuing the command, as the auto-focus controller remembers the last parameters given and will only change the parameters specified in the new AF auto-focus command. This allows the user to repeat the last command without having to resend all the arguments.

The AFOCUS (or AF) arguments affect the auto-focus configuration in the following ways:

AF X= [% of speed] Y= [travel distance in mm] Z= [Hill Detect enable] F= [Hill Offset]

% of speed: Speed can be set with the X parameter in a range from 1 to 100. If the X parameter in the command is not specified or the value of X=0 is entered, the last value given will be used. (The default value of 10 will be used if nothing has been entered.)

The hardware auto-focus circuit generates a new focus value for every video frame (every 16 milliseconds for an NTSC/RS170 camera). This acquisition rate, along with the scan speed, determines the axial resolution obtainable with the auto-focus system. For instance, if the scan rate is 0.1 mm/sec, then the distance between focus values will be

$$0.1 \frac{mm}{second} × 0.016 \frac{seconds}{frame} = 1.6 \frac{µm}{frame}$$

This speed would give roughly a 1.6 µm focus resolution. Typically, ASI’s focus drives have a maximum auto-focus speed of about 0.6 mm/second, so this resolution would be obtained with the scan speed set to approximately 16 (% of maximum). For a high power objective lens, you may want to use a scan speed of perhaps 5 (% of maximum), to yield a resolution of about

$$5\% × 0.6 \frac{mm}{second} × 0.016 \frac{seconds}{frame} = 0.48 \frac{µm}{frame}$$

The trade-off when scanning at a slower speed for better resolution is the extra time it takes to complete the scan. For the fastest operation, pick the maximum speed that gives you the resolution you require and choose a small travel distance.

Travel Distance: Entered in millimeters, this is the distance the focus drive is allowed to move while looking for the best focus value, with one exception: when correctly zeroed before executing the AFOCUS (or AF) command, the drive will not travel more negative than 200 µm from its zero position. This optional parameter in the AFLIM (or AL) command is designed to prevent damage to the sample or to the optics.

Type of Search: Two different types of search modes are available: Normal and Hill Detect.

Normal: Enabled when AF Z=0. In this mode, the controller moves down 1/2 the travel distance, scans up the full travel distance, and then returns to the position that had the peak focus value. This is the Default mode.

Hill Detection: Enabled when AF Z=1. In this mode, the controller moves down 1/2 the travel distance, then starts to scan up until the focus value rises from its lowest value to a peak then begins to decrease back to a lower value again. When this hill is detected, the controller will return to the position that had that maximum focus value at the top of the hill. This search is generally used to find the first focus point. Note that if no hill is found, the controller will return to the highest focus value detected after reaching the end of the full travel distance.

The Hill Offset value is the percentage that the focus value must decrease from its recent maximum before the scan is satisfied that a hill has been detected.

## AUTO-FOCUS CONFIGURATION

To ensure safe and proper operation during auto-focus moves, perform the following procedure:

1.Focus the sample manually and then press the ZERO button on the controller. When correctly configured, the controller will never auto-focus to a position greater than 200 µm in the negative direction from the 0000 positioned. (This safety feature may be disabled by the AFLIM (or AL) command ) By zeroing the controller at a typical focus location, you should have a reasonable range for the auto-focus to operate yet still protect an objective from crashing into the sample (Fig 3). You can also use the HERE Z=0 command to zero the Z-axis position with software, if Z is the focus axis.

Fig 3: Set objective farther than 200 microns from the sample, then set the focus drive’s position to ZERO.

2.Insure that DIP Switch 2 on the back of your controller to UP position (Fig. 4). Your LCD will now display the focus value marked as F:#### on its bottom line. This number will always be between 0000 and 2047. 0000 indicates that little or no video signal is getting to the auto-focus card; check your connections. If the focus value reads 2047, then the system is saturated with too much signal; try adjusting the Video Gain, Brightness, and Contrast controls on your camera if available. If this does not help, then go to step 4.

Fig 4: Shows the focus value as F:1696 on a two-line LCD display (a) and as F:1904 on a three-line LCD display (b). Dip Switch 2 on the back of a controller in the UP position ©.

3.By focusing through your sample, you can watch the focus value change on the LCD screen. When the sample is held steady, the focus value on the LCD should remain steady; ideally, only the last digit should fluctuate by one or two counts. This is due to slight noise in the system and is tolerable. If the fluctuations are bigger, check the connections for any problems.

4. AFCALIB X= defines the contrast parameter for the auto-focus system. During an auto-focus run, if the controller finds the maximum and minimum focus value less than the contrast, it declares the run a failure and returns to the starting position. The AFC X value should always be more then the focus value fluctuation that you saw in step 3. Default value is 10.

5.Next, using the AFOCUS command, pick the auto-focus speed and travel distance suitable for your sample type, objective, and optics. A “Normal” focus on the slowest speed is often the best option – we recommend a speed less than 20 (% of the maximum speed of your focus controller). If you anticipate always being near the focus position, you can set a 10 to 50 micron range and swiftly get to the best focus in that range. The command for a 50 micron travel range would be:

AF X=5 Y=0.05 where AF X= [% of speed] Y= [travel distance in millimeters]

6.Next, issue an AFCALIB (or AFC) command without parameters. This will begin the Auto Calibration routine. The AFC routine will carry out a series of focus scans to automatically find and set various internal parameters to get a large focus value and enough contrast to avoid any false positives. It uses the speed and travel distance currently set by the AF command. It does not use the Hill Detect enable or Hill Offset settings – the Auto-Calibration routine will perform its scans in “Normal” mode over the full travel distance.

7.Finally, perform a regular auto-focus run using the AFOCUS command to see if the new settings work. If the results are satisfactory, you can save these settings with an SS Z command, otherwise they will revert to their previous values upon reset or power down.

8.If the focusing is inconsistent, try the Manual Calibration steps found in the following section. For additional help contact Support[at]ASIimaging[dot]com.

## MANUAL CALIBRATION

The AFCALIB (or AFC) Auto Calibration routine tunes the system moderately so that the system works for most samples and objectives. Try the following steps for better results:

1.Manually focus the sample and insure that DIP Switch 2 on the back of your controller is in the UP position. It will now display the focus value marked as F:#### on the bottom line of your LCD. This number will always be between 0000 and 2047.

2.Next, set AFADJ X=0 Y=0 Z=0

The Y value sets an internal digital potentiometer that controls the video signal amplitude. Its range is from 0 to 100. A value of 0 completely attenuates the video signal, while 100 permits 100% of the video signal to be used.

With Y set at 0, there is no video signal in the system, therefore the focus value should also be 0000. However, due to various reasons like noise, initial offsets, etc., the focus value as displayed on the LCD may larger. The X value of the AFADJ command sets a “zero-adjust potentiometer”. Starting from 0, slowly increase the X value in steps of 5 to 10 until the focus value reads as close as possible to 0000. When you find the right value, you will have set up the auto-focus system such that, with zero video signal, the focus value is also almost zero. At this point, the focus value will be totally dependent on the video input and not on any internal noise or offsets. The value of X you find depends solely on the controller electronics and should not vary with your samples, your optics, or your camera setup.

If this step is not performed, over time, the internal noises and offsets will accumulate, and the focus value will ramp up and saturate.

The Z value sets the gain of an Analog-to-Digital converter (ADC) which is connected to an internal integrator that converts spatial frequencies into the focus value. A value of 0 indicates a gain of 1x – for now leave it set at 0.

3.Now, to tune other parameters, insure that your camera is connected and feeding video into the auto-focus card. Allow some video signal into the system by increasing the Y value of the AFADJ command to approximately 90.

4.The AFLIM (or AL) command’s X and Y values adjust the highlighted or sampled video area as seen on the Video OUT port when viewed on a monitor. Only the highlighted video frame area is used for auto-focusing. The X and Y range is from 0 to 100. A value of 0 highlights 0% of the respective X or Y axis, while 100 highlights almost 100% of the respective axis. Select a value such that the sample object you are interested in falls in the highlighted area and any unwanted artifacts are excluded. You can set the X and Y values generously: the larger the sampled area, the better the focus value.

By default, the highlighted area is centered in the video frame. If you want to offset this area, carefully adjust the two potentiometers mounted on the auto-focus circuit board.

5.Next, set the AFADJ Z value, i.e. the ADC gain. Setting AFADJ Z=1 will double the focus value as seen on the LCD screen, Z=2 will quadruple, and Z=3 will octuple. When you feel you have found a right value, try moving your sample in and out of focus to check that the focus value does not rise to 2047. When the focus value reads 2047, the system is saturated – try turning the gain down to the next lower value.

Z=0 1x
Z=1 2x
Z=2 4x
Z=3 8x

6.Now re-adjust AFADJ Y value to fine-tune the video strength. Increase Y value until it just reaches the 2047 ceiling, and then decrease it a little. When you feel you found a right value, try moving your sample in and out of focus and check that the focus value is not saturating at the 2047.

7.The auto-focus system works well when the difference between the in-focus focus value and the out-of-focus focus value is large. The contrast value, as set with the AFCALIB X command, should be larger than the focus value fluctuations seen when the sample is held steady, but smaller than the difference between the in-focus and out-of-focus focus values. The correct contrast value helps avoid false focusing. If the controller finds the difference between the maximum and minimum focus values to be less than the contrast, it declares the run a failure and returns to the starting position.

8.If the system consistently settles at a spot that is a bit off the focus or the image is not sharp enough, try adjusting the AFCALIB Y or frame offset value. Since the focus controller’s speed and video frame rate are not synchronized, the auto-focus controller uses the frame offset value to calculate the correction needed to move the sample to the correct focus position. For NTSC cameras used with ASI’s motorized focus controllers or piezo-Z stages, the frame offset is commonly between 3.5 and 4.

9.Another parameter that can help with better scanning is the scan speed as set with the AFOCUS (or AF) command’s X parameter which ranges from 1% to 100% of maximum of the focus drive’s speed. A slower speed takes a longer time but ensures better focus results. Typically, ASI’s focus drives have a maximum auto-focus speed of about 0.6 mm/second.

## TROUBLESHOOTING AND OPTIMIZATION

##### Focus value on the LCD ramps up and saturates when the sample is motionless and the focus controller is quiescent

This problem occurs when the Zero Potentiometer (set by X argument of AFADJ) is incorrectly set. This can be corrected by running the Auto Calibration routine (AFCALIB or AFC) or by performing Step 2 of Manual Calibration .

##### Hill Detect does not work well.

Two parameters that greatly influence Hill Detect performance are Hill Offset (set by the F argument of AFOCUS or AF) and Contrast (set by X argument of AFCALIB or AFC).

If the Hill Detect continues to scan the sample for considerable time after passing the best focus, it could be caused by large AF Hill Offset percentage – try decreasing it.

If the Hill Detect returns a false focus, try increasing the AFC’s contrast value or try increasing the AF’s Hill Offset.

##### Stage returns to a position just off the best focus spot.

There are two things you can do to improve this: try rescanning with lower speed setting (set by the X argument of AFOCUS or AF) or try changing the frame offset (set by Y argument of AFCALIB or AFC).

To determine which option is best for you, try the following: manually focus through the sample and stop at the spot where you read the highest focus value (F:#### value on bottom line of your LCD). Store this position by pressing the ZERO button or by issuing the ZERO or Z software command. Now run auto-focus with the AFOCUS (or AF) command or by pressing the @ button on the controller a couple of times. If the focus controller consistently returns around the same incorrect position, then changing the frame offset is a good solution. On the other hand, if it randomly returns to a position on the + or - side of 0.00000, then reducing the speed is your best option.

Frame offset is a constant that depends on your camera’s frame rate and your focus drive’s speed. This setting compensates for time lags inherent in the video processing. The default value for an ASI motorized focus controller and an NTSC camera is 3.5, however. If you were using a faster focus drive such as ASI’s piezo-Z stage, your frame offset would be about 3.75. If you are using a digital camera in progressive scan mode, your frame offset could be as high as 4.

##### Unfocused spot has a higher focus value than a focused spot.

Here are a few things you can try when the focus value reads a higher value at a defocused spot than at a focused spot:

• Turn off any Automatic Gain Control (AGC) settings on your video system and use a Manual or Fixed Gain setting instead.
• The illumination might be insufficient; increase the lighting on your sample.
• Limiting the background might help; decrease the highlighted screen area with AFLIM (or AL) command.

## AUTO-FOCUS High Level COMMANDS

Apart from the core MS2000 commands like Command:MOVE,Command:MOVREL, Command:WHERE etc , below is a list of AutoFocus module specific commands.

 Command:AFADJ 2016/09/07 22:03 Command:AFCALIB (AFC) 2018/02/07 22:14 Command:AFINFO 2016/09/07 22:03 Command:AFLIM (AL) 2018/02/07 22:15 Command:AFMOVE (AM) 2018/02/07 22:15 Command:AFOCUS (AF) 2018/02/07 22:11

## Auto-Focus Low Level Command (version 8.8+)

Low level command set is ideal Ms2000 interface for scripts/non humans. Since the commands are so rigid and streamlined, parsing on both sides should be a lot easier. Most frequently used autofocus commands are implemented in the low level command set. All values specified through this section of the manual are hexadecimal.

### Command: Perform Autofocus

Byte# Function Possible Values
1 Axis Identifier 0x18/0x19/0x1A/ 0x1B
2 Command 0x5A
3 Termination 0x3A

1st Byte: is the Axis Identifier, this can be any of the valid axis identifier (0x18/0x19/0x1A/ 0x1B). The MS2000 already knows the Autofocus axis. We only have it, to keep it consistent with rest of the low level command set. 2nd Byte: is the command. Its 0x5A, same as the Autofocus scan edit command, when this is followed by the termination byte, Ms2000 knows to perform autofocus command. 3rd Byte: is the termination character “:” in ASCII or 0x3A in hex. If not received, MS2000 ignore the command.

Response: 0x01 or 0x02 (At the end of the autofocus scan, either 0x01 or 0x02 are returned. 0x01 means scan complete and successful. 0x02 means scan complete but failed to find best focus spot)

Example 0x18 0x5A 0x3A

Byte# Function Possible Values
1 Axis Identifier 0x18/0x19/0x1A/ 0x1B
2 Command 0x5B
3 Termination 0x3A

1st Byte: is the Axis Identifier, this can be any of the valid axis identifier (0x18/0x19/0x1A/ 0x1B). The MS2000 already knows the Autofocus axis. We only have it, to keep it consistent with rest of the low level command set. 2nd Byte: is the command. Its 0x5B. Always returns 8 bytes corresponding to various autofocus settings. 3rd Byte: is the termination character “:” in ASCII or 0x3A in hex. If not received, MS2000 ignore the command.

##### Response:
Byte# Function Example 1 Example 2
1 LSB of autofocus scan range in 10th of a micron0xD0 0xE8
2 MSB of autofocus scan range in 10th of a micron0x07(0x07DO is 2000 in decimal..so 0.2mm) 0x03(0x03E8 is 1000 in decimal..so 0.1mm)
3 Autofocus Scan speed percent0x0A (scan speed is 10%) 0x05 (scan speed is 5%)
4 Current Autofocus mode 0x00 (normal or split scan mode) 0x01 (scan mode is Hill Detect )
5 Hill Offset percent 0x46 (Is 70% hill offset) 0x3C (Hill Offset is 60%)
6 AFMOVE enable (when enable performs an autofocus automatically after each move) 0x00 0x01
7 LSB of Contrast Value 0x0A 0xC8
8 MSB of Contrast Value 0x00 (Contrast of 10) 0x00 (Contrast of 200)

Example 0x18 0x5B 0x3A
Reply 0xD0 0x07 0x0A 0x00 0x46 0x00 0x0A 0x00

#### Command: Edit Autofocus Settings

This command lets you edit the autofocus scan range, hill offset percent, autofocus mode and autofocus scan speed.
1st Byte: is the Axis Identifier, this can be any of the valid axis identifier (0x18/0x19/0x1A/ 0x1B). The MS2000 already knows the Autofocus axis. We only have it, to keep it consistent with rest of the low level command set.
2nd Byte: is the command. Its 0x5A, same as the Autofocus scan command, when this is followed by other bytes and not the termination byte, Ms2000 knows that it’s the edit version of the command
3rd Byte: Number of bytes following, notifies the controller of the number of bytes following this byte.
4th Byte: When set to 0x01, only edits the settings, when set with 0x02 edits the settings and performs an autofocus scan.
5th and 6th Byte: Are the Least Significant byte and Most Significant Byte of the autofocus scan range entered in units of 10th of a micron. Maximum possible value is 0xFFFF or 65535(in decimal) or 6.5535mm
7th Byte: is Autofocus scan speed percent. Only accepts values between 1 to 100 (0x64 in hex).
8th Byte: is Autofocus scanning mode. When set to 0x00 performs the normal/split autofocus scan. When set to 0x01 perform the scan in Hill Detect mode
9th Byte: is the Hill Offset percent
10th Byte: AFMOVE enable. When set to 0x01 performs an autofocus scan after every command move. Can only be either 0x00 or 0x01.
11th and 12th Byte: LSB and MSB of the Contrast Value. If the controller finds the difference between the maximum and minimum focus value to be less than the contrast value, it declares the run a failure and returns to the starting position. Maximum value allowed is 0x07D0 (ie 2000 in decimal)
13th Byte: is the termination character “:” in ASCII or 0x3A in hex. If not received, MS2000 ignores the command.

##### Response:

0x01 or 0x02 when 4th byte is 0x02 or else no acknowledgement is give

Byte# Function Example 1 Example 2 Example 3
1 Axis Identifier 0x18 0x18 0x18
2 Command 0x5A 0x5A 0x5A
3 No of bytes following 0x03 0x04 0x09
4 Perform an Autofocus scan after edit. 0x01 0x02 0x02
5 LSB of scan range 0xE8 0xD0 0xE8
6 MSB of scan range 0x03 0x07 0x03
7 Scan speed percent - 0x8C 0x0A
8 Autofocus Mode - - 0x00
9 Hill Detect Offset percent - - 0x3C
10 AFMOVE enable - - 0x01
11 LSB of Contrast Value - - 0x0A
12 MSB of Contrast Value - - 0x00
13 Termination 0x3A 0x3A 0x3A
Command string 0x18
0x5A
0x03
0x01
0xE8
0x03
0x3A
0x18
0x5A
0x04
0x02
0xD0
0x07
0x8C
0x3A
0x18
0x5A
0x09
0x02
0xE8
0x03
0x0A
0x00
0x3C
0x01
0x0A
0x00
0x3A
Result Only edits the autofocus scan range to 0.1mm and does not perform a autofocus scan in the end. Sets the autofocus scan range to 0.2mm, ignores the edit of scan speed as the parameter is out of range and performs an autofocus scan. Sets scan range to 0.1mm; Sets to Normal Mode; Sets scan speed to 10%;Sets Hill Detect offset to 60%. Sets Contrast Value to 10

If you just wanted to edit the scan range, then you can only update the 2 scan range bytes. But if you want to just edit the Hill Offset percent and leave all other settings unchanged, you still have to enter proper values for the bytes before it.

Example 0x18 0x5A 0x03 0x01 0xE8 0x03 0x3A
Example 0x18 0x5A 0x04 0x02 0xD0 0x07 0x8C 0x3A