Option Explicit'********DO NOT remove or change the expand line or the included file contents******************'  MachStdMIll license terms REQUIRE that the copyright and License terms remain a part of this source file#Expand <Masters\Headers\CopyRightAndLicenseNotice>	'**************************************************************************************#Expand <Masters\Headers\MachConstants>Const MSMLEDBase 						= 1700				' standard MSM resource space baseConst LatheHomeAndTCSequenceXZYLED		= MSMLEDBase + 69Const LatheHomeAndTCSequenceZYXLED		= MSMLEDBase + 70Const debug = false					' controls creation of debug mesages in the mach history.error log file.Const SimulXYRef = false				' if true, for mill mode only, references X,Y simultaniously instead of sequentiallyConst XAxisBit = 1Const YAxisBit = 2Const UseMach1024RefCode = true		' For mill ref all operastions, this constant controls how we ref the axes. 									' if true, the refall all home script uses the old mach3 1024 screen set reference code that date back to mach2.x days									'	This old code ONLY handles 4 axis mills!									'	This old code may fail on some machine with later versions of mach due to mach timing hole bugs. 									'									' if false, the script will use MSM enhaced ref logic that 									' 	a) references all the enabled axes (this is recommmended for use with the MSM 6 axis screen set)									' 	b) waits for mach ref operations to complete (there are timing holes in later version of mach that can make a ref operation return before it is done).									DebugMsg("Ref All Home operation started")If LCase(Right(GetActiveScreenSetName(), 5)) = ".lset"  Then	' we are running a Lset file which means we are in lathe mode	DebugMsg("Lathe mode ref all")	If GetUserLED(LatheHomeAndTCSequenceXZYLED) = 1 Then			' this is the traditional X then Z lathe ordering with Y last if Y is enabled.		' Typically used on a lathe.		DebugMsg("LatheHomeAndTCSequenceXZYLED set")				' First ref X then do Z...		DebugMsg("ref X")		DoOEMButton( HomeRefXOEMBtn )		Call MachRefWait()		DoOEMButton(ZeroXEncoderOEMBtn)				DebugMsg("ref Z")		DoOEMButton( HomeRefZOEMBtn )		Call MachRefWait()		DoOEMButton(ZeroZEncoderOEMBtn)				' and if it's enabled, do Y last		If Not GetOEMLED(MachYAxisDisabledOEMLED) Then			' Y axis is enabled, so we assume we are in lateh-on-mill operation and include the Y axis in the homign sequence			DebugMsg("ref Y")			DoOEMButton( HomeRefYOEMBtn )				Call MachRefWait()			DoOEMButton(ZeroYEncoderOEMBtn)		End If	Else		' this is the safer order (XZY) for MIll-Turn homing		DebugMsg("LatheHomeAndTCSequenceZYXLED IS set")				' First ref Z then Y then X...		DebugMsg("ref Z")		DoOEMButton( HomeRefZOEMBtn )		Call MachRefWait()		DoOEMButton(ZeroZEncoderOEMBtn)				' and if it's enabled, do Y 		If Not GetOEMLED(MachYAxisDisabledOEMLED) Then			' Y axis is enabled, so we assume we are in lathe-on-mill operation and include the Y axis in the homign sequence			DebugMsg("ref Y")			DoOEMButton( HomeRefYOEMBtn )				Call MachRefWait()			DoOEMButton(ZeroYEncoderOEMBtn)		End If				DebugMsg("ref X")		DoOEMButton( HomeRefXOEMBtn )		Call MachRefWait()		DoOEMButton(ZeroXEncoderOEMBtn)	End If	DebugMsg("Lathe Ref all op finished")Else	' we are in mill mode		If UseMach1024RefCode Then		' this is the code that stock mach3 has always done 		' as the stock code does not wait for the ref ops to complete, we do the same (incorrect) thing here... (See MachRefWait() )		' code to send all 4 axes home to reference				'DebugMsg("old 1024 style ref w/o axis waits")		'DebugMsg("Ref = ZYXA")		DoOEMButton( HomeRefZOEMBtn )				If SimulXYRef Then			RefCombination(XAxisBit + YAxisBit)		Else			' ref XY sequentially			DoOEMButton( HomeRefYOEMBtn )			DoOEMButton( HomeRefXOEMBtn )		End if 		DoOEMButton( HomeRefAOEMBtn )				'DebugMsg("Zeroing XYZ")		DoOEMButton(ZeroXEncoderOEMBtn)		DoOEMButton(ZeroYEncoderOEMBtn)		DoOEMButton(ZeroZEncoderOEMBtn)	Else		' we reference all the enabled axes - this supports referencing for the 6 axis screen set version of MSM 		DebugMsg("MSM Mill ref with axis waits")				' first Z		If Not GetOEMLED(MachZAxisDisabledOEMLED) Then			' Z axis is enabled			DebugMsg("Referencing Z")			DoOEMButton( HomeRefZOEMBtn )				Call MachRefWait()			DoOEMButton(ZeroZEncoderOEMBtn)		End If				If SimulXYRef Then			RefCombination(XAxisBit + YAxisBit)			Call MachRefWait()			DoOEMButton(ZeroYEncoderOEMBtn)			DoOEMButton(ZeroXEncoderOEMBtn)		Else			' ref XY sequentially								' then Y			If Not GetOEMLED(MachYAxisDisabledOEMLED) Then				' Y axis is enabled				DebugMsg("Referencing Y")				DoOEMButton( HomeRefYOEMBtn )					Call MachRefWait()				DoOEMButton(ZeroYEncoderOEMBtn)			End If			' then X			If Not GetOEMLED(MachXAxisDisabledOEMLED) Then				' X axis is enabled				DebugMsg("Referencing X")				DoOEMButton( HomeRefXOEMBtn )					Call MachRefWait()				DoOEMButton(ZeroXEncoderOEMBtn)			End If		End if 				' then the rotary axes - we do these in the same relative order as the linear axes		' so C next		If Not GetOEMLED(MachCAxisDisabledOEMLED) Then			' C axis is enabled			DebugMsg("Referencing C")			DoOEMButton( HomeRefCOEMBtn )				Call MachRefWait()			' there is not mach call to sero an encoder for a rotary axis		End If		' then B		If Not GetOEMLED(MachBAxisDisabledOEMLED) Then			' B axis is enabled			DebugMsg("Referencing B")			DoOEMButton( HomeRefBOEMBtn )				Call MachRefWait()			' there is not mach call to sero an encoder for a rotary axis		End If		' and finally A		If Not GetOEMLED(MachAAxisDisabledOEMLED) Then			' A axis is enabled			DebugMsg("Referencing A")			DoOEMButton( HomeRefAOEMBtn )				Call MachRefWait()			' there is not mach call to sero an encoder for a rotary axis		End If		DebugMsg("MSM new code Mill Ref all op finished")	End If	End IfExit Sub Sub MachRefWait()	' ok the following is a mach hack....	' (relevant to mach 3.x.x)	'	' it turns out that Mach reference operations do not wait for the action to be complete before returning to the calling script. 	' Yet, the calls are commonly used as if they are blocking calls (even though they turn out not to be) - 1024.set also uses them as if they are blocking	' Add to this the fact that there is no built in API to wait in a script for a reference operation to be complete...	' and we have a timing hole that the script can fall into.	'	' But we can trick mach by inserting a short dwell into the Gcode queue, 	' the dwell will show up internally to mach as a gcode movement that IsMoving() API ***will*** wait on,	' AND (this is key) mach will ***not*** start executing the dwell command until the reference op is done.... 	' thus we can force a wait until the ref (and dwell) operation is completed.	'	' The dwell time amount is not important.  I used P0.1, the time unit (sec or ms) depends on the mach config setting.	' Either way the added delay will not be noticed as part of the overall homing sequence time. 	Code("G4 P0.1")	While IsMoving() 		sleep 10	Wend	Exit SubEnd SubSub DebugMsg(ByVal m As String)	If Debug Then		message("MSMRefAll: " & m)		sleep (50) ' attempt to get msg to stay in mach msg queu for history log	End If	Exit SubEnd Sub  