Hello Guest it is May 04, 2024, 04:39:48 PM

Author Topic: Umbrella Style M6 Script  (Read 251 times)

0 Members and 1 Guest are viewing this topic.

Umbrella Style M6 Script
« on: March 29, 2024, 05:18:43 PM »
I have an old Milltronics Partner 1 mill with a 6 tool umbrella style tool changer that I'm rebuilding and using Mach4 with a Hicon Integra controller.  I haven't been able to find a m6 script for this style so I have attempted to modify the tool change script that is on CNC4PC.  Can you guys that actually know how to program LUA check my code and let me know if it looks correct or what I should modify?  I'm using a stepper to rotate the umbrella and calling it C axis.  It has a home sensor and also a sensor for each tool position, but I'm not sure if I really need the tool position sensor.  I didn't use it in this code.  Once I finish getting the mill running I'll modify the I/O if I need to and set the correct tool change position.

Thanks!

Mike

Code: [Select]
----------Variables----------
local inst = mc.mcGetInstance()
local DrawBarOut = mc.OSIG_OUTPUT2 -------- Release drawbar ------
local BlowOut = mc.OSIG_OUTPUT3 -------- Tool blow ------
local UmbrellaIn = mc.OSIG_OUTPUT5 -------- Swing Umbralla In ------
local UmbrellaOutSensor = mc.ISIG_Input20 -------- Umbralla Out Sensor ------
local UmbrellaInSensor = mc.ISIG_Input21 -------- Umbralla In Sensor ------
local numtools = 6

local ToolXPositions = {}
ToolCPositions[1] = 0.0
ToolCPositions[2] = 60.
ToolCPositions[3] = 120.
ToolCPositions[4] = 180.
ToolCPositions[5] = 240.
ToolCPositions[6] = 300.


function m6()
local GCode = ""
GCode = GCode .. string.format("M9\n") --Coolant Off
    GCode = GCode .. string.format("M5\n") --Stop Spindle
GCode = GCode .. string.format("G04 P500\n")
mc.mcCntlGcodeExecuteWait(inst, GCode)

------ compare current and next tools ------
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)
if (SelectedTool == CurrentTool) then
mc.mcCntlSetLastError(inst, "Next tool = Current tool")
do return end
end

if (SelectedTool > numtools) or (SelectedTool <= 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Tool number out of range!")
do return end
else
end

------ Get current machine state ------
local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)

------ Get Position data for current tool ------
Num1 = CurrentTool
CPos1 = ToolCPositions[CurrentTool]

------ Get position data for next tool ------
Num2 = SelectedTool
CPos2 = ToolCPositions[SelectedTool]

------ Move to tool change position ------
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=Tool change position
GCode = GCode .. string.format("G00 G90 G53 C%.4f\n", CPos1) --Current tool Umbrella position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to get tool in spindle
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)

if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

------ Unclamp tool ------
hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Move to tool change Clearance position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z0\n" --Z=Tool change clearance position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 1) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--turn on tool blowoff nozzles
hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 1)

--Rotate Umbrella to next tool
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 C%.4f", CPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)

--turn off tool blowoff nozzles
local hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 0)

--swing umbrella to tool change position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--lower Z to tool change position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=tool change position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Clamp tool
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 0)
GCode = ""
GCode = GCode .. "G04 P1000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 1) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end
end
------ Reset state ------
mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)

------ Set new tool ------
mc.mcToolSetCurrent (inst, SelectedTool)
mc.mcCntlSetLastError(inst, string.format("Tool change - Tool: %.0f", SelectedTool))

mc.mcCntlGcodeExecuteWait(inst, "G43 H"..SelectedTool)

end

if (mc.mcInEditor() == 1) then
m6()
end

