Hello Guest it is April 26, 2024, 07:32:43 AM

Author Topic: Avid cnc dual Z  (Read 3333 times)

0 Members and 1 Guest are viewing this topic.

Offline DAAD

*
  •  103 103
    • View Profile
Avid cnc dual Z
« on: March 06, 2020, 03:25:16 AM »
Hello,

I've recently bought an avid pro cnc with dual Z.
One is setup with a spindle, the other is setup with a router on an extrenal relay (m07).
At the moment i 'cant swap between both Z axis propperly.

Anyone has an idea of howto tackle this in mach 4?

I see some options, but have never written any code... So this is my biggest hurdle.

Option 1: Tool number changes (1-99) / (100-200) from Z to A axis with an x/Y tool offset included
I suppose i need an adapted M6 tool change macro tha t get this done?

Option 2: Some kind of separate button inside mach 4, that arranges:
-Swap between Z and A axis
-Ofsetst the x/Y position
-Changes the M03 to M07 command
Run 2 separate Gcode files for different axis.

Input would be appreciated.
If someone can help me or knows someone who can help me to get this done, please get in touch.
It's for running in an commercial setting and i'm prepared to pay for a solution.

Thanks in advance,

Adam


Re: Avid cnc dual Z
« Reply #1 on: March 10, 2020, 03:10:09 AM »
if its 2 separate servo axis button not help you
you must define it in M6 but this not only
because also yours gcode file should consider when use A also on file use a instead of z
or you can do some thing in lua that replace all  z with a

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: Avid cnc dual Z
« Reply #2 on: March 10, 2020, 04:51:13 AM »
mc.mcAxisMapMotor(MINSTANCE mInst, int axis_id, int motor_id)
mcAxisUnmapMotor(MINSTANCE mInst, int axis_id, int motor_id)

You will need to pay attention to your homing too as both will need to be homed.  I would use C axis (linear W) to home the router instead of A.  You might want a rotary A at some point.  Make sure the C axis is included in your homing order.

The default mapping should be done in the load script, as the Mach config will be changed to whatever the motor the Z axis had mapped last. 

So if your spindle is motor 3 and your router is motor 4, the load script should contain:
Code: [Select]
mc.mcAxisMapMotor(0, mc.Z_AXIS, 3)
mc.mcAxisMapMotor(0, mc.C_AXIS, 4)

You might want the make the home all button map the Z and C axes to their default motors too.  Lots of stuff to think about here...  :( 

When running, you can swap the Z axis to the different motors with some script. 

Code: [Select]
-- To change Z from spindle to router
mc.mcAxisUnmapMotor(0, mc.Z_AXIS, 3)
mc.mcAxisMapMotor(0, mc.Z_AXIS, 4)
mc.mcSetPoundVar(0, mc.SV_HEAD_SHIFT_X, <X offset from spindle Z>)
mc.mcSetPoundVar(0, mc.SV_HEAD_SHIFT_Y, <Y offset from spindle Z>)
mc.mcSignalSetState(0, mc.OSIG_OUTPUT10, 1) -- marking we are using the router as Z (and possibly energize a SPDT to rout the M3 signal).
-- To change Z from router to spindle
mc.mcAxisUnmapMotor(0, mc.Z_AXIS, 4)
mc.mcAxisMapMotor(0, mc.Z_AXIS, 3)
mc.mcSetPoundVar(0, mc.SV_HEAD_SHIFT_X, 0)
mc.mcSetPoundVar(0, mcSV_HEAD_SHIFT_Y, 0)
mc.mcSignalSetState(0, mcOSIG_OUTPUT10, 0) -- marking we are using the spindle as Z (and possibly de-energize a SPDT to route the M3 signal).

This solution would probably be well served being written as a LUA module.  That way, screen scripts can call it and M codes can call it too. 

 I would also think about making M3 and M5 a script that energizes/de-energizes the appropriate relay to turn the spindle/router on/off depending on which is currently mapped to the Z axis.  I would probably do this with a SPDT relay wired to an output that is asserted when using the router commented above).  Sometimes it is better to do things in hardware...

Here is an example lua module to get you started.  I coded it up quickly and for brevity it does NOT check all of the API return codes.  Which you should do!!

