Obsidean_VM/01-Documentation/Vetromeccanica/Time DeltaTime TIME_TCK().md

48 lines
1.6 KiB
Markdown

***
* Temp:
* TimeFromRUN : DINT
* DeltaTime : DINT
* Static:
* TimeRecorded : DINT
```pascal
#TimeFromRUN := TIME_TO_DINT(TIME_TCK());
IF #TimeFromRUN < #TimeRecorded THEN
#TimeRecorded := #TimeFromRUN;
END_IF;
// Time from last call
#DeltaTime := #TimeFromRUN - #TimeRecorded;
#TimeRecorded := #TimeFromRUN;
IF #DeltaTime = 0 THEN
RETURN;
END_IF;
```
This allow to calcolate a fix number related to the cicle of the PLC. If the cycle of the PLC is faster then then `Smoothed Time` will be grater.
So then the `Actual_Torque_Smoth` can be calculated independently of the speed of the PLC
```pascal
// 500 ms of smooth
#"Smoothed Time" := (#"Smoothed Time" * 999.0 + (500 / #DeltaTime)) / 1000.0;
IF (#"Smoothed Time" > 0) THEN // not divide by 0
#Actual_Torque_Smoth := (#Actual_Torque_Smoth * REAL_TO_INT(#"Smoothed Time"-1) + INT_TO_REAL(#Motor.STATUS_VFD_ACT_Torque)) / REAL_TO_INT( #"Smoothed Time");
IF (#Actual_Torque_Smoth < 0.001) OR NOT (#Actual_Torque_Smoth < 0 OR #Actual_Torque_Smoth > 0) THEN // Nan Check
#Actual_Torque_Smoth :=0;
END_IF;
// 70 bot a 1000
#Actual_Speed_Smoth := (#Actual_Speed_Smoth * REAL_TO_INT(#"Smoothed Time"-1) + INT_TO_REAL(#Motor.STATUS_VFD_ACT_Speed_Hz)) / REAL_TO_INT( #"Smoothed Time");
IF (#Actual_Speed_Smoth < 0.001) OR NOT (#Actual_Speed_Smoth < 0 OR #Actual_Speed_Smoth > 0) THEN // Nan Check
#Actual_Speed_Smoth :=0;
END_IF;
END_IF;
```
The parameter 500 means aprox 500ms independently of the PLC cycle.
The check `OR NOT (#Actual_Speed_Smoth < 0 OR #Actual_Speed_Smoth > 0)` is for the cases on NaN. So in this cases it reset the value to 0.