﻿<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1">
  <POU Name="AHC_PIDController" Id="{83cd4fcb-16a0-434b-9129-e71326d379e7}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK AHC_PIDController
VAR_INPUT
    Active                  : BOOL;
    TrajectorySetpoint      : LREAL;
    ProcessValue            : LREAL;
    TrajectoryVelocity      : LREAL;
    AHCAcceleration         : LREAL;
	AHCVelocity				: LREAL;
    VelocityMax             : LREAL;
    Kp                      : LREAL;
    Ki                      : LREAL;
    Kd                      : LREAL;
    Kv                      : LREAL;	// percentage of MRU velocity in process
    Ka                      : LREAL;	// percentage of MRU acceleration in process
    Scantime                : LREAL;
END_VAR
VAR
	tmp 				: LREAL;

	_Error 				: LREAL; 
	
	E 					: ARRAY[0..1] OF LREAL;
	meas				: ARRAY[0..1] OF LREAL;
	
	P 					: LREAL;
	I 					: LREAL;
	deltaI				: LREAL;
	D 					: LREAL;
	Tc 					: LREAL;

	_out 				: LREAL;
END_VAR

]]></Declaration>
    <Implementation>
      <ST><![CDATA[{warning disable C0371}
Tc := Scantime;

_Error := TrajectorySetpoint - ProcessValue;
 
E[1] := E[0];
E[0] := _Error; 

meas[1] := meas[0];
meas[0] := ProcessValue;

_Error := E[0];

IF Active THEN
		
	IF ABS(Kd) > 0 THEN
		D := (meas[0] - meas[1]) * Kd / Tc;
	ELSE
		D := 0;
	END_IF
	
	P := E[0] * Kp;
	
	IF ABS(Ki) <= 1E-100 THEN
		I := 0.0;
	ELSE
		deltaI := E[0] * Ki * Tc;	
		tmp := P + D + I + deltaI  + Kv * TrajectoryVelocity + Ka * AHCAcceleration;
		IF tmp = LIMIT(-VelocityMax, tmp, VelocityMax) THEN
			I := I + deltaI;
		END_IF
	END_IF

	_out := LIMIT(-VelocityMax, P + D + I + deltaI  + TrajectoryVelocity +  Kv * AHCVelocity +  Ka * AHCAcceleration, VelocityMax);
	
ELSE
	_out := 0;
END_IF;
	



]]></ST>
    </Implementation>
    <Property Name="Out" Id="{3aeb765f-298c-4cbc-b501-e6ec32024062}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'variable'}
{attribute 'TcRpcEnable'}
//(m/s)
PROPERTY Out : LREAL
]]></Declaration>
      <Get Name="Get" Id="{3244765a-369b-4bf4-a780-c2a1179c190f}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[Out := _out;

]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="Error" Id="{e90f3e76-701c-46a9-a6fc-4d4af8f071dd}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'variable'}
{attribute 'TcRpcEnable'}
PROPERTY Error : LREAL
]]></Declaration>
      <Get Name="Get" Id="{edf30a8c-c40f-4e81-9453-fcdb23247957}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[Error := _Error;
]]></ST>
        </Implementation>
      </Get>
    </Property>
  </POU>
</TcPlcObject>