From 99934ef97c17ad757a8551b5f52df22dc6f81507 Mon Sep 17 00:00:00 2001 From: "m.heisig" Date: Mon, 16 Feb 2026 11:34:39 +0100 Subject: [PATCH] Fixed ramp generator and tests --- BaseComponents.tsproj | 6 +- BasicComponents/BasicComponents.plcproj | 8 +- .../Components/Controller/FB_RampGen.TcPOU | 86 ------ .../Controller/FB_RampGenerator.TcPOU | 91 ++++++ .../Utilities/FB_RampGenerator.TcPOU | 132 --------- .../Datentypen/ST_HMI_ORP_SENSOR_DATA.TcDUT | 47 --- .../UtilitiesTests/FB_RampGeneratorTest.TcPOU | 269 ++++-------------- 7 files changed, 143 insertions(+), 496 deletions(-) delete mode 100644 BasicComponents/POUs/Components/Controller/FB_RampGen.TcPOU create mode 100644 BasicComponents/POUs/Components/Controller/FB_RampGenerator.TcPOU delete mode 100644 BasicComponents/POUs/Components/Utilities/FB_RampGenerator.TcPOU delete mode 100644 BasicComponents/POUs/HMI/Datentypen/ST_HMI_ORP_SENSOR_DATA.TcDUT diff --git a/BaseComponents.tsproj b/BaseComponents.tsproj index 1cb7207..59b3e7c 100644 --- a/BaseComponents.tsproj +++ b/BaseComponents.tsproj @@ -116,13 +116,13 @@ - + {9FD32FC8-0CF9-4C5B-95FB-F35423496A77} - + @@ -136,7 +136,7 @@ - + BasicComponents Instance {08500001-0000-0000-F000-000000000064} diff --git a/BasicComponents/BasicComponents.plcproj b/BasicComponents/BasicComponents.plcproj index 010b0ee..0042593 100644 --- a/BasicComponents/BasicComponents.plcproj +++ b/BasicComponents/BasicComponents.plcproj @@ -82,7 +82,7 @@ Code - + Code @@ -107,9 +107,6 @@ Code - - Code - Code @@ -154,9 +151,6 @@ Code - - Code - Code diff --git a/BasicComponents/POUs/Components/Controller/FB_RampGen.TcPOU b/BasicComponents/POUs/Components/Controller/FB_RampGen.TcPOU deleted file mode 100644 index 95c302d..0000000 --- a/BasicComponents/POUs/Components/Controller/FB_RampGen.TcPOU +++ /dev/null @@ -1,86 +0,0 @@ - - - - - - 0.0); - RETURN; -END_IF - - -_rDiff := rTarget - rOut; - - -IF _rDiff > 0 THEN - // Aufwärtsrampe - _rStep := rRiseRate * _rT; - - IF _rStep > _rDiff THEN - rOut := rTarget; - ELSE - rOut := rOut + _rStep; - END_IF - -ELSIF _rDiff < 0 THEN - // Abwärtsrampe - _rStep := rFallRate * _rT; - - IF _rStep > ABS(_rDiff) THEN - rOut := rTarget; - ELSE - rOut := rOut - _rStep; - END_IF -END_IF - - -xBusy := (ABS(rTarget - rOut) > 0.0);]]> - - - \ No newline at end of file diff --git a/BasicComponents/POUs/Components/Controller/FB_RampGenerator.TcPOU b/BasicComponents/POUs/Components/Controller/FB_RampGenerator.TcPOU new file mode 100644 index 0000000..6a05817 --- /dev/null +++ b/BasicComponents/POUs/Components/Controller/FB_RampGenerator.TcPOU @@ -0,0 +1,91 @@ + + + + + + 0.0); + RETURN; +END_IF + +// Calculate diff to target +_rDiff := rTarget - _rOut; + +// Check if we need to ramp up or down +IF _rDiff > 0 THEN + // Ramp up + _rStep := ABS(rRiseRate) * _rT; + + // Clamp to target value + IF _rStep > _rDiff THEN + _rOut := rTarget; + ELSE + _rOut := _rOut + _rStep; + END_IF +ELSIF _rDiff < 0 THEN + // Ramp down + _rStep := ABS(rFallRate) * _rT; + + // Clamp to target value + IF _rStep > ABS(_rDiff) THEN + _rOut := rTarget; + ELSE + _rOut := _rOut - _rStep; + END_IF +END_IF + +// Handle busy flag +xBusy := (ABS(rTarget - _rOut) > 0.0); + +// Copy internals to outputs +rOut := _rOut;]]> + + + + + + + + + \ No newline at end of file diff --git a/BasicComponents/POUs/Components/Utilities/FB_RampGenerator.TcPOU b/BasicComponents/POUs/Components/Utilities/FB_RampGenerator.TcPOU deleted file mode 100644 index 5983a0e..0000000 --- a/BasicComponents/POUs/Components/Utilities/FB_RampGenerator.TcPOU +++ /dev/null @@ -1,132 +0,0 @@ - - - - - - T#0S THEN - _rRampUpSpeed := (rTargetMax - rTargetMin) * (_rCycleTime / TIME_TO_REAL(timRampUp)); -ELSE - _rRampUpSpeed := rTargetMax; -END_IF -IF timRampDown <> T#0S THEN - _rRampDownSpeed := -(rTargetMax - rTargetMin) * (_rCycleTime / TIME_TO_REAL(timRampDown)); -ELSE - _rRampDownSpeed := -rTargetMax; -END_IF - - -// Calculate distance left to go -_rDistanceToGo := rTarget - rSetpoint; - -// Calculate new setpoint -IF (_rDistanceToGo > 0.0) THEN - IF (_rDistanceToGo > _rRampUpSpeed) THEN - rSetpoint := rSetpoint + _rRampUpSpeed; - ELSE - rSetpoint := rTarget; - END_IF -ELSIF (_rDistanceToGo < 0.0) THEN - IF (_rDistanceToGo < _rRampDownSpeed) THEN - rSetpoint := rSetpoint + _rRampDownSpeed; - ELSE - rSetpoint := rTarget; - END_IF -ELSE - rSetpoint := rTarget; -END_IF - -// Check if we are in range of target range -IF ABS(rSetpoint-rTarget) <= 0.001 THEN - xInTarget := TRUE; -ELSE - xInTarget := FALSE; -END_IF ]]> - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/BasicComponents/POUs/HMI/Datentypen/ST_HMI_ORP_SENSOR_DATA.TcDUT b/BasicComponents/POUs/HMI/Datentypen/ST_HMI_ORP_SENSOR_DATA.TcDUT deleted file mode 100644 index 941f421..0000000 --- a/BasicComponents/POUs/HMI/Datentypen/ST_HMI_ORP_SENSOR_DATA.TcDUT +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/BasicComponents/POUs/Unittests/UtilitiesTests/FB_RampGeneratorTest.TcPOU b/BasicComponents/POUs/Unittests/UtilitiesTests/FB_RampGeneratorTest.TcPOU index d09b19a..5667299 100644 --- a/BasicComponents/POUs/Unittests/UtilitiesTests/FB_RampGeneratorTest.TcPOU +++ b/BasicComponents/POUs/Unittests/UtilitiesTests/FB_RampGeneratorTest.TcPOU @@ -3,17 +3,6 @@ - - - - 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();]]> - - _rSetpoint, - xInTarget => _xInTarget); + rRiseRate := 2, + rFallRate := 2, + rOut => _rSetpoint, + xBusy =>); // check for whether InTarget is reached at the specified time IF NOT _fbDelayInTargetResultDown.Q THEN - AssertFalse(_xInTarget, 'InTarget reached earlier then expected.'); + AssertTrue(_fbRMPInTargetResultDown.xBusy, 'InTarget reached earlier then expected.'); ELSE - AssertTrue(_xInTarget, 'InTarget not reached in time.'); + AssertFalse(_fbRMPInTargetResultDown.xBusy, 'InTarget not reached in time.'); TEST_FINISHED(); END_IF]]> @@ -138,16 +84,13 @@ END_IF]]> _rSetpoint, - xInTarget => _xInTarget); + rRiseRate := 2, + rFallRate := 2, + rOut => _rSetpoint, + xBusy => ); // check for whether InTarget is reached at the specified time -IF NOT _fbDelayInTargetResultUp.Q THEN - AssertFalse(_xInTarget, 'InTarget reached earlier then expected.'); +IF (NOT _fbDelayInTargetResultUp.Q) THEN + AssertTrue(_fbRMPInTargetResultUp.xBusy, 'InTarget reached earlier then expected.'); ELSE - AssertTrue(_xInTarget, 'InTarget not reached in time.'); + AssertFalse(_fbRMPInTargetResultUp.xBusy, 'InTarget not reached in time.'); TEST_FINISHED(); -END_IF]]> - - - - - - _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]]> - - - - - - _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]]> @@ -297,31 +129,30 @@ END_VAR VAR CONSTANT // expected final value - rExpected : REAL := -9.4564; + rExpected : REAL := -0.8; // delta for assertion rDelta : REAL := 0.0001; // delay until final value is reached - timDelay : TIME := T#470MS; + timDelay : TIME := T#790MS; END_VAR]]> _rSetpoint, - xInTarget =>); + xEnable := TRUE, + rTarget := -1, + rRiseRate := 1, + rFallRate := 1, + rOut => _rSetpoint, + xBusy =>); // check for current expected value IF NOT _fbDelayRampDownContinuity.Q THEN @@ -360,12 +191,10 @@ _fbDelayRampDownTime(IN := TRUE, PT := timDelay); // run RampGenerator _fbRMPRampDownTime( rTarget := -7.4367, - rTargetMin := -10, - rTargetMax := 10, - timRampUp := T#500MS, - timRampDown := T#100MS, - rSetpoint => _rSetpoint, - xInTarget =>); + rRiseRate := 20, + rFallRate := 100, + rOut => _rSetpoint, + xBusy =>); // check whether final value is reach on time or before IF NOT _fbDelayRampDownTime.Q THEN @@ -390,31 +219,30 @@ END_VAR VAR CONSTANT // expected final value - rExpected : REAL := 8.673; + rExpected : REAL := 0.8; // delta for assertions rDelta : REAL := 0.0001; // delay until final value is reached - timDelay : TIME := T#860MS; + timDelay : TIME := T#790MS; END_VAR]]> _rSetpoint, - xInTarget =>); + xEnable := TRUE, + rTarget := 2, + rRiseRate := 1, + rFallRate := 1, + rOut => _rSetpoint, + xBusy =>); // check for current expected value IF NOT _fbDelayRampUpContinuity.Q THEN @@ -436,13 +264,13 @@ END_VAR VAR CONSTANT // expected final value - rExpected : REAL := 8.3456; + rExpected : REAL := 0.55; // delta for assertions rDelta : REAL := 0.0001; // delay until final value is reached - timDelay : TIME := T#440MS; + timDelay : TIME := T#550MS; END_VAR]]> _rSetpoint, - xInTarget =>); + rTarget := 0.55, + xEnable := TRUE, + rRiseRate := 1, + rFallRate := 100, + rOut => _rSetpoint, + xBusy =>); // check whether final value is reach on time or before IF NOT _fbDelayRampUpTime.Q THEN