# The Script Builder Tool

KDE applications can be controlled externally from another program, from a console prompt, or from a shell script using the Desktop COmmunication Protocol (DCOP). KStars takes advantage of this feature to allow rather complex behaviors to be scripted and played back at any time. This can be used, for example, to create a classroom demo to illustrate an astronomical concept.

The problem with DCOP scripts is, writing them is a bit like programming, and can seem a daunting task to those who do not have programming experience. The Script Builder Tool provides a GUI point-and-click interface for constructing KStars DCOP scripts, making it very easy to create complex scripts.

## Introduction to the Script Builder

Before explaining how to use the Script Builder, I provide a very brief introduction to all of the GUI components; for more infomation, use the "What's This?" function.

The Script Builder Tool
Script Builder Tool

The Script Builder is shown in the above screenshot. The box on the left is the Current Script box; it shows the list of commands that comprise the current working script. The box on the right is the Function Browser; it displays the list of all available script functions. Below the Function Browser, there is a small panel which will display short documentation about the script function highlighted in the Function Browser. The panel below the Current Script box is the Function Arguments panel; when a function is highlighted in the Current Script box, this panel will contain items for specifying values for any arguments that the highlighted function requires.

Along the top of the window, there is a row of buttons which operate on the script as a whole. From left to right, they are: New Script, Open Script, Save Script, Save Script As..., and Test Script. The function of these buttons should be obvious, except perhaps the last button. Pressing Test Script will attempt to run the current script in the main KStars window. You should move the Script Builder window out of the way before pressing this, so you can see the results.

In the center of the window, there is a column of buttons which operate on individual script functions. From top to bottom, they are: Add Function, Remove Function, Copy Function, Move Up, and Move Down. Add Function adds the currently-highlighted function in the Function Browser to the Current Script box (you can also add a function by double-clicking on it). The rest of the buttons operate on the function highlighted in the Current Script box, either removing it, duplicating it, or changing its position in the current script.

## Using the Script Builder

In order to illustrate using the Script Builder, we present a small tutorial example where we make a script that tracks the Moon while the clock runs at an accelerated rate.

If we are going to track the Moon, we will need to point the display at it first. The lookToward function is used to do this. Highlight this function in the Function Browser, and note the documentation displayed in the panel below the Browser. Press the Add Function button to add this function to the Current Script box. The Function Arguments panel will now contain a combobox labeled dir, short for direction. This is the direction in which the display should be pointed. The combobox contains only the cardinal compass points, not the Moon or any other objects. You can either enter Moon in the box manually, or press the Object button to use the Find Object window to select the Moon from the list of named objects. Note that, as usual, centering on an object automatically engages object-tracking mode, so there is no need to add the setTracking function after lookToward.

Now that we have taken care of pointing at the Moon, we next want to make time pass at an accelerated rate. Use the setClockScale function for this. Add it to the script by double-clicking on it in the Function Browser. The Function Arguments panel contains a timestep spinbox for setting the desired time step for the simulation clock. Change the timestep to 3 hours.

OK, we have pointed at the Moon and accelerated the clock. Now we just want the script to wait for several seconds while the display tracks on the Moon. Add the waitFor function to the script, and use the Function Arguments panel to specify that it should wait for 20 seconds before continuing.

To finish up, let us reset the clock's timestep to the normal value of 1 second. Add another instance of setClockScale, and set its value to 1 sec.

Actually, we are not quite done yet. We should probably make sure that the display is using Equatorial coordinates before the script tracks the Moon with an accelerated time step. Otherwise, if the display is using Horizontal coordinates, it will rotate very quickly through large angles as the Moon rises and sets. This can be very confusing, and is avoided by setting the View Option UseAltAz to false. To change any View Option, use the changeViewOption function. Add this function to the script, and examine the Function Arguments panel. There is a combobox which contains the list of all options which can be adjusted by changeViewOption. Since we know we want the UseAltAz option, we could simply select it from the combobox. However, the list is quite long, and there is no explanation of what each item is for. It therefore may be easier to press the Browse Tree button, which will open a window containing a tree view of the available options, organized by topic. In addition, each item has a short explanation of what the option does, and the data type of the option's value. We find UseAltAz under the Skymap options category. Just highlight this item and press OK, and it will be selected in the combobox of the Function Arguments panel. Finally, make its value false or 0.

One more step: changing UseAltAz at the end of the script does us no good; we need this to be changed before anything else happens. So, make sure this function is highlighted in the Current Script box, and press the Move Up button until it is the first function.

Now that we have finished the script, we should save it to disk. Press the Save Script button. This will first open a window in which you can provide a name for the script, and fill in your name as the author. Enter Tracking the Moon for a name, and your name as the author, and press OK. Next, you will see the standard &kde; Save File dialog. Specify a filename for the script and press OK to save the script. Note that if your filename does not end with .kstars, this suffix will be automatically attached. If you are curious, you can examine the script file with any text editor.

