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

473 lines
13 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1">
<POU Name="FB_RampGeneratorTest" Id="{883a6789-24bc-434e-bdcd-11047d9142bc}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_RampGeneratorTest EXTENDS TcUnit.FB_TestSuite
VAR
// variables for max clamp test
_fbRMPMaxClamp : FB_RampGenerator;
_fbDelayMaxClamp : TON;
_fbDelayMaxClampBuffer : TON;
// variables for min clamp test
_fbRMPMinClamp : FB_RampGenerator;
_fbDelayMinClamp : TON;
_fbDelayMinClampBuffer : TON;
_xInitMinClamp : BOOL := FALSE;
// variables for ramp down timing test
_fbRMPRampDownTime : FB_RampGenerator;
_fbDelayRampDownTime : TON;
// variables for ramp up timing test
_fbRMPRampUpTime : FB_RampGenerator;
_fbDelayRampUpTime : TON;
// variables for ramp down continuity test
_fbRMPRampDownContinuity : FB_RampGenerator;
_fbDelayRampDownContinuity : TON;
_rNextExpectedDown : REAL := 0;
// variables for ramp up continuity test
_fbRMPRampUpContinuity : FB_RampGenerator;
_fbDelayRampUpContinuity : TON;
_rNextExpectedUp : REAL := 0;
// variables for in target up test
_fbRMPInTargetResultUp : FB_RampGenerator;
_fbDelayInTargetResultUp : TON;
// variables for in target down test
_fbRMPInTargetResultDown : FB_RampGenerator;
_fbDelayInTargetResultDown : TON;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[// test for correct cycle time and it's readout
TestCycleTimeReadout();
// test clamping
TestMaxClamping();
TestMinClamping();
// test ramp up/down
TestRampUpTime();
TestRampUpContinuity();
TestRampDownTime();
TestRampDownContinuity();
// test inTarget
TestInTargetResultUp();
TestInTargetResultDown();]]></ST>
</Implementation>
<Method Name="TestCycleTimeReadout" Id="{adf8238e-e5ed-478c-ac22-37930b46e697}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestCycleTimeReadout
VAR
// ramp generator instance
_fbRampGen : FB_RampGenerator;
// 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.001 THEN
AssertTrue(FALSE, 'Project cycle time not set to 10ms!');
ELSE
_fbRampGen(
rTarget:= 0.0,
rTargetMin:= 0.0,
rTargetMax:= 100.0,
timRampUp:= T#5S,
timRampDown:= T#5S,
rSetpoint=> ,
xInTarget=> );
// Project should be set to 10ms cycle time for this test to work
AssertEquals_REAL(Expected := 10.0, Actual := _fbRampGen.CycleTime, Delta := 0.01, 'Cycle time is not equal to project cycle time (10ms)');
END_IF
TEST_FINISHED();]]></ST>
</Implementation>
</Method>
<Method Name="TestInTargetResultDown" Id="{d659dd70-b87c-48f4-9d7b-df95aaef8540}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestInTargetResultDown
VAR
// in target result
_xInTarget : BOOL;
// current ramp setpoint
_rSetpoint : REAL;
END_VAR
VAR CONSTANT
// delay until in target should be reached
timDelay : TIME := T#990MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestInTargetResultDown');
// timer until InTarget is supposed to be reached
_fbDelayInTargetResultDown(IN := TRUE, PT := timDelay);
// run RampGenerator
_fbRMPInTargetResultDown(
rTarget := -1,
rTargetMin := -1,
rTargetMax := 1,
timRampUp := T#2S,
timRampDown := T#2S,
rSetpoint => _rSetpoint,
xInTarget => _xInTarget);
// check for whether InTarget is reached at the specified time
IF NOT _fbDelayInTargetResultDown.Q THEN
AssertFalse(_xInTarget, 'InTarget reached earlier then expected.');
ELSE
AssertTrue(_xInTarget, 'InTarget not reached in time.');
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestInTargetResultUp" Id="{41467b7e-bd5d-4d30-abee-4fe231a34fce}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestInTargetResultUp
VAR
// in target result
_xInTarget : BOOL;
// current ramp setpoint
_rSetpoint : REAL;
END_VAR
VAR CONSTANT
// delay until in target should be reached
timDelay : TIME := T#740MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestInTargetResultUp');
// timer until InTarget is supposed to be reached
_fbDelayInTargetResultUp(IN := TRUE, PT := timDelay);
// run RampGenerator
_fbRMPInTargetResultUp(
rTarget := 1,
rTargetMin := -1,
rTargetMax := 1,
timRampUp := T#1S500MS,
timRampDown := T#1S500MS,
rSetpoint => _rSetpoint,
xInTarget => _xInTarget);
// check for whether InTarget is reached at the specified time
IF NOT _fbDelayInTargetResultUp.Q THEN
AssertFalse(_xInTarget, 'InTarget reached earlier then expected.');
ELSE
AssertTrue(_xInTarget, 'InTarget not reached in time.');
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestMaxClamping" Id="{65cd0699-a59f-4537-9fbd-e53ee8fb2124}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestMaxClamping
VAR
// current ramp setpoint
_rSetpoint : REAL;
END_VAR
VAR CONSTANT
// expected clamped value
rExpected : REAL := 5;
// delta for assertion
rDelta : REAL := 0.0001;
// delay until clamping is reached
timDelayRamp : TIME := T#500MS;
// delay to check for value changes after clamping
timDelay : TIME := T#1S;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestMaxClamping');
// timer until until clamping is reach + timeBuffer afterwards to check whether is stays clamped or not
_fbDelayMaxClamp(IN := TRUE, PT := timDelayRamp);
_fbDelayMaxClampBuffer(IN := TRUE, PT := timDelay);
// run RampGenerator
_fbRMPMaxClamp(
rTarget := 10,
rTargetMin := 0,
rTargetMax := 5,
timRampUp := T#500MS,
timRampDown := T#500MS,
rSetpoint => _rSetpoint,
xInTarget =>);
// check for clamping
IF NOT _fbDelayMaxClamp.Q THEN
// too early
AssertTrue(_rSetpoint < rExpected,'Clamped value reached before expected time');
ELSE
// after expected rampTime
IF NOT _fbDelayMaxClampBuffer.Q THEN
AssertEquals_REAL(Expected := rExpected, Actual := _rSetpoint, Delta := rDelta, 'Value did not stay on or did not reach MaxTarget');
ELSE
TEST_FINISHED();
END_IF
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestMinClamping" Id="{e7c98271-509c-4227-870c-3cb3245da266}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestMinClamping
VAR
// current ramp setpoint
_rSetpoint : REAL;
END_VAR
VAR CONSTANT
// expected clamped value
rExpected : REAL := 5;
// delta for assertion
rDelta : REAL := 0.0001;
// delay until clamping is reached
timDelayRamp : TIME := T#500MS;
// delay to check for value changes after clamping
timDelay : TIME := T#1S;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestMinClamping');
// init start at top cap
IF NOT _xInitMinClamp THEN
_fbRMPMinClamp.SetStart(10);
_xInitMinClamp := TRUE;
END_IF
// timer until until clamping is reach + timeBuffer afterwards to check whether is stays clamped or not
_fbDelayMinClamp(IN := TRUE, PT := timDelayRamp);
_fbDelayMinClampBuffer(IN := TRUE, PT := timDelay);
// run RampGenerator
_fbRMPMinClamp(
rTarget := 0,
rTargetMin := 5,
rTargetMax := 10,
timRampUp := T#500MS,
timRampDown := T#500MS,
rSetpoint => _rSetpoint,
xInTarget =>);
// check for clamping
IF NOT _fbDelayMinClamp.Q THEN
// too early
AssertTrue(_rSetpoint >= (rExpected - rDelta), 'Clamped value reached before expected time');
ELSE
// after expected rampTime
IF NOT _fbDelayMinClampBuffer.Q THEN
AssertEquals_REAL(Expected := rExpected, Actual := _rSetpoint, Delta := rDelta, 'Value did not stay on or did not reach MinTarget');
ELSE
TEST_FINISHED();
END_IF
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestRampDownContinuity" Id="{dcb7f0b0-2158-4cf1-af71-745bee5bf2bd}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestRampDownContinuity
VAR
// current ramp setpoint
_rSetpoint : REAL;
// calculated ramp speed
_rMyRampSpeed : REAL;
END_VAR
VAR CONSTANT
// expected final value
rExpected : REAL := -9.4564;
// delta for assertion
rDelta : REAL := 0.0001;
// delay until final value is reached
timDelay : TIME := T#470MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestRampDownContinuity');
// ramp timer, calc speed & calc next expected ramp step
_fbDelayRampDownContinuity(IN := TRUE, PT := timDelay);
_rMyRampSpeed := 20 * (10 / TIME_TO_REAL(T#1S));
_rNextExpectedDown := _rNextExpectedDown - _rMyRampSpeed;
// run RampGenerator
_fbRMPRampDownContinuity(
rTarget := -9.4564,
rTargetMin := -10,
rTargetMax := 10,
timRampUp := T#1S,
timRampDown := T#1S,
rSetpoint => _rSetpoint,
xInTarget =>);
// check for current expected value
IF NOT _fbDelayRampDownContinuity.Q THEN
AssertEquals_REAL(Expected := _rNextExpectedDown, Actual := _rSetpoint, Delta := rDelta, 'Expected current value was not achieved.');
ELSE
// check for final value
AssertEquals_REAL(Expected := rExpected, Actual := _rSetpoint, Delta := rDelta, 'Value was not reached in time.');
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestRampDownTime" Id="{c4784967-e4fc-415b-8fa5-fb9ce6bb34f7}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestRampDownTime
VAR
// current ramp setpoint
_rSetpoint : REAL;
END_VAR
VAR CONSTANT
// expected final value
rExpected : REAL := -7.4367;
// delta for assertions
rDelta : REAL := 0.0001;
// delay until final value is reached
timDelay : TIME := T#74MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestRampDownTime');
// timer until bottom of ramp is supposed to be reached
_fbDelayRampDownTime(IN := TRUE, PT := timDelay);
// run RampGenerator
_fbRMPRampDownTime(
rTarget := -7.4367,
rTargetMin := -10,
rTargetMax := 10,
timRampUp := T#500MS,
timRampDown := T#100MS,
rSetpoint => _rSetpoint,
xInTarget =>);
// check whether final value is reach on time or before
IF NOT _fbDelayRampDownTime.Q THEN
AssertTrue(_rSetpoint > (rExpected - rDelta),'Value reached before expected time.');
ELSE
AssertEquals_REAL(Expected := rExpected, Actual := _rSetpoint, Delta := rDelta, 'Value was not reached in time.');
TEST_FINISHED();
END_IF
]]></ST>
</Implementation>
</Method>
<Method Name="TestRampUpContinuity" Id="{3771104c-32d2-4bae-b36b-27a109bac10f}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestRampUpContinuity
VAR
// current ramp setpoint
_rSetpoint : REAL;
// calculated ramp speed
_rMyRampSpeed : REAL;
END_VAR
VAR CONSTANT
// expected final value
rExpected : REAL := 8.673;
// delta for assertions
rDelta : REAL := 0.0001;
// delay until final value is reached
timDelay : TIME := T#860MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestRampUpContinuity');
// ramp timer, calc speed & calc next expected ramp step
_fbDelayRampUpContinuity(IN := TRUE, PT := timDelay);
_rMyRampSpeed := 16.567 * (10 / TIME_TO_REAL(T#1S650MS));
_rNextExpectedUp := _rNextExpectedUp + _rMyRampSpeed;
// run RampGenerator
_fbRMPRampUpContinuity(
rTarget := 8.673,
rTargetMin := -7,
rTargetMax := 9.567,
timRampUp := T#1S650MS,
timRampDown := T#1S,
rSetpoint => _rSetpoint,
xInTarget =>);
// check for current expected value
IF NOT _fbDelayRampUpContinuity.Q THEN
AssertEquals_REAL(Expected := _rNextExpectedUp, Actual := _rSetpoint, Delta := rDelta, 'Expected current value was not achieved.');
ELSE
// check for final value
AssertEquals_REAL(Expected := rExpected, Actual := _rSetpoint, Delta := rDelta, 'Value was not reached in time.');
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
<Method Name="TestRampUpTime" Id="{79d093cd-b7fc-4c92-b138-2dee08eb11bc}">
<Declaration><![CDATA[{warning disable C0394}
METHOD TestRampUpTime
VAR
// current ramp setpoint
_rSetpoint : REAL;
END_VAR
VAR CONSTANT
// expected final value
rExpected : REAL := 8.3456;
// delta for assertions
rDelta : REAL := 0.0001;
// delay until final value is reached
timDelay : TIME := T#440MS;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('TestRampUpTime');
// timer until bottom of ramp is supposed to be reached
_fbDelayRampUpTime(IN := TRUE, PT := timDelay);
// run RampGenerator
_fbRMPRampUpTime(
rTarget := 8.3456,
rTargetMin := -10,
rTargetMax := 10,
timRampUp := T#1S70MS,
timRampDown := T#100MS,
rSetpoint => _rSetpoint,
xInTarget =>);
// check whether final value is reach on time or before
IF NOT _fbDelayRampUpTime.Q THEN
AssertTrue(_rSetpoint < rExpected,'Value reached before expected time.');
ELSE
AssertEquals_REAL(Expected := rExpected, Actual := _rSetpoint, Delta := rDelta, 'Value was not reached in time.');
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
</POU>
</TcPlcObject>