Turntable Script | Object2VR

In Object2VR all turntables are controlled via a script within the turntable directory. (You can find the directory in the settings/preferences). The scripts are written in ECMAScript/Javascript with some special addition to communicate with Object2VR.
The main script start running after the “connect” button is pressed. All global objects and variables live until the “disconnect” button is pressed.

User Interface

There are two global objects to define the user interface (UI) on startup: setup and control.

setup: (shown in the “Table Setup” panel) should be used for one time settings like the serial port, table configuration like steps per round, maximum speed etc.

control: (shown in the “Table Control” panel) should be used for controls that are used on a regular basis like move the table/arm, trigger the camera from the table etc.

Both panel objects have the same methods and can be used in the same way. All UI elements are inserted into a grid. You can start a new line with a call to .addNewLine(). All examples use “setup”

Methodes

  • addLabel(string text)
  • addButton(string text)
  • addCheckbox(string text)
  • addCombobox()
  • addPortCombobox()
  • addPortComboBox()
  • addSpinbox(int min,int max)
  • addDoubleSpinBox(double min, doublemax,int precision=1)
  • addNewLine()
  • setEnabled(bool v) – enables/disables all UI elements within the panel

Label

setup.addLabel("Serial Port:");

or

control.addLabel("Move:");

This call adds a basic text label. The return object has a text property, so the can change its text later on.

Properties

  • enabled
  • visible
  • text – label on the button

Example

lPosition=control.addLabel("-");

...

lPosition.text="-5";

Button

setup.addButton("connect");

or

control.addButton("Stop");

This call adds a push button to the user interface. To react to a button click, you can attach a function to one of the events.

Events

  • onclick – fired after a click
  • onmousedown – button is pressed
  • onmouseup – button is released

Properties

  • enabled
  • visible
  • text – label on the button
  • down – the button is down
  • checked – the button is checked
  • checkable – the button is “sticky” and stays pressed after a click

Example

btLeft=control.addButton("<<");
btLeft.onclick=function() { 
  speed+=50;
  sendTmclCmd(1,0,speed); 
};

btStop=control.addButton("Stop");
btStop.onclick=function() {
  speed=0;
  sendTmclCmd(3,0,0); 
};

Check Box

setup.addCheckbox("Invert Direction");

This call adds a check box to the user interface.

Events

  • onchange

Properties

  • enabled
  • visible
  • text – label on the button
  • checked – the button is checked

Combo Box

setup.addCombobox();

This call adds a simple combo box to the users interface. with a call to .addItem(<label>,<value>) you can add the items to the list. The current value can be accessed with the currentvalue property.

Events

  • onchange

Properties

  • enabled
  • visible
  • currentvalue

Methodes

  • addItem(<label>,<value>)
  • currentIndex()
  • count()
  • setCurrentIndex(<value>)

Example

setup.addLabel("Acceleration:");
cbAccel=setup.addCombobox();
cbAccel.addItem("Very Slow",1);
cbAccel.addItem("Slow",10);
cbAccel.addItem("Normal",50);
cbAccel.addItem("Fast",200);
cbAccel.addItem("Very fast",1000);
cbAccel.currentvalue=host.loadSetting("accel",50);
cbAccel.onChange=function() {
  host.saveSetting("accel",cbAccel.currentValue);
}

Port Combo Box

setup.addPortCombobox();

This is a special version of a combo box that filled with all serial ports available to the system. The currentvalue property is the internal port name that can be used with the global createSerialPort function.

Spin Box

setup.addSpinbox(<int min>,<int max>);

This call adds a spin box, a text box to enter integer values, with little arrow buttons on the side.

Events

  • onchange

Properties

  • enabled
  • visible
  • value
  • suffix

Double Spin Box

setup.addDoubleSpinbox(<float min>,<float max>, <int precision>);

This call adds a doulbe spin box, a text box to enter float values, with little arrow buttons on the side.

Events

  • onchange

Properties

  • enabled
  • visible
  • value
  • suffix

Serial Port Object

A port object represents a serial port. To create a port object, you need to call the global method

createSerialPort(<string portname>,<int speed>,<string config>)

  • portname – The internal name of the port.
  • speed – baud rate of the connection. Typical values are 2400, 9600 or 115200
  • config – defines the number of data bits, parity and stop bits, default is “8N1”

Properties

  • boudRate
  • portName
  • config

Methodes

bool open ()

Opens the port, return true if successful.

