#!/bin/sh  
# \
exec oagwish "$0" "$@"


set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.0 $ \$Author: borland $"

APSApplication . -name SCU6Feedforward -version $CVSRevisionAuthor \
  -overview {This is the sector 6 SCU corrector feedforward utility (skew quad feedforward is obsolete), to distinguish with obsolete SCU6 feedforward, we name it as SCU6 feedforward }

set FFDir /home/helios/oagData/sr/SCU6/feedforward/
set mainStatus "Ready."
set Interval 1

set runControlPV OAG137RC
set runControlPV1 OAG136RC
set runControlDesc "SCU6 Corrector Feedforward"
set runControlPingTimeout 30

proc SetMainStatus {text} {
    global mainStatus
    set mainStatus $text
    update
}

APSScrolledStatus .status -parent .userFrame -textVariable mainStatus -width 90

set width 60
APSLabeledEntry .runcontrol -parent .userFrame -label "Run control pv:" \
    -textVariable runControlPV -width $width \
    -contextHelp "enter the run control pv for running feedforward."
APSLabeledEntry .desc -parent .userFrame -label "Run control description:" \
    -textVariable runControlDesc -width $width \
    -contextHelp "run control description."
APSLabeledEntry .time -parent .userFrame -label "Run control ping timeout (s):" \
    -textVariable runControlPingTimeout -width $width 
APSLabeledEntry .interval -parent .userFrame -label "Interval (s): " \
    -textVariable Interval -width $width \
    -contextHelp "Enter the time between samples in seconds in this field."

cd $FFDir

set runControlDesc "SCU6 corrector feedforward"
APSFrame .w1 -parent .userFrame 
set w1 .userFrame.w1.frame

APSButton .run -parent $w1 -text RUN -command RunFeedforward \
  -contextHelp "Start SCU6 feedforward process."
APSButton .stop -parent $w1 -text "STOP" -command "AbortFeedforward" \
  -contextHelp "Abort SCU6 feedforward process"
APSButton .info -parent $w1 -text "INFO" -command "FeedforwardInfo" \
  -contextHelp "Bring up run control screen for feedforward"
APSButton .zero -parent $w1 -text "Zero FF" -command "ZeroFF" \
  -contextHelp "zero corrector and waveform FF."
APSButton .ff -parent $w1 -text "Transfer FF to Waveform" -command "APSExecLog .scu6Vectorfeedforward -width 80 -unixCommand \
        \"TransferFFtoWaveform\"" 

proc ZeroFF {args} {
    SetMainStatus "Zero corrector FF ..."
    if [catch {exec sddscasr -restore -pend=30 /home/helios/oagData/sr/orbitControllaw/waveforms/zeroCorrFF.sdds } result] {
        return -code error "Error zeroing scalar corrector FF: $result"
    }
    if [catch {exec sddswput -pend=30 /home/helios/oagData/sr/orbitControllaw/waveforms/zeroCorrFFWF.sdds } result] {
        return -code error "Error zeroing corrector FF waveform: $result"
    }
    SetMainStatus "done."
}

proc RunFeedforward {args} {
    global inputFile runControlPV runControlDesc 
    global runControlPingTimeout Interval tubeAmplifier FFDir rf12Amplifier
    
    if ![string length $runControlPV] {
        SetMainStatus "run control pv is not provided.!"
        return
    }
    
    set inputFile FF
    
    if ![file exist $FFDir/$inputFile] {
        SetMainStatus "$FFDir/$inputFile does not exist"
        return
    }

    SetMainStatus "Running SCU6 corrector feedforward..."
    AbortFeedforward

    # N.B.: No tests for this FF, since we use the SCU6 current readback. If we had tests, we might fail to 
    # change the skews when the device trips or quenches
    
    APSExecLog .scu6feedforward -width 80 \
        -unixCommand \
        "sddsfeedforward $FFDir/$inputFile -interval=$Interval -verbose -infiniteLoop -runControlPV=string=$runControlPV,pingTimeout=$runControlPingTimeout \"-runControlDescription=$runControlDesc\" "
    APSExecLog .scu6Vectorfeedforward -width 80 \
        -unixCommand \
        "TransferFFtoWaveform" 
}

proc AbortFeedforward {args} {
    set full 0
    APSParseArguments {full}
    global runControlPV runControlPV1
    if [catch {exec cavput -list=$runControlPV.ABRT=1,$runControlPV1.ABRT=1 -pend=30} result] {
	SetMainStatus "Error in aborting feedforward: $result"
	return
    }
    catch {exec cawait -waitfor=$runControlPV.RUN,equal=0 -waitfor=$runControlPV1.RUN,equal=0 -and}
}

proc FeedforwardInfo {args} {
    global runControlPV runControlPV1
    exec medm -x -attach -macro RCPV=$runControlPV ./sr/psApp/APSRunControlSingle.adl &
     exec medm -x -attach -macro RCPV=$runControlPV1 ./sr/psApp/APSRunControlSingle.adl &
    
}

