##########################################################################
# Description:
# This database gets the digital input status from the Aerotech controller
# and splits it into two MBBI records.
# This database also gets axis specifc status and fault pv`s
# Disableallpvs PV can be used to disable all the pv`s for the device
#
# Macros:
# U 	- Device prefix
# PORT 	- Aerotech asyn port
# AXIS 	- Aerotech axis identifier
# SCAN 	- Desired scan rate
# MAD   - mad device name
##########################################################################
record(ai, "$(DEVICE):$(LJAW):DISTAT") {
  field(DESC, "digital inputs")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getDINall(@$(AXIS1)) $(ASYN_PORT)")
  field(SCAN, "1 second")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):DISTATS1") {
  field(DESC, "Digital input lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):DISTAT CP MS")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):DISTATS2") {
  field(DESC, "Digital input higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):DISTAT CP MS")
  field(SHFT, "16")
}

record(ai, "$(DEVICE):$(LJAW):STAT") {
  field(DESC, "axis status")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto axisStatus(@$(AXIS1)) $(ASYN_PORT)")
  field(SCAN, "1 second")
}

record(ai, "$(DEVICE):$(LJAW):FAULT") {
  field(DESC, "axis fault")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto axisFault(@$(AXIS1)) $(ASYN_PORT)")
  field(SCAN, "1 second")
}

record(ai, "$(DEVICE):$(LJAW):AUTOHOMETRIGFAULT") {
  field(DESC, "fault to trigger autohome")
  field(DTYP, "stream")
  field(INP,  "@aerotech.proto getIGLOBAL(60) $(ASYN_PORT)")
  field(PINI, "YES")
  field(SCAN,"1 second")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):AUTOHOMETRIGFAULTS1") {
  field(DESC, "fault lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):AUTOHOMETRIGFAULT CP MS")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):AUTOHOMETRIGFAULTS2") {
  field(DESC, "fault higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):AUTOHOMETRIGFAULT CP MS")
  field(SHFT, "16")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):STATS1") {
  field(DESC, "status lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):STAT CP MS")
  field(NOBT, "16")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):STATS2") {
  field(DESC, "status higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):STAT CP MS")
  field(SHFT, "16")
  field(NOBT, "16")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):FAULTS1") {
  field(DESC, "fault lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):FAULT CP MS")
  field(NOBT, "16")
}

record(mbbiDirect, "$(DEVICE):$(LJAW):FAULTS2") {
  field(DESC, "fault higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(LJAW):FAULT CP MS")
  field(SHFT, "16")
  field(NOBT, "16")
}

record(bi, "$(DEVICE):$(LJAW):ENABLEDSTAT") {
  field(DESC, "Axis enabled")
  field(INP,  "$(DEVICE):$(LJAW):STATS1.B0 CP MS")
  field(ZNAM, "Disabled")
  field(ONAM, "Enabled")
}

record(bi, "$(DEVICE):$(LJAW):HOMEDSTAT") {
  field(DESC, "Axis homed")
  field(INP,  "$(DEVICE):$(LJAW):STATS1.B1 CP MS")
  field(ZNAM, "Not Homed")
  field(ONAM, "Homed")
  field(ZSV, "MAJOR")
  field(OSV, "NO_ALARM")
}

# If both limit switch show as active
record(calcout, "$(DEVICE):$(LJAW):CABLESTAT_CALC") {
  field(DESC, "Motor Cable Status")
  field(INPA, "$(DEVICE):$(LJAW):STATS2.B6 CP MS")
  field(INPB, "$(DEVICE):$(LJAW):STATS2.B7 CP MS")
  field(CALC, "(A&&B)")
  field(FLNK, "$(DEVICE):$(LJAW):CABLESTAT")
}

record(bo, "$(DEVICE):$(LJAW):CABLESTAT") {
  field(DESC, "Motor Cable Status")
  field(DOL,  "$(DEVICE):$(LJAW):CABLESTAT_CALC MS")
  field(ONAM, "Motor cable disconnected")
  field(ZNAM, "OK")
  field(ZSV, "MAJOR")
  field(OSV, "NO_ALARM")
}

record(bi, "$(DEVICE):$(LJAW):MOVEACTIVE") {
  field(DESC, "Move active")
  field(INP,  "$(DEVICE):$(LJAW):STATS1.B3 CP MS")
  field(ZNAM, "Not moving")
  field(ONAM, "Moving")
  field(OSV, "MINOR")
  field(ZSV, "NO_ALARM")
}

# When Move Active PV transitions to 0. 
# Then, Move complete. 
# Then count up.
record(calcout, "$(DEVICE):$(LJAW):CALC_COUNTER1") {
  field(DESC, "Check if moving")
  field(CALC, "A")
  field(INPA, "$(DEVICE):$(LJAW):MOVEACTIVE CP")
  field(OUT, "$(DEVICE):$(LJAW):USECYCLES.PROC")
# When Transition to Zero
  field(OOPT, "4")
  field(ASG,  "Internal")
}

# Store device use in counts.
# This record counts up 1 after every collimator use
record(calc, "$(DEVICE):$(LJAW):USECYCLES") {
  field(DESC, "Counts the times collimator is used")
  field(CALC, "VAL+1")
  field(HIHI, "900")
  field(HIGH, "800")
  field(HHSV, "MAJOR")
  field(HSV, "MINOR")
  info(autosaveFields, "VAL")
}

# When Move Active PV transitions to 0. 
# Then move is complete
# Then start sequence to delay by 8 seconds
# Set Done move Bit after 8 seconds
# Done to compensate for slow LVDT!
# Kludge
record(calcout, "$(DEVICE):$(LJAW):CALC_COUNTER2") {
  field(DESC, "Check if moving")
  field(CALC, "A")
  field(INPA, "$(DEVICE):$(LJAW):MOVEACTIVE CP")
  field(OUT, "$(DEVICE):$(LJAW):DONEMOVE_SEQ.PROC")
# When Transition to Zero
  field(OOPT, "4")
  field(ASG,  "Internal")
  field(FLNK, "$(DEVICE):$(LJAW):DONEMOVE_SEQ")
}

record(seq, "$(DEVICE):$(LJAW):DONEMOVE_SEQ") {
  field(DESC, "Done Move")
  field(DLY1, "8")
  field(DOL1, "0")
  field(LNK1, "$(DEVICE):$(LJAW):DONEMOVE CA")
  field(ASG,  "Internal")
}

record(bi, "$(DEVICE):$(LJAW):DONEMOVE") {
  field(DESC, "Move active")
  field(ZNAM, "Move complete")
  field(ONAM, "Move active")
}

# When Move Active transitions to 1. 
# Then device active
# Then set DONEMOVEPV appropriately
record(calcout, "$(DEVICE):$(LJAW):CALC_COUNTER3") {
  field(DESC, "Check if moving")
  field(CALC, "A")
  field(INPA, "$(DEVICE):$(LJAW):MOVEACTIVE CP")
  field(OUT, "$(DEVICE):$(LJAW):DONEMOVE CA")
# When Transition to One
  field(OOPT, "5")
  field(ASG,  "Internal")
}

