Hi,
OK I've got a solution that has improved the function a lot.
function OfflineButton()
local inst=mc.mcGetInstance()
local hsigenable=mc.mcSignalGetHandle(inst,mc.OSIG_ENABLE0)
local hsigbuttonstate=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT2)
if (mc.mcSignalGetState(hsigenable)==1) then
if (mc.mcSignalGetState(hsigbuttonstate)==1) then
-- Mach is enabled and the servos are 'online'
-- Need to save current position and set servos 'offline'
local state=mc.mcCntlGetState(inst)
if (state ~=0)then
mc.mcCntlCycleStop(inst)
end
local Xpos=mc.mcAxisGetMachinePos(inst,mc.X_AXIS)
local Zpos=mc.mcAxisGetMachinePos(inst,mc.Z_AXIS)
mc.mcCntlSetPoundVar(inst,500,Xpos)
mc.mcCntlSetPoundVar(inst,501,Zpos)
mc.mcSignalSetState(hsigbuttonstate,0)
else
--Mach enabled but servos 'offline'
--Need to restore current position and reset servos 'online'
local state=mc.mcCntlGetState(inst)
if (state ~=0)then
mc.mcCntlCycleStop(inst)
end
local Xpos=mc.mcCntlGetPoundVar(inst,500)
local Zpos=mc.mcCntlGetPoundVar(inst,501)
local rc=mc.mcCntlGcodeExecuteWait(inst,'g53 g0 x'..Xpos..' z'..Zpos)
if (rc ~=0) then
mc.mcCntlEnable(inst,0)
mc.mcCntlEnable(inst,1)
rc=mc.mcCntlGcodeExecuteWait(inst,'g53 g0 x'..Xpos..' z'..Zpos)
end
if (rc==0) then mc.mcSignalSetState(hsigbuttonstate,1) end
end
else
--Mach disabled....do nothing
end
end
if ( mc.mcInEditor()==1) then
OfflineButton()
end
Note Dany this is the complete fuction code intended to be put in a folder that you may use your new found editor/debug skills!
Once youv've got it running to your satisfaction then strip off the last mc.mcInEditor test lines at the end ans put the remainder in the screen load script.
Note also that I think you have attached the function as 'clicked button' script which is not correct. Attach the function as 'Left up' script.
The changes over my original code all relate to the function performing when using Gcode files. You will note that this group of lines occur in two different
places. It purpose is to test if the controller is at idle before you attempt any thing else. If you do attempt to do something with the control state other than
idle will crash Mach.
local state=mc.mcCntlGetState(inst)
if (state ~=0)then -- state==0 measns control state=idle
mc.mcCntlCycleStop(inst) -- A CycleStop will ensure the control state is idle
end
The second change relates to the difficulty I note and posted in my previous post in this thread. The issue is that if you have a Gcode job loaded and running, even with
the job stopped or EVEN closed, the Gcode interpreter is unavailable to accept a fresh MDI command embedded in the GcodeExecuteWait() API call. The only way I could
find which would ensure that the Gcode interpreter was ready to accept a fresh MDI command was to disable and then enable Mach. This cycle in addition to freeing
the interpreter of code allows the Gcode job as loaded is preserved at its correct line number. I am a little unsure how it handles modals yet but it seems to work on
my laptop.
local rc=mc.mcCntlGcodeExecuteWait(inst,'g53 g0 x'..Xpos..' z'..Zpos)
if (rc ~=0) then
mc.mcCntlEnable(inst,0)
mc.mcCntlEnable(inst,1)
rc=mc.mcCntlGcodeExecuteWait(inst,'g53 g0 x'..Xpos..' z'..Zpos)
end
if (rc==0) then mc.mcSignalSetState(hsigbuttonstate,1) end
The code works this way: the GcodeExecuteWait() API is attempted. If the return code is zero then the call succeeded. If the call failed the return code will be other than
zero. The two CntlEnable() calls first disable Mach and then enable Mach again. The GcodeExexcuteWait() call should now succeed. If it does as evidenced by a return
code of zero then OSIG_OUTPUT2 can be set to reinstate the servos.
Still not quite sure why with the control state at idle the GcodeExecuteWait() API wont progress...that's subject to further investigation and another thread. In the mean
time this should go close to what Dany has wanted from the start.
Craig