Hello Guest it is April 23, 2024, 06:23:54 PM

Author Topic: edit Cyclestop in screen  (Read 4949 times)

0 Members and 1 Guest are viewing this topic.

Re: edit Cyclestop in screen
« Reply #20 on: April 24, 2019, 04:35:34 AM »
I'd say you need run the CycleStop() function as a coroutine using the wait coroutine in the PLC, yield the coroutine after mc.mcCycleStop(inst) so that it waits for mach state to be idle, and then use the GCodeExecute API when it resumes.
Re: edit Cyclestop in screen
« Reply #21 on: April 24, 2019, 06:58:34 AM »
That kind off makes sense   do you have a example of how I could wait till the program comes to idle I sort of think the program is busy after the cyclestop  it runs the mc.mcCntlGcodeExecuteWait(inst, "G90 G53 Z0") line and it is then lost  and does not execute   but I do not know haw to poll for idle state and wait till it is achieved
But it seems like some thing like this is happening

 The Retract button code is also a mystery to me
-- button left down event script raises the signal
local inst = mc.mcGetInstance();
local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_RTRCT);
if (rc == mc.MERROR_NOERROR) then
    mc.mcSignalSetState(hSig, 1);
end

and yet it turns of the spindle raises the spindle and returns to machine home  this is almost what I want but I want to turn off the coolant as well at the same time   I see no code here that performs what Retract  actually does just a signal state changed
Regards Ray
Re: edit Cyclestop in screen
« Reply #22 on: April 24, 2019, 05:22:35 PM »
Hi,
when using mc.mcCntlGcodeExecute***() type statements you have seen that the machine needs to be in idle state
before it can accept such a command.

As you know the GUI and Machs Gcode interpreter are in different chunks. Thus a command such as mcCntlGcodeExecute()
would fail if the GUI chunk is currently running. If however the GUI chunk never runs then the screen is frozen, live
updates to DRO's the tool path display or anything else is possible. In the normal operation of Mach it is necessary for both
chunks to run and yet they cannot run simultaneously.

At other times Machs motion planner is under control of another instruction set, an MDI command string for example.
Thus any instruction like mcCntlGcodeExecute() would have to wait until that string were concluded before the motion planner
can be engaged.

It might in some circumstances be possible to add a wait period within your code so that a particular command could
be progressed where it had been unable to be progressed a few milliseconds before.

When I suggested testing the return codes it was more about alerting you to the clash rather than solving it.
If you are aware that a clash is occurring then you have the opportunity to study the circumstances where it occurs
and a subtle change of approach might avoid the clash altogether.

There is a list of all machine states in:

https://www.machsupport.com/forum/index.php?topic=40051.0

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: edit Cyclestop in screen
« Reply #23 on: April 25, 2019, 03:22:50 AM »
In your PLC script you should have the following code already if you're using the standard Mach4 screens. This bit of code monitors the state of mach, and when it is idle it will resume the coroutine from where it yielded.
Code: [Select]
-------------------------------------------------------
--  Coroutine resume
-------------------------------------------------------
if (wait ~= nil) and (machState == 0) then --wait exist and state == idle
local state = coroutine.status(wait)
    if state == "suspended" then --wait is suspended
        coroutine.resume(wait)
    end
end

The CycleStop() function will go in the screen load script. This will also turn off the COOLANT ON output, but you can change this to whatever output you have mapped for your coolant output.
Code: [Select]
function CycleStop()
local inst = mc.mcGetInstance()
local rc, hSig
mc.mcCntlCycleStop(inst)
mc.mcSpindleSetDirection(inst, 0)
hSig = mc.mcSignalGetHandle(inst, mc.OSIG_COOLANTON)
if (hSig == mc.MERROR_NOERROR) then
mc.mcSignalSetState(hSig, mc.MC_OFF)
end
mc.mcCntlSetLastError(inst, "Cycle Stopped")
coroutine.yield() --This is where the function yields and the code in the PLC comes into play
rc = mc.mcCntlMdiExecute(inst, "G90 G53 Z0 Y0")
if (rc ~= mc.MERROR_NOERROR) then
mc.mcCntlSetLastError(inst, "Error moving to Z0 Y0")
end
end

This will go in the button script for your stop button
Code: [Select]
--Create a coroutine called 'wait'
wait = coroutine.create(CycleStop)
--Start the coroutine
coroutine.resume(wait)

