Does anyone have a more efficient way or a simpler way to get robot cycle time using Karel?
I wrote the code below, but it seems like it's always off by a second or less, sometimes more. This is of course when I compare it with me looking at the robot and using my smartphone stopwatch. I don't understand why this is!
Thanks for your ideas?
Code
---------------------------------------------------------------------
---- Section: Get Cycle Times
---------------------------------------------------------------------
WRITE ('TASKS RUNNING AVERAGES (seconds)', CR)
WRITE ('------------------------------------', CR)
--Run this program for only 5 minutes then shutdown
WHILE (finished = FALSE) AND (runPeriod < runTime) DO
--Turn ON Digital Outpput 200
DOUT[200] = ON
--WRITE ('clock_P2P',(clock_P2P/1000)::9::2, CR)
--Do not START timing until output #4 "DO_Rbt_In_Cycle" is ON
WAIT FOR DOUT[4] = ON
--WRITE ('DO_Rbt_In_Cycle',DOUT[4], CR)
--Get value from Register (R835) "l_nTaskEcho"
GET_REG(835,FALSE,taskNo,realVal,Status)
--Gets the actual running speed from Register "GI_Speed"
GET_REG(848,FALSE,actual_Speed,realVal,Status)
--Sets PLC speed to Register 13
SET_INT_REG(13, actual_Speed, Status)
-- TP_CLS
-- WRITE ('l_nTaskEcho',taskNo, CR)
-- WRITE ('GI_Speed',actual_Speed, CR)
--This porgram can only handle 15 TASKS and would not run if we are over.
IF (taskNo > 0) AND (taskNo < 16) THEN
--Reset timer variables
clock_tasks = 0
-- WRITE ('DO_Rbt_In_Cycle',DOUT[4], CR)
--Start Timing the current Task Cycle Time
CONNECT TIMER TO clock_tasks
CONNECT TIMER TO clock_P2P
--Do not STOP timing until output #4 "DO_Rbt_In_Cycle" is OFF
WAIT FOR DOUT[4] = OFF
--WRITE ('DO_Rbt_In_Cycle',DOUT[4], CR)
--Move the value of clock_tasks to a Real register
cycle_Time = clock_tasks / 1000
--Keep track of the number of MINUTES this program has run for
runPeriod = runPeriod + clock_tasks / 1000
--Compute the Running Average based on the current task running
Processes[taskNo].RunningTask = taskNo
Processes[taskNo].TotalCycleTime = Processes[taskNo].TotalCycleTime + cycle_Time
Processes[taskNo].NumCycles = Processes[taskNo].NumCycles + 1
Processes[taskNo].RunningAverage = Processes[taskNo].TotalCycleTime / Processes[taskNo].NumCycles
--Move the value to the starting Register matching the Task number + 600
SET_REAL_REG((taskNo + 600), Processes[taskNo].RunningAverage, Status)
WRITE ('Task',taskNo, ' Running Average', (Processes[taskNo].RunningAverage)::9::2, CR)
SET_REAL_REG(597, runPeriod, Status)
Display More