Files
BasicComponents/PLC/POUs/Unittests/UtilitiesTests/FB_PT1Test.TcPOU
2025-11-13 09:19:39 +01:00

276 lines
9.0 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1">
<POU Name="FB_PT1Test" Id="{70928114-b90c-4509-80ef-931fbb32faf9}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_PT1Test EXTENDS TcUnit.FB_TestSuite
VAR
// variables for test using T1 = 50 ms
_fbPT1_StepPT1_50 : FB_PT1;
_xInitStepPT1_50 : BOOL := FALSE;
_alrExpectedValuesStepPT1_50 : ARRAY[1..20] OF LREAL;
_iCounterStepPT1_50 : INT := 1;
// variables for test using T1 = 168 ms
_fbPT1_StepPT1_168 : FB_PT1;
_xInitStepPT1_168 : BOOL := FALSE;
_alrExpectedValuesStepPT1_168 : ARRAY[1..20] OF LREAL;
_iCounterStepPT1_168 : INT := 1;
// variables for test using T1 = 15 ms and a step of 2.345
_fbPT1_StepPT1_15_2345 : FB_PT1;
_xInitStepPT1_15_2345 : BOOL := FALSE;
_alrExpectedValuesStepPT1_15_2345 : ARRAY[1..20] OF LREAL;
_iCounterStepPT1_15_2345 : INT := 1;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[// test readout of task cycle time
TestCycleTimeReadout();
// test step responses for different time constants
TestOutputStepResponseT1_50ms();
TestOutputStepResponseT1_168ms();
TestOutputStepResponseStep2dot345_T1_15ms();]]></ST>
</Implementation>
<Method Name="TestCycleTimeReadout" Id="{904f47d5-67b3-4556-aaef-cec669b6e988}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestCycleTimeReadout
VAR
// pt1 instance
_fbPT1 : FB_PT1;
// task index instance
_fbGetCurTaskIdx : GETCURTASKINDEX;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestCycleTimeReadout');
// read current cycle time and abort test in case of cycletime not 10 ms.
_fbGetCurTaskIdx();
IF ((UDINT_TO_REAL(TwinCAT_SystemInfoVarList._TASKInfo[_fbGetCurTaskIdx.index].CycleTime) * 10E-5) - 10.0) > 0.0001 THEN
AssertTrue(FALSE, 'Project cycle time not set to 10ms!');
ELSE
_fbPT1(
lrInput := 0,
timT := T#10MS,
lrOutput => );
// Project should be set to 10ms cycle time for this test to work
AssertEquals_LREAL(Expected := 10.0, Actual := _fbPT1.CycleTime, Delta := 0.01, 'Cycle time is not equal to project cycle time (10ms)');
END_IF
TEST_FINISHED();]]></ST>
</Implementation>
</Method>
<Method Name="TestOutputStepResponseStep2dot345_T1_15ms" Id="{6ad357cb-7b1c-47ef-888c-e7cf604e0662}">
<Declaration><![CDATA[{warning disable C0394}
{attribute 'analysis' := '-26'}
METHOD TestOutputStepResponseStep2dot345_T1_15ms
VAR
// pt1 output
_lrOutput : LREAL;
// error message
_sMessage : STRING := 'Step ';
END_VAR
VAR CONSTANT
// pt1 setpoint
lrSetpoint : LREAL := 2.345;
// delta for assertion
lrDelta : LREAL := 0.0001;
// T1
timT : TIME := T#15MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestOutputStepResponseStep2dot345_T1_15ms');
// init expected values for 20 cyclic calls
IF NOT _xInitStepPT1_15_2345 THEN
_xInitStepPT1_15_2345 := TRUE;
_alrExpectedValuesStepPT1_15_2345[1] := 0.938;
_alrExpectedValuesStepPT1_15_2345[2] := 1.5008;
_alrExpectedValuesStepPT1_15_2345[3] := 1.83848;
_alrExpectedValuesStepPT1_15_2345[4] := 2.041088;
_alrExpectedValuesStepPT1_15_2345[5] := 2.1626528;
_alrExpectedValuesStepPT1_15_2345[6] := 2.23559168;
_alrExpectedValuesStepPT1_15_2345[7] := 2.279355008;
_alrExpectedValuesStepPT1_15_2345[8] := 2.305613005;
_alrExpectedValuesStepPT1_15_2345[9] := 2.321367803;
_alrExpectedValuesStepPT1_15_2345[10] := 2.330820682;
_alrExpectedValuesStepPT1_15_2345[11] := 2.336492409;
_alrExpectedValuesStepPT1_15_2345[12] := 2.339895445;
_alrExpectedValuesStepPT1_15_2345[13] := 2.341937267;
_alrExpectedValuesStepPT1_15_2345[14] := 2.34316236;
_alrExpectedValuesStepPT1_15_2345[15] := 2.343897416;
_alrExpectedValuesStepPT1_15_2345[16] := 2.34433845;
_alrExpectedValuesStepPT1_15_2345[17] := 2.34460307;
_alrExpectedValuesStepPT1_15_2345[18] := 2.344761842;
_alrExpectedValuesStepPT1_15_2345[19] := 2.344857105;
_alrExpectedValuesStepPT1_15_2345[20] := 2.344914263;
END_IF
// run PT1
_fbPT1_StepPT1_15_2345(
lrInput := lrSetpoint,
timT := timT,
lrOutput => _lrOutput);
// set error message according to current step
_sMessage := CONCAT(_sMessage, TO_STRING(_iCounterStepPT1_15_2345));
_sMessage := CONCAT(_sMessage, ' did not return expected value.');
// check values of current step
AssertEquals_LREAL(Expected := _alrExpectedValuesStepPT1_15_2345[_iCounterStepPT1_15_2345], Actual := _lrOutput, Delta := lrDelta, _sMessage);
_iCounterStepPT1_15_2345 := _iCounterStepPT1_15_2345 + 1;
// finish test after 20 cycles
IF _iCounterStepPT1_15_2345 > 20 THEN
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestOutputStepResponseT1_168ms" Id="{95beda9c-524a-46f3-bd27-507fc0dda2e0}">
<Declaration><![CDATA[{warning disable C0394}
{attribute 'analysis' := '-26'}
METHOD TestOutputStepResponseT1_168ms
VAR
// pt1 output
_lrOutput : LREAL;
// error message
_sMessage : STRING := 'Step ';
END_VAR
VAR CONSTANT
// pt1 setpoint
lrSetpoint : LREAL := 1;
// delta for assertion
lrDelta : LREAL := 0.0001;
// T1
timT : TIME := T#168MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestOutputStepResponseT1_168ms');
// init expected values for 20 cyclic calls
IF NOT _xInitStepPT1_168 THEN
_xInitStepPT1_168 := TRUE;
_alrExpectedValuesStepPT1_168[1] := 0.056179775;
_alrExpectedValuesStepPT1_168[2] := 0.109203383;
_alrExpectedValuesStepPT1_168[3] := 0.159248137;
_alrExpectedValuesStepPT1_168[4] := 0.206481388;
_alrExpectedValuesStepPT1_168[5] := 0.251061085;
_alrExpectedValuesStepPT1_168[6] := 0.293136305;
_alrExpectedValuesStepPT1_168[7] := 0.332847749;
_alrExpectedValuesStepPT1_168[8] := 0.370328212;
_alrExpectedValuesStepPT1_168[9] := 0.405703032;
_alrExpectedValuesStepPT1_168[10] := 0.439090502;
_alrExpectedValuesStepPT1_168[11] := 0.470602271;
_alrExpectedValuesStepPT1_168[12] := 0.500343717;
_alrExpectedValuesStepPT1_168[13] := 0.528414295;
_alrExpectedValuesStepPT1_168[14] := 0.554907874;
_alrExpectedValuesStepPT1_168[15] := 0.579913049;
_alrExpectedValuesStepPT1_168[16] := 0.60351344;
_alrExpectedValuesStepPT1_168[17] := 0.625787966;
_alrExpectedValuesStepPT1_168[18] := 0.646811114;
_alrExpectedValuesStepPT1_168[19] := 0.666653186;
_alrExpectedValuesStepPT1_168[20] := 0.685380535;
END_IF
// run PT1
_fbPT1_StepPT1_168(
lrInput := lrSetpoint,
timT := timT,
lrOutput => _lrOutput);
// set error message according to current step
_sMessage := CONCAT(_sMessage, TO_STRING(_iCounterStepPT1_168));
_sMessage := CONCAT(_sMessage, ' did not return expected value.');
// check values of current step
AssertEquals_LREAL(Expected := _alrExpectedValuesStepPT1_168[_iCounterStepPT1_168], Actual := _lrOutput, Delta := lrDelta, _sMessage);
_iCounterStepPT1_168 := _iCounterStepPT1_168 + 1;
// finish test after 20 cycles
IF _iCounterStepPT1_168 > 20 THEN
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestOutputStepResponseT1_50ms" Id="{13fc9847-7f4d-4bad-80e0-d8a65864a692}">
<Declaration><![CDATA[{warning disable C0394}
{attribute 'analysis' := '-26'}
METHOD TestOutputStepResponseT1_50ms
VAR
// pt1 output
_lrOutput : LREAL;
// error message
_sMessage : STRING := 'Step ';
END_VAR
VAR CONSTANT
// pt1 setpoint
lrSetpoint : LREAL := 1;
// delta for assertion
lrDelta : LREAL := 0.0001;
// T1
timT : TIME := T#50MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestOutputStepResponseT1_50ms');
// init expected values for 20 cyclic calls
IF NOT _xInitStepPT1_50 THEN
_xInitStepPT1_50 := TRUE;
_alrExpectedValuesStepPT1_50[1] := 0.166666667;
_alrExpectedValuesStepPT1_50[2] := 0.305555556;
_alrExpectedValuesStepPT1_50[3] := 0.421296296;
_alrExpectedValuesStepPT1_50[4] := 0.517746914;
_alrExpectedValuesStepPT1_50[5] := 0.598122428;
_alrExpectedValuesStepPT1_50[6] := 0.665102023;
_alrExpectedValuesStepPT1_50[7] := 0.720918353;
_alrExpectedValuesStepPT1_50[8] := 0.767431961;
_alrExpectedValuesStepPT1_50[9] := 0.806193301;
_alrExpectedValuesStepPT1_50[10] := 0.838494417;
_alrExpectedValuesStepPT1_50[11] := 0.865412014;
_alrExpectedValuesStepPT1_50[12] := 0.887843345;
_alrExpectedValuesStepPT1_50[13] := 0.906536121;
_alrExpectedValuesStepPT1_50[14] := 0.922113434;
_alrExpectedValuesStepPT1_50[15] := 0.935094528;
_alrExpectedValuesStepPT1_50[16] := 0.945912107;
_alrExpectedValuesStepPT1_50[17] := 0.954926756;
_alrExpectedValuesStepPT1_50[18] := 0.962438963;
_alrExpectedValuesStepPT1_50[19] := 0.968699136;
_alrExpectedValuesStepPT1_50[20] := 0.973915947;
END_IF
// run PT1
_fbPT1_StepPT1_50(
lrInput := lrSetpoint,
timT := timT,
lrOutput => _lrOutput);
// set error message according to current step
_sMessage := CONCAT(_sMessage, TO_STRING(_iCounterStepPT1_50));
_sMessage := CONCAT(_sMessage, ' did not return expected value.');
// check values of current step
AssertEquals_LREAL(Expected := _alrExpectedValuesStepPT1_50[_iCounterStepPT1_50], Actual := _lrOutput, Delta := lrDelta, _sMessage);
_iCounterStepPT1_50 := _iCounterStepPT1_50 + 1;
// finish test after 20 cycles
IF _iCounterStepPT1_50 > 20 THEN
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
</POU>
</TcPlcObject>