Sharing this for helping others and for feedback.
Neutron lathe is retrofit with Pokeys57cnc.
6 Tools on rotation with movement/change in one direction and locking in the other.
6 Sensors on the back with a magnet to sense when the tool goes past the locking point.
An induction sensor to ensure the turret has moved fully into lock.
24V brushed DC motor with IBT_2 driver. One pin for direction one for enable. Can set motor speed with PWM on enable.
inst = mc.mcGetInstance()
-- Grabs register value in string format
function GetRegister_Str(regname)
--inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
return mc.mcRegGetValueString(hreg)
end
-- Grabs register value in number format
function GetRegister_Num(regname)
--inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
hreg = tonumber(hreg)
return mc.mcRegGetValueString(hreg)
end
--Writes to a register
function WriteRegister(regname, regvalue)
--inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("iRegs0/%s", regname))
mc.mcRegSetValueString(hreg, tostring(regvalue))
end
--Writes to a POKEYS register
function WritePokeysReg(regname, regvalue)
--inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, string.format("PoKeys_42110/%s", regname))
mc.mcRegSetValueString(hreg, tostring(regvalue))
return
end
--Grabs the state of a signal
function GetSignal(signame)
--inst = mc.mcGetInstance()
local hsig = mc.mcSignalGetHandle(inst, signame)
return mc.mcSignalGetState(hsig)
end
--Writes to a signal
function WriteSignal(signame, setval)
--inst = mc.mcGetInstance()
local hsig = mc.mcSignalGetHandle(inst, signame)
mc.mcSignalSetState(hsig, setval)
return
end
function m6()
local nTools = 6
local turretMove = false
local turretLock = false
local selectedTool = mc.mcToolGetSelected(inst)
selectedTool = math.tointeger(selectedTool)
local currentTool = mc.mcToolGetCurrent(inst)
currentTool = math.tointeger(currentTool)
local toolReached = 0
--map an array of tool turret position signals to tool numbers
local Tools = {
[1] = mc.ISIG_INPUT1,
[2] = mc.ISIG_INPUT2,
[3] = mc.ISIG_INPUT3,
[4] = mc.ISIG_INPUT4,
[5] = mc.ISIG_INPUT5,
[6] = mc.ISIG_INPUT6}
local toolSig={}; --empty array for storing signal values
--if selected tool is invalid msg and exit
if (selectedTool>nTools) or (selectedTool<1) then
mc.mcCntlSetLastError(inst, "Invalid Tool! Must be 1-6")
do return end
end
--if tool is the same exit tool change
if selectedTool == currentTool then
mc.mcCntlSetLastError(inst, "Tools are the same no change needed!")
do return end
end
--Check if turret is locked if not msg and exit
turretLock = GetSignal(mc.ISIG_INPUT0)
--turretLock = mc.mcSignalGetState(ISIG0_hand)
if turretLock == 1 then
mc.mcCntlSetLastError(inst, "Turret may not be locking properly or lock sensor may be misaligned")
do return end
end
--Remove this line if you would not like the Z axis to move
--mc.mcCntlGcodeExecuteWait(inst, "g90 g53 g0 z0");--Move the Z axis to 400 out of the way
----Go forward until tool sensor input matches selectedTool----
WriteSignal(mc.OSIG_OUTPUT0, 1) --Set direction to move turret
--mc.mcSignalSetState(OSIG0_hand, 1)
WritePokeysReg("PWM 20 duty", 0.77)-- Set motor output PWM level ie turn motor on
turretMove = true
while (turretMove == true) do
--Grab all the states of the turret sensors
for i=1,nTools,1 do
toolSig[i] = GetSignal(Tools[i])
end
--Check if the turret sensor we want has triggered
--if so turn off motor
if toolSig[selectedTool] == 0 then
WritePokeysReg("PWM 20 duty", 0)-- Set motor OFF
turretMove = false
end
end
----lock turret in place and check locked----
WriteSignal(mc.OSIG_OUTPUT0, 0)--Set direction to lock turret
--mc.mcSignalSetState(OSIG0_hand, 0) --Set direction to lock turret
WritePokeysReg("PWM 20 duty", 0.77)-- Set motor output PWM level ie turn motor on
turretMove = true
while (turretMove == true) do
turretLock = GetSignal(mc.ISIG_INPUT0) --get lock signal
--if turret is locked and we stopped on the selected tool then all good
if (turretLock == 0) then
WritePokeysReg("PWM 20 duty", 0)-- Set motor OFF
turretMove = false
mc.mcToolSetCurrent(inst, selectedTool) --set current tool
mc.mcCntlSetLastError(inst, "Auto Tool Change Complete")
do return end
--elseif (turretLock == 0) and (toolSig[selectedTool] == 1) then
-- WritePokeysReg("PWM 20 duty", 0)-- Set motor OFF
-- turretMove = false
-- mc.mcCntlSetLastError(inst, "Tool locking error check correct tool check lock sensor")
-- do return end
--end
end
do return end
end
if (mc.mcInEditor() == 1) then
m6()
end