Code: [Select]
local zControl = {
DESC = "Sample Z motor control module."
}

local spindle = 3
local router = 4
local routerOffsetX = 6.500
local routerOffsetY = -3.500

---  This can be called, if needed, but it is more of a utility function for the below module functions.
function zControl.UnmapMotors()
--Just unmapp everything to be safe.
local rc = mc.mcAxisUnmapMotor(0, mc.C_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(0, 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(0, 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

---  Call this function to use the spindle on the Z axis.
function zControl.UseSpindle()
if (zControl.UnmapMotors()) then
return 1 -- failure
end
mc.mcAxisMapMotor(0, mc.Z_AXIS, spindle)
mc.mcSetPoundVar(0, mc.SV_HEAD_SHIFT_X, 0)
mc.mcSetPoundVar(0, mcSV_HEAD_SHIFT_Y, 0)
-- marking we are using the spindle as Z (and possibly de-energize a SPDT to route the M3
mc.mcSignalSetState(0, mc.OSIG_OUTPUT10, 0)
return 0 -- success
end

---  Call this function to use the spindle on the Z axis.
function zControl.UseRouter()
zControl.UnmapMotors()
mc.mcAxisMapMotor(0, mc.Z_AXIS, router)
mc.mcSetPoundVar(0, mc.SV_HEAD_SHIFT_X, routerOffsetX)
mc.mcSetPoundVar(0, mcSV_HEAD_SHIFT_Y, routerOffsetY)
marking we are using the router as Z (and possibly de-energize a SPDT to route the M3
mc.mcSignalSetState(0, mc.OSIG_OUTPUT10, 1)
return 0 -- success
end

---  This function probably should be called by the home all button script and the screen load script.
function zControl.DefaultMotors()
if (zControl.UseSpindle()) then
return 1 -- failure
end
if (mc.mcAxisMapMotor(0, mc.C_AXIS, router) ~= mc.MERROR_NOERROR) then
return 1 -- failure
end
return 0 -- success
end

return zControl

This is definitely advanced stuff and you are going to have to make this all work yourself.  Because nobody is going to have a machine that is setup exactly like yours, etc...  It is going to take some learning and patience.  But it can be done. 

Steve

Offline DAAD

*
  •  103 103
    • View Profile
Re: Avid cnc dual Z
« Reply #3 on: March 10, 2020, 07:30:02 AM »
Thanks for taking the time to reply!

Advanced stuff for sure! Will take my time and try to get my head around...

Daad

Offline DAAD

*
  •  103 103
    • View Profile
Re: Avid cnc dual Z
« Reply #4 on: March 14, 2020, 02:54:10 PM »
Tried to read up on lua code this week and took some time this afternoon to fiddle with the axis mapping.

After some trial and error i've got a working button to swap the axis and the headoffest! YAY!!

For future reference see my code attached:

Code: [Select]
-- To change Z from spindle to router
mc.mcAxisUnmapMotor(0, mc.Z_AXIS, 2)
mc.mcAxisUnmapMotor(0, mc.A_AXIS, 4)
mc.mcAxisMapMotor(0, mc.Z_AXIS, 4)
mc.mcAxisMapMotor(0, mc.A_AXIS, 2)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_X, -7.8007)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_Y, -157.7648)
mc.mcSignalSetState(0, mc.OSIG_OUTPUT10, 1) -- marking we are using the router as Z (and possibly energize a SPDT to rout the M3 signal).

Next step would be to get this into a macro.
I've tried to setup an m230 macro for this one, but i can't get it working.
Someone that can set me in the right direction to get this macro functional?

This way i would be able to load it trough my postprocessor

@Steve, many thanks for the provided code, without i would not have pulled this off.

Next week i will check the other code, and try to get the m3 / m7 swap (spindle/router).
But for now i'm happy that both Z's are alive!!

Adam



« Last Edit: March 14, 2020, 02:58:10 PM by DAAD »

Offline DAAD

*
  •  103 103
    • View Profile
Re: Avid cnc dual Z
« Reply #5 on: March 15, 2020, 03:40:22 PM »
Something i didn't notice yesterday is that when i zero The Z axis and do a swap, only the last Zero position is held.

I should need something so when i Zero Z ands swap axis (A becomes Z) the Zero value for the Z gets saved onto the axis i've zeroed.
If i then Zero the second Z axis (A) the original value stays linked to the original Z. Same things should happen when i swap from A to Z (Zero A gets saved onto A axis). This way when a toolchange comes, i don't need to rezero the tool height every time.

Any idea howto get this working? At the moment i have a button for axisswap and x/Y position, if i hade a solution for this one, i can use the second z axis in a basic form for now. I would use 2 postprocessors to use spindle or router at whilst trying to sort the rest out.

At the end i would like to have some macro's that automatically call an axis swap/tool swap. This way minimal effort would be used to have 2 tools available.
I'm mostly making cabinets for a living...

Thanks for the input.

Adam

Offline DAAD

*
  •  103 103
    • View Profile
Re: Avid cnc dual Z
« Reply #6 on: March 22, 2020, 03:09:25 PM »
A new update,

Got some time to get the dro values swap with spindle selected.
Next to implement is to get the correct output signals to choose between router and spindle.

Code below (in it's basic state) as i'm still figuring out howto write a working function... (my damned brain can't still find the logic...)

Code: [Select]
-- To change Z from spindle to router
mc.mcGetInstance()
mc.mcCntlGcodeExecuteWait (0, "G90 G53 Z-50 A-50") --Both spindles in known position
local pVal = mc.mcCntlGetPoundVar(0, mc.SV_CURRENT_ABS_Z) -- Get work position of Z-Axis and put in local var P
local tVal = mc.mcCntlGetPoundVar(0, mc.SV_CURRENT_ABS_A) -- Get work position of A-Axis and put in local var T
mc.mcAxisUnmapMotor(0, mc.Z_AXIS, 2)
mc.mcAxisUnmapMotor(0, mc.A_AXIS, 4)
mc.mcAxisMapMotor(0, mc.Z_AXIS, 4)
mc.mcAxisMapMotor(0, mc.A_AXIS, 2)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_X, -7.8007)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_Y, -157.7648)
mc.mcAxisSetPos(0, 3, pVal) ---Set Z Axis op A waarde
mc.mcAxisSetPos(0, 2, tVal) ---Set A Axis op Z waarde
mc.mcSignalSetState(0, mc.OSIG_OUTPUT10, 1) -- marking we are using the router as Z (and possibly energize a SPDT to rout the M3 signal).
wx.wxMilliSleep(500)

All input welcome,

Thanks for the help received yet!

Offline DAAD

*
  •  103 103
    • View Profile
Re: Avid cnc dual Z
« Reply #7 on: April 02, 2020, 02:25:41 PM »
Working screen button to swap functions between router and spindle:

Code: [Select]
-- To change Z from spindle to router
mc.mcGetInstance()
local OffsetX = -7.9266
local OffsetY = -156.5752
local Spindle = 2
local Router = 4

mc.mcCntlGcodeExecuteWait (0, "G0 G90 G53 Z-20 A-20\n X"..OffsetX.."Y"..OffsetY.."") --Both spindles in known position

UnmapMotors() --Function in screen script

mc.mcAxisMapMotor(0, mc.Z_AXIS, Router)
mc.mcAxisMapMotor(0, mc.A_AXIS, Spindle)

mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_X, OffsetX)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_Y, OffsetY )

local TempZ = mc.mcAxisGetPos (inst, 2) -- Get work position of Z-Axis
local TempA = mc.mcAxisGetPos (inst, 3) -- Get work position of A-Axis
mc.mcAxisSetPos(0, 3, TempZ) -- Replace A-axis value with Z value
mc.mcAxisSetPos(0, 2, TempA) -- Replace Z-axis value with A value

local h_OUTPUT10 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
    mc.mcSignalSetState (h_OUTPUT10, 1) -- From M3 to M8

Offline DAAD

*
  •  103 103
    • View Profile
Re: Avid cnc dual Z
« Reply #8 on: April 06, 2020, 07:44:59 AM »
Finall trying to get all info into a module as Steve suggested but can't get it working.

Done the following steps:

1) added this script to the screen script to load the module.
Code: [Select]
--Zcontrol module
package.loaded.zControl = nil
zc = require "MyzControl"

