minute read
Tasks
You are reading our Automation1 API documentation for the Python™ programming language.
In the Python API, Tasks lets you interact with and manage the Automation1 controller tasks.
The Basics
The Automation1 controller has one or more tasks that execute AeroScript programs and AeroScript commands on the controller. Each task has a task name and a task index.
Task indices start at 1. Task index 0 is reserved and cannot be used.
How to Use
In the Python API, Tasks is part of the Controller.runtime property. For all the APIs that operate under this property, the Automation1 controller must be running before you can do work with tasks. For more information about this property, see the Controller page.
To work with tasks in the Python API, use the Controller.runtime.tasks property and index the task that you want to work with. You can specify a task by task name or task index. Refer to the example that follows.
controller.runtime.tasks["Task 1"]
controller.runtime.tasks[1]
You can get the status of a task to see if the task is running an AeroScript program or if there is an error on the task. Refer to the example that follows.
controller_task_status = controller.runtime.tasks[task].status
Each time that you access the status property, you get a new status from the controller. For best results, access the status property one time only to get the most current status. Then use the returned ControllerTaskStatus object until you need to get the most current status of the controller again. If you do not do this and try to access the status property too many times, bugs known as race conditions can occur. These bugs occur when the status of the controller changes between multiple accesses of the property.
Manage AeroScript Programs
You can use tasks in the Python API to run AeroScript programs on controller tasks. This is one of two ways to command your Automation1 controller, with the other being Commands. Use the program property on a task to manage AeroScript programs on the controller task. Refer to the example that follows.
controller.runtime.tasks[task].program.run(controller_file_name)
The ControllerTask.program.run method starts the execution of the AeroScript program, but it does not wait for the program to complete. The specified controller file can be either an AeroScript source file (.ascript) or a compiled AeroScript file (.a1exe). If the controller file is an AeroScript source file, then the file will compile before it executes.
To determine if an AeroScript program is still running, get the status of the task. Then examine the task_state property, which returns the TaskState enum. The values of this enum will give you information about your program:
- The TaskState.ProgramRunning enum value tells you if an AeroScript program is currently running on that task.
- The TaskState.ProgramComplete enum value tells you if an AeroScript program completed successfully on that task.
- The TaskState.Error enum value tells you if an AeroScript program failed with an error, which you can examine with the error property. Use the error_message property to get the string message of the error. Refer to the example that follows.
task_state = controller.runtime.tasks[task_name].status.task_state
task_error = controller.runtime.tasks[task_name].error
To stop an AeroScript program that is running, use the ControllerTask.program.stop method. Refer to the example that follows.
controller.runtime.tasks[task_name].program.stop()
Handle AeroScript Callbacks
AeroScript programs that run on a controller task can use the Callback()
AeroScript function to send information to or pass execution to a Python custom application. When the Callback()
function is executed from AeroScript, program execution on that task waits for any registered Python callback handler to execute. After the Python callback handler completes execution, the AeroScript program will continue.
Each callback is identified by a unique identifier, which is a callback ID. This ID is used when you register for the callback and when you execute the Callback()
AeroScript function. You can give these handlers data from AeroScript and return data back to AeroScript. To learn more, see the Callback Functions page.
Inputs into a Python callback handler and outputs out of a Python callback handler can be AeroScript integers, AeroScript reals, and AeroScript strings. AeroScript integers are 64-bit. They are represented in Python by the int data type. AeroScript reals are double-precision floating-point numbers. They are represented in Python by the float data type.
You can use the callback property to register for the Callback()
AeroScript function. When you register a handler for the Callback()
function, you must specify a function that is invoked when the callback function is executed. The Callback()
function requires values to be returned to AeroScript. Thus, your delegate will also return a value. If you do not want to return one or more values, return None.
If an error or exception occurs inside the Python callback handler, it will cause a task error to occur on the AeroScript program that raised the callback.
Example Code
import automation1 as a1
def my_callback(args: a1.ControllerTaskCallbackArguments):
print(f"Integers ({len(args.aeroscript_integer_inputs)}):")
for integer in args.aeroscript_integer_inputs:
print(integer)
print(f'Reals ({len(args.aeroscript_real_inputs)}):')
for real in args.aeroscript_real_inputs:
print(real)
print(f'Strings ({len(args.aeroscript_string_inputs)}):')
for string in args.aeroscript_string_inputs:
print(string)
return a1.ControllerTaskCallbackReturn(
[1, 2, 3],
None,
["Hello", "from", "python", "!"]
)
controller = a1.Controller.connect()
controller.start()
controller.runtime.tasks[1].callback.register(1, my_callback)
# Do something else here, such as sleep or a long-running operation. The callback will be handled on a background thread.
controller.disconnect()
Task Variables
In the Python API, Task Variables let you get and set task variables on a task on an Automation1 controller. You can use task variables to share information between iterations of a program running on the same task.
IMPORTANT: Refer to Task Variables to learn how to use Task variables in the Python API.
Thread Safety
The threading library is the only multithreading library that is officially supported by the Automation1 Python API.
All the methods and properties that operate under the Controller.runtime.tasks property are thread safe. You can call them from two or more threads without interference.
The Controller.runtime.tasks property is a collection of controller tasks that can be iterated over.
WARNING: While you can call the iter function on the Controller.runtime.tasks property, it is not recommended. If you do this, the iter function will return an iterator object that is not thread safe.
The Python API is thread safe. But the controller might not be able to handle all the task operations that you do on separate threads. A single controller task can run only one AeroScript program at a time. You must synchronize or coordinate your process to prevent controller errors from occurring.
For Example
You can make a single Python thread responsible for a single controller task.
Full Reference
For more information about the properties and methods that are available for Tasks, refer to the lists that follow.
Controller.runtime.tasks Properties
Gets the number of tasks on the Automation1 controller.
Gets the ControllerTask object for a specific task.
Gets the numeric index of this task, which you can use to uniquely identify this task.
Gets the current status for this task and returns a new ControllerTaskStatus object, which represents a moment in time for this task.
Controller.runtime.tasks[].debug Methods
Does a single line of program execution on the currently paused AeroScript program on this task, stepping into all function calls. You can call this method only if there is an AeroScript program loaded on this task and it is paused.
Does execution on the currently paused AeroScript program on this task, stepping out of the current function call. You can call this method only if there is an AeroScript program loaded on this task and it is paused.
Does a single line of program execution on the currently paused AeroScript program on this task, stepping over all function calls. You can call this method only if there is an AeroScript program loaded on this task and it is paused.
Controller.runtime.tasks[].program Methods
Loads the specified controller file as an AeroScript program on this task. If the controller file is an AeroScript source file, then the file is compiled before it loads. Loading does not start the program. You must call the Controller.runtime.tasks[task].program.start() method to begin program execution on this task. The AeroScript program must exist as a controller file on the Automation1 controller. You cannot load AeroScript libraries on a task. The controller file must be an AeroScript program (either a source program file or a compiled program file).
controller_file_name: The controller file that is the AeroScript program to load on this task (source program file or compiled program file).
Stops the currently running AeroScript program on this task, terminating program execution. If the milliseconds_timeout argument is not specified, this method waits indefinitely for the loaded program to pause. Otherwise it will wait for the specified number of milliseconds. To continue program execution, call the Controller.runtime.tasks[task].program.start() method.
millisecond_timeout: The number of milliseconds to wait before you pause the program on this task. If the program uses a quantity of time that is larger than this before it pauses, an exception is raised.
Runs the specified controller file as an AeroScript program on this task. If the controller file is an AeroScript source file, then the file is compiled before it runs. Running a program will automatically load the program and begin program execution on this task. This method waits for the program to load and start, but it does not wait for the program to complete. The AeroScript program must exist as a controller file on the Automation1 controller. You cannot run AeroScript libraries on a task. The controller file must be an AeroScript program (either a source program file or a compiled program file).
controller_file_name: The controller file or compiled AeroScript program to run on this task (source program file or compiled program file).
Starts or continues the currently loaded AeroScript program on this task, beginning program execution. A program must be loaded onto this task before you can start or continue it. To load a program, call the Controller.runtime.tasks[task].program.load(controller_file_name) method.
The Controller.runtime.tasks[task].program.start() method waits for the loaded program to start, but it does not wait for the program to complete.
Stops the currently running AeroScript program on this task, terminating program execution. If the milliseconds_timeout argument is not specified, this method will wait indefinitely for the loaded program to stop. Otherwise it will wait for the specified number of milliseconds. This method will also stop queue mode and buffered run mode. After a program is stopped, it is unloaded from the task.
milliseconds_timeout: The number of milliseconds to wait to stop the program on this task. If the program uses more than this quantity of time to stop, an exception is raised.
Controller.runtime.tasks[].motion Methods
Enables retrace mode for the AeroScript program running on this task. When you enable retrace mode, the controller executes motion and motion-synchronized functions in reverse order through functions that were previously executed. The controller keeps a history of moves and synchronized commands that it can use for retrace.
Disables retrace mode for the AeroScript program running on this task. When you disable retrace mode, the controller executes forward through the retrace history until it moves to the program line where retrace was activated.
Enables the feedhold state on this task. When you enable the feedhold state, motion immediately decelerates, and program execution and immediate commands on this task stop.
Disables the feedhold state on this task. When you disable the feedhold state, motion on this task accelerates to the speed it was before feedhold was enabled. Program execution and immediate commands on this task continue from where they were before the feedhold state was enabled.
Sets the MFO (manual feedrate override) percentage for this task, which scales the programmed feedrate for all coordinated motion and motion generated by the MoveRapid()
AeroScript function. The true feedrate of the move is calculated by multiplying the MFO percentage with the programmed feedrate. To get the current value of MFO for this task, use Status (ControllerRuntime.Status)
to retrieve TaskStatusItem.Mfo
.
mfoPercent: The MFO percentage to set.
ControllerTaskStatus Properties
Gets the index of the task on the controller.
Gets the value of the task mode on the task.
Gets the state of execution for the task.
Gets the name of the AeroScript source file for the program that is loaded or running on the task, or null if there is no program loaded or if there is no source file.
Gets the name of the compiled AeroScript file for the program that is loaded or running on the task, or null if there is no program loaded.
Gets the error on the task, if any. This will be zero if there is no error.
Gets the string message of the error on the task, if any. This will be empty if there is no error.
Gets the warning on the task, if any. This will be zero if there is no warning.
Gets the string message of the warning on the task, if any. This will be empty if there is no warning.