Hello Guest it is March 28, 2024, 09:28:08 AM

Author Topic: Altering M6 but am confused  (Read 9266 times)

0 Members and 2 Guests are viewing this topic.

Altering M6 but am confused
« on: September 12, 2016, 05:33:08 PM »
I am attempting to create my own M6 code based on the videos provided but have run into some issues.  First off the reason I am altering the existing code is I don't want to perform 2 probes since I always reference the vacuum table as Z 0.0.   Because if this I only want my machine to go to a predefined spot, prompt to change bits, and Zero to the touch plate which is currently embedded flush with the work surface.  My main confusion is the commands for mcaxisgetpos and mcaxissetpos and how they relate to what I am doing.  I feel I have a good handle on it most of it, but after 8 hours of reading forums I figured it was time to eat my pride and ask for help.

Here is what I have so far, any help or corrections would be appreciated.

function M6()
    local inst = mc.mcGetInstance();
    local selectedtool = mc.mcToolGetSelected(inst)
    local currenttool = mc.mcToolGetCurrent(inst)
    local TouchPlateThickness = 0.0;                     --What is the offset for the touch plate to the table top.  I dont have this figured in yet.

    if selectedtool == currenttool then
    return
    mc.mcCntlSetLastError(inst, "ToolChange Activated But Not Required")
    else
    mc.mcCntlGcodeExecute (inst,"M05") --make sure spindle is not running
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0") --move z axis all the way up
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 X0.464 Y-99.808") -- Move XY to above fixed touchplate location
    local changetoo = mc.mcToolGetDesc(inst,selectedtool)
    wx.wxMessageBox("Please change to tool number "..selectedtool.." "..changetoo.." and press ok to continue")
    currenttool = selectedtool --not sure if this goes in this location or is required elsewhere????
    toollen = mc.mcToolGetData(inst, mc.MTOOL_MILL_HEIGHT)
    if toollen == 0 then
    return
    wx.wxMessageBox("No tool length currently defined")
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0 \n G91 G31 Z-6 F25")
    mc.mcAxisSetPos(inst, 2, 0.0) ???
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
    --wx.wxMessageBox("Please turn on spindle and click ok to continue") --can be removed if required
    mc.mcToolSetCurrent(inst, selectedtool)
    mc.mcCntlSetLastError(inst, "ToolChange Finished")
    end
    else
    local probestart = -6 + toollen
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z"..probestart.."\n G91 G31 Z-1 F25")
    mc.mcAxisSetPos(inst, 2, 0.0) ???
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
    mc.mcToolSetCurrent(inst, selectedtool)
    mc.mcCntlSetLastError(inst, "ToolChange Finished")
    end
end


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

Re: Altering M6 but am confused
« Reply #1 on: September 13, 2016, 02:17:15 AM »
This code doesn't look like it should compile.  There are mis-matched if/end blocks and you have code after "return" lines that will never be executed.  I don't know if something got changed when you pasted the code here.

