Supported platforms: CODESYS 3.5 SP19 SAFETY

S_HBridgeControllerDO (FB)

Description

S_HBridgeControllerDO POU implements logic to control and diagnose H-Bridge with DO signals. It can be used to control for example electric motor or linear actuator. 

 

POU can be used with DO pins which gives possibility to control both pin high and low side. 

 

 

Initialization

Init method is used to initialize the necessary static calculation parameters for the function block instance.

The method shall be called at least once before the actual function block can be called, i.e. at the application initialization phase.

The method checks the validity of input parameters. However, none of the function block outputs are affected by the initialization.

 

 

A parameter error is diagnosed if:

  • Init method is not called before the main FB call OR

  • i_pEventCode = 0 OR

 

 

Do not call Init method when control is active. 

Initialization parameters

Parameter

Type

Unit

Range

Description

S_i_OverCurrentLimit

SAFEUINT

mA

 -

Limit for overcurrent diagnostic.

S_i_CurrentDiagnosticDelay

SAFEUINT

ms

-

Diagnostic delay for current diagnostic

S_i_ControlStopDelay

SAFEUINT

ms

-

Delay time for waiting motor/actuator stop.

i_pEventCode

POINTER TO EventCode

-

≠ 0

Pointer to application variable where event code is stored.

 

Initialization method return value

TRUE: All initialization parameters ok.

FALSE: Error in initialization parameter(s).

 

Operation

POU is in normal operation mode if

• The control is enabled, i.e. i_Enable is TRUE AND no error diagnosed.

 

In normal operation DO outputs are controlled according to control request signals S_i_Forward and S_i_Backward, see table below.

 

S_i_Forward

S_i_Backward

Motor/Actuator Control Direction

Pin1 HS DO Pin2 HS DO Pin1 LS DO Pin2 LS DO
 FALSE FALSE

-

FALSE FALSE FALSE FALSE

TRUE

FALSE

Forward

TRUE FALSE FALSE TRUE

FALSE

TRUE

Backward

FALSE TRUE TRUE FALSE
TRUE TRUE - FALSE FALSE FALSE FALSE

 

 

POU's internal state machinePOU's internal state machine

Internally POU has state machine of type HBridgeStates.

 

State

Description

 Idle S_i_Forward = FALSE andS_i_Backward = FALSE. DO outputs are set FALSE.

StartControl

  1. Depending on the control direction, either Pin1 or Pin2 HS DO is first set TRUE.
  2. Wait that both pin's DOS statutes rise.   
  3. LS DO of pin pair* is set TRUE
Control

Control continues until control request is reset, control direction changes or any error occurs.

EndControl
  1. HS DO is set first to FALSE
  2. Wait i_ControlStopDelay time
  3. LS DO of pin pair* is set FALSE

 

 Diagnostic is disabled in this state, because running motor may induce electricity to outputs and cause unnecessary alarms.

Error Error is diagnosed. DO outputs are set FALSE.
ControlDisabled Control is disabled. see Control_disabled_reasons 

 * Pin pair: If pin 1 HS DO is controlled, the pin pair is pin 2. If pin 2 HS DO is controlled, the pin pair is pin 1.

Error Diagnostics

 

Conditions

DO outputs

o_Status. OutputValid

o_Status error status

Event code FunctionID

Event code EventID

i_Enable = TRUE

AND 
i_pEventCode = NULL

FALSE

FALSE

ParameterError

-

-

i_Enable rises AND

 (S_i_Forward OR S_i_Backward is TRUE)

FALSE

FALSE

StartupError

H_BRIDGE

STARTUP_ERROR

i_Enable = TRUE AND (i_Pin1OverCurrentProtectionStatus OR i_Pin2OverCurrentProtectionStatus is TRUE)

FALSE

FALSE

Pin1OverCurrent/

Pin2OverCurrent

H_BRIDGE

OVER_CURRENT

i_Enable = TRUE

AND (i_Pin1Current OR i_Pin2Current < 0)

FALSE

FALSE

InputLow

H_BRIDGE

INPUT_TOO_LOW

i_Enable = TRUE AND 

S_i_Forward = TRUE AND S_i_Backward = TRUE

FALSE

FALSE

ParameterError

H_BRIDGE

PARAMETER_ERROR

i_Enable = TRUE AND

(i_Pin1DOStatus OR i_Pin2DOStatus is FALSE when the corresponding output is controlled)

FALSE

FALSE

Pin1ShortToGround/

Pin2ShortToGround1

H_BRIDGE1

VOLTAGE_SHORTCUT1

i_Enable = TRUE AND

(i_Pin1DOStatus OR i_Pin2DOStatus is TRUE when the corresponding output is not controlled)

FALSE

FALSE

Pin1ExternalVoltage/

Pin2ExternalVoltage1

H_BRIDGE1

EXTERNAL_VOLTAGE1

i_Enable = TRUE AND

(i_Pin1Current OR i_Pin2Current >  G_HBRIDGE_CURRENT_LIMIT_CONTROL_OFF  (HW specific constant) when the corresponding output is not controlled)

FALSE

FALSE

Pin1CurrentLeak/

