#
# $Log: not supported by cvs2svn $
# Revision 1.8  2008/11/14 15:54:36  soliday
# Updated to restore fft control PVs based on the current lattice.
#
# Revision 1.7  2007/03/01 17:24:27  soliday
# Added missing calls to APSLogScriptAction to store initialization values.
#
# Revision 1.6  2007/02/27 22:33:41  sereno
# Added explicit computation of the booster revolution (FFT sampling) frequency.
#
# Revision 1.5  2006/06/14 01:18:33  sereno
# Changed procedure to switch to the PAR rf12 phase shifter instead of SR
# phase shifter and bunch current monitor time delay.
#
# Revision 1.4  2006/05/25 20:18:07  soliday
# Sereno added the APSMpConfigLongControllawActuators machine procedure.
#
# Revision 1.3  2004/10/27 16:23:41  soliday
# Updated machine procedure descriptions per Sereno's request.
#
# Revision 1.2  2003/11/04 00:24:11  shang
# Fixed four B2C8P1 pv names that are used for waveform processing (to be consistent
# with Nick's chang to BBPMHistWaveformSetup)
#
# Revision 1.1  2003/11/03 22:45:19  shang
# tranferred from BBPMHistoryWaveformSetup
#


proc APSMpConfigInjControllawBPMsInfo {} {
    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM005 under required reading.

This machine procedure configures BPM, Beam History Module (BHM) and waveform processing controls for use by the longitudinal and horizontal injection controllaws.  The default BPM is B2C8P2 and should be used if it is not broken.  The default values for waveform offset and averaging should be used but may be changed in the event the B2C8P2 waveform becomes noisy.
"
}

proc APSMpConfigInjControllawBPMsInitDialog {frame} {
    global APSMpConfigInjControllawBPMs
    
    set APSMpConfigInjControllawBPMs(bpm) B2C8P2
    set APSMpConfigInjControllawBPMs(offset) 4
    set APSMpConfigInjControllawBPMs(ave) 6
    pack [frame ${frame}.f1]
    APSRadioButtonFrame .rf0 -parent $frame.f1 -label "Select BPM: " \
      -buttonList  {B2C8P2 B3C2P1 B2C8P1} -valueList {B2C8P2 B3C2P1 B2C8P1} -packOption "-side top" \
      -variable  APSMpConfigInjControllawBPMs(bpm) -orientation horizontal \
      -contextHelp "Select bpm for history setup." \
      -commandList {"set APSMpConfigInjControllawBPMs(offset) 4;set APSMpConfigInjControllawBPMs(ave) 6" \
                      "set APSMpConfigInjControllawBPMs(offset) 6;set APSMpConfigInjControllawBPMs(ave) 10" \
                      "set APSMpConfigInjControllawBPMs(offset) 6;set APSMpConfigInjControllawBPMs(ave) 6"}
    APSLabeledEntry .offset -parent $frame.f1 -label "Waveform offset number:" \
      -textVariable APSMpConfigInjControllawBPMs(offset) \
      -contextHelp "Waveform offset number:"
    APSLabeledEntry .ave -parent $frame.f1 -label "Waveform average number:" \
      -textVariable APSMpConfigInjControllawBPMs(ave) \
      -contextHelp "Waveform average number:"
    APSMpReturn ok
}

