kmonroe023 Posted August 24, 2015 Posted August 24, 2015 I've noticed what I think is a bug with the way subroutines handle local (L) variables. I have a simple PLC that calls 2 subprograms, the first toggles the return value "bSecToggle" from 0 to 1 at the rate designated by the first variable passed. In the example below the output variable toggles at a rate of one second. Calling this subroutine is conditioned upon variable "foo1". The second subroutine simply accumulates a timer as long as the passed variable is non-zero. I've noticed that if both subroutines are called, neither functions as expected. If I set "foo1" equal to 0 to disable calling the first subroutine, the second performs as expected. Likewise if I disable the second subroutine the first functions as expected. It appears that the local variables of one subroutine are stepping on the other. Is this the desired behavior? I assumed that local variables inside sub-programs were unique to each sub-program. However, my tests indicate otherwise. Please see my code below: open plc 1 // one second toggle timer if (foo1 == 1){ call toggleTimer(1,&bSecToggle); } // Cycle Timer logic call mainCycleTimer(bCutterRunning,&cycleTime); close Here is the subroutine code (both subprogs are in the same .pmc file): open subprog mainCycleTimer (CutterRunning,&timerOut) local CycleTimeStarted,CycleTimeStart; if(CutterRunning==1 && CycleTimeStarted!=1) { CycleTimeStart=Sys.Time; CycleTimeStarted=1; } if (CutterRunning==1) { timerOut=Sys.Time - CycleTimeStart; } else {CycleTimeStarted=0} return; close open subprog toggleTimer (tTime,&outFlag) local ToggleTimer,ToggleTimeStart; // Toggle timer logic if (ToggleTimeStart==0){ ToggleTimeStart=Sys.Time; } else{ ToggleTimer=Sys.Time-ToggleTimeStart } if (ToggleTimer>tTime){ ToggleTimeStart=0; ToggleTimer=0; if (outFlag==0){ outFlag = 1; } else {outFlag=0} } return; close
curtwilson Posted August 24, 2015 Posted August 24, 2015 Each top level program in Power PMAC (each PLC program, each coordinate system for a motion program) has its own stack of local variables. But for a given top-level program, it is the same stack when different subroutines are called. This explains the behavior you are seeing. It is good general practice -- and needed in your case -- not to expect that subroutines can retain a value from one call to the next in a local variable.
Recommended Posts