Hello Guest it is March 29, 2024, 10:17:40 AM

Author Topic: Need help with M6 Macro, translating from Mach3 to Mach4  (Read 6908 times)

0 Members and 1 Guest are viewing this topic.

Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #20 on: October 06, 2018, 11:27:45 PM »
Hi,
I've just realized that I have not allowed anyway to turn the TurretLock signal off.

If you are OK with the idea that if the turret is in position as indicated with the TurretPosition switch then the
TurretLock signal could be turned off immediately. That would require only two more lines of code, one to set the
register to zero and the other to your the output off.

Code: [Select]
function m180()
-- Lock the turret.
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT3)
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretLock')
if (mc.mcRegGetValue(hreg)==0) then
mc.mcRegSetValue(hreg,1)
mc.mcSignalGetState(hsig,1)
end
local rc=mc.mcSignalWait(inst,mc.ISIG_INPUT4,1,11)
if (rc==mc.MERROR_NOERROR) then
elseif (rc==mc.MERROR_TIMED_OUT) then
wx.wxMessageBox('Turret failed to lock')
return
end
mc.mcCntlSetLastError(inst,'Turret Locked')
mc.mcRegSetValue(hreg,0)
mc.mcSignalSetState(hsig,0)

end
if (mc.mcInEditor()==1) then
m180()
end

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #21 on: October 07, 2018, 12:48:22 AM »
Hi,
now we need a function to advance the turret by one. We need another register....open the Regfile plugin and add another register
iRegs0/TurretPosition. It will record the current position of the turret, from 0 through 5.

Lets assume for the moment that we know, or rather Mach knows that the turret is locked into a position. This code might work:

Code: [Select]
function m185()
--Advance the Turret
local inst=mc.mcGetInstance()
local hsig1=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT3)
local hsig2=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT4)
local hsig3=mc.mcSignalGetHandle(inst,mc.ISIG_INPUT4)
local hreg1=mc.mcRegGetHandle(inst,'iRegs0/TurretLock')
local hreg2=mc.mcRegGetHandle(inst,'iRegs0/TurretAdvance')
local hreg3=mc.mcRegGetHandle(inst,'iRegs0/TurretPosition')
if (mc.mcSignalGetState(hsig1)==0) then
mc.mcSignalSetState(hsig1,1)
mc.mcRegSetValue(hreg1,1)
end
mc.mcSignalSetState(hsig2,1)
mc.mcRegSetValue(hreg2,1)
local rc=mc.mcSignalWait(inst,mc.ISIG_INPUT4,1,10)
if (rc==mc.MERROR_TIMED_OUT) then
wx.wxMessageBox('Turret failed to Advance')
return
end
mc.mcSignalSetState(hsig2,0)
mc.mcRegSetValue(hreg2,0)
--************************************************
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT3)
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretLock')
if (mc.mcRegGetValue(hreg)==0) then
mc.mcRegSetValue(hreg,1)
mc.mcSignalGetState(hsig,1)
end
local rc=mc.mcSignalWait(inst,mc.ISIG_INPUT4,1,11)
if (rc==mc.MERROR_NOERROR) then
elseif (rc==mc.MERROR_TIMED_OUT) then
wx.wxMessageBox('Turret failed to lock')
return
end
mc.mcCntlSetLastError(inst,'Turret Locked')
mc.mcRegSetValue(hreg,0)
mc.mcSignalSetState(hsig,0)
--*************************************************
local hreg4=mc.mcRegGetHandle(inst,'iRegs0/TurretPosition')
local currentTool=mc.mcRegGetValue(hreg4)
local currentTool=currentTool + 1
if (currentTool==6) then currentTool=0 end
mc.mcRegSetValue(hreg4,currentTool)
end
if (mc.mcInEditor()==1) then
m185()
end

The first part just sets up the signals required and turns the TurretLock and TurretAdvance outputs on. Once the ToolPosition
switch makes the TurretAdvance output turns off. The code between the two lines of asterisk is just our m180, or turret lock function.
The last bit of code increases the count of the Turret Position register.

We are getting close now.

Note all this code is pretty ragged and not well tested yet. I'm also beginning to wonder if I've used registers when I haven't needed to. In particular
the four ToolIndex,  ToolPosition, TurretLock, and TurretAdvance are looking redundant. They wont do any harm so I'll leave them be but we may at
a later time decide that they are not required.