proc APSMpConfigInjControllawBPMs {args} {
    global APSMpConfigInjControllawBPMs

    set offset ""
    set ave ""
    set bpm ""
    
    APSMpStep init -promptDialog APSMpConfigInjControllawBPMsInitDialog \
	-vars {offset ave bpm}
    if {![string length bpm] || ![string length $ave] || ![string length $offset]} {
        APSMpReturn error "APSMpConfigInjControllawBPMs: bpm, offset or average number is not given"
    }

    set paramString ""
    foreach param {offset ave bpm} {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction \
             -procedure APSMpConfigInjControllawBPMs \
             -action initialize \
             -state ok \
             -parameters $paramString}

    if [regexp {B([1-4])C([0-9])P([1-2])$} $bpm bpmString nQuad nCell nPM] {
        set bpmIndex [expr int(20 * ($nQuad - 1) + 2 * $nCell + ($nPM -1))]
        if {${bpmIndex} < 73} {
            set rangeInt [expr int($bpmIndex/8.0)]
            set lowerRange [expr int((8 * $rangeInt) + 1)]
            set upperRange [expr int(8 * ($rangeInt + 1))]
            set tCardNumber [join "$lowerRange $upperRange" "_"]
        } elseif {${bpmIndex} >= 73 && ${bpmIndex} <= 77} {
            set tCardNumber 73_77
        } else {
            set tCardNumber 78_80
        }
    } else {
        APSMpReturn error "APSMpConfigInjControllawBPMs: Booster BPM names have the form B[1-4]C[0-9]P[1-2]."
    }
    # Check to see if this is a horizontal/longitudinal plane bpm.
    # Horizontal:  B2C8P2, B3C2P1
    # Vertical:    B2C8P1
    if {![string compare $bpm B2C8P2] || ![string compare $bpm B3C2P1]} {
        set plane H
    } elseif {![string compare $bpm B2C8P1]} {
        set plane V
    } else {
        APSMpReturn error  "Please select a valid booster injection controllaw bpm.\nHorizontal:  B2C8P2, B3C2P1\n
                  Vertical:    B2C8P1"
    }

# Get the booster revolution frequency and convert to kHz.

    if {[catch {exec cavget -list=Mt:BoosterRampTurnsAO,A014-IETS:BTC:BInjectionCycleM,A014-IETS:BTC:BExtractionCycleM,A014-IETS:BTC:SRSetFreqM -pend=5 -floatformat=%lf]} results]} {
        set booRevFreq 814.667
    } else {
        set srExtractionTurns [lindex $results 0]
        set bInjectionCycle [lindex $results 1]
        set bExtractionCycle [lindex $results 2]
        set srFreq [lindex $results 3]
        set rfFrequency [expr 432 * $srFreq * 3 * $srExtractionTurns / ($bExtractionCycle - $bInjectionCycle)]
        set booRevFreq [expr $rfFrequency / 432000.0]
    }



    #Find the lattice we are in so that we can extract the fft peak search PVs from the 
    #correct system reference files.
    if [catch {exec cavget -list=B:QF:SlopeRefAO -pend=30} qf] {
	APSMpReturn error $qf
    }
    if [catch {exec cavget -list=B:QD:SlopeRefAO -pend=30} qd] {
	APSMpReturn error $qd
    }
    global Lattice
    set Lattice [APSMpCheckBoosterLattice -qd $qd -qf $qf]
    if {[string compare $Lattice "unknown"]==0} {
	APSMpStep "Can not determine lattice, please manually select the lattice!"
	set Lattice ""
	APSDialog .lattice -name "Choose Lattice Dialog" \
	    -okCommand "update" -cancelCommand "destroy .lattice"
	APSRadioButtonFrame .radio -parent .lattice.userFrame -buttonList "92nm 109nm 132nm" \
	    -variable Lattice -valueList "x13.75-y5.80 x12.75-y9.80 x11.75-y9.80"
	tkwait window .lattice
	if {![string length $Lattice]} {
	    APSMpReturn error "The lattice was not chosen!"
	}
    }
    switch $Lattice {
	x13.75-y5.80 {
	    set lattice 92nm
            set SCRFile /home/helios/oagData/SCR/snapshots/Booster/Booster-LowE2Reference325.gz
	}
	x12.75-y9.80 {
	    set lattice 109nm
            set SCRFile /home/helios/oagData/SCR/snapshots/Booster/Booster-LowE1Reference325.gz
            if {[file mtime $SCRFile] < 1226429729} {
                #Use 132nm lattice file because the old 109nm file does not have the fft peak search PVs.
                set SCRFile /home/helios/oagData/SCR/snapshots/Booster/Booster-Reference325.gz
            }
	}
	x11.75-y9.80 {
	    set lattice 132nm
            set SCRFile /home/helios/oagData/SCR/snapshots/Booster/Booster-Reference325.gz
	}
	default {
	    APSMpReturn error "Unknown lattice: $Lattice"
	}
    }
    if {[catch {exec sddsselect $SCRFile /home/helios/oagData/pem/booster/SCRFilterFile \
                  -match=ControlName -pipe=out | \
                  sdds2stream -pipe=in -column=ControlName,ValueString} results]} {
	APSMpReturn error "APSMpConfigInjControllawBPMs: Invalid SCR file"
    }
    set B2C8P2fftPeakSearchStartP ""
    set B2C8P2fftPeakSearchStopP ""
    set B2C8P2fftPeakSearchStart2P ""
    set B2C8P2fftPeakSearchStop2P ""
    set B2C8P1fftPeakSearchStartP ""
    set B2C8P1fftPeakSearchStopP ""
    set B3C2P1fftPeakSearchStartP ""
    set B3C2P1fftPeakSearchStopP ""
    set B3C2P1fftPeakSearchStart2P ""
    set B3C2P1fftPeakSearchStop2P ""
    foreach {controlName valueString} $results {
	if {$controlName == "B2C8P2:fftPeakSearchStartP"} {
            set B2C8P2fftPeakSearchStartP $valueString
        } elseif {$controlName == "B2C8P2:fftPeakSearchStopP"} {
            set B2C8P2fftPeakSearchStopP $valueString
        } elseif {$controlName == "B2C8P2:fftPeakSearchStart2P"} {
            set B2C8P2fftPeakSearchStart2P $valueString
        } elseif {$controlName == "B2C8P2:fftPeakSearchStop2P"} {
            set B2C8P2fftPeakSearchStop2P $valueString
        } elseif {$controlName == "B2C8P1:fftPeakSearchStartP"} {
            set B2C8P1fftPeakSearchStartP $valueString
        } elseif {$controlName == "B2C8P1:fftPeakSearchStopP"} {
            set B2C8P1fftPeakSearchStopP $valueString
        } elseif {$controlName == "B3C2P1:fftPeakSearchStartP"} {
            set B3C2P1fftPeakSearchStartP $valueString
        } elseif {$controlName == "B3C2P1:fftPeakSearchStopP"} {
            set B3C2P1fftPeakSearchStopP $valueString
        } elseif {$controlName == "B3C2P1:fftPeakSearchStart2P"} {
            set B3C2P1fftPeakSearchStart2P $valueString
        } elseif {$controlName == "B3C2P1:fftPeakSearchStop2P"} {
            set B3C2P1fftPeakSearchStop2P $valueString
        }
    }




    switch $plane {
        H {
            APSMpStep "Unlock BPM."
            if [catch {exec cavput -list=${bpm}:lock_bo.VAL=0 -pendIoTime=20 } result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs1: $result"
            }
            APSMpStep "Placing the BPM into X-Only mode and setup the bpm timing\nto detect exactly the first (injection) turn."
            # Turning Force Free Run/Self Test/ off;
            # Turning Backup Trigger on;
            # Setting Gain to Low;
            # Setting Ring to Sync;
            # Setting Resync to Inhibit;
            # Setting 250 ns delay to disable;
            # Setting P0 to 0;
            # Enabling the timing card start ramp signal;
            # Setting timing variables for the first 272 turns (N=0 and M=68);
            # Setting the global timing variables to "best" values; 
            # Setting memory scanner average weight to 2;
            # Setting mode to Delay;
            # Setting Resync To LocalB to Disable;
            # Setting Resync Reset to Disable.

            if [catch {exec cavput -list=${bpm}:disablex_bo=0,${bpm}:disabley_bo=1,${bpm}:disablesum_bo=0,${bpm}:ffreerun_bo=0,${bpm}:test_bo=1,${bpm}:backuptrig_bo=1,${bpm}:normal_bo=0,${bpm}:ring_bo=0,${bpm}:resync_bo=1,B:bpmT${tCardNumber}:maxDelay_bo=1,B:bpmT${tCardNumber}:P0_delay_lo=0,B:bpmT${tCardNumber}:ext_start_bo=0,B:bpmT${tCardNumber}:mode_bo=1,B:bpmT${tCardNumber}:LBresyncDISbo=1,B:bpmT${tCardNumber}:resync_reset_bo=1,B:bpmT${tCardNumber}:Delay_count_lo=0,B:bpmT${tCardNumber}:Cycle_count_lo=68,It:Ddg3chan6.DLY=0.29588,It:Ddg9chan4.DLY=0.29720 -pendIoTime=30 } result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs2: $result"
            }
                
            APSMpStep "Locking BPM."
            if [catch {exec cavput -list=${bpm}:lock_bo.VAL=1 -pend=30} result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs3: $result"
            }
    
            APSMpStep "Set up the beam history module."
            # Setting the proper BPM index for the bpm name string; 
            # Setting the fifo resync mode to disable;
            # Setting the Fifo fill mode to "Stop on Full"; 
            # Setting the Fifo read mode to enable; 
            # Setting the Fifo mode to "Continuous"; 
            # Enabling waveform acquisition;
            # Enable FFT waveform processing;
            # Set the booster revolution (sampling) frequency in kHz.
            # Set the longitudinal peak search start channel to 20 (15.91 kHz);
            # Set the longitudinal peak search end channel to 45 (35.80 kHz);
            # Set the horizontal peak search start channel to 200 (159.11 kHz);
            # Set the horizontal peak search end channel to 370 (294.36 kHz);
            # Set the longitudinal peak search threshold to 0;
            # Set the horizontal peak search threshold to 0;
            # Set the time-domain window to blackman;
            # Set the time-domain processing to Sub/Ave;
            # Set the time-domain waveform offset to 6;
            # Set the time-domain number to average to 6.

            if [catch {exec cavput -list=${bpm}:bh.BPMI=${bpmIndex},${bpm}:bh.FIR0=0,${bpm}:bh.FIM0=1,${bpm}:bh.BRDE=1,${bpm}:bh.BRDM=1,${bpm}:bh:wfDisable_bo=1,${bpm}:fftFftDisableC=0,${bpm}:fftSampleFreqP=${booRevFreq},${bpm}:fftPeakSearchStartP=[set ${bpm}fftPeakSearchStartP],${bpm}:fftPeakSearchStopP=[set ${bpm}fftPeakSearchStopP],${bpm}:fftPeakFindThresP=0,${bpm}:fftPeakSearchStart2P=[set ${bpm}fftPeakSearchStart2P],${bpm}:fftPeakSearchStop2P=[set ${bpm}fftPeakSearchStop2P],${bpm}:fftPeakFindThres2P=0,${bpm}:fftWindowTypeC=1,${bpm}:bh:x:FunctionP=3,${bpm}:bh:x:OffsetP=${offset},${bpm}:bh:x:num2aveP=${ave} -pendIoTime=30 } result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs4: $result"
            }
            APSMpStep "Booster bpm $bpm set to X-Only mode to detect first turn (injection) position."
        }
        V {
            APSMpStep "Unlock BPM."
            if [catch {exec cavput -list=${bpm}:lock_bo.VAL=0 -pendIoTime=20 } result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs5: $result"
            }
            APSMpStep "Placing the BPM into Y-Only mode and setup the bpm timing\nto detect exactly the first (injection) turn."
            # Turning Force Free Run/Self Test/ off;
            # Turning Backup Trigger on;
            # Setting Gain to Low;
            # Setting Ring to Sync;
            # Setting Resync to Inhibit;
            # Setting 250 ns delay to disable;
            # Setting P0 to 0;
            # Enabling the timing card start ramp signal;
            # Setting timing variables for the first 272 turns (N=0 and M=68);
            # Setting the global timing variables to "best" values; 
            # Setting memory scanner average weight to 2;
            # Setting mode to Delay;
            # Setting Resync To LocalB to Disable;
            # Setting Resync Reset to Disable.
    
            if [catch {exec cavput -list=${bpm}:disablex_bo=1,${bpm}:disabley_bo=0,${bpm}:disablesum_bo=0,${bpm}:ffreerun_bo=0,${bpm}:test_bo=1,${bpm}:backuptrig_bo=1,${bpm}:normal_bo=0,${bpm}:ring_bo=0,${bpm}:resync_bo=1,B:bpmT${tCardNumber}:maxDelay_bo=1,B:bpmT${tCardNumber}:P0_delay_lo=0,B:bpmT${tCardNumber}:ext_start_bo=0,B:bpmT${tCardNumber}:mode_bo=1,B:bpmT${tCardNumber}:LBresyncDISbo=1,B:bpmT${tCardNumber}:resync_reset_bo=1,B:bpmT${tCardNumber}:Delay_count_lo=0,B:bpmT${tCardNumber}:Cycle_count_lo=68,It:Ddg3chan6.DLY=0.29588,It:Ddg9chan4.DLY=0.29720 -pendIoTime=30 } result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs6: $result"
            }

            APSMpStep "Locking BPM."
            if [catch {exec cavput -list=${bpm}:lock_bo.VAL=1  -pend=30} result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs7: $result"
            }
            APSMpStep "Set up the beam history module."
            # Setting the proper BPM index for the bpm name string; 
            # Setting the fifo resync mode to disable;
            # Setting the Fifo fill mode to "Stop on Full"; 
            # Setting the Fifo read mode to enable; 
            # Setting the Fifo mode to "Continuous"; 
            # Enabling waveform acquisition;
            # Enable FFT waveform processing;
            # Set the vertical peak search start channel to 200 (159.11 kHz);
            # Set the vertical peak search end channel to 370 (294.36 kHz);
            # Set the vertical peak search threshold to 0;
            # Set the time-domain window to blackman;
            # Set the time-domain processing to Sub/Ave;
            # Set the time-domain waveform offset to 6;
            # Set the time-domain number to average to 6.
            
            if [catch {exec cavput -list=${bpm}:bh.BPMI=${bpmIndex},${bpm}:bh.FIR0=0,${bpm}:bh.FIM0=1,${bpm}:bh.BRDE=1,${bpm}:bh.BRDM=1,${bpm}:bh:wfDisable_bo=1,${bpm}:fftFftDisableC=0,${bpm}:fftSampleFreqP=${booRevFreq},${bpm}:fftPeakSearchStartP=[set ${bpm}fftPeakSearchStartP],${bpm}:fftPeakSearchStopP=[set ${bpm}fftPeakSearchStopP],${bpm}:fftPeakFindThresP=0,${bpm}:fftWindowTypeC=1,${bpm}:bh:y:FunctionP=3,${bpm}:bh:y:OffsetP=${offset},${bpm}:bh:y:num2aveP=${ave} -pendIoTime=20 } result] {
                APSMpReturn error "APSMpConfigInjControllawBPMs8: $result"
            }
            
            APSMpStep "Booster bpm $bpm set to Y-Only mode to detect first turn (injection) position."
        }
    }
    APSMpReturn ok
}

