esrf

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


WBV_MAXI24V = 8
WBV_MAXO24V = 8

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bv '{
   local par BV_IN BV_OUT BV_LED
   local vals[]
    
   # PC code:
   if(0) {
   array vals[WBV_MAXI24V + WBV_MAXO24V]
   } 

   if ("$#" == 0)
      par = "STATUS"
   else 
      par = "$1"
   
   if (par == "dbg") {
      BV_DBG = 1
      }
   else if (par == "nodbg") {
      BV_DBG = 0
      }
	else if ((par == "IN") || (par == "in")) {
		par = "IN"
      } 
   else if ((par == "OUT") || (par == "out")) {
		par = "OUT"    
	   }
   else if ((par == "STATUS") || (par == "status")) {
		par = "STATUS"    
	   }
   else if ((par == "ON") || (par == "on")) {
		par = "ON"    
	   }
   else if ((par == "OFF") || (par == "off")) {
		par = "OFF"
      }
   else if (!((par == "1mA")   || (par == "100uA") ||\
              (par == "10uA")  || (par == "1uA")   ||\
              (par == "100nA") || (par == "10nA") )) {
      print "ERROR: Command not recognized"
      return
      }    

   vals = bvstatus()
   BV_IN  = vals[0]
   BV_OUT = vals[1]
   BV_LED = vals[2]
   
    if (par == "STATUS") 
        bvpstatus()

    if (par == "IN") {
        if (BV_IN & !BV_OUT) {
			printf("BeamViewer is already IN the beam !\n")
			}	
     	else if (!BV_IN & BV_OUT) {
			printf("BeamViewer was OUT of the beam ...\n")
			if (bvinsert()) {
				printf("... and is now IN the beam.\n")
				}
			else printf("ERROR : BeamViewer didn\'t reach final position\n")
			}
		else printf("ERROR : BeamViewer is currently in an undefined state (neither IN nor OUT)\n")
		}

	if (par == "OUT") {
		if (!BV_IN & BV_OUT) {
			printf("BeamViewer is already OUT of the beam !\n")
			}
     	else if (BV_IN & !BV_OUT) {
			printf("BeamViewer was IN the beam ...\n")
			if (bvremove()) {
				print "... and is now OUT of the beam."
				}
			else printf("ERROR : BeamViewer didn\'t reach final position\n")	
			}
      else printf("ERROR : BeamViewer is currently in an undefined state (neither IN nor OUT)\n")
		}

	if (par == "ON") {
      if (BV_LED)
			printf("Illuminator is already %s\n", par)           
 		else {         
			if (bvsetledon())
				printf("Illuminator is now ON\n")
			else printf("ERROR Illuminator\n")
			}
      } 

   if (par == "OFF") {
      if (!BV_LED)        
			printf("Illuminator is already %s\n", par)
		else {          
         if (bvsetledoff())
            printf("Illuminator is now OFF\n")
         else printf("ERROR Illuminator\n")
         }
      }

   if (par == "1mA") 
      _bvsetgain(6)
	
   if (par == "100uA")
      _bvsetgain(5)
	
   if (par == "10uA")
      _bvsetgain(4)
	
   if (par == "1uA")
      _bvsetgain(3)
	
   if (par == "100nA")
      _bvsetgain(2)

   if (par == "10nA")
     _bvsetgain(1)
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def _bvsetgain(gain) '{
       
   BV_GAIN = gain
    
   if (BV_GAIN == "6") {
      wwrite o24v[4] 1   # K1             
      wwrite o24v[5] 0   # K2             
      wwrite o24v[6] 1   # K3
      printf("range Current: 1 mA (gain = 6)\n")
      } 
   else if (BV_GAIN == "5") {
      wwrite o24v[4] 0   # K1             
      wwrite o24v[5] 0   # K2             
      wwrite o24v[6] 1   # K3        
      printf("range Current: 100uA (gain = 5)\n")
      }
   else if (BV_GAIN == "4") {
      wwrite o24v[4] 1   # K1
      wwrite o24v[5] 1   # K2                                     
      wwrite o24v[6] 0   # K3
      printf("range Current: 10uA (gain = 4)\n")
      }
   else if (BV_GAIN == "3") {
      wwrite o24v[4] 0   # K1
      wwrite o24v[5] 1   # K2                         
      wwrite o24v[6] 0   # K3
      printf("range Current: 1 uA (gain = 3)\n")
      }
   else if (BV_GAIN == "2") {
      wwrite o24v[4] 1   # K1
      wwrite o24v[5] 0   # K2
      wwrite o24v[6] 0   # K3
      printf("range Current: 100 nA (gain = 2)\n")
      }
   else if (BV_GAIN == "1") {
      wwrite o24v[4] 0   # K1                         
      wwrite o24v[5] 0   # K2
      wwrite o24v[6] 0   # K3
      printf("range Current: 10 nA (gain = 1)\n")
      }
   else {        
      printf("ERROR setting gain %s\n", BV_GAIN)
      wwrite o24v[4] 1   # K1                         
      wwrite o24v[5] 0   # K2
      wwrite o24v[6] 1   # K3
      }    
               
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bvstatus() '{
   local ds key
   local vals_in[] vals_out[] vals[]

   # PC code: non portable
   if(0) {
   array vals_in[BV_MAXI24V] vals_out[BV_MAXI24V]
   array vals[BV_MAXI24V + BV_MAXO24V]

   key = 1
   ds = "isg/wcesla/wc"
   esrf_io(ds,"DevReadNoCachePhys", key, vals_in)        
   esrf_io(ds,"DevReadNoCachePhys", key+1, vals_out)        
   } 

   wago_readch("i24v", vals_in)
   wago_readch("o24v", vals_out)
   

   for (i = 0; i < 16; i++) {
      if (i < 8)
         vals[i] = vals_in[i]
      else
         vals[i] = vals_out[i-8]    
   }

   if (BV_DBG)
      printf("Status Wago_750-436 Inputs : In[0] = %d / Out[1] = %d / Led[2] = %d\n",\
    	   vals[0], vals[1], vals[2])

   return vals
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bvpstatus() '{
   local BV_IN BV_OUT BV_LED
   local BV_K1 BV_K2 BV_K3
   local BV_CURR 
   if(0) {
   array vals[BV_MAXI24V + BV_MAXO24V]
   }
   local vals[]

   vals = bvstatus()    
    
   BV_IN  =  vals[0]
   BV_OUT =  vals[1]
   BV_LED =  vals[2]
   # vals[3] : not used anymore (WBV_REM)
   BV_K1 =  vals[12]
   BV_K2 =  vals[13]
   BV_K3 =  vals[14]

   # Gain status:   
   if (!BV_K1 & !BV_K2 & !BV_K3) {
      BV_GAIN = 1, BV_CURR = "10nA"
   } else if (BV_K1 & !BV_K2 & !BV_K3) {
      BV_GAIN = 2, BV_CURR = "100nA"
   } else if (!BV_K1 & BV_K2 & !BV_K3) {
      BV_GAIN = 3, BV_CURR = "1uA"    
   } else if (BV_K1 & BV_K2 & !BV_K3) {
      BV_GAIN = 4, BV_CURR = "10uA"    
   } else if (!BV_K1 & !BV_K2 & BV_K3) {
      BV_GAIN = 5, BV_CURR = "100uA"
   } else if (BV_K1 & !BV_K2 & BV_K3) {            
      BV_GAIN = 6, BV_CURR = "1mA"
   }

   printf("\n---------------------------\n")
   # Gain status:
   printf(" %-20s : %-3s (gain = %d)\n", "PhotoDiode I range", BV_CURR, BV_GAIN)
   
    # Cylinder status:
    if (BV_IN & !BV_OUT) {
        printf(" %-20s : %-3s\n", "Cylinder","IN")
    	} 
    else if (!BV_IN & BV_OUT) {
        printf(" %-20s : %-3s\n", "Cylinder","OUT")
	}
    else 
        printf(" %-20s : %-3s\n", "Cylinder","ERROR : Check valve")

    # Led status:
    printf(" %-20s : %-3s\n", "Illuminator",BV_LED ? "ON" : "OFF")
    printf("---------------------------\n\n")
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bvinsert() '{
    if(0) {
    printf("\ntotototototototototo\n")
    array vals[BV_MAXI24V + BV_MAXO24V]
    }
    local vals[]

    wwrite o24v[1] 0 
    wwrite o24v[1] 1 
    wwrite o24v[1] 0   
    sleep(3)  # let\'s time to pneumatic reachs destination
    vals = bvstatus() 
    return (vals[0] & !vals[1])
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bvremove() '{
    if(0) {
    array vals[BV_MAXI24V + BV_MAXO24V]
    }
    local vals[]

    wwrite o24v[0] 0   
    wwrite o24v[0] 1   
    wwrite o24v[0] 0   
    sleep(3)  # let\'s time to pneumatic reachs destination
    vals = bvstatus()
    return (!vals[0] & vals[1])
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bvsetled '{
	local par BV_LED
    	if(0) {
    	array vals[BV_MAXI24V + BV_MAXO24V]
    	}
    	local vals[]

	if($# != 1) {
		p "Usage: $0 ON|OFF"
		exit
        }
    
	vals = bvstatus()  
	BV_LED = vals[2]  
        
	par = "$1"	# 1rst argument of command [on/off]

	if ((par == "ON") || (par == "on")) {
		par = "ON"
		} 
	else if ((par == "OFF") || (par == "off")) {
		par = "OFF"    
		} 
	else {
        	print "ERROR: Command not recognized"
		return
    	}  
    
	if (par == "ON") {
		if (BV_LED)
			printf("Illuminator is already %s\n", par)           
 		else {         
			if (bvsetledon())
				print "Illuminator is now ON"
			else	print "ERROR Illuminator"
			}
		} 
	else if (par == "OFF") {
        	if (!BV_LED)        
			printf("Illuminator is already %s\n", par)
		else {          
 			if (bvsetledoff())
				print "Illuminator is now OFF"
			else	print "ERROR Illuminator"
    			}
		}	
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bvsetledon() '{
    if(0) {
    array vals[BV_MAXI24V + BV_MAXO24V]
    }
    local vals[]

    wwrite o24v[2] 0   
    wwrite o24v[2] 1   
    wwrite o24v[2] 0  
    sleep(1) 
    vals = bvstatus()
    return (vals[2])    
}'

# /users/opisg/test/isdd/wbv/wbv_ctl_PhCh.mac
def bvsetledoff() '{
    if(0) {
    array vals[BV_MAXI24V + BV_MAXO24V]
    }
    local vals[]

    wwrite o24v[3] 0
    wwrite o24v[3] 1   
    wwrite o24v[3] 0
    sleep(1)
    vals = bvstatus()
    return (!vals[2]) 
}'