Now that we have a completed script, we can run it in a couple of ways. From a console prompt, you can simply execute the script as long as an instance of KStars is currently running. Alternatively, you can execute the script from within KStars using the Run Script item in the File menu.

## Device Automation with INDI

Device scheduling and automation is supported for all INDI-compliant devices. You can coordinate any number of devices to perform complex operations using &kstars; Script Builder. This can be accomplished by using &kstars; INDI DCOP interface, which provides different classes of functions to suit your tasks. The INDI DCOP functions can be decomposed into five different classes. The following is a review of the functions and their arguments as supported in KStars. It it highly recommended to read the INDI Concepts section as we will employ key INDI concepts throughout this tutorial.

1. Generic Device Functions: Functions to establish/shutdown devices..etc.

   - `startINDI (QString deviceName, bool useLocal)` : Establish an INDI service either as local or server.

   - `shutdownINDI (QString deviceName)` : Shutdown the INDI service.

   - `switchINDI(QString deviceName, bool turnOn)` : Connect or Disconnect an INDI device.

   - `setINDIPort(QString deviceName, QString port)` : Set the device's connection port.

   - `setINDIAction(QString deviceName, QString action)` : Activate an INDI action. The action can be any element of a switch property

   - `waitForINDIAction(QString deviceName, QString action)` : Pause script execution until the specified action property returns with OK status.

2. Telescope Functions: Functions to control the telescope motion and status.

   - `setINDIScopeAction(QString deviceName, QString action)` : Set the telescope mode or action. Available options are SLEW, TRACK, SYNC, PARK, and ABORT.

   - `setINDITargetCoord(QString deviceName, double RA, double DEC)` : Set the telescope JNow target coordinates to RA and DEC.

   - `setINDITargetName(QString deviceName, QString objectName)` : Set the telescope JNow target coordinates to the coordinates of objectName. KStars will lookup the object name in its database and will fetch RA and Dec once found.

   - `setINDIGeoLocation(QString deviceName, double longitude, double latitude)` : Set the telescope geographical location to the longitude and latitude as specified. The longitude is measured from Greenwich, UK, to the East. However, while it is common to use negative longitudes for the Western hemisphere, INDI requires longitude values between 0 and 360 degrees. So if you have a negative longitude, simply add 360 degrees to get the value that INDI expects. For example, Calgary, Canada coordinates in &kstars; are longitude: -114 04 58; latitude: 51 02 58. So INDI's would need longitude = 360 - 114.083 = 245.917 degrees.

   - `setINDIUTC(QString ddeviceName, QString UTCDateTime)` : Set the telescope UTC Date and Time in ISO 8601 format. The format is YYYY-MM-DDTHH:MM:SS (e.g. 2004-07-12T22:05:32).

3. Camera/CCD Functions: Functions to control the camera/CCD properties and status.

   - `setINDICCDTemp(QString deviceName, int temp)` : Set the CCD chip target temperature in degrees celsius.

   - `setINDIFrameType(QString deviceName, QString type)` : Set the CCD frame type. Available options are FRAME_LIGHT, FRAME_BIAS, FRAME_DARK, and FRAME_FLAT.

   - `startINDIExposure(QString deviceName, int timeout)` : Start the CCD/Camera exposure for the duration specified by timeout in seconds.

4. Focuser Functions: Functions to control the focuser motion and status.

   - `setINDIFocusSpeed(QString deviceName, QString action)` : Set the focuser speed. Available options are FOCUS_HALT, FOCUS_SLOW, FOCUS_MEDIUM, and FOCUS_FAST.

   - `setINDIFocusTimeout(QString deviceName, int timeout)` : Set the duration in seconds for any subsequent startINDIFocus operations.

   - `startINDIFocus(QString deviceName, int focusDir)` : Move the focuser either inward (focusDir = 0) or outward (focusDir = 1). The speed and duration of this operation is set by the `setINDIFocusSpeed()` and `setINDIFocusTimeout()` functions.

5. Filter Functions: Functions to control the filter position.

   - `setINDIFilterNum(QString deviceName, int filter_num)` : Change the filter position to `filter_num`. The user can assign aliases to filter numbers in the Configure INDI dialog box under the Devices menu (e.g. Filter 1 = Red, Filter 2 = Green..etc).

Note that the device name is the first argument of all INDI functions. This permits different commands that are sent to different INDI devices to be intermixed together in one script. The Script Builder tool provides two options to facilitate the creation and editing of INDI scripts:

- `Append waitForINDIAction after any INDI action` : When checked, the Script Builder tool will automatically add `waitForINDIAction()` after any action it recognizes. For example, If you add `switchINDI()` function to the script and this option was checked, the Script Builder will add "waitForINDIAction CONNECTION" in the script file just after `switchINDI()`. This will cause the script to pause after it issues `switchINDI()` until `switchINDI()` returns with OK status (&ie; device connection was successful). It is critically important to know that the Script Builder cannot automatically add `waitForINDIAction()` for generic actions added using the `setINDIAction()` function. This is because KStars cannot determine the parent property of generic actions. Therefore, you must manually add `waitForINDIAction()` after generic actions when desired.

