In this example first line of the script creates an axis-type object with the variable name "x", which tries to use controller with the
serial number "123". If this controller is not connected, then the script will return an error and terminate. The second line of the script
sends a "move to position 50" command to this controller.
Function usage example.
New file object creation
XILab scripts can read from and write to files. To do this you need to create a "file" object, passing desired filename in its constructor.
File object has the following functions:
return_type
Function_name
Description
bool open() Opens the file. File is opened in read-write mode if possible, in read-only mode otherwise.
void close() Closes the file.
Number size() Returns file size in bytes.
bool seek(Number
pos)
Sets current position in file to pos bytes
1
.
bool resize(Number
size)
Resizes the file to size bytes. If size is less than current file size, then the file is truncated, if it is greater than
current file size, then the file is padded with zero bytes.
bool remove() Removes the file.
String read(Number
maxsize)
Reads up to maxsize bytes from the file and returns result as a string. Data is read in utf-8 Unicode encoding.
Number write(String
s, Number maxsize)
Writes up to maxsize btyes to the file from the string. Data is written in utf-8 unicode encoding, end-of-line
character should be set by user. Returns amount of written bytes or -1 if an error occurred.
All file functions which return bool type, return "true" on success and "false" on failure.
Use "/" symbol as path separator, this works on all systems (Windows/Linux/Mac).
1
Seeking beyond the end of a file: If the position is beyond the end of a file, then seek() shall not immediately extend the file. If a
write is performed at this position, then the file shall be extended. The content of the file between the previous end of file and the
newly written data is UNDEFINED and varies between platforms and file systems.
Example:
var winf = new_file("C:/file.txt"); // An example of file name and path on Windows
var linf = new_file("/home/user/Desktop/file.txt"); // An example of file name and path on Linux
var macf = new_file("/Users/macuser/file.txt"); // An example of file name and path on Mac
var f = winf; // Pick a file name
if (f.open()) { // Try to open the file
f.write( "some text" ); // If successful, then write desired data to the file
f.close(); // Close the file
} else { // If file open failed for some reason
log( "Failed opening file" ); // Log an error
}
Function usage example.
Creation of calibration structure
new_calibration(double A, int Microstep) function takes as a parameter a floating point number A, which sets the ratio of user units to
motor steps, and microstep division mode, which was either read earlier from MicrostepMode field of get_engine_settings() return
type, or set by a MICROSTEP_MODE_ constant. This function returns calibration_t structure, which should be passed to calibrated
get_/set_ functions to get or set values in user units. The following two forms are functionally equivalent:
// create calibration: type 1
var calb = new_calibration(c1, c2);
// create calibration: type 2
var calb = new Object();
calb.A = c1;
calb.MicrostepMode = c2;
Function usage example.
Get next serial