Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: OregonMike on November 20, 2023, 02:09:46 PM
-
It's my first Lua attempt, and a macro looked less challenging than a Lua panel. I to code this -
After homing, I use the bit like an edge finder (turning it manually as I jog) and set the X and Y zero. I then jog X and Y by 1/2 the bit diameter using the MDI (hopefully after raising the Z) and zero Z and Y again. I verify that the cutter is centered over the corner of the workpiece.
When I step through this code in the debugger, it works. The axes jog and the work coordinates are zeroed.
function m1250()
local inst = mc.mcGetInstance()
local AxisJog = .125
-- Jog controller 0 in the X and Y axes.
mc.mcJogIncStart(0, 0, AxisJog)
mc.mcJogIncStart(0, 1, AxisJog)
-- Set the position of the X axis by changing the fixture offset.
mc.mcAxisSetPos(0, 0, 0)
mc.mcAxisSetPos(0, 1, 0)
end
if (mc.mcInEditor() == 1) then
m1250()
end
When I run this from the MDI (m1250) on the CNC (or the Mach4-Demo), the coordinates zero, but the machine does not jog.
I think this tells me the macro is being compiled. It is enabled.
I have deleted the mcLua.mcc file and restarted Mach4.
I suspect this has something to do with the Mach4 core versus the GUI, but I'm at a loss.
Can anyone suggest how to make the jog command work?
Thank you in advance for suggestions.
(I would guess the work coordinates could be changed without jogging, but I would like to move the machine to the new (0,0) to get visual confirmation.)
-
CntlGcodeExecute and CntlGcodeExecuteWait will not compile (pasted at end of post) into the m1250 file described above.
Suggestions?
Is there another jog command that might work in a macro in the profile\...macros directory??
Does this code need to be located somewhere else so jog commands can be executed? m1250 gets called from the MDI okay, but it seems like the mcJogIncStart and CntlGcodeExecute can't be found when they are called from m1250.
-- Jog controller 0 in the X and Y axes.
-- mc.mcJogIncStart(0, 0, AxisJog)
-- mc.mcJogIncStart(0, 1, AxisJog)
mc.mcCntlGcodeExecute(inst, “G00 X0.125 Y0.125” )
-- mc.mcCntlGcodeExecuteWait(inst, “G00 X0.125 Y0.125” )
Can't start debugging for 'C:\Mach4Hobby\Profiles\Mach4Router\Macros\m1250.mcs'. Compilation error:
m1250.mcs:9: unexpected symbol near '<\226>'
Program stopped (pid: 14244).
-
I do not know about the jog commands but i do something similar.
What you need to do is get the machine coordinates for your current position.
Then add your 0.125 to them.
Then use a g0 or g1 move to the new position.
-
Thank you, Bill. I have done that manually for years, typing the 1/2-bit-diameter into the MDI and zeroing with mouse clicks.
Since my post I did add a button with script to read the bit diameter (0.5 in this example) from a text box and write the following to the MDI
G0 Z1
G0 X0.25 Y0.25
m1250()
When executed, the machine moves and the call to m1250 runs this code to reset the local coordinates.
mc.mcAxisSetPos(0, 0, 0)
mc.mcAxisSetPos(0, 1, 0)
I could have written G90 to the MDI instead of m1250(), but the local coordinates would change back if a G90.1 ever got into the Gcode.
What I would like to do is get the whole thing to work from the button. I think a module is the way to do this, but I haven't got any code to run in a module for the button (even though the scripting manual makes it sound easy).
Bill - I think you answered a question on soft limits override for me about a year ago and said the override just didn't work. Support agreed. The override does work now in the current version of Mach4.
-
A button can run a function.
button code
wait = coroutine.create (SetWorkZero)
SetWorkZero is function name
screen load script code
function SetWorkZero()
what you want to do
end
-
I tried in vain to use "wait = coroutine.create(TestButton)". Does it go in the "Clicked Script". I can't find it in the screen or script manual. Could you say a little more about how it is used, where it goes, or what manual it's in. I don't mind reading them.
I can put "TestButton()" in for the "clicked script" and it finds the function in the Screen Load Script (I added it there). The problem is that the axes do not get zeroed. The machine jogs and the function goes to completion (the last line executes) without errors but these two lines do nothing.
mc.mcAxisSetPos(inst, 0, 0)
mc.mcAxisSetPos(inst, 1, 0)
The button does not find the function if it's in the modules directory and I do get an error that it's nil.
I would make a code window in the post as other do, if I knew how. Sorry.
The code-
function TestButton()
--Move axis 1/2 bit diameter in X+ and Y+
--Set current position as X and Y zero in work coordinates
--Get bit diameter and set jog distance
local strBitDiam = scr.GetProperty("JogToValue", "Value")
local AxisJog = tonumber(strBitDiam) / 2
local strAxisJog = tostring(AxisJog)
-- Jog controller 0 in the X and Y axes;
mc.mcJogIncStart(inst, 2, 1)
mc.mcJogIncStart(inst, 0, AxisJog)
mc.mcJogIncStart(inst, 1, AxisJog)
--Put code in MDI to show step
--This does execute
scr.SetProperty("mdi1", "Value", "After jog")
-- Set local coordinates to zero for the X and Y axes
--These do not execute, or at least values do not get zeroed. No error returned.
mc.mcAxisSetPos(inst, 0, 0)
mc.mcAxisSetPos(inst, 1, 0)
--Put code in MDI to show step
--This does execute
scr.SetProperty("mdi1", "Value", "After zeroing")
end
-
Hi,
Add this function to the screen load script
function TestButton()
--Move axis 1/2 bit diameter in X+ and Y+
--Set current position as X and Y zero in work coordinates
local inst = mc.mcGetInstance()
--Get bit diameter and set jog distance
local strBitDiam = scr.GetProperty("JogToValue", "Value")
local AxisJog = tonumber(strBitDiam) / 2
-- Jog controller 0 in the X and Y axes;
mc.mcJogIncStart(inst, 2, 1)
coroutine.yield() --Wait for idle state
mc.mcJogIncStart(inst, 0, AxisJog)
mc.mcJogIncStart(inst, 1, AxisJog)
coroutine.yield() --Wait for idle state
-- Set local coordinates to zero for the X and Y axes
mc.mcAxisSetPos(inst, 0, 0)
mc.mcAxisSetPos(inst, 1, 0)
end
Add this to clicked script or left up script of a button
wait = coroutine.create(TestButton)
-
Thank you. I was reading some yesterday and ran into the coroutine.create as used in another part of Mach4 and sort of understood what it does. I'll do this when I get home and can access Mach4.
-
Thank you again. I did exactly what you said and, of course, it worked. Waiting for the idle state is something I totally missed.
I also see that this
function Test_Clicked_Script(...)
wait = coroutine.create(TestButton)
end
(in addition to the function) was put into the ScreenScript.lua file which was much easier (and less risky) than me trying to put the code in a module.