record(stringout, "$(DEVICE):$(LJAW):FAULTACK") {
  field(DESC, "Ack fault on axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisFaultAck(@$(AXIS1)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(LJAW):ENABLE") {
  field(DESC, "Enable axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisEnable(@$(AXIS1)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(LJAW):ABORT") {
  field(DESC, "Abort axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisAbort(@$(AXIS1)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(LJAW):DISABLE") {
  field(DESC, "Disable axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisDisable(@$(AXIS1)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(LJAW):HOME") {
  field(DESC, "Home axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisHome(@$(AXIS1)) $(ASYN_PORT)")
}

record(ai, "$(DEVICE):$(LJAW):IDES") {
  field(DESC, "axis current")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getCur(@$(AXIS1)) $(ASYN_PORT)")
  field(SCAN, "1 second")
  field(PREC, 2)
}

record(ai, "$(DEVICE):$(LJAW):VFBK") {
  field(DESC, "axis velocity")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getVelo(@$(AXIS1)) $(ASYN_PORT)")
  field(SCAN, "1 second")
  field(PREC, 2)
}

record(ai, "$(DEVICE):$(LJAW):EXTPOSN") {
  field(DESC, "Aux Encoder Position")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto extPos(@$(AXIS1)) $(ASYN_PORT)")
  field(SCAN, "1 second")
  field(PREC, 3)
  field(LINR, "SLOPE")
  info(autosaveFields, "ESLO EOFF EGU")
}

record(ai, "$(DEVICE):$(LJAW):EncOffset") {
    field(DESC, "$(MAD)-$(LJAW) Encoder offset")
    field(DTYP, "stream")
    field(INP, "@aerotech.proto getEncOff(@$(AXIS1)) $(ASYN_PORT) ")
    field(PINI, "YES")
    field(PREC, "3")
    field(EGU, "mm")
}

#
# Record set Average Current Threshold to controller
#
record(ao, "$(DEVICE):$(LJAW):SET_AVG_CURRENT") {
  field(DESC, "Set avg current to AB")
  field(DTYP, "stream")
  field(EGU, "A")
  field(OUT,  "@aerotech.proto setAvgCurrent(@$(AXIS1)) $(ASYN_PORT)")
}

#
# RBV for Average Current Threshold
#
record(ai, "$(DEVICE):$(LJAW):GET_AVG_CURRENT") {
  field(DESC, "RBV for avg current")
  field(DTYP, "stream")
  field(INP,  "@aerotech.proto getAvgCurrent(@$(AXIS1)) $(ASYN_PORT)")
  field(EGU, "A")
  field(PINI, "YES")
  field(SCAN,"1 second")
}

#
# Record set Max Current Clamp to controller
#
record(ao, "$(DEVICE):$(LJAW):SET_MAX_CURRENT") {
  field(DESC, "Set Max Current Clamp")
  field(DTYP, "stream")
  field(EGU, "A")
  field(OUT,  "@aerotech.proto setMaxCurrentClamp(@$(AXIS1)) $(ASYN_PORT)")
}

#
# RBV for Max Current Clamp
#
record(ai, "$(DEVICE):$(LJAW):GET_MAX_CURRENT") {
  field(DESC, "RBV for Max Current Clamp")
  field(DTYP, "stream")
  field(INP,  "@aerotech.proto getMaxCurrentClamp(@$(AXIS1)) $(ASYN_PORT)")
  field(EGU, "A")
  field(PINI, "YES")
  field(SCAN,"1 second")
}

#
# Commit Parameters after set parameters
#
record(stringout, "$(DEVICE):$(LJAW):COMMITPARAM") {
  field(DESC, "Commit Parameters")
  field(DTYP, "stream")
  field(OUT, "@aerotech.proto commitParameters $(ASYN_PORT)")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP0IN0") {
  field(DESC, "Port 0 Input 0")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B0 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP0IN1") {
  field(DESC, "Port 0 Input 1")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B1 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP0IN2") {
  field(DESC, "Port 0 Input 2")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B2 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP0IN3") {
  field(DESC, "Port 0 Input 3")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B3 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP0IN4") {
  field(DESC, "Port 0 Input 4")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B4 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP0IN5") {
  field(DESC, "Port 0 Input 5")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B5 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN0") {
  field(DESC, "Port 1 Input 0")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B8 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN1") {
  field(DESC, "Port 1 Input 1")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.B9 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN2") {
  field(DESC, "Port 1 Input 2")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.BA CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN3") {
  field(DESC, "Port 1 Input 3")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.BB CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN4") {
  field(DESC, "Port 1 Input 4")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.BC CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN5") {
  field(DESC, "Port 1 Input 5")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.BD CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN6") {
  field(DESC, "Port 1 Input 6")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.BE CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP1IN7") {
  field(DESC, "Port 1 Input 7")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS0.BF CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN0") {
  field(DESC, "Port 2 Input 0")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B0 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN1") {
  field(DESC, "Port 2 Input 1")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B1 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN2") {
  field(DESC, "Port 2 Input 2")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B2 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN3") {
  field(DESC, "Port 2 Input 3")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B3 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN4") {
  field(DESC, "Port 2 Input 4")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B4 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN5") {
  field(DESC, "Port 2 Input 5")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B5 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN6") {
  field(DESC, "Port 2 Input 6")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B6 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(LJAW):DIP2IN7") {
  field(DESC, "Port 2 Input 7")
  field(INP,  "$(DEVICE):$(LJAW):DISTATS1.B7 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

##########################################################################
# Description:
# This database gets the digital input status from the Aerotech controller
# and splits it into two MBBI records.
# This database also gets axis specifc status and fault pv`s
# Disableallpvs PV can be used to disable all the pv`s for the device
#
# Macros:
# U 	- Device prefix
# PORT 	- Aerotech asyn port
# AXIS 	- Aerotech axis identifier
# SCAN 	- Desired scan rate
# MAD   - mad device name
##########################################################################
record(ai, "$(DEVICE):$(RJAW):DISTAT") {
  field(DESC, "digital inputs")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getDINall(@$(AXIS2)) $(ASYN_PORT)")
  field(SCAN, "1 second")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):DISTATS1") {
  field(DESC, "Digital input lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):DISTAT CP MS")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):DISTATS2") {
  field(DESC, "Digital input higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):DISTAT CP MS")
  field(SHFT, "16")
}

record(ai, "$(DEVICE):$(RJAW):STAT") {
  field(DESC, "axis status")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto axisStatus(@$(AXIS2)) $(ASYN_PORT)")
  field(SCAN, "1 second")
}

record(ai, "$(DEVICE):$(RJAW):FAULT") {
  field(DESC, "axis fault")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto axisFault(@$(AXIS2)) $(ASYN_PORT)")
  field(SCAN, "1 second")
}

record(ai, "$(DEVICE):$(RJAW):AUTOHOMETRIGFAULT") {
  field(DESC, "fault to trigger autohome")
  field(DTYP, "stream")
  field(INP,  "@aerotech.proto getIGLOBAL(60) $(ASYN_PORT)")
  field(PINI, "YES")
  field(SCAN,"1 second")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):AUTOHOMETRIGFAULTS1") {
  field(DESC, "fault lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):AUTOHOMETRIGFAULT CP MS")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):AUTOHOMETRIGFAULTS2") {
  field(DESC, "fault higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):AUTOHOMETRIGFAULT CP MS")
  field(SHFT, "16")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):STATS1") {
  field(DESC, "status lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):STAT CP MS")
  field(NOBT, "16")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):STATS2") {
  field(DESC, "status higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):STAT CP MS")
  field(SHFT, "16")
  field(NOBT, "16")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):FAULTS1") {
  field(DESC, "fault lower bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):FAULT CP MS")
  field(NOBT, "16")
}

record(mbbiDirect, "$(DEVICE):$(RJAW):FAULTS2") {
  field(DESC, "fault higher bits")
  field(DTYP, "Raw Soft Channel")
  field(INP, "$(DEVICE):$(RJAW):FAULT CP MS")
  field(SHFT, "16")
  field(NOBT, "16")
}