- `Reuse INDI device name` : When checked, the device name field of all subsequent functions is automatically filled with the last device name. The last device name is set every time `startINDI()` function is added to the current script. When working with multiple devices, it is recommended to have this option off.

Now we are ready to create a demo script that controls LX200 GPS telescope, in addition to Finger Lakes CCD camera. Our task is simple. We will ask the telescope to slew and track Mars, then we will ask the camera to take three shots 10 seconds each separated by 20 seconds.

Since there is no direct feedback from the INDI DCOP interface about the progress, value, or status of the device operations and parameters (except for `waitForINDIAction()`), device automation in KStars is similar to an open-loop control system. In such a system, there is usually no direct feedback to measure the progress of the system and to correct for errors. Consequently, you must design your device automation scripts with careful consideration. All automation scripts must be subjected to rigorous testing before deployment.

The Script Builder Tool
Script Builder Tool

The demo script is shown in the above screenshot. Note that we checked `"Append waitForINDIAction after any INDI action"` and unchecked `"Reuse INDI device name"`. The first function to add is `startINDI()` as shown above. We want to run our devices as local, so we will not change the service mode provided in the function arguments window. We type in our device name, starting with the telescope "LX200 GPS". We repeat the same operation again for "FLI CCD". There is a `waitFor()` function after that. It is generally recommended to use `waitFor()` function immediately after `startINDI()` to pause the script for 1-5 seconds. This will ensure that all properties are built and are ready to receive command. It is also useful for controlling remote devices because retrieving and building properties might take some time. In the next function, `switchINDI()`, we connect to each device.

Since `"Append waitForINDIAction after any INDI action"` is checked, we do not need to add `waitForINDIAction()` after `switchINDI()` to insure that we only continue executing the script after we successfully connect. This is because the Script Builder tool will do this automatically for us when we save the script. Now let us set the telescope mode to tracking, click on the `setINDIScopeAction()` function and select TRACK. Note that we need to set the telescope to tracking before issuing the coordinates it needs to track. The `setINDIScopeAction()` function is provided for convenience; since in this example, it simply issues a generic `setINDIAction()` function followed by the keyword TRACK. However, the benefit of using `setINDIScopeAction()` is that KStars can automatically append `waitForINDIAction()` after it when required. This facility is not automatically available to generic actions as we discussed before.

Next we use the `setINDITargetName()` function and set it to Mars. Finally, the last few steps involve capturing an image for 10 seconds which can be done by using `startINDIExposure()` function, and waiting for 20 seconds in between which can be done by using `waitFor()` function with a value of 20.

We can now save our script and execute it at any time. The saved script will be similar to this:

>         #!/bin/bash
>         #KStars DCOP script: Demo Script
>         #by Jasem Mutlaq
>         #last modified: Thu Jan 6 2005 09:58:26
>         #
>         KSTARS=`dcopfind -a 'kstars*'`
>         MAIN=KStarsInterface
>         CLOCK=clock#1
>         dcop $KSTARS $MAIN  startINDI "LX200 GPS" true
>         dcop $KSTARS $MAIN  startINDI "FLI CCD" true
>         dcop $KSTARS $MAIN  waitFor 3
>         dcop $KSTARS $MAIN  switchINDI "LX200 GPS" true
>         dcop $KSTARS $MAIN  waitForINDIAction "LX200 GPS" CONNECTION
>         dcop $KSTARS $MAIN  switchINDI "FLI CCD" true
>         dcop $KSTARS $MAIN  waitForINDIAction "FLI CCD" CONNECTION
>         dcop $KSTARS $MAIN  setINDIScopeAction "LX200 GPS" TRACK
>         dcop $KSTARS $MAIN  waitForINDIAction "LX200 GPS" ON_COORD_SET
>         dcop $KSTARS $MAIN  setINDITargetName "LX200 GPS" Mars
>         dcop $KSTARS $MAIN  waitForINDIAction "LX200 GPS" EQUATORIAL_EOD_COORD
>         dcop $KSTARS $MAIN  startINDIExposure "FLI CCD" 10
>         dcop $KSTARS $MAIN  waitForINDIAction "FLI CCD" EXPOSE_DURATION
>         dcop $KSTARS $MAIN  waitFor 20
>         dcop $KSTARS $MAIN  startINDIExposure "FLI CCD" 10
>         dcop $KSTARS $MAIN  waitForINDIAction "FLI CCD" EXPOSE_DURATION
>         dcop $KSTARS $MAIN  waitFor 20
>         dcop $KSTARS $MAIN  startINDIExposure "FLI CCD" 10
>         dcop $KSTARS $MAIN  waitForINDIAction "FLI CCD" EXPOSE_DURATION

The INDI Library provides robust scripting tools that enable developers to orchestrate very complex scripts. For more details, refer to the INDI Developer Manual.