function m6() local inst = mc.mcGetInstance() local rc = 0 local Alarm = mc.mcSignalGetHandle(inst, mc.OSIG_ALARM) local AlarmState = 0 local RequestedTool = mc.mcToolGetSelected(inst) local CurrentTool = mc.mcToolGetCurrent(inst) local ToolChangePosition = mc.mcCntlGetPoundVar(inst, 500) local ToolChangeAdjustPosition = (ToolChangePosition + .125) local Coolant = mc.mcSignalGetHandle(inst, mc.OSIG_COOLANTON) local Orient = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT23) local CarouselForward = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT20) local CarouselReverse = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT21) local ToolDrawbar= mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT22) local ErrorWaitTime = 12.5 local NextOperationDelay = 500 local DrawbarDelay = 100 local StartCarRoatate = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15) local CarouselHomed = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT9) local CarouselHomedState = mc.mcSignalGetState(CarouselHomed) local CurrentCarouselPosition = mc.mcRegGetHandle(inst,"Click/DS2") local CurrentCarouselPositionValue = mc.mcRegGetValue(CurrentCarouselPosition) local DS1 = mc.mcRegGetHandle(inst,"Click/DS1") mc.mcRegSetValue(DS1,RequestedTool) local CarouselForwardInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1) local CarouselReverseInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2) local DrawBarReleaseSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT3) local CarouselForwardState = mc.mcSignalGetState(CarouselForwardInput) local CarouselReverseState = mc.mcSignalGetState(CarouselReverseInput) local DrawBarReleaseState = mc.mcSignalGetState(DrawBarReleaseSig) ------------------------------------------------------------------------------------------------------------- --Wrong Conditions --If the carousel is forwrad, something is wrong. if CarouselForwardState == 1 then mc.mcCntlSetLastError(inst, "Carousel is already forward.") mc.mcSignalSetState(Alarm, 1) return end --If the drawbar is released, something is wrong. if DrawBarReleaseState == 1 then mc.mcCntlSetLastError(inst, "Tool Drawbar is Released.") mc.mcSignalSetState(Alarm, 1) return end ------------------------------------------------------------------------------------------------------------- --Tool Change Start if CarouselHomedState == 0 then mc.mcCntlSetLastError(inst, "Carousel is not homed, home carousel and try again.") return else if RequestedTool > 20 then mc.mcCntlSetLastError(inst, "Requested tool is out of limit.") else if CurrentTool == 0 then --Turn off spindle and coolant. mc.mcSpindleSetDirection(inst, 0) mc.mcSignalSetState(Coolant, 0) mc.mcSignalSetState(Orient, 0) --Orient the spindle. mc.mcSignalSetState(Orient, 1) --Move the Z Up. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z0.00") --Check the orient signal. rc = mc.mcSignalWait(inst, mc.ISIG_INPUT5, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem with the spindle orientation.") mc.mcSignalSetState(Alarm, 1) return end--Orient Error Check --Start the Carousel Rotation mc.mcSignalSetState(StartCarRoatate, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT7, mc.WAIT_MODE_HIGH, 15) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem rotating the carousel.") mc.mcSignalSetState(Alarm, 1) return end--Wait for the Carousel to rotate --Turn off the Carousel Rotation mc.mcSignalSetState(StartCarRoatate, 0) --Release the tool mc.mcSignalSetState(ToolDrawbar, 1) wx.wxMilliSleep(DrawbarDelay) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT3, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem releasing the tool.") mc.mcSignalSetState(Alarm, 1) return end--Release tool error check. wx.wxMilliSleep(NextOperationDelay) --Move the carousel forward. mc.mcSignalSetState(CarouselReverse, 0) mc.mcSignalSetState(CarouselForward, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT1, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem moving the carousel forward.") mc.mcSignalSetState(Alarm, 1) return end--Carousel Forward error check. wx.wxMilliSleep(NextOperationDelay) --Move the Z to the Tool Change Adjust Position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangeAdjustPosition) wx.wxMilliSleep(NextOperationDelay) --Clamp the tool mc.mcSignalSetState(ToolDrawbar, 0) --Move the Z to the tool change position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangePosition) wx.wxMilliSleep(DrawbarDelay) --rc = mc.mcSignalWait(inst, mc.ISIG_INPUT4, mc.WAIT_MODE_HIGH, ErrorWaitTime) --if rc ~= 0 then -- mc.mcCntlSetLastError(inst, "There was a problem clamping the tool.") -- mc.mcSignalSetState(Alarm, 1) -- return --end--Clamp tool error check. wx.wxMilliSleep(NextOperationDelay) --Move the carousel reverse. mc.mcSignalSetState(CarouselForward, 0) mc.mcSignalSetState(CarouselReverse, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT2, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem moving the carousel back.") mc.mcSignalSetState(Alarm, 1) return end--Carousel reverse error check. --Turn off spindle orient. mc.mcSignalSetState(Orient, 0) --mc.mcSignalSetState(OrientLock, 0) --Final Check. Be sure the machine is ready to run. local CarouselForwardInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1) local CarouselReverseInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2) local DrawBarReleaseSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT21) local CarouselForwardState = mc.mcSignalGetState(CarouselForwardInput) local CarouselReverseState = mc.mcSignalGetState(CarouselReverseInput) local DrawBarReleaseState = mc.mcSignalGetState(DrawBarReleaseSig) --If the carousel is forwrad, something is wrong. if CarouselReverseState == 0 then mc.mcCntlSetLastError(inst, "There is something wrong.") mc.mcSignalSetState(Alarm, 1) return end --If the drawbar is released, something is wrong. if DrawBarReleaseState == 1 then mc.mcCntlSetLastError(inst, "There is something wrong.") mc.mcSignalSetState(Alarm, 1) return end --Check for the Alarm Output. AlarmState = mc.mcSignalGetState(Alarm) if AlarmState == 1 then mc.mcCntlSetLastError(inst, "Could not complet tool change. Check Error Log.") mc.mcCntlEStop() else mc.mcCntlSetLastError(inst, "Tool change complete, current tool is now: T" .. RequestedTool) mc.mcToolSetCurrent(inst, RequestedTool) end--AlarmState == 1 else--Current Tool == 0 ------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------- if CurrentTool ~= CurrentCarouselPositionValue then mc.mcRegSetValue(DS1,CurrentTool) wx.wxMilliSleep(NextOperationDelay) --Start the Carousel Rotation mc.mcCntlSetLastError(inst, "Rotate the carousel to match the current tool.") --return --mc.mcSignalSetState(Alarm, 1) mc.mcSignalSetState(StartCarRoatate, 1) wx.wxMilliSleep(NextOperationDelay) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT7, mc.WAIT_MODE_HIGH, 15) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem rotating the carousel.") mc.mcSignalSetState(Alarm, 1) return end--Wait for the Carousel to rotate --Turn off the Carousel Rotation mc.mcSignalSetState(StartCarRoatate, 0) local DS1 = mc.mcRegGetHandle(inst,"Click/DS1") mc.mcRegSetValue(DS1,RequestedTool) end --Current Carousel Position ~= Current Tool ------------------------------------------------------------------------------------------------------------- if RequestedTool == 0 then --Turn off spindle and coolant. mc.mcSpindleSetDirection(inst, 0) mc.mcSignalSetState(Coolant, 0) mc.mcSignalSetState(Orient, 0) --Orient the spindle. mc.mcSignalSetState(Orient, 1) mc.mcCntlSetLastError(inst, "Spindle Orient") --Move the Z to the Tool Change Position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangePosition) --Check the orient signal. rc = mc.mcSignalWait(inst, mc.ISIG_INPUT5, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem with the spindle orientation.") mc.mcSignalSetState(Alarm, 1) return end--Orient Error Check --Move the carousel forward. mc.mcSignalSetState(CarouselReverse, 0) mc.mcSignalSetState(CarouselForward, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT1, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem moving the carousel forward.") mc.mcSignalSetState(Alarm, 1) return end--Carousel Forward error check. wx.wxMilliSleep(NextOperationDelay) --Release the tool mc.mcSignalSetState(ToolDrawbar, 1) --Move the Z to the Tool Change Adjust Position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangeAdjustPosition) wx.wxMilliSleep(DrawbarDelay) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT3, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem releasing the tool.") mc.mcSignalSetState(Alarm, 1) return end--Release tool error check. --Move the Z Up. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z0.00") wx.wxMilliSleep(NextOperationDelay) --Clamp the tool mc.mcSignalSetState(ToolDrawbar, 0) wx.wxMilliSleep(DrawbarDelay) --rc = mc.mcSignalWait(inst, mc.ISIG_INPUT4, mc.WAIT_MODE_HIGH, ErrorWaitTime) --if rc ~= 0 then -- mc.mcCntlSetLastError(inst, "There was a problem clamping the tool.") -- mc.mcSignalSetState(Alarm, 1) -- return --end--Clamp tool error check. wx.wxMilliSleep(NextOperationDelay) --Move the carousel reverse. mc.mcSignalSetState(CarouselForward, 0) mc.mcSignalSetState(CarouselReverse, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT2, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem moving the carousel back.") mc.mcSignalSetState(Alarm, 1) return end--Carousel reverse error check. --Turn off spindle orient. mc.mcSignalSetState(Orient, 0) --mc.mcSignalSetState(OrientLock, 0) --Final Check. Be sure the machine is ready to run. local CarouselForwardInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1) local CarouselReverseInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2) local DrawBarReleaseSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT21) local CarouselForwardState = mc.mcSignalGetState(CarouselForwardInput) local CarouselReverseState = mc.mcSignalGetState(CarouselReverseInput) local DrawBarReleaseState = mc.mcSignalGetState(DrawBarReleaseSig) --If the carousel is forwrad, something is wrong. if CarouselReverseState == 0 then mc.mcCntlSetLastError(inst, "There is something wrong.") mc.mcSignalSetState(Alarm, 1) return end --If the drawbar is released, something is wrong. if DrawBarReleaseState == 1 then mc.mcCntlSetLastError(inst, "There is something wrong.") mc.mcSignalSetState(Alarm, 1) return end --Check for the Alarm Output. AlarmState = mc.mcSignalGetState(Alarm) if AlarmState == 1 then mc.mcCntlSetLastError(inst, "Could not complet tool change. Check Error Log.") mc.mcCntlEStop() else mc.mcCntlSetLastError(inst, "Tool change complete, current tool is now: T" .. RequestedTool) mc.mcToolSetCurrent(inst, RequestedTool) end--AlarmState == 1 else --Requested == 0 ------------------------------------------------------------------------------------------------------------- if RequestedTool == CurrentTool then mc.mcCntlSetLastError(inst, "The requested tool is already in the spindle.") else --Turn off spindle and coolant. mc.mcSpindleSetDirection(inst, 0) mc.mcSignalSetState(Coolant, 0) mc.mcSignalSetState(Orient, 0) --Reset the DS1 value to the Requested Too2 mc.mcRegSetValue(DS1,RequestedTool) --Orient the spindle. mc.mcSignalSetState(Orient, 1) mc.mcCntlSetLastError(inst, "Spindle Orient") --Move the Z to the Tool Change Position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangePosition) --Check the orient signal. rc = mc.mcSignalWait(inst, mc.ISIG_INPUT5, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem with the spindle orientation.") mc.mcSignalSetState(Alarm, 1) return end--Orient Error Check --Move the carousel forward. mc.mcCntlSetLastError(inst, "Move the carousel forward.") mc.mcSignalSetState(CarouselReverse, 0) mc.mcSignalSetState(CarouselForward, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT1, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem moving the carousel forward.") mc.mcSignalSetState(Alarm, 1) return end--Carousel Forward error check. wx.wxMilliSleep(NextOperationDelay) --Release the tool mc.mcCntlSetLastError(inst, "Release the tool.") mc.mcSignalSetState(ToolDrawbar, 1) --CAB --Move the Z to the Tool Change Adjust Position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangeAdjustPosition) wx.wxMilliSleep(DrawbarDelay) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT3, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem releasing the tool.") mc.mcSignalSetState(Alarm, 1) return end--Release tool error check. --Move the Z Up. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z0.00") wx.wxMilliSleep(NextOperationDelay) --Start the Carousel Rotation mc.mcCntlSetLastError(inst, "Rotate the carousel to the requested tool.") mc.mcSignalSetState(StartCarRoatate, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT7, mc.WAIT_MODE_HIGH, 15) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem rotating the carousel.") mc.mcSignalSetState(Alarm, 1) return end--Wait for the Carousel to rotate --Turn off the Carousel Rotation mc.mcSignalSetState(StartCarRoatate, 0) --Move the Z to the tool change adjust position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangeAdjustPosition) wx.wxMilliSleep(NextOperationDelay) --Clamp the tool mc.mcCntlSetLastError(inst, "Clamp the tool.") mc.mcSignalSetState(ToolDrawbar, 0) --Move the Z to the Tool Change Position. mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G00 Z" .. ToolChangePosition) wx.wxMilliSleep(DrawbarDelay) --rc = mc.mcSignalWait(inst, mc.ISIG_INPUT4, mc.WAIT_MODE_HIGH, ErrorWaitTime) --if rc ~= 0 then -- mc.mcCntlSetLastError(inst, "There was a problem clamping the tool.") -- mc.mcSignalSetState(Alarm, 1) -- return --end--Clamp tool error check. wx.wxMilliSleep(NextOperationDelay) --Move the carousel reverse. mc.mcCntlSetLastError(inst, "Move the carousel back.") mc.mcSignalSetState(CarouselForward, 0) mc.mcSignalSetState(CarouselReverse, 1) rc = mc.mcSignalWait(inst, mc.ISIG_INPUT2, mc.WAIT_MODE_HIGH, ErrorWaitTime) if rc ~= 0 then mc.mcCntlSetLastError(inst, "There was a problem moving the carousel back.") mc.mcSignalSetState(Alarm, 1) return end--Carousel reverse error check. --Turn off spindle orient. mc.mcSignalSetState(Orient, 0) --mc.mcSignalSetState(OrientLock, 0) --Final Check. Be sure the machine is ready to run. local CarouselForwardInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1) local CarouselReverseInput = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2) local DrawBarReleaseSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT21) local CarouselForwardState = mc.mcSignalGetState(CarouselForwardInput) local CarouselReverseState = mc.mcSignalGetState(CarouselReverseInput) local DrawBarReleaseState = mc.mcSignalGetState(DrawBarReleaseSig) --If the carousel is forwrad, something is wrong. if CarouselReverseState == 0 then mc.mcCntlSetLastError(inst, "There is something wrong.") mc.mcSignalSetState(Alarm, 1) return end --If the drawbar is released, something is wrong. if DrawBarReleaseState == 1 then mc.mcCntlSetLastError(inst, "There is something wrong.") mc.mcSignalSetState(Alarm, 1) return end --Check for the Alarm Output. AlarmState = mc.mcSignalGetState(Alarm) if AlarmState == 1 then mc.mcCntlSetLastError(inst, "Could not complet tool change. Check Error Log.") mc.mcCntlEStop() else mc.mcCntlSetLastError(inst, "Tool change complete, current tool is now: T" .. RequestedTool) mc.mcToolSetCurrent(inst, RequestedTool) end--AlarmState == 1 end--RequestedTool == CurrentTool end--Requested == 0 end--Current Tool == 0 end--Requested > 20 end--Carousel Not Homed end--m6 if (mc.mcInEditor() == 1) then m6() end