Implementation and Use of a Tcl/Tk API for the Accelerator Control
System of the The Svedberg Laboratory
L. Thuresson, V. Ziemann
The Svedberg Laboratory, Uppsala, Sweden

E. J. Veldhuizen
Department of Radiation Sciences, Uppsala University, Uppsala, Sweden
Abstract
The installation of a Tcl/Tk [1] Application Program
Interface (API) for the The Svedberg Laboratory (TSL)
accelerator control system has greatly simplified thedevelopment of high level applications for steering, dataacquisition and control. The Tcl scripting language has awell defined C interface that makes it easy to add userextensions in compiled code. The Tk toolkit provides abasic widget set for creating platform independent
Graphical User Interfaces (GUI). Third party extension
packages add high level widgets that further reduce
development time for GUI based applications. We describe
the implementation and discuss problems arising when
maintaining a Tcl installation incorporating large numbersof extension packages. Finally, we describe some of theapplications written using this framework, such as
automatic beam alignment for proton therapy and a GUI
based interface for operation and testing of the personnelsafety Radiation Protection Interlock (RPI) system.
1 Introduction
The TSL control system controls the Gustaf-WernerCyclotron and CELSIUS storage ring together with about
200 m of beam lines. The cyclotron produces protons and awide range of light and heavy ions with rigidities up to 2Tm. The beams are used for nuclear physics, materialsciences, isotope production, and clinical proton therapy.
CELSIUS is mainly used for nuclear physics experimentswith projectiles of up to 7 Tm (e.g. 1.3 GeV protons)
interacting with a cluster jet target and, soon, a hydrogenpellet target.
The control system hardware is based on VME-computersand UNIX workstations. The VME-computers are used asinterface to various front-end electronics equipment. EachVME-computer holds a live version of the subset of thecontrol system database that corresponds to the equipment
connected. The VME-computers are connected to a
dedicated Ethernet network together with the workstations,which area used as operator consoles [2]. Each workstation
holds a copy of the complete database but only the parts
currently in use by applications are updated.
The TSL control system uses a generic device description
called a parameter to model the hardware equipment in thecontrol system database. The parameter has a 48-bit status
field and optionally two control values and two acquisition
values, integer or floating point. For example a parameter
representing a magnet power supply typically includes thestatus field, one control value (current) and two acquisition

values (current and voltage), whereas a vacuum valve
parameter only has the status field. C and FORTRAN
libraries are already available for writing control systemapplications and now we are adding Tcl.
The GUI based high-level application programs requiredfor day-to-day operation of the TSL accelerator havetraditionally been developed by the control systemengineering group using the C programming language.
Writing GUI based programs for the X-windows system is
a complicated and time consuming task, even with the helpof GUI builder tools. The installation of a Tcl/Tk API to thecontrol system has radically changed this. High level
widget extensions to the Tk tool-kit makes it possible todevelop GUI based applications in a fraction of the time it
used to take to do the same in C [3,4].
2 Implementation
Third party extensions to Tcl called packages1 can bewritten in Tcl or in compiled code where Tcl's C language
API provides access to the Tcl interpreter. A function
written in compiled code can be compiled and linked
together with the Tcl library to provide a new Tcl-shell withan extended command set. As the number of packages in aTcl installation increases, the number of used combinationsof Tcl-shells with different extensions included can rise to apoint that makes maintenance a nightmare. This wasobserved by the developers of Tcl and as of version 7.5
packages in compiled code can be loaded on demand by
the running application2. The mechanism used toimplement this is the dynamic linking and loading ofshared libraries found in many modern operating systems.
Unfortunately the implementation of dynamic loading isvery platform dependent [5]. Particularly it can be difficult
to load packages written in C++ (which happens to be thelanguage of choice for the control system Tcl API package)since global objects depend on constructor functions being
called at program startup and the corresponding destructor
functions being called at termination. Another difficulty isthat the C++ run-time library is not linked into the Tcl/Tkshells from the standard distribution. The plus patches [6]1 Extensions built as packages follow a set ofprogramming rules to avoid interference with otherpackages [1].
2 Packages written in Tcl could be dynamically loaded
before version 7.5.

