Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: Benzon.as on March 14, 2025, 11:10:17 AM
-
I am trying to get my tool changer to work on our Mach4 lathe.
I have space for 6 tools, each spaced 60 degrees apart.
However, I can't get motor A to activate when I call for a tool change.
I have tried using this M6 macro, but nothing happens when I request a change. It seems like I am missing something?
function m6()
inst = mc.mcGetInstance()
if not inst then
mc.mcCntlSetLastError(inst, "Failed to get Mach4 instance.")
return
end
--... (previous code)
if selectedtool == currenttool then
mc.mcCntlSetLastError(inst, "No tool change needed.")
return
end
-- Enable motor A before performing the tool change
mach.MotorEnable(1, true)
mc.mcCntlSetLastError(inst, "Enabled Motor A")
local moves = math.abs(selectedtool - currenttool)
local direction = selectedtool > currenttool and 1 or -1
if moves == 0 then
mc.mcCntlSetLastError(inst, "No tool change needed.")
return
end
mc.mcCntlSetLastError(inst, "Moving to Tool ".. selectedtool)
local max_tool_pockets = 6 -- Set the maximum number of tool pockets supported by your machine
local move_value = math.min(math.abs((direction * moves) * 60), max_tool_pockets)
mc.mcCntlSetLastError(inst, "G0 A".. move_value)
mc.mcCntlGcodeExecuteWait(inst, "G0 A".. move_value.. " G52")
if (move_value == max_tool_pockets) and (direction > 0) then
mc.mcCntlSetLastError(inst, "Tool change completed. Maximum tool pocket reached.")
elseif (move_value == max_tool_pockets) and (direction < 0) then
mc.mcCntlSetLastError(inst, "Tool change completed. Minimum tool pocket reached.")
else
mc.mcCntlSetLastError(inst, "Tool change completed.")
end
-- Disable motor A after the tool change is complete
mach.MotorEnable(1, false)
mc.mcCntlSetLastError(inst, "Disabled Motor A")
mc.mcToolSetCurrent(inst, selectedtool)
mc.mcCntlSetLastError(inst, "Tool Loaded")
if mc.mcInEditor() == 1 then
m6()
end
end
-
-- Enable motor A before performing the tool change
mach.MotorEnable(1, true)
mc.mcCntlSetLastError(inst, "Enabled Motor A")
Make sure you are selecting the correct motor for your tool changer motor
-
Hi,
this code looks really wierd. For example:
if selectedtool == currenttool then
That is the wrong syntax, it should be:
if (selectedtool == currenttool) then
And then there is this:
mach.MotorEnable(1, true)
That is not a valid Mach instruction.......i.e. it is not in the Mach API.
This code looks like it was composed by AI. I suggest you ask the AI gods why it does not work.
We mortals on planet Earth use the Mach API.
I suspect this is what you are looking for:
mcAxisEnable
C/C++ Syntax:
int mcAxisEnable(
MINSTANCE mInst,
int axisId,
BOOL enabled);
LUA Syntax:
rc = mc.mcAxisEnable(
number mInst,
number axisId,
number enabled);
Description:
Enable or disable the specified axis.
Parameters: Parameter Description
mInst The controller instance.
axisId The axis ID.
enabled Send true to enable or false to disable axis.
Returns: Return Code Description
MERROR_NOERROR No Error.
MERROR_INVALID_INSTANCE The mInst parameter was out of range.
MERROR_AXIS_NOT_FOUND The axis specified by axisId was not found.
Notes:
Used to enable or disable axis. Disabling an axis will make motion for the axis impossible.
Usage:
int mInst = 0;
// Set Y_AXIS to be disabled.
mcAxisEnable(mInst, Y_AXIS, false);
Further, you'll have to use the Lua syntax, for example:
local rc=mc.mcAxisEnable(inst,1) and then you'll presumably, by following good coding practice, test the 'rc' to establish whether the function succeeded or not.
Craig