' 注意:
' 1: 注释的格式 单引号表示注释到行尾
' 2: 编辑本文件时请采用4位制表符(TAB)，否则可能注释无法对齐。
' 3: 修改时尽量进行详细的注释
'
' 子程序编写规定
' 0~999 系统级子程序，仅供系统使用，用户只可修改参数或进行调用，不可使用这些程序号自行编写子程序
' 其中:
' 0~199 由Ncstudio主程序使用的子程序
' 200~999 由解析引擎内部使用的子程序
' 该类子程序每个程序号代表一个固定的含义，编写者须在该程序前用注释标明。
'
' 1000~9999 用户子程序，用户可以自由编写，自由调用
' M1000~9999 与' 1000~9999 用户子程一一对应

' 固定参数从#-1 到 #-64，其使用情况为：
' #-1(Q63) 单位mm, 对刀块厚度
' #-2(Q62) 单位mm, 固定对刀块与刀尖接触时的工件坐标
' #-3(Q61) 单位毫秒, 主轴启动(停止)延时
' #-4(Q60)
' #-5(Q59) 逻辑值, 是否自动回换刀位
' #-6(Q58) 单位mm, 换刀位X机械坐标
' #-7(Q57) 单位mm, 换刀位Y机械坐标
' #-8(Q56) 单位mm, 换刀位Z机械坐标
' #-9(Q55) 单位mm, 固定对刀块X机械坐标
' #-10(Q54) 单位mm, 固定对刀块Y机械坐标
' #-11(Q53) 单位mm, 浮动对刀块与刀尖接触时的机械坐标
' #-12(Q52) 单位mm, 短线段界限，小于此长度的线段认为是短线段
' #-13(Q51) 单位mm, 测量刀具长度时工件与刀尖接触时的机械坐标
' #-14(Q50) 单位deg, 细节加工识别角度
' #-15(Q49) 单位ms, 急转弯延时时间
' #-16(Q48) 单位ms, 轮廓加工延时时间
' #-17(Q47) 单位mm/min, 细节加工连接速度
' #-18(Q46) 单位mm, 固定对刀快速下刀结束位置（Z轴机械坐标）
' #-19(Q45) 安全高度暂存
' #-20(Q44) 工作模式暂存
' #-22(Q42) X轴机械坐标暂存(粗定位阶段)
' #-23(Q41) X轴机械坐标暂存(精定位阶段)
' #-24(Q40) X轴粗定位信号和精定位信号之间的距离
' #-25(Q39) Y轴机械坐标暂存(粗定位阶段)
' #-26(Q38) Y轴机械坐标暂存(精定位阶段)
' #-27(Q37) Y轴粗定位信号和精定位信号之间的距离
' #-28(Q36) Z轴机械坐标暂存(粗定位阶段)
' #-29(Q35) Z轴机械坐标暂存(精定位阶段)
' #-30(Q34) Z轴粗定位信号和精定位信号之间的距离
' #-39(Q25) X轴回退距离
' #-40(Q24) Y轴回退距离
' #-41(Q23) Z轴回退距离
' #-42(Q22) 加工完成后主轴回到固定点的X坐标
' #-43(Q21) 加工完成后主轴回到固定点的Y坐标
' #-44(Q20) 加工完成后主轴回到固定点的Z坐标
' #-31(Q33) ~ -64(Q0) Reserverd for future use
' #-46(Q18) 对刀后抬刀高度
'
'0~199 for Ncstudio
'
'200~999 由解析引擎内部使用的子程序
'	200~599 为M指令使用代码 :+200
'	600~999 为G指令使用代码 :+600
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Note:
' 1: The format of a comment: a single quotation in front indicates it’s a comment to the end of line.  
' 2: 8-bit “Tab” recommend to be used for the alignment when editing the file. 
' 3: The comments should be easy to understand. 
'
' Compiling rules for subprogram
' Subprograms NO.0-999 are only valid for System. 
'	Users are allowed to adjust parameters and call these subprograms but forbidden to write subprograms with NO.0~999.
' Subprograms NO.0-199 are confined to utility in Ncstudio main program.
' Subprograms NO.200-999 are for Interpreter.
' Each NO represents a functional subprogram, please give detailed comments when programming new one.
'
' Subprograms NO.1000-9999 are user’s subprograms which can be compiled and called freely by users.
' Commands M1000-9999 are exactly corresponding to subprograms NO.10000-9999.

