#!/bin/bash -norc
#==============================================================
#
#  Abs:  Starts  matlab -> ff feedback switching script
#
#  Name:switchMatlabToFF
#   $1 :  matlab feedback
#   $2:  ff feedback
#   $3, $4, $5: numbers for ML02 PVs
#  
#  eg: switchMatlabToFF INL1 FBO1:TR02 101 102 103 INL
#  eg: switchMatlabToFF LTL0 FBO3:TR01 104 105 106 LTL
# ------------------------------------------------------------
# This script gets called from lclshome .
# ------------------------------------------------------------
#
#  Facility:  SLAC
#
#  Auth: nov 5 2010 dfairley
#
# Mod 
#
# 05-NOV-2010 d. fairley created
#
#==============================================================
#
# turn off matlab feedback and disable the matlab feedback general STATE PV so it cannot run
caput FBCK:$1:1:STATE 0
# turn off and disable the general STATE PV
caput FBCK:$6:1:STATE 0
caput FBCK:$6:1:STATE.DISV 0
# clear the matlab feedback statusdesc string
caput FBCK:$6:1:STATUSDESC.VAL ""
# 
# enable the fast feedback STATE PV so it can be started
caput FBCK:$2:STATE.DISV 1
caput FBCK:$2:INSTALLED 1
#
#
# disable to matlab fb alarms
#
disableALHGroup XCORGb$1
disableALHGroup YCORGb$1
disableALHGroup FBCKGb$1
disableALHGroup BPMSGb$1
#
# enable to fast fb alarms
#
enableALHGroup ACT$1
enableALHGroup SENS$1
enableALHGroup STATE$1
enableALHGroup STAT$1

# write instructions to status strings for edm display
#
# swaps strings for matlab feedback OFF 1=off, 2=on
# matlab string
caput SIOC:SYS0:ML02:A0$3.DESC "RESERVED FOR FF"
caput SIOC:SYS0:ML02:AO$3 1
# ff string
caput SIOC:SYS0:ML02:AO$4.DESC "RESERVED FOR FF"
caput SIOC:SYS0:ML02:AO$4 2

# If the feedback is the Bunch Change, change Sonyas PV
if [ "$2" == "FB02:GN01" ]; then
    caput IOC:IN20:BP01:QANNH.INPA FBCK:FB02:GN01:S1DES CPP
    caput IOC:IN20:BP01:QANNL.INPA FBCK:FB02:GN01:S1DES CPP
fi