is a set of patches to the Tcl/Tk distribution that enablesloading of C++ packages.
2.1 The Tcl APIThe Tcl API uses the object-oriented approach [1] whenadding commands for accessing control system parameters.
A new Tcl command is created for each parameter theapplication program needs to access. In the example below
the cs_par command is used to create a new Tcl commandfor parameter Q_A1. The new command is given the same
name as the parameter and the first argument to thecommand specifies the action to perform.
% cs_par Q_A1=> Q_A1% Q_A1 c1_read=> 35.2

The parameter-commands have a number of options that
control return format, update method, error reporting, etc.
The Tcl API uses an option-handling model where default
values for options are inherited from parent to child when aparameter-command is created. The Tcl interpreter is theparent of the new command and the command is the parentof six children corresponding to actions performed on theparameter.

3.1 Radiation protection interlock system
One system where Tix is used is the RPI system. Thissystem supervises the laboratory localities to ensure
personnel safety during accelerator operation. The RPI
divides localities into areas that can be sealed-off before
the beam is allowed to enter. The number and types of RPI
sensors/actors varies in each area, but the control system
API allows the application program to query the control
system database about each area configuration. The Tcl/Tkuser interface uses this information to build an area control
panel at run time. The Tk packer geometry manager is ideal
for this situation, since it automatically adjusts size andposition of the control panel widgets when theconfiguration changes. The Tix-package has been used toconstruct a hierarchy of high level widgets that match theconfiguration of sensor/actor groups in an area. The RPI
system includes some 50 radiation detectors for measuring
the dose rate in different areas of the laboratory. The BLTpackage [9] is used to generate time plots of the dose ratemeasurements.

Interpreter->Parameter|->status|->actuation|->acquisition 1|->acquisition 2|->control 1|->control 2

The default option values can be changed with config
commands, or be overridden on the command line when
executing a parameter-command. An extensive set of
options gives the experienced programmer power to finetune the behaviour of the package, yet with sane default
values beginners get a quick start.
3 Applications
Although Tcl is great for rapid development of small
applications it’s support for structuring the code and data ispoor. This becomes more and more noticeable as theprograms grow larger. The Tix mega-widget3 package [7]has been used at TSL in the development of larger
applications. The Tix-package comes with a simple Object
Oriented Programming (OOP) framework, the Tix Intrinsic.
The Tix Intrinsic is not a general purpose OOP system suchas [Incr Tcl] [8]. It is mainly designed for writing widgets,
but it has been used successfully to encapsulate the control
system functionality directly into the widgets used forinteraction with the operator. [Incr Tcl] has not yet beenused at TSL mainly because it requires modification of theTcl core and thus can not be dynamically loaded4.
3 Widgets made out of other widgets.
4 Tcl 8.0, released Aug 97, have built-in support for namespaces. This will allow new versions of object based
systems like [Incr Tcl] to be loaded dynamically.

Fig 1. RPI system console and area sensor display.
The RPI system also has a simulation mode to simplifytesting. In simulation mode the operator can select which
sensor signals should use the actual hardware sensor ortake its value from a button or slider. The simulation mode
also makes it possible to run the RPI system on an off-line
computer with no connection to the actual sensor/actors
while testing new versions of the program.
3.2 Correlation plot facility
The availability of all control system parameters together
with a graphical user interface allows writing a simple
application that changes one parameter and monitors
various other simultaneously to determine theirdependence. The application is enhanced with a graphicaldisplay of the data taken. This code is modelled after asimilar system in the SLC control system at SLAC [10].
3.3 Heavy ion irradiation facility
Tcl/Tk has been used to control a recently constructedheavy ion irradiation facility for material science. The

