Hello Guest it is March 28, 2024, 12:11:50 PM

Author Topic: M6 Script Problem  (Read 1937 times)

0 Members and 1 Guest are viewing this topic.

M6 Script Problem
« 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.
Re: M6 Script Problem
« Reply #1 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
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: M6 Script Problem
« Reply #2 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
Re: M6 Script Problem
« Reply #3 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
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: M6 Script Problem
« Reply #4 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
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com

Offline jbuehn

*
  •  101 101
    • View Profile
Re: M6 Script Problem
« Reply #5 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!
Re: M6 Script Problem
« Reply #6 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
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: M6 Script Problem
« Reply #7 on: January 02, 2021, 08:37:22 AM »
Brian , is there a video on how to insert a button on the mach4 screen ?
Re: M6 Script Problem
« Reply #8 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
« Last Edit: January 02, 2021, 11:11:24 AM by Brian Barker »
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: M6 Script Problem
« Reply #9 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