proc APSMpConfigLongControllawActuatorsInfo {} {
    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM006 under required reading.

This machine procedure configures links that point to two sets of  matrix and tests files for booster longitudinal controllaw.  The first set of files uses only the BRFPhase and StartRampTime actuators.  The second set uses the BRFPhase, SRRFPhase and StartRampTime actuators.  The second set of actuators should be used to change both booster and SR rf phase to preserve injection efficiency.  The first set should be used if SR rf phase correction is not desired (such as the phase shifter is out of range).
"
}

proc APSMpConfigLongControllawActuatorsInitDialog {frame} {
    global APSMpConfigLongControllawActuators
    set longControllawDir /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2
    set matrixSize [lindex [exec sdds2stream ${longControllawDir}/longBooster.matrix -rows] 0]
    switch $matrixSize {
        2 {set APSMpConfigLongControllawActuators BRFPhase}
        4 {set APSMpConfigLongControllawActuators BRFPhase-SRRFPhase-BCDelay}
        default {APSMpReturn error "Unknown longitudinal controllaw configuration"}
    }
    pack [frame ${frame}.f1]
    APSRadioButtonFrame .rf0 -parent $frame.f1 -label "Select ActuatorSet: " \
      -buttonList  {BRFPhase BRFPhase-SRRFPhase-BCDelay} -valueList {BRFPhase BRFPhase-SRRFPhase-BCDelay} -packOption "-side top" \
      -variable APSMpConfigLongControllawActuators -orientation horizontal \
      -contextHelp "Select which set of matrix and tests files to use for booster longitudinal controllaw."
    APSMpReturn ok
}

