With the help of the forum and after spending many many hours reading the API docs and trying to grasp how to code i finally arrived at a script that is working for the moment. !!! Huray!!!
I've made a module with all the needed functions includes so they can be called from within a button or marco.
I've also set up an m6 macro for my needs. This relies on only needing 2 tools at a time. Before running the code the Z and A axis needs to be zeroed, after that switching between spindle and router follows the tools demanded.
There is a toggle button + led indicating router or spindle active + output for M3/m8. This still does not work after returning out of an m6. The button can be in the wrong state despite asking in the code to check the signal and changing screen propperties.
What could be better is including more API return codes, but this is something that i still struggle with, if anyone has pointers of how to get this better/safer help is appreciated.
Lastly i have the same spindle script included into the screen unload script so when staring mach 4, the spindle is automatically set to normal state. This is working 8/10 and sometimes does not change the router back to spindle. The reason why i want to do this before closing the program is so that the Z and A axis dro don't get screwed up, and the previous values can be used after a restart/home procedure.
All comment are welcome, and for anybody that has helped me out this far many thanks!
Codes below:
local zControl = {} --Control for dual head setup & button functions
local inst = mc.mcGetInstance("MyzControl.lua")
-----------Variables List--------------------------------------------
local OffsetX = -7.9266 --Spindle vs router position
local OffsetY = -156.5752 --Spindle vs router position
local ToolSet = -12.8 -- Toolsetplate vs machine bed position
--local OffsetMBX = (80 + OffsetX) --used when need offset to spindle calc variable
--local OffsetMBY = (200 + OffsetY) --used when need offset to router calc variable
local Spindle = 2 -- motor 2
local Router = 4 -- motor 4
---------------Gcode Use in macro M6-----------------------------------
function zControl.CallGcodeMacro() -- Checks if gcode gets executed before running the rest of the script
local rc = mc.mcCntlGcodeExecuteWait (inst,"G90 G53 G0 Z-20 A-20")
if rc~=0 then
mc.mcCntlEnable(inst, false)
mc.mcCntlSetLastError (inst, "Something whent wrong when executing gcode / machine disabled for safety. error state "..rc)
end
end
---------------Gcode Use in button Spindle / router---------------------
function zControl.CallGcodeButton() -- Checks if gcode gets executed before running the rest of the script
local rc = mc.mcCntlGcodeExecute (inst,"G00 G90 G53 G0 Z-20 A-20\n G90 X0 Y0 \n")
if rc~=0 then
mc.mcCntlEnable(inst, false)
mc.mcCntlSetLastError (inst, "Something whent wrong when executing gcode / machine disabled for safety. error state "..rc)
end
end
---------------Goto machine bed & probe button ----------------------------------------------------
function zControl.ProbeMBZ()
mc.mcCntlSetLastError(inst, "Goto machine bed probe point")
local val = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_14) --PoundVar 4014 coordinates sytem
local msg = "G00 G90 G53 Z-20 A-20\n G59 X0 Y0\nG"..val--Use G59 as workpiece zero, return to current fixture offset
mc.mcCntlGcodeExecute(inst, msg) --Use G59 as workpiece zero for position Return to G54
wx.wxMessageBox("Load the correct tool & lower the bit within 50mm before probing. Attach the MAGNET!!!","If not abort",16)
mc.mcCntlSetLastError(inst, "Probing in progress!")
mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z-50 F200")
--local ToolSet = -12.8 -- Toolset plate difference machine bed / plate
mc.mcAxisSetPos(inst, mc.Z_AXIS, ToolSet)
mc.mcCntlGcodeExecute(inst,"G00 G90 G53 Z-20")
end
-----------------If spindle is loaded do nothing / eliminate DRO Swap---------------------------------
function zControl.CheckSpindle()
local axisID, rc = mc.mcMotorGetAxis(inst,Spindle) --Z axis maakt deel uit van variable rc om return code te bekomen
local h_OUTPUT4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
local state = mc.mcSignalGetState(h_OUTPUT4)
if ((axisID == mc.Z_AXIS) and (state == 1) and (rc==mc.MERROR_NOERROR)) then --If Zaxis is loaded with motor 2 spindle is active
mc.mcCntlSetLastError(inst, "Spindle already active, No swap required")
--scr.SetProperty('togRouterSpindle', 'Button State', '0')
else zControl.UseSpindle()
end
end
-----------------If router is loaded do nothing / eliminate DRO Swap---------------------------------
function zControl.CheckRouter()
local axisID, rc = mc.mcMotorGetAxis(inst,Router) --Z axis maakt deel uit van variable rc om return code te bekomen
local h_OUTPUT4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
local state = mc.mcSignalGetState(h_OUTPUT4)
if ((axisID == mc.Z_AXIS) and (state == 0)and (rc==mc.MERROR_NOERROR)) then --If Zaxis is loaded with motor 4 router is active
mc.mcCntlSetLastError(inst, "Router already active, No swap required")
--scr.SetProperty('togRouterSpindle', 'Button State', '1')
else zControl.UseRouter()
end
end
---------------------Setup spindle on Z axis / Activate M03 / Swap Workpiece DRO / Check states-------
function zControl.UseSpindle()
if (zControl.UnmapMotors() == 1) then --Return out of UseRouter if unmapmotors returns an error
return 1 -- failure
end
mc.mcAxisMapMotor(inst, mc.Z_AXIS, Spindle)
mc.mcAxisMapMotor(inst, mc.A_AXIS, Router)
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_X, 0)
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, 0)
local TempZ = mc.mcAxisGetPos(inst, mc.Z_AXIS) -- Get work position of Z-Axis
local TempA = mc.mcAxisGetPos(inst, mc.A_AXIS) -- Get work position of A-Axis
mc.mcAxisSetPos(inst, mc.A_AXIS, TempZ) -- Replace A-axis value with Z value
mc.mcAxisSetPos(inst, mc.Z_AXIS, TempA) -- Replace Z-axis value with A value
local h_OUTPUT4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
--local h_RELAY2 = mc.mcSignalGetHandle(inst, mc.OSIG_MISTON) --Future dust collection
mc.mcSignalSetState(h_OUTPUT4, 1)
--mc.mcSignalSetState(h_RELAY2, 1) --Future dust collection
local axisID, rc = mc.mcMotorGetAxis(inst,Spindle) --Z axis maakt deel uit van variable rc om return code te bekomen
local state = mc.mcSignalGetState(h_OUTPUT4)
if ((axisID == mc.Z_AXIS) and (state == 1) and (rc==mc.MERROR_NOERROR)) then
mc.mcCntlSetLastError(inst, 'Spindle active - Output set')
scr.SetProperty('togRouterSpindle', 'Button State', tostring(0))
return
else
mc.mcCntlSetLastError(inst, "Spindle or output not activated / process stopped")
mc.mcCntlEnable (inst, false)
end
end
---------------------Setup router on Z axis / Activate M08 / Swap Workpiece DRO / Check states-------
function zControl.UseRouter()
if (zControl.UnmapMotors() == 1) then --Return out of UseRouter if unmapmotors returns an error
return 1 --failure
end
mc.mcAxisMapMotor(inst, mc.Z_AXIS, Router)
mc.mcAxisMapMotor(inst, mc.A_AXIS, Spindle)
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_X, OffsetX)
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, OffsetY)
local TempZ = mc.mcAxisGetPos(inst, mc.Z_AXIS) -- Get work position of Z-Axis
local TempA = mc.mcAxisGetPos(inst, mc.A_AXIS) -- Get work position of A-Axis
mc.mcAxisSetPos(inst, mc.A_AXIS, TempZ) -- Replace A-axis value with Z value
mc.mcAxisSetPos(inst, mc.Z_AXIS, TempA) -- Replace Z-axis value with A value
local h_OUTPUT4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
mc.mcSignalSetState(h_OUTPUT4, 0)
mc.mcCntlSetLastError(inst, 'Router active')
local axisID, rc = mc.mcMotorGetAxis(inst,Router) --Z axis maakt deel uit van variable rc om return code te bekomen
local state = mc.mcSignalGetState(h_OUTPUT4)
if ((axisID == mc.Z_AXIS) and (state == 0)and (rc==mc.MERROR_NOERROR)) then
mc.mcCntlSetLastError(inst, 'Router active - Output set')
scr.SetProperty('togRouterSpindle', 'Button State', tostring(1))
return
else
mc.mcCntlSetLastError(inst, "Router or output not activated / process stopped")
mc.mcCntlEnable (inst, false)
end
end
----------------Unmap motors to be safe----------------------------------
function zControl.UnmapMotors()
local rc = mc.mcAxisUnmapMotor(inst, mc.A_AXIS, Router)
if ((rc ~= mc.MERROR_MOTOR_NOT_FOUND) and (rc ~= mc.MERROR_NOERROR)) then
-- expect mc.MERROR_MOTOR_NOT_FOUND or mc.MERROR_NOERROR
return 1 -- failure
end
rc = mc.mcAxisUnmapMotor(inst, mc.Z_AXIS, Router)
if ((rc ~= mc.MERROR_MOTOR_NOT_FOUND) and (rc ~= mc.MERROR_NOERROR)) then
-- expect mc.MERROR_MOTOR_NOT_FOUND or mc.MERROR_NOERROR
return 1 -- failure
end
rc = mc.mcAxisUnmapMotor(inst, mc.A_AXIS, Spindle)
if ((rc ~= mc.MERROR_MOTOR_NOT_FOUND) and (rc ~= mc.MERROR_NOERROR)) then
-- expect mc.MERROR_MOTOR_NOT_FOUND or mc.MERROR_NOERROR
return 1 -- failure
end
rc = mc.mcAxisUnmapMotor(inst, mc.Z_AXIS, Spindle)
if ((rc ~= mc.MERROR_MOTOR_NOT_FOUND) and (rc ~= mc.MERROR_NOERROR)) then
-- expect mc.MERROR_MOTOR_NOT_FOUND or mc.MERROR_NOERROR
return 1 -- failure
end
return 0 -- success
end
function m6() --Dual Head
local inst = mc.mcGetInstance()
local SelectedTool = mc.mcToolGetSelected(inst)
SelectedTool = math.tointeger(SelectedTool)
local CurrentTool = mc.mcToolGetCurrent(inst)
CurrentTool = math.tointeger(CurrentTool)
package.path = wx.wxGetCwd() .. "\\Modules\\AvidCNC\\?.lua"
zc = require "MyzControl"
----------------Toolchange-------------------------------
if ((SelectedTool >=1) and (SelectedTool <=99)) then
zc.CallGcodeMacro()
zc.CheckSpindle()
mc.mcToolSetCurrent(inst, SelectedTool)
elseif ((SelectedTool >=100) and (SelectedTool <=150))then
zc.CallGcodeMacro()
zc.CheckRouter()
mc.mcToolSetCurrent(inst, SelectedTool)
end
------------------Remarks ----------------------------------
--[[if selectedTool == currentTool then
mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")]] --Unused always needs to check Spindle or router activation
end
if (mc.mcInEditor() == 1) then
m6()
end
added to the screen unload script
zc.CheckSpindle()