void close ()

Closes the port.

int sendChar (char v)

Send a single character v. The parameter can be the ASCII code. Returns the number of characters actually sent.

int sendData (string data)

Send the data string in binary form without conversion. Returns the number of characters actually sent.

int sendString (string str)

Send the str string in ASCII encoding. Returns the number of characters actually sent.

int sendUtf8String (QString str)

Send the str string in UTF8 encoding. Returns the number of characters actually sent.

int bytesAvailable ()

Returns the number of bytes waiting in the receive queue.

string readAll ()

Reads the entire receive queue.

string readLine (int mstimeout,string endSequence=”n”,bool keepEnd=true)

Waits for a maximum of mstimeout to read a single line with endSequence as a line separator.

string readRawData (int mstimeout,int len)

Waits for a maximum of mstimeout to read len raw character.

array readRawDataArray (int mstimeout,int len)

Waits for a maximum of mstimeout to read len raw character. The return value as an array of int with the raw ASCII codes.

bool hasLine (string endSequence=”n”)

Peeks into the receive queue and returns true if the endSequence can be found.

string getLine (QString endSequence=”n”,bool keepEnd=true)

Reads a line from the receive queue without waiting.

void sleep (int ms)

Block the script for ms milliseconds.

Example

This example function sends a command to a trinamic stepper controller with a check sum, and waits for a reply.

function sendTmclCmdEx(cmd,type,bank,value) 
{
	var chs,s,rv,i,sendstr;
	value=Math.round(value) & 0xffffffff;
	sendstr=String.fromCharCode(1,cmd,type,bank,(value>>24) & 0xff,(value>>16) & 0xff,(value>>8) & 0xff,value & 0xff);
	chs=0; // calculate the checksum
	for(i=0;i<sendstr.length;i++) {
		chs+=sendstr.charCodeAt(i);
	}
	sendstr+=String.fromCharCode(chs & 0xff);
	port.sendString(sendstr);
	s=port.readRawDataArray(100,9);
	if ((s.length==9) && (s[2]==100)) {
		rv=(s[4] << 24) | (s[5] << 16) | (s[6] << 8) | (s[7]); 
		return rv;
	} else {
		host.debugLog("error");
		return false;
	}
}

Host Object

The host object give direct access to Object2VR and it can be used to print error or debug messages, update the progress, or store settings in the registry. The script has an instance named host on startup.

Methodes

  • void saveSetting (string key, Object value)
  • Object loadSetting (string key, Object default={})
  • debugLog (string msg)
  • errorLog (string msg)
  • messageLog (string msg)
  • processEvents () – process events in the Object2VR GUI to make the script non blocking
  • bool updateTablePosition (int column,int row,int state) – set the current position in Object2VR progress indicator

Timer Object

To create a timer object call the global function createTimer.

Properties

  • bool singleShot
  • int interval

Events

  • ontimeout

Methodes

  • start(int msec)
  • start()
  • stop()

Example

var timer;
timer=createTimer();
timer.interval=500;
timer.ontimeout=updatePosition;
timer.start();

Object2VR events

Whenever Object2VR ask the table to do something it calls a predefined function. Those functions must be defined in the script to properly react to the calls.

exConnect ()

This function is called after the connect button is pressed

exDisconnect ()

This function is called after the disconnect button is pressed

exStart (columns,rows)

This function is called before the single step capture process. The two parameters define the number of columns and rows to capture. It may be a good idea to store those values, if they are needed later for the exStep function.

exStep ()

This function is called after each photo in a single step capture process. The table should move to the next position and return true if successful.

exMoveTo (column, row)

This function is called after a recapture. The table should move to the given column and row and return true if successful, or false on error. If the table does not support this, the function should not be defined.

exTriggerCamera ()

This function is called if the trigger source is set to “table” in the Object2VR capture settings. The function should return true if successful. If the table has no camera trigger output, the function should not be defined.

exQuickShoot (columns,rows)

This function is called if the “quick shoot” button is pressed. The table should rotate and trigger the camera while rotation for each column and row. The function should return true if successful. If the table has no quick shoot capabilities, the function should not be defined. The “quick shoot” button will not be shown in the user interface.

exMultirow ()

This function is called after “connect” and should return true if the table support multi row shooting. If the function is not defined, the return value false is assumed.

Examples Scripts

These two scripts show the communication with a trinamic and nanotec stepper controller.
turntable_examples.zip