esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

global FAST_SHUTTER_STATE
global long  array musstdat[100000][5]	
global float array musstemp[100000][7]


FAST_SHUTTER_STATE = "unknown"


def musstPX_init(phi_steps) '{
   musst_comm("ABORT")

   musst_comm(sprintf("CH PHI %d", phi_steps))
}'

def musstPX_loadprog '{
   constant MUSSTPROG "~blissadm/local/isg/musst/oscillPX.mprg"

   local isgname
   local clear

   clear = 1
   isgname = MUSST_AUX["default"]

   musst_comm("ABORT")

   musst_upload_program(isgname, MUSSTPROG, clear)
}'

def musstPX_oscill(e1, e2, esh1, esh2, delta, mar_readout) '{
   local comm do_mar_readout aux

   if (e1 > e2) {
      aux = e1
      e1 = e2
      e2 = aux
      aux = esh1
      esh1 = esh2
      esh2 = aux
   }

   #if (mar_readout == 1) {
   #  do_mar_readout = 0
   #} else {
   #  do_mar_readout = 3
   #} 

   musst_comm("ABORT")
   musst_comm(sprintf("VAR E1 %d", e1))
   musst_comm(sprintf("VAR E2 %d", e2))
   musst_comm(sprintf("VAR ESH1 %d", esh1))
   musst_comm(sprintf("VAR ESH2 %d", esh2))
   musst_comm(sprintf("VAR DE %d", delta))

   if ( mar_readout == 1 ) {
      musst_comm(sprintf("VAR MAROSC 1"))
   } else {
      musst_comm(sprintf("VAR MAROSC 0"))
   }  

   #print "MAR READOUT VAL=" do_mar_readout
   #musst_comm(sprintf("VAR DO_MAR_READOUT %d", do_mar_readout))
 
   musst_comm("RUN OSCILLPX")
}'


def musstPX_getdata(mdat) '{
   local npoints nval

   npoints = musstPX_done()
   nval = 5
#   long array mdat[npoints][nval]
   isgname = MUSST_AUX["default"]
   n = musst_getdata(isgname, npoints, nval, mdat)
   if (n != npoints) {
      print "WHAT?? (MUSST speaking)" 
      return(0)
   } else {
      return(n)
   }
}'


def musstPX_done() '{
   return(musst_comm("?VAR NPOINTS"))
}'


def musstPX_read(OSCIL_MOTOR_NUM, musstvel) '{
    local MUSST_NDAT npts step_size msg retpts

    musst_comm("ABORT")
    MUSST_NDAT = musstPX_done()
    npts = MUSST_NDAT

    if (MUSST_NDAT == 0) {
        msg = "Ooops... no oscillation data from MUSST"
        print msg
        return(-1) 
    } else if (MUSST_NDAT > 100000) {
        msg = "Ooops... too much oscillation data from MUSST"
        print msg
        return(-1) 				
		} else {
       #
       # get the data from musst
       #
       retpts = musstPX_getdata(musstdat)       

       if ( retpts == 0 ) {
           msg = "Error reading data from MUSST"
           print msg
           return(-1) 
       } 

       step_size = motor_par(OSCIL_MOTOR_NUM, "step_size")

       #
       # first column in temp contains time in ms
       #
       musstemp[0:npts-1][0]  = musstdat[0:npts-1][0]
       musstemp[0:npts-1][0]  = musstemp[0:npts-1][0] / 1000.0
     
       #
       # velocity in 
       #     v(i) = [ x(i) - x(i-1) ] /  [ t(i) - t(i-1) ]
       # then convert from steps / microsec into deg/sec 
       #
       musstemp[1:npts-1][1] = musstdat[1:npts-1][1] - musstdat[0:npts-2][1]
       musstemp[1:npts-1][1] /= ( musstdat[1:npts-1][0] - musstdat[0:npts-2][0] +1)

       musstemp[1:npts-1][1] = musstemp[1:npts-1][1] * 1e6 / step_size
       musstemp[0][1]        = 0

       #
       # save position in degrees
       #
       musstemp[0:npts-1][2] = musstdat[0:npts-1][1]
       musstemp[0:npts-1][2] /= step_size
       
       # I0 values ( +- 10V in 32bits )
       musstemp[0:npts-1][3] = musstdat[0:npts-1][3]
       musstemp[0:npts-1][3] /= 0x7FFFFFFF

       musstemp[0:npts-1][3] *= 10


       # I1 values
       musstemp[0:npts-1][4] = musstdat[0:npts-1][2]
       musstemp[0:npts-1][4] /= 0x7FFFFFFF
       musstemp[0:npts-1][4] *= 10

       # 6th value in musstdat array is shutter command
       #
       musstemp[0:npts-1][5] = 1 - ((musstdat[0:npts-1][4] & 0x0100) / 0x0100)

       # 7th extra column reads shutter status 
       musstemp[0:npts-1][6] = (musstdat[0:npts-1][4] & 0x0001) / 0x0001

       musstvel = musstemp[0:npts-1]

       return( npts )
   }
}'


def musstPX_shopen() '{
   if (musst_comm("?IO SHUTSTATE")==0) {
     musst_comm("IO ~SHUTTER")
   }
}'


def musstPX_shclose() '{
   if (musst_comm("?IO SHUTSTATE")==1) {
     musst_comm("IO SHUTTER")
   }
}'


def musstPX_getphi() '{
   local phi_steps

   sscanf(musst_comm("?VAL PHI"), "%f", phi_steps)
   return(phi_steps)
}'

def msopen '{
  FAST_SHUTTER_STATE = "moving"
  printf("Fast shutter was %s ,", _msstate_lbl())
  musstPX_shopen()
  sleep(0.01)
  FAST_SHUTTER_STATE =  _msstate_lbl()
  printf(" now is %s.\n", FAST_SHUTTER_STATE)
}'

def msclose '{
  FAST_SHUTTER_STATE = "moving"
  printf("Fast shutter was %s ,", _msstate_lbl())
  musstPX_shclose()
  sleep(0.01)
  FAST_SHUTTER_STATE =  _msstate_lbl()
  printf(" now is %s.\n", FAST_SHUTTER_STATE)
}' 


def _msstate() '{
   return(musst_comm("?IO SHUTSTATE"))
}'


def msstate '{
   printf("Fast shutter is %s\n", _msstate_lbl())
}'


def _msstate_lbl() '{
   return(_msstate() ? "OPEN" : "CLOSED")
}'


def musstPX_update_msstate '{
  FAST_SHUTTER_STATE = _msstate_lbl()
}'