loading

JScript

Please be sure to read the Plugins page to get a general sense of if the JScript module is a good fit for your project.

The JScript module provides a way to create custom Javascript scripts that can be used to process image statistics and map then toward servo/motor values. This module is intented to be used as a way to quickly perform custom operations without needing to implement a Plugin or use the API which typically require external tools.

The interface allows two methods for specifying your code. One technique requires that you specify a text file as the source of the program. You can use your favorite text editor to edit/create this file. The file should be a regular text file containing JScript commands/functions/operators. The program interface displays the current file being used, available system or user variables (and their values) and any messages the program produces.

The alternate way is to include the JScript text within the provided text box (see below). Using this technique allows you to include the JScript code within the .robo program and not require another user to save the code and reference a text file as needed by the above file based technique. Using the text box provides a quick way to enter in text but is NOT meant as a replacement for a full featured text editor as its editing features are very basic.

Interface

RoboRealm Specific Functions

  • VariableExists("variable_name") - returns true or false depending on if the specified variable exists in RoboRealm. Several of the variable return functions below will return a valid value regardless of if the variable exists or not. You can use this function to determine if the value returned from one of these functions is a valid value.

  • GetVariable("variable_name") - returns an integer value of the specified variable. Zero is returned when the variable does not exist.

  • GetFloatVariable("variable_name") - returns the float value of the specified variable. A zero is returned when the variable does not exist.

  • GetStrVariable("variable_name") - returns the string value of the specified variable. An empty string "" is returned when the variable does not exist.

  • GetArrayVariable("array_variable_name") - returns the array associated with the specified variable. An array is ALWAYS returned but the length (ubound) will be zero if the array does not exist.

  • SetArrayVariable("array_variable_name", array_variable) - sets an array associated with the specified variable name; for example
    ReDim points(4)
    
    points(0) = 100
    points(1) = 100
    points(2) = 150
    points(3) = 200
    
    SetArrayVariable "waypoints", points
    

  • SetVariable "variable_name", variable_value - sets the variable value to the specified variable

  • SetTimedVariable "variable_name", variable_value, timeout - sets the variable value to the specified variable after the timeout (milliseconds) has passed. This allows you to specify when in time a variable's value should be changed. Note that each call to SetTimedVarible will reset the timer so be sure to only call it ONCE to allow the time to lapse and set the specified value. This function is useful when you need an alternative to the Sleep function. By setting a variable to a value in the future you can emulate a Sleep function by allowing something to happen for a period of time, but then switching it off after the time has lapsed. So instead of using
    SetVariable("motor", 100);
    Sleep(1000)
    SetVariable("motor", 0);
    
    you could use
    SetVariable("motor", 100);
    SetTimedVariable("motor", 0, 1000);
    

  • AddTimedVariable "variable_name", variable_value, timeout - similar to SetTimedVariable, this function will instead add the timing parameter to a queue instead of overwriting the timing information. For example:
    SetTimedVariable("motor", 100, 1000);
    SetTimedVariable("motor", 200, 1500);
    SetTimedVariable("motor", 300, 2000);
    
    will result in the motor variable being set to 300 in 2 seconds (2000 ms). What happens is that the first two calls to SetTimedVariable are OVERWRITTEN by the third. Instead, if you use
    AddTimedVariable("motor", 100, 1000);
    AddTimedVariable("motor", 200, 1500);
    AddTimedVariable("motor", 300, 2000);
    
    the motor variable will be set to 100 in 1 second, 200 in 1.5 seconds and finally 300 in 2 seconds. The AddTimedVariable will add each call into a list that is executed in sequence based on the specified time.

  • Sleep(X) - suspends processing for X number of milliseconds and returns control to the pipeline to acquire additional images and send values to external devices.

  • WaitVariable("variable_name", variable_value) - suspends processing until the specified variable equals the specified value. This function returns control to the pipeline to acquire additional images and send values to external devices while it waits for the variable value.

  • WaitVariableChange("variable_name") - suspends processing until the specified variable changes its value. This function returns control to the pipeline to acquire additional images and send values to external devices while it waits for the value to change.

  • WaitImage() - suspends processing and returns control to the pipeline for one pipeline iteration. This allows for the acquisition of additional images and to send values to external devices.

  • GetPixels() - returns an array of RGB values of the current image

  • SetPixels(pixel_data_array) - sets the current image to the specified pixel RGB data

  • SetParameter("module_name", count, "param_name", "param_value") - changes the specified parameter in the specified module to the specified value. This is useful when a GUI dialog does not have a variable selection as part of the dropdown. Avoid using this function unless necessary as it is slow to update the GUI. Count is used to distinquish between multiple uses of the same module (i.e. module_name would be the same for either module).

  • GetParameter("module_name", count, "param_name") - returns the specified parameter in the specified module. This is useful when a GUI dialog does not expose a value within a variable.

  • GetFloatParameter("module_name", count, "param_name") - the float version of the above routine.

  • GetStrParameter("module_name", count, "param_name") - the string version of the above routine.

  • Write("text") - sends text to the module message output area in the dialog interface (used for debugging)

  • StopProcessing() - stops RoboRealm from continuing to process the images with the remaining modules

  • CameraOff() - switches off the image capturing from the current camera

  • CameraOn() - switches on the image capturing using the current camera

  • PushButton("module_name", count, "Button") - automates pushing a button in one of the RoboRealm GUI windows. For example:
    if (GetStrVariable("test") != "pushed")
    {
      SetVariable("test", "pushed");
      PushButton("Read_AVI", 0, "Start");
    }
    
    will cause the Read_AVI module (assuming one exists in the pipeline) to start playback.
    PushButton("RoboRealm", 0, "Snap");
    
    will cause RoboRealm to create a snapshot of the current image.

  • atan2(x,y) - arc tangent function not present in JScript but useful for trigonometric calculations

  • sqrt(x) - square root function not present in JScript but useful for trigonometric calculations

