Script Executor

ARC supports a number of programming languages built-in (JavaScript, EZ-Script & Python). The scripting languages contain many robot-specific commands. The power of scripting also enables robot skill controls to interact with each other using the ControlCommand().

This part of the tutorial will demonstrate how to execute a user-defined script within your plugin. An example of how this is useful is if your plugin provides configuration for the user to configure custom code for a specific action. If you review DJ's Tic Tac Toe example, you will notice that the configuration dialog allows the user to create a custom script for Winning, Losing, and Tie.

The namespace for all scripting methods is ARC.Scripting.

Simple Example
This code example demonstrates how to run a script piece of code that will speak a phrase out of the speaker when the button is pressed. You will need to add a Button to your form and assign the Click event for it to work.

Code:


public partial class MainForm : ARC.UCForms.FormPluginMaster {

ARC.Scripting.Executor _executor;

public MainForm()
: base() {

InitializeComponent();

_executor = new ARC.Scripting.Executor();
}

private void button1_Click(object sender, EventArgs e) {

_executor.StartScriptASync("Say(\"Hello, I am script\")");
}
}


Example with Events
There are many events that can be associated with the Executor, such as OnDone, OnError, etc.. This allows your program to accommodate behaviors. In this example, the Executor will display a message box when the script has completed executing.

Code:


public partial class MainForm : ARC.UCForms.FormPluginMaster {

ARC.Scripting.Executor _executor;

public MainForm()
: base() {

InitializeComponent();

_executor = new ARC.Scripting.Executor();
_executor.OnDone += _executor_OnDone;
}

private void button1_Click(object sender, EventArgs e) {

_executor.StartScriptASync("Say(\"Hello, I am a script\")");
}

void _executor_OnDone(string compilerName, TimeSpan timeTook) {

MessageBox.Show(string.Format("Script has completed and took {0} milliseconds", timeTook.TotalMilliseconds));
}
}


Starting A New Script When Another Is Running
If the executor is currently running an ASync script in the background while you launch another, the first script will be cancelled and the most recent script will begin executing. Each instance of an Executor can run only one script. To run multiple scripts at the same time, use an Executor per script.

Inside the Executor
The executor has many methods and events.

ARC.Scripting.Executor.ExecuteScriptSingleLine(string line);
Executes only one line of script and blocks until it has completed. This method returns the result of the single line of code. For example, if the code was a function to return the value of a servo (example GetServo(d0)), the value of the servo d0 will be returned.

ARC.Scripting.Executor.Resume();
There is a script command to "Pause" the current running script. If the command is ever executed in the script, this will resume the execution.

ARC.Scripting.Executor.StartScriptASync(Command[] compiled);
This executes the compiled Command Opcode array in the background. If you precompile the script into an array of Command Opcodes, this method can be used. The advantage to pre-compiling the source into Command Opcodes is that the script will execute faster because it will not need to be compiled each time. There is a compiler cache in the Executor, however. This means that if you run the same script twice that is not compiled, the last Opcode cache will be used.

ARC.Scripting.Executor.StartScriptASync(string script);
This method compiles the plain text script and executes it in the background. There is a compiler cache in the Executor, however. This means that if you run the same script consecutive times, the last Opcode cache will be used.

ARC.Scripting.Executor.StartScriptBlocking(string script);
This method compiles the plain text script and executes it on the current thread. There is a compiler cache in the Executor, however. This means that if you run the same script consecutive times, the last Opcode cache will be used.

ARC.Scripting.Executor.StopScript();
Stops the current ASync running script on this Executor.

Events
These events can be assigned to an Executor and will be raised at their appropriate function. The "CompilerName" parameter will include the optional compiler name that can be provided when the Executor class is initiated. In the above examples, the Executor class is created without any parameters. If a parameter was supplied, it would be the name of this compiler. If your program has many Executors that share these events, the CompilerName parameter will come in handy to identify what executor it originated from.

event OnCmdExecHandler(string compilerName, int lineNumber, string execTxt)
Event is raised for each line that is executed in the script. This will dramatically slow the execution of the script, but is great for debugging.

event OnDoneHandler(string compilerName, TimeSpan timeTook)
Event is raised when the script has completed.

event OnPausedHandler(string compilerName)
Event is raised when the script calls the Pause method. The Executor.Resume() function is necessary to continue, or StopScript().

event OnResumeHandler(string compilerName)
Event is raised when the script is instructed to resume after a Pause.

event OnStartHandler(string compilerName)
Event is raised when the script begins to execute.