Re: Umbrella Style M6 Script
« Reply #1 on: March 30, 2024, 07:44:48 PM »
I completely forgot about the debug function! lol.  I ran it and found a couple of little things to fix but now I'm getting this error and I'm not sure why?
LuaExamples/m6.mcs:175: wxLua: Expected a 'number' for parameter 3, but got a 'nil'.
Function called: 'mcCntlSetPoundVar(number, number, nil)'
The script I modified from debugs with no issues even though I didn't change those sections?

Updated code:

Code: [Select]
----------Variables----------
local inst = mc.mcGetInstance()
local DrawBarOut = mc.OSIG_OUTPUT2 -------- Release drawbar ------
local BlowOut = mc.OSIG_OUTPUT3 -------- Tool blow ------
local UmbrellaIn = mc.OSIG_OUTPUT5 -------- Swing Umbralla In ------
local UmbrellaOutSensor = mc.ISIG_Input20 -------- Umbralla Out Sensor ------
local UmbrellaInSensor = mc.ISIG_Input21 -------- Umbralla In Sensor ------
local numtools = 6

local ToolCPositions = {}
ToolCPositions[1] = 0.0
ToolCPositions[2] = 60.
ToolCPositions[3] = 120.
ToolCPositions[4] = 180.
ToolCPositions[5] = 240.
ToolCPositions[6] = 300.


function m6()
local GCode = ""
GCode = GCode .. string.format("M9\n") --Coolant Off
    GCode = GCode .. string.format("M5\n") --Stop Spindle
GCode = GCode .. string.format("G04 P500\n")
mc.mcCntlGcodeExecuteWait(inst, GCode)

------ compare current and next tools ------
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)
if (SelectedTool == CurrentTool) then
mc.mcCntlSetLastError(inst, "Next tool = Current tool")
do return end
end

if (SelectedTool > numtools) or (SelectedTool <= 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Tool number out of range!")
do return end
else
end

------ Get current machine state ------
local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)

------ Get Position data for current tool ------
Num1 = CurrentTool
CPos1 = ToolCPositions[CurrentTool]

------ Get position data for next tool ------
Num2 = SelectedTool
CPos2 = ToolCPositions[SelectedTool]

------ Move to tool change position ------
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=Tool change position
GCode = GCode .. string.format("G00 G90 G53 C%.4f\n", CPos1) --Current tool Umbrella position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to get tool in spindle
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)

if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

------ Unclamp tool ------
hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Move to tool change Clearance position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z0\n" --Z=Tool change clearance position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 1) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--turn on tool blowoff nozzles
hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 1)

--Rotate Umbrella to next tool
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 C%.4f", CPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)

--turn off tool blowoff nozzles
local hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 0)

--swing umbrella to tool change position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--lower Z to tool change position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=tool change position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Clamp tool
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 0)
GCode = ""
GCode = GCode .. "G04 P1000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 1) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end
end
------ Reset state ------
mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)

------ Set new tool ------
mc.mcToolSetCurrent (inst, SelectedTool)
mc.mcCntlSetLastError(inst, string.format("Tool change - Tool: %.0f", SelectedTool))

mc.mcCntlGcodeExecuteWait(inst, "G43 H"..SelectedTool)

if (mc.mcInEditor() == 1) then
m6()
end

Re: Umbrella Style M6 Script
« Reply #2 on: March 31, 2024, 01:22:08 PM »
when in debug did you check the values sent and received and rc's returned of the mcCtntlGetPoundvar and mcCtntlSetPoundvar?
Re: Umbrella Style M6 Script
« Reply #3 on: March 31, 2024, 02:23:21 PM »
How do I check the sent and received info?  I don't see an option for that?
Also, I did find my error, I had an end in the wrong place.  once I moved it to the correct position it runs correctly now.  Looks like I just need to get everything back together on my machine and try it out!

Corrected code:
Code: [Select]
----------Variables----------
local inst = mc.mcGetInstance()
local DrawBarOut = mc.OSIG_OUTPUT2 -------- Release drawbar ------
local BlowOut = mc.OSIG_OUTPUT3 -------- Tool blow ------
local UmbrellaIn = mc.OSIG_OUTPUT5 -------- Swing Umbralla In ------
local UmbrellaOutSensor = mc.ISIG_Input20 -------- Umbralla Out Sensor ------
local UmbrellaInSensor = mc.ISIG_Input21 -------- Umbralla In Sensor ------
local numtools = 6