record(bi, "$(DEVICE):$(RJAW):ENABLEDSTAT") {
  field(DESC, "Axis enabled")
  field(INP,  "$(DEVICE):$(RJAW):STATS1.B0 CP MS")
  field(ZNAM, "Disabled")
  field(ONAM, "Enabled")
}

record(bi, "$(DEVICE):$(RJAW):HOMEDSTAT") {
  field(DESC, "Axis homed")
  field(INP,  "$(DEVICE):$(RJAW):STATS1.B1 CP MS")
  field(ZNAM, "Not Homed")
  field(ONAM, "Homed")
  field(ZSV, "MAJOR")
  field(OSV, "NO_ALARM")
}

# If both limit switch show as active
record(calcout, "$(DEVICE):$(RJAW):CABLESTAT_CALC") {
  field(DESC, "Motor Cable Status")
  field(INPA, "$(DEVICE):$(RJAW):STATS2.B6 CP MS")
  field(INPB, "$(DEVICE):$(RJAW):STATS2.B7 CP MS")
  field(CALC, "(A&&B)")
  field(FLNK, "$(DEVICE):$(RJAW):CABLESTAT")
}

record(bo, "$(DEVICE):$(RJAW):CABLESTAT") {
  field(DESC, "Motor Cable Status")
  field(DOL,  "$(DEVICE):$(RJAW):CABLESTAT_CALC MS")
  field(ONAM, "Motor cable disconnected")
  field(ZNAM, "OK")
  field(ZSV, "MAJOR")
  field(OSV, "NO_ALARM")
}

record(bi, "$(DEVICE):$(RJAW):MOVEACTIVE") {
  field(DESC, "Move active")
  field(INP,  "$(DEVICE):$(RJAW):STATS1.B3 CP MS")
  field(ZNAM, "Not moving")
  field(ONAM, "Moving")
  field(OSV, "MINOR")
  field(ZSV, "NO_ALARM")
}

# When Move Active PV transitions to 0. 
# Then, Move complete. 
# Then count up.
record(calcout, "$(DEVICE):$(RJAW):CALC_COUNTER1") {
  field(DESC, "Check if moving")
  field(CALC, "A")
  field(INPA, "$(DEVICE):$(RJAW):MOVEACTIVE CP")
  field(OUT, "$(DEVICE):$(RJAW):USECYCLES.PROC")
# When Transition to Zero
  field(OOPT, "4")
  field(ASG,  "Internal")
}

# Store device use in counts.
# This record counts up 1 after every collimator use
record(calc, "$(DEVICE):$(RJAW):USECYCLES") {
  field(DESC, "Counts the times collimator is used")
  field(CALC, "VAL+1")
  field(HIHI, "900")
  field(HIGH, "800")
  field(HHSV, "MAJOR")
  field(HSV, "MINOR")
  info(autosaveFields, "VAL")
}

# When Move Active PV transitions to 0. 
# Then move is complete
# Then start sequence to delay by 8 seconds
# Set Done move Bit after 8 seconds
# Done to compensate for slow LVDT!
# Kludge
record(calcout, "$(DEVICE):$(RJAW):CALC_COUNTER2") {
  field(DESC, "Check if moving")
  field(CALC, "A")
  field(INPA, "$(DEVICE):$(RJAW):MOVEACTIVE CP")
  field(OUT, "$(DEVICE):$(RJAW):DONEMOVE_SEQ.PROC")
# When Transition to Zero
  field(OOPT, "4")
  field(ASG,  "Internal")
  field(FLNK, "$(DEVICE):$(RJAW):DONEMOVE_SEQ")
}

record(seq, "$(DEVICE):$(RJAW):DONEMOVE_SEQ") {
  field(DESC, "Done Move")
  field(DLY1, "8")
  field(DOL1, "0")
  field(LNK1, "$(DEVICE):$(RJAW):DONEMOVE CA")
  field(ASG,  "Internal")
}

record(bi, "$(DEVICE):$(RJAW):DONEMOVE") {
  field(DESC, "Move active")
  field(ZNAM, "Move complete")
  field(ONAM, "Move active")
}

# When Move Active transitions to 1. 
# Then device active
# Then set DONEMOVEPV appropriately
record(calcout, "$(DEVICE):$(RJAW):CALC_COUNTER3") {
  field(DESC, "Check if moving")
  field(CALC, "A")
  field(INPA, "$(DEVICE):$(RJAW):MOVEACTIVE CP")
  field(OUT, "$(DEVICE):$(RJAW):DONEMOVE CA")
# When Transition to One
  field(OOPT, "5")
  field(ASG,  "Internal")
}

