fbGetSysTime(sNetID:='', bEnable:=TRUE, dwCycle:=1); //Call the function block to get System Time. Set the FB's required parameters.
tsDateTime:= fbGetSysTime.systemTime; //From the function block call, get the systemTime and copy it into a TIMESTRUCT datatype
fbTimeToReal_1(Execute:= TRUE, DateTime:=tsDateTime, TimeLREAL=> lrSysTimeCurrent); //Convert the TIMESTRUCT to a LREAL datatype.
ONS1(CLK:= bResetTracking);//ONS1 is an R_TRIG(equivalent of a oneshot in RSLogix), and is triggered via 'bResetTracking' Bool.
fbGetCurTaskIndex(); //Function block call to get the current task index number
//If this is the first cycle of the task (just powered up after a powered down state), then set the nCase to 1
IF _TaskInfo[fbGetCurTaskIndex.index].FirstCycle THEN
nCase := 1;
END_IF
CASE nCase OF
1: //Allow 1 second to pass to ensure the FB calls above are done. This is way more time than needed, but insignificant nonetheless.
tmrTON1.PT:=T#1S;
tmrTON1.IN:=TRUE;
IF tmrTON1.Q THEN
nCase := 2;
END_IF
2: //Calculate the amount of time the machine was powered off and add it to the total time monitored.
lrSecondsWhileOff := lrSysTimeCurrent - lrTimeRecordedAtOff;
lrTotalTimeMonitored := lrTotalTimeMonitored + lrSecondsWhileOff;
nCase := 3;
3: //Increment running time of each station and machine, incrementing by 1 every 1-second.
tmr_OnTime(IN:= NOT tmr_OffTime.Q, PT:= T#500MS);
tmr_OffTime(IN:= tmr_Ontime.Q, PT:= T#500MS);
ONS2(CLK:=tmr_OnTime.Q);
IF ONS2.Q THEN
IF GV_Stations.bSTA1Started THEN
lrStation1RunningTime := lrStation1RunningTime + 1;
END_IF
IF GV_Stations.bSTA2Started THEN
lrStation2RunningTime := lrStation2RunningTime + 1;
END_IF
IF GV_Stations.bSTA3Started THEN
lrStation3RunningTime := lrStation3RunningTime + 1;
END_IF
IF GV_Stations.bSTA4Started THEN
lrStation4RunningTime := lrStation4RunningTime + 1;
END_IF
IF GV_Stations.bSTA5Started THEN
lrStation5RunningTime := lrStation5RunningTime + 1;
END_IF
IF GV_Stations.bSTA6Started THEN
lrStation6RunningTime := lrStation6RunningTime + 1;
END_IF
lrTimeRecordedAtOff := lrSysTimeCurrent; //Continually record the LREAL equivalent of the current System Time so that we have it when the machine is powered off.
lrTotalTimeMonitored := lrTotalTimeMonitored + 1; //Keep incrementing Total Time Monitored by 1, every 1-second
//Calculate the utilization of each station and machine. Return the values in a % format
GV_Stations.Sta1Util := (lrStation1RunningTime / lrTotalTimeMonitored)*100;
GV_Stations.Sta2Util := (lrStation2RunningTime / lrTotalTimeMonitored)*100;
GV_Stations.Sta3Util := (lrStation3RunningTime / lrTotalTimeMonitored)*100;
GV_Stations.Sta4Util := (lrStation4RunningTime / lrTotalTimeMonitored)*100;
GV_Stations.Sta5Util := (lrStation5RunningTime / lrTotalTimeMonitored)*100;
GV_Stations.Sta6Util := (lrStation6RunningTime / lrTotalTimeMonitored)*100;
GV_Stations.MachineUtil := ((GV_Stations.Sta1Util + GV_Stations.Sta2Util + GV_Stations.Sta3Util + GV_Stations.Sta4Util + GV_Stations.Sta5Util + GV_Stations.Sta6Util) / 6);
//The calculation of machine utilization % is complete with the lines of code above.
//The following lines of code below and within the remainder of the Case statement are simply for building and concactenating the text strings, and are optional.
stStation1Util := LREAL_TO_FMTSTR(GV_Stations.Sta1Util, 2, TRUE);
stStation2Util := LREAL_TO_FMTSTR(GV_Stations.Sta2Util, 2, TRUE);
stStation3Util := LREAL_TO_FMTSTR(GV_Stations.Sta3Util, 2, TRUE);
stStation4Util := LREAL_TO_FMTSTR(GV_Stations.Sta4Util, 2, TRUE);
stStation5Util := LREAL_TO_FMTSTR(GV_Stations.Sta5Util, 2, TRUE);
stStation6Util := LREAL_TO_FMTSTR(GV_Stations.Sta6Util, 2, TRUE);
stMachineUtil := LREAL_TO_FMTSTR(GV_Stations.MachineUtil, 2, TRUE);
stStation1aUtil := CONCAT(stStation1Util, stUtilization);
stStation2aUtil := CONCAT(stStation2Util, stUtilization);
stStation3aUtil := CONCAT(stStation3Util, stUtilization);
stStation4aUtil := CONCAT(stStation4Util, stUtilization);
stStation5aUtil := CONCAT(stStation5Util, stUtilization);
stStation6aUtil := CONCAT(stStation6Util, stUtilization);
stMachine1Util := CONCAT(stMachineUtil, stUtilization);
GV_Stations.Station1Utilization:= CONCAT(stStation1aUtil, stDateTime);
GV_Stations.Station2Utilization:= CONCAT(stStation2aUtil, stDateTime);
GV_Stations.Station3Utilization:= CONCAT(stStation3aUtil, stDateTime);
GV_Stations.Station4Utilization:= CONCAT(stStation4aUtil, stDateTime);
GV_Stations.Station5Utilization:= CONCAT(stStation5aUtil, stDateTime);
GV_Stations.Station6Utilization:= CONCAT(stStation6aUtil, stDateTime);
GV_Stations.TotalMachineUtilization:= CONCAT(stMachine1Util, stDateTime);
END_IF;
END_CASE
//When this R_TRIG (oneshot) is triggered, reset the utilization % to restart it from 0. This also resets the date started.
IF ONS1.Q THEN
tsTrackTimeStartTime := fbGetSysTime.systemTime;
lrTrackTimeStartTime := lrSysTimeCurrent;
stDateTime:= SYSTEMTIME_TO_STRING(tsTrackTimeStartTime);
lrStation1RunningTime := 0;
lrStation2RunningTime := 0;
lrStation3RunningTime := 0;
lrStation4RunningTime := 0;
lrStation5RunningTime := 0;
lrStation6RunningTime := 0;
lrTotalTimeMonitored := 0;
lrSecondsWhileOff := 0;
bResetTracking := FALSE;
END_IF
GV_Stations.Date_Started := stDateTime;
tmrTON1();
tmr_OnTime();
tmr_OffTime();