\subsection{The Velocity Selector}
A velocity selector is a kind of turbine in the neutron beam. Only neutrons
which manage to travel through the device between two turbine blades make it
through the device, the rest is annihilated in the blades or the turbine
housing.  Thus, a velocity selector is a kind of monochromator. 
The behaviour of a velocity selector is determined by two
parameters: the rotation speed and the dejustment of the velocity selector
to the neutron beam, the tilt angle. The velocity
(wavelength) of the neutrons coming through is determined by the rotation speed
of the thing.  The tilt angle determines the velocity distribution function.

Velocity selectors have a few peculiarities due to the fact that they
rotate rather quickly. This may give rise to resonances at certain
rotation speeds which may damage the device and the surroundings of it. This
problem is coped with by having forbidden regions of rotation speeds which
cannot be selected. The second implication of the fast rotation is, that the
rotation needs to stop or be below a certain threshold before the tilt
angle may be changed. So, changing tilt has to go in three steps:
first stop rotation, drive tilt, accelerate rotation to the former speed.
Tilt is usually controlled by some stepper motor. 

The rotation speed of the velocity selector needs to be monitored at regular
intervalls. If it goes out of tolerances, a error handling procedure must be
initiated. Furthermore logging of the rotation speeds of the velocity
selector may be required. All this is already realised in the environment
controller code. In order to minimise duplication of code, these facilities
are implemented by installing a environment controller for the velocity
selector. This environement controller will have a dummy driver which does
not allow the velocity selector to be driven through the environment
controller. An object is installed into the SICServer with the name of the
velocity selector and watch appended to it. Through this object such things
as tolerances, error handling procedures and logging of velocity selector
values can be configured. The only task left for the velocity selector code
is to maintain the proper state of the environment controller mode at
apropriate places.  


From this introduction the necessary datastructure for this beast can be
easily deduced. Besides the usual objectdescriptor we need:
\begin{itemize}
\item A list of forbidden regions, realised as a standard linked list of
min, max values. 
\item A motor to drive the tilt with. 
\item Fields to hold rotation and tilt and some auxilliary parameters.
\item Various interrupt to trigger in case of failures. 
\item As an interface a drivable interface for the rotation is needed in 
oder to use the device executor for changing rotation speed.
\item A pointer to the environment controller object monitoring the velocity
selector.
\item A driver for actually driving the velocity selector.
\end{itemize}
A suitable datastructure  looks like this:

