#
# Vitara Automatic Operation
#
# Required macros:
#	DEV				Device prefix for each new PV in this file
#	CH				Device prefix including channel
#	FREQ_MOTOR		PV name of Vitara freq motor
record( bo, "$(DEV):AUTO_MODE_ENABLE" )
{
	field( DESC, "Automatic mode enable/disable" )
	field( ZNAM, "Manual" )
	field( ONAM, "Automatic" )
	field( ZSV,  "MINOR" )
	field( OSV,  "NO_ALARM" )
}

record( ao, "$(DEV):PID_COARSE_AUTO_ON_LIMIT" )
{
	field( DESC, "PID AutoOn Threshhold" )
	field( EGU,  "V" )
	field( PREC, "4" )
	field( PINI, "YES" )
	field( VAL,  ".25" )
	info( autosaveFields, "VAL" )
}

record( ao, "$(DEV):PID_COARSE_AUTO_OFF_LIMIT" )
{
	field( DESC, "PID AutoOff Threshhold" )
	field( EGU,  "V" )
	field( PREC, "4" )
	field( PINI, "YES" )
	field( VAL,  ".05" )
	info( autosaveFields, "VAL" )
}

record( ao, "$(DEV):AUTO_SEARCH_TOL" )
{
	field( DESC, "Freq Err tolerance" )
	field( EGU,  "Hz" )
	field( PREC, "0" )
	field( PINI, "YES" )
	field( VAL,  "3" )
	info( autosaveFields, "VAL" )
}

# Enable coarse PID when the piezo element is too far from it's center
# ABS(piezo - center) > AUTO_ON_LIMIT
# and the chamber freq motor is not moving
# and the RF lock is enabled
record( calcout, "$(DEV):PID_COARSE_AUTO_ON" )
{
	field( DESC, "Enable coarse PID when too far" )
	field( INPA, "$(DEV):PID_COARSE.ERR CP MS" )
	field( INPB, "$(DEV):PID_COARSE_AUTO_ON_LIMIT CP MS" )
	field( INPC, "$(FREQ_MOTOR).DMOV" )
	field( INPD, "$(DEV):PHASE_LOCKED CP MS" )
	field( CALC, "(ABS(A)>B) && c=1 && d=1" )

	field( OUT, "$(DEV):PID_COARSE.FBON" )
	field( OOPT, "Transition To Non-zero" )
	field( OCAL, "1" )
	field( DISV, "0" )
	field( SDIS, "$(DEV):AUTO_MODE_ENABLE" )
}

# Disable PID when the piezo element is near it's center
# ABS(piezo - center) < AUTO_OFF_LIMIT
# and the chamber freq motor is not moving
# or if the RF lock is disabled
record( calcout, "$(DEV):PID_COARSE_AUTO_OFF" )
{
	field( DESC, "Disable coarse PID when at mean" )
	field( INPA, "$(DEV):PID_COARSE.ERR CP MS" )
	field( INPB, "$(DEV):PID_COARSE_AUTO_OFF_LIMIT CP MS" )
	field( INPC, "$(FREQ_MOTOR).DMOV" )
	field( INPD, "$(DEV):PHASE_LOCKED CP MS" )
	field( CALC, "((ABS(A)<B) && C=1)||D=0" )

	field( OUT, "$(DEV):PID_COARSE.FBON" )
	field( OOPT, "Transition To Non-zero" )
	field( DOPT, "Use OCAL" )
	field( DISV, "0" )
	field( SDIS, "$(DEV):AUTO_MODE_ENABLE" )
}

record( ao, "$(DEV):FREQ_SEARCH_VEL" )
{
	field( DESC, "Motor spd during Freq Srch" )
	field( EGU,  "mm/sec" )
	field( PREC, "4" )
	field( VAL,  "0.1" )
	field( PINI, "YES" )
	field( FLNK, "$(DEV):FREQ_MOTOR_VEL_CALC PP NMS" )
	info( autosaveFields, "VAL" )
}

record( ao, "$(DEV):PID_COARSE_VEL" )
{
	field( DESC, "Motor spd during PID op" )
	field( EGU,  "mm/sec" )
	field( PREC, "4" )
	field( VAL,  "0.0001" )
	field( PINI, "YES" )
	field( FLNK, "$(DEV):FREQ_MOTOR_VEL_CALC PP NMS" )
	info( autosaveFields, "VAL" )
}

record( calcout, "$(DEV):FREQ_MOTOR_VEL_CALC" )
{
	field( DESC, "Freq motor velocity calc" )
	field( INPA, "$(DEV):PID_COARSE.FBON CP" )
	field( INPB, "$(DEV):FREQ_SEARCH_VEL NPP NMS" )
	field( INPC, "$(DEV):PID_COARSE_VEL NPP NMS" )
	field( CALC, "A=0?B:C" )
	field( OUT,  "$(FREQ_MOTOR).VELO CA NMS" )
	field( DISV, "0" )
	field( SDIS, "$(DEV):AUTO_MODE_ENABLE" )
}

record( calcout, "$(DEV):AUTO_SEARCH_CALC" )
{
	field( DESC, "Auto Search watcher" )
	field( SCAN, "2 second" )
	field( INPA, "$(DEV):FREQ_ERR_LAST_N_RMS NPP MS" )
	field( INPB, "$(DEV):AUTO_SEARCH_TOL     NPP NMS" )
	field( INPD, "$(DEV):PHASE_LOCKED NPP NMS" )
	field( INPE, "$(DEV):FREQ_SEARCH_ACTIVE  NPP MS" )
	field( CALC, "A>B&&!D&&!E" )
	field( OUT,  "$(DEV):FREQ_SEARCH_START    PP MS" )
	field( OOPT, "When Non-zero" )
	field( DISV, "0" )
	field( SDIS, "$(DEV):AUTO_MODE_ENABLE" )
}

record( calcout, "$(DEV):AUTO_RF_LOCK_ENABLE_CALC" )
{
	field( DESC, "Auto RF Lock watcher" )
	field( SCAN, "2 second" )
	field( INPA, "$(CH)DIODE_PWR_RMS  NPP MS" )
	field( INPB, "$(CH)DIODE_PWR.LOLO NPP MS" )
	field( INPC, "$(CH)DIODE_PWR.HIHI NPP MS" )
	field( INPD, "$(CH)RF_PWR_RMS     NPP MS" )
	field( INPE, "$(CH)RF_PWR.LOLO    NPP MS" )
	field( INPF, "$(CH)RF_PWR.HIHI    NPP MS" )
	field( INPG, "$(DEV):FREQ_SEARCH_ACTIVE CP MS" )
	field( CALC, "(A>B)&&(A<C)&&(D>E)&&(D<F)&&!G" )
	field( OUT,  "$(DEV):RF_LOCK_ENABLE PP MS" )
#	field( OOPT, "On Change" )
	field( DISV, "0" )
	field( SDIS, "$(DEV):AUTO_MODE_ENABLE" )
}

