Hi,
because the PLC script runs every 50ms (default) the code needs to be pretty quick and slick otherwise
it will block the rest of Mach.
For this reason the code I posted is pretty lean but at the same time detailed enough to make it readable.
Note these lines:
local workZMaxHandle=mc.mcRegGetHandle(inst,'core/inst/PathZmax')
local workZMax=mc.mcRegGetValue(workZMaxHandle)-zOffset
local workZMaxHandle=mc.mcRegGetHandle(inst,'iRegs0/workZMax')
Note that I have redefined the variable workZMaxHandle, first I used it to address the core register PathZmax
and then redefined it to address the instance register iRegs0/workZMax. I did this so the one variable is reused.
Note that I did it a second time with workZMinHandle. On the one hand it reduces the number of variables
that Lua has to keep a track of but on the other hand can be confusing for someone else to read.
If I were writing this code for myself I would streamline it even further, albeit at the expense of readability.
local zOffset=mc.mcCntlGetOffset(inst,mc.Z_AXIS,mc.MC_OFFSET_FIXTURE)
local regHandle=mc.mcRegGetHandle(inst,'core/inst/PathZmax')
local MaxMin=mc.mcRegGetValue(regHandle)-zOffset
regHandle=mc.mcRegGetHandle(inst,'iRegs0/workZMax')
mc.mcRegSetValue(regHandle,MaxMin)
regHandle=mc.mcRegGetHandle(inst,'core/inst/PathZmin')
MaxMin=mc.mcRegGetValue(regHandle)-zOffset
regHandle=mc.mcRegGetHandle(inst,'iRegs0/workZMin')
mc.mcRegSetValue(regHandle,MaxMin)
Here I reduced the number of variables to three, zOffset, regHandle, and MaxMin, with the last two being recycled at need.
Not as readable but will run a little faster.
Craig