proc APSMpConfigLongControllawActuators {args} {
    global APSMpConfigLongControllawActuators
    set longControllawDir /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2
    APSMpStep init -promptDialog APSMpConfigLongControllawActuatorsInitDialog
    APSMpStep "Actuator chosen is $APSMpConfigLongControllawActuators"
    switch $APSMpConfigLongControllawActuators {
        BRFPhase {
            exec rm $longControllawDir/longBooster.matrix
            exec rm $longControllawDir/longBooster.tests
            exec rm $longControllawDir/longBooster.deltaLimits
            exec ln -s $longControllawDir/longBooster.matrix-0001 $longControllawDir/longBooster.matrix
            exec ln -s $longControllawDir/longBooster.tests-0003 $longControllawDir/longBooster.tests
            exec ln -s $longControllawDir/longBooster.deltaLimits-0002 $longControllawDir/longBooster.deltaLimits
            exec cavput -list=Booster:ControlLawLongRC.ABRT=1
            APSMpStep "Finished configuring booster longitudinal controllaw to use only the BRFPhase actuator."
            after 1000
        } BRFPhase-SRRFPhase-BCDelay {
            exec rm $longControllawDir/longBooster.matrix
            exec rm $longControllawDir/longBooster.tests
            exec rm $longControllawDir/longBooster.deltaLimits
            exec ln -s $longControllawDir/longBooster.srAct.matrix-0000 $longControllawDir/longBooster.matrix
            exec ln -s $longControllawDir/longBooster.srAct.tests-0000 $longControllawDir/longBooster.tests
            exec ln -s $longControllawDir/longBooster.srAct.deltaLimits-0000 $longControllawDir/longBooster.deltaLimits
            exec cavput -list=Booster:ControlLawLongRC.ABRT=1
            APSMpStep "Finished configuring booster longitudinal controllaw to use BRFPhase SRRFPhase and BCDelay actuators."
            after 1000
        } default {
            {APSMpReturn error "Unknown longitudinal controllaw configuration"}
        }
    }
    APSMpReturn ok
}

