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

#
# Feedforward on S5B:QS and S7A:QS to correct coupling perturbation due to SCU0
# See /home/helios/SR/daily/2013/01/25/3/SCU0 and /home/helios/SR/daily/2013/01/27/2/SCU0
#

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 SCU0CouplingFeedforward -version $CVSRevisionAuthor \
  -overview {This is the SCU0 coupling compensation feedforward utility. }

set FFDir /home/helios/oagData/sr/SCU0/couplingFeedforward/
set mainStatus "Ready."
set Interval 1

set runControlPV APS:RunControlSlot0RC
set runControlDesc "SCU0 Coupling 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 "SCU0 coupling compensation feedforward"
APSFrame .w1 -parent .userFrame 
set w1 .userFrame.w1.frame

APSButton .run -parent $w1 -text RUN -command RunFeedforward \
  -contextHelp "Start SCU0 feedforward process."
APSButton .stop -parent $w1 -text "STOP" -command "AbortFeedforward" \
  -contextHelp "Abort SCU0 feedforward process"
APSButton .info -parent $w1 -text "INFO" -command "FeedforwardInfo" \
  -contextHelp "Bring up run control screen for feedforward"


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 SCU0 coupling compensation feedforward..."
    AbortFeedforward

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

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

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

