#
# $Log: not supported by cvs2svn $
# Revision 1.5  2002/10/22 02:01:12  shang
# added plane option to open RTFB loops
#
# Revision 1.4  2002/10/22 01:33:15  shang
# added pendIOTime to cavput command
#
# Revision 1.3  2002/10/22 01:28:46  shang
# "fixed more typo errors"
#
# Revision 1.2  2002/10/22 01:24:10  shang
# fixed misspelled errors
#
# Revision 1.1  2002/10/22 01:08:23  shang
# first version, without test
##
# First version  Hairong Shang
#

proc APSMpReturnToVectorInfo {} {
    return "This procedure is used to set the correctors to scalar mode while running in vector mode. The corrector is set to scalar mode which causes the problem that values in corrector and BPM setpoint vectors are diverging though the real orbit is not."
}

proc APSMpReturnToVector {args} {
    #following steps are used for restoring orbit
    #1) Check if controllaw is running in vector mode
    #2) abort the runcontrol pv in the chosen plane, the set the dp runControl pv to resume mode
    #3) open the RT feedback loops
    #4) disable vector mode in the chosen plane
    #5) set RTFB bpm setpoints to zero for that plane
    #6) close RT feedback loops
    #7) pop up dialog box to remind user to start SROrbitControllaw
    set paramFile /home/helios/oagData/sr/rtfeedback/parameterFiles/srfb-default.param
    set plane ""
    APSMpStep init -promptDialog APSMpReturnToVectorInitDialog \
      -vars {plane} -proc APSMpReturnToVector
    if ![string length $plane] {
        APSStrictParseArguments {plane}
        if ![string length $plane] {
            APSMpReturn error "plane is not choosen"
        }
    }
    switch $plane {
        h {
            set Coord X
        }
        v {
            set Coord Y
        }
        default {
            APSMpReturn error "Plane should be h or v!"
        }
    }
    APSMpStep "Checking if dp controllaw is running in $plane plane."
    if [catch {exec cavget -list=DP:S:OrbitControlLaw${Coord}SDDS.RUN -pend=30} dpRun] {
        APSMpReturn error "APSReturnToVector: $dpRun"
    }
    if !$dpRun {
        APSMpReturn error "controllaw is not running vector mode, no need to run this procedure!"
    }
    APSMpStep "Aborting dp runcontrol pv."
    if [catch {exec cavput -list=DP:S:OrbitControlLaw${Coord}SDDS.ABRT=1 -pend=30} result] {
        APSMpReturn error "APSReturnToVector: $result"
    }
    APSMpStep "Setting dp runcontrol pv to resume state"
    if [catch {exec cavput -list=DP:S:OrbitControlLaw${Coord}SDDS.SUSP=0 -pend=30} result] {
        APSMpReturn error "APSReturnToVector: $result"
    }
    APSMpStep "Load Parameters"
    if [catch {APSReadExecuteParamFile -filename $paramFile -readonly 1} result] {
        APSMpReturn error "APSReturnToVector: $result"
    }
    APSMpStep "Open RTFB loops"
    if [catch {APSOpenRTFBLoops -plane $plane} result] {
        APSMpReturn error "APSReturnToVector: $result"
    }
    APSMpStep "Set correctors to scalar mode."
    if [catch {APSSetCorrMode -corrMode scalar -plane $plane} result] {
        APSMpReturn error "APSReturnToVector: $result"
    }
    APSMpStep "Set BPM setpoints to zero."
    if [catch {APSZeroRTBPMSetpoints -plane $plane} result] {
        APSMpReturn error "APSReturnToVector: $result"
    }
    APSMpStep "Close RTFB loops and Ramping high pass filter"
    if [catch {APSMpCloseRTFBLoops -plane $plane} result] {
        APSMpReturn error "APSReturnToVector: $result"
    }
    #pop up reminding dialog box
    set answer [tk_dialog2 .remind "Start OrbitControllaw" \
		    "Press start button in SROrbitControllaw to restart data pool orbit correction" \
		    warning -1 ok cancel]
    
    APSMpReturn ok
}

proc APSMpReturnToVectorInitDialog {frame} {
    global APSMpReturnToVector
    set APSMpReturnToVector(plane) ""
    
    pack [frame $frame.f1]    
    APSRadioButtonFrame .plane -parent $frame.f1 -variable APSMpReturnToVector(plane) \
      -buttonList {Horizontal Vertical} -valueList {h v} \
      -label "Choose plane:   " -orientation horizontal \
      -contextHelp "choose plane for returning to vector mode"  
    APSMpReturn ok
}