The second issue is that this last macro includes the code of another one, can I use a subroutine or is pasting the code in complete better?
In this case I suspect its better to paste the code in directly. It admittedly means that there is some repeated code, but memory is cheap. It may make
the code a little easier to read with subroutines but I think it will run best as repeated code.

In this instance I will go with the repeated code strategy and later try reducing it by subroutines and compare the two.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #22 on: October 07, 2018, 01:17:44 AM »
hi,
lets review what we have got, what we need to do yet and some ultimate goal.

We have a macro (m180) that can lock the turret. It still needs to be tested on your hardware.
We have a macro (m185) that advances the turret one position and then locks it place and advances a counter to indicate the new position. Again this needs
to be tested on your hardware.

What remains to be done is have a procedure made up of these two operations that can advance until the Index switch is detected. Mach can then reset its
tool position counter and be assured for the rest of the session it will stay in sync with the tool.

Another useful procedure would be to have a function that advanced two positions, or three positions or more. Thus if the current tool is no.2 and
tool no. 4 is required use the 'advance by two' function.

Why might you ask that I am adding all these functions? Am I not complicating matters.....and the answer is yes...to a certain extent.

My ultimate strategy is to have a table of functions called Turret[].

It will have a function called Lock.
It will also have a function called Advance.
It will have a function called Index.
It will have a function Advance2.
It will have a function Advance3.

and so on.... You can see where this is going. My hope is that Index your machine at startup you will call Turret.Index     That's it! You need do no more.
To go from your current position to two positions further you call Turret.Advance2   That's it! You need do no more.

Code like that is best as a module....but we will come to that later.

First trick is to make some code that works...there is still a little to go yet.  The next thing is to refine the code so that its efficient and robust. THEN if all
is well stick it all in a multifunction table.

Provided we can get code to work you could bail before doing all the table/module stuff, I won't drag you down that rathole unless you want to! ;D

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #23 on: October 07, 2018, 04:01:46 AM »
Hi,
I have had some more thoughts about using the registers vs the ISIG_INPUTnnn and OSIG_OUTPUTnnn.

Part of me is convinced I need to use registers so that the data can be used by both the GUI AND Machs core Gcode processor.
The problem as evidenced by the code I have written is that I seem to be duplicating everything.

I realize now what I can do.

We have two input signals from the index and position switches on your turret. They have to be treated as input signals, ie ISIG_INPUT....
That's how we have treated them and recalling the code which we placed in the SigLib table:

mc.ISIG_INPUT3]=function(state)
   local hreg=mc.mcRegGetHandle(inst,'iRegs0/ToolIndex')
   if state==1 then
      mc.mcRegSetValue(hreg,1)
   else
      mc.mcRegSetValue(hreg,0)
   end
end,
[mc.ISIG_INPUT4]=function(state)
   local hreg=mc.mcRegGetHandle(inst,'iRegs0/ToolPosition')
   if state==1 then
      mc.mcRegSetValue(hreg,1)
   else
      mc.mcRegSetValue(hreg,0)
   end
end,
the registers ToolIndex and ToolPosition will always be in lockstep with ISIG_INPUT3 and ISIG_INPUT4. Thus I don't need to use Signal APIs to read the
inputs, just use the Reg APIs instead.

The question is how do we deal with the outputs? The outputs must use the OSIG APIs to cause the output pins of your controller to operate.
However we wish to use the registers TurretLock and TurretAdvance internally. This can only happen if you duplicate the API calls, hardly
code efficient and worse it will fail when Machs Gcode processor is running and starves the GUI of service then the OSOG_OUTPUT APIs wont work.

The answer is that OSIG_OUTPUT3 must be in lockstep with TurretLock and OSIG_OUTPUT4 with TurretAdvance.
This can be achieved by a few lines of code in the PLC script.

This is the code I will put in the PLC script:
Code: [Select]
local hreg=mc.mcRegGetHandle(inst,'iReg0/TurretLock')
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT3)
mc.mcSignalSetState(hsig)=mc.mcRegGetValue(hreg)
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretAdvance')
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT4)