record(stringout, "$(DEVICE):$(RJAW):FAULTACK") {
  field(DESC, "Ack fault on axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisFaultAck(@$(AXIS2)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(RJAW):ENABLE") {
  field(DESC, "Enable axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisEnable(@$(AXIS2)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(RJAW):ABORT") {
  field(DESC, "Abort axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisAbort(@$(AXIS2)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(RJAW):DISABLE") {
  field(DESC, "Disable axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisDisable(@$(AXIS2)) $(ASYN_PORT)")
}

record(stringout, "$(DEVICE):$(RJAW):HOME") {
  field(DESC, "Home axis")
  field(DTYP, "stream")
  field(OUT,  "@aerotech.proto axisHome(@$(AXIS2)) $(ASYN_PORT)")
}

record(ai, "$(DEVICE):$(RJAW):IDES") {
  field(DESC, "axis current")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getCur(@$(AXIS2)) $(ASYN_PORT)")
  field(SCAN, "1 second")
  field(PREC, 2)
}

record(ai, "$(DEVICE):$(RJAW):VFBK") {
  field(DESC, "axis velocity")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getVelo(@$(AXIS2)) $(ASYN_PORT)")
  field(SCAN, "1 second")
  field(PREC, 2)
}

record(ai, "$(DEVICE):$(RJAW):EXTPOSN") {
  field(DESC, "Aux Encoder Position")
  field(PINI, "YES")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto extPos(@$(AXIS2)) $(ASYN_PORT)")
  field(SCAN, "1 second")
  field(PREC, 3)
  field(LINR, "SLOPE")
  info(autosaveFields, "ESLO EOFF EGU")
}

record(ai, "$(DEVICE):$(RJAW):EncOffset") {
    field(DESC, "$(MAD)-$(RJAW) Encoder offset")
    field(DTYP, "stream")
    field(INP, "@aerotech.proto getEncOff(@$(AXIS2)) $(ASYN_PORT) ")
    field(PINI, "YES")
    field(PREC, "3")
    field(EGU, "mm")
}

#
# Record set Average Current Threshold to controller
#
record(ao, "$(DEVICE):$(RJAW):SET_AVG_CURRENT") {
  field(DESC, "Set avg current to AB")
  field(DTYP, "stream")
  field(EGU, "A")
  field(OUT,  "@aerotech.proto setAvgCurrent(@$(AXIS2)) $(ASYN_PORT)")
}

#
# RBV for Average Current Threshold
#
record(ai, "$(DEVICE):$(RJAW):GET_AVG_CURRENT") {
  field(DESC, "RBV for avg current")
  field(DTYP, "stream")
  field(INP,  "@aerotech.proto getAvgCurrent(@$(AXIS2)) $(ASYN_PORT)")
  field(EGU, "A")
  field(PINI, "YES")
  field(SCAN,"1 second")
}

#
# Record set Max Current Clamp to controller
#
record(ao, "$(DEVICE):$(RJAW):SET_MAX_CURRENT") {
  field(DESC, "Set Max Current Clamp")
  field(DTYP, "stream")
  field(EGU, "A")
  field(OUT,  "@aerotech.proto setMaxCurrentClamp(@$(AXIS2)) $(ASYN_PORT)")
}

#
# RBV for Max Current Clamp
#
record(ai, "$(DEVICE):$(RJAW):GET_MAX_CURRENT") {
  field(DESC, "RBV for Max Current Clamp")
  field(DTYP, "stream")
  field(INP,  "@aerotech.proto getMaxCurrentClamp(@$(AXIS2)) $(ASYN_PORT)")
  field(EGU, "A")
  field(PINI, "YES")
  field(SCAN,"1 second")
}

#
# Commit Parameters after set parameters
#
record(stringout, "$(DEVICE):$(RJAW):COMMITPARAM") {
  field(DESC, "Commit Parameters")
  field(DTYP, "stream")
  field(OUT, "@aerotech.proto commitParameters $(ASYN_PORT)")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP0IN0") {
  field(DESC, "Port 0 Input 0")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B0 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP0IN1") {
  field(DESC, "Port 0 Input 1")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B1 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP0IN2") {
  field(DESC, "Port 0 Input 2")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B2 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP0IN3") {
  field(DESC, "Port 0 Input 3")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B3 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP0IN4") {
  field(DESC, "Port 0 Input 4")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B4 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP0IN5") {
  field(DESC, "Port 0 Input 5")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B5 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN0") {
  field(DESC, "Port 1 Input 0")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B8 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN1") {
  field(DESC, "Port 1 Input 1")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.B9 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN2") {
  field(DESC, "Port 1 Input 2")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.BA CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN3") {
  field(DESC, "Port 1 Input 3")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.BB CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN4") {
  field(DESC, "Port 1 Input 4")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.BC CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN5") {
  field(DESC, "Port 1 Input 5")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.BD CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN6") {
  field(DESC, "Port 1 Input 6")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.BE CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP1IN7") {
  field(DESC, "Port 1 Input 7")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS0.BF CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN0") {
  field(DESC, "Port 2 Input 0")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B0 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN1") {
  field(DESC, "Port 2 Input 1")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B1 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN2") {
  field(DESC, "Port 2 Input 2")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B2 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN3") {
  field(DESC, "Port 2 Input 3")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B3 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN4") {
  field(DESC, "Port 2 Input 4")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B4 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN5") {
  field(DESC, "Port 2 Input 5")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B5 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN6") {
  field(DESC, "Port 2 Input 6")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B6 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

# Borrowed
# File: xleap_din_bit.template
# Author: Wayne Lewis
# Date: 2017-05-08
#
# Description:
# This database is the template for the generic Aerotech 
# digital input PVs.
#
# Macros:
# U 	- Device prefix
# Port 	- Aerotech input port number (port is Aerotech terminology)
# In .	- Input number
# Word 	- MBBI word (from aerotech_din.db) 
# Bit 	- Bit number from word

record(bi, "$(DEVICE):$(RJAW):DIP2IN7") {
  field(DESC, "Port 2 Input 7")
  field(INP,  "$(DEVICE):$(RJAW):DISTATS1.B7 CP NMS")
  field(ZNAM, "Off")
  field(ONAM, "On")
  info(autosaveFields, "DESC")
}

record(motor, "$(DEVICE):$(LJAW):MOTR") {
  field(DESC, "$(MAD)-$(LJAW)")
  field(DTYP, "asynMotor")
  field(DIR, "Pos")
  field(VELO, "2")
  field(VBAS, "1")
  field(VMAX, "4")
  field(ACCL, "0.1")
  field(BDST, "0")
  field(OUT, "@asyn($(MOTOR_PORT),$(AXIS1))")
  field(MRES, "4e-4")
  field(ERES, "1")
  field(PREC, "4")
  field(EGU, "mm")
  field(DHLM, "0")
  field(DLLM, "0")
  field(UEIP, "1")
  field(TWV, "1")
  field(FOFF, "Frozen")
  field(OFF, "0")
  field(URIP, "0")
  field(RRES, "1")
  field(RTRY, "0")
  field(RDBL, "")
  field(RDBD, "1")
  field(DLY, "0")
  field(ASG, "$(ASG)")
  field(SCAN, "Passive")
  info(autosaveFields_pass0, "VAL")
  info(autosaveFields, "DIR DESC VMAX VBAS ACCL VELO MRES ERES PREC EGU DHLM DLLM DLY TWV RDBL RRES RTRY URIP UEIP FOFF OFF FRAC JVEL JAR HVEL HLM LLM")
}
record(motor, "$(DEVICE):$(RJAW):MOTR") {
  field(DESC, "$(MAD)-$(RJAW)")
  field(DTYP, "asynMotor")
  field(DIR, "Pos")
  field(VELO, "2")
  field(VBAS, "1")
  field(VMAX, "4")
  field(ACCL, "0.1")
  field(BDST, "0")
  field(OUT, "@asyn($(MOTOR_PORT),$(AXIS2))")
  field(MRES, "4e-4")
  field(ERES, "1")
  field(PREC, "4")
  field(EGU, "mm")
  field(DHLM, "0")
  field(DLLM, "0")
  field(UEIP, "1")
  field(TWV, "1")
  field(FOFF, "Frozen")
  field(OFF, "0")
  field(URIP, "0")
  field(RRES, "1")
  field(RTRY, "0")
  field(RDBL, "")
  field(RDBD, "1")
  field(DLY, "0")
  field(ASG, "$(ASG)")
  field(SCAN, "Passive")
  info(autosaveFields_pass0, "VAL")
  info(autosaveFields, "DIR DESC VMAX VBAS ACCL VELO MRES ERES PREC EGU DHLM DLLM DLY TWV RDBL RRES RTRY URIP UEIP FOFF OFF FRAC JVEL JAR HVEL HLM LLM")
}
# Collimator Database Entries for $(DEVICE)
#  This collimator has 2 jaws, each driven by a motor.
#  Looking upbeam (towards the gun)
#          On the right - $(DEVICE):$(RJAW):MOTR
#          On the  left - $(DEVICE):$(LJAW):MOTR
#
# Macros:
# device   - device name eg: COLL 
# mad      - MAD device name eg: CEHTR (check MAD deck)
# units    - user units (mm)
# higap    - set hi gap for collimator
# logap    - set logap for the collimator
# jump     - set jog value
# hipos    - max set jog value
# lopos    - min set jog value
# leftjaw  - Left jaw attribute
# rightjaw - Right jaw attribute
#-------------------------------------------------------------------------------
# Mod:
#       18-Sep-2020, N. Balakrishnan  (namrata):
#        Added ASG for SETGAP and SETCENTER PVs.
#        Added PREC for SETGAP,SETCENTER,GETGAP and GETCENTER PVs.
#       10-Mar-2020, N. Balakrishnan  (namrata):
#        Added Pv to calculate collimator state.
#       19-Nov-2020, N. Balakrishnan  (namrata):
#        CATER 138562 Error in Gap calculation around set center value.
#-------------------------------------------------------------------------------
# Store if device is in position
record(bi, "$(DEVICE):State"){
  field(DESC, "Collimator usable")
  field(ZNAM, "Active")
  field(ONAM, "Locked Off")
}

record(ao, "$(DEVICE):SETGAP") {
  field(DESC, "Set $(MAD) Collimator Gap")
  field(EGU, "mm")
  field(OMSL, "supervisory")
  field(PREC, "2")
  field(DRVH, "104")
  field(DRVL, "2")
  field(SCAN, "Passive")
  field(FLNK, "$(DEVICE):MAKEGAP")
  info(autosaveFields, "DRVH DRVL")
  field(ASG, "$(ASG)")
}

record(calcout, "$(DEVICE):UPGAP") {
  field(DESC, "$(MAD) center Increment")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):UPGAPJUMP")
  field(INPB, "$(DEVICE):GETGAP")
  field(CALC, "B+A")
  field(OUT, "$(DEVICE):SETGAP PP")
}

record(calcout, "$(DEVICE):DNGAP") {
  field(DESC, "$(MAD) center Decrement")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):DNGAPJUMP")
  field(INPB, "$(DEVICE):GETGAP")
  field(CALC, "B-A")
  field(OUT, "$(DEVICE):SETGAP PP")
}

record(ao, "$(DEVICE):UPGAPJUMP") {
  field(DESC, "$(MAD) Gap Up Jump")
#  field(PINI, "YES")
  field(EGU, "mm")
  field(OMSL, "supervisory")
  field(VAL, "1")
  field(DRVH, "104")
  field(DRVL, "2")
  field(SCAN, "Passive")
  info(autosaveFields, "VAL DRVH DRVL")
}

record(ao, "$(DEVICE):DNGAPJUMP") {
  field(DESC, "$(MAD) Gap Dn Jump")
  field(EGU, "mm")
  field(OMSL, "supervisory")
  field(VAL, "1")
  field(DRVH, "104")
  field(DRVL, "2")
  field(SCAN, "Passive")
  info(autosaveFields, "VAL DRVH DRVL")
}

record(calc, "$(DEVICE):GETGAP") {
  field(DESC, "Get $(MAD) Collimator Gap")
  field(EGU, "mm")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(LJAW):MOTR.RBV")
  field(INPB, "$(DEVICE):$(RJAW):MOTR.RBV")
  field(CALC, "-A+B")
  field(PREC, "2")
}

#
# Each jaw has it's own calc record
#
record(fanout, "$(DEVICE):MAKEGAP") {
  field(DESC, "$(MAD) Coll Gap Sync")
  field(SCAN, "Passive")
  field(SELM, "Mask")
  field(SELL, "3")
  field(LNK1, "$(DEVICE):$(RJAW):GAPCALC")
  field(LNK2, "$(DEVICE):$(LJAW):GAPCALC")
  field(ASG, "$(ASG)")
}

#
# both records add to CENTER, since they each increase
# in distance as they move away from CENTER.
#
record(calcout, "$(DEVICE):$(RJAW):GAPCALC") {
  field(DESC, "$(MAD) Right Gap Calc")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):GETCENTER")
  field(INPB, "$(DEVICE):SETGAP")
  field(CALC, "A+(B/2)")
  field(OUT, "$(DEVICE):$(RJAW):MOTR PP")
}

record(calcout, "$(DEVICE):$(LJAW):GAPCALC") {
  field(DESC, "$(MAD) Left Gap Calc")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):GETCENTER")
  field(INPB, "$(DEVICE):SETGAP")
  field(CALC, "A-(B/2)")
  field(OUT, "$(DEVICE):$(LJAW):MOTR PP")
}

