Supported platforms: CODESYS 3.5, CODESYS 3.5 SAFETY

CANopenEMCY (FB)

Description

This function block implements both the EMCY producer and consumer.

EMCY Producer

The EMCY producer sends an EMCY message when an EMCY is activated with method Activate.

In case that all active EMCYs are deactivated, a null EMCY message with error code 0 is sent.

 

EMCY Consumer

The EMCY consumer registers a callback function block for each listed EMCY producer.

The last received EMCY message from each producer is copied to the EMCYObject table given in the application.

Limitations

CANopen OD protocol has to be initialized before initializing EMCY with the Init method. The required code is included in MultiTool Creator code template.

 

The mandatory Error Register OD index 16#1001 must be registered in the application code. The required code is included in MultiTool Creator code template.

 

The function block is included in CANopen library 4.0.2.4 or later.

 

ClearLog method is included in CANopen library 4.0.5.4 or later.

 

Init (METHOD)

This method initializes EMCY protocol and adds the following OD indexes:

 

This method also sets up the null EMCY message data.

Inputs

Input variable name

Data type

Description

i_pDevice

POINTER TO Device

Pointer to device

i_pErrorRegister

POINTER TO BYTE

Pointer to error register data

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 the consumed EMCY message COB-ID.

Inputs

Input variable name

Data type

Description

i_pDevice

POINTER TO Device

Pointer to device

i_pCallback

POINTER TO CANopenEMCYCallback

Pointer to EMCY consumer handler instance.

Outputs

Output variable name

Data type

Range

Description

o_Error

ENUM

Errors

Protocol error.

Error diagnostic

Error

Reason

ProtocolDataMissing

EMCY 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 updates the error register status (1001h) and the list of active EMCYs (1003h). Also, this method sends EMCY messages.

 

The EMCY message is sent after the EMCY is activated with the method Activate.

A null EMCY message is sent after the last active EMCY has been deactivated with the method Deactivate.

 

Set the inhibit time and application cycle time limit for sending EMCY messages.

The Update method call sends the oldest unsent EMCY message.

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.

 

Activate (METHOD)

This method adds the given EMCY object to the list of active EMCYs.

Inputs

Input variable name

Data type

Description

i_pEMCYObject

POINTER TO EMCYListObject

Pointer to EMCY object to add.

Return value

Data type

Range

Description

BOOL

 

Return value is TRUE if EMCY added successfully, else FALSE.

 

Deactivate (METHOD)

This method removes the given EMCY object from the list of active EMCYs.

Inputs

Input variable name

Data type

Description

i_pEMCYObject

POINTER TO EMCYListObject

Pointer to EMCY object to remove.

Return value

Data type

Range

Description

BOOL

 

Return value is TRUE if EMCY removed successfully, else FALSE.

 

 

ClearLog (METHOD)

This method clears the list of active EMCYs.

 

This method is for application's disposal. However, the application does not need to handle the OD index 1003 sub 0 as specified in CiA DS301. The handler is included in the library.

Inputs

-

Return value

Data type

Range

Description

BOOL

 

Return value is TRUE if EMCY cleared successfully, else FALSE.

 

 

Example code

Definitions:

 

Device: EPEC_CANopen.Device;

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

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

 

(* Consumer data, add only if consumer *)

MonitoredEMCYs: ARRAY [1..3] OF EMCYObject;

EMCYCallbacks: ARRAY [1..3] OF EPEC_CANopen.CANopenEMCYCallback := [

    (i_NodeID := 1, i_pEMCYObject := ADR(MonitoredEMCYs[1]), i_pNext := 0),

    (i_NodeID := 3, i_pEMCYObject := ADR(MonitoredEMCYs[2]), i_pNext := 0),

    (i_NodeID := 4, i_pEMCYObject := ADR(MonitoredEMCYs[3]), i_pNext := 0)];

 

(* Producer data, add only if producer *)

EMCY_Objects: ARRAY [0..2] OF EPEC_CANopen.EMCYListObject := [

    (Error := (Code := WORD#16#0001, Field := [16#00, 16#11, 16#12, 16#34, 16#56],

         RegisterMask := G_CANOPEN_ERR_REG_BITMASK_CURRENT)),

    (Error := (Code := WORD#16#0002, Field := [16#00, 16#22, 16#23, 16#45, 16#67],

         RegisterMask := G_CANOPEN_ERR_REG_BITMASK_VOLTAGE)),

    (Error := (Code := WORD#16#0003, Field := [16#00, 16#33, 16#34, 16#56, 16#78],

         RegisterMask := G_CANOPEN_ERR_REG_BITMASK_TEMPERATURE))

];

 

EMCY: EPEC_CANopen.EMCYData; (* Protocol data *)

EMCY_Handler: EPEC_CANopen.CANopenEMCY; (* Protocol handler *)

EMCY_Error: EPEC_CANopen.Errors := EPEC_CANopen.Errors.NotInitialized;

 

Init:

 

Protocols[6].Protocol:=EPEC_CANopen.ProtocolType.EMCY;

Protocols[6].pData:=ADR(G_CANopen_CAN1.EMCY);

Protocols[6].itfProtocol:=G_CANopen_CAN1.EMCY_Handler;

Protocols[6].pError:=ADR(G_CANopen_CAN1_VAR.EMCY_Error);

 

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

    i_pDevice:=ADR(Device),

    i_pProtocol:=ADR(Protocols[6])

);

IF ProtocolAdded[6] THEN

    EMCY_Handler.Init(

        i_pDevice := ADR(Device),

        o_Error => EMCY_Error

    );

 

    (* Add consumed EMCYs only if consumer *)

    EMCY_Handler.AddCallbackToList(

        i_pDevice := ADR(Device),

        i_pCallback:=ADR(EMCYCallbacks[1])

    );

    EMCY_Handler.AddCallbackToList(

        i_pDevice := ADR(Device),

        i_pCallback:=ADR(EMCYCallbacks[2])

    );

    EMCY_Handler.AddCallbackToList(

        i_pDevice := ADR(Device),

        i_pCallback:=ADR(EMCYCallbacks[3])

    );

 

ELSE

    EMCY_Error := EPEC_CANopen.Errors.ProtocolDataMissing;

END_IF

 

Code:

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

 

EMCY_Handler.Update(

    i_Enable := TRUE,

    o_Error => EMCY_Error

);

 

Activation code:

 

EMCY_Handler.Activate(

    i_pEMCYObject := ADR(G_CANopen_CAN1.EMCY_Objects[2])

);

 

Deactivation code:

 

EMCY_Handler.Deactivate(

    i_pEMCYObject := ADR(G_CANopen_CAN1.EMCY_Objects[2])

);

 

See also

- Using CANopen EMCY

 

 

 

 

Source file CANopenEMCY_(FB).htm

Last updated 13-Jun-2024