facility is capable of homogeneously irradiate a 40 x 40mm sample and varying the dose rate over 6 orders ofmagnitude. Most of the hardware equipment (such as anelectrostatic deflector at the ion source, steering magnets,
shutters, beam intensity monitors and timing electronics)
was already present and connected to the TSL control
system because they were used in other experiments. Thusa major portion of the task was writing software tocoordinate the equipment actions during the irradiation
process. The only time-critical section was the scanning of
the beam over the surface of the sample. This was written
in C. All other functions, like calculation of the scan speed
and chopper frequency, calibration of the beam and putting
shutters in the right position was done in Tcl, which wasmuch faster to program.
A new version of the control system for the irradiation
facility is in development. It will have the time-criticalsection hardwired and use Tcl/Tk for all other functions.
The first prototype of the Tcl/Tk program was written
within two days. The new version will allow us tographically pre-program a complete irradiation and monitor
it's progress when running.
The final application is intended for researchers that haveto perform the irradiation but do not have any knowledge
about accelerators and thus it requires a graphical userinterface that is foolproof and user friendly.
3.4 Front-end to other applications
Applications written in C or FORTRAN that sometimes
have arcane command line options were provided with aTcl/Tk front-end. This greatly increased the acceptance of
new control features by our operator community andreduced errors in running the applications.
In one case a slow steering system that monitors andcorrects the position of the proton beam for proton therapy
treatment received a Tcl face-lift. The needed Tcl code waswritten within a day and has made expert intervention
when running the system obsolete.
In another case a program that executes other programs
synchronised with injections into the CELSIUS storage
ring, which typically happen every five minutes, received aTcl front-end. It is now routinely used to deliver beam toexperiments that parasitically utilise it when CELSIUS
does not need beam for injections.
In a third case applications that provide simplified
interfaces to complex instruments connected to the control
system via GPIB such as digital oscilloscopes or network
analysers are written. These applications allow setting up
and using these devices by non-experts. The applications

are enhanced with simple data analysis and display routines
written in Tcl using the BLT-package. These codes allow
fast on-line interpretation of the accumulated data.
Consequently, data taking and analysis during accelerator
physics experiments have become faster and more reliable.
In another group of applications we use Tcl/Tk
applications to provide simple interfaces to automate
repetitive tasks that are typically done by shell scripts.
4 Conclusions
The advent of Tcl has increased the amount of useful
applications that can not only be used by their respective
authors dramatically. The availability of all control systemparameters, GPIB devices, advanced graphics, andapplications written in other languages such as C,
FORTRAN, or shell scripts under the Tcl/Tk roof of agraphical user interface provides a perfect framework forgenerating small applications that help our operators
running the cyclotron and CELSIUS more stable andreliable. The ease at which GUI based application can bebuilt with Tcl/Tk has lead to an increased interest from theoperations/physicist groups to take part in program
development for the control system.
References[1]

J. K. Ousterhout, Tcl and the Tk Toolkit, AddisonWesly Professional Computing Series 1994.
[2] K Gajewski, L Thuresson, O Johansson, Upgradingof the control system for the accelerators at TSL,ICALEPCS 1991.
[3] B. Welch, Practical Programming in Tcl and Tk
second edition, Prentice Hall 1997.
[4] Tcl/Tk
isavailablefrom
http://sunscript.sun.com/TclTkCore/[5] K. B. Kenny, Dynamic Loading for Tcl: (Whatbecame of it ?), http://ce-toolkit.crd.ge.com/
papers/gecrd/mtl/mdip/tcl94/tcl94.ps[6] Plus
patchesareavailablefrom
http://www.worldaccess.nl/~nijtmans/plus.html[7] I. K Lam, Designing Mega Widgets in the Tix
Library, Tcl/Tk Workshop 1995, Tix is available
fromhttp://www.xpi.com/tix/[8] [Incr Tcl] is available from http://www.tcltk.com/itcl/[9] BLT is available from http://www.tcltk.com/blt/[10] L. Hendrickson, N. Phinney, L. Sanches-Chopitea,
Correlation Plot Facility in the SLC control system,
PAC 1991.