#
# CENTER the collimator gap for $(DEVICE)
#
record(ao, "$(DEVICE):SETCENTER") {
  field(DESC, "$(MAD) Collimator center")
  field(EGU, "mm")
  field(OMSL, "supervisory")
  field(DRVH, "104")
  field(DRVL, "2")
  field(SCAN, "Passive")
  field(FLNK, "$(DEVICE):MAKECEN")
  info(autosaveFields, "DRVH DRVL")
  field(ASG, "$(ASG)")
  field(PREC, "2")
}

record(calcout, "$(DEVICE):UPCENTER") {
  field(DESC, "$(MAD) center Increment")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):CENTERJUMP")
  field(INPB, "$(DEVICE):GETCENTER")
  field(CALC, "B+A")
  field(OUT, "$(DEVICE):SETCENTER PP")
}

record(calcout, "$(DEVICE):DNCENTER") {
  field(DESC, "$(MAD) center Decrement")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):CENTERJUMP")
  field(INPB, "$(DEVICE):GETCENTER")
  field(CALC, "B-A")
  field(OUT, "$(DEVICE):SETCENTER PP")
}

record(ao, "$(DEVICE):CENTERJUMP") {
  field(DESC, "$(MAD) Coll. Jump")
  field(EGU, "mm")
  field(OMSL, "supervisory")
  field(VAL, "1")
  field(DRVH, "104")
  field(DRVL, "2")
  field(SCAN, "Passive")
  field(PINI, "YES")
  info(autosaveFields, "VAL DRVH DRVL")
}

record(fanout, "$(DEVICE):MAKECEN") {
  field(DESC, "$(MAD) Coll center Sync")
  field(SCAN, "Passive")
  field(SELM, "Mask")
  field(SELL, "3")
  field(LNK1, "$(DEVICE):$(RJAW):CENCALC")
  field(LNK2, "$(DEVICE):$(LJAW):CENCALC")
  field(ASG, "$(ASG)")
}

record(calcout, "$(DEVICE):$(RJAW):CENCALC") {
  field(DESC, "$(MAD) Right Jaw Calc")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):SETCENTER")
  field(INPB, "$(DEVICE):GETGAP")
  field(CALC, "A+(B/2)")
  field(OUT, "$(DEVICE):$(RJAW):MOTR PP")
}

record(calcout, "$(DEVICE):$(LJAW):CENCALC") {
  field(DESC, "$(MAD) Left Jaw Calc")
  field(EGU, "mm")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):SETCENTER")
  field(INPB, "$(DEVICE):GETGAP")
  field(CALC, "A-(B/2)")
  field(OUT, "$(DEVICE):$(LJAW):MOTR PP")
}

record(calc, "$(DEVICE):GETCENTER") {
  field(DESC, "Get $(MAD) Coll Posn")
  field(EGU, "mm")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(RJAW):MOTR.RBV")
  field(INPB, "$(DEVICE):$(LJAW):MOTR.RBV")
  field(CALC, "((A+B)/2)")
  field(PREC, "2")
}

record(dfanout, "$(DEVICE):STOP") {
  field(DESC, "$(MAD) Stop Motion")
  field(SCAN, "Passive")
  field(DOL, "1")
  field(OMSL, "supervisory")
  field(OUTA, "$(DEVICE):$(LJAW):MOTR.STOP PP")
  field(OUTB, "$(DEVICE):$(RJAW):MOTR.STOP PP")
}

record(dfanout, "$(DEVICE):EXTRACT") {
  field(DESC, "$(MAD) Retract Jaws")
  field(SCAN, "Passive")
  field(DOL, "1")
  field(OMSL, "supervisory")
  field(OUTA, "$(DEVICE):$(LJAW):MOTRLO PP")
  field(OUTB, "$(DEVICE):$(RJAW):MOTRHI PP")
}

record(ao, "$(DEVICE):$(LJAW):MOTRLO") {
  field(DESC, "Move $(MAD) Low")
  field(EGU, "mm")
  field(OMSL, "closed_loop")
  field(OIF, "Full")
  field(DOL, "$(DEVICE):$(LJAW):MOTR.LLM")
  field(DRVH, "4000000000")
  field(DRVL, "-4000000000")
  field(SCAN, "Passive")
  field(OUT, "$(DEVICE):$(LJAW):MOTR.VAL")
  field(FLNK, "$(DEVICE):$(LJAW):MOTR")
}

