Writing a PLC program

A simple PLC program for continuous image acquisition from a camera or a file source includes the following:

Variables

PROGRAM MAIN
VAR
    hr                :    HRESULT;
    fbCamera          :    FB_VN_SimpleCameraControl;
    eState            :    ETcVnCameraState;
    ipImageIn         :    ITcVnImage;
    ipImageInDisp     :    ITcVnDisplayableImage;
    nNewImageCounter  :    UINT;
END_VAR

Name

Type

Description

hr

HRESULT

All functions and many methods of the Tc3_Vision library return a result code of type HRESULT for error handling. Further information about the concept behind it and the meaning of individual codes can be found under HRESULT.

fbCamera

FB_VN_SimpleCameraControl

Each camera and File Source Control is represented by a function block instance. In the simplest case this is an instance of the FB_VN_SimpleCameraControl, which covers the common properties of camera and File Source Control and can therefore be linked with both device types. Alternatively, FB_VN_GevCameraControl can be used for cameras and FB_VN_FileSourceControl for File Source Controls.

eState

ETcVnCameraState

Each Vision device has a state of type ETcVnCameraState.

ipImageIn

ITcVnImage

Images for processing are managed using ITcVnImage variables. In this sample, access to the camera input image is gained via ipImageIn.

ipImageInDisp

ITcVnDisplayableImage

Images for display are managed via ITcVnDisplayableImages.

Program code with state machine

Each Vision device has an internal state, TCVN_CS_INITIAL being the initial state. From this, the device can be brought into the TCVN_CS_ACQUIRING state using the StartAcquisition method. In this state, images can be captured and transferred. If an image is completely stored in the memory, it can be retrieved via fbCamera.GetCurrentImage(ipImageIn).

To check if an image was received completely and could be retrieved, the IF query queries the HRESULT of the method and whether the pointer to the memory area of the image is not 0. After that, you can start analyzing the image. To check and indicate that an image could be retrieved, the counter variable nNewImageCounter is incremented as an example.

F_VN_TransformIntoDisplayableImage then moves the image pointer to ipImageInDisp to display the image. The image can then no longer be retrieved via ipImageIn. Alternatively, a copy can be created with F_VN_CopyIntoDisplayableImage to display and edit the image.

eState := fbCamera.GetState();

IF eState = TCVN_CS_ERROR THEN
    hr := fbCamera.Reset();

ELSIF eState < TCVN_CS_ACQUIRING THEN
    hr := fbCamera.StartAcquisition();

ELSIF eState = TCVN_CS_ACQUIRING THEN
    hr := fbCamera.GetCurrentImage(ipImageIn);

    // Check if new Image was received
    IF SUCCEEDED(hr) AND ipImageIn <> 0 THEN
        nNewImageCounter := nNewImageCounter + 1;

        // Place to call vision algorithms
        hr := F_VN_TransformIntoDisplayableImage (ipImageIn, ipImageInDisp, hr);
    END_IF
END_IF

Further information on the state machine can be found in the API chapter Image Acquisition.

Further information

Next step

Initializing a function block