local ToolCPositions = {}
ToolCPositions[1] = 0.0
ToolCPositions[2] = 60.
ToolCPositions[3] = 120.
ToolCPositions[4] = 180.
ToolCPositions[5] = 240.
ToolCPositions[6] = 300.


function m6()
local GCode = ""
GCode = GCode .. string.format("M9\n") --Coolant Off
    GCode = GCode .. string.format("M5\n") --Stop Spindle
GCode = GCode .. string.format("G04 P500\n")
mc.mcCntlGcodeExecuteWait(inst, GCode)

------ compare current and next tools ------
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)
if (SelectedTool == CurrentTool) then
mc.mcCntlSetLastError(inst, "Next tool = Current tool")
do return end
end

if (SelectedTool > numtools) or (SelectedTool <= 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Tool number out of range!")
do return end
else
end

------ Get current machine state ------
local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)

------ Get Position data for current tool ------
Num1 = CurrentTool
CPos1 = ToolCPositions[CurrentTool]

------ Get position data for next tool ------
Num2 = SelectedTool
CPos2 = ToolCPositions[SelectedTool]

------ Move to tool change position ------
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=Tool change position
GCode = GCode .. string.format("G00 G90 G53 C%.4f\n", CPos1) --Current tool Umbrella position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to get tool in spindle
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)

if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

------ Unclamp tool ------
hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Move to tool change Clearance position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z0\n" --Z=Tool change clearance position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 1) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--turn on tool blowoff nozzles
hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 1)

--Rotate Umbrella to next tool
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 C%.4f", CPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)

--turn off tool blowoff nozzles
local hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 0)

--swing umbrella to tool change position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--lower Z to tool change position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=tool change position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Clamp tool
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 0)
GCode = ""
GCode = GCode .. "G04 P1000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)
mc.mcSignalSetState(hsig1,0)
mc.mcSignalSetState(hsig2,0)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 1) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

------ Reset state ------
mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)

------ Set new tool ------
mc.mcToolSetCurrent (inst, SelectedTool)
mc.mcCntlSetLastError(inst, string.format("Tool change - Tool: %.0f", SelectedTool))

mc.mcCntlGcodeExecuteWait(inst, "G43 H"..SelectedTool)
end

if (mc.mcInEditor() == 1) then
m6()
end

Re: Umbrella Style M6 Script
« Reply #4 on: March 31, 2024, 02:45:14 PM »
Run the debugger in the single step mode
local rc
local SelectedTool
rc, SelectedTool = mc.mcToolGetSelected(inst)
hover over the rc and SelectedTool and you will see the value

Same for the  get/set poundvar and any other function call.
HTH

RT
Re: Umbrella Style M6 Script
« Reply #5 on: March 31, 2024, 09:41:13 PM »
Thanks rhtuttle!  That helped a lot!

I decided to modify the code in case I need to run programs with more than 6 tools so that I can manually change any tool over 6. Debugging was kind of a pain because I had to force I/O every time it was looking for something to be in a certain state.  Once I figured that out I was able to debug the program all the way thru, with less than 6 tools and more than 6.

Here's the updated and debugged code.  Hopefully it works, I'll find out in a while after I finish putting my machine back together.

Code: [Select]
----------Variables----------
local inst = mc.mcGetInstance()
local DrawBarOut = mc.OSIG_OUTPUT2 -------- Release drawbar ------
local BlowOut = mc.OSIG_OUTPUT3 -------- Tool blow ------
local UmbrellaIn = mc.OSIG_OUTPUT5 -------- Swing Umbralla In ------
local UmbrellaOutSensor = mc.ISIG_Input20 -------- Umbralla Out Sensor ------
local UmbrellaInSensor = mc.ISIG_Input21 -------- Umbralla In Sensor ------
local numtools = 6