record(ao, "$(DEVICE):$(LJAW):MOTRHI") {
  field(DESC, "Move $(MAD) High")
  field(EGU, "mm")
  field(OMSL, "closed_loop")
  field(OIF, "Full")
  field(DOL, "$(DEVICE):$(LJAW):MOTR.HLM")
  field(DRVH, "4000000000")
  field(DRVL, "-4000000000")
  field(SCAN, "Passive")
  field(OUT, "$(DEVICE):$(LJAW):MOTR.VAL")
  field(FLNK, "$(DEVICE):$(LJAW):MOTR")
}

record(ao, "$(DEVICE):$(RJAW):MOTRHI") {
  field(DESC, "Move $(MAD) High")
  field(EGU, "mm")
  field(OMSL, "closed_loop")
  field(OIF, "Full")
  field(DOL, "$(DEVICE):$(RJAW):MOTR.HLM")
  field(DRVH, "4000000000")
  field(DRVL, "-4000000000")
  field(SCAN, "Passive")
  field(OUT, "$(DEVICE):$(RJAW):MOTR.VAL")
  field(FLNK, "$(DEVICE):$(RJAW):MOTR")
}

record(ao, "$(DEVICE):$(RJAW):MOTRLO") {
  field(DESC, "Move $(MAD) Low")
  field(EGU, "mm")
  field(OMSL, "closed_loop")
  field(OIF, "Full")
  field(DOL, "$(DEVICE):$(RJAW):MOTR.LLM")
  field(DRVH, "4000000000")
  field(DRVL, "-4000000000")
  field(SCAN, "Passive")
  field(OUT, "$(DEVICE):$(RJAW):MOTR.VAL")
  field(FLNK, "$(DEVICE):$(RJAW):MOTR")
}


record(calc, "$(DEVICE):DONEMOVING") {
  field(DESC, "$(MAD) Moving")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(RJAW):MOTR.DMOV")
  field(INPB, "$(DEVICE):$(LJAW):MOTR.DMOV")
  field(CALC, "A&B")
}

record(calcout, "$(DEVICE):$(LJAW):CALCNEGPOS") {
  field(SCAN, "1 second")
  field(DTYP, "Soft Channel")
  field(FLNK, "$(DEVICE):$(LJAW):LVPOSDISP")
  field(CALC, "-1*A")
  field(INPA, "$(DEVICE):$(LJAW):LVPOS")
  field(OUT, "$(DEVICE):$(LJAW):LVPOSDISP.VAL")
}

record(ao, "$(DEVICE):$(LJAW):LVPOSDISP") {
}

record(calcout, "$(DEVICE):$(LJAW):CALCNEGMOTR") {
  field(SCAN, "1 second")
  field(DTYP, "Soft Channel")
  field(FLNK, "$(DEVICE):$(LJAW):MOTRDISP")
  field(CALC, "-1*A")
  field(INPA, "$(DEVICE):$(LJAW):MOTR.RBV")
  field(OUT, "$(DEVICE):$(LJAW):MOTRDISP.VAL")
}

record(ao, "$(DEVICE):$(LJAW):MOTRDISP") {
}

record(ao, "$(DEVICE):TRACKINGERRORLIMIT") {
    field(DESC, "Alarm for LVDT Motor Error")
    field(DTYP, "Soft Channel")
    field(VAL,  "0.25")
    field(LOPR, "0.05")
    field(HOPR, "20.0")
    field(PINI, "YES")
    field(PREC, "2")
    field(DRVH, "20.0")
    field(DRVL, "0.05")
    field(EGU, "mm")
    info(autosaveFields, "VAL")
}

record(ao, "$(DEVICE):COLLIMATORPOSITIONLIMIT") {
    field(DESC, "Alarm for Gap and Center Error")
    field(DTYP, "Soft Channel")
    field(VAL,  "1.0")
    field(LOPR, "0.05")
    field(HOPR, "20.0")
    field(PINI, "YES")
    field(PREC, "2")
    field(DRVH, "20.0")
    field(DRVL, "0.05")
    field(EGU, "mm")
    info(autosaveFields, "VAL")
}

# Bit 0:  Gap discrepancy > COLLIMATORPOSITIONLIMIT
# Bit 1:  Center discrepancy > COLLIMATORPOSITIONLIMIT
# Bit 2:  Jaw 1 Motor and LVDT Error is Greater than TRACKINGERRORLIMIT
# Bit 3:  Jaw 1 is Below LOW
# Bit 4:  Jaw 1 is Below LOLO
# Bit 5:  Jaw 1 is Above HIGH
# Bit 6:  Jaw 1 is Above HIHI
# Bit 7:  Jaw 1 is at Low Limit Switch
# Bit 8:  Jaw 1 is at High Limit Switch
# Bit 9:  Jaw 2 Motor and LVDT Error is Greater than TRACKINGERRORLIMIT
# Bit 10: Jaw 2 is Below LOLO
# Bit 11: Jaw 2 is Below LOW
# Bit 12: Jaw 2 is Above HIGH
# Bit 13: Jaw 2 is Above HIHI
# Bit 14: Jaw 2 is at Low Limit Switch
# Bit 15: Jaw 2 is at High Limit Switch

record(calc, "$(DEVICE):LEFTERROR") {
    field(SCAN, "1 second")
    field(CALC, "(A>I) + ((B<C)<<1) + ((B<D)<<2) + ((B>E)<<3) + ((B>F)<<4) + (G<<5) + (H<<6)")
    field(INPA, "$(DEVICE):$(LJAW):TRACKINGERROR")
    field(INPB, "$(DEVICE):$(LJAW):LVPOS")
    field(INPC, "$(DEVICE):$(LJAW):LVPOS.LOLO")
    field(INPD, "$(DEVICE):$(LJAW):LVPOS.LOW")
    field(INPE, "$(DEVICE):$(LJAW):LVPOS.HIGH")
    field(INPF, "$(DEVICE):$(LJAW):LVPOS.HIHI")
    field(INPG, "$(DEVICE):$(LJAW):MOTR.LLS")
    field(INPH, "$(DEVICE):$(LJAW):MOTR.HLS")
    field(INPI, "$(DEVICE):TRACKINGERRORLIMIT")
    field(FLNK, "$(DEVICE):RIGHTERROR")
}

record(calc, "$(DEVICE):RIGHTERROR") {
    field(SCAN, "Passive")
    field(CALC, "(A>I) + ((B<C)<<1) + ((B<D)<<2) + ((B>E)<<3) + ((B>F)<<4) + (G<<5) + (H<<6)")
    field(INPA, "$(DEVICE):$(RJAW):TRACKINGERROR")
    field(INPB, "$(DEVICE):$(RJAW):LVPOS")
    field(INPC, "$(DEVICE):$(RJAW):LVPOS.LOLO")
    field(INPD, "$(DEVICE):$(RJAW):LVPOS.LOW")
    field(INPE, "$(DEVICE):$(RJAW):LVPOS.HIGH")
    field(INPF, "$(DEVICE):$(RJAW):LVPOS.HIHI")
    field(INPG, "$(DEVICE):$(RJAW):MOTR.LLS")
    field(INPH, "$(DEVICE):$(RJAW):MOTR.HLS")
    field(INPI, "$(DEVICE):TRACKINGERRORLIMIT")
    field(FLNK, "$(DEVICE):ERROR")
}

