# This program runs the booster corrector initialization
# procedure for the horizontal/vertical/both supplies.
# This program first initiallizes all booster correctors,
# then loads a zero ramp table and activates auto mode, 
# then loads the zero ramptable again and activates it.

proc APSMpBoosterSetCorrectorInitInfo {} {
    return "This program runs the booster corrector initialization\
procedure for the horizontal/vertical/both supplies.\
This program first initiallizes all booster correctors,\
then loads a zero ramp table and activates auto mode,\
then loads the zero ramptable again and activates it."

    return
}

proc APSMpBoosterSetCorrectorInitDialog {frame} {
    APSSetVarAndUpdate status "Set Booster Corrector Init Plane(s)"
    label $frame.la1 -text "Set Booster Corrector Init Plane(s)"
    pack $frame.la1 -side top
    
    APSRadioButtonFrame .rb1 -parent $frame -label "" \
	-variable APSMpBoosterSetCorrectorInit(selection) \
	-buttonList {Horizontal Vertical Both} \
	-valueList {h v b} \
	-packOption {-side top} \
	-contextHelp "These buttons select various combinations of booster corrector supplies."
    
    return
}

proc APSMpBoosterSetCorrectorInit {args} {
    set selection b

    APSStrictParseArguments {selection}

    APSMpStep "Set Booster Corrector Init Plane(s)" \
	-promptDialog APSMpBoosterSetCorrectorInitDialog -vars {selection}

    APSSetVarAndUpdate status "Starting corrector ramp initialization procedure for $selection supplies..."
    if [catch {APSMpBoosterSetCorrectorInitNormal -selection $selection} done] {
	APSMpReturn error "APSMpBoosterSetCorrectorInit: $done"
    }
    APSSetVarAndUpdate status "Done."
    after 2000
    
    APSMpReturn ok
}

proc APSMpBoosterSetCorrectorInitNormal {args} {
    set dir "/home/helios/oagData/booster/orbitCorr"
    set horizontal 1
    set vertical 1
    set selection b
    set loadAdl 1
    APSStrictParseArguments {selection loadAdl}
    if {$loadAdl} {
	catch {exec medm -x -attach /net/helios/iocapps/adlsys/booster/psApp/BoosterRampControl.adl &}
    }
    if {$selection=="v"} {
	set horizontal 0
    } elseif {$selection=="h"} {
	set vertical 0
    }

    if {$horizontal && $vertical} {
	set filename ${dir}/booster.zero.ramp
    } elseif $horizontal {
	set filename ${dir}/booster.zero.h.ramp
    } elseif $vertical {
	set filename ${dir}/booster.zero.v.ramp
    }
    
    APSSetVarAndUpdate status "Performing INIT..."
    if [catch {exec caput B:BM:InitSEQ 1} results] {
	APSMpReturn error "APSMpBoosterSetCorrectorInitNormal: $results"
    }
    after 3000
    APSSetVarAndUpdate status "Performing first ramploadnew using $filename..."
    if [catch {exec ramploadnew $filename} results] {
	APSMpReturn error "APSMpBoosterSetCorrectorInitNormal: $results"
    }
    after 3000
    APSSetVarAndUpdate status "Performing AUTO..."
    if [catch {exec caput B:BM:AutoSEQ 1} results] {
	APSMpReturn error "APSMpBoosterSetCorrectorInitNormal: $results"
    }
    after 3000
    APSSetVarAndUpdate status "Performing second ramploadnew using $filename..."
    if [catch {exec ramploadnew $filename} results] {
	APSMpReturn error "APSMpBoosterSetCorrectorInitNormal: $results"
    }
    after 3000
    APSSetVarAndUpdate status "Performing ACTIVATE..."
    if [catch {exec caput B:BM:SwapSEQ 1} results] {
	APSMpReturn error "APSMpBoosterSetCorrectorInitNormal: $results"
    }
    after 3000
    #APSSetVarAndUpdate status "Setting CurrentAO's..."
    #if [catch {exec setbramp2ao -b} results] {
#	APSMpReturn error "APSMpBoosterSetCorrectorInitNormal: $results"
 #   }
    after 1000
    APSMpReturn ok
}

























