<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
  <POU Name="FB_AnalogSensorMockup" Id="{198f1df4-73a2-495b-b972-189adc06c969}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_AnalogSensorMockup
VAR_INPUT
	// Raise sensor level from min to max
	xEnable : BOOL;
	
	// Switch to error high
	xErrorHigh : BOOL := FALSE;
	
	// Switch to error low
	xErrorLow : BOOL := FALSE;
	
	// Sensor value when enabled
	rOnValue : REAL;
	
	// Sensor value when dissabled
	rOffValue : REAL;
	
	// Sensor value error high
	rErrorLowValue : REAL;
	
	// Sensor value error high
	rErrorHighValue : REAL;
	
	// Sensor noise value
	rNoise : REAL := 0.0;
	
	// Analog output scaling
	stAnalogScaling : ST_ANALOG_IO_CONFIG;
END_VAR
VAR_OUTPUT
	iAnalogValue AT %Q* : INT;
END_VAR
VAR
	// Internal target value
	_rTargetValue : REAL;

	// Temp variable
	_lrTmp : LREAL;
	
	// Filter data
	_rFilterParam : REAL := 0.005;
	_rFilterParam2 : REAL := 1.0 - _rFilterParam;
	
	// Calculated value
	_rValue : REAL;
	
	// Last calculated value
	_rPrevValue : REAL := 0.0;
	
	// Analog output
	{attribute 'hide'}
	_fbAnalogOutput : FB_AnalogOutput('');
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[// Pressure only if enabled
IF xEnable OR xErrorLow OR xErrorHigh THEN
	// Normal enable value
	_rTargetValue := rOnValue;
	
	// Switch to error high value
	IF xErrorHigh AND (NOT xErrorLow) THEN
		_rTargetValue := rErrorHighValue;
	END_IF
	
	// Switch to error low value
	IF xErrorLow AND (NOT xErrorHigh) THEN
		_rTargetValue := rErrorLowValue;
	END_IF
ELSE
	_rTargetValue := rOffValue;
END_IF

// Get a random number
GVL_GLOBAL.fbRandom(Seed := 1337, Num => _lrTmp);

// Exponential smoothing
_rValue := _rFilterParam * _rTargetValue + _rFilterParam2 * _rPrevValue;
_rPrevValue := _rValue;

// Add some random noise
IF xEnable OR xErrorLow OR xErrorHigh THEN
	_rValue := _rValue + (LREAL_TO_REAL(_lrTmp - 0.5) * rNoise);
END_IF


// Calculate analog output from ramp generator setpoint
_fbAnalogOutput(
	rSetpoint:= _rValue, 
	stScalingConfig:= stAnalogScaling,
	xReleaseErrors:= FALSE, 
	xReleaseHardwareErrors:= FALSE, 
	xConfirmAlarms:= , 
	iAnalogValue=> iAnalogValue, 
	xError=> );]]></ST>
    </Implementation>
    <LineIds Name="FB_AnalogSensorMockup">
      <LineId Id="1084" Count="38" />
      <LineId Id="9" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>