Skip to main content

Mach 4 Gems

Mach 4 Gems

These are G code and Lua snippets that have solved some problems in the past that may prove useful or insightful.

Abort a Tool Change if Not Homed
function UserMCodeModule.UserMainToolChangeCycle( tc )
	-- If any enabled axis isn't homed, abort.
	local a,b,c = m.VerifyHoming()
	if not a then
		w.Error('Cannot perform a tool change while any axes are not homed')
	end

	return tc,true,w.FunctionCompleted()
end
Perform an Action When RapidPath Becomes Operational (On Startup)
function UserGUIModule.ReactToRPOperational( callback, timer_period )
	-- Create a timer that checks for RapidPath to become operational.
	-- Once that happens, perform the action that depends on operationality.

	-- The RP plugin should have created this before the screen ever begins to load.
	local opstate_hreg = w.GetRegHandle(0, 'RapidPath', 'FSMState')

	user._rpopnl_handler = wx.wxEvtHandler()
	user._rpopnl_timer = wx.wxTimer(user._rpopnl_handler)
	user._rpopnl_handler:Connect(wx.wxEVT_TIMER,
								 function( event )
									 event:Skip()
									 -- Check for operational.
									 local opstate = mc.mcRegGetValueString(opstate_hreg)
									 if opstate == 'Operational' then
										 user._rpopnl_timer:Stop()
										 callback()
									 end
								 end
	)
	-- The timer period can be adjusted to improve responsiveness.
	if timer_period == nil then
		-- Check every 200 ms.
		timer_period = 200
	end
	user._rpopnl_timer:Start(timer_period)
end

Call this function whenever you want to do something that hinges on RapidPath being operational. For example, setting an Mach analog output or digital output.

You can invoke it when the screen loads doing something like this. In this example, analog output #3 is set to 50.0 as soon as the Mach analog is connected to the EtherCAT analog output.

function UserGUIModule.UserScreenLoadScript()
	------------------ Add user code below this line ------------------ 
	user.ReactToRPOperational( function()
        local ao_idx = 3
        local ao_value = 50
		w.LogF('Setting analog output #%d initial value to: %f',ao_idx,ao_value)
        mc.mcAnalogOutputWrite(0, ao_idx, ao_value)
	end)
	------------------ Don't Edit below this line ------------------ 
	return true, true, w.FunctionCompleted()	
end