Error Handling

This AeroScript program shows how to use OnError statements to configure a task error handler.

// Error Handling Example:
// Demonstrates how to use the onerror() statement to configure a task error handler.
// Instructions:
// 1. Run this program on any task.
// 2. The X axis will be enabled and will perform a MoveFreerun().
// 3. From the Variables & I/O module in Automation1 Studio, toggle the value
//    of Digital Output 0 on X. Notice that the value of the digital output determines
//    the direction of the move.
// 4. While the axis is moving, disable the X axis. This will cause a task error and the 
//    OnTaskError() function will be called automatically to clear the task error and
//    to restart this program.

program

	// Configure a handling routine to recover from task errors.
	onerror(OnTaskError())

	// Enable the X axis.
	Enable(X)

	// This program runs as an infinite loop reading a digital output
	// to determine which direction to move the X axis.
	while (true)
		if (DigitalOutputGet(X, 0) == 0)
			MoveFreerun(X, 100)
			Dwell(0.100)
		else
			MoveFreerun(X, -100)
			Dwell(0.100)
		end
	end

end


// This function is the error handler that will be called when a
// task error occurs.
// For example, if the MoveFreerun() is issued when the X axis is disabled,
// this will cause a task error. This routine is an example of how to
// recover from that task error.
function OnTaskError()
	// Clear the task error and wait for a small amount of time for the task
	// error to clear.
	AcknowledgeAll()
	Dwell (0.100)

	// Additional custom error handling can go here.

	// In this example, this error handling routine 
	// will restart the program. Instead of restarting the program, you
	// can exit the program with either the ProgramExit() function
	// or by simply returning from this function.
	ProgramRestart()
end