MceStateMonitoring

0.1.0

Monitoring logic for a state machine, returns elapsed time [ms]

Version history

0.1.0

  Rioual

Changes

Added:
  • initial version

Overview

kindnametypedefaultcomment
inputnStateDINTstate of a state machine to monitor.
inputbFreezeTimerBOOLstop the timer
in_outstateDataMceStateMonitoringDatadata for state machine monitoring

Details

nState

DINT

State of a state machine to monitor.

bFreezeTimer

BOOL

Stop the timer

Data for state machine monitoring.

Source code

Declarations

(*Monitoring logic for a state machine, returns elapsed time [ms]*)
FUNCTION MceStateMonitoring : DINT

(*
 * -----------------------------------------------------------------------------
 * Name               : MceStateMonitoring
 * Version            : 0.1.0
 * Date               : 2024-02-06
 * Author             : Rioual
 * Family             : YaskawaMce
 * Organisation       : github.com/YaskawaEurope/mlx-examples
 * 
 * -----------------------------------------------------------------------------
 * Monitoring logic for a state machine, returns elapsed time [ms]
 * -----------------------------------------------------------------------------
 *)

VAR_INPUT
  nState : DINT; (*state of a state machine to monitor.*)
  bFreezeTimer : BOOL; (*stop the timer*)
END_VAR

VAR_IN_OUT
  stateData : MceStateMonitoringData; (*data for state machine monitoring*)
END_VAR

VAR
  nTdiff : DINT; (*time since last call [µs]*)
  tNow : LTIME; (*plc clock*)
  bOsrStateChange : BOOL; (*rising edge*)
END_VAR

Logic

//*******************************************************************************************************************
// detect state change
//*******************************************************************************************************************
IF nState <> stateData.nStateStored THEN
  stateData.nStatePrevious := stateData.nStateStored ;
  bOsrStateChange := TRUE;
  stateData.nStateStored := nState;
ELSE
  bOsrStateChange := FALSE;
END_IF;

//*******************************************************************************************************************
// time state active
//*******************************************************************************************************************
tNow := LTIME(); // system clock in ns resolution

IF (tNow > stateData.tStored) THEN
  nTdiff := LTIME_TO_DINT(tNow - stateData.tStored) / 1000;
  IF (nState <> 0) AND (nTdiff > 0) AND NOT bFreezeTimer THEN
    stateData.nStateTimeUs := stateData.nStateTimeUs + nTdiff;
  END_IF;
END_IF;

stateData.tStored := tNow;
stateData.nStateTime := stateData.nStateTimeUs / 1000;

// store and reset timer at state change
IF bOsrStateChange THEN
  stateData.nStateTimePrevious := stateData.nStateTime;
  stateData.nStateTimeUs := 0;
END_IF;

MceStateMonitoring := stateData.nStateTime;

Implementation

Snippet of the function call:
dummy := MceStateMonitoring(
           nState := , 
           bFreezeTimer := , 
           stateData := );

Pages built with Hugo - 23 Apr 2024 11:54 CEST