record(calc, "$(DEVICE):ERROR") {
    field(SCAN, "Passive")
    field(CALC, "((ABS(C-E)>G)) + ((ABS(D-F)>G)<<1) + (A<<2) + (B<<9)")
    field(INPA, "$(DEVICE):LEFTERROR")
    field(INPB, "$(DEVICE):RIGHTERROR")
    field(INPC, "$(DEVICE):GETGAP")
    field(INPD, "$(DEVICE):GETCENTER")
    field(INPE, "$(DEVICE):SETGAP")
    field(INPF, "$(DEVICE):SETCENTER")
    field(INPG, "$(DEVICE):COLLIMATORPOSITIONLIMIT")
}record(ai, "$(DEVICE):$(LJAW):LVRAW") {
  field(DESC, "$(MAD)-$(LJAW) LVDT Raw Data")
  field(SCAN, "1 second")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getAIN(@$(AXIS1),$(AI_CHAN)) $(ASYN_PORT)")
  field(LINR, "NO CONVERSION")
  field(PHAS, "0")
  field(EGU, "raw")
  field(PREC, "4")
 field(FLNK, "$(DEVICE):$(LJAW):LVRAW_FILTER")
}

record(calc, "$(DEVICE):$(LJAW):LVRAW_FILTER") {
  field(DESC, "$(MAD)-$(LJAW) FIR Filter")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):$(LJAW):LVRAW")
  field(INPB, "$(DEVICE):$(LJAW):LVGETCYCLES")
  field(INPC, "$(DEVICE):$(LJAW):LVRAW_FILTER")
  field(PREC, "4")
  field(CALC, "C + B*(A - C)")
  field(FLNK, "$(DEVICE):$(LJAW):LVPOS")
}

record(calc, "$(DEVICE):$(LJAW):LVGETCYCLES") {
    field(DESC, "Current smoothing factor")
    field(CALC, "A")
    field(INPA, "$(DEVICE):$(LJAW):LVSETCYCLES")
    field(SCAN, "Passive")
    field(PINI, "YES")
    field(PREC, "2")
}

record(ao, "$(DEVICE):$(LJAW):LVSETCYCLES") {
    field(DESC, "Set Smoothing Factor")
    field(DTYP, "Soft Channel")
    field(VAL,  "0.4")
    field(LOPR, "0.05")
    field(HOPR, "1.0")
    field(PINI, "YES")
    field(PREC, "2")
    field(DRVH, "1.0")
    field(DRVL, "0.05")
    field(FLNK, "$(DEVICE):$(LJAW):LVGETCYCLES")
    info(autosaveFields, "VAL")
}

record(sub, "$(DEVICE):$(LJAW):LVPOS") {
  field(DESC, "$(MAD)-$(LJAW) Position")
  field(SCAN, "Passive")
  field(INPA, "-21.9456")
  field(INPB, "-3.35519")
  field(INPC, "0.0107526")
  field(INPD, "0.00210737")
  field(INPE, "-8.71188e-05")
  field(INPF, "0.0")
  field(INPG, "0.0")
  field(INPH, "0.0")
  field(INPL, "$(DEVICE):$(LJAW):LVRAW_FILTER")
  field(EGU, "mm")
  field(PREC, "4")
  field(PINI, "YES")
  field(INAM, "InitPoly")
  field(SNAM, "CalcPoly")
  field(HIHI, "35")
  field(LOLO, "34")
  field(HIGH, "34.8")
  field(LOW,  "34.2")
  field(HHSV, "MAJOR")
  field(LLSV, "MAJOR")
  field(HSV,  "MINOR")
  field(LSV,  "MINOR")
  field(FLNK, "$(DEVICE):$(LJAW):TRACKINGERROR")
  info(autosaveFields, "LOW LOLO HIGH HIHI HHSV LLSV HSV LSV A B C D E F G H")
}

record(ao, "$(DEVICE):$(LJAW):XACT") {
  field(DESC, "LVDT position")
  field(SCAN, "Passive")
  field(DOL, "$(DEVICE):$(LJAW):LVPOS")
  field(OMSL, "closed_loop")
  field(PREC, "4")
  field(EGU, "mm")
}

record(bo, "$(DEVICE):$(LJAW):TRIM_DBG") {
  field(DTYP, "Raw Soft Channel")
  field(DESC, "Trim debug flag")
  field(VAL, "0")
  field(ZNAM, "Debug Off")
  field(ONAM, "Debug On")
}

record(calc, "$(DEVICE):$(LJAW):TRACKINGERROR"){
  field(DESC, "Error Between LVDT and Motor")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):$(LJAW):LVPOS")
  field(INPB, "$(DEVICE):$(LJAW):MOTR.RBV")
  field(PREC, "4")
  field(CALC, "A - B")
  field(FLNK, "$(DEVICE):$(LJAW):XACT")
}record(ai, "$(DEVICE):$(RJAW):LVRAW") {
  field(DESC, "$(MAD)-$(RJAW) LVDT Raw Data")
  field(SCAN, "1 second")
  field(DTYP, "stream")
  field(INP, "@aerotech.proto getAIN(@$(AXIS2),$(AI_CHAN)) $(ASYN_PORT)")
  field(LINR, "NO CONVERSION")
  field(PHAS, "0")
  field(EGU, "raw")
  field(PREC, "4")
 field(FLNK, "$(DEVICE):$(RJAW):LVRAW_FILTER")
}

record(calc, "$(DEVICE):$(RJAW):LVRAW_FILTER") {
  field(DESC, "$(MAD)-$(RJAW) FIR Filter")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):$(RJAW):LVRAW")
  field(INPB, "$(DEVICE):$(RJAW):LVGETCYCLES")
  field(INPC, "$(DEVICE):$(RJAW):LVRAW_FILTER")
  field(PREC, "4")
  field(CALC, "C + B*(A - C)")
  field(FLNK, "$(DEVICE):$(RJAW):LVPOS")
}

record(calc, "$(DEVICE):$(RJAW):LVGETCYCLES") {
    field(DESC, "Current smoothing factor")
    field(CALC, "A")
    field(INPA, "$(DEVICE):$(RJAW):LVSETCYCLES")
    field(SCAN, "Passive")
    field(PINI, "YES")
    field(PREC, "2")
}

record(ao, "$(DEVICE):$(RJAW):LVSETCYCLES") {
    field(DESC, "Set Smoothing Factor")
    field(DTYP, "Soft Channel")
    field(VAL,  "0.4")
    field(LOPR, "0.05")
    field(HOPR, "1.0")
    field(PINI, "YES")
    field(PREC, "2")
    field(DRVH, "1.0")
    field(DRVL, "0.05")
    field(FLNK, "$(DEVICE):$(RJAW):LVGETCYCLES")
    info(autosaveFields, "VAL")
}

record(sub, "$(DEVICE):$(RJAW):LVPOS") {
  field(DESC, "$(MAD)-$(RJAW) Position")
  field(SCAN, "Passive")
  field(INPA, "23.1767")
  field(INPB, "3.26685")
  field(INPC, "-0.00385633")
  field(INPD, "-0.00493726")
  field(INPE, "3.81212e-05")
  field(INPF, "2.82327e-05")
  field(INPG, "0.0")
  field(INPH, "0.0")
  field(INPL, "$(DEVICE):$(RJAW):LVRAW_FILTER")
  field(EGU, "mm")
  field(PREC, "4")
  field(PINI, "YES")
  field(INAM, "InitPoly")
  field(SNAM, "CalcPoly")
  field(HIHI, "35")
  field(LOLO, "34")
  field(HIGH, "34.8")
  field(LOW,  "34.2")
  field(HHSV, "MAJOR")
  field(LLSV, "MAJOR")
  field(HSV,  "MINOR")
  field(LSV,  "MINOR")
  field(FLNK, "$(DEVICE):$(RJAW):TRACKINGERROR")
  info(autosaveFields, "LOW LOLO HIGH HIHI HHSV LLSV HSV LSV A B C D E F G H")
}