This will allow some simplification of the two macros we have written thus far:
Code: [Select]
function m180()
-- Lock the turret.
local inst=mc.mcGetInstance()
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretLock')
if (mc.mcRegGetValue(hreg)==0) then
mc.mcRegSetValue(hreg,1)
end
local rc=mc.mcSignalWait(inst,mc.ISIG_INPUT4,1,11)
if (rc==mc.MERROR_NOERROR) then
elseif (rc==mc.MERROR_TIMED_OUT) then
wx.wxMessageBox('Turret failed to lock')
return
end
mc.mcCntlSetLastError(inst,'Turret Locked')
mc.mcRegSetValue(hreg,0)
end
if (mc.mcInEditor()==1) then
m180()
end
And m185:
Code: [Select]
function m185()
--Advance the Turret
local inst=mc.mcGetInstance()
local hreg1=mc.mcRegGetHandle(inst,'iRegs0/TurretLock')
local hreg2=mc.mcRegGetHandle(inst,'iRegs0/TurretAdvance')
local hreg3=mc.mcRegGetHandle(inst,'iRegs0/TurretPosition')
if (mc.mcRegGetValue(hreg1)==0) then
mc.mcRegSetValue(hreg1,1)
end
mc.mcRegSetValue(hreg2,1)
local rc=mc.mcSignalWait(inst,mc.ISIG_INPUT4,1,10)
if (rc==mc.MERROR_TIMED_OUT) then
wx.wxMessageBox('Turret failed to Advance')
return
end
mc.mcRegSetValue(hreg2,0)
--********************************************************************
local inst=mc.mcGetInstance()
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretLock')
if (mc.mcRegGetValue(hreg)==0) then
mc.mcRegSetValue(hreg,1)
end
local rc=mc.mcSignalWait(inst,mc.ISIG_INPUT4,1,11)
if (rc==mc.MERROR_NOERROR) then
elseif (rc==mc.MERROR_TIMED_OUT) then
wx.wxMessageBox('Turret failed to lock')
return
end
mc.mcCntlSetLastError(inst,'Turret Locked')
mc.mcRegSetValue(hreg,0)
--************************************************************************
local hreg4=mc.mcRegGetHandle(inst,'iRegs0/TurretPosition')
local currentTool=mc.mcRegGetValue(hreg4)
local currentTool=currentTool + 1
if (currentTool==6) then currentTool=0 end
mc.mcRegSetValue(hreg4,currentTool)
end
if (mc.mcInEditor()==1) then
m185()
end

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #24 on: October 07, 2018, 04:13:03 AM »
Hi,
gross error in the PLC script, try this instead:

Code: [Select]
local hreg=mc.mcRegGetHandle(inst,'iReg0/TurretLock')
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT3)
mc.mcSignalSetState(hsig,(mc.mcRegGetValue(hreg)))
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretAdvance')
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT4)

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #25 on: October 07, 2018, 04:40:38 AM »
Hi.
yet another error:

Code: [Select]
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretLock')
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT3)
mc.mcSignalSetState(hsig,(mc.mcRegGetValue(hreg)))
local hreg=mc.mcRegGetHandle(inst,'iRegs0/TurretAdvance')
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT4)
mc.mcSignalSetState(hsig,(mc.mcRegGetValue(hreg)))

It getting late in the day and I'm making all sorts of mistakes....my apologies to those of you trying to follow what I'm doing.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #26 on: October 07, 2018, 11:34:59 PM »
I just found all of your replies right now, and wanted to say thank you!

It's late for me right now but I'll be going over all of this in the next day or two and report back!
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #27 on: October 08, 2018, 01:52:03 AM »
Hi,
we really need to rest it on your hardware. Depending on just exactly how and when the tool-in-position switch operates could
have a major bearing on if it works.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #28 on: February 21, 2022, 03:12:18 PM »
Hi Craig,

Apologies for necro-posting, but I've also been struggling with the m6 macro on a similar machine and have followed along with this topic, was hoping for some assistance from here as I am unsure how to go about testing the functions we have here. Thanks!

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #29 on: February 23, 2022, 12:20:59 PM »
Havoc and Chriscam,

This might help some.
When I was trying to learn Lua I made this for people like me to get started.

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