For example, you have (I've changed the indentation to better show the content of the "if" clause):

Code: [Select]
function M6()
    local inst = mc.mcGetInstance();
    local selectedtool = mc.mcToolGetSelected(inst)
    local currenttool = mc.mcToolGetCurrent(inst)
    local TouchPlateThickness = 0.0;                     --What is the offset for the touch plate to the table top.  I dont have this figured in yet.

    if selectedtool == currenttool then
        return
        mc.mcCntlSetLastError(inst, "ToolChange Activated But Not Required")
    else
      .... more code here
The mc.mcCntlSetLastError() will never execute because it is after the "return".  Any lines of code between a "return" and an "end" (end of code block) will not be executed.  So move the mc.mcCntLSetLastError() line between the "if" and the "return" so that it looks like this:
Code: [Select]
    if selectedtool == currenttool then
        mc.mcCntlSetLastError(inst, "ToolChange Activated But Not Required")
        return
    else
      .... more code here
There are other instances of this further down in the code, including one section of code that performs a probe inside the "if toollen == 0" block.  After moving that mc.mcCntlSetLastError() to before the "return", that prove code should probably be deleted (all the code after the "return" and before the "else").

If the indentation of your original source file is the same as what showed up in your forum post, I suggest changing the indentation so the contents of every "if/end" or "if/else/end" block is immediately visible.  My quick guess is that you need one more "end" just before the "if mc.mcInEditor" line.

Now to your questions:

(1) The line "currenttool = selectedtool" is not needed.  There is nothing below that line that uses "currenttool", it all uses "selectedtool".

(2) I don't see any calls to mc.mcAxisGetPos().

(3) The calls to mc.mcAxisSetPos() tell Mach4 that the where ever Z axis currently is should be called "0.0".  It does this by changing the fixture offset for the currently selected fixture.  And the Z axis *should* be at the point where the new tool touched the touch plate, presuming that the G31 probe command moved far enough for that to happen.

Bob

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Altering M6 but am confused
« Reply #2 on: September 13, 2016, 02:41:11 AM »
Quote
The mc.mcCntlSetLastError() will never execute because it is after the "return".  Any lines of code between a "return" and an "end" (end of code block) will not be executed.  So move the mc.mcCntLSetLastError() line between the "if" and the "return" so that it looks like this:

Never noticed that cheers Bob, although looking at it further because its an "if" or "else" statement then the return is not actually required.


rae_gordon - I understand what you are trying to do using a fixed reference point for zero and will dig out the code for you to play with that I  did for a friend who does just the same thing and sets his wcs to his bed.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Altering M6 but am confused
« Reply #3 on: September 13, 2016, 10:21:15 AM »
I haven't tried to compile the project yet, I was just attempting to write and understand what I was doing but was at a standstill due to a lack of what the commands were actually performing. I actually just added the second if statement, and thought that Return was part of the format based upon Daz's M6 sample Code.  Bob, your description of  mc.mcAxisSetPos() clears alot of confusion for me.  All the references I found of the commands tell me what the format should be, but I haven't found a references that give descriptions of what the actually do.  I need to get a Lua guide for reference.

The reason for my nested IF statement and what looks like a duplicate code is I frequently use long bits, 6" or greater for foam cutting.  In the event that I don't have a bit defined, it is likely to be a shorter standard length router bit but didn't want to risk slamming a longer bit into the copper plate that I have surfaced into the table.  Of course I would never put the wrong bit in the router (again).  Because of extreme variety of length I am hesitant to use a guess length as Daz uses in his M6 code. Pondering more on this, I see additional merits for a spring loaded touch plate so that I could rapid , touch, backup, then zero, and not have to wait the eternity it would take to hit the touch plate by stating at the highest height.  Maybe have an Estop if the plate bottoms out.


Daz, I am going to wait for your sample code before I proceed further with what I have in the event that I am on the wrong path.  while I wait for that I am going to re-explore touch plates.

Thanks

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Altering M6 but am confused
« Reply #4 on: September 13, 2016, 11:49:50 AM »
One thing to remember when your writting a macro is the ability you have to step through the code live (debug) and see whats going on, start with a small piece of code IE moving to a safe Z then moving to the position over your touchplate, once you have that then you can move on to creating your toolchange.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Altering M6 but am confused
« Reply #5 on: September 13, 2016, 01:07:47 PM »
Thanks Daz,
  I actually had your original M6 profile working for a while but didn't have the touch plate fixed  nor the time to work the bugs, mainly I would forget that it ran 2 touch cycles so user error would kick in and I would change the bit on the first touch cycle. so I shelved it while I completed a job run.  I pondered the idea of altering your code so that it would go ahead and do the first probe without user intervention, but I don't like the idea of the chance that the router may be running when it does the probe.  I had problems where I was fusing my solid state Relays and the router would not turn off.  I believe I solved that problem by utilizing a contactor relay, but time will tell and you never know when goblins may visit my Gcode.  In the mean time I ended up putting collars on my router bits and using that as a stop for setting the bit when I reinserted it, unfortunately the collars are not balanced and with the longer bits it was notable.  Because of this I would have to put the collar on before I removed a bit from the machine, and removed the collar after reinserting.  That worked as a poor mans bit changer, but user error usually showed up,such as forgetting to put the collar on.  


Re: Altering M6 but am confused
« Reply #6 on: September 13, 2016, 01:10:25 PM »
If for some reason you dont have the sample code available I will continue to edit what I have.  I don't think I am far away from my goal, but there is always a better way.
Re: Altering M6 but am confused
« Reply #7 on: September 14, 2016, 06:11:25 PM »
This is my new code am I am going to try it out on my machine tomorrow.  I have a looping section in there that seems to work, does anyone see any problems with running something like that.  Also there is a section below that I need help with where I want to be able to manually jog the Z axis while in the M6 macro.  Is there a way to do this then continue the macro.



function M6()
    local inst = mc.mcGetInstance();
    local selectedtool = mc.mcToolGetSelected(inst)
    local currenttool = mc.mcToolGetCurrent(inst)
    local hsig = mc.mcSignalGetHandle(inst, mc.ISIG_PROBE)

    if selectedtool == currenttool then
        mc.mcCntlSetLastError(inst, "ToolChange Activated But Not Required")
        do return end
    else
        mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0") --move z axis all the way up
        mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 X0.464 Y-99.808") -- Move XY to above fixed touchplate location
        local changetoo = mc.mcToolGetDesc(inst,selectedtool)

        wx.wxMessageBox("Please change to tool "..selectedtool.." which is a "..changetoo.."")
        toollen = mc.mcToolGetData(inst, mc.MTOOL_MILL_HEIGHT, selectedtool)
        if toollen ~= 0 then
            local probestart = -6 + toollen
            mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z"..probestart.."\n G91 G31 Z-.5 F25")
            ProbeState = mc.mcSignalGetState(hsig)
                repeat
                   mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
                   wx.wxMessageBox("ERROR: Touch Probe was not detected reset tool")
                   mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z"..probestart.."\n G91 G31 Z-.5 F25")
                until(ProbeState ~= 0)
            mc.mcAxisSetPos(inst, 2)
            mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
            mc.mcToolSetCurrent(inst, selectedtool)
            mc.mcCntlSetLastError(inst, "ToolChange Finished")
        else
            wx.wxMessageBox("No tool length currently defined Lower Z to above touch plate, hit enter to start probe")
 --need a way to lower Z manually here--???
           mc.mcCntlGcodeExecuteWait(inst," G91 G31 Z-.5 F25")
            ProbeState = mc.mcSignalGetState(hsig)
                repeat
                   mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
                   wx.wxMessageBox("ERROR: Touch Probe was not detected reset tool")
                   mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z"..probestart.."\n G91 G31 Z-.5 F25")
                until(ProbeState ~= 0)
            mc.mcAxisSetPos(inst, 2)
            mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
            --wx.wxMessageBox("Please turn on spindle and click ok to continue") --can be removed if required
            mc.mcToolSetCurrent(inst, selectedtool)
            mc.mcCntlSetLastError(inst, "ToolChange Finished")end
    end
    mc.mcToolSetCurrent(inst, selectedtool)
end

if (mc.mcInEditor() == 1) then
    M6()

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Altering M6 but am confused
« Reply #8 on: September 15, 2016, 04:54:31 AM »
I would hold off from running this code for now as at a quick glance there are quite a few problems that will arise but should have time today or tomorrow to go through it live on machine.

1.  the "do return end" is not needed (was a mistake on my part originally)
2.  the "repeat - until" could give you a constant loop, if it doesnt hit on first run then it will go straight back to 0 and do the same again.
3.  mc.mcAxisSetPos(inst, 2) will not set anything as correct format is mc.mcAxisSetPos(inst, AxisNumber, AxisPos) so you are missing what you are setting too.

DazTheGas
New For 2022 - Instagram: dazthegas

Offline Stuart

*
  •  311 311
    • View Profile
Re: Altering M6 but am confused
« Reply #9 on: September 15, 2016, 05:52:28 AM »
It's that cheeky so and so again  :)

If the OP and DTG would be kind enough to post the code when it is proven I and I suspect others would be extremely grateful for you endeavours

Stuart