proc APSMpConfigLongControllawActuatorsInfo {} {
    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM006 under required reading.

This machine procedure configures links that point to two sets of  matrix and tests files for booster longitudinal controllaw.  The first set of files uses only the BRFPhase and StartRampTime actuators.  The second set uses the PARRF12Phase and StartRampTime actuators.  The second set of actuators should be used to preserve booster injection efficiency yet not affect the booster to storage ring phase which affects storage ring injection efficiency.  The first set should be used if the par rf becomes unstable leading to double bunching.
"
}

proc APSMpConfigLongControllawActuatorsInitDialog {frame} {
    global APSMpConfigLongControllawActuators
    set longControllawDir /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2
    set activeActuator [lindex [exec sdds2stream ${longControllawDir}/longBooster.matrix -col=Actuators] 1]
    puts $activeActuator
    switch $activeActuator {
        BRFPhase {set APSMpConfigLongControllawActuators BRFPhase}
        PARRF12Phase {set APSMpConfigLongControllawActuators PARRF12Phase}
        default {APSMpReturn error "Unknown longitudinal controllaw configuration"}
    }
    pack [frame ${frame}.f1]
    APSRadioButtonFrame .rf0 -parent $frame.f1 -label "Select ActuatorSet: " \
      -buttonList  {BRFPhase PARRF12Phase} -valueList {BRFPhase PARRF12Phase} -packOption "-side top" \
      -variable APSMpConfigLongControllawActuators -orientation horizontal \
      -contextHelp "Select which set of matrix and tests files to use for booster longitudinal controllaw."
    APSMpReturn ok
}

