Skip to content

Commit

Permalink
Fix time measurament issue when system goes to sleep/hibernate
Browse files Browse the repository at this point in the history
  • Loading branch information
arkypita committed Aug 27, 2023
1 parent e513d69 commit 0abfc91
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion LaserGRBL/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// È possibile specificare tutti i valori oppure impostare valori predefiniti per i numeri relativi alla revisione e alla build
// utilizzando l'asterisco (*) come descritto di seguito:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("5.4.0")]
[assembly: AssemblyVersion ("5.5.0")]
[assembly: NeutralResourcesLanguage("en")]
61 changes: 45 additions & 16 deletions LaserGRBL/Core/GrblCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3851,12 +3851,18 @@ internal ListLLC GetClone()

return clone;
}

//internal void FixWeirdMeasures()
//{
// foreach (LaserLifeCounter LLC in this)
// LLC.FixWeirdMeasures();
//}
}

static ListLLC mLLCL;
static LaserLifeCounter mCurrentLLC;
private static long mLastStatusHiResTimeNano;
private static long mLastPowerHiResTimeNano;
private static long? mLastStatusHiResTimeNano;
private static long? mLastPowerHiResTimeNano;
private static long mLastSave;
private static string mLLCFileName;

Expand Down Expand Up @@ -3946,7 +3952,7 @@ internal void AddTrueLaserTimePower(TimeSpan elapsed, double powerperc)
clx = Math.Min(9, Math.Max(0, clx)); //ensure 0-9 range
mTimeClasses[clx] = mTimeClasses[clx] + elapsed;
}

mLastUsage = DateTime.Today;
//System.Diagnostics.Debug.WriteLine($"LT: {mTimeInRun.TotalSeconds} LTT: {mTimeUsageNormalizedPower.TotalSeconds}");
}

Expand All @@ -3970,6 +3976,22 @@ void IDeserializationCallback.OnDeserialization(object sender)
if (mTimeClasses == null)
mTimeClasses = new TimeSpan[10];
}

//internal void FixWeirdMeasures()
//{
// TimeSpan TT = TimeSpan.Zero;
// foreach (TimeSpan TS in Classes)
// TT = TT.Add(TS);

// if (
// LastUsage.HasValue &&
// DateTime.Today < new DateTime(2023, 12, 31) && // smette di farlo ad una certa data
// TimeInRun.TotalHours > 100 && // almeno 100 ore
// TimeInRun.TotalHours > ((LastUsage.Value - MonitoringDate.Value).TotalHours + 24) * 2 && //TIR > 2 * DELTA TRA DATE MONITORATE
// TimeInRun.TotalHours > TT.TotalHours * 100 //TIR > 100 volte Classes.Time
// )
// mTimeInRun = TimeSpan.FromHours(TT.TotalHours * 1.5); //ri-assegna da TT (total time preso dalle classi) esteso * 1.5
//}
}

static LaserLifeHandler()
Expand All @@ -3982,6 +4004,8 @@ static LaserLifeHandler()

if (mLLCL == null) mLLCL = new ListLLC();
if (mLLCL.Count == 0) mLLCL.Add(LaserLifeCounter.CreateDefault());

//mLLCL.FixWeirdMeasures();
}
}

Expand All @@ -3992,13 +4016,15 @@ public static void ComputeLaserTime(GrblCore.MacStatus status)
lock (mLLCL)
{
long now = HiResTimer.TotalNano;
if (mLastStatusHiResTimeNano != 0)
if (mLastStatusHiResTimeNano != null)
{
double delta = now - mLastPowerHiResTimeNano;
double perc = status == MacStatus.Run ? 1 : 0; //potenza da applicare a questo delta
double normal = delta * perc;

mCurrentLLC?.AddRunTime(TimeSpan.FromMilliseconds(normal / 1000 / 1000));
long delta = now - mLastPowerHiResTimeNano.Value;
if (delta > 0 && delta < 600000000000L) //do not add delta if bigger then 600s (or negative) - just a safety test
{
double perc = status == MacStatus.Run ? 1 : 0; //potenza da applicare a questo delta
double normal = delta * perc;
mCurrentLLC?.AddRunTime(TimeSpan.FromMilliseconds(normal / 1000 / 1000));
}
}
mLastStatusHiResTimeNano = now;
}
Expand All @@ -4020,15 +4046,18 @@ public static void ComputeLaserTrueTime(float power)
lock (mLLCL)
{
long now = HiResTimer.TotalNano;
if (mLastPowerHiResTimeNano != 0)
if (mLastPowerHiResTimeNano != null)
{
double elapsed = now - mLastPowerHiResTimeNano;
decimal maxpwm = Configuration.MaxPWM;
long delta = now - mLastPowerHiResTimeNano.Value;
if (delta > 0 && delta < 600000000000L) //do not add delta if bigger then 600s (or negative) - just a safety test
{
decimal maxpwm = Configuration.MaxPWM;

if (maxpwm >= 10 && maxpwm <= 100000) //we have a configured value in a valid range
{
double powerperc = Math.Max(0, Math.Min(1, (double)power / (double)maxpwm)); //potenza da applicare a questo delta
mCurrentLLC?.AddTrueLaserTimePower(TimeSpan.FromMilliseconds(elapsed / 1000 / 1000), powerperc);
if (maxpwm >= 10 && maxpwm <= 100000) //we have a configured value in a valid range
{
double powerperc = Math.Max(0, Math.Min(1, (double)power / (double)maxpwm)); //potenza da applicare a questo delta
mCurrentLLC?.AddTrueLaserTimePower(TimeSpan.FromMilliseconds((double)delta / 1000000.0), powerperc);
}
}
}
mLastPowerHiResTimeNano = now;
Expand Down
1 change: 1 addition & 0 deletions LaserGRBL/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ void MainFormFormClosing(object sender, FormClosingEventArgs e)

private void UpdateTimer_Tick(object sender, EventArgs e)
{
long foo = HiResTimer.TotalNano; //ensure call TotalNano to be able to detect and fix sleep/hibernation
TimerUpdate();
ConnectionForm.TimerUpdate();
PreviewForm.TimerUpdate();
Expand Down
10 changes: 7 additions & 3 deletions LaserGRBL/Tools/HiResTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,15 @@ public static long TotalNano
ComputeFrequency(now);

TimeReference delta = now.Subtract(mLastReference);
long deltaNano = delta.IsGoodHiRes() ? delta.HiResNano : delta.LowResNano;
long nanoSleepThreshold = 10000000000L; // se passano più di 10s tra due chiamate abbiamo avuto una sleep da non conteggiare! (assicurati di chiamare TotalNano con una frequenza più alta)

if (delta.IsGoodHiRes())
mTotalNano = mTotalNano + Math.Max(0, delta.HiResNano);
if (deltaNano < 0)
LaserGRBL.Logger.LogMessage("Issue detector", "Negative delta detected!");
else if (deltaNano > nanoSleepThreshold)
LaserGRBL.Logger.LogMessage("Issue detector", "System sleep/hibernation detected ({0}s)", deltaNano / 1000000000L);
else
mTotalNano = mTotalNano + Math.Max(0, delta.LowResNano);
mTotalNano += deltaNano;

mLastReference = now;

Expand Down
2 changes: 1 addition & 1 deletion setup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "LaserGRBL"
#define MyAppVersion "5.4.0"
#define MyAppVersion "5.5.0"
#define MyAppVersionName "Rhydon"
#define MyAppPublisher "LaserGRBL"
#define MyAppURL "https://lasergrbl.com"
Expand Down

0 comments on commit 0abfc91

Please sign in to comment.