record(ao, "$(DEVICE):$(RJAW):XACT") {
  field(DESC, "LVDT position")
  field(SCAN, "Passive")
  field(DOL, "$(DEVICE):$(RJAW):LVPOS")
  field(OMSL, "closed_loop")
  field(PREC, "4")
  field(EGU, "mm")
}

record(bo, "$(DEVICE):$(RJAW):TRIM_DBG") {
  field(DTYP, "Raw Soft Channel")
  field(DESC, "Trim debug flag")
  field(VAL, "0")
  field(ZNAM, "Debug Off")
  field(ONAM, "Debug On")
}

record(calc, "$(DEVICE):$(RJAW):TRACKINGERROR"){
  field(DESC, "Error Between LVDT and Motor")
  field(SCAN, "Passive")
  field(INPA, "$(DEVICE):$(RJAW):LVPOS")
  field(INPB, "$(DEVICE):$(RJAW):MOTR.RBV")
  field(PREC, "4")
  field(CALC, "A - B")
  field(FLNK, "$(DEVICE):$(RJAW):XACT")
}record(bi,"$(DEVICE):$(LJAW):CORRECT")
{
  field(DESC, "CORRECT Button")
  field(DTYP, "Soft Channel")
  field(OSV, "MINOR")
  field(ZSV, "NO_ALARM")
  field(PINI, "YES")
  field(INP, "0")
  field(ZNAM, "OFF")
  field(ONAM, "On")
}

record(bi,"$(DEVICE):$(LJAW):SETMODEALARM")
{
  field(DESC, "Set Button Alarm")
  field(DTYP, "Soft Channel")
  field(OSV, "MAJOR")
  field(ZSV, "NO_ALARM")
  field(PINI, "YES")
  field(INP, "0")
  field(ZNAM, "OFF")
  field(ONAM, "On")
}
record(bi,"$(DEVICE):$(RJAW):CORRECT")
{
  field(DESC, "CORRECT Button")
  field(DTYP, "Soft Channel")
  field(OSV, "MINOR")
  field(ZSV, "NO_ALARM")
  field(PINI, "YES")
  field(INP, "0")
  field(ZNAM, "OFF")
  field(ONAM, "On")
}

record(bi,"$(DEVICE):$(RJAW):SETMODEALARM")
{
  field(DESC, "Set Button Alarm")
  field(DTYP, "Soft Channel")
  field(OSV, "MAJOR")
  field(ZSV, "NO_ALARM")
  field(PINI, "YES")
  field(INP, "0")
  field(ZNAM, "OFF")
  field(ONAM, "On")
}
##########################################################################
# Description:
# This template contains database for adding to alarm handler
#
# Macros:
# P 	- Device prefix
##########################################################################
# Error between Encoder and Potentiometer
record(calcout, "$(DEVICE):$(LJAW):RdbkError") {
  field(CALC, "ABS(A-B)>C?1:0")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(LJAW):MOTR.RBV")
  field(INPB, "$(DEVICE):$(LJAW):LVPOS")
  field(INPC, "1")
  field(HIGH, "1")
  field(LOW,  "0")
  field(HSV, "MAJOR")
  field(LSV, "NO_ALARM")
  info(autosaveFields, "CALC INPC")
}

# LVDT Cable error
record(calcout, "$(DEVICE):$(LJAW):LVDTError") {
  field(CALC, "A=B?((B=C)?0:1):0")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(LJAW):LVPOS_SUB.A")
  field(INPB, "$(DEVICE):$(LJAW):LVPOS")
  field(INPC, "$(DEVICE):$(LJAW):MOTR.RBV")
  field(HIGH, "1")
  field(LOW,  "0")
  field(HSV, "MAJOR")
  field(LSV, "NO_ALARM")
  info(autosaveFields, "CALC INPC")
}

# Error PV for Alarm Handler 
record(calcout, "$(DEVICE):$(LJAW):MOTRERROR") {
  field(CALC, "C?3:(B?1:(A?2:0))")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(LJAW):LVDTError MS")
  field(INPB, "$(DEVICE):$(LJAW):RdbkError MS")
  field(INPC, "$(DEVICE):$(LJAW):CABLESTAT MS")
  field(INPD, "$(DEVICE):$(LJAW):STAT MS")
  field(OUT, "$(DEVICE):$(LJAW):MOTRERROR_STAT MS")
  field(FLNK, "$(DEVICE):$(LJAW):MOTRERROR_STAT")
  field(HIGH, "1")
  field(LOW,  "0")
  field(HSV, "MAJOR")
  field(LSV, "NO_ALARM")
  info(autosaveFields, "CALC INPC")
}
# Error PV for Alarm Handler 
record(mbbi, "$(DEVICE):$(LJAW):MOTRERROR_STAT"){
  field(ZRST, "OK")
  field(ONST, "Motor LVDT Mismatch")
  field(TWST, "LVDT Issue")
  field(THST, "Motor cable disconnected")
}

##########################################################################
# Description:
# This template contains database for adding to alarm handler
#
# Macros:
# P 	- Device prefix
##########################################################################
# Error between Encoder and Potentiometer
record(calcout, "$(DEVICE):$(RJAW):RdbkError") {
  field(CALC, "ABS(A-B)>C?1:0")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(RJAW):MOTR.RBV")
  field(INPB, "$(DEVICE):$(RJAW):LVPOS")
  field(INPC, "1")
  field(HIGH, "1")
  field(LOW,  "0")
  field(HSV, "MAJOR")
  field(LSV, "NO_ALARM")
  info(autosaveFields, "CALC INPC")
}

# LVDT Cable error
record(calcout, "$(DEVICE):$(RJAW):LVDTError") {
  field(CALC, "A=B?((B=C)?0:1):0")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(RJAW):LVPOS_SUB.A")
  field(INPB, "$(DEVICE):$(RJAW):LVPOS")
  field(INPC, "$(DEVICE):$(RJAW):MOTR.RBV")
  field(HIGH, "1")
  field(LOW,  "0")
  field(HSV, "MAJOR")
  field(LSV, "NO_ALARM")
  info(autosaveFields, "CALC INPC")
}

# Error PV for Alarm Handler 
record(calcout, "$(DEVICE):$(RJAW):MOTRERROR") {
  field(CALC, "C?3:(B?1:(A?2:0))")
  field(SCAN, "1 second")
  field(INPA, "$(DEVICE):$(RJAW):LVDTError MS")
  field(INPB, "$(DEVICE):$(RJAW):RdbkError MS")
  field(INPC, "$(DEVICE):$(RJAW):CABLESTAT MS")
  field(INPD, "$(DEVICE):$(RJAW):STAT MS")
  field(OUT, "$(DEVICE):$(RJAW):MOTRERROR_STAT MS")
  field(FLNK, "$(DEVICE):$(RJAW):MOTRERROR_STAT")
  field(HIGH, "1")
  field(LOW,  "0")
  field(HSV, "MAJOR")
  field(LSV, "NO_ALARM")
  info(autosaveFields, "CALC INPC")
}
# Error PV for Alarm Handler 
record(mbbi, "$(DEVICE):$(RJAW):MOTRERROR_STAT"){
  field(ZRST, "OK")
  field(ONST, "Motor LVDT Mismatch")
  field(TWST, "LVDT Issue")
  field(THST, "Motor cable disconnected")
}

