Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: rafiki on May 19, 2020, 10:10:01 AM

Title: M6 Script Problem
Post by: rafiki on May 19, 2020, 10:10:01 AM
I have written the simplest possible script to position the tool over a touch plate at machine zero, capture the Z, change to a new tool,  and update the Z axis for the new tool.

This code runs just fine, but Mach exhibits bizarre behavior.  After the code runs the Z work zero can be off by anywhere between a few hundreths of a mm to 50mm.  This happens even if you leave the same tool in before and after the change.

Code: [Select]
function m6()

local inst = mc.mcGetInstance()

local selectedTool = mc.mcToolGetSelected(inst)
local currentTool = mc.mcToolGetCurrent(inst)

local MoveDistance = -15     
local MoveSpeed = 15                   

local TouchZ
local CycleWait

  if selectedTool == currentTool then
mc.mcCntlSetLastError(inst, "Current tool same as selected tool.  No tool change required.")
  else
    mc.mcCntlSetLastError(inst, "Tool Change")

mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0 X0.0 Y0.0")

CycleWait = mc.mcCntlWaitOnCycleStart(inst, "Position the tool less than 1/2 inch over the touch plate and click Start", 1000000)

mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z"..MoveDistance.." F"..MoveSpeed)

TouchZ = mc.mcAxisGetPos(inst, 2)

mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")

CycleWait = mc.mcCntlWaitOnCycleStart(inst, "Change the tool, reposition it over touch plate and click Start", 1000000)

mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z"..MoveDistance.." F"..MoveSpeed)

mc.mcAxisSetPos(inst, 2, TouchZ)

mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
 
mc.mcToolSetCurrent(inst, selectedTool)

CycleWait = mc.mcCntlWaitOnCycleStart(inst, "Turn the spindle on, click cycle start, and we are cutting.", 1000000)

mc.mcCntlSetLastError(inst, "Tool Change Completed.")
  end

end

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

It seems like there is a bug somewhere in Mach4.  I don't see any line in this code that could possibly cause this behavior.  Also, the touch screen hangs, and this code hangs if I run either of them after a fresh boot of Mach4.  After Mach4 loads I need to load a GCode file and start it before the touch screen or this script will run properly.

Very frustrating.
Title: Re: M6 Script Problem
Post by: Brian Barker on May 20, 2020, 08:58:57 AM
So there are many things that are wrong with this script. The best thing to do would be to send in to support and they will help you make one that works as you would like. When you have it all working please post it back here so everyone can see the solution.

some of the issues:
You can't use GetAxisPos to get where the probe hit
How your doing the Gcode calls it not correct
You also have NO error checking on any of the calls so you don't know why it is failing.

The guys will hook you up! Trevor is waiting for you

Thanks
Brian
Title: Re: M6 Script Problem
Post by: edcross on December 31, 2020, 04:37:44 PM
Mach4 v 4300 , vcarve desktop pro v 10.5  , PP attached , script from newfangled video on youtube.com - using Mach2/3 atc inch pp from vcarve attached . Can you help edcross
Title: Re: M6 Script Problem
Post by: Brian Barker on December 31, 2020, 07:05:58 PM
I will post one here tomorrow that I made working with a user. We just did a simple button on the screen
Title: Re: M6 Script Problem
Post by: Brian Barker on January 01, 2021, 10:09:44 AM
This Code goes into a button on the screen. It will set the offset of the current tool and the Tool setter is mounted on the table.



Code: [Select]
--PRIVATE this is it runs as it's own chunk outside of the screen
local inst = mc.mcGetInstance()
local ProbeDownDist = -15--distance to look down to find the part
local posmode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3) --get the current mode so we can return to it when macro ends
local currenttool = mc.mcToolGetCurrent(inst)
local curpos = mc.mcCntlGetValue(inst,mc.VAL_AXIS_MACHINE_POS,mc.Z_AXIS)
local rc = 0
local MyChoice = wx.wxMessageBox("Click Ok to Begin Probing the New Tool","Click OK to continue" , 16)
if(MyChoice == 4)then
    mc.mcCntlSetLastError(inst, "Probing in Progress!")
rc = mc.mcCntlGcodeExecuteWait(inst, string.format("G91 G31 Z%.3f F100.", ProbeDownDist))--probe the new tool Change F to set the Feed
if(rc ~= mc.MERROR_NOERROR)then
return;
end

