Hello Guest it is July 12, 2020, 04:38:59 PM

Author Topic: Mach4 Button OFFLINE  (Read 13439 times)

0 Members and 1 Guest are viewing this topic.

Re: Mach4 Button OFFLINE
« Reply #130 on: February 03, 2018, 02:26:57 PM »
Hi,

Quote
through the relay I will use a function, of which the units ignore any signal. (they still remain in position, without moving)
when the function changes state, normal use of the drives is restored

Isn't that what they are supposed to do? The trick is that when you restore the servos (so that they again respond to movement signals) is that while
they were disabled you may have jogged or MDI'ed Mach so Mach believes the servos are in one place when in fact they are where they were left
some time ago. Ergo the 'restore machine coordinate' instructions in the code.

Craig
My wife left with my best friend...
     and I miss him!
Re: Mach4 Button OFFLINE
« Reply #131 on: February 03, 2018, 02:55:44 PM »
it's exactly like you just said.
« Last Edit: February 03, 2018, 02:57:24 PM by daniba73 »
Re: Mach4 Button OFFLINE
« Reply #132 on: February 03, 2018, 06:31:41 PM »
Hi,
have been doing some testing and the function works as anticipated under some circumstances but not others.

If you go 'offline' and jog or MDI when you go 'online' the machine coordinates reset to that they were prior to going offline. As anticipated.

Where it fails is if some Gcode from a Gcode job is executed between going offline and then later going back online the servos reinstate
but the machine coordinates do not (always) update.

Quote
rc = mc.mcCntlGcodeExecute(
      number mInst,
      string commands)

But the notes say:
Quote
Notes:


If the control is in the idle state, this function will execute the G code in MDI mode.


I can report that if the control is not idle when this statement is executed then you will crash.

There needs to be a test in the code that prevents the statement from running if the control is not in idle state. That's easy enough but there is a further complication
that I can't solve yet.

The return codes for this API are:
Quote
Returns: Return Code Description
MERROR_NOERROR No Error.
MERROR_INVALID_INSTANCE The mInst parameter was out of range.
MERROR_NOT_NOW The operation could not be completed at this time.
MERROR_NOT_COMPILED The macro scripts could not be compiled.

If have found that if a Gcode file is loaded but stopped (control idle) the API will not execute and comes back with a return code of -18 which corresponds to
MERROR_NOT_NOW

I don't know why this should be. I suspect that the GcodeExecuteWait() API requires MDI functionality but the loaded Gcode is already hogging the Gcode
interpreter so the MDI can't run. I feel a call being put out to Daz and Smurph about this!

Craig
My wife left with my best friend...
     and I miss him!
Re: Mach4 Button OFFLINE
« Reply #133 on: February 04, 2018, 02:58:52 AM »
Hi,
OK I've got a solution that has improved the function a lot.

Code: [Select]
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
My wife left with my best friend...
     and I miss him!
Re: Mach4 Button OFFLINE
« Reply #134 on: February 04, 2018, 05:59:39 PM »
Hi,
Thanks so much!!!!
I ask for patience, for a short time I have no way of doing tests.
Re: Mach4 Button OFFLINE
« Reply #135 on: February 04, 2018, 06:20:12 PM »
Hi Dany,
I was doing some testing this morning, most of it going really well but on occassion it would fail, I'd have
to hit the button a second time to get the servos to re-engage. Haven't sorted out why yet but am thinking about
it. I may have a workaround solution for it yet. Hoping to get some more insight into the workings of Machs
Idle state.

Craig
My wife left with my best friend...
     and I miss him!
Re: Mach4 Button OFFLINE
« Reply #136 on: February 06, 2018, 03:57:32 AM »
Hi Dany,
have tightened up on the code. Have included a loop that retries the GcodeExecuteWait() call to a maximum of five times. My testing has shown that its now
reasonably robust. Its time for you to put it into action and see how it performs in the real world.

Code: [Select]
function OfflineButton()
local inst=mc.mcGetInstance()
local hsigenable=mc.mcSignalGetHandle(inst,mc.OSIG_ENABLE0)
local hsigbuttonstate=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT2)
if (mc.mcCntlGetState(inst) ~=0)then mc.mcCntlCycleStop(inst)end
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 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 Xpos=mc.mcCntlGetPoundVar(inst,500)
        local Zpos=mc.mcCntlGetPoundVar(inst,501)
        local rc=1
        local iter=0
        repeat
            rc=mc.mcCntlGcodeExecuteWait(inst,'g53 g0 x'..Xpos..' z'..Zpos)
            iter=iter+1
            if iter >=5 then wx.wxMessageBox('GcodeExecuteWait() Failed') break end
            if rc~=0 then mc.mcCntlEnable(inst,0) mc.mcCntlEnable(inst,1) end
        until (rc==0 ) 
        if (rc==0) then mc.mcSignalSetState(hsigbuttonstate,1) end
    end
else
    --Mach disabled....do nothing
end
end
if ( mc.mcInEditor()==1) then
    OfflineButton()
end

Craig
My wife left with my best friend...
     and I miss him!
Re: Mach4 Button OFFLINE
« Reply #137 on: February 13, 2018, 04:53:25 AM »
Hello
sorry for late.
I tried..
but it does not work ...
Re: Mach4 Button OFFLINE
« Reply #138 on: February 13, 2018, 11:40:19 AM »
Hi,
have you single stepped through it with the debugger?

Did you attach it to the left-up script or the clicked script?

Craig
My wife left with my best friend...
     and I miss him!
Re: Mach4 Button OFFLINE
« Reply #139 on: February 14, 2018, 04:51:00 AM »
Hi,
I attach the screen image