Skip to content

Commit

Permalink
Bug fixes, code clean up and new timer
Browse files Browse the repository at this point in the history
  • Loading branch information
fisothemes committed Jul 21, 2022
1 parent 34818cf commit 0326346
Show file tree
Hide file tree
Showing 32 changed files with 361 additions and 282 deletions.
Binary file modified .vs/Retentive Timers/v15/.suo
Binary file not shown.
44 changes: 39 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ This project creates retentive variants of the standard library PLC timers:

**In this project the following will be implementated:**

* 👍 FB_RTON - Retentive Timer On-Delay
* 👍 FB_RTOF - Retentive Timer Off-Delay
* 👍 FB_RTP - Retentive Pulse Timer
* 👍 FB_Timer - Pausable Timer
* 👍 FB_RTimer - Retentive Pausable Timer
* 👍 FB_RTON - Retentive Timer On-Delay
* 👍 FB_RTOF - Retentive Timer Off-Delay
* 👍 FB_RTP - Retentive Pulse Timer
* 👍 FB_Timer - Pausable Timer
* 👍 FB_RTimer - Retentive Pausable Timer
* 👍 FB_Stopwatch - Normal Stopwatch
* 👍 FB_RStopwatch - Retentive Stopwatch

- - - -
### FB_RTON
Expand Down Expand Up @@ -104,3 +106,35 @@ fbRTimer : FB_RTimer;
```Pascal
fbRTimer(bStart := bStart, bPause := bPause, tSet := tSet_Time, bElapsed => bElapsed_RTime, tElapsed => tElapsed_RTime);
```

- - - -
## FB_Stopwatch

**Declaration:**
```Pascal
(* <name of timer> : FB_Stopwatch *)
// Declaring a Stopwatch
fbStopwatch : FB_Stopwatch;
```

**Implimentation**
```Pascal
fbStopwatch(bStart := bStart, tElapsed => tElapsed_Time);
```

- - - -
## FB_RStopwatch

**Declaration:**
```Pascal
(* <name of timer> : FB_RStopwatch *)
// Declaring a Stopwatch
fbRStopwatch : FB_RStopwatch;
```