local probedz = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_MACH_POS_Z) -- Z Probe position in Machine coords
local Retract = string.format("G00 G90 G53 Z%.3f\nG%.0f", curpos, posmode)
rc = mc.mcCntlGcodeExecuteWait(inst, Retract)--Retract
if(rc ~= mc.MERROR_NOERROR)then
return;
end

if(math.abs(curpos + ProbeDownDist - probedz) <.01)then
wx.wxMessageBox("Probe did not contact Probe","ERROR" );
else
local OffsetToMasterTool = 100.406;
local NewOffset = probedz - OffsetToMasterTool
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, currenttool, NewOffset)
end
end

Please tell me if this works for you . .Now if you want to change where the setter is located we can do that but the script will need to change.

Thanks
Brian
Title: Re: M6 Script Problem
Post by: jbuehn on January 01, 2021, 04:26:12 PM
Brain - are there any best practices for using PRIVATE? Specifically, is it best to only use it when absolutely necessary?

Thanks!
Title: Re: M6 Script Problem
Post by: Brian Barker on January 01, 2021, 05:48:56 PM
—Private will make it run in its own thread. So it will not effect the GUI Lua chunk. I did that so we could use the execute wait commands. This should be used when you don’t need it to work with the GUI.  Also the message boxes will not hault the execution of the GUI chunk. I think you see where I am going with this ...

—Private must be on the first line
Title: Re: M6 Script Problem
Post by: edcross on January 02, 2021, 08:37:22 AM
Brian , is there a video on how to insert a button on the mach4 screen ?
Title: Re: M6 Script Problem
Post by: Brian Barker on January 02, 2021, 11:09:03 AM
https://www.machsupport.com/mach4-screen-editing-custom-button-functions/

I think that will get you close, and I think we did a video of simple screen edits.

Edit: check his out https://m.youtube.com/watch?v=P1xZkFgS5cQ
Title: Re: M6 Script Problem
Post by: edcross on January 02, 2021, 01:20:40 PM
Brian , i was able to create a button called tool change in the jogging panel . How do i add your M6 script it it , i did not see that in the example video .

     Thanks   Edcross
Title: Re: M6 Script Problem
Post by: Brian Barker on January 02, 2021, 04:56:26 PM
When you see the properties for the button. You will see a properties with a lightning bolt through it. You just copy and past the code in the on clicked script.
Title: Re: M6 Script Problem
Post by: gorf23 on January 04, 2021, 04:20:47 PM
Brian

One problem i seem to be having is the rc return value from the g31, i get 0 back if the probe is hit or has run to the end of the travel
if i run to the the end of travel i get the message in the history that probe interrupted but still no error value returned in the rc only 0.
so when i check for the error the code skips over the error and return statements, and just keeps running.

this it the line i am running
rc = mc.mcCntlGcodeExecuteWait(inst, string.format("G91 G31 Z-%.3f F%.3f", ZMaxTravel, SlowFeed))     --probe Z

Thanks gary   
Title: Re: M6 Script Problem
Post by: Brian Barker on January 04, 2021, 05:10:21 PM
Hello,
Is this with the ESS ? if so you need to change a setting in the config.. Don't know what it is off the top of my head :(
Title: Re: M6 Script Problem
Post by: gorf23 on January 05, 2021, 08:02:18 AM
I was testing this with the pokeys57cnc, i do has a ESS also i will test it on that later...

Thanks gary
Title: Re: M6 Script Problem
Post by: gorf23 on January 05, 2021, 07:45:50 PM
Brian

I went back to the ESS and checked my code, i am using the ESS probing register to detect probe hit or if not hit error, not mach4.

I had asked pokeys to add the probing register over a year ago and they did, but they never finished it to get working correct the only values working are when probe starts moving =  2, and   -4  but that value is set when probe is hit or when reached end of travel so no way to tell the difference.
I did email pokeys again a couple days ago but haven't herd back

Thanks gary
Title: Re: M6 Script Problem
Post by: Brian Barker on January 05, 2021, 08:33:44 PM
I just do the math to see if it hit or not. That is sort of standard for big machines. Am I missing something ? My code I posted needs nothing about the probe hit or not, it will ask tell you if it did
Title: Re: M6 Script Problem
Post by: gorf23 on January 05, 2021, 08:52:05 PM
Sorry i may have got off topic here maybe i should start a new topic

Gary