#!/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: shang $"
APSApplication . -name "SRXrayP1SquareMatrixSteering"  -version $CVSRevisionAuthor \
  -overview "Does SR Xray bpm P1 square matrix steering for xray bpm sanity check."

proc ApplySteering {args} {
    global sectorList checked plane steerAngle accumAngle
    
    #steerAngle is micro units, bpm setpoint is in mm units 
    #need change micro to mm  Xray bpm upstream and downsteam distance is 8m meter
    #from angle to position delta, delta = angle * 8 / 2 = angle * 4
    set delta [expr $steerAngle * 4.0  * 1.0e-3]
  
    set negPutList ""
    set posPutList ""
    foreach sector $sectorList {
        if $checked($sector) {
            set sector1 [expr $sector +1]
            if {$plane!="both"} {
                lappend negPutList S${sector}B:P1:ms:${plane}:SetpointAO=[expr -1.0 * $delta]
                lappend posPutList S${sector1}A:P1:ms:${plane}:SetpointAO=$delta
                
               
            } else {
                lappend negPutList S${sector}B:P1:ms:x:SetpointAO=[expr -1.0 * $delta]
                lappend negPutList S${sector}B:P1:ms:y:SetpointAO=[expr -1.0 * $delta]

                lappend posPutList S${sector1}A:P1:ms:x:SetpointAO=$delta
                lappend posPutList S${sector1}A:P1:ms:y:SetpointAO=$delta
            }
        }
    }
    if [llength $negPutList] {
        SetStatus "Make $steerAngle micro radian steering for selected steering..."
        SetStatus "cavput -list=[join $negPutList ,] -delta -pend=20 "
        SetStatus "cavput -list=[join $posPutList ,] -delta -pend=20"
        if [catch {exec cavput -list=[join $negPutList ,] -delta -pend=20  
            exec cavput -list=[join $posPutList ,]  -delta -pend=20 } result] {
            return -code error "Error changing setpoints: $result"
        }
        set accumAngle [expr $accumAngle + $steerAngle]
        update
        SetStatus "done."
    } else {
        SetStatus "No sectors chosen!"
    }
}

proc SetStatus {text} {
    global status
    set status $text
    update
}

set status ""

APSScrolledStatus .status -parent .userFrame -width 60 -textVariable status

set sectorList [exec sdds2stream -col=Sector /home/helios/oagData/sr/IDs/sectors.sdds]

set varList ""
set buttonList ""
foreach sector $sectorList {
    set checked($sector) 0
    lappend buttonList [format %02d $sector] 
    lappend varList checked($sector)
}

set checkButtonList [APSCheckButtonFrame .sector -parent .userFrame -buttonList $buttonList -variableList $varList  \
                       -label "ID sectors:" \
                       -orientation horizontal -limitPerRow 8 -allNone 1]

set steerAngle 5
set accumAngle 0
APSLabeledEntry .angle -parent .userFrame -label "Steering Angle (micro radians): " \
    -textVariable steerAngle -width 40
APSLabeledOutput .accum -parent .userFrame -label "Accumelated Steering Angle (micor radians):" -textVariable accumAngle -width 40

set plane x
APSRadioButtonFrame .plane -parent .userFrame -label "Steer plane: " -buttonList {x y both} -valueList {x y both} \
  -variable plane -orientation horizontal

APSButton .apply -parent .userFrame -text "APPLY" -command "ApplySteering"

