Optic Control#
The feature is based on the SLP protocol that enables cameras, light devices, and optic devices (e.g., a lens) to communicate directly with each other.
Info
Documentation for legacy Basler lights without ILC and for discontinued Basler Camera Lights is no longer included in the current Basler Product Documentation. If you need information about these products, refer to the offline documentation of an earlier release, that you can download by clicking here.
Info
For dart M camera modules, the Optic Control feature is currently only available if you use a Basler dart M interface board (either the Interface Board dart M GigE RJ45 PoE or the Interface Board dart M GigE RJ45 AUX).
Using the Feature#
To use the Optic Control feature, you need the following:
- A Basler Lens with autofocus functionality.
For information about a suitable lens, see the Optotune Lenses in the Partner Liquid Lens topic or the Computar LensConnect Series with Variable Focal Length section in the Partner Motorized Lenses topic. - A suitable Basler Lens Sync Cable to connect an optic lens to a camera. The sync cable allows you to attach a light device and a lens, and control them via a single GPIO port of the camera. Which Basler Sync Cable you need depends on the peripheral devices you want to operate.
For information about the available cable variants and their supported use cases, see Basler Sync Cables. - A compatible camera running firmware that includes the Peripheral Device Control feature.
Initializing Optic Devices#
Before you can use an optic device, it needs to be initialized.
To initialize your optic device:
- Follow the steps described in Preparing Peripheral Devices and Checking and Clearing Errors.
- Select the optic device you want to control.
For the first optic device, setOpticControllerSelectortoOptic1. - Execute the
OpticControllerInitializecommand to initialize the attached lens.
Depending on the lens type, you may notice its internal motors moving. Initialization time also depends on the lens type. - Monitor the
OpticControllerStatusparameter. When initialization is complete, the parameter should have the valueReady. If it doesn't, see Checking and Clearing Errors below.
Depending on the connected lens, only the parameters supported by that lens are available for configuration.
The table below provides an overview of the possible statuses the optic controller inside the optic device can have.
| Value | Meaning |
|---|---|
| Ready | The optic device is ready for use. |
| Busy | The optic device is currently busy processing an operation and is temporarily unavailable for new commands. |
| NotConnected | The optic device isn't connected to the camera, or it cannot be detected. |
| NotInitialized | The optic device has not been initialized yet. To initialize the device, execute the OpticControllerInitializecommand. |
| Error | An error has occurred in the optic device. For details, see the Error Statuses table. |
| Initializing | The optic device is currently being initialized. During this process, the device may not be available for operation. |
Configuring the Focus#
Manual Focus#
Manual focus is best when you need to switch repeatedly between predefined settings. Unlike autofocus, it does not need to analyze multiple images, so it operates quickly. The speed depends on the lens type:
- Liquid lenses typically switch to a new focus in about 20 milliseconds.
- For motorized lenses, the speed depends on the size of the change:
Moving from the nearest to the furthest focus position takes significantly longer than switching between intermediate positions.
All lenses mentioned in Using the Feature above support focus control using the FocusStepper parameter. The parameter value has no direct physical meaning. Instead, the lens's supported focus range is mapped to an integer range, where 0 corresponds to the nearest focus distance and the maximum FocusStepper corresponds to the furthest focus distance.
Some calibrated lenses also support focus control using the FocalPower parameter. This parameter is a floating-point value that specifies the lens's optical power in diopters.
Autofocus#
The camera can automatically adjust the lens focus to achieve the best focus in the image. It uses images captured by the camera's image sensor.
Info
Autofocus
- Autofocus is image-based and operates within a user-defined ROI (Region of Interest). ROI 3 must be used for the autofocus.
- The autofocus ROI is subject to size limitations (maximum 256 x 256 pixels) and cannot cover the entire image area. As a result, autofocus evaluates only the image data within the configured auto function ROI rather than the complete image.
- The autofocus process requires the acquisition of multiple images. Consequently, the time needed to achieve focus depends heavily on the camera's frame rate. Higher frame rates generally result in faster autofocus operation.
Info
Image Requirements for Autofocus
- Autofocus requires sufficient contrast in the image to operate reliably.
- Uniform or low-contrast surfaces may not provide enough image detail for accurate focusing.
- Edges, barcodes, text, and other distinct structures can significantly improve autofocus performance and accuracy.
To use autofocus:
- The autofocus parameters are in the
Auto Function Controlcategory of the feature tree.
Open this category to adjust them. - On a color camera, use the
BslAutoFocusColorSelectparameter to select the color channel used for autofocus.
Because a lens's focal length also depends on the wavelength of the light, the best focus setting differs slightly for red, green, and blue light. For most applications, the default setting,Green, is a good choice. - Use
BslAutoFocusStepperLowerLimitandBslAutoFocusStepperUpperLimitto restrict the focus range that the camera searches.
If you know the distance range in which your objects will appear, set both limits to the correspondingFocusSteppervalues. This can significantly speed up autofocus. - Use the
FocusAutoModeparameter to select how the camera controls focus automatically.- In modes whose names start with
ImageBased, the camera analyzes image data to evaluate the focus of the current scene.ImageBasedis the default mode and is suitable for most situations. The camera captures images while stepping through focus positions to find the optimum. ImageBasedFastreduces the number of images captured, so autofocus finishes sooner, but it's less accurate and may miss the optimum under unfavorable conditions.ImageBasedAccuratecan improve accuracy, especially when used with the upper and lower focus limits.
- In modes whose names start with
- Use the Auto Function ROI feature to select ROI 3 used for autofocus.
ROI 3 for autofocus is limited to a maximum size of 256 x 256 pixels. A smaller value is possible. - Start grabbing images.
Autofocus requires image acquisition. - Set
FocusAutotoOnceto start autofocus.
The camera adjusts the focus while grabbing images. When the operation is complete, it setsFocusStepperto the best value found and resetsFocusAutotoOff.
Info
The FocalPower parameter is available only if the connected lens provides calibrated focal power values. Support for this parameter depends on the lens manufacturer.
Increasing Autofocus Speed#
To reduce the time required for autofocus, consider the following recommendations:
- Set focus limits (
BslAutoFocusStepperLowerLimitandBslAutoFocusStepperUpperLimit) to restrict the range over which autofocus searches for the optimal focus position. - Reduce the Image ROI during autofocus operation to maximize the achievable frame rate and allow autofocus to complete more quickly.
- Use a fast lens that supports rapid focus adjustments.
Configuring Aperture#
If the lens supports aperture control, you can use the ApertureStepper parameter to adjust the iris opening. The parameter value does not represent a physical aperture (f-number). Instead, it maps the lens's supported aperture range to an integer range, where a value of 0 corresponds to the widest aperture opening and the maximum ApertureStepper value represents the minimum aperture opening.
Configuring Focal Length#
If the lens supports focal-length control, use the FocalLengthStepper parameter to adjust its focal length, i.e., the zoom. The parameter value has no direct physical meaning. Instead, it maps the lens's supported focal-length range to an integer range, where 0 corresponds to the shortest focal length and the maximum the FocalLengthStepper value corresponds to the longest focal length supported by the lens.
After changing the focal length, the focus position may shift, so you may need to readjust the focus.
Checking and Clearing Errors#
Error Statuses#
The BslPeripheralDeviceErrorStatus parameter can take the following values:
| Value | Meaning |
|---|---|
| Communication Error | A communication error occurs if the data transmission between the optic device and the camera is faulty. This can be caused by using cables that are too long or by too much noise on the line. |
| Connection Lost | The connection to a previously connected optic device has been lost. This can happen, for example, if a cable has become loose. |
| No Error | No error was detected. |
| Not Connected | The optic device is not connected to the camera or has not been enumerated yet. Execute the BslPeripheralEnumerateDevices command to enumerate the device. |
| Unsupported Device Version | The optic device is not compatible with the current camera. Contact support to find out whether firmware updates are available. |
Sample Code#
Info
The code samples apply only to ace 2, boost R, and dart M cameras that include the Peripheral Devices Control feature in their firmware.
ace 2, boost R, dart M Cameras#
// Select and initialize the optic controller.
camera.OpticControllerSelector.SetValue(OpticControllerSelector_Optic1);
camera.OpticControllerInitialize.Execute();
// Set focus position manually using stepper (0 = nearest focus position).
camera.FocusStepper.SetValue(1000);
// Set focus position manually using focal power (0 = nearest focus position).
camera.FocalPower.SetValue(1.0);
// Set aperture (0 = maximum aperture opening).
camera.ApertureStepper.SetValue(50);
// Set focal length (0 = shortest focal length).
camera.FocalLengthStepper.SetValue(200);
// Configure the autofocus ROI.
camera.AutoFunctionROISelector.SetValue(AutoFunctionROISelector_ROI3);
camera.AutoFunctionROIUseFocus.SetValue(true);
camera.AutoFunctionROIWidth.SetValue(128);
camera.AutoFunctionROIHeight.SetValue(128);
camera.AutoFunctionROIOffsetX.SetValue(840);
camera.AutoFunctionROIOffsetY.SetValue(480);
// Use the green color channel for focus evaluation.
camera.BslAutoFocusColorSelect.SetValue(BslAutoFocusColorSelect_Green);
// Restrict the autofocus search range.
// Limiting the range speeds up the focusing process.
camera.BslAutoFocusStepperLowerLimit.SetValue(800);
camera.BslAutoFocusStepperUpperLimit.SetValue(1200);
// Configure autofocus behavior.
camera.FocusAutoMode.SetValue(FocusAutoMode_ImageBased);
// Start autofocus.
camera.FocusAuto.SetValue(FocusAuto_Once);
INodeMap& nodemap = camera.GetNodeMap();
// Select and initialize the optic controller.
CEnumParameter(nodemap, "OpticControllerSelector").SetValue("Optic1");
CCommandParameter(nodemap, "OpticControllerInitialize").Execute();
// Set focus position manually using stepper (0 = nearest focus position).
CIntegerParameter(nodemap, "FocusStepper").SetValue(1000);
// Set focus position manually using focal power (0 = nearest focus position).
CFloatParameter(nodemap, "FocalPower").SetValue(1.0);
// Set aperture (0 = maximum aperture opening).
CIntegerParameter(nodemap, "ApertureStepper").SetValue(50);
// Set focal length (0 = shortest focal length).
CIntegerParameter(nodemap, "FocalLengthStepper").SetValue(200);
// Configure autofocus ROI.
CEnumParameter(nodemap, "AutoFunctionROISelector").SetValue("ROI3");
CBooleanParameter(nodemap, "AutoFunctionROIUseFocus").SetValue(true);
CIntegerParameter(nodemap, "AutoFunctionROIWidth").SetValue(128);
CIntegerParameter(nodemap, "AutoFunctionROIHeight").SetValue(128);
CIntegerParameter(nodemap, "AutoFunctionROIOffsetX").SetValue(840);
CIntegerParameter(nodemap, "AutoFunctionROIOffsetY").SetValue(480);
// Use the green color channel for focus evaluation.
CEnumParameter(nodemap, "BslAutoFocusColorSelect").SetValue("Green");
// Restrict the autofocus search range.
// Limiting the range speeds up the focusing process.
CIntegerParameter(nodemap, "BslAutoFocusStepperLowerLimit").SetValue(800);
CIntegerParameter(nodemap, "BslAutoFocusStepperUpperLimit").SetValue(1200);
// Configure autofocus behavior.
CEnumParameter(nodemap, "FocusAutoMode").SetValue("ImageBased");
// Start autofocus.
CEnumParameter(nodemap, "FocusAuto").SetValue("Once");
// Select and initialize the optic controller.
camera.Parameters[PLCamera.OpticControllerSelector].SetValue(PLCamera.OpticControllerSelector.Optic1);
camera.Parameters[PLCamera.OpticControllerInitialize].Execute();
// Set focus position manually using stepper (0 = nearest focus position).
camera.Parameters[PLCamera.FocusStepper].SetValue(1000);
// Set focus position manually using focal power (0 = nearest focus position).
camera.Parameters[PLCamera.FocalPower].SetValue(1.0);
// Set aperture (0 = maximum aperture opening).
camera.Parameters[PLCamera.ApertureStepper].SetValue(50);
// Set focal length (0 = shortest focal length).
camera.Parameters[PLCamera.FocalLengthStepper].SetValue(200);
// Configure autofocus ROI.
camera.Parameters[PLCamera.AutoFunctionROISelector].SetValue(PLCamera.AutoFunctionROISelector.ROI3);
camera.Parameters[PLCamera.AutoFunctionROIUseFocus].SetValue(true);
camera.Parameters[PLCamera.AutoFunctionROIWidth].SetValue(128);
camera.Parameters[PLCamera.AutoFunctionROIHeight].SetValue(128);
camera.Parameters[PLCamera.AutoFunctionROIOffsetX].SetValue(840);
camera.Parameters[PLCamera.AutoFunctionROIOffsetY].SetValue(480);
// Use the green color channel for focus evaluation.
camera.Parameters[PLCamera.BslAutoFocusColorSelect].SetValue(PLCamera.BslAutoFocusColorSelect.Green);
// Restrict the autofocus search range.
// Limiting the range speeds up the focusing process.
camera.Parameters[PLCamera.BslAutoFocusStepperLowerLimit].SetValue(800);
camera.Parameters[PLCamera.BslAutoFocusStepperUpperLimit].SetValue(1200);
// Configure autofocus behavior.
camera.Parameters[PLCamera.FocusAutoMode].SetValue(PLCamera.FocusAutoMode.ImageBased);
// Start autofocus.
camera.Parameters[PLCamera.FocusAuto].SetValue(PLCamera.FocusAuto.Once);
/* Macro to check for errors */
#define CHECK(errc) if (GENAPI_E_OK != errc) printErrorAndExit(errc)
GENAPIC_RESULT errRes = GENAPI_E_OK; /* Return value of pylon methods */
/* Select and initialize the optic controller. */
errRes = PylonDeviceFeatureFromString(hdev, "OpticControllerSelector", "Optic1");
CHECK(errRes);
errRes = PylonDeviceExecuteCommandFeature(hdev, "OpticControllerInitialize");
CHECK(errRes);
/* Set focus position manually using stepper (0 = nearest focus position). */
errRes = PylonDeviceSetIntegerFeature(hdev, "FocusStepper", 1000);
CHECK(errRes);
/* Set focus position manually using focal power (0 = nearest focus position). */
errRes = PylonDeviceSetFloatFeature(hdev, "FocalPower", 1.0);
CHECK(errRes);
/* Set aperture (0 = maximum aperture opening). */
errRes = PylonDeviceSetIntegerFeature(hdev, "ApertureStepper", 50);
CHECK(errRes);
/* Set focal length (0 = shortest focal length). */
errRes = PylonDeviceSetIntegerFeature(hdev, "FocalLengthStepper", 200);
CHECK(errRes);
/* Configure autofocus ROI. */
errRes = PylonDeviceFeatureFromString(hdev, "AutoFunctionROISelector", "ROI3");
CHECK(errRes);
errRes = PylonDeviceSetBooleanFeature(hdev, "AutoFunctionROIUseFocus", 1);
CHECK(errRes);
errRes = PylonDeviceSetIntegerFeature(hdev, "AutoFunctionROIWidth", 128);
CHECK(errRes);
errRes = PylonDeviceSetIntegerFeature(hdev, "AutoFunctionROIHeight", 128);
CHECK(errRes);
errRes = PylonDeviceSetIntegerFeature(hdev, "AutoFunctionROIOffsetX", 840);
CHECK(errRes);
errRes = PylonDeviceSetIntegerFeature(hdev, "AutoFunctionROIOffsetY", 480);
CHECK(errRes);
/* Use the green color channel for focus evaluation. */
errRes = PylonDeviceFeatureFromString(hdev, "BslAutoFocusColorSelect", "Green");
CHECK(errRes);
/* Restrict the autofocus search range. */
/* Limiting the range speeds up the focusing process. */
errRes = PylonDeviceSetIntegerFeature(hdev, "BslAutoFocusStepperLowerLimit", 800);
CHECK(errRes);
errRes = PylonDeviceSetIntegerFeature(hdev, "BslAutoFocusStepperUpperLimit", 1200);
CHECK(errRes);
/* Configure autofocus behavior. */
errRes = PylonDeviceFeatureFromString(hdev, "FocusAutoMode", "ImageBased");
CHECK(errRes);
/* Start autofocus. */
errRes = PylonDeviceFeatureFromString(hdev, "FocusAuto", "Once");
CHECK(errRes);
# Select and initialize the optic controller.
camera.OpticControllerSelector.Value = "Optic1"
camera.OpticControllerInitialize.Execute()
# Set focus position manually using stepper (0 = nearest focus position).
camera.FocusStepper.Value = 1000
# Set focus position manually using focal power (0 = nearest focus position).
camera.FocalPower.Value = 1.0
# Set aperture (0 = maximum aperture opening).
camera.ApertureStepper.Value = 50
# Set focal length (0 = shortest focal length).
camera.FocalLengthStepper.Value = 200
# Configure autofocus ROI.
camera.AutoFunctionROISelector.Value = "ROI3"
camera.AutoFunctionROIUseFocus.Value = True
camera.AutoFunctionROIWidth.Value = 128
camera.AutoFunctionROIHeight.Value = 128
camera.AutoFunctionROIOffsetX.Value = 840
camera.AutoFunctionROIOffsetY.Value = 480
# Use the green color channel for focus evaluation.
camera.BslAutoFocusColorSelect.Value = "Green"
# Restrict the autofocus search range.
# Limiting the range speeds up the focusing process.
camera.BslAutoFocusStepperLowerLimit.Value = 800
camera.BslAutoFocusStepperUpperLimit.Value = 1200
# Configure autofocus behavior.
camera.FocusAutoMode.Value = "ImageBased"
# Start autofocus.
camera.FocusAuto.Value = "Once"
You can also use the pylon Viewer to easily set the parameters.