proc APSMpConfigLongControllawActuators {args} {
    global APSMpConfigLongControllawActuators
    set longControllawDir /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2
    APSMpStep init -promptDialog APSMpConfigLongControllawActuatorsInitDialog
    APSMpStep "Actuator chosen is $APSMpConfigLongControllawActuators"
    switch $APSMpConfigLongControllawActuators {
        BRFPhase {
            set previousDir [exec pwd]
            cd $longControllawDir

            set f1 [APSNextGenerationedName \
                      -newFile 1 \
                      -name longBooster.matrix-0000 \
                      -directory /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2]
            set f2 [APSNextGenerationedName \
                      -newFile 1 \
                      -name longBooster.tests-0000 \
                      -directory /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2]
            set f3 [APSNextGenerationedName \
                      -newFile 1 \
                      -name longBooster.deltaLimits-0000 \
                      -directory /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2]
            set f1 [string range $f1 0 end-4][format %04d [expr [string trimleft [string range $f1 end-3 end] 0] - 1]]
            set f2 [string range $f2 0 end-4][format %04d [expr [string trimleft [string range $f2 end-3 end] 0] - 1]]
            set f3 [string range $f3 0 end-4][format %04d [expr [string trimleft [string range $f3 end-3 end] 0] - 1]]
            
            file delete -force longBooster.matrix longBooster.tests longBooster.deltaLimits
            exec ln -s [file tail $f1] longBooster.matrix      
            exec ln -s [file tail $f2] longBooster.tests       
            exec ln -s [file tail $f3] longBooster.deltaLimits 
            cd $previousDir
            exec cavput -list=Booster:ControlLawLongRC.ABRT=1 -pend=20
            APSMpStep "Finished configuring booster longitudinal controllaw to use only the BRFPhase actuator."
            after 1000
        } PARRF12Phase {
            set previousDir [exec pwd]
            cd $longControllawDir

            set f1 [APSNextGenerationedName \
                      -newFile 1 \
                      -name longBooster.parRf12Act.matrix-0000 \
                      -directory /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2]
            set f2 [APSNextGenerationedName \
                      -newFile 1 \
                      -name longBooster.parRf12Act.tests-0000 \
                      -directory /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2]
            set f3 [APSNextGenerationedName \
                      -newFile 1 \
                      -name longBooster.parRf12Act.deltaLimits-0000 \
                      -directory /home/helios/OAG/oagData/controllaw/BOOSTER/longBooster/B2C8P2]
            set f1 [string range $f1 0 end-4][format %04d [expr [string trimleft [string range $f1 end-3 end] 0] - 1]]
            set f2 [string range $f2 0 end-4][format %04d [expr [string trimleft [string range $f2 end-3 end] 0] - 1]]
            set f3 [string range $f3 0 end-4][format %04d [expr [string trimleft [string range $f3 end-3 end] 0] - 1]]
            
            file delete -force longBooster.matrix longBooster.tests longBooster.deltaLimits
            exec ln -s [file tail $f1] longBooster.matrix      
            exec ln -s [file tail $f2] longBooster.tests       
            exec ln -s [file tail $f3] longBooster.deltaLimits 
            cd $previousDir
            exec cavput -list=Booster:ControlLawLongRC.ABRT=1 -pend=20
            APSMpStep "Finished configuring booster longitudinal controllaw to use BRFPhase SRRFPhase and BCDelay actuators."
            after 1000
        } default {
            {APSMpReturn error "Unknown longitudinal controllaw configuration"}
        }
    }
    APSMpReturn ok
}