\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap1}
$\langle$data {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@/*------------------ The velocity selector datastructure -----------------*/ @\\
\mbox{}\verb@ @\\
\mbox{}\verb@     typedef struct __VelSel {@\\
\mbox{}\verb@                        pObjectDescriptor  pDes;@\\
\mbox{}\verb@                        char *pName;@\\
\mbox{}\verb@                        pIDrivable         pDrivInt;@\\
\mbox{}\verb@                        pICallBack         pCall;@\\
\mbox{}\verb@                        int                iForbidden;@\\
\mbox{}\verb@                        ObPar              *pPar;@\\
\mbox{}\verb@                        pMotor             pTilt;@\\
\mbox{}\verb@                        float              fRot;@\\
\mbox{}\verb@                        float              fTilt;@\\
\mbox{}\verb@                        pEVControl         pMonitor;@\\
\mbox{}\verb@                        pVelSelDriv        pDriv;@\\
\mbox{}\verb@                      } VelSel;  @\\
\mbox{}\verb@/*----------------- Forbidden region single item -------------------------*/@\\
\mbox{}\verb@    typedef struct __Verbot {@\\
\mbox{}\verb@                       float fMin;@\\
\mbox{}\verb@                       float fMax;@\\
\mbox{}\verb@                    } Verbot, *pVerbot;@\\
\mbox{}\verb@@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
The fields are:
\begin{description}
\item[pDes] The usual object descriptor.
\item[pName] The velocity selector name.
\item[pDrivInt] The drivable interface.
\item[pCall] The callback  interface.
\item[iForbidden] The handle to the lsit of forbidden regions.
\item[pPar] The velocity selector parameters.
\item[pTilt] The motor to use for tilting the turbine.
\item[pMonitor] The environment controller object representing the velocity
selector in the environment control subsystem.
\item[pDriv] The velocity selector driver.  
\end{description}
Verbot is a trivial little data structure holding a single forbidden
region for the vlocity selector. This is the structure held in the
forbidden list. Please note that the upper and lower limits of the
device are held in that list as well. 

In order to interface with this a few functions will be defined. All
functions starts with VS. Functions returning an int return 0 on failure, 1
on success for the rest of this discussion.  The first set of functions
deals with creating and configuring a velocity selector. 

\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap2}
$\langle$proto1 {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@    typedef struct __VelSel *pVelSel;@\\
\mbox{}\verb@    typedef struct __VelSelDriv *pVelSelDriv;@\\
\mbox{}\verb@@\\
\mbox{}\verb@    pVelSel VSCreate(pMotor pTilt, pVelSelDriv pDriv);@\\
\mbox{}\verb@    void    VSDestroy(void *self);@\\
\mbox{}\verb@@\\
\mbox{}\verb@    int VSAddVerbot(pVelSel self, float fMin, float fMax);@\\
\mbox{}\verb@    int VSSetPar(pVelSel self,SConnection *pCon, char *name, float fVal);@\\
\mbox{}\verb@    int VSGetPar(pVelSel self,char *name, float *fVal);@\\
\mbox{}\verb@    int VSGetRotation(pVelSel self, float *fRot);@\\
\mbox{}\verb@    int VSGetTilt(pVelSel self, float *fTilt);@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
Most functions take a pointer to a velocity selector data structure as
first parameter. This parameter is not discussed furtheron.
\begin{description}
\item[VSCreate] Creates a velocity selector object. A driver and a
motor for the tiliting operation need to be given as parameters. On
return either a pointer to a newly allocated velocity selector data
structure is returned or NULL as indication of failure. 
The velocity selector will make sure that the tilt motor is only
accessible with internal
privilege. This effectively reserves the tilt motor to the velocity
selector object. This obliges the velocity selector to set a lower
privilege 
while running that motor and to set it back again afterwards. 
The idea behind this
scheme is to prevent a user from accidentally tampering with the tilt motor
without passing through the velocity selector interface.  
\item[VSDestroy] is a function which properly frees all data
structures associated with the velocity selector. This is the function
to specify as KillFunction for the interpreter.
\item[VSAddVerbot] adds a forbidden region between fMin and fmax
rotation speeds. 
\item[VSSetPar, VSGetPar] set and retrive velocity selector
parameters. Parameter names given as parameter name are the same as in
the user documentation. 
\item[VSGetRotation] retrieves the current rotation speed.
\item[VSGetTilt] retrieves the current tilt angle.  
\end{description}

\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap3}
$\langle$protos2 {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@/*@\\
\mbox{}\verb@    int VSSetRotation(pVelSel self, SConnection *pCon, float fNew);@\\
\mbox{}\verb@    int VSSetTilt(pVelSel self, SConnection *pCon, float FNewTilt);@\\
\mbox{}\verb@*/@\\
\mbox{}\verb@    int VSSetTiltRot(pVelSel self, SConnection *pCon,@\\
\mbox{}\verb@                     float fNewRot, float fNewTilt);@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
\begin{description}
\item[VSSetTiltRot] sets both rotation speed and tilt angle to new
values.   
\end{description}

Please note, that further manipulation functions are hidden in the Drivable
interface functions for the rotation which this module defines. 


Another common task for a velocity selector is to measure its  loss current.
This is done regularly in order to catch possible problems with the
velocity selector early on. 

\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap4}
$\langle$protos4 {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@   int VSGetLossCurrent(pVelSel self, SConnection *pCon, float *fLoss);@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
Last not least a velocity selector needs an interface to the interpreter as
well. As usual there are two functions: a creation function and a bigger
action function which takes the widget commands. 

\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap5}
$\langle$protos3 {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@     int VelSelFactory(SConnection *pCon, SicsInterp *pSics, void *pData,@\\
\mbox{}\verb@                       int argc, char *argv[]);@\\
\mbox{}\verb@     int VelSelAction(SConnection *pCon, SicsInterp *pSics, void *pData,@\\
\mbox{}\verb@                       int argc, char *argv[]);@\\
\mbox{}\verb@ @\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
\subsubsection{The velocity selector driver}
As usual for hardware devices in SICS the velocity selector consists of the
logical selector described above and a hardware driver which takes care of
simulating actual hardware or talking to it. This driver is described here. 
\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap6}
$\langle$Driver {\footnotesize ?}$\rangle\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@    typedef struct __VelSelDriv {@\\
\mbox{}\verb@                                  void *pPrivate;@\\
\mbox{}\verb@                                  void (*DeletePrivate)(void *pData);@\\
\mbox{}\verb@                                  float fTolerance;@\\
\mbox{}\verb@                                  int (*Halt)(pVelSelDriv self);@\\
\mbox{}\verb@                                  int (*GetError)(pVelSelDriv self,@\\
\mbox{}\verb@                                                  int *iCode, char *pError,@\\
\mbox{}\verb@                                                  int iErrlen);@\\
\mbox{}\verb@                                  int (*TryAndFixIt)(pVelSelDriv self,@\\
\mbox{}\verb@                                                     int iCode);@\\
\mbox{}\verb@                                  int (*GetRotation)(pVelSelDriv self,@\\
\mbox{}\verb@                                                     float *fRot);@\\
\mbox{}\verb@                                  int (*SetRotation)(pVelSelDriv self,@\\
\mbox{}\verb@                                                     float fRot);@\\
\mbox{}\verb@                                  int (*GetStatus)(pVelSelDriv self, @\\
\mbox{}\verb@                                                   int *iCall, float *fCur);@\\
\mbox{}\verb@                                  int (*GetDriverText)(pVelSelDriv self,@\\
\mbox{}\verb@                                                       char *pText, @\\
\mbox{}\verb@                                                       int iTextLen);@\\
\mbox{}\verb@                                  int (*GetLossCurrent)(pVelSelDriv self,@\\
\mbox{}\verb@                                                        float *fLoss);@\\
\mbox{}\verb@                                  int (*Init)(pVelSelDriv self, @\\
\mbox{}\verb@                                             SConnection *pCon);@\\
\mbox{}\verb@                                }VelSelDriv;   @\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-1ex}
\footnotesize\addtolength{\baselineskip}{-1ex}
\begin{list}{}{\setlength{\itemsep}{-\parsep}\setlength{\itemindent}{-\leftmargin}}
\item Macro referenced in scrap ?.
\end{list}
\end{minipage}\\[4ex]
\end{flushleft}
pPrivate is an area where individual drivers may store their necessary data.
DeletePrivate is a function which will be automatically called when the
driver is destroyed with pPrivate passed as parameter. This functions
purpose is to destroy pPrivate and remove all memory associated with it.
Each driver is resonsible for filling the right thing in here.

