Supported platforms: CODESYS 3.5, CODESYS 3.5 SAFETY

 

CANopenHB (FB)

Description

This function block implements both Heartbeat producer and consumer.

 

HB Producer

Sends current NMT status to CAN bus according to the Producer Heartbeat Time (i.e. SDO index 16#1017).

 

HB Consumer

Registers a callback FBs to all listed heartbeat producers.

 

Limitations

NMT and OD protocols must also be added to protocol stack and initialized before initializing HB with Init method.

 

When Init method is called, it sends the first HB message, i.e. 'boot-up' message. The NMT status should be NMTState.BootUp at that time.

 

Init (METHOD)

Initializes HB protocol and adds OD indexes:

Sends the first HB message.

Inputs

Input variable name

Data type

Description

i_pDevice

POINTER TO Device

Pointer to device

Outputs

Output variable name

Data type

Range

Description

o_Error

Errors (ENUM)

 

Protocol error.

Error diagnostic

Error

Reason

ODFull

Object dictionary is full. Cannot add index.

COBIDAlreadyRegistered

COB-ID is already registered.

CallbackBufferFull

Number of registered COB-IDs exceeded G_MAX_NUMBER_OF_COBIDS when initializing callback.

InterfaceError

Device channel interface missing, or low level (CL2) library returned error.

NotInitialized

CAN channel not initialized, or not connected to given CANopen device.

InputError

Given pointer NULL or protocol data missing.

NoError

No errors.

 

AddCallbackToList (METHOD)

This method registers a callback handler for consumed heartbeat message COB-ID.

Inputs

Input variable name

Data type

Description

i_pDevice

POINTER TO Device

Pointer to device

i_pCallback

POINTER TO CANopenHBCallback

Pointer to HB consumer handler instance.

Outputs

Output variable name

Data type

Range

Description

o_Error

ENUM

Errors

Protocol error.

Error diagnostic

Error

Reason

ProtocolDataMissing

HB protocol not added to stack.

InputError

Given device or instance pointer null.

InterfaceError

Device channel interface missing, or low level (CL2) library returned error.

NoError

No errors.

 

Update (METHOD)

This method

Inputs

Input variable name

Data type

Description

i_Enable

BOOL

Enable protocol operation. NOT IMPLEMENTED.

Outputs

Output variable name

Data type

Range

Description

o_Error

Errors (ENUM)

 

Protocol error.

Error diagnostic

Error

Reason

ProtocolDataMissing

NMT or HB protocol not added to stack.

InterfaceError

Device channel interface missing, or low level (CL2) library returned error.

NotInitialized

Protocol has not been initialized, or null device pointer given to init..

NoError

No errors.

 

Main FB

The function block instance only contains protocol data. It has no functionality and no input or output parameters.

 

 

Example code

This function block is included in MultiTool Creator code template if CANopen is selected to use.

 

Definitions:

 

Device: EPEC_CANopen.Device;

Protocols: ARRAY[1..n] OF EPEC_CANopen.ProtocolEntry;

ProtocolAdded: ARRAY[1..n] OF BOOL := [n(0)];

 

HBCTimes: ARRAY[1..1] OF UDINT := [UDINT#400];

HB_Error: EPEC_CANopen.Errors := EPEC_CANopen.Errors.NotInitialized;

 

HBCallbacks: ARRAY[1..1] OF EPEC_CANopen.CANopenHBCallback := [

    (i_NodeID := 2, i_Time := HBCTimes[1], i_pNext := 0)];

 

HB: EPEC_CANopen.HBData; (* Protocol data *)

HB_Handler: EPEC_CANopen.CANopenHB; (* Protocol handler *)

 

 

 

Init:

 

 

HB.HeartBeat.PT := T#200MS; (* HB producer time *)

HB.pHBCTimes := ADR(HBCTimes); (* HB consumer times *)

 

Protocols[3].Protocol:=EPEC_CANopen.ProtocolType.HB;

Protocols[3].pData:=ADR(HB);

Protocols[3].itfProtocol:=HB_Handler;

Protocols[3].pError:=ADR(HB_Error);

 

ProtocolAdded[3] := CANopenDeviceAddProtocol( (* Add to protocol stack *)

    i_pDevice:=ADR(Device),

    i_pProtocol:=ADR(Protocols[3])

);

IF ProtocolAdded[3] THEN

    HB_Handler.Init(

        i_pDevice := ADR(Device),

        o_Error => HB_Error

    );

 

    (* Add monitored nodes only if consumer *)

    HB_Handler.AddCallbackToList(

        i_pDevice := ADR(Device),

        i_pCallback:=ADR(HBCallbacks[1])

    );

 

ELSE

    HB_Error := EPEC_CANopen.Errors.ProtocolDataMissing;

END_IF

 

 

Code:

 

 

(* NOTE! The Update method call is needed for producer only. *)

 

HB_Handler.Update(

    i_Enable := TRUE,

    o_Error => HB_Error

);

 

 

See also

-

 

 

 

Source file topic001013.htm

Last updated 21-Feb-2025