function m111() local inst = mc.mcGetInstance() --get register handles and values local CamOnReg = mc.mcRegGetHandle(inst, 'iRegs0/CamOnOff') local CamOnVal = mc.mcRegGetValue(CamOnReg) local CamCntReg = mc.mcRegGetHandle(inst, 'iRegs0/CamCount') local CamCntVal = mc.mcRegGetValue(CamCntReg) local CamPixReg = mc.mcRegGetHandle(inst, 'iRegs0/CamPixels') --local CamPixVal = mc.mcRegGetValue(CamPixReg) local CamInReg = mc.mcRegGetHandle(inst, 'iRegs0/CamInches') local CamInVal = mc.mcRegGetValue(CamInReg) --get cam door signal handle local CamDoorH = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3) local CamXH = mc.mcRegGetHandle(inst, 'Camera/x1') local CamYH = mc.mcRegGetHandle(inst, 'Camera/y1') local CamScaleAmtX = mc.mcRegGetHandle(inst, 'iRegs0/XScaleAmount') local CamScaleAmtY = mc.mcRegGetHandle(inst, 'iRegs0/YScaleAmount') local CamRotAmt = mc.mcRegGetHandle(inst, 'iRegs0/Rotation') --calculate pixels per inch -- local InPerPix = (CamInVal / 1024) -- x dimension local InPerPix = (CamInVal / 1280) -- Y dimension mc.mcRegSetValue(CamPixReg, InPerPix) --if camera on do operations if (CamOnVal == 1) then --if count is 0 or 1 first dot if (CamCntVal <= 1) then --open camera door mc.mcSignalSetState(CamDoorH, 1) --get x and y position X1Pos = mc.mcAxisGetPos(inst, 0) Y1Pos = mc.mcAxisGetPos(inst, 1) mc.mcCntlSetLastError(inst, "X1Pos = " .. tostring(X1Pos)) mc.mcCntlSetLastError(inst, "Y1Pos = " .. tostring(Y1Pos)) --get camera info -- local X1DotRaw = 820 -- local Y1DotRaw = 400 mc.mcCntlGcodeExecute(inst, "G4 P1000") local X1DotRaw = mc.mcRegGetValue(CamXH) local Y1DotRaw = mc.mcRegGetValue(CamYH) mc.mcCntlSetLastError(inst, "X1DotRaw = " .. tostring(X1DotRaw)) mc.mcCntlSetLastError(inst, "Y1DotRaw = " .. tostring(Y1DotRaw)) --if camera info is 0 on both x and y exit if (X1DotRaw == 0) and (Y1DotRaw ==0) then wx.wxMessageBox("Did not find dot.") mc.mcRegSetValue(CamCntReg, 0) mc.mcCntlGcodeExecuteWait(inst, "M30") do return end end --calc dot offset --pixels from edge to center of dot X1DotOff = (X1DotRaw - 512) -- Y1DotOff = (640 - Y1DotRaw) Y1DotOff = (Y1DotRaw - 640) X1DotOff = (X1DotOff * InPerPix) Y1DotOff = (Y1DotOff * InPerPix) mc.mcCntlSetLastError(inst, "X1DotOff = " .. tostring(X1DotOff)) mc.mcCntlSetLastError(inst, "Y1DotOff = " .. tostring(Y1DotOff)) --set count to 2 mc.mcRegSetValue(CamCntReg, 2) mc.mcCntlSetLastError(inst, "Found Dot #1") wx.wxMilliSleep(1000) --if count is 2 second dot elseif (CamCntVal == 2) then --open camera door mc.mcSignalSetState(CamDoorH, 1) --get x and y position X2Pos = mc.mcAxisGetPos(inst, 0) Y2Pos = mc.mcAxisGetPos(inst, 1) mc.mcCntlSetLastError(inst, "X2Pos = " .. tostring(X2Pos)) mc.mcCntlSetLastError(inst, "Y2Pos = " .. tostring(Y2Pos)) --get camera info -- local X2DotRaw = 420 -- local Y2DotRaw = 800 mc.mcCntlGcodeExecute(inst, "G4 P1000") local X2DotRaw = mc.mcRegGetValue(CamXH) local Y2DotRaw = mc.mcRegGetValue(CamYH) mc.mcCntlSetLastError(inst, "X2DotRaw = " .. tostring(X2DotRaw)) mc.mcCntlSetLastError(inst, "Y2DotRaw = " .. tostring(Y2DotRaw)) --if camera info is 0 on both x and y exit if (X2DotRaw == 0) and (Y2DotRaw ==0) then wx.wxMessageBox("Did not find dot.") mc.mcRegSetValue(CamCntReg, 0) mc.mcCntlGcodeExecuteWait(inst, "M30") do return end end --calc dot offset --pixels from edge to center of dot X2DotOff = (X2DotRaw - 512) -- Y2DotOff = (640 - Y2DotRaw) Y2DotOff = (Y2DotRaw - 640) X2DotOff = (X2DotOff * InPerPix) Y2DotOff = (Y2DotOff * InPerPix) mc.mcCntlSetLastError(inst, "X2DotOff = " .. tostring(X2DotOff)) mc.mcCntlSetLastError(inst, "Y2DotOff = " .. tostring(Y2DotOff)) --set count to 3 mc.mcRegSetValue(CamCntReg, 3) mc.mcCntlSetLastError(inst, "Found Dot #2") wx.wxMilliSleep(1000) --if count is 3 third dot elseif (CamCntVal == 3) then --open camera door mc.mcSignalSetState(CamDoorH, 1) --get x and y position X3Pos = mc.mcAxisGetPos(inst, 0) Y3Pos = mc.mcAxisGetPos(inst, 1) mc.mcCntlSetLastError(inst, "X3Pos = " .. tostring(X3Pos)) mc.mcCntlSetLastError(inst, "Y3Pos = " .. tostring(Y3Pos)) --get camera info -- local X3DotRaw = 620 -- local Y3DotRaw = 480 mc.mcCntlGcodeExecute(inst, "G4 P1000") local X3DotRaw = mc.mcRegGetValue(CamXH) local Y3DotRaw = mc.mcRegGetValue(CamYH) mc.mcCntlSetLastError(inst, "X3DotRaw = " .. tostring(X3DotRaw)) mc.mcCntlSetLastError(inst, "Y3DotRaw = " .. tostring(Y3DotRaw)) --if camera info is 0 on both x and y exit if (X3DotRaw == 0) and (Y3DotRaw == 0) then wx.wxMessageBox("Did not find dot.") mc.mcRegSetValue(CamCntReg, 0) mc.mcCntlGcodeExecuteWait(inst, "M30") do return end end --calc dot offset --pixels from edge to center of dot X3DotOff = (X3DotRaw - 512) -- Y3DotOff = (640 - Y3DotRaw) Y3DotOff = (Y3DotRaw - 640) X3DotOff = (X3DotOff * InPerPix) Y3DotOff = (Y3DotOff * InPerPix) mc.mcCntlSetLastError(inst, "X3DotOff = " .. tostring(X3DotOff)) mc.mcCntlSetLastError(inst, "Y3DotOff = " .. tostring(Y3DotOff)) --set count to 4 -- mc.mcRegSetValue(CamCntReg, 4) mc.mcCntlSetLastError(inst, "Found Dot #3") -- wx.wxMilliSleep(1000) --close camera door mc.mcSignalSetState(CamDoorH, 0) -- --if count is 4 do calcs and cut file -- elseif (CamCntVal == 4) then -- Do calcs and run file --calculate which dot is which --add x and y for each dot local Dot1XYPos = (X1Pos + Y1Pos) local Dot2XYPos = (X2Pos + Y2Pos) local Dot3XYPos = (X3Pos + Y3Pos) mc.mcCntlSetLastError(inst, "Dot1XYPos = " .. tostring(Dot1XYPos)) mc.mcCntlSetLastError(inst, "Dot2XYPos = " .. tostring(Dot2XYPos)) mc.mcCntlSetLastError(inst, "Dot3XYPos = " .. tostring(Dot3XYPos)) --if dot xypos is smallest set as dot a if (Dot1XYPos <= Dot2XYPos) and (Dot1XYPos <= Dot3XYPos) then XAPos = X1Pos YAPos = Y1Pos XADotOff = X1DotOff YADotOff = Y1DotOff elseif (Dot2XYPos <= Dot1XYPos) and (Dot2XYPos <= Dot3XYPos) then XAPos = X2Pos YAPos = Y2Pos XADotOff = X2DotOff YADotOff = Y2DotOff elseif (Dot3XYPos <= Dot1XYPos) and (Dot3XYPos <= Dot2XYPos) then XAPos = X3Pos YAPos = Y3Pos XADotOff = X3DotOff YADotOff = Y3DotOff end mc.mcCntlSetLastError(inst, "XADotOff = " .. tostring(XADotOff)) mc.mcCntlSetLastError(inst, "YADotOff = " .. tostring(YADotOff)) --if dot xypos is middle set as dot b if ((Dot1XYPos >= Dot2XYPos) and (Dot1XYPos <= Dot3XYPos)) or ((Dot1XYPos <= Dot2XYPos) and (Dot1XYPos >= Dot3XYPos)) then XBPos = X1Pos YBPos = Y1Pos XBDotOff = X1DotOff YBDotOff = Y1DotOff elseif ((Dot2XYPos >= Dot1XYPos) and (Dot2XYPos <= Dot3XYPos)) or ((Dot2XYPos <= Dot1XYPos) and (Dot2XYPos >= Dot3XYPos)) then XBPos = X2Pos YBPos = Y2Pos XBDotOff = X2DotOff YBDotOff = Y2DotOff elseif ((Dot3XYPos >= Dot1XYPos) and (Dot3XYPos <= Dot2XYPos)) or ((Dot3XYPos <= Dot1XYPos) and (Dot3XYPos >= Dot2XYPos)) then XBPos = X3Pos YBPos = Y3Pos XBDotOff = X3DotOff YBDotOff = Y3DotOff end mc.mcCntlSetLastError(inst, "XBDotOff = " .. tostring(XBDotOff)) mc.mcCntlSetLastError(inst, "YBDotOff = " .. tostring(YBDotOff)) --if dot xypos is largest set as dot c if (Dot1XYPos >= Dot2XYPos) and (Dot1XYPos >= Dot3XYPos) then XCPos = X1Pos YCPos = Y1Pos XCDotOff = X1DotOff YCDotOff = Y1DotOff elseif (Dot2XYPos >= Dot1XYPos) and (Dot2XYPos >= Dot3XYPos) then XCPos = X2Pos YCPos = Y2Pos XCDotOff = X2DotOff YCDotOff = Y2DotOff elseif (Dot3XYPos >= Dot1XYPos) and (Dot3XYPos >= Dot2XYPos) then XCPos = X3Pos YCPos = Y3Pos XCDotOff = X3DotOff YCDotOff = Y3DotOff end mc.mcCntlSetLastError(inst, "XCDotOff = " .. tostring(XCDotOff)) mc.mcCntlSetLastError(inst, "YCDotOff = " .. tostring(YCDotOff)) --move to dot a and set proj home on dot XAPosSftd = (XAPos + XADotOff) YAPosSftd = (YAPos + YADotOff) mc.mcCntlGcodeExecuteWait(inst, string.format("G0 X"..XAPosSftd.."Y"..YAPosSftd)) mc.mcAxisSetPos(inst, 0, XAPos) mc.mcAxisSetPos(inst, 1, YAPos) mc.mcCntlSetLastError(inst, "XAPosSftd = " .. tostring(XAPosSftd)) mc.mcCntlSetLastError(inst, "YAPosSftd = " .. tostring(YAPosSftd)) mc.mcCntlSetLastError(inst, "XAPos = " .. tostring(XAPos)) mc.mcCntlSetLastError(inst, "YAPos = " .. tostring(YAPos)) --shift from cam to router --calculate ab scale and rotation --calculate ab g code x length ABGcdXLnth = (XBPos - XAPos) --calculate ab g code y length if (YBPos >= YAPos) then ABGcdYLnth = (YBPos - YAPos) elseif (YBPos <= YAPos) then ABGcdYLnth = (YAPos - YBPos) end mc.mcCntlSetLastError(inst, "ABGcdXLnth = " .. tostring(ABGcdXLnth)) mc.mcCntlSetLastError(inst, "ABGcdYLnth = " .. tostring(ABGcdYLnth)) --g code lenght of ab ABGcdTtlLnth = (math.sqrt((ABGcdXLnth * ABGcdXLnth) + (ABGcdYLnth * ABGcdYLnth))) mc.mcCntlSetLastError(inst, "ABGcdTtlLnth = " .. tostring(ABGcdTtlLnth)) --calculate a and b offset x position XADotPos = (XAPos + XADotOff) XBDotPos = (XBPos + XBDotOff) --calculate a and b offset y position YADotPos = (YAPos + YADotOff) YBDotPos = (YBPos + YBDotOff) --calculate ab offset x length ABOffXLnth = (XBDotPos - XADotPos) --calculate ab offset Y length if (YBDotPos >= YADotPos) then ABOffYLnth = (YBDotPos - YADotPos) elseif (YBDotPos <= YADotPos) then ABOffYLnth = (YADotPos - YBDotPos) end mc.mcCntlSetLastError(inst, "XADotPos = " .. tostring(XADotPos)) mc.mcCntlSetLastError(inst, "XBDotPos = " .. tostring(XBDotPos)) mc.mcCntlSetLastError(inst, "YADotPos = " .. tostring(YADotPos)) mc.mcCntlSetLastError(inst, "YBDotPos = " .. tostring(YBDotPos)) mc.mcCntlSetLastError(inst, "ABOffXLnth = " .. tostring(ABOffXLnth)) mc.mcCntlSetLastError(inst, "ABOffYLnth = " .. tostring(ABOffYLnth)) --offset lenght of ab ABOffTtlLnth = (math.sqrt((ABOffXLnth * ABOffXLnth) + (ABOffYLnth * ABOffYLnth))) mc.mcCntlSetLastError(inst, "ABOffTtlLnth = " .. tostring(ABOffTtlLnth)) --calculate x scale amount ScaleAmountX = (ABOffTtlLnth / ABGcdTtlLnth) --set x scale amount mc.mcAxisSetScale(inst, 0, ScaleAmountX) mc.mcRegSetValue(CamScaleAmtX, ScaleAmountX) mc.mcCntlSetLastError(inst, "ScaleAmountX = " .. tostring(ScaleAmountX)) --calculate bc scale --calculate bc g code x length BCGcdYLnth = (YCPos - YBPos) --calculate bcb g code y length if (XCPos >= XBPos) then BCGcdXLnth = (XCPos - XBPos) elseif (XCPos <= XBPos) then BCGcdXLnth = (XBPos - XCPos) end mc.mcCntlSetLastError(inst, "BCGcdYLnth = " .. tostring(BCGcdYLnth)) mc.mcCntlSetLastError(inst, "BCGcdXLnth = " .. tostring(BCGcdXLnth)) --g code lenght of bc BCGcdTtlLnth = (math.sqrt((BCGcdXLnth * BCGcdXLnth) + (BCGcdYLnth * BCGcdYLnth))) mc.mcCntlSetLastError(inst, "BCGcdTtlLnth = " .. tostring(BCGcdTtlLnth)) --calculate b and c offset x position XBDotPos = (XBPos + XBDotOff) XCDotPos = (XCPos + XCDotOff) --calculate b and c offset y position YBDotPos = (YBPos + YBDotOff) YCDotPos = (YCPos + YCDotOff) --calculate bc offset x length BCOffYLnth = (YCDotPos - YBDotPos) --calculate bc offset Y length if (XCDotPos >= XBDotPos) then BCOffXLnth = (XCDotPos - XBDotPos) elseif (XCDotPos <= XBDotPos) then BCOffXLnth = (XBDotPos - XCDotPos) end mc.mcCntlSetLastError(inst, "XBDotPos = " .. tostring(XBDotPos)) mc.mcCntlSetLastError(inst, "XCDotPos = " .. tostring(XCDotPos)) mc.mcCntlSetLastError(inst, "YBDotPos = " .. tostring(YBDotPos)) mc.mcCntlSetLastError(inst, "YCDotPos = " .. tostring(YCDotPos)) mc.mcCntlSetLastError(inst, "BCOffYLnth = " .. tostring(BCOffYLnth)) mc.mcCntlSetLastError(inst, "BCOffXLnth = " .. tostring(BCOffXLnth)) --offset lenght of ab BCOffTtlLnth = (math.sqrt((BCOffXLnth * BCOffXLnth) + (BCOffYLnth * BCOffYLnth))) mc.mcCntlSetLastError(inst, "BCOffTtlLnth = " .. tostring(BCOffTtlLnth)) --calculate y scale amount ScaleAmountY = (BCOffTtlLnth / BCGcdTtlLnth) --set x scale amount mc.mcAxisSetScale(inst, 1, ScaleAmountY) mc.mcRegSetValue(CamScaleAmtY, ScaleAmountY) mc.mcCntlSetLastError(inst, "ScaleAmountY = " .. tostring(ScaleAmountY)) --calculate rotation and rotate ABGcdRot = math.atan(ABGcdYLnth / ABGcdXLnth) ABOffRot = math.atan(ABOffYLnth / ABOffXLnth) ABRotRad = (ABOffRot - ABGcdRot) ABRotDeg = math.deg(ABRotRad) -- ABRotDeg = (ABRotDeg * -1.0) mc.mcCntlGcodeExecuteWait(inst, string.format("G68 X0.0 Y0.0 R"..tostring(ABRotDeg))) mc.mcRegSetValue(CamRotAmt, ABRotDeg) mc.mcCntlSetLastError(inst, "ABRotDeg = " .. tostring(ABRotDeg)) -- --set count to 5 -- mc.mcRegSetValue(CamCntReg, 5) --set count to 4 mc.mcRegSetValue(CamCntReg, 4) mc.mcCntlSetLastError(inst, "Scale and Rotation complete \nStarting Cut") wx.wxMilliSleep(1000) -- --if count is 5 end of cut reset all -- elseif (CamCntVal == 5) then --if count is 4 end of cut reset all elseif (CamCntVal == 4) then -- mc.mcCntlSetLastError(inst, "Dot 1 X Position "..tostring(X1Pos)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 1 Y Position "..tostring(Y1Pos)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 1 X Offset "..tostring(X1DotOff)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 1 Y Offset "..tostring(Y1DotOff)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 2 X Position "..tostring(X2Pos)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 2 Y Position "..tostring(Y2Pos)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 2 X Offset "..tostring(X2DotOff)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 2 Y Offset "..tostring(Y2DotOff)) -- wx.wxMilliSleep(1000) -- -- mc.mcCntlSetLastError(inst, "Dot 3 X Position "..tostring(X3Pos)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 3 Y Position "..tostring(Y3Pos)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 3 X Offset "..tostring(X3DotOff)) -- wx.wxMilliSleep(1000) -- mc.mcCntlSetLastError(inst, "Dot 3 Y Offset "..tostring(Y3DotOff)) -- wx.wxMilliSleep(1000) --return to 0,0 and remove rotation mc.mcCntlGcodeExecuteWait(inst, "G0 X0 Y0") -- local SftdEndMachPosX = mc.mcAxisGetMachinePos(inst, 0) -- local SftdEndMachPosY = mc.mcAxisGetMachinePos(inst, 1) -- local EndMachPosX = (SftdEndMachPosX + 0.01) -- local EndMachPosY = (SftdEndMachPosY + 0.01) mc.mcCntlGcodeExecuteWait(inst, "G69") mc.mcRegSetValue(CamRotAmt, 0.0) -- mc.mcCntlGcodeExecuteWait(inst, "G53") -- mc.mcCntlGcodeExecuteWait(inst, string.format("G0 X"..tostring(EndMachPosX).." Y"..tostring(EndMachPosY))) mc.mcCntlGcodeExecuteWait(inst, "G54") --remove scaling mc.mcAxisSetScale(inst, 0, 1.0) mc.mcAxisSetScale(inst, 1, 1.0) mc.mcRegSetValue(CamScaleAmtX, 1.0) mc.mcRegSetValue(CamScaleAmtY, 1.0) --set count to 0 mc.mcRegSetValue(CamCntReg, 0) mc.mcCntlSetLastError(inst, "Job complete \nReset complete") wx.wxMilliSleep(1000) end else wx.wxMessageBox("Camera not turned on.") mc.mcCntlGcodeExecuteWait(inst, "M30") end end if (mc.mcInEditor() == 1) then m111() end