s1cy

This VmExecutor controls the vertical center position.

#!/bin/env python

# File name: /usr/local/experiment/Tango/VmCode/s1cy.py

import PyTango
import math

## Detector Slit Horizontal Center
##
## This motor does the combined move of two motors
## to control the vertical centering of the slits

MOTOR_TOP    = "p99/motor/d1.01"
MOTOR_BOTTOM = "p99/motor/d1.02"

class VM:
    #
    # init_device
    #
	def __init__( self):
		self.PositionSim = 0
		self.ResultSim = []
		self.ResultSim.append("Vertical: PositionSim not set")
		self.ResultSim.append("slit_left: PositionSim not set")
		self.ResultSim.append("slit_right: PositionSim not set")
		self.proxies = []
		self.proxies.append( PyTango.DeviceProxy( MOTOR_TOP)) 
		self.proxies.append( PyTango.DeviceProxy( MOTOR_BOTTOM))
		return
	#
	# dev_state
	#
	def dev_state( self):
		argout = PyTango.DevState.ON
		#
		# if one device is in FAULT the VM is in FAULT too
		#
		for proxy in self.proxies:
			if proxy.state() == PyTango.DevState.FAULT:
				argout = PyTango.DevState.FAULT
				break
		if argout == PyTango.DevState.ON:
			#
			# if one device is MOVING the VM is MOVING too
			#
			for proxy in self.proxies:
				if proxy.state() == PyTango.DevState.MOVING:
					argout = PyTango.DevState.MOVING
					break
			
		return argout
	#
	# Position
	#
	def read_Position( self):
		position = self.calc_pos()
		return position

	def write_Position( self, argin):
		pos_top = self.proxies[0].Position
		pos_bottom = self.proxies[1].Position

		delta = self.calc_delta(argin)

		self.proxies[0].Position = pos_top + delta
		self.proxies[1].Position = pos_bottom + delta

		return 1

	#
	# UnitLimitMax
	#
	def read_UnitLimitMax( self):
	        pos_top = self.proxies[0].Position
		pos_bottom = self.proxies[1].Position
		center=(pos_top+pos_bottom)/2.
		check_limit_top = self.proxies[0].UnitLimitMax - pos_top
		check_limit_bottom = self.proxies[1].UnitLimitMax - pos_bottom
		if check_limit_top < check_limit_bottom:
			limit_max = center + check_limit_top
		else:
			limit_max = center + check_limit_bottom
		return limit_max
	
	def write_UnitLimitMax( self, argin):
		pass
		#PyTango.Except.throw_exception( "vm", "UnitLimitMax can not be written", "VmExecutor")
	#
	# UnitLimitMin
	#
	def read_UnitLimitMin( self):
		pos_top = self.proxies[0].Position
		pos_bottom = self.proxies[1].Position
		center=(pos_top+pos_bottom)/2.
		check_limit_top = self.proxies[0].UnitLimitMin - pos_top
		check_limit_bottom = self.proxies[1].UnitLimitMin - pos_bottom
		if check_limit_top < check_limit_bottom:
			limit_min = center + check_limit_top
		else:
			limit_min = center + check_limit_bottom
		return limit_min
	
	def write_UnitLimitMin( self, argin):
		pass		
		#PyTango.Except.throw_exception( "vm", "UnitLimitMin can not be written", "VmExecutor")
	#
	# CwLimit, CcwLimit
	#
	def read_CwLimit( self):
		argout = 0
		#
		# if one of the motors is in the limit return 1
		#
		for proxy in self.proxies:
			if proxy.CwLimit == 1:
				argout = 1
				break
		return argout

	def read_CcwLimit( self):
		argout = 0
		#
		# if one of the motors is in the limit return 1
		#
		for proxy in self.proxies:
			if proxy.CcwLimit == 1:
				argout = 1
				break
		return argout

	#	
	# PositionSim	
	#	
	def read_PositionSim( self):
		return self.PositionSim

	def write_PositionSim( self, argin):
		self.PositionSim = argin

	def read_ResultSim( self):


		pos_top = self.proxies[0].Position
		pos_bottom = self.proxies[1].Position

		delta = self.calc_delta(self.PositionSim)

		self.ResultSim[0] = "Horizontal Centering: %g " % self.PositionSim
		self.ResultSim[1] = "slit_top: %g " % (pos_top + delta)
		self.ResultSim[2] = "slit_bottom: %g " % (pos_bottom + delta)
		
		return self.ResultSim

	def StopMove( self):
		for proxy in self.proxies:
			proxy.StopMove()
		return 1
	
	def Calibrate(self, argin):
		pos_top = self.proxies[0].Position
		pos_bottom = self.proxies[1].Position

		delta=self.calc_delta(argin)

		newpos_top = pos_top + delta
		newpos_bottom =  pos_bottom + delta

 		self.proxies[0].Calibrate(newpos_top)
		self.proxies[1].Calibrate(newpos_bottom)
			
		return 1
		
	def calc_pos(self):
		p1 = self.proxies[0].Position
		p2 = self.proxies[1].Position

		pos = (p1 + p2)/2.
	
		return pos
		
	def calc_delta(self, argin):
		pos = self.calc_pos()
		delta = argin - pos
		return delta