#!/bin/bash
#--------------------------------------------------------------------------
# This script the new Longitudinal fast feedback (FB04:LG01) and
# enables Joe's 6x6 Matlab feedback.
#
# The setpoints from Longitudinal fast feedback are copied to Joe's
# Matlab feedback PVs.
#
# Date: Feb/24/2011
# Author: L.Piccoli
#--------------------------------------------------------------------------

#--------------------------------------------------------------------------
# Return the current status of the 6x6 feedback.
#
# @return "ON" if at least one of the 6x6 PVs is 1, "OFF" if all 6x6 PVs
# are 0
#--------------------------------------------------------------------------
function is6x6On() {
    dl1State=`caget -t -n SIOC:SYS0:ML00:AO290 | bc`
    bc1EState=`caget -t -n SIOC:SYS0:ML00:AO292 | bc`
    bc1CState=`caget -t -n SIOC:SYS0:ML00:AO293 | bc`
    bc2EState=`caget -t -n SIOC:SYS0:ML00:AO294 | bc`
    bc2CState=`caget -t -n SIOC:SYS0:ML00:AO295 | bc`
    dl2State=`caget -t -n SIOC:SYS0:ML00:AO296 | bc`
    if [ "$dl1State" -eq 0 ] && \
       [ "$bc1EState" -eq 0 ] && \
       [ "$bc1CState" -eq 0 ] && \
       [ "$bc2EState" -eq 0 ] && \
       [ "$bc2CState" -eq 0 ] && \
       [ "$dl2State" -eq 0 ]; then
      echo "OFF"
    else
      echo "ON"
    fi
}

#--------------------------------------------------------------------------
# Wrapper for a simple caput <PV> <value> command. The output generated
# by caput is redirected to /dev/null, so it does not polute the output
# of the script.
#
# @return "ON" if at least one of the 6x6 PVs is 1, "OFF" if all 6x6 PVs
# are 0
#--------------------------------------------------------------------------
function ca_put {
    caput $1 $2 > /dev/null
}

#--------------------------------------------------------------------------
# Ask whether operators really want to switch
#
# @return 0 if switching should proceed, 1 if script should exit!
#--------------------------------------------------------------------------
function areYouSure {
  lem_energy_gev=`caget -t REFS:DMP1:400:EDES`
  lem_energy_mev=`echo $lem_energy_gev*1000 | bc`
  dl2_setpoint_mev=`caget -t FBCK:FB04:LG01:S6DES`
  vernier_mev=`caget -t FBCK:FB04:LG01:DL2VERNIER`

  msg="Switching to 6x6 will restore the following values:

BC1 peak current setpoint: `caget -t FBCK:FB04:LG01:S3DES` amps
BC2 peak current setpoint: `caget -t FBCK:FB04:LG01:S5DES` amps
L3 energy Vernier: `caget -t FBCK:FB04:LG01:DL2VERNIER` MeV"

  zenity --question --title "Switch to 6x6?" --text "$msg"
  echo $?
}

#--------------------------------------------------------------------------
# Main
#--------------------------------------------------------------------------

#
# Make sure that the 6x6 Matlab feedback is OFF before switching
# from the Longitudinal fast feedback
#
joeState=`is6x6On`
if [ "$state" == "ON" ]; then
  msg="Joe's 6x6 Matlab feedback must be OFF to switch
from the new Longitudinal feedback!

Please make sure Joe's 6x6 is OFF before trying again.

The 6x6 is considered on if at least one of the
states is enabled."

  zenity --error --text "$msg" --title "Error"
  exit 0
fi

# Check if switch is really wanted and show new setpoints
result=`areYouSure`
if [ "$result" == "1" ]; then
  exit 0
fi

#
# Turn Longitudinal fast feedback OFF
#
echo "1) Turning Longitudital feedback off"
ca_put FBCK:FB04:LG01:STATE 0

#
# Disable Longitudinal alarms
#
disableALHGroup ACTLNG0
disableALHGroup SENSLNG0
disableALHGroup STATELNG0
disableALHGroup STATLNG0

#
# Change messages that show which feedback is enabled/disabled
#
ca_put SIOC:SYS0:ML02:AO125 2
ca_put SIOC:SYS0:ML02:AO126 1

#
# Copy setpoint parameters from the Longitudinal feedback to the 6x6 feedback
#
echo "2) Copying BC1 peak current set point"
ca_put SIOC:SYS0:ML00:AO016 `caget -t FBCK:FB04:LG01:S3DES`

echo "3) Copying BC2 peak current set point"
ca_put SIOC:SYS0:ML00:AO044 `caget -t FBCK:FB04:LG01:S5DES`

#echo "4) Copying L3 FCOM energy to L3 CA energy"
#ca_put SIOC:SYS0:ML00:AO075 `caget -t FBCK:FB04:LG01:A6`

echo "4) Setting L3 Energy Vernier"
lem_energy_gev=`caget -t REFS:DMP1:400:EDES`
lem_energy_mev=`echo $lem_energy_gev*1000 | bc`
dl2_setpoint_mev=`caget -t FBCK:FB04:LG01:S6DES`
vernier_mev=`caget -t FBCK:FB04:LG01:DL2VERNIER`
ca_put SIOC:SYS0:ML00:AO289 $vernier_mev

# Turn all actuator FBCK PVs OFF
for x in `caget -t FBCK:FB04:LG01:A{1,2,3,4,5,6}DEVNAME`; do
    ca_put $x:FBCK 0;
done

#
# Show parameters and asks permission to enable 6x6
# feedback.
#
echo "----------------------------------------------------------"
echo "> BC1 peak current setpoint: `caget -t SIOC:SYS0:ML00:AO016` amps"
echo "> BC2 peak current setpoint: `caget -t SIOC:SYS0:ML00:AO044` amps"
echo "> L3 energy                : `caget -t SIOC:SYS0:ML00:AO075` MeV"
echo "----------------------------------------------------------"

# Enable "fancy" phase control
ca_put SIOC:SYS0:ML00:AO058 1

# Enable Nate's timing feedback thingy
ca_put SIOC:SYS0:ML01:AO281 1
echo "Please turn 6x6 feedback ON!"

msg="You can now turn the 6x6 feedback on.

Notice that these values have been set:

BC1 peak current setpoint: `caget -t SIOC:SYS0:ML00:AO016` amps
BC2 peak current setpoint: `caget -t SIOC:SYS0:ML00:AO044` amps
L3 energy                : `caget -t SIOC:SYS0:ML00:AO075` MeV"

zenity --info --text "$msg" --title "6x6 is ready"

echo "Done."
