Measuring accurate cycle times inside Fairino LUA scripts is trickier than expected. The standard LUA timing functions don't behave as you'd think when robot motion is involved.

The problem with os.clock()

os.clock() returns CPU time consumed by the LUA interpreter, not wall-clock elapsed time. When the robot executes a MoveL or MoveJ, the actual motion happens in the real-time controller — the LUA script just waits. So os.clock() returns very small numbers that don't include motion time.

Workaround 1: Empirical ratio

Some users determine an empirical conversion factor between os.clock() ticks and real seconds. This works for approximate timing — e.g. controlling how long a vacuum pump runs after a pick operation:

-- NOT accurate for motion timing, but works for delays
local start = os.clock()
-- ... do some non-motion operations ...
local elapsed = os.clock() - start

The ratio varies between firmware versions and controller hardware. You'll need to calibrate it yourself.

Workaround 2: Use RegisterVar() for debugging

The RegisterVar() function lets you watch variable values in real-time from the WebApp. While it doesn't directly give you timestamps, you can monitor your loop counter and manually time it:

local cycle_count = 0
RegisterVar("cycle_count", cycle_count)

while true do
  -- ... your program logic ...
  cycle_count = cycle_count + 1
  RegisterVar("cycle_count", cycle_count)
end

Workaround 3: Measure through PLC (most reliable)

The most accurate method — use your PLC to measure cycle time externally:

  1. Set a digital output HIGH at the start of the cycle
  2. Set it LOW at the end of the cycle
  3. Measure the pulse duration on the PLC side

This gives you real wall-clock timing unaffected by LUA interpreter limitations.

Missing function: GetSpeed()

Related note: there is currently no GetSpeed() function to read the current global speed percentage. You can set the speed with SetSpeed(vel) but cannot read it back programmatically. This is a known gap in the LUA API.

Missing function: GetRobotState()

Similarly, detecting whether the robot is currently running, paused, or stopped from within LUA is not straightforward. The Python SDK has better support for state queries — if you need reliable state detection, consider using an external SDK-based monitoring script.