From 74b1b968471ab65c961bc2647fee063caf0d577f Mon Sep 17 00:00:00 2001 From: KaushikMalapati Date: Wed, 9 Oct 2024 23:54:46 -0700 Subject: [PATCH] fixing indexing bug --- .../Library/Devices/PPM/FB_PPM_PowerMeter.TcPOU | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lcls-twincat-common-components/Library/Devices/PPM/FB_PPM_PowerMeter.TcPOU b/lcls-twincat-common-components/Library/Devices/PPM/FB_PPM_PowerMeter.TcPOU index 186e8e1..d8917ab 100644 --- a/lcls-twincat-common-components/Library/Devices/PPM/FB_PPM_PowerMeter.TcPOU +++ b/lcls-twincat-common-components/Library/Devices/PPM/FB_PPM_PowerMeter.TcPOU @@ -214,7 +214,7 @@ IF rTrig_Background.Q OR bResetAutoBackground THEN // Dump 5s worth of readings IF udBackgroundVoltageBufferIndex > 6 THEN FOR i := (udBackgroundVoltageBufferIndex - 5) TO (udBackgroundVoltageBufferIndex - 1) DO - fBackgroundVoltageSum := fBackgroundVoltageSum - fBackgroundVoltageBuffer[i MOD 100]; + fBackgroundVoltageSum := fBackgroundVoltageSum - fBackgroundVoltageBuffer[((i-1) MOD 100) + 1]; END_FOR udBackgroundVoltageBufferIndex := udBackgroundVoltageBufferIndex - 5; IF udBackgroundVoltageBufferIndex <> 1 AND BACKGROUND_MODE = Passive THEN @@ -233,8 +233,8 @@ ELSIF eEnumGet = E_PPM_States.OUT AND stYAxisState = 0 THEN // Every second, move fBackgroundVoltageAcc into buffer and sum IF tonBackgroundAutoCollecting.Q = TRUE THEN tonBackgroundAutoCollecting( IN := FALSE); - fBackgroundVoltageStale := fBackgroundVoltageBuffer[udBackgroundVoltageBufferIndex MOD 100]; - fBackgroundVoltageBuffer[udBackgroundVoltageBufferIndex MOD 100] := fBackgroundVoltageAcc / uAccCount; + fBackgroundVoltageStale := fBackgroundVoltageBuffer[((udBackgroundVoltageBufferIndex - 1) MOD 100) + 1]; + fBackgroundVoltageBuffer[((udBackgroundVoltageBufferIndex - 1) MOD 100) + 1] := fBackgroundVoltageAcc / uAccCount; // Remove overwritten voltage IF udBackgroundVoltageBufferIndex > 100 THEN fBackgroundVoltageSum := fBackgroundVoltageSum - fBackgroundVoltageStale;