The description of the rest of the functions is very much like as described
for the motor interface. Possible return values for GetStatus can be:
\begin{itemize}
\item VSNOCON: connection to velocity selector lost.
\item VSACCEL: selector accelerating or breaking.
\item VSFAIL: velocity selector failure.
\item VSOK:   all fine, at requested velocity.
\end{itemize}

Special is the GetDriverText which is meant to
return a formatted string of hardware specific information from the device.
This is put into pText, but maximum iTextlen characters. 

Another special is the GetLossCurrent. This is a special self test of the
velocity selector which helps to identify possible problems with the device
early on. This test must be  made regularly and, of course,  the driver must
support it.

Init is meant to do what it says, initialize the velocity selector. This is
here in order to support restarting the velocity selector in case of a
problem.

At the time of writing (Juli 1997) two drivers for velocity selectors exist:
 a simulation driver and a driver for a Dornier velocity selector run
through a Macintosh--PC terminal server communicating with the velocity
selector control PC. Please note, that the Dornier program had been slightly
modified in order to make this scheme work.

\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap7}
\verb@"velo.h"@ {\footnotesize ? }$\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@/*-------------------------------------------------------------------------@\\
\mbox{}\verb@@\\
\mbox{}\verb@                V E L O C I T Y   S E L E C T O R@\\
\mbox{}\verb@@\\
\mbox{}\verb@  Header file for the SICS velocity selector module. For documentation@\\
\mbox{}\verb@  see velo.w.@\\
\mbox{}\verb@@\\
\mbox{}\verb@  Mark Koennecke, Juli 1997@\\
\mbox{}\verb@@\\
\mbox{}\verb@  copyright: see implementation file@\\
\mbox{}\verb@-----------------------------------------------------------------------------*/@\\
\mbox{}\verb@#ifndef SICSVELO@\\
\mbox{}\verb@#define SICSVELO@\\
\mbox{}\verb@@\\
\mbox{}\verb@/*------------------------- live & death & shape ------------------------*/@\\
\mbox{}\verb@@$\langle$proto1 {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@/*------------------------- drive around -----------------------------------*/@\\
\mbox{}\verb@@$\langle$protos2 {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@@$\langle$protos4 {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@/*------------------------- Interpreter interface ------------------------*/@\\
\mbox{}\verb@@$\langle$protos3 {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@#endif@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-2ex}
\end{minipage}\\[4ex]
\end{flushleft}
\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap8}
\verb@"velo.i"@ {\footnotesize ? }$\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@/*--------------------------------------------------------------------------@\\
\mbox{}\verb@@\\
\mbox{}\verb@  Internal header file describing the velocity selector data structure.@\\
\mbox{}\verb@@\\
\mbox{}\verb@  Mark Koennecke, Juli, 1997@\\
\mbox{}\verb@@\\
\mbox{}\verb@----------------------------------------------------------------------------*/@\\
\mbox{}\verb@#ifndef VELOINTERNAL@\\
\mbox{}\verb@#define VELOINTERNAL@\\
\mbox{}\verb@@$\langle$data {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@#define VELOREDO  2@\\
\mbox{}\verb@#define VELOFAIL  0@\\
\mbox{}\verb@#define VELOOK    1@\\
\mbox{}\verb@@\\
\mbox{}\verb@#define VSNOCON 0@\\
\mbox{}\verb@#define VSOK    1@\\
\mbox{}\verb@#define VSACCEL -7@\\
\mbox{}\verb@#define VSFAIL -2@\\
\mbox{}\verb@@\\
\mbox{}\verb@#endif@\\
\mbox{}\verb@@\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-2ex}
\end{minipage}\\[4ex]
\end{flushleft}
\begin{flushleft} \small
\begin{minipage}{\linewidth} \label{scrap9}
\verb@"velodriv.h"@ {\footnotesize ? }$\equiv$
\vspace{-1ex}
\begin{list}{}{} \item
\mbox{}\verb@@\\
\mbox{}\verb@/*--------------------------------------------------------------------------@\\
\mbox{}\verb@        V E L O C I T Y   S E L E C T O R  D R I V E R@\\
\mbox{}\verb@@\\
\mbox{}\verb@  Header file for the velocity selector driver and its related functions.@\\
\mbox{}\verb@@\\
\mbox{}\verb@  Mark Koennecke, Juli 1997@\\
\mbox{}\verb@  copyright: see implementation file@\\
\mbox{}\verb@-----------------------------------------------------------------------------*/@\\
\mbox{}\verb@#ifndef VELODRIV@\\
\mbox{}\verb@#define VELODRIV@\\
\mbox{}\verb@#include <tcl.h>@\\
\mbox{}\verb@@$\langle$Driver {\footnotesize ?}$\rangle$\verb@@\\
\mbox{}\verb@/*-------------------- live & death ----------------------------------------*/@\\
\mbox{}\verb@  pVelSelDriv VSCreateSim(void);@\\
\mbox{}\verb@  pVelSelDriv VSCreateDornierSINQ(char *name,Tcl_Interp *pTcl);@\\
\mbox{}\verb@@\\
\mbox{}\verb@  void        VSDeleteDriver(pVelSelDriv self);@\\
\mbox{}\verb@@\\
\mbox{}\verb@#endif @\\
\mbox{}\verb@@$\diamond$
\end{list}
\vspace{-2ex}
\end{minipage}\\[4ex]
\end{flushleft}
