Use Case: IF_TargetsHandlerEventsListener

Overview

You can implement the interface IF_TargetsHandlerEventsListener and pass it as an input to the method SubscribeEventsListener. It allows you to intercept the occurrences of events related to the behavior of the function block FB_TargetsHandler.

For example, every time a target is added to a targets handler, the related OnAddTarget method is called for each subscribed events listener, passing the information on the new target as an input.

The following two examples show common uses of such an interface.

Extending the Data Linked to Each Target in the List

Each target is represented by a structure ST_RobotTarget that contains a minimum set of information.

One common requirement could be to extend such data with additional user data. The interface IF_TargetsHandlerEventsListener can be used for such purpose.

Example:

If you use a camera system that can estimate features of the detected products, such data can be linked to each target in the list.

For a camera system that can detect the color from a list of possible colors and that can estimate the weight of each product, the custom structure can be as follows:

TYPE ST_ProductExtendedData :
STRUCT
    //possible values for ET_Color are
    //[None, Red, Green, Blue, Yellow, Cyan, Magenta]
    etColor : ET_Color;
    lrWeight : LREAL;
    xValidData : BOOL;
END_STRUCT
END_TYPE

The field xValidData is optional and has been added to provide direct feedback on the validity of the information stored inside the structure. After you have defined the structure, you can create an array of this extended data that has the same size as the array of targets contained inside the function block FB_TargetsHandler:

G_astProductExtendedData : ARRAY [1..Gc_udiMaxNumberOfTargets] OF ST_ProductExtendedData;

As a next step, implement the interface IF_TargetsHandlerEventsListener.OnAddTarget so that every time a new target is added to the list, the additional information is filled at the proper index of the external array astProductExtendedData.

To achieve this, create a temporary variable when the additional information coming from the camera is stored. This variable will then be used inside the OnAddTarget method to store the information at the correct list index.

G_stLastProductExtendedData : ST_ProductExtendedData;

In the body of the implementation of IF_TargetsHandlerEventsListener.OnAddTarget, insert the following code:

G_astProductExtendedData[i_stTarget.udiListIndex].etColor := G_stLastProductExtendedData.etColor;
G_astProductExtendedData[i_stTarget.udiListIndex].lrWeight := G_stLastProductExtendedData.lrWeight;
G_astProductExtendedData[i_stTarget.udiListIndex].xValidData := TRUE;

Where i_stTarget is an input of type ST_RobotTarget that contains the information of the target that has been added to the list, including the list index (i_stTarget.udiListIndex).

This has the effect that, every time a new target is added to the list, the additional data is also written to the external structure at the same index.

Equivalently, you can implement the method IF_TargetsHandlerEvent­sListener.OnRemoveTarget so that every time a target is removed from the list of the targets handler, the data in the external list is also cleaned.

G_astProductExtendedData[i_stTarget.udiListIndex].etColor := ET_Color.None;
G_astProductExtendedData[i_stTarget.udiListIndex].lrWeight := 0.0;
G_astProductExtendedData[i_stTarget.udiListIndex].xValidData := FALSE;

Adding Application Logger Diagnostics

The targets handler function block does not provide a built-in logger point (such as APL.FB_LoggerPoint) to be linked to the Application Logger.

Nevertheless, you can add custom diagnostics to the function block by implementing the interface IF_TargetsHandlerEventsListener and generating the desired diagnostics depending on the specific event.

Example:

Implement the interface IF_TargetsHandlerEventsListener so that it contains an instance of APL.FB_LoggerPoint and register the logger point to the Application Logger. In order to generate some diagnostics every time a new target is added to the list, implement IF_Target­sHandlerEventsListener.OnAddTarget as indicated in the following code example:

fbLoggerPoint.AddLogEntry(
    i_etLogLevel := APL.ET_LogLevel.DebugMessage,
    i_etDiag := GD.ET_Diag.Ok,
    i_udiDiagExt := 0,
    i_sMessage := CONCAT('New target with Id ', TO_STRING(i_stTarget.udiUnivocalId))
);