**Implimentation**
```Pascal
fbRStopwatch(bStart := bStart, tElapsed => tElapsed_Time);
```
9 changes: 3 additions & 6 deletions Retentive Timers/Retentive Timers.tsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
<Task Id="3" Priority="20" CycleTime="100000" AmsPort="350" AdtTasks="true">
<Name>PlcTask</Name>
</Task>
<Task Id="4" Priority="21" CycleTime="100000" AmsPort="351" AdtTasks="true">
<Name>PlcTask1</Name>
</Task>
</Tasks>
</System>
<Plc>
Expand All @@ -24,14 +21,14 @@
<Id NeedCalleeCall="true">0</Id>
<Name>PlcTask</Name>
<ManualConfig>
<OTCID>#x02010040</OTCID>
<OTCID>#x02010030</OTCID>
</ManualConfig>
<Priority>21</Priority>
<Priority>20</Priority>
<CycleTime>10000000</CycleTime>
</Context>
</Contexts>
<TaskPouOids>
<TaskPouOid Prio="21" OTCID="#x08502001"/>
<TaskPouOid Prio="20" OTCID="#x08502001"/>
</TaskPouOids>
</Instance>
</Project>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.4">
<DUT Name="ST_PLC_TASK_INFORMATION" Id="{a37eef95-8873-4302-8f44-a07702bc3028}">
<Declaration><![CDATA[TYPE ST_PLC_TASK_INFORMATION EXTENDS PlcTaskSystemInfo :
STRUCT
tCycleTime : TIME; // Current task cycle time
tlCycleTime : LTIME; // Current task cycle time in LTIME
END_STRUCT
END_TYPE
]]></Declaration>
</DUT>
</TcPlcObject>
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
<Declaration><![CDATA[PROGRAM P_Example_1_TONs
VAR
bStart : BOOL;
tSet_Time : TIME := T#10M;
tSet_Time : TIME := T#10S;
tElapsed_Time_TON,
tElapsed_Time_RTON : TIME;
bElapsed_Time_TON,
bElapsed_Time_RTON : BOOL;
fbTON : TON;
fbRTON : FB_RTON;
stPLC_Task_Info : ST_PLC_TASK_INFORMATION;
nCC, nRCC : UDINT;
END_VAR
]]></Declaration>
<Implementation>
Expand Down Expand Up @@ -53,7 +54,8 @@ fbRTON(IN := bStart, PT := tSet_Time,
<LineId Id="51" Count="0" />
<LineId Id="67" Count="0" />
<LineId Id="66" Count="0" />
<LineId Id="68" Count="1" />
<LineId Id="68" Count="0" />
<LineId Id="87" Count="0" />
<LineId Id="71" Count="0" />
<LineId Id="70" Count="0" />
</LineIds>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Declaration><![CDATA[PROGRAM P_Example_2_TOFs
VAR
bStart : BOOL;
tSet_Time : TIME := T#1M;
tSet_Time : TIME := T#10S;
tElapsed_Time_TOF,
tElapsed_Time_RTOF : TIME;
bElapsed_Time_TOF,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Declaration><![CDATA[PROGRAM P_Section_2
VAR
bStart, bPause : BOOL;
tSet_Time : TIME := T#1M;
tSet_Time : TIME := T#10S;
tElapsed_Time,
tElapsed_RTime : TIME;
bElapsed_Time,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.4">
<POU Name="P_Example_Stopwatch" Id="{869ebe88-c85f-4038-b57e-d41e80f8402e}" SpecialFunc="None">
<Declaration><![CDATA[PROGRAM P_Example_Stopwatch
VAR
bStart : BOOL;
tElapsed_Time_Stopwatch,
tElapsed_Time_RStopwatch : TIME;
fbStopwatch : FB_Stopwatch;
fbRStopwatch : FB_RStopwatch;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[(*
NOTES:
-> Activate Configuration and START(F5) PLC Boot Project
-> Login (if you haven't already)
-> Make sure both timers are set for 10 mins
-> Start the timers by writing a TRUE to the bStart variable
-> Let the timers run for a little bit
-> Notice that both elapsed time read are the same
-> STOP(Shift+F5) the PLC Boot Project for approx. 10-30secs
-> START(F5) the PLC Boot Project
*)
fbStopwatch(bStart := bStart, tElapsed => tElapsed_Time_Stopwatch);
fbRStopwatch(bStart := bStart, tElapsed => tElapsed_Time_RStopwatch); ]]></ST>
</Implementation>
<LineIds Name="P_Example_Stopwatch">
<LineId Id="15" Count="9" />
<LineId Id="28" Count="2" />
<LineId Id="32" Count="0" />
</LineIds>
</POU>
</TcPlcObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.4">
<POU Name="P_Section_3" Id="{9dfa112c-c28e-495f-b48c-fe41abc3434c}" SpecialFunc="None">
<Declaration><![CDATA[PROGRAM P_Section_3
VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[P_Example_Stopwatch();]]></ST>
</Implementation>
<LineIds Name="P_Section_3">
<LineId Id="6" Count="0" />
</LineIds>
</POU>
</TcPlcObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.4">
<POU Name="FB_RStopwatch" Id="{924f45b3-a994-4dee-b158-033e4b7ec793}" SpecialFunc="None">
<Declaration><![CDATA[// Retentive stopwatch
FUNCTION_BLOCK FB_RStopwatch
VAR_INPUT
bStart : BOOL;
END_VAR
VAR_OUTPUT
tElapsed : TIME;
END_VAR
VAR
stPLC_Task_Info : ST_PLC_TASK_INFORMATION;
tAccumilator : TIME;
fbTON : FB_RTON;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[IF bStart THEN stPLC_Task_Info := F_Get_PLC_Task_Information(); END_IF
fbTON(PT := stPLC_Task_Info.tCycleTime*2 + tAccumilator);
fbTON(IN := bStart AND NOT(fbTON.Q));
tAccumilator := fbTON.ET;
IF bStart THEN tElapsed := tAccumilator; END_IF]]></ST>
</Implementation>
<LineIds Name="FB_RStopwatch">
<LineId Id="53" Count="3" />
<LineId Id="17" Count="0" />
</LineIds>
</POU>
</TcPlcObject>
77 changes: 26 additions & 51 deletions Retentive Timers/RetentiveTimers/POUs/Function Blocks/FB_RTOF.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,29 @@ VAR_OUTPUT
ET : TIME; // Elapsed time
END_VAR
VAR
bMeasure_Cycle_Time : BOOL;
tStart_Timestamp : TIME;
arCycle_Time : ARRAY[0..1] OF TIME; // 0: Actual cycle time 1: Copy of cycle time
eState : (IDLE, READY, START);
stPLC_Task_Info : ST_PLC_TASK_INFORMATION;
tAccumulator : TIME;
nLast_Cycle_Count : UDINT;
eState : (IDLE, RUN);
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[LBL_TOP:
CASE eState OF
CASE eState OF
IDLE:
IF IN THEN eState := READY; JMP LBL_TOP; END_IF
READY:
Q := TRUE; ET := T#0MS;
IF NOT(IN) THEN // Start Cycle time measurement and move to next state
bMeasure_Cycle_Time := TRUE;
tStart_Timestamp := TIME();
eState := START;
IF IN THEN Q := TRUE; tAccumulator := ET := T#0MS; eState := RUN; JMP LBL_TOP; END_IF
RUN:
stPLC_Task_Info := F_Get_PLC_Task_Information();
IF NOT(IN) AND ((nLast_Cycle_Count <> stPLC_Task_Info.CycleCount) OR nLast_Cycle_Count = 0) THEN
Q := ET <> PT;
IF (ET >= PT) THEN Q := FALSE; eState := IDLE; JMP LBL_TOP; END_IF
tAccumulator := tAccumulator + stPLC_Task_Info.tCycleTime;
ET := LIMIT(T#0MS, THIS^.Lerp(T#0MS, PT, TO_REAL(tAccumulator)/TO_REAL(PT))-stPLC_Task_Info.tCycleTime, PT);
END_IF
START:
IF bMeasure_Cycle_Time THEN
arCycle_Time[0] := arCycle_Time[1] := TIME() - tStart_Timestamp;
bMeasure_Cycle_Time := FALSE;
END_IF
THIS^.Increment(arCycle_Time[0], arCycle_Time[1]);
ET := LIMIT(T#0MS, THIS^.Lerp(T#0MS, PT, TO_REAL(arCycle_Time[0])/TO_REAL(PT))-arCycle_Time[1], PT);
IF (ET = PT) THEN Q := FALSE; eState := IDLE; JMP LBL_TOP; END_IF
IF IN THEN ET := T#0MS; eState := IDLE; JMP LBL_TOP; END_IF
nLast_Cycle_Count := stPLC_Task_Info.CycleCount;
IF IN THEN tAccumulator := ET := T#0MS;END_IF
END_CASE]]></ST>
</Implementation>
<Method Name="Increment" Id="{7b36d870-5ab6-4877-8cf2-57f450b8b3d3}">
<Declaration><![CDATA[METHOD PRIVATE Increment
VAR_INPUT
tValue : REFERENCE TO TIME;
tIncrement : TIME;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[tValue := tValue + tIncrement;
]]></ST>
</Implementation>
</Method>
<Method Name="Lerp" Id="{36a01201-4a8e-4415-91bf-222a16e52a06}">
<Declaration><![CDATA[// Linear interpolation (Lerp) - this method interpolates values between 2 time points.
METHOD PRIVATE Lerp : TIME
Expand All @@ -73,21 +52,17 @@ Lerp := TO_TIME(TO_REAL(tStart) + TO_REAL(tFinish - tStart)*fPercentage);]]></ST
</Implementation>
</Method>
<LineIds Name="FB_RTOF">
<LineId Id="417" Count="0" />
<LineId Id="348" Count="1" />
<LineId Id="388" Count="0" />
<LineId Id="421" Count="0" />
<LineId Id="390" Count="0" />
<LineId Id="416" Count="0" />
<LineId Id="392" Count="3" />
<LineId Id="391" Count="0" />
<LineId Id="356" Count="10" />
<LineId Id="420" Count="0" />
<LineId Id="179" Count="0" />
</LineIds>
<LineIds Name="FB_RTOF.Increment">
<LineId Id="16" Count="0" />
<LineId Id="13" Count="0" />
<LineId Id="463" Count="1" />
<LineId Id="468" Count="0" />
<LineId Id="466" Count="0" />
<LineId Id="469" Count="0" />
<LineId Id="490" Count="0" />
<LineId Id="472" Count="0" />
<LineId Id="487" Count="1" />
<LineId Id="475" Count="2" />
<LineId Id="482" Count="0" />
<LineId Id="481" Count="0" />
<LineId Id="467" Count="0" />
</LineIds>
<LineIds Name="FB_RTOF.Lerp">
<LineId Id="9" Count="0" />
Expand Down
Loading

0 comments on commit 0326346

Please sign in to comment.