#!/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)]

APSApplication . -name "Standardize with Decay"

set pv ""
set lowerLimit 0
set upperLimit 100
set numSteps 60
set stepInterval 1
set numCycles 2
set status "Ready..."
set decayRate 5

APSScrolledStatus .status -parent .userFrame -textVariable status -packOption "-fill x -expand true"

APSLabeledEntry .pv -parent .userFrame -label "PV Name:" -textVariable pv
APSLabeledEntry .ll -parent .userFrame -label "Lower Limit:" -textVariable lowerLimit
APSLabeledEntry .up -parent .userFrame -label "Upper Limit:" -textVariable upperLimit
APSLabeledEntry .steps -parent .userFrame -label "Steps for ramp or half standardization cycle:" -textVariable numSteps
APSLabeledEntry .interval -parent .userFrame -label "Step interval in seconds:" -textVariable stepInterval
APSLabeledEntry .cycles -parent .userFrame -label "Number of standardization cycles:" -textVariable numCycles
APSLabeledEntry .decay -parent .userFrame -label "Decay Rate:" -textVariable decayRate

pack [frame .userFrame.b1] -side top -anchor nw
pack [frame .userFrame.b2] -side top -anchor nw
APSButton .rampup -parent .userFrame.b1 -text "Ramp to Upper Limit" -command "RampUp"
APSButton .rampdown -parent .userFrame.b1 -text "Ramp to Lower Limit" -command "RampDown"
APSButton .standardizeup -parent .userFrame.b2 -text "Standardize to Upper Limit" -command "StandardizeUp"
APSButton .standardizedown -parent .userFrame.b2 -text "Standardize to Lower Limit" -command "StandardizeDown"


package require BLT
namespace import ::blt::vector
namespace import ::blt::graph
graph .userFrame.g -width 800 -title "StripChart"
pack .userFrame.g -expand 1 -fill both
.userFrame.g grid configure -hide no

set X [vector #auto]
set Y [vector #auto]

$X set "0"
$Y set "0"
.userFrame.g element create Setpoints -xdata $X -ydata $Y -symbol circle -pixels 2 -linewidth 1


proc UpdatePlot {args} {
    global TimeList CurrentList pvvalue X Y
    lappend CurrentList $pvvalue
    lappend TimeList [expr [llength $TimeList] + 1]
    $X set $TimeList
    $Y set $CurrentList

}

proc RampUp {args} {
    global pv lowerLimit upperLimit numSteps stepInterval status decayRate
    global TimeList CurrentList pvvalue
    set TimeList ""
    set CurrentList ""

    set finalSetpoint $upperLimit
    
    pv linkw pvvalue $pv
    pv umon pvvalue UpdatePlot

    APSSetVarAndUpdate status "Ramping Up"

    if {[catch {APSStandardizeWithDecay -pv $pv \
                  -lowerLimit $lowerLimit \
                  -upperLimit $upperLimit \
                  -numSteps $numSteps \
                  -stepInterval $stepInterval \
                  -numCycles 0 -decayRate $decayRate \
                  -finalSetpoint $finalSetpoint} results]} {
        APSSetVarAndUpdate status "Error: $results"
    }
    
    #pv unlink pvvalue

    APSSetVarAndUpdate status "Done Ramping Up"
}

proc RampDown {args} {
    global pv lowerLimit upperLimit numSteps stepInterval decayRate
    global TimeList CurrentList pvvalue
    set TimeList ""
    set CurrentList ""

    set finalSetpoint $lowerLimit
    
    pv linkw pvvalue $pv
    pv umon pvvalue UpdatePlot

    APSSetVarAndUpdate status "Ramping Down"

    if {[catch {APSStandardizeWithDecay -pv $pv \
                  -lowerLimit $lowerLimit \
                  -upperLimit $upperLimit \
                  -numSteps $numSteps \
                  -stepInterval $stepInterval \
                  -numCycles 0 -decayRate $decayRate \
                  -finalSetpoint $finalSetpoint} results]} {
        APSSetVarAndUpdate status "Error: $results"
    }

    #pv unlink pvvalue
    
    APSSetVarAndUpdate status "Done Ramping Down"
}

proc StandardizeUp {args} {
    global pv lowerLimit upperLimit numSteps stepInterval numCycles decayRate
    global TimeList CurrentList pvvalue
    set TimeList ""
    set CurrentList ""

    set finalSetpoint $upperLimit
    set approachFromLowerLimit 1

    pv linkw pvvalue $pv
    pv umon pvvalue UpdatePlot

    APSSetVarAndUpdate status "Standardizing Up"

    if {[catch {APSStandardizeWithDecay -pv $pv \
                  -lowerLimit $lowerLimit \
                  -upperLimit $upperLimit \
                  -numSteps $numSteps \
                  -stepInterval $stepInterval \
                  -numCycles $numCycles \
                  -finalSetpoint $finalSetpoint -decayRate $decayRate \
                  -approachFromLowerLimit $approachFromLowerLimit} results]} {
        APSSetVarAndUpdate status "Error: $results"
    }

    #pv unlink pvvalue
    
    APSSetVarAndUpdate status "Done Standardizing Up"
}

proc StandardizeDown {args} {
    global pv lowerLimit upperLimit numSteps stepInterval numCycles decayRate
    global TimeList CurrentList pvvalue
    set TimeList ""
    set CurrentList ""

    set finalSetpoint $upperLimit
    set approachFromUpperLimit 1

    pv linkw pvvalue $pv
    pv umon pvvalue UpdatePlot

    APSSetVarAndUpdate status "Standardizing Down"

    if {[catch {APSStandardizeWithDecay -pv $pv \
                  -lowerLimit $lowerLimit \
                  -upperLimit $upperLimit \
                  -numSteps $numSteps \
                  -stepInterval $stepInterval \
                  -numCycles $numCycles \
                  -finalSetpoint $finalSetpoint -decayRate $decayRate \
                  -approachFromUpperLimit $approachFromUpperLimit} results]} {
        APSSetVarAndUpdate status "Error: $results"
    }

    #pv unlink pvvalue
    
    APSSetVarAndUpdate status "Done Standardizing Down"
}

