Hello Guest it is May 19, 2025, 12:43:45 PM

Author Topic: I am trying to get my tool changer macro to work on our Mach4 lathe.  (Read 853 times)

0 Members and 1 Guest are viewing this topic.

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?

Code: [Select]
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

Offline Graham Waterworth

*
  • *
  •  2,782 2,782
  • Yorkshire Dales, England
Re: I am trying to get my tool changer macro to work on our Mach4 lathe.
« Reply #1 on: March 15, 2025, 09:47:01 AM »
Quote
-- 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
Without engineers the world stops
Re: I am trying to get my tool changer macro to work on our Mach4 lathe.
« Reply #2 on: March 15, 2025, 06:24:55 PM »
Hi,
this code looks really wierd. For example:

Code: [Select]
if selectedtool == currenttool then
That is the wrong syntax, it should be:

Code: [Select]
if (selectedtool == currenttool) then
And then there is this:

Code: [Select]
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:

Quote
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
« Last Edit: March 15, 2025, 06:36:10 PM by joeaverage »
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'