Machsupport Forum
Mach Discussion => Mach4 General Discussion => Mach4 Toolbox => Topic started by: jmu@david.bike on September 19, 2023, 03:24:55 PM
-
Hi, I converted an Emco PC Turn to Mach 4. Everything works perfect except the tool change. The automatic tool changer is turret style and has six positions. input6 is a counter for each tool position; input7 is triggered for tool one. output6 switches on the turret. Processing: When output6 is activated the turret starts to turn. For each tool position input6 is counted up to tool position 6. When input 7 is triggered the tool position starts with tool position one. The turret turns until the correct tool position is counted. Then output6 is deactivated. The turret turns back for 2.5 seconds.
Is there anyone who can create the macro. Of course I pay for it.
Johannes
-
I tried myself, and after first tests the M6 macro for the emco pc turn 50 works. :)
local inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)
package.path = path .. "\\Modules\\?.lua;" .. path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;"
function m6()
local selectedtool = mc.mcToolGetSelected(inst)
local currenttool = mc.mcToolGetCurrent(inst)
local function turretrotate()
local handle = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
local rc = mc.mcSignalSetState(handle, 1)
mc.mcCntlSetLastError(inst, "Turret started")
end
local function turretstop()
local handle = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
local rc = mc.mcSignalSetState(handle, 0)
mc.mcCntlSetLastError(inst, "Turret stopped")
end
if (selectedtool < 1) or (selectedtool > 6) then
rc = mc.mcCntlSetLastError(inst, "Tool no. out of range")
return "Tool change Error."
elseif (selectedtool >= 1) and (selectedtool <= 6) then
if (selectedtool == currenttool) then
mc.mcCntlSetLastError(inst, "Current tool == Selected tool, so there is nothing to do")
return "nothing to do"
else
rc = mc.mcCntlSetLastError(inst, "Performing tool change")
turretrotate()
while (selectedtool ~= currenttool) do
--repeat
local SensorState = mc.mcSignalGetState(inst, mc.ISIG_INPUT7) and "Active" or "Inactive"
if SensorState == "Active" then
currenttool = currenttool + 1
print(string.format("Found Tool No: %.0f", currenttool))
if (currenttool == 7) then
currenttool = 1
end
end
break
end
wx.wxMilliSleep(5000) -- 5 seconds delay
turretstop()
mc.mcToolSetCurrent(inst, currenttool)
--mc.mcCntlGcodeExecuteWait(inst, "G43 H" .. newtool)
end
end
end
-
After further tests i realized that the macro does not work correctly. As soon as I switch e.g. from T0101 to T0303 it looses the counter.
Has someone a clue what I need to change to make the counter work?
-
The script is running now. Getting the signal counter work was a little bit tricky. There is not much documentation or tutorials to find in the WWW.
The machine is an emco PC turn 55 lathe which is upgraded with a smoothstepper.
function m6()
local inst = mc.mcGetInstance()
local handle = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT7) -- Eingang welcher einen Schritt angibt
local currentState = mc.mcSignalGetState(handle)
local oldState = 0
local counter = 0
local maxTool = 6
local count = 0
local currentTool = mc.mcToolGetCurrent(inst) -- jmu korrigiert auf mc.mcToolGetCurrent
local selectedTool = mc.mcToolGetSelected(inst)
-- Lokale Hilfsfunktionen
local function turretRotate()
local handle = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
mc.mcSignalSetState(handle, 1)
mc.mcCntlSetLastError(inst, "Turret started")
end
local function turretStop()
local handle = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
mc.mcSignalSetState(handle, 0)
mc.mcCntlSetLastError(inst, "Turret stopped")
end
-- Ablauf
--Def, wann kein Wechsel
if (selectedTool < 0 or selectedTool > maxTool) then
mc.mcCntlSetLastError(inst, "Tool number out of range")
return "Tool change Error."
elseif selectedTool == currentTool then
mc.mcCntlSetLastError(inst, "Current tool is the same as the selected tool. Nothing to do.")
return "Nothing to do"
else
-- Anzahl Schritte ausrechnen
if (selectedTool > currentTool) then
count = selectedTool - currentTool
elseif (selectedTool < currentTool) then
count = maxTool - currentTool + selectedTool
end
print(string.format("Count: %d", count))
mc.mcCntlSetLastError(inst, "Performing tool change")
turretRotate()
-- Signale zählen
repeat
while (counter <= count) do
wx.wxMilliSleep(100)
currentState = mc.mcSignalGetState(handle)
if (currentState == 1 and oldState == 0) then -- nur Zählen wenn Eingang high!
counter = counter + 1
mc.mcCntlSetLastError(inst, "Counter: " .. counter)
print("Current State:", currentState)
print("Counter:", counter)
end
oldState = currentState
end
until (counter >= count)
wx.wxMilliSleep(2800) --Wartezeit, bis WKZ über dem Rastmechanismus
turretStop()
-- gewähltes Werkzeug speichern
mc.mcToolSetCurrent(inst, selectedTool)
end
end