Hello Guest it is April 25, 2024, 01:35:51 PM

Author Topic: M6 toolchange Lua script help?  (Read 536 times)

0 Members and 1 Guest are viewing this topic.

M6 toolchange Lua script help?
« on: September 06, 2022, 11:27:48 AM »
Hi all! I'm trying to make a custom M6 tool change script and I'm running into some issues. I'm certain that these issues are stemming from my lack of experience in Lua.

Here's what I'm trying to accomplish:

I have a 5x10 router that I'm trying to do a manual tool change on. What I'd like to do is when a tool change command comes up in G Code (ex T2M6) I want the machine to ask jog to a particular place and stop and wait for me to change the tool. From there I want it to tap on a zero plate, measure the tool, apply the tool offset and keep on cutting.

I found a script in a Mach video that works mostly out of the box (albeit with the movable touch plate). I've been trying to slowly modify that script one bit at a time because it's mostly what I want.

My first roadblock has been running into soft limits. Specifically that I cannot disable them when the script is running.

I looked at the API docs and I do see that you can disable (and re-enable) soft limits with lua script. I tried putting some lines of code into my M6 script to do that, but thus far they aren't working.

I think what's going on is that the soft limits code example in the API docs doesn't show the code being used in the context of a larger script, but that's just a guess on my part.

Code: [Select]
function m6()
   
local inst = mc.mcGetInstance()

----------------------------------------------------------------------------------
--change lines here to either auto rapid, or manually jog to a tool change position
----------------------------------------------------------------------------------
--Manual Lines. Uncomment line below to allow you to manually jog to a tool change position.
local MyChoice = wx.wxMessageBox("Click OK, \nThen Jog to A Safe Tool Change Position,\nInsert New tool,\nThen Click Cycle Start.","Click OK to continue" , 16)
---------------------------------------------------------------------------------
--Auto Lines.  Uncomment both lines below (and comment out local MyChoice line above) to automatically move to tool change position.
--Edit to include the machine coordinate values of that tool change position.

--AUTO LINES
--mc.mcCntlGcodeExecuteWait(inst, "G53 G0 Z0\nG53 G0 X14.7148 Y-22.713")--Move the Z to Home.Then to the X, Y Coords for our touch pad.
--mc.mcCntlSetLastError(inst, 'Now in Tool Change Position. Hit Cycle Start!')
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local posmode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3) --get the current mode so we can return to it when macro ends
local selectedtool = mc.mcToolGetSelected(inst)
local currenttool = mc.mcToolGetCurrent(inst)
   
 if selectedtool == currenttool then
        mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")
 else
        mc.mcCntlToolChangeManual(inst, true);
  mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedtool) .. "   Previous Tool == " .. tostring(currenttool))
  mc.mcToolSetCurrent(inst, selectedtool)
 


local MyChoice = wx.wxMessageBox("Click Ok to Begin Probing the New Tool","Click OK to continue" , 16)
--//es turn off soft limits
rc = mc.mcSoftLimitSetState(
0 mInst,
1 axis,
1 on)



        mc.mcCntlSetLastError(inst, "Probing in Progress!")
        mc.mcCntlGcodeExecuteWait(inst, " G91 G31 Z-5. F5.")--probe the new tool
local probedz = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_POS_Z) -- Z Probe position in Machine coords
        mc.mcCntlGcodeExecute(inst, string.format('G ' .. posmode))--return to pre macro mode G90, or G91
  mc.mcCntlGcodeExecuteWait(inst, "G00 G53 Z0 ")--Retract
 
 -- //es turn ON soft limits
--int mInst=0;
--int axis = Z_AXIS;
-- int TurnOn = MC_ON; // MC_OFF to turn off.
-- mcSoftLimitSetState(mInst, axis, TurnOn); // Set the softlimit forthe Z axis to be ON.

 
local NewOffset = probedz
  mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedtool, NewOffset)
  mc.mcCntlSetLastError(inst, string.format("Auto tool setting complete, Offset = %.4f", NewOffset))
wx.wxMessageBox("Toolchange Complete.\nTLO Set")
        end

end

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

This is the current iteration of the script I'm running. Starting on line 34 is where I am trying to disable the soft limits.

I've also tried a snippet of code starting on 34 that looks like this:

Code: [Select]
int mInst=0;
int axis = Z_AXIS;
int TurnOn = MC_ON; // MC_OFF to turn off.
mcSoftLimitSetState(mInst, axis, TurnOn); // Set the softlimit forthe Z axis to be ON.

That also didn't work. It's really hard to figure out why. I've been using the Zerobrain editor in Mach 4. I get errors on compiling. To test "in the real world" I have to shot down mach, save the file, start it back up, home the machine and run the script again. It's very time consuming.

Can someone point me in the right direction as to where I'm failing? I'm good at programming G/M codes on controllers that are similar to Fanuc based ones, but I don't have any formal training in any scripting language and it's really hurting me here!
Re: M6 toolchange Lua script help?
« Reply #1 on: September 06, 2022, 04:30:48 PM »
You have done pretty well so far! In the API docs it gives API usage for Lua and C/C++(I think) looks like you are using the C/C++ version. Below is what you need for lua.

mc.mcSoftLimitSetState(inst, mc.Z_AXIS, mc.MC_ON)

You also don’t need to define variable types with lua, so where you have:
int axis = mc.Z_AXIS
It can just be:
axis = mc.Z_AXIS

But really you don’t need the above as you can use mc.Z_AXIS and mc.MC_ON directly in the API call

Hope this helps..
Re: M6 toolchange Lua script help?
« Reply #2 on: September 06, 2022, 07:59:26 PM »
Hi,
to amplify what Swifty has said....Lua is a type free language, which has advantages and disadvantages as well.

For instance if you write:

local myNumber=123.789
then no troubles Lua stores that as a number, but you could also have written:
local myNumber='Just some random string'
and Lua would store that as a string. The only issue with this idea is when you have a variable and you are unsure about how Lua has stored it. In this case it's
a good practice to be explicit.... for example:
local myNumber=tonumber('123.789')   or
local myNumber=tostring(123.789) say.

You will see the functions tostring() and tonumber() used frequently throughout Lua code. If a variable is interpreted in a manner contrary to your expectation you get some very
weird results, and that where these functions come in handy.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: M6 toolchange Lua script help?
« Reply #3 on: September 06, 2022, 08:27:06 PM »
Joe, thanks for the tip. That should be helpful going forward.

Swift just so we're on the same page I got that script from a Mach 4 help video, the first (And only thing) I changed was the soft limits disable that you see in there.

Looks like your code did what I needed. I was looking int he API and was was confusing is what part of the code to use. You can see in the help doc that the last line is actually all I needed, but I thought I needed the lines above it as well:

 (hopefully that attached)


My next thing I want to try and solve is accounting for the difference in touch off VS the table surface.

What I'm heading towards is having a fixed touch off place mounted to the machine. Typically that plate would be above the table surface in height. So whatever that number is would need to be added to the tool length.

So let's say that the tool touch off is .5" above the table surface, and the tool is measured at 5 inches, it's actual length is 5.5" (or -5.5)

It looks like this line is where the tool length is figured out:

mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedtool, NewOffset)

Could I just change this to: mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedtool, NewOffset + .5)

To add (or subtract) .5" from the height? I thought I tried that, but it seemed to make the script report zero tool height every time I ran it, but there were no error messages?