minute read
Tasks
You are reading our Automation1 API documentation for the C programming language.
The Basics
In the C API, Tasks lets you interact with and manage the Automation1 controller tasks.
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
Tasks is part of the controller runtime. Thus the Automation1 controller must be running before you do work with tasks. For more information, see the Controller page.
All the C API functions that work with tasks accept a task index. You can use the Automation1_Controller_GetTaskIndexFromTaskName() function to get the task index for a task name.
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.
bool Automation1_Task_GetStatus(Automation1Controller controller, Automation1TaskStatus* taskStatusArrayOut, int32_t taskStatusArrayLength);
Manage AeroScript Programs
You can use tasks in the C API to run AeroScript programs on controller tasks. This is one of two ways to command your Automation1 controller, with the other being Commands.
bool Automation1_Task_ProgramRun(Automation1Controller controller, int32_t taskIndex, const char* controllerFileName);
The Automation1_Task_ProgramRun() function starts the execution of the AeroScript program, but it does not wait for the program to complete. The specified controller file can be an AeroScript source file (.ascript) or a compiled AeroScript file (.a1exe). If the controller file is an AeroScript source file, then the file will be compiled before it is executed.
To determine if an AeroScript program is still running, do the steps that follow:
- Get the status of the task by using the Automation1_Task_GetStatus() function and the Automation1TaskStatus struct.
- Examine the
TaskState
member that returns the Automation1TaskState enum. The value of this enum will give you information about your program: - The Automation1TaskState_ProgramRunning enum value tells you if an AeroScript program is currently running on that task.
- The Automation1TaskState_ProgramComplete enum value tells you if an AeroScript program completed successfully on that task.
- The Automation1TaskState_Error enum value tells you if an AeroScript program failed with some type of error that you can examine with the
Error
member.
To stop an AeroScript program that is running, use the Automation1_Task_ProgramStop() function.
bool Automation1_Task_ProgramStop(Automation1Controller controller, int32_t taskIndex, int32_t millisecondsTimeout);
Handle AeroScript Callbacks
AeroScript programs that run on a controller task can use the Callback()
AeroScript function to send information or to pass execution to a custom C application. When the Callback()
function is executed from AeroScript, program execution on that task waits for a registered C callback handler to execute. When the C callback handler completes, 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 they can return data to AeroScript. To learn more, see the Callback Functions page.
Inputs into a C callback handler and outputs out of a C callback handler can be AeroScript integers, AeroScript reals, and AeroScript strings. AeroScript integers are 64-bit. They are represented in C by the int64_t
data type. AeroScript reals are double-precision floating-point numbers. They are represented in C by the double data type.
When called, your callback handler is given two arguments, an Automation1TaskCallbackArguments handle and an Automation1TaskCallbackReturn handle. To get the AeroScript inputs from the Automation1TaskCallbackArguments handle, use the Automation1_Task_CallbackGetArguments() function. Then do one of the options that follow:
- If your callback handler was successful, use the Automation1_Task_CallbackSetReturn() function to set the AeroScript outputs on the Automation1TaskCallbackReturn handle.
- If your callback handler was not successful, use the Automation1_Task_CallbackSetErrorMessage() function to set the error message on the Automation1TaskCallbackReturn handle.
Each callback handler that you register will execute on a new thread when the callback is raised by an AeroScript program.
You can use the Automation1_Task_CallbackRegister() function to register a handle for the Callback()
AeroScript function. When you register a handler for this function, you must specify a function pointer of the Automation1TaskCallback
type that will be invoked when the Callback()
function is executed. You can register a callback ID one time per task.
To change the registration of the callback ID, you must unregister the handler that you are currently using. Then register a new handler.
Automation1_Task_CallbackRegister(Automation1Controller controller, int32_t taskIndex, int32_t callbackId, Automation1TaskCallback handler);
Automation1_Task_CallbackUnregister(Automation1Controller controller, int32_t taskIndex, int32_t callbackId);
Example Code
bool myCallback(Automation1TaskCallbackArguments callbackArguments, Automation1TaskCallbackReturn callbackReturn)
{
// Call the Automation1_Task_CallbackGetArguments() function one time to get the size of each array.
int32_t taskIndex;
int32_t callbackId;
int32_t integerInputsLen;
int32_t realInputsLen;
int32_t stringInputsLen;
int32_t maxStringInputLen;
if (!Automation1_Task_CallbackGetArguments(callbackArguments,
&taskIndex, &callbackId,
NULL, &integerInputsLen,
NULL, &realInputsLen,
NULL, &stringInputsLen, &maxStringInputLen))
{
/* handle error */
}
// Allocate space for the arrays and call the Automation1_Task_CallbackGetArguments() function again to populate them. AeroScript strings are stored in succession based on the stringInputsLen and maxStringInputLen values. The total space that an array of AeroScript strings requires is stringInputsLen * maxStringInputLen.
int64_t* integerInputs = (int64_t*)malloc(sizeof(int64_t) * integerInputsLen);
double* realInputs = (double*)malloc(sizeof(double) * realInputsLen);
char* stringInputs = (char*)malloc(sizeof(char) * stringInputsLen * maxStringInputLen);
if (!Automation1_Task_CallbackGetArguments(callbackArguments,
NULL, NULL,
integerInputs, &integerInputsLen,
realInputs, &realInputsLen,
stringInputs, &stringInputsLen, &maxStringInputLen))
{
/* handle error */
}
// Loop over and print the results.
for (int i = 0; i < integerInputsLen; i++)
{
printf("%d\n", integerInputs[i]);
}
for (int i = 0; i < realInputsLen; i++)
{
printf("%f\n", realInputs[i]);
}
for (int i = 0; i < stringInputsLen; i++)
{
printf("%s\n", &stringInputs[i * maxStringInputLen]);
}
free(integerInputs);
free(realInputs);
free(stringInputs);
// Set the values to return to an AeroScript program.
int64_t integerOutputs[4] = { 5, 2, 9, 102 };
double realOutputs[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
const char* stringOutputs[3] = { "Hello", "From", "C!" };
if (!Automation1_Task_CallbackSetReturn(callbackReturn,
integerOutputs, 4,
realOutputs, 5,
stringOutputs, 3))
{
/* handle error */
}
return true;
}
void registerMyCallback()
{
Automation1Controller controller;
if (!Automation1_Connect(&controller)) { /* handle error */ }
if (!Automation1_Task_CallbackRegister(controller, 1, 1, my_callback)) { /* handle error */ }
/* Do something else here, such as sleep or a long-running operation. The callback will be handled on a background thread. */
if (!Automation1_Task_CallbackUnregister(controller, 1, 1)) { /* handle error */ }
if (!Automation1_Disconnect(controller)) { /* handle error */ }
}
Task Variables
In the C API, Task Variables lets 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 C API.
Thread Safety
All the Automation1_Task_ functions are thread safe. You can call them from two or more threads without interference.
Only one thread at a time can compile an AeroScript program.
For Example
Let's say that you call the Automation1_Task_ProgramRun() or Automation1_Task_ProgramLoad() function concurrently from two or more threads. The AeroScript programs that you specified also need to be compiled. As a result, all the threads will be blocked until one of the threads is done compiling.
Each callback handler that you register executes on a new thread when the callback is raised by an AeroScript program.
The C API is thread safe. But the controller might not be able to manage 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 C thread responsible for a single controller task.
Full Reference
For more information about the structs and functions that are available for Tasks, refer to the lists that follow.
Automation1TaskStatus struct
Represents the status of a controller task on an Automation1 controller.
The task index that corresponds to this status.
The error on the task, if any. If there is no error, this will be zero.
The human-readable error message that corresponds to the error on the task, if any. If there is no error on the task, this string will be empty.
The warning on the task, if any. If there is no warning, this will be zero.
The human-readable warning message that corresponds to the warning on the task, if any. If there is no warning on the task, this string will be empty.
The state of execution for the task.
The value of the task mode on the task.
The null-terminated name of the compiled AeroScript file for the program that is loaded or running on the task. If no program is loaded, the string will be empty.
The null-terminated name of the compiled AeroScript file for the program that is loaded or running on the task. If no program is loaded, the name will be empty.
Functions
Gets the current status of one or more tasks.
controller
(In): The controller from which to get the array of task status.
taskStatusArrayOut
(Out): The out array of task status that you got from the controller. The size of this array determines the tasks for which to get status. For example, if this array has a size of 3
, the status for task 0, task 1, and task 2 are obtained from the controller and populated into the taskStatusArrayOut argument in that order. Use this only if the function call was successful. This argument must have memory preallocated before you pass it into this function.
taskStatusArrayLength
(In): The maximum number of elements to copy to the taskStatusArrayOut argument. This number must not be greater than the length of the taskStatusArrayOut
array.
Returns: True if the task status was successfully obtained. False if the task status was not obtained. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Runs the specified controller file as an AeroScript program on this task. If the controller file is an AeroScript source file, then the file will first be compiled. When you run a program, it will load automatically and program execution will begin on this task. This function 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
(In): The controller on which to run the program.
taskIndex
(In): The task on which to run the program.
controllerFileName
(In): The null-terminated name of the controller file that is the AeroScript program to run on this task (source program file or compiled program file).
Returns: True if the program was successfully started on the specified task. False if the program was not started on the specified task. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Loads the specified controller file as an AeroScript program on this task. If the controller file is an AeroScript source file, then the file will first be compiled. When you load the program, this process does not start it. You must call the Automation1_Task_ProgramStart() function 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
(In): The controller on which to load the program.
taskIndex
(In): The task on which to load the program.
controllerFileName
(In): The null-terminated name of the controller file that is the AeroScript program to load on this task (source program file or compiled program file).
Returns: True if the program was successfully loaded onto the specified task. False if the program was not loaded onto the specified task. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Starts or resumes the AeroScript program that is currently loaded on this task, beginning program execution. A program must be loaded onto this task so you can start or resume it. To load a program, call the Automation1_Task_ProgramLoad() function. This function waits for the loaded program to start, but it does not wait for the program to complete.
controller
(In): The controller on which to start or resume the program.
taskIndex
(In): The task on which to start or resume the program.
Returns: True if the program successfully started or resumed. False if the program did not start or resume. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Stops the AeroScript program that is currently running on this task, terminating program execution. This function waits the quantity of time that you specified for the loaded program to stop. If the timeout is set to -1
, this function will wait indefinitely. After a program is stopped, it is unloaded from the task. This function will also end a command queue that is running on this task.
controller
(In): The controller on which to stop the program.
taskIndex
(In): The task on which to stop the program.
millisecondsTimeout
(In): 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, the function will fail. This function will wait indefinitely for the loaded program to stop if this value is set to -1
.
Returns: True if the program stopped successfully before the specified quantity of time. False if the program did not stop before the specified quantity of time. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Pauses the AeroScript program that is currently running on this task, suspending program execution. This function waits the quantity of time that you specified for the loaded program to pause. If the timeout is set to -1
, this function will wait indefinitely. To resume program execution, call the Automation1_Task_ProgramStart() function.
controller
(In): The controller on which to pause the program.
taskIndex
(In): The task on which to pause the program.
millisecondsTimeout
(In): The number of milliseconds to wait to pause the program on this task. If the program uses more than this quantity of time to stop, the function will fail. This function will wait indefinitely for the loaded program to stop if this value is set to -1
.
Returns: True if the program paused successfully before the specified quantity of time. False if the program did not pause before the specified quantity of time. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Does a single line of program execution on the AeroScript program that is currently paused on this task, stepping into any function calls. You can call this function only if there is an AeroScript program loaded on this task and it is paused.
controller
(In): The controller on which to execute the single line of program execution.
taskIndex
(In): The task on which to execute the single line of program execution.
Returns: True if the single line of program execution executed successfully. False if the single line of program execution did not execute. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Does a single line of program execution on the AeroScript program that is currently paused on this task, stepping over any function calls. You can call this function only if there is an AeroScript program loaded on this task and it is paused.
controller
(In): The controller on which to execute the single line of program execution.
taskIndex
(In): The task on which to execute the single line of program execution.
Returns: True if the single line of program execution executed successfully. False if the single line of program execution did not execute. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Does execution on the AeroScript program that is currently paused on this task, stepping out of the current function call. You can call this function only if there is an AeroScript program loaded on this task and it is paused.
controller
(In): The controller on which to execute the single line of program execution.
taskIndex
(In): The task on which to execute the single line of program execution.
Returns: True if the single line of program execution executed successfully. False if the single line of program execution did not execute. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Registers a callback that can be raised by an AeroScript program. If the callback was successful, you must return true from the handler. If the callback was not successful, you must return false from the handler. You can register a callback ID only one time per task. To change the registration of a callback ID, you must unregister it first. To do this, see the Automation1_Task_CallbackUnregister() function.
controller
(In): The controller on which to register a callback.
taskIndex
(In): The task on which to register a callback.
callbackId
(In): A number that uniquely identifies each callback per task.
handler
(In): The code to execute when the callback is raised by an AeroScript program on this task.
Returns: True if the callback was registered successfully. False if the callback was not registered. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Unregisters a callback on a task.
controller
(In): The controller on which to unregister a callback.
taskIndex
(In): The task on which to unregister a callback.
callbackId
(In): A number that uniquely identifies the callback per task.
Returns: True if the callback was unregistered successfully. False if the callback was not unregistered. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Gets the arguments that are specified to a callback by an AeroScript program.
callbackArguments
(In): The callback arguments handle that is specified to a callback function.
taskIndexOut
(Out): The task on which this callback was raised.
callbackIdOut
(Out): The unique identifier of this callback on the task that it was raised on.
aeroScriptIntegersOut
(Out): The array of integer values that are specified to the callback by an AeroScript program.
aeroScriptIntegersLengthOut
(Out): The length of the aeroScriptIntegersOut
array. This argument will be set to the exact length if the aeroScriptIntegersOut argument is null. If the aeroScriptIntegersOut argument is not null, this argument will be set to the number of integers that was copied into the aeroScriptIntegersOut argument. To prevent calling this function two times to get the exact size, set this parameter based on the number of integers that you typically specify to your callback.
aeroScriptRealsOut
(Out): The array of real values that are specified to the callback by an AeroScript program.
aeroScriptRealsLengthOut
(Out): The length of the aeroScriptRealsOut
array. This argument will be set to the exact length if the aeroScriptRealsOut argument is null. If the aeroScriptRealsOut argument is not null, this will be set to the number of reals that was copied into the aeroScriptRealsOut argument. To prevent calling this function two times to get the exact size, set this parameter based on the number of reals that you typically specify to your callback.
aeroScriptStringsOut
(Out): The array of string values that are specified to the callback by an AeroScript program. AeroScript strings will be null-terminated and stored in succession based on the maxAeroScriptStringLengthOut argument. The number of strings that will be copied is based on the aeroScriptStringsLengthOut argument. Set this argument to null to get the exact values for the aeroScriptStringLengthsOut and maxAeroScriptStringLengthOut arguments.
aeroScriptStringsLengthOut
(Out): The length of the aeroScriptStringsOut
array. This argument will be set to the exact length if the aeroScriptStringsOut argument is null. If the aeroScriptStringsOut argument is not null, this argument will be set to the number of strings that was copied into the aeroScriptStringsOut argument. To prevent calling this function two times to get the exact size, set this parameter based on the number of strings that you typically specify to your callback.
maxAeroScriptStringLengthOut
(Out): The maximum length of each AeroScript string (including the null-terminator). This argument will be set to the exact length if the aeroScriptStringsOut argument is null. If the aeroScriptStringsOut argument is not null, this argument will be set to the maximum string length that was used to copy strings into the aeroScriptStringsOut argument. To prevent calling this function two times to get the exact size, set this parameter to 1000
characters.
Returns: True if all the arguments were copied successfully. False if all the arguments were not copied. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Sets the values to return to an AeroScript program when this callback succeeds. These values are returned to an AeroScript program only if the callback that sets them returns true.
callbackReturn
(In): The callback return handle that is specified to a callback function.
aeroScriptIntegers
(In): The array of integers to return to an AeroScript program.
aeroScriptIntegersLength
(In): The length of the aeroScriptIntegers
array.
aeroScriptReals
(In): The array of reals to return to an AeroScript program.
aeroScriptRealsLength
(In): The length of the aeroScriptReals
array.
aeroScriptStrings
(In): The array of null-terminated strings to return to an AeroScript program.
aeroScriptStringsLength
(In): The length of the aeroScriptStrings
array.
Returns: True if the return values were set successfully. False if the return values were not set. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Sets the error message to return to an AeroScript program when this callback fails. This error message is returned to an AeroScript program only if the callback that sets the error message returns false.
callbackReturn
(In): The callback return handle that is specified to a callback function.
errorMessage
(In): The null-terminated string that is displayed in the error message of an AeroScript program.
Returns: True if the error message was set successfully. False if the error message was not set. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Enables retrace mode for the AeroScript program running on the specified 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.
controller
(In): The controller on which to enable retrace.
taskIndex
(In): The task on which to enable retrace.
Returns: True if retrace was enabled successfully. False if retrace was not enabled successfully. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Disables retrace mode for the AeroScript program running on the specified 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.
controller
(In): The controller on which to disable retrace.
taskIndex
(In): The task on which to disable retrace.
Returns: True if retrace was disabled successfully. False if retrace was not disabled. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Enables the feedhold state on the specified task. When you enable the feedhold state, motion immediately decelerates, and program execution and immediate commands on the task stop.
controller
(In): The controller on which to enable feedhold.
taskIndex
(In): The task on which to enable feedhold.
Returns: True if the feedhold state was enabled successfully. False if the feedhold state was not enabled. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
Disables the feedhold state on the specified task. When you disable the feedhold state, motion on the 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.
controller
(In): The controller on which to disable feedhold.
taskIndex
(In): The task on which to disable feedhold.
Returns: True if the feedhold state was disabled successfully. False if the feedhold state was not disabled. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.
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.
controller
(In): The controller on which to set MFO.
taskIndex
(In): The task on which to set MFO.
mfoPercent
(In): The MFO percentage to set.
Returns: True if MFO was set successfully. False if MFO was not set. See the Automation1_GetLastError() and Automation1_GetLastErrorMessage() functions for more information. You can find information about these functions in the Errors and Error Handling section of C API Guidelines.