local ToolCPositions = {}
ToolCPositions[1] = 0.0
ToolCPositions[2] = 60.
ToolCPositions[3] = 120.
ToolCPositions[4] = 180.
ToolCPositions[5] = 240.
ToolCPositions[6] = 300.


function m6()
local GCode = ""
GCode = GCode .. string.format("M9\n") --Coolant Off
    GCode = GCode .. string.format("M5\n") --Stop Spindle
GCode = GCode .. string.format("G04 P500\n")
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=Tool change position
mc.mcCntlGcodeExecuteWait(inst, GCode)

------ compare current and next tools ------
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)
if (SelectedTool == CurrentTool) then
mc.mcCntlSetLastError(inst, "Next tool = Current tool")
do return end
end

if (SelectedTool <= 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Tool number out of range!")
do return end
else
end

------ Get current machine state ------
local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)

if (SelectedTool > numtools) then
mc.mcCntlSetLastError(inst, "Change to tool " .. tostring(selectedTool) .. " and press start to continue") --Message at beginning of tool change
mc.mcCntlToolChangeManual(inst, true) --This will pause the tool change here and wait for a press of cycle start to continue

------ Reset state ------
mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)

------ Set new tool ------
mc.mcToolSetCurrent (inst, SelectedTool)
mc.mcCntlSetLastError(inst, string.format("Tool change complete - Tool: %.0f", SelectedTool))
mc.mcCntlGcodeExecuteWait(inst, "G43 H"..SelectedTool)

do return end
else
end

------ Get Position data for current tool ------
Num1 = CurrentTool
CPos1 = ToolCPositions[CurrentTool]

------ Get position data for next tool ------
Num2 = SelectedTool
CPos2 = ToolCPositions[SelectedTool]

------ Move to tool change position ------
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=Tool change position
GCode = GCode .. string.format("G00 G90 G53 C%.4f\n", CPos1) --Current tool Umbrella position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to get tool in spindle
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)  --Make sure hsig is off
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

------ Unclamp tool ------
hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Move to tool change Clearance position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z0\n" --Z=Tool change clearance position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)  --Make sure hsig is off
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--turn on tool blowoff nozzles
hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 1)

--Rotate Umbrella to next tool
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 C%.4f", CPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)

--turn off tool blowoff nozzles
local hsig1 = mc.mcSignalGetHandle(inst, BlowOut)
mc.mcSignalSetState(hsig1, 0)

--swing umbrella to tool change position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 1)

--Verify Umbrella is In
mc.mcSignalSetState(hsig,0)  --Make sure hsig is off
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaInSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

--lower Z to tool change position
GCode = ""
GCode = GCode .. "G00 G90 G53 Z-2.0\n" --Z=tool change position
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Clamp tool
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 0)
GCode = ""
GCode = GCode .. "G04 P1000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)

--Swing umbrella to standby position
hsig1 = mc.mcSignalGetHandle(inst, UmbrellaIn)
mc.mcSignalSetState(hsig1, 0)

--Verify Umbrella is Out
mc.mcSignalSetState(hsig,0)  --Make sure hsig is off
GCode = ""
GCode = GCode .. "G04 P2000\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
hSig, rc = mc.mcSignalGetHandle(inst, UmbrellaOutSensor)
hReg, rc = mc.mcSignalGetState(hSig)
if (hReg == 0) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Umbrella Not In Position!")
do return end
end

------ Reset state ------
mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)

------ Set new tool ------
mc.mcToolSetCurrent (inst, SelectedTool)
mc.mcCntlSetLastError(inst, string.format("Tool change complete - Tool: %.0f", SelectedTool))

mc.mcCntlGcodeExecuteWait(inst, "G43 H"..SelectedTool)
end

if (mc.mcInEditor() == 1) then
m6()
end