Examples

To access variables from your JScript file/script use the following commands:

MyVar = GetVariable("variable_name")

Or to create a new variable use

SetVariable("variable_name", variable_value)

Once a new variable is created this variable becomes available to control functions (such as in the SSC module) that can be used to automatically control a servo/motor.

You can also get variable arrays using

MyVar = GetArrayVariable("variable_array_name")

or get individual entries in an array using

MyVar = GetVariable("variable_array_name:2")

which would return the second entry in an array.

To change a variable's value in X milliseconds you can use

SetTimedVariable("variable_name", variable_value, timeout_in_milliseconds)

which is very useful for resetting motor values after a period of time.

All regular JS Script commands and operators are available for use in the JScript file. See Microsoft Scripting for complete documentation on how to use these commands or view the sample files for examples.

For example the following program (assuming you have added the Center Of Gravity module) will map COG to servo motors:

var cogx GetVariable("cog_x");
var 
cogy GetVariable("cog_y");

if 
(cogx < 140)
{
  left_motor 
60;
  
right_motor 128;
}
else
if 
(cogx > 180)
{
  left_motor 
128;
  
right_motor 60;
}
else
{
  left_motor 
128;
  
right_motor 128;
}

SetVariable(
"left_motor", left_motor);
SetVariable("right_motor", right_motor);

Note that the COGX values range from 0 to 320. COGY ranges from 0 to 160. These values are bounded by the current image size. These values may change if you crop, shrink, etc. the image dimensions.

You could then use a module like the SSC module to map the "left_motor" and "right_motor" variables to actual servos.

You can also process the image pixels directly using JScript. This is useful for prototyping but not recommended due to performance reasons for actual usage. The JScript module supports two routines to get and set the image pixels. Following is the example SwapColor module but in JScript.

var pixels

pixels 
GetPixels();

for 
(i=0;i<(320*240*3)-1;i+=3)
{
    tmp 
pixels[i]
    pixels[i] 
pixels[i+2];
    
pixels[i+2] tmp;
}

SetPixels(pixels)
;

To write variables or debug statements to a log file you can use

var fso = new ActiveXObject("Scripting.FileSystemObject");
var 
fi fso.OpenTextFile("test.txt"8true);
fi.writeLine(GetVariable("cog_x")+":"+GetVariable("cog_y"));
fi.close();

Note that JScript can access external activeX objects such as the Microsoft XML Http object. The following example shows this technique to issue a custom URL to a remote machine. Be sure to also have a look at the HTTP module for such tasks.

// Show Status Output 1
var objHTTP = new ActiveXObject("MSXML2.ServerXMLHTTP");
objHTTP.open("GET""http://172.17.42.54/axis-cgi/io/output.cgi?check=1");
objHTTP.send("");

// Activate Output 1
var objHTTP = new ActiveXObject("MSXML2.ServerXMLHTTP");
objHTTP.open("GET""http://172.17.42.54/axis-cgi/io/output.cgi?action=1:/");
objHTTP.send("");

// Deactivate Output 1
var objHTTP = new ActiveXObject("MSXML2.ServerXMLHTTP");
objHTTP.open("GET""http://172.17.42.54/axis-cgi/io/output.cgi?action=1:\\");
objHTTP.send("");

The module has a built in timeout mechanism to avoid infinite loops from hanging the application. This timeout is set in Options Button-> Other Tab-> Timeouts Pipeline should you need to increase the default 10 seconds.

Variables

On each successful run of the script the SCRIPT_COUNT variable is incremented. Thus you can use this variable to indicate a first run state where variables can be initialized to default values. Note that the Reload and Run button will reset this count to 0. You can also use the IMAGE_COUNT variable to determine first run when the Run button in the main GUI is toggled. The SCRIPT_COUNT is NOT changed when the main GUI Run button is toggled.

Downloads

If RoboRealm complains about missing components you may need to download the Microsoft Script Control.

See Also


VBScript Program
CScript Program
Python Program


 New Post 

JScript_Program Related Forum PostsLast postPostsViews
Java help needed
Hello There I am trying to run following java code in Jscript to stitch video using video/camera but showing error...
7 year 2 2248
JScript GetArrayVariable()
I try to get the confidence array form the fiducials data in a JScript with the GetArrayVariable() function
8 year 2 2079
HTTP module, string variables, JSON parsing...
If one has used the JScript module to create a 6 KB or 8 KB string variable containing JSON data, is it possible to use the HTTP...
9 year 5 3076