2) MyzControl.lua file added to /modules/avidCNC directory

Code: [Select]
--Check tool & set corresponding axis
--Motor 2 = Z
--Motor 4 = A
--Axis 2 = Z
--Axis 3 =A

local zControl = {}
local inst = mc.mcGetInstance -- Needed?
--local profile = mc.mcProfileGetName(inst)
--local path = mc.mcCntlGetMachDir(inst)

local OffsetX = -7.9266
local OffsetY = -156.5752
local Spindle = 2
local Router = 4

---  This can be called, if needed, but it is more of a utility function for the below module functions.

function zControl.UnmapMotors()
--Just unmapp everything to be safe.
local rc = mc.mcAxisUnmapMotor(0, 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(0, 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(0, 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(0, 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
--Test function message
function zControl.zMessage(msg)
wx.wxMessageBox(msg)
end

--- Call this function to swap Z&A values DRO.
function zControl.SwapPosZA()
local TempZ = mc.mcAxisGetPos (inst, 2) -- Get work position of Z-Axis
local TempA = mc.mcAxisGetPos (inst, 3) -- Get work position of A-Axis
mc.mcAxisSetPos(0, 3, TempZ) -- Replace A-axis value with Z value
mc.mcAxisSetPos(0, 2, TempA) -- Replace Z-axis value with A value
--return 0 -- succes?
end

---  Call this function to use the spindle on the Z axis.
function zControl.UseSpindle()
if (zControl.UnmapMotors()) then
return 1 -- failure
end
mc.mcAxisMapMotor(0, mc.Z_AXIS, Spindle)
mc.mcAxisMapMotor(0, mc.A_AXIS, Router)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_X, 0)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_Y, 0)
local h_OUTPUT10 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
mc.mcSignalSetState(h_OUTPUT10, 0)
mc.mcCntlSetLastError(inst, 'm3 spindle signal ON') -- This will send a history / error window message
-- mc.mcCntlSetLogging(inst, 'm110() Turned OUTPUT10 ON') -- This will send a log window message
return 0 -- success
end

---  Call this function to use the router on the Z axis.
function zControl.UseRouter()
if zControl.UnmapMotors() then
return 1 --failure
end
mc.mcAxisMapMotor(0, mc.Z_AXIS, Router)
mc.mcAxisMapMotor(0, mc.A_AXIS, Spindle)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_X, OffsetX)
mc.mcCntlSetPoundVar(0, mc.SV_HEAD_SHIFT_Y, OffsetY)
local h_OUTPUT10 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
mc.mcSignalSetState(h_OUTPUT10, 1)
mc.mcCntlSetLastError(inst, 'm8 router signal ON') -- This will send a history / error window message
return 0 -- success
end

---  This function probably should be called by the home all button script and the screen load script.
--[[function zControl.DefaultMotors()
if zControl.UseSpindle() then
return 1 -- failure
end
if (mc.mcAxisMapMotor(0, mc.A_AXIS, Router) ~= mc.MERROR_NOERROR) then
return 1 -- failure
end
return 0 -- success
end]]

return zControl

3) Made a test macro M5001 and it seems to work if i call the zMessage function (message pops up) but all other functions i can't call.

[/code]function m5001() --test module function
local inst = mc.mcGetInstance()

package.path = wx.wxGetCwd() .. "\\Modules\\AvidCNC\\?.lua"
zc = require "MyzControl"
--if zc == nill then
--   wx.wxMessageBox (zc==nil)
--else
--zc.zMessage('test hello')
zc.UnmapMotors()
end


if (mc.mcInEditor() == 1) then
 m5001()
end[/code]

All pointers are welcome.

Adam

Offline DAAD

*
  •  103 103
    • View Profile
Re: Avid cnc dual Z
« Reply #9 on: April 17, 2020, 11:52:35 AM »
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:

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


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


Code: [Select]
added to the screen unload script
zc.CheckSpindle()
« Last Edit: April 17, 2020, 11:58:07 AM by DAAD »