' Static/constant parameters in this system vary from #-1 to #-64, whose descriptions are as follows:
' #-1(Q63) unit:mm	Thickness of touch probe
' #-2(Q62) unit:mm  Coordinate (Z) of cutter tip in WCS when touching fixed touch probe 
' #-3(Q61) unit:ms  Delay of spindle ON (or OFF)
' #-4(Q60)
' #-5(Q59) unit:logicals  Whether automatically back to tool change position
' #-6(Q58) unit:mm Coordinate(X) of tool change position
' #-7(Q57) unit:mm Coordinate(Y) of tool change position
' #-8(Q56) unit:mm Coordinate(Z) of tool change position
' #-9(Q55) unit:mm Coordinate(X) of fixed tool touch probe
' #-10(Q54) unit:mm Coordinate(Y) of fixed tool touch probe
' #-11(Q53) unit:mm Coordinate (Z) of cutter tip in MCS when touching mobile tool touch probe
' #-12(Q52) unit:mm Maximum length of Short Line, line segment shorter are described as Short Line
' #-13(Q51) unit:mm Coordinate (Z) of cutter tip in MCS when touching work piece during measuring tool length
' #-14(Q50) unit:deg Identify angles in minutia machining
' #-15(Q49) unit: ms Delay in rapid turn
' #-16(Q48) unit: ms Delay in contouring
' #-17(Q47) unit:mm/min Connection speed in minutia machining
' #-18(Q46) unit: mm Coordinate (Z) in MCS where spindle stops rapid motion and slowly moves to fixed tool touch probe
' #-19(Q45) Temporal save for SAFETY HEIGHT
' #-20(Q44) Temporal save for MACHINING MODE
' #-22(Q42) Temporal save for coordinate(X) during rough positioning
' #-23(Q41) Temporal save for coordinate(X) during fine positioning
' #-24(Q40) Distance between signals of rough positioning and fine positioning (X-axis)
' #-25(Q39) Temporal save for coordinate(Y) during rough positioning 
' #-26(Q38) Temporal save for coordinate(Y) during fine positioning
' #-27(Q37) Distance between signals of rough positioning and fine positioning (Y-axis)
' #-28(Q36) Temporal save for coordinate (Z) during rough positioning 
' #-29(Q35) Temporal save for coordinate (Z) during fine positioning
' #-30(Q34) Distance between signals of rough positioning and fine positioning (Z-axis)
' #-39(Q25) Distance retracted (X-axis)
' #-40(Q24) Distance retracted (Y-axis)
' #-41(Q23) Distance retracted (Z-axis)
' #-42(Q22) Coordinate(X) of fixed point spindle back to after finishing machining
' #-43(Q21) Coordinate(Y) of fixed point spindle back to after finishing machining
' #-44(Q20) Coordinate(Z) of fixed point spindle back to after finishing machining
' #-31(Q33) ~ -64(Q0) Reserved for future use
' #-46(Q18) Tool retract height after tool touch
'
'	0~199 for Ncstudio
'
' 200-599 are code numbers for M command: +200
'	600-599 are code numbers for G command: +600
'	Details: NO.[subprogram]=NO.[command M]+200
' E.g.：M06 is a universal M code, M206 (206=6+200) is a code used in subprogram.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'系统启动自动执行
'System Start automated execution
O11
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'回工件原点
'Back to WCS zero
O12
	'参数：#-19 -- 安全高度
	'Parameter: #-19 -- Safe Height
	' #-20 -- 加工模式
	' #-20 -- machining mode
	'运动到工件坐标原点上方安全高度处
	'Traverse to Safe Height above the Work piece Zero
	G00 G90 X0 Y0 Z=#-19
	'据洛克反映：F7回原点问题（有时原点数据丢失，有时偏位，偶然发生）
	'老叶说用新电脑2.8G就会有问题，现在他们给客户换成旧电脑，很长时间没有问题了
	'所以增加下面代码试图解决此问题。 --zzk 
	G04 P100
	G906
	G04 P100
	'恢复加工模式
	'Resume the machining mode
	M802 P=#-20
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'程序文件自动执行序言代码
'Cycle Start automated execution
'参数：#1 -- 设定主轴转速
'Parameter: #1 –- set spindle speed
O14
	'同步
	'Synchronization
	G906	
	'M3; 
	'开启主轴冷却
	'Spindle Coolant on     
	M901 H6 P1;          
	M801 ""
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'程序文件自动执行结束代码
'Cycle Stop automated execution
O15
	'同步
	'Synchronization
	G906
	IF(#STOPSPINWHENRECYCLE && #ONRECYCLE)M5;  
	IF(!#ONRECYCLE && #STOPSPINEND)M5; 
	'关闭主轴冷却
	'Spindle Coolant off
	M901 H6 P0;      
	M801 ""
	IF(#AXISENDACTION = 1) M801 "|D|spindle back to fixed point";  G53 G00 G90 X=#-42 Y=#-43 Z=#-44
	IF(#AXISENDACTION = 2) M801 "|D|Spindle back to Work piece Zero";   G00 G90 X=0 Y=0 Z=#-19
	'回到换刀位
	'Back to tool change position
	IF(#-5) G53 G00 G90 X=#-6 Y=#-7 Z=#-8
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'浮动对刀: 对准工件表面
'Tool measurement with mobile tool touch probe: to work piece surface
O13
	'同步
	'Synchronization
	G906
	'主轴停止
	'Spindle OFF
	M5
	M801 ""
	
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	G04 P10
	'关闭缓冲区
	'Buffer zone off	
	M802 P196609
	G04 P10
	'刀尖向下运动直到接触到对刀块表面
	'Cutter moves downward till touching tool touch probe
	G904 Z-400 K15 H1
 	G904 Z200  K15 H0
	G904 Z-60  K15 H1
	G04 P10
	G906
	#1=#SSCURPOS2
	G904 Z100 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#1=(#SSCURPOS2+#1)
	G904 Z60  K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#1=(#SSCURPOS2+#1)/3
	'开启缓冲区
	'Buffer zone on	
	M802 P196608   
	G04 P10
	G53 G00 Z=#1
	'对刀后，刀尖向上运动一高度，以保护刀尖
	'After tool touching, cutter moves certain distance upward to protect the tip
	G905 G903 G00 G91 Z=#-46 F2000
	'同步
	'Synchronization
	G906
	'设置当前点Z方向工件坐标, #-1 是对刀块厚度
	'Set Current point Z coordinate in WCS, #-1 is the thickness of the tool touch probe 
	G92 Z=#-1 + #-46
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'测量工作台表面
'To measure work plane surface
O18
	'同步
	'Synchronization
	G906
	'主轴停止
	'Spindle OFF
	M5	
	M801 ""
	
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	G04 P10
	'关闭缓冲区
	'Buffer zone off	
	M802 P196609
	G04 P10
	'刀尖向下运动直到接触到对刀块表面
	'Cutter moves downward till touching surface of tool touch probe
	G904 Z-400 K15 H1
	G904 Z200 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#1=#SSCURPOS2
	G904 Z100 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#1=(#SSCURPOS2+#1)
	G904 Z60 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#1=(#SSCURPOS2+#1)/3
	G04 P10
	'开启缓冲区
	'Buffer zone on
	M802 P196608		
	G04 P10
	'对刀后，刀尖向上运动一高度，以保护刀尖
	'After tool touching, cutter moves certain distance upward to protect the tip
	G903 G00 G91 Z=#-46
	'同步
	'Synchronization
	G906
	'设置工作台表面软限位
	'Set software limits of work plane surface
	#1=#1-(#-1)
	M801 "func//:_OnSetLimitLowerZ"
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定对刀：校正刀杆长度之第一步
'Tool measurement with fixed touch probe: the first step getting the standard
O16
	'同步
	'Synchronization	
	G906
	M801 ""
		
	'Z轴上升到顶端
	'Z-axis moves to top
	G53 G00 G90 Z-1
	M5
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	'固定对刀块的机械坐标
	'MCS coordinate of fixed tool touch probe
	G53 G00 G90 X=#-9 Y=#-10 Z=#-18
	G04 P10
	'关闭缓冲区
	'Buffer zone off	
	M802 P196609
	G04 P10
	'刀尖向下运动直到接触到对刀块表面
	'Cutter moves downward till touching surface of tool touch probe
	G904 Z-400 K15 H1
	G904 Z200 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#-2=#SSCURWORKCOOR2
	G904 Z100 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#-2=(#SSCURWORKCOOR2+#-2)
	G904 Z60 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#-2=(#SSCURWORKCOOR2+#-2)/3
	G04 P10
	'开启缓冲区
	'Buffer zone on
	M802 P196608
	G04 P10
	'为了避免刀尖一直接触到对刀块，向上运动到一高度
	'After tool touching, cutter moves certain distance upward to protect the tip	
	G00 G91 Z=#-46
	G906
	G04 P100
	'记录当前点Z轴工件坐标
	'Save coordinate(Z) in WCS	
	#-2=#-2+ #-46
	'Z轴上升到顶端
	'Z-axis moves to top
	G53 G00 G90 Z-1
	'回到工件原点上方
	'Back to zero in WCS
	'G00 G90 X0Y0
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定对刀：校正刀杆长度之第二步
'Tool measurement with fixed touch probe: the second step measuring changed tool
O17
	'同步
	'Synchronization	
	G906
	M801 ""
	
	'Z轴上升到顶端
	'Z-axis moves to top
	G53 G00 G90 Z-1
	M5
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	'固定对刀块的机械坐标
	'MCS coordinate of fixed tool touch probe	
	G53 G00 G90 X=#-9 Y=#-10 Z=#-18		
	G04 P10
	'关闭缓冲区
	'Buffer zone off	
	M802 P196609		
	G04 P10
	'刀尖向下运动直到接触到对刀块表面
	'Cutter moves downward till touching surface of tool touch probe
	G904 Z-400 K15 H1
	G904 Z200 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#30 = #SSCURWORKCOOR2
	G904 Z100 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#30 = #SSCURWORKCOOR2 + #30
	G904 Z100 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#30 = #SSCURWORKCOOR2 + #30
	#30 = #30 / 3
	'开启缓冲区
	'Buffer zone on
	M802 P196608		
	G04 P10
	G00 Z=#30
	'为了避免刀尖一直接触到对刀块，向上运动到一高度
	'After tool touching, cutter moves certain distance upward to protect the tip	
	G00 G91 Z=#-46
	G04 P500
	G906
	'恢复当前点Z轴工件坐标
	'resume coordinate(Z) in WCS
	G92 Z=#-2		
	G906
	'Z轴上升到顶端
	'Z-axis moves to top
	G53 G00 G90 Z-1
	'回到工件原点上方
	'Back to zero in WCS	
	'G00 G90 X0 Y0		
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'测量刀具长度：使用浮动对刀子程序，将对刀结果存入刀具偏置
'To measure tool length: call subprogram” Measuring with mobile touch probe” 
'and save the result to “TOOL OFFSET” in tool management list. 
O20
	'同步
	'Synchronization
	G906
	'主轴停止
	'Spindle OFF
	
	'Z轴上升到顶端
	'Z-axis moves to top
	G53 G00 G90 Z-1
	M5
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	M801 ""
	G04 P10
	'关闭缓冲区
	'Buffer zone off
	M802 P196609		
	G04 P10
	'刀尖向下运动直到接触到对刀块表面
	'Cutter moves downward till touching surface of tool touch probe
	G904 Z-400 K15 H1
	G904 Z200 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#-13=#SSCURPOS2
	G904 Z100 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#-13=(#SSCURPOS2+#-13)
	G904 Z60 K15 H0
	G904 Z-60 K15 H1
	G04 P10
	G906
	#-13=(#SSCURPOS2+#-13)/3
	G04 P10
	'开启缓冲区
	'Buffer zone on
	M802 P196608	
	G04 P10
	'对刀结束后，刀尖向上运动到达一高度，以保护刀尖
	'After tool touching, cutter moves certain distance upward to protect the tip	
	G905 G903 G00 G91 Z=#-46 F2000
	G90
	'同步
	'Synchronization	
	G906
	'直接将对刀仪下表面机械坐标存入刀具偏置
	'Save coordinate in MCS of touch probe lower surface into “TOOLOFFSET”
	'下表面机械坐标 = 当前机械坐标 - 对刀块厚度
	'coordinate in MCS of touch probe lower surface = current coordinate in MCS - touch probe thickness
	#-13=#-13 - #-1
	'设置当前刀具坐标偏置
	'Set offset of current tool
	G923 Z=#-13		
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'手动测量
O21
	G906
	G923 Z=#SSCURPOS2;
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'单独测量
O22
	IF(#TOOLINDEX > #TOOLLIBSIZE)M801 "|D|Illegal tool NO."; M30
	M5
	
	
	'Z轴上升到顶端
	'Z-axis moves to top
	G53 G00 G90 Z-1
	M5
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	
	'移动到固定对刀仪位置
	G905 G903 G53 G00 G90 X=#-9 Y=#-10 Z=#-18

	'对刀
	G65 P20 L1
	
	M801 "|D|Auto set tool length successfully！";
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'#CTOFFSETx 当前刀具号刀具偏置

'全部测量
O23
	IF(#TOOLINDEX > #TOOLLIBSIZE)M801 "|D|Illegal tool NO.!"; M30

	#30 =#TOOLINDEX
	G65 P231 L=#TOOLLIBSIZE
	M801 "|D|Auto set all tool length successfully！";
M17	
'''''''''''''''''''''''''''''''''''''''''
O231
  IF(#30 > #TOOLLIBSIZE)#30=1
  '换刀
	IF(!(#30=#TOOLINDEX))T=#30
	'更新
	#30=#TOOLINDEX + 1
	
	'Z轴上升到顶端
	'Z-axis moves to top
	G53 G00 G90 Z-1
	M5
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	'移动到固定对刀仪位置
	G905 G903 G53 G00 G90 X=#-9 Y=#-10 Z=#-18
	
	'对刀
	G65 P20 L1	
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'全部回机械原点
'All axes back to REF
O1
	'Z轴回机械原点
	'Z axis back to REF
	G65 P103 L1
	'XY轴同时回机械原点
	'X\Y axes back to REF simultaneously
	G65 P104 L1
	M801 ""
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'X轴单独回机械原点（参考点）
'X-axis back to REF
O101
'粗定位阶段
'Rough positioning
	'屏蔽限位
	'Shield limit signal
	M802 P131072
	'X轴负向运动，直到X轴零点信号出现
	'X-axis moves in minus direction until appearance of switch zero signal(X-axis)	
	G904 X=#BKREFSPEEDROUGH0  * #REFDIRROUGH0 I0 P1	
	G04 P10
	'X轴正向运动，直到X轴零点信号出现
	'X-axis moves in plus direction until appearance of switch zero signal (X-axis)
	G904 X=#BKREFSPEEDROUGH0 / 3 * #REFDIRROUGH0 * -1 I0 P1		
	G04 P10
	'X轴正向运动，直到X轴零点信号消失
	'X-axis moves in plus direction until disappearance of switch zero signal (X-axis)
	G904 X=#BKREFSPEEDROUGH0 / 9 * #REFDIRROUGH0 * -1 I0 P0	
	G04 P10
'精定位阶段
'Fine positioning
	'关闭缓冲区
	'Buffer zone off
	M802 P196609	
	G04 P10
	'X轴负向运动，直到X轴零点信号出现
	'X-axis moves in minus direction until appearance of encoder zero signal (X-axis)
	G904 X=100 * #REFDIRROUGH0 I0 P1
	G04 P10
	'X轴正向运动，直到X轴零点信号消失
	'X-axis moves in plus direction until disappearance of encoder zero signal (X-axis)
	G904 X=-60 * #REFDIRROUGH0 I0 P0
	G04 P10
	'开启缓冲区
	'Buffer zone on 		
	M802 P196608	
	G04 P10
	'X轴正向再运动X回退距离，以脱离信号敏感区域
	'X-axis moves [distance retracted] in plus direction to deviate from signal sensitive area
	G905 G903 G91 G00 X=#-39 * #REFDIRROUGH0 * -1 F500
	'同步	
	'Synchronization
	G906
	'恢复限位
	'Reset Limit	
	M802 P131073
	'向系统指示：X轴回机械原点完成
	'Feedback to system: X-axis back to REF is done	
	M902 H0		
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Y轴单独回机械原点（参考点）
'Y-axis back to REF
O102
'粗定位阶段
'Rough positioning
	'屏蔽限位
	'Shield limit signal
	M802 P131072
	'Y轴负向运动，直到Y轴零点信号出现
	'Y-axis moves in minus direction until appearance of switch zero signal(Y-axis)	
	G904 Y=#BKREFSPEEDROUGH1 * #REFDIRROUGH1 J3 Q1	
	G04 P10
	'Y轴正向运动，直到Y轴零点信号出现
	'Y-axis moves in plus direction until appearance of switch zero signal (Y-axis)
	G904 Y=#BKREFSPEEDROUGH1 / 3 * #REFDIRROUGH1 * -1 J3 Q1	
	G04 P10
	'Y轴正向运动，直到Y轴零点信号消失
	'Y-axis moves in plus direction until disappearance of switch zero signal (Y-axis)
	G904 Y=#BKREFSPEEDROUGH1 / 9 * #REFDIRROUGH1 * -1 J3 Q0
	G04 P10
'精定位阶段
'Fine positioning
	'关闭缓冲区
	'Buffer zone off
	M802 P196609
	G04 P10
	'Y轴负向运动，直到Y轴零点信号出现
	'Y-axis moves in minus direction until appearance of encoder zero signal (Y-axis)
	G904 Y=100 * #REFDIRROUGH1 J3 Q1
	'Y轴正向运动，直到Y轴零点信号消失
	'Y-axis moves in plus direction until disappearance of encoder zero signal (Y-axis)
	G904 Y=-60 * #REFDIRROUGH1 J3 Q0	
	G04 P10
	'开启缓冲区
	'Buffer zone on
	M802 P196608	
	G04 P10
	'Y轴正向再运动Y回退距离，以脱离信号敏感区域
	'Y-axis moves [distance retracted] in plus direction to deviate from signal sensitive area
	G905 G903 G91 G00 Y=#-40 * #REFDIRROUGH1 * -1 F500
	'同步
	'Synchronization	
	G906
	'恢复限位
	'Reset Limit	
	M802 P131073
	'向系统指示：Y轴回机械原点完成
	'Feedback to system: Y-axis back to REF is done	
	M902 H1		
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Z轴单独回机械原点（参考点）
'Z-axis back to REF
O103
'粗定位阶段
'Rough positioning
	'屏蔽限位
	'Shield limit signal
	M802 P131072
	'Z轴正向运动，直到Z轴零点信号出现
	'Z-axis moves in plus direction until appearance of switch zero signal (Z-axis)	
	G904 Z=#BKREFSPEEDROUGH2 * #REFDIRROUGH2 K6 H1	
	G04 P10
	'Z轴负向运动，直到Z轴零点信号出现
	'Z-axis moves in minus direction until appearance of switch zero signal(Z-axis)	
	G904 Z=#BKREFSPEEDROUGH2 / 3 * #REFDIRROUGH2 * -1 K6 H1
	G04 P10
	'Z轴负向运动，直到Z轴零点信号消失
	'Z-axis moves in minus direction until disappearance of switch zero signal (Z-axis)
	G904 Z=#BKREFSPEEDROUGH2 / 9 * #REFDIRROUGH2 * -1 K6 H0	
	G04 P10
'精定位阶段
'Fine positioning
	'关闭缓冲区
	'Buffer zone off
	M802 P196609
	G04 P10
	'Z轴正向运动，直到Z轴零点信号出现
	'Z-axis moves in plus direction until appearance of encoder zero signal (Z-axis)
	G904 Z=100 * #REFDIRROUGH2 K6 H1
	'Z轴负向运动，直到Z轴零点信号消失
	'Z-axis moves in minus direction until disappearance of encoder zero signal (Z-axis)	
	G904 Z=-60 * #REFDIRROUGH2 K6 H0	
	G04 P10
	'开启缓冲区
	'Buffer zone on
	M802 P196608	
	G04 P10
	'Z轴负向再运动Z回退距离，以脱离信号敏感区域
	'Z-axis moves [distance retracted] in minus direction to deviate from signal sensitive area
	G905 G903 G91 G00 Z=#-41 * #REFDIRROUGH2 F500
	'同步
	'Synchronization	
	G906
	'恢复限位
	'Reset Limit	
	M802 P131073
	'向系统指示：Z轴回机械原点完成
	'Feedback to system: Z-axis back to REF is done	
	M902 H2	
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'XY轴同时回机械原点（参考点）
'X/Y-axis back to REF
O104
'粗定位阶段
'Rough positioning
	'屏蔽限位
	'Shield limit signal
	M802 P131072
	'X(Y)轴负(负)向运动，直到X(Y)轴零点信号出现
	'X/Y-axis moves in minus direction until appearance of switch zero signal(X/Y-axis)	
	G904 X=#BKREFSPEEDROUGH0 * #REFDIRROUGH0 I0 P1 Y=#BKREFSPEEDROUGH1 * #REFDIRROUGH1 J3 Q1	
	G04 P10
	'X(Y)轴正(正)向运动，直到X(Y)轴零点信号出现
	'X/Y-axis moves in plus direction until appearance of switch zero signal(X/Y-axis)
	G904 X=#BKREFSPEEDROUGH0 / 3 * #REFDIRROUGH0 * -1 I0 P1 Y=#BKREFSPEEDROUGH1 / 3 * #REFDIRROUGH1 * -1 J3 Q1
	G04 P10
	'X(Y)轴正(正)向运动，直到X(Y)轴零点信号消失
	'X/Y-axis moves in plus direction until disappearance of switch zero signal(X/Y-axis)
	G904 X=#BKREFSPEEDROUGH0 / 9 * #REFDIRROUGH0 * -1 I0 P0 Y=#BKREFSPEEDROUGH1 / 9 * #REFDIRROUGH1 * -1 J3 Q0	
	G04 P10
'精定位阶段
'Fine positioning
	'关闭缓冲区
	'Buffer zone off
	M802 P196609
	G04 P10
	'X(Y)轴负(负)向运动，直到X(Y)轴零点信号出现
	'X/Y-axis moves in minus direction until appearance of encoder zero signal(X/Y-axis)	
	G904 X=100 * #REFDIRROUGH0 I0 P1 Y=100 * #REFDIRROUGH1 J3 Q1	
	G04 P10
	'X(Y)轴正(正)向运动，直到X(Y)轴零点信号消失
	'X/Y-axis moves in plus direction until disappearance of encoder zero signal(X/Y-axis)	
	G904 X=-60 * #REFDIRROUGH0 I0 P0 Y=-60 * #REFDIRROUGH1 J3 Q0	
	G04 P10
	'开启缓冲区
	'Buffer zone on
	M802 P196608	
	G04 P10
	'X(Y)轴正(正)向再运动X(Y)回退距离，以脱离信号敏感区域
	'X/Y-axis moves [distance retracted] in plus direction to deviate from signal sensitive area
	G905 G903 G91 G00 X=#-39 * #REFDIRROUGH0 * -1 Y=#-40 * #REFDIRROUGH1 * -1 F500
	'同步
	'Synchronization	
	G906
	'恢复限位
	'Reset Limit	
	M802 P131073
	'向系统指示：X轴回机械原点完成
	'Feedback to system: X-axis back to REF is done		
	M902 H0
	'向系统指示：Y轴回机械原点完成
	'Feedback to system: Y-axis back to REF is done		
	M902 H1		
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'主轴正转(只支持一个方向)
'spindle CW (only available for one single direction )
O203
	G906
	IF(!#SSINPORT10)M801 "|D|Tool clamping signal is not detected..";M30
	if(#SSOUTPORT0)M901 H0 P0; M801 "|D|spindle speed is increasing..";  G04 P=#-3
	if(!#SSOUTPORT1)M901 H1 P1;  M801 "|D|spindle speed is increasing.."; G04 P=#-3
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'主轴反转(只支持一个方向)
'spindle CCW (only available for one single direction )
O204
	G906
	if(#SSOUTPORT1)M901 H1 P0;  G04 P=#-3
	if(!#SSOUTPORT0)M901 H0 P1;  G04 P=#-3
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'主轴停转
'Spindle OFF
O205
	G906
	if(#SSOUTPORT0)M901 H0 P0; M801 "|D|Spindle OFF";G04  P=#-3
	if(#SSOUTPORT1)M901 H1 P0;M801 "|D|Spindle OFF"; G04  P=#-3
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'#15 当前刀具号
'#16 目标刀具号
'#CHANGETOOL_AHEAD_POS0 换刀前置点X坐标
'#CHANGETOOL_AHEAD_POS1 换刀前置点Y坐标
'#CALIPOS0 换刀完成后对刀点X坐标
'#CALIPOS1 换刀完成后对刀点Y坐标
'#CALIPOS2 换刀完成后对刀点Z坐标
'#CHANGETOOL_XYAXISSPEED 水平移动时进出刀库速度
'#CHANGETOOLSPEED 换刀时主轴移动速度
'#17 G90(TRUE)/G91(FALSE)模态信息
'#18 当前刀具位置X坐标
'#19 当前刀具位置Y坐标
'#20 当前刀具位置Z坐标
'#21 目标刀具位置X坐标
'#22 目标刀具位置Y坐标
'#23 目标刀具位置Z坐标
'松刀信号输入端口地址43
'夹刀信号输入端口地址44
'防尘罩上抬检测信号输入端口地址45
'打开关闭防尘罩信号输出端口地址6
'卸刀夹刀信号输出端口地址7
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'#15 current tool number
'#16 target tool number
'#CHANGETOOL_AHEAD_POS0 Tool change ahead position coordinate(X)
'#CHANGETOOL_AHEAD_POS1 Tool change ahead position coordinate(Y)
'#CALIPOS0 Tool measuring coordinate(X) after tool change
'#CALIPOS1 Tool measuring coordinate(Y) after tool change
'#CALIPOS2 Tool measuring coordinate(Z) after tool change
'#CHANGETOOL_XYAXISSPEED Tool change speed of XY axes movement
'#CHANGETOOLSPEED Tool change speed
'#17 G90(TRUE)/G91(FALSE)module information
'#18 coordinate(X) of current tool position
'#19 coordinate(Y) of current tool position
'#20 coordinate(Z) of current tool position
'#21 coordinate(X) of target tool position
'#22 coordinate(Y) of target tool position
'#23 coordinate(Z) of target tool position
'Input port address of unclamping signal: 43
'Input port address of clamping signal: 44
'Input port address of Dust Cover Up arrived detection: 45
'Output port address of Dust Cover Up/Down: 6
'Output port address of Clamp/Unclamp tool: 7

'换刀子程序
'O"CHANGETOOL"
O206
	G906
	IF(!#IS_BKREF0 || !#IS_BKREF1 || !#IS_BKREF2) M801 "|P|Not back to REF yet";M5;M30;
  IF(#16 < 1) M801 "|P|Illegal target tool NO.";M5;M30;
  IF(#16 > #TOOLLIBSIZE) M801 "|P|Illegal target tool NO.";M5;M30;
	IF(#15 < 0) M801 "|P|Illegal current tool NO.";M5;M30;
	IF(#15 > #TOOLLIBSIZE) M801 "|P|Illegal current tool NO.";M5;M30;
	IF(#16=#15) M801 "|E|current tool NO. and target tool NO. is same, so do not change the tool"; M17

	M801 "|E|From {#15} tool to {#16} tool"
	'禁止暂停抬刀
	M802 P=(8*65536+1)
	M802 P=(16*65536+1) '使用换刀行程

	M5
	'抬起吸尘罩
	'Dust hood up
	M901 H25 P1
	'暂停1秒，等待主轴停稳
	G04 P1000
	G906 H22 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Spindle zero signal is not detected!"; M30;
	G906 H23 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Dust cover up signal is not detected!"; M30;
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	
	G906
	#48 =1  '进入换刀状态 

	'Z轴上升到顶端	
	G53 G00 G90 Z-1

	'若换刀后需要重新回到当前工件坐标，记录当前工件坐标
	'Save the current coordinate in WCS, for returning to it after tool change.
	IF (#MOVETOPREPOS_AFTER_CHANGETOOLS) G906;                                           
	IF (#MOVETOPREPOS_AFTER_CHANGETOOLS) #1=#SSCURWORKCOOR0;	#2=#SSCURWORKCOOR1;
	'等待防尘罩上抬端口信号，假设防尘罩上抬信号输入端口号为45，等待5000ms
	'Wait for the input signal of dust hood uplift. The port address is 45 , wait for 5000ms.
	'G906 H45 P1 L5000 
	'IF(#SYNTIMEOUT<0) M801 "|E|Dust hood is not up"; 	M901 H6 P0; M30;
	'Z移到换刀前置点处
	'Coordinate (Z) in MCS moves to tool change ahead position
	G905 G903 G53 G90  G01 Z= #CHANGETOOL_AHEAD_POS2 
	'将X移动到当前刀具位置，Y移动到换刀前置点
	'Coordinate (X) moves to current tool position, coordinate(Y) moves to tool change ahead position
	G905 G903 G53 G00 Y=#CHANGETOOL_AHEAD_POS1 X=#18 
	'将Z移动到当前刀具位置
	'Coordinate (Z) moves to current tool position
	G905 G903 G53 G01 Z=#20
	'将Y移动到当前刀具位置
	'Coordinate (Y) moves to current tool position 
	G905 G903 G53 G01 Y=#19  F=#CHANGETOOLSPEED                    
  
	G04 P500
	'卸刀信号端口输出，电磁阀工作，气缸进气卸刀。卸刀信号的端口号为9
	'tool- unclamping signal outputs, electromagnetism clique functions, 
	'the air fills the cylinder, thus unclamping tool. The output address is 9.
	M901 H9 P1			
	G04 P=#CTDELAY
	'调用松刀检测子程序2次
	'Call subprogram “tool- unclamping detection” twice
	G65 P2061 L2

	G04 P500
	G905 G903 G53 G01 Z=#20+10 F=#CHANGETOOLSPEED
	G905 G903 G53 G01 Z=#CTUP
	
	G905 G903 G53 G00 X=#21 Y=#22
	G905 G903 G53 G01 Z=#23+10
	G905 G903 G53 G01 Z=#23    F=#CHANGETOOLSPEED
	G04 P500
	M901 H9 P0
	G04 P=#CTDELAY
	'调用夹刀检测子程序2次
	'Call subprogram “tool-clamping detection” twice
	G65 P2062 L2

	G04 P500
	'显示换刀后新刀具号
	'update changed tool number
	G906                          
	#48 =0  '退出换刀状态
	M903 H=#16  
	G905 G903 G53 G01 Y=#CHANGETOOL_AHEAD_POS1 F=#CHANGETOOLSPEED
	'Z轴上升到顶端	
	G53 G00 G90 Z-1       
	
	

	'放下吸尘罩
	'down Dust Hood
	M901 H25 P0 
	G04 P200
	
	G906
	'恢复暂停抬刀
	'Enable Tool-uplift on Pause
	M802 P=(8*65536+0)  	
	M802 P=(16*65536+0) '使用机床行程
	
	M801 "|M|tool change completed!"
	'执行第二次对刀校正Z轴
	'execute second step tool measurement to measure new tool
	IF(#CALITOOL_AFTER_CHANGETOOLS)G65 P17 L1					          
	IF (#MOVETOPREPOS_AFTER_CHANGETOOLS) G905 G903 G00 X=#1 Y=#2
	
  IF( #17) G90 
  IF(!#17) G91
  'M3
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'自动换刀功能-松刀检测子程序
'Automatic Tool Change-subprogram for tool-unclamping detection
o2061
	'等待松刀端口信号，假设松刀信号输入端口号为9，等待5s
	'wait for Signal from tool-unclamping input signal, port address 9, wait for 5seconds
	G906 H9 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-unclamping signal is not detected"; M30;
	G04 P50
M17
'自动换刀功能-夹刀检测子程序
'Automatic tool change-subprogram for tool-clamping detection  
o2062
	'等待夹刀端口信号，假设夹刀信号输入端口号为10，等待5s
	'wait for Signal from tool-clamping input port, port address 10, wait for 5seconds
	G906 H10 P1 L5000 
	IF(#SYNTIMEOUT<0) M801 "|P|Tool-clamping signal is not detected"; M30;
	G04 P50
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'冷却开
'coolant on
O208
	M901 H5 P1
	G04 P5
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'冷却关
'coolant off
O209
	M901 H5 P0
	G04 P5
	G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'FANUC固定循环指令，仅在铣床系统中使用。
'固定循环中定义的变量意义及变量次序:
' X--#1----钻孔中心X坐标值
' Y--#2----钻孔中心Y坐标值
' Z--#3----Z平面的工件坐标(孔深度，应为负值)
' R--#4----R平面的工件坐标(在R平面快进转为工进，大于零值)
' Q--#5----每一次的加工深度(增量值且为正值)
' P--#6----(单位为ms)
' F--#7----进给速度（模态值，会影响后续指令）
' L--#8----废除,原来为加工重复的次数
' I--#9----镗削时X轴偏移量
' J--#10---镗削时Y轴偏移量
' K--#11---指定孔加工重复的次数
' G99/G98--#12---G99->1; G98->0
' G90/G91--#13---G91->1; G90->0
' Z0--#14---Initial point height
'注意：在调用以下固定循环时处在非增量方式下(G90)
'      在循环指令中调用的子程序，不能使用#1 - #13 否则可能引起程序出错

'FANUC command for Fixed repetition, only used in milling machine system
'Meanings and order of the variables defined in Fixed repetition.
' X--#1----coordinate(X) of the boring center
' Y--#2----coordinate(Y) of the boring center
' Z--#3----work piece coordinates on plane Z(depth of the hole, minus)
' R--#4----work piece coordinates on plane R (quick feed turn into )
' Q--#5----machining depth each time (incremental and positive)
' P--#6----(unit is ms)
' F--#7----feed speed（modal value，affecting subsequent commands）
' L--#8----aborted, machining repeat times originally
' I--#9----offset on X axis in boring
' J--#10--- offset on Y axis in boring
' K--#11---set repeat times of hole machining
' G99/G98--#12---G99->1; G98->0
' G90/G91--#13---G91->1; G90->0
' Z0--#14---Initial point height
'Note:  The following Fixed Repetitions are called under non-incremental mode (G90)
'       Subprograms called in cycling commands must not use parameters #1-#13, or errors will arise.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G73，高速深孔往复排屑钻
'Fixed repetition, G73, Drilling cycle - high speed peck-drilling (deep hole)
'参数：
'Parameter:
' #50---退刀量
'	#50---tool retracting distance
O673
	G65 P873 L=#11
M17

O773
	G90 G0 Z=#4-#52*ABS(#5)
	#52=#52+1
	G90 G1 Z=#4-#52*ABS(#5) F=#7
	G91 G0 Z=#50
M17

O873
	G90
	IF(#13)G91
	G0 X=#1 Y=#2
	G90 G0 Z=#4
	#52=0
	G65 P773 L=INT(ABS(#3-#4)/ABS(#5))
	G90 G0 Z=#4-#52*ABS(#5)
	G90 G1 Z=#3 F=#7
	G04 P100
	G90 G0 Z=#4
	IF(!#12)G90 G0 Z=#14
	IF(#13)G91
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G76, 镗削
'Fixed repetition, G76, Boring cycle - presicion
O676
	G90
	IF(#13)G91
	G0 X=#1 Y=#2
	G91
	G0 Z=#4
	G1 Z=#3 F=#7
	G4 P=#6
	G1 X=#9 Y=#10
	G4 P=#6
	G0 Z=#4
	G90
	IF(!#12)G90 G0 Z=#14
	IF(#13)G91
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G81，一般钻孔
'Fixed repetition, G81, drilling cycle
O681
	G90
	IF(#13)G91
	G0 X=#1 Y=#2
	G90 G0 Z=#4
	G1 Z=#3 F=#7
	G04 P100
	G0 Z=#4
	G90
	IF(!#12)G90 G0 Z=#14
	IF(#13)G91
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G82，锪孔
'Fixed repetition, G82, Drilling cycle - spot-drilling (with Dwell)
' 该循环基本与G81相同，唯一区别在于：
'	It’s generally the same with G81, and the only difference is:
' G82可以设置在孔底的停留时间P__。
' G82 allows the preset of lingering time P at the hole-bottom
O682
	IF(#13)G91
	G0 X=#1 Y=#2
	G90 G0 Z=#4
	G1 Z=#3 F=#7
	G4 P=#6 F=#7
	G0 Z=#4
	G90
	IF(!#12)G90 G0 Z=#14
	IF(#13)G91
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G83，钻深孔
'Fixed repetition, G83, Drilling cycle - peck-drilling (deep hole)
O683
	G90
	IF(#13)G91
	G0 X=#1 Y=#2
	G90 G0 Z=#4
	#52=0
	G65 P783 L=INT(ABS(#3-#4)/ABS(#5))
	G90 G0 Z=#4-#52*ABS(#5)
	G90 G1 Z=#3 F=#7
	G04 P100
	G90 G0 Z=#4
	IF(!#12)G90 G0 Z=#14
	IF(#13)G91
M17
	
O783
	G90 G0 Z=#4-#52*ABS(#5)
	#52=#52+1
	G90 G1 Z=#4-#52*ABS(#5) F=#7
	G90 G0 Z=#4
	M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G85，精镗孔
'Fixed repetition, G85, boring cycle
' 该循环基本与G81相同，唯一区别在于：
'	It’s generally the same with G81, and the only difference is:
' G85提刀时采用加工时的进给速度，而G81采用快速定位时的进给速度。
'	When retracting tool, G85 adopts the feed speed of machining while G81 adopts the feed speed in rapid positioning 
O685
	G90
	IF(#13)G91
	G0 X=#1 Y=#2
	G91
	G0 Z=#4
	G1 Z=#3 F=#7
	G1 Z=#4
	G65 P785 L=#11-1
	G90
	IF(!#12)G90 G0 Z=#14
	IF(#13)G91
M17
	
O785
		G0 X=#1 Y=#2
		G1 Z=#3 F=#7
		G1 Z=#4
	M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G86，镗孔
'Fixed repetition, G86, Boring cycle - spindle stop at the depth
O686
	G90
	IF(#13) G91
	G0 X=#1 Y=#2
	G91
	G0 Z=#4
	G1 Z=#3 F=#7
	M5
	G0 Z=#4
	M3
	G65 P786 L=#11-1
	G90
	IF(#13) G91
M17
	
O786
		G0 X=#1 Y=#2
		G1 Z=#3 F=#7
		M5
		G0 Z=#4
		M3
	M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'固定循环, G89,镗孔,(与G82的差别只是在于提刀时G89为工进)
'Fixed repetition, G89, Boring cycle - dwell at the depth
'注意: 采用这种方式加工,如果连续加工的孔间距较小,可能出现刀具已经定位到下一个孔
' 的加工位置而主轴尚未达到规定转速,为此可以在各孔动作之间加入暂停G04.
'Note：In such machining mode, given gaps between neighboring holes are too narrow in continuously machining, 
'tool may be positioned to the next hole while spindle is still under set speed. 
'To this end, command G04 for pause is added.
O689
	G90
	IF(#13) G91
	G0 X=#1 Y=#2
	G91
	G0 Z=#4
	G1 Z=#3 F=#7
	G4 P=#6
	G1 Z=#4
	G65 P789 L=#11-1
	G90
	IF(#13) G91
M17
	
O789
		G0 X=#1 Y=#2
		G1 Z=#3 F=#7
		G4 P=#6
		G1 Z=#4
	M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'吸尘罩打开关闭
O300
		M901 H25 P1
		G906
M17
'''''''''''''''''''''''''''''''''''''
O400
		M901 H25 P0
		G906
M17
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