This worked for me in the simulator, but always take caution when trying bits of code off the internet!
« Last Edit: April 25, 2019, 03:24:32 AM by SwiftyJ »
Re: edit Cyclestop in screen
« Reply #24 on: April 25, 2019, 05:40:22 AM »
Hi
I did as you said and I understand what we are trying to do (I think)  this is the error message
and the history
this is line 198    coroutine.yield() --This is where the function yields and the code in the PLC comes into play
This is 694 to 699
function btnStop_Left_Up_Script(...)
    CycleStop()
    --Create a coroutine called 'wait'
    wait = coroutine.create(CycleStop)
    --Start the coroutine
    coroutine.resume(wait)







I am running in simulation mode as I took a copy home there is a large time difference to where I am to where you are  so I work on it at night after work and do testing on the machine in the day
Regards Ray

Lua: Error while running chunk
?:0: attempt to call field 'CycleStop' (a nil value)
stack traceback:
   ?: in main chunk
   ?: in main chunk
Cycle StoppedLua: Error while running chunk
attempt to yield from outside a coroutine
stack traceback:
   [C]: in function 'yield'
   C:\Mach4Hobby\ScreenScript.lua:198: in function 'CycleStop'
   C:\Mach4Hobby\ScreenScript.lua:695: in function <C:\Mach4Hobby\ScreenScript.lua:694>
Cycle StoppedLua: Error while running chunk
attempt to yield from outside a coroutine
stack traceback:
   [C]: in function 'yield'
   C:\Mach4Hobby\ScreenScript.lua:198: in function 'CycleStop'
   C:\Mach4Hobby\ScreenScript.lua:695: in function <C:\Mach4Hobby\ScreenScript.lua:694>



This is what I placed in the stop button script is this correct
CycleStop()
 
--Create a coroutine called 'wait'
wait = coroutine.create(CycleStop)
--Start the coroutine
coroutine.resume(wait)

« Last Edit: April 25, 2019, 05:49:56 AM by Raymondo »
Re: edit Cyclestop in screen
« Reply #25 on: April 25, 2019, 07:20:52 AM »
Remove CycleStop() from your button script so it just says:

Code: [Select]
--Create a coroutine called 'wait'
wait = coroutine.create(CycleStop)
--Start the coroutine
coroutine.resume(wait)
Re: edit Cyclestop in screen
« Reply #26 on: April 25, 2019, 08:03:42 AM »
Thank you is now works in simulation mode 
I will try it on the machine tomorrow
I will let you know how it goes but it is looking good
Thanks
Regards Ray
Re: edit Cyclestop in screen
« Reply #27 on: April 26, 2019, 02:52:49 AM »
I have run the machine and low and behold it now works perfectly I have done a few moree modifications it now returns to home and resets the Gcode I had to run coroutine  a second time to allow the moves to finish 
I use M9 in the command instead of playing with turning off the output directly

I have to say a big THANK YOU  I do not think I could have figured it out by myself  and it for me just makes a already great machine just that bit better to use for what I am using it for
Thanks again I  never recommend Mach4 highly enough it is just fantastic and the help received invaluable
Regards Ray

Offline reuelt

*
  •  520 520
    • View Profile
Re: edit Cyclestop in screen
« Reply #28 on: April 28, 2019, 12:56:34 AM »
Sorry,
I know what you have achieved
BUT perhaps there is a easier way in MACH4 config - hidden somewhere.

In MACH3, all you need is a few clicks in Config | General config to achieve what you want to do.
(see attached)

And MACH4 is supposed to be BETTER!! Or we should tell NFS to make some changes in next version.
"the gift of God is eternal life through Jesus Christ our Lord"
Re: edit Cyclestop in screen
« Reply #29 on: April 28, 2019, 01:57:17 AM »
Hi
I wanted to do a  few more things including turning of the coolant   and from a stop I would never restart from that point but I am sure A lot of people in fact most people it works just fine for them as it is and would be the standard way most controllers in the commercial world work  But and a big BUT
Mach4 is really configurable and I think just fantastic and are sure you can make it do what ever you wish I just wanted some thing more than what most people would want am are still learning the Lua script  though I did write the script for  a 8 tool rotary tool changer and that works great but this I could not at first find where and how to edit what I wanted but Mach4 help is the best bar none
Regards Ray