Pin2CurrentLeak2

H_BRIDGE2

OVER_CURRENT2

i_Enable = TRUE AND

(i_Pin1Current OR i_Pin2Current >  S_i_OverCurrentLimit)

FALSE

FALSE

Pin1OverCurrent/

Pin2OverCurrent2

H_BRIDGE2

OVER_CURRENT2

1) After voltage diagnostic delay (60 ms) is elapsed. Note! Diagnostic is disabled when o_BridgeState = EndControl.

2) After diagnostic delay S_i_CurrentDiagnosticDelay is elapsed. Note! Diagnostic is disabled when o_BridgeState = EndControl.

 

 

The o_Status error bits and EventCode error are not reset before the rising edge of i_Enable input.

 

Inputs

Input variable name

Data type

Range

Description

i_Enable

BOOL

 

Enable POU operation

S_i_Forward

BOOL

 

Run motor forward

S_i_Backward BOOL   Run motor backward
i_Pin1DOStatus BOOL   Pin 1 output digital status (DOS)
i_Pin2DOStatus BOOL   Pin 2 output digital status (DOS)
i_Pin1Current BOOL   Measured pin 1 current (mA)
i_Pin2Current BOOL   Measured pin 2 current (mA)
i_Pin1OverCurrentProtectionStatus BOOL   Pin 1 overcurrent status from FW
i_Pin2OverCurrentProtectionStatus BOOL   Pin 2 overcurrent status from FW

Outputs

Output variable name

Data type

Range

Description

o_Pin1HsDO

BOOL

 

Pin 1 high side DO control

o_Pin2HsDO

BOOL

 

Pin 2 high side DO control

o_Pin1LsDO BOOL   Pin 1 low side DO control
o_Pin2LsDO BOOL   Pin 2 low side DO control
o_Status Status   Status of control outputs
o_BridgeState HBridgeStates   Bridge control sequence state
o_Forward BOOL   Motor/Actuator forward control active
o_Backward BOOL   Motor/Actuator backward control active

 

 o_BridgeState value ControlDisabled is not necessary error situation. It can mean one of the following:

 

  • Motor/actuator is not connected or wiring is broken
    • Check wiring
  • Actuator has been driven to its end position
    • Deactivate control request and try to control other direction
  • pin LS FET is shortcut to ground
    • Deactivate control request and try to control other direction

 

 

 o_BridgeState value Error indicates error state. Check o_Status output to identify error.

 

Control can be retried after resetting POU i.e. set input i_Enable to false and then true.

 

Example code

Definitions:

 

hBridgeControl: EPEC_SHB.S_HBridgeControllerDO;        
inited:         BOOL;

 

// POU input variables
eventCode :     EPEC_DITF.EventCode;
enable:         BOOL;
controlForward: BOOL;  // Control request for example from control unit DI
controlBackward:BOOL;  // Control request for example from control unit DI
pin1DOStatus :  BOOL;  // DO pin 1 DOS  
pin2DOStatus:   BOOL;  // DO pin 2 DOS 

pin1Current:    DINT;  // DO pin 1 measured current 
pin2Current:    DINT;  // DO pin 2 measured current


// POU output variables    
pin1HsDO:       BOOL;  // Control to DO pin 1 high side
pin2HsDO:       BOOL;  // Control to DO pin 2 high side
pin1LsDO:       BOOL;  // Control to DO pin 1 low side
pin2LsDO:       BOOL;  // Control to DO pin 2 low side
status:         EPEC_SHB.Status;
bridgeState:    EPEC_SHB.HBridgeStates;

 

 

Safety Code:

IF NOT inited THEN

    // Call POU init method to initialize parameters.
    hBridgeControl.Init(

S_i_OverCurrentLimit := UINT#6000,

S_i_CurrentDiagnosticDelay := UINT#500,

S_i_ControlStopDelay := UINT#500,

i_pEventCode:= ADR(eventCode));
    inited := TRUE;
END_IF


hBridgeControl(
    i_Enable:= enable, 
    S_i_Forward:= controlForward, 
    S_i_Backward := controlBackward,
    i_Pin1DOStatus:= pin1DOStatus, 
    i_Pin2DOStatus:= pin2DOStatus, 
    i_Pin1Current:= pin1Current, 
    i_Pin2Current:= pin2Current, 
    o_Pin1HsDO=> pin1HsDO, 
    o_Pin2HsDO=> pin2HsDO, 
    o_Pin1LsDO=> pin1LsDO, 
    o_Pin2LsDO=> pin2LsDO, 
    o_Status=> status, 
    o_BridgeState=> bridgeState, 
    o_Forward=> , 
    o_Backward=> );

 

IF bridgeState = EPEC_SHB.HBridgeStates.ControlDisabled THEN
    // Control disabled. Inform user to try control other direction.
    ;

ELSIF bridgeState = EPEC_SHB.HBridgeStates.Error THEN
    // Error handling. Error can be identified from status variable. 
    ;
END_IF

 

 

 

 Assignments to actual I/O variables is not implemented in this example.

 

 

See also

-

 

 

 

 

Source file HBridgeControllerDO.htm

Last updated 21-Feb-2025