##############################################################
#
# Records to read asynPortDriver parameters from the param 
# library for the class pmacController.
#
# The Asyn address is set as zero because that's what we use 
# for controller parameters.
# 
##############################################################

# % macro, PORT, The asyn port for the pmac controller
# % macro, P, PV Prefix
# % macro, NAXES, Number of axes
# % macro, TIMEOUT, Timeout for controller communication
# % macro, FEEDRATE, feedrate below which we go into error
# % macro, CSG0, Name for Coordinate System Group 0
# % macro, CSG1, Name for Coordinate System Group 1
# % macro, CSG2, Name for Coordinate System Group 2
# % macro, CSG3, Name for Coordinate System Group 3
# % macro, CSG4, Name for Coordinate System Group 4
# % macro, CSG5, Name for Coordinate System Group 5
# % macro, CSG6, Name for Coordinate System Group 6
# % macro, CSG7, Name for Coordinate System Group 7

#
# This associates an edm screen with the template
# % gui, $(PORT), edm, pmacController.edl, pmac=$(MOC)
#

#
# allow query of this controller's Port Name
#
record(stringout, "$(MOC):Port")
{
    field(VAL, "$(PORT)")
    field(PINI, "YES")
}

##############################################################
# Ensure all PVs are up to date with the current brick state
##############################################################
record(busy,"$(MOC):PollAllNow") {
    field(DESC,"Poll brick status")
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),0)PMAC_C_POLLALLNOW")
    field(ZNAM, "Done")
    field(ONAM, "Polling")
}

##############################################################
# Control deferred mode for all real 1axes in this brick
##############################################################
record(bo, "$(MOC):DeferMoves")
{
    field(PINI, "YES")
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),0,4)MOTOR_DEFER_MOVES")
    field(VAL, "0")
    field(ZNAM, "Defer Off")
    field(ONAM, "Defer On")
}

##############################################################
# Send stop all command
##############################################################
record(bo, "$(MOC):StopAll")
{
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),0,4)PMAC_C_STOPALL")
}

##############################################################
# Control deferred mode for all real 1axes in this brick
##############################################################
record(bo, "$(MOC):KillAll")
{
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),0,4)PMAC_C_KILLALL")
}

##############################################################
# Send arbitrary command to the geobrick controller
##############################################################
record(waveform, "$(MOC):SendCmd")
{
    field(DTYP, "asynOctetWrite")
    field(FTVL, "CHAR")
    field(INP,  "@asyn($(PORT),0,4)PMAC_C_WRITE_CMD")
    field(NELM, "100")
    field(SCAN, "Passive")
}


# a dummy real motor 0 with mres of 1, used by the CS axis mres DB logic in pmacDirectMotor.template
record(motor,"$(MOC)M0")
{
	field(DTYP,"Soft Channel")
	field(MRES,"1")
}

# ///
# /// Select a group of coordinate systems into the controller.
# ///
# % autosave 2
record(mbbo, "$(MOC):COORDINATE_SYS_GROUP")
{
   field(PINI, "YES")
   field(DTYP, "asynInt32")
   field(OUT,  "@asyn($(PORT),0,4)PMAC_C_COORDINATE_SYS_GROUP")
   field(VAL, "0")
   field(ZRST, "")
   field(ZRVL, "0")
   field(ONST, "")
   field(ONVL, "1")
   field(TWST, "")
   field(TWVL, "2")
   field(THST, "")
   field(THVL, "3")
   field(FRST, "")
   field(FRVL, "4")
   field(FVST, "")
   field(FVVL, "5")
   field(SXST, "")
   field(SXVL, "6")
   field(SVST, "")
   field(SVVL, "7")
}

# ///
# /// Read back the current group of coordinate systems into the controller.
# ///
record(mbbi, "$(MOC):COORDINATE_SYS_GROUP_RBV")
{
   field(PINI, "YES")
   field(DTYP, "asynInt32")
   field(INP,  "@asyn($(PORT),0,4)PMAC_C_COORDINATE_SYS_GROUP")
   field(SCAN, "I/O Intr")
   field(ZRST, "")
   field(ZRVL, "0")
   field(ONST, "")
   field(ONVL, "1")
   field(TWST, "")
   field(TWVL, "2")
   field(THST, "")
   field(THVL, "3")
   field(FRST, "")
   field(FRVL, "4")
   field(FVST, "")
   field(FVVL, "5")
   field(SXST, "")
   field(SXVL, "6")
   field(SVST, "")
   field(SVVL, "7")
}

#
# Control read only mode for axes
#
record(bo, "$(MOC):AxesReadonly")
{
    field(PINI, "YES")
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),0,4)PMAC_C_AXIS_READONLY")
    field(VAL, "0")
    field(ZNAM, "Read/Write")
    field(ONAM, "Readonly")
}

#
# Read back the readonly mode for axes
# 
record(bi, "$(MOC):AxesReadonly_RBV")
{
    field(DTYP, "asynInt32")
    field(INP,  "@asyn($(PORT),0,4)PMAC_C_AXIS_READONLY")
    field(SCAN, "I/O Intr")
    field(ZNAM, "Read/Write")
    field(ONAM, "Readonly")
}

##############################################################
# Record of the Kinematics on the PMAC
##############################################################
record(waveform, "$(MOC):FWD_KINEMATIC_1_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_1")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_2_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_2")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_3_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_3")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_4_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_4")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_5_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_5")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_6_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_6")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_7_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_7")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_8_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_8")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_9_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_9")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_10_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_10")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_11_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_11")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_12_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_12")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_13_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_13")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_14_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_14")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_15_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_15")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):FWD_KINEMATIC_16_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_FWD_KIN_16")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_1_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_1")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_2_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_2")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_3_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_3")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_4_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_4")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_5_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_5")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_6_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_6")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_7_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_7")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_8_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_8")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_9_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_9")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_10_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_10")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_11_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_11")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_12_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_12")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_13_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_13")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_14_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_14")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_15_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_15")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}

record(waveform, "$(MOC):INV_KINEMATIC_16_RBV")
{
  field(DTYP, "asynOctetRead")
  field(INP,  "@asyn($(PORT),0)PMAC_C_INV_KIN_16")
  field(FTVL, "CHAR")
  field(NELM, "20000")
  field(SCAN, "I/O Intr")
}


#####################################################
# Below are the records originally from
#    pmacStatus.template
#####################################################

# pmac type record
record(mbbi, "$(MOC):PMACTYPE") {
  field(PINI, "YES")
  field(DTYP, "asynInt32")
  field(ZRVL, "604020")
  field(ONVL, "602413")
  field(TWVL, "603382")
  field(THVL, "602404")
  field(ZRST, "Power PMAC")
  field(ONST, "Turbo PMAC2")
  field(TWST, "Geobrick LV")
  field(THST, "PMAC Clipper")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VIM_cid")
}

# % archiver 10 Monitor
# This makes the component icon reflect the severity
# % gui, $(PORT), sevr
record(ai, "$(MOC):CPULOAD") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_CPU_USAGE")
  field(PREC, "2")
  field(EGU, "%")
  field(HIGH, "60")
  field(HSV, "MINOR")
  field(HIHI, "80")
  field(HHSV, "MAJOR")
}

record(ai, "$(MOC):SERVO_FREQ") {
  field(EGU, "Hz")
  field(PREC, "1")
}

record(longin, "$(MOC):READ_CTRLSTAT1") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_STATUS_BITS01")
  field(FLNK, "$(MOC):CTRLSTAT:status1")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):CTRLSTAT:status1") {
  field(INP, "$(MOC):READ_CTRLSTAT1")
}

record(longin, "$(MOC):READ_CTRLSTAT2") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_STATUS_BITS02")
  field(FLNK, "$(MOC):CTRLSTAT:status2")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):CTRLSTAT:status2") {
  field(INP, "$(MOC):READ_CTRLSTAT2")
}

record(longin, "$(MOC):READ_CTRLSTAT3") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_STATUS_BITS03")
  field(FLNK, "$(MOC):CTRLSTAT:status3")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):CTRLSTAT:status3") {
  field(INP, "$(MOC):READ_CTRLSTAT3")
}


record(longin, "$(MOC):READ_PLCBITS00") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_PLC_BITS00")
  field(FLNK, "$(MOC):PLCDISBITS00")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):PLCDISBITS00") {
  field(INP, "$(MOC):READ_PLCBITS00")
}

record(longin, "$(MOC):READ_PLCBITS01") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_PLC_BITS01")
  field(FLNK, "$(MOC):PLCDISBITS01")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):PLCDISBITS01") {
  field(INP, "$(MOC):READ_PLCBITS01")
}

record(longin, "$(MOC):READ_GPIO_INP") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_GPIO_INPUTS")
  field(FLNK, "$(MOC):GPIO_INP_BITS")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):GPIO_INP_BITS") {
  field(INP, "$(MOC):READ_GPIO_INP")
}

record(longin, "$(MOC):READ_GPIO_OUT") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_GPIO_OUTPUTS")
  field(FLNK, "$(MOC):GPIO_OP_BITS")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):GPIO_OP_BITS") {
  field(INP, "$(MOC):READ_GPIO_OUT")
}

record(longin, "$(MOC):READ_PROGBITS") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_C_PROG_BITS")
  field(FLNK, "$(MOC):PROGBITS")
}

# % archiver 10 Monitor
record(mbbiDirect, "$(MOC):PROGBITS") {
  field(INP, "$(MOC):READ_PROGBITS")
}

record(ai, "$(MOC):MACROERRS") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDM_M5035")
}

record(seq, "$(MOC):AXIS1:ReadBack") {
}

record(seq, "$(MOC):AXIS2:ReadBack") {
}

record(seq, "$(MOC):AXIS3:ReadBack") {
}

record(seq, "$(MOC):AXIS4:ReadBack") {
}

record(seq, "$(MOC):AXIS5:ReadBack") {
}

record(seq, "$(MOC):AXIS6:ReadBack") {
}

record(seq, "$(MOC):AXIS7:ReadBack") {
}

record(seq, "$(MOC):AXIS8:ReadBack") {
}

record(seq, "$(MOC):AXIS9:ReadBack") {
}

record(seq, "$(MOC):AXIS10:ReadBack") {
}

record(seq, "$(MOC):AXIS11:ReadBack") {
}

record(seq, "$(MOC):AXIS12:ReadBack") {
}

record(seq, "$(MOC):AXIS13:ReadBack") {
}

record(seq, "$(MOC):AXIS14:ReadBack") {
}

record(seq, "$(MOC):AXIS15:ReadBack") {
}

record(seq, "$(MOC):AXIS16:ReadBack") {
}

record(seq, "$(MOC):AXIS17:ReadBack") {
}

record(seq, "$(MOC):AXIS18:ReadBack") {
}

record(seq, "$(MOC):AXIS19:ReadBack") {
}

record(seq, "$(MOC):AXIS20:ReadBack") {
}

record(seq, "$(MOC):AXIS21:ReadBack") {
}

record(seq, "$(MOC):AXIS22:ReadBack") {
}

record(seq, "$(MOC):AXIS23:ReadBack") {
}

record(seq, "$(MOC):AXIS24:ReadBack") {
}

record(seq, "$(MOC):AXIS25:ReadBack") {
}

record(seq, "$(MOC):AXIS26:ReadBack") {
}

record(seq, "$(MOC):AXIS27:ReadBack") {
}

record(seq, "$(MOC):AXIS28:ReadBack") {
}

record(seq, "$(MOC):AXIS29:ReadBack") {
}

record(seq, "$(MOC):AXIS30:ReadBack") {
}

record(seq, "$(MOC):AXIS31:ReadBack") {
}

record(seq, "$(MOC):AXIS32:ReadBack") {
}

record(seq, "$(MOC):AxisRB") {
  field(LNK1, "$(MOC):AXIS1:ReadBack.PROC PP")
  field(LNK2, "$(MOC):AXIS2:ReadBack.PROC PP")
  field(LNK3, "$(MOC):AXIS3:ReadBack.PROC PP")
  field(LNK4, "$(MOC):AXIS4:ReadBack.PROC PP")
  field(LNK5, "$(MOC):AXIS5:ReadBack.PROC PP")
  field(LNK6, "$(MOC):AXIS6:ReadBack.PROC PP")
  field(LNK7, "$(MOC):AXIS7:ReadBack.PROC PP")
  field(LNK8, "$(MOC):AXIS8:ReadBack.PROC PP")
  field(LNK9, "$(MOC):AxisRB2.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DLY5, "0.5")
  field(DLY6, "0.5")
  field(DLY7, "0.5")
  field(DLY8, "0.5")
  field(DLY9, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
  field(DOL5, "1")
  field(DOL6, "1")
  field(DOL7, "1")
  field(DOL8, "1")
  field(DOL9, "1")
}

record(seq, "$(MOC):AxisRB2") {
  field(LNK1, "$(MOC):AXIS9:ReadBack.PROC PP")
  field(LNK2, "$(MOC):AXIS10:ReadBack.PROC PP")
  field(LNK3, "$(MOC):AXIS11:ReadBack.PROC PP")
  field(LNK4, "$(MOC):AXIS12:ReadBack.PROC PP")
  field(LNK5, "$(MOC):AXIS13:ReadBack.PROC PP")
  field(LNK6, "$(MOC):AXIS14:ReadBack.PROC PP")
  field(LNK7, "$(MOC):AXIS15:ReadBack.PROC PP")
  field(LNK8, "$(MOC):AXIS16:ReadBack.PROC PP")
  field(LNK9, "$(MOC):AxisRB3.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DLY5, "0.5")
  field(DLY6, "0.5")
  field(DLY7, "0.5")
  field(DLY8, "0.5")
  field(DLY9, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
  field(DOL5, "1")
  field(DOL6, "1")
  field(DOL7, "1")
  field(DOL8, "1")
  field(DOL9, "1")
}

record(seq, "$(MOC):AxisRB3") {
  field(LNK1, "$(MOC):AXIS17:ReadBack.PROC PP")
  field(LNK2, "$(MOC):AXIS18:ReadBack.PROC PP")
  field(LNK3, "$(MOC):AXIS19:ReadBack.PROC PP")
  field(LNK4, "$(MOC):AXIS20:ReadBack.PROC PP")
  field(LNK5, "$(MOC):AXIS21:ReadBack.PROC PP")
  field(LNK6, "$(MOC):AXIS22:ReadBack.PROC PP")
  field(LNK7, "$(MOC):AXIS23:ReadBack.PROC PP")
  field(LNK8, "$(MOC):AXIS24:ReadBack.PROC PP")
  field(LNK9, "$(MOC):AxisRB4.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DLY5, "0.5")
  field(DLY6, "0.5")
  field(DLY7, "0.5")
  field(DLY8, "0.5")
  field(DLY9, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
  field(DOL5, "1")
  field(DOL6, "1")
  field(DOL7, "1")
  field(DOL8, "1")
  field(DOL9, "1")
}

record(seq, "$(MOC):AxisRB4") {
  field(LNK1, "$(MOC):AXIS25:ReadBack.PROC PP")
  field(LNK2, "$(MOC):AXIS26:ReadBack.PROC PP")
  field(LNK3, "$(MOC):AXIS27:ReadBack.PROC PP")
  field(LNK4, "$(MOC):AXIS28:ReadBack.PROC PP")
  field(LNK5, "$(MOC):AXIS29:ReadBack.PROC PP")
  field(LNK6, "$(MOC):AXIS30:ReadBack.PROC PP")
  field(LNK7, "$(MOC):AXIS31:ReadBack.PROC PP")
  field(LNK8, "$(MOC):AXIS32:ReadBack.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DLY5, "0.5")
  field(DLY6, "0.5")
  field(DLY7, "0.5")
  field(DLY8, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
  field(DOL5, "1")
  field(DOL6, "1")
  field(DOL7, "1")
  field(DOL8, "1")
}

record(longin, "$(MOC):NAXES") {
  field(PINI, "YES")
  field(VAL, "$(N)")
}

##############################################################
# This record indicates if we have a controller problem
##############################################################

# ///
# /// Record used to indicate if the controller has a problem. This is set in the
# /// controller poll loop, and is used to set the motor record axis status.
# ///
record(bi, "$(MOC):GLOBAL_PROBLEM_RBV")
{
   field(DTYP, "asynInt32")
   field(INP,  "@asyn($(PORT),0,4)PMAC_C_GLOBALSTATUS")
   field(SCAN, "I/O Intr")
   field(ZNAM, "Status OK")
   field(ONAM, "Status Problem")
}

##############################################################
# Monitor and set the PMAC feedrate
##############################################################

# ///
# /// Get the PMAC feedrate. This is read in the controller poll loop.
# ///
record(ai, "$(MOC):FEEDRATE_RBV")
{
   field(DTYP, "asynInt32")
   field(INP,  "@asyn($(PORT),0,4)PMAC_C_FEEDRATE")
   field(SCAN, "I/O Intr")
   field(PREC, "2")
}

# ///
# /// Set the PMAC feedrate limit. This determins if the current feedrate
# /// read from the controller is out of range, and will be used to set axis alarms.
# ///
record(longout, "$(MOC):FEEDRATE_LIMIT")
{
   field(PINI, "YES")
   field(VAL, "100")
   field(DTYP, "asynInt32")
   field(OUT,  "@asyn($(PORT),0,4)PMAC_C_FEEDRATE_LIMIT")
   field(DRVH, "100")
   field(DRVL, "0")
}

# ///
# /// Set the PMAC feedrate limit. This writes a new feedrate to
# /// the controller.
# ///
record(longout, "$(MOC):FEEDRATE")
{
   field(PINI, "YES")
   field(VAL, "100")
   field(DTYP, "asynInt32")
   field(OUT,  "@asyn($(PORT),0,4)PMAC_C_FEEDRATE")
   field(DRVH, "100")
   field(DRVL, "0")
}

# ///
# /// Record used to indicate if the feedrate is below the limit. This is set from the
# /// controller poll loop, and is used to set the motor record axis status.
# ///
record(bi, "$(MOC):FEEDRATE_PROBLEM_RBV")
{
   field(PINI, "YES")
   field(DTYP, "asynInt32")
   field(INP,  "@asyn($(PORT),0,4)PMAC_C_FEEDRATE_PROBLEM")
   field(SCAN, "I/O Intr")
   field(ZNAM, "Feedrate OK")
   field(ONAM, "Feedrate Problem")
}

###################################################################
#  These records report the current comms statistics              #
#                                                                 #
###################################################################

record(longin, "$(MOC):STAT_MSG_NO_RBV") {
  field(DESC, "Number of messages sent")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_NO_OF_MSGS")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_TOTAL_WRITE_RBV") {
  field(DESC, "Total bytes written")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_TBYTES_WRITE")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_TOTAL_READ_RBV") {
  field(DESC, "Total bytes read")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_TBYTES_READ")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_MSG_WRITE_RBV") {
  field(DESC, "Last message bytes written")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_MBYTES_WRITE")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_MSG_READ_RBV") {
  field(DESC, "Last message bytes read")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_MBYTES_READ")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_MSG_TIME_RBV") {
  field(DESC, "Last message time (ms)")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_MSG_TIME")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_MAX_WRITE_RBV") {
  field(DESC, "Maximum written bytes")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_MAX_BYTES_WRITE")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_MAX_READ_RBV") {
  field(DESC, "Maximum read bytes")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_MAX_BYTES_READ")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_MAX_TIME_RBV") {
  field(DESC, "Maximum time for message")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_MAX_TIME")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_AVE_WRITE_RBV") {
  field(DESC, "Average written bytes")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_AVE_BYTES_WRITE")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_AVE_READ_RBV") {
  field(DESC, "Average read bytes")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_AVE_BYTES_READ")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):STAT_AVE_TIME_RBV") {
  field(DESC, "Average time for message")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_AVE_TIME")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):FAST_SIZE_RBV") {
  field(DESC, "Size of fast PMAC store")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_FAST_STORE")
  field(SCAN, "I/O Intr")
}

record(longout, "$(MOC):REPORT_FAST") {
  field(DESC, "Report the contents of fast store")
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_C_REPORT_FAST")
}

record(longin, "$(MOC):MEDIUM_SIZE_RBV") {
  field(DESC, "Size of medium PMAC store")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_MEDIUM_STORE")
  field(SCAN, "I/O Intr")
}

###################################################################
#  These records support debugging                                #
#                                                                 #
###################################################################

record(longout, "$(MOC):REPORT_MEDIUM") {
  field(DESC, "Report the contents of medium store")
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_C_REPORT_MEDIUM")
}

record(longin, "$(MOC):SLOW_SIZE_RBV") {
  field(DESC, "Size of slow PMAC store")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_C_SLOW_STORE")
  field(SCAN, "I/O Intr")
}

record(longout, "$(MOC):REPORT_SLOW") {
  field(DESC, "Report the contents of slow store")
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_C_REPORT_SLOW")
}

record(longout, "$(MOC):DEBUG_LEVEL"){
  field(DESC, "Set the debug level")
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_C_DEBUG_LEVEL")
}

record(longout, "$(MOC):DEBUG_AXISNO"){
  field(DESC, "Debug which axis (0 is controller)")
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_C_DEBUG_AXIS")
}

record(longout, "$(MOC):DEBUG_CSNO"){
  field(DESC, "Debug which CS (0 is main controller)")
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_C_DEBUG_CS")
}

record(longout, "$(MOC):DEBUG_EXECUTE"){
  field(DESC, "Apply the debug settings")
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_C_DEBUG_CMD")
}
# powerPmacStatus.template - device specific status PVs not compatible with PMAC
# % macro, PORT, The asyn port for the pmac controller
# % macro, P, PV Prefix

record(longin, "$(MOC):CALC_SERVO_FREQ") {
  field(DESC, "Servo Interrupt Time")
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_Sys.ServoPeriod")
  field(SCAN, "I/O Intr")
}

record(longin, "$(MOC):READ_ECHO") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_ECHO")
}
# % macro, PMAC,  Pmac/Geobrick name
# % macro, AXIS,    Axis number
# % macro, PORT,    Asyn port
record(stringout, "$(MOC):AXIS1:DESC") {
  field(PINI, "YES")
}

record(ai, "$(MOC):AXIS1:POSITION") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#1P")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS1:FOLL_ERROR") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#1F")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS1:VELOCITY") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#1V")
  field(PREC, "2")
}

record(seq, "$(MOC):AXIS1:ReadBack") {
  field(LNK1, "$(MOC):AXIS1:POSITION.PROC PP")
  field(LNK2, "$(MOC):AXIS1:FOLL_ERROR.PROC PP")
  field(LNK3, "$(MOC):AXIS1:VELOCITY.PROC PP")
  field(LNK4, "$(MOC):AXIS1:status1.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
}

record(longin, "$(MOC):AXIS1:READ_STAT1") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),1)PMAC_C_AXIS_BITS01")
  field(FLNK, "$(MOC):AXIS1:status1")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS1:status1") {
  field(INP, "$(MOC):AXIS1:READ_STAT1")
}

record(longin, "$(MOC):AXIS1:READ_STAT2") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),1)PMAC_C_AXIS_BITS02")
  field(FLNK, "$(MOC):AXIS1:status2")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS1:status2") {
  field(INP, "$(MOC):AXIS1:READ_STAT2")
}

record(longin, "$(MOC):AXIS1:READ_STAT3") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),1)PMAC_C_AXIS_BITS03")
  field(FLNK, "$(MOC):AXIS1:status3")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS1:status3") {
  field(INP, "$(MOC):AXIS1:READ_STAT3")
}
# % macro, PMAC,  Pmac/Geobrick name
# % macro, AXIS,    Axis number
# % macro, PORT,    Asyn port
record(stringout, "$(MOC):AXIS2:DESC") {
  field(PINI, "YES")
}

record(ai, "$(MOC):AXIS2:POSITION") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#2P")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS2:FOLL_ERROR") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#2F")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS2:VELOCITY") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#2V")
  field(PREC, "2")
}

record(seq, "$(MOC):AXIS2:ReadBack") {
  field(LNK1, "$(MOC):AXIS2:POSITION.PROC PP")
  field(LNK2, "$(MOC):AXIS2:FOLL_ERROR.PROC PP")
  field(LNK3, "$(MOC):AXIS2:VELOCITY.PROC PP")
  field(LNK4, "$(MOC):AXIS2:status1.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
}

record(longin, "$(MOC):AXIS2:READ_STAT1") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),2)PMAC_C_AXIS_BITS01")
  field(FLNK, "$(MOC):AXIS2:status1")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS2:status1") {
  field(INP, "$(MOC):AXIS2:READ_STAT1")
}

record(longin, "$(MOC):AXIS2:READ_STAT2") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),2)PMAC_C_AXIS_BITS02")
  field(FLNK, "$(MOC):AXIS2:status2")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS2:status2") {
  field(INP, "$(MOC):AXIS2:READ_STAT2")
}

record(longin, "$(MOC):AXIS2:READ_STAT3") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),2)PMAC_C_AXIS_BITS03")
  field(FLNK, "$(MOC):AXIS2:status3")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS2:status3") {
  field(INP, "$(MOC):AXIS2:READ_STAT3")
}
# % macro, PMAC,  Pmac/Geobrick name
# % macro, AXIS,    Axis number
# % macro, PORT,    Asyn port
record(stringout, "$(MOC):AXIS3:DESC") {
  field(PINI, "YES")
}

record(ai, "$(MOC):AXIS3:POSITION") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#3P")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS3:FOLL_ERROR") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#3F")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS3:VELOCITY") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#3V")
  field(PREC, "2")
}

record(seq, "$(MOC):AXIS3:ReadBack") {
  field(LNK1, "$(MOC):AXIS3:POSITION.PROC PP")
  field(LNK2, "$(MOC):AXIS3:FOLL_ERROR.PROC PP")
  field(LNK3, "$(MOC):AXIS3:VELOCITY.PROC PP")
  field(LNK4, "$(MOC):AXIS3:status1.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
}

record(longin, "$(MOC):AXIS3:READ_STAT1") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),3)PMAC_C_AXIS_BITS01")
  field(FLNK, "$(MOC):AXIS3:status1")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS3:status1") {
  field(INP, "$(MOC):AXIS3:READ_STAT1")
}

record(longin, "$(MOC):AXIS3:READ_STAT2") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),3)PMAC_C_AXIS_BITS02")
  field(FLNK, "$(MOC):AXIS3:status2")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS3:status2") {
  field(INP, "$(MOC):AXIS3:READ_STAT2")
}

record(longin, "$(MOC):AXIS3:READ_STAT3") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),3)PMAC_C_AXIS_BITS03")
  field(FLNK, "$(MOC):AXIS3:status3")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS3:status3") {
  field(INP, "$(MOC):AXIS3:READ_STAT3")
}
# % macro, PMAC,  Pmac/Geobrick name
# % macro, AXIS,    Axis number
# % macro, PORT,    Asyn port
record(stringout, "$(MOC):AXIS4:DESC") {
  field(PINI, "YES")
}

record(ai, "$(MOC):AXIS4:POSITION") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#4P")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS4:FOLL_ERROR") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#4F")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS4:VELOCITY") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#4V")
  field(PREC, "2")
}

record(seq, "$(MOC):AXIS4:ReadBack") {
  field(LNK1, "$(MOC):AXIS4:POSITION.PROC PP")
  field(LNK2, "$(MOC):AXIS4:FOLL_ERROR.PROC PP")
  field(LNK3, "$(MOC):AXIS4:VELOCITY.PROC PP")
  field(LNK4, "$(MOC):AXIS4:status1.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
}

record(longin, "$(MOC):AXIS4:READ_STAT1") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),4)PMAC_C_AXIS_BITS01")
  field(FLNK, "$(MOC):AXIS4:status1")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS4:status1") {
  field(INP, "$(MOC):AXIS4:READ_STAT1")
}

record(longin, "$(MOC):AXIS4:READ_STAT2") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),4)PMAC_C_AXIS_BITS02")
  field(FLNK, "$(MOC):AXIS4:status2")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS4:status2") {
  field(INP, "$(MOC):AXIS4:READ_STAT2")
}

record(longin, "$(MOC):AXIS4:READ_STAT3") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),4)PMAC_C_AXIS_BITS03")
  field(FLNK, "$(MOC):AXIS4:status3")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS4:status3") {
  field(INP, "$(MOC):AXIS4:READ_STAT3")
}
# % macro, PMAC,  Pmac/Geobrick name
# % macro, AXIS,    Axis number
# % macro, PORT,    Asyn port
record(stringout, "$(MOC):AXIS5:DESC") {
  field(PINI, "YES")
}

record(ai, "$(MOC):AXIS5:POSITION") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#5P")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS5:FOLL_ERROR") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#5F")
  field(PREC, "2")
}

record(ai, "$(MOC):AXIS5:VELOCITY") {
  field(DTYP, "asynFloat64")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#5V")
  field(PREC, "2")
}

record(seq, "$(MOC):AXIS5:ReadBack") {
  field(LNK1, "$(MOC):AXIS5:POSITION.PROC PP")
  field(LNK2, "$(MOC):AXIS5:FOLL_ERROR.PROC PP")
  field(LNK3, "$(MOC):AXIS5:VELOCITY.PROC PP")
  field(LNK4, "$(MOC):AXIS5:status1.PROC PP")
  field(DLY1, "0")
  field(DLY2, "0.5")
  field(DLY3, "0.5")
  field(DLY4, "0.5")
  field(DOL1, "1")
  field(DOL2, "1")
  field(DOL3, "1")
  field(DOL4, "1")
}

record(longin, "$(MOC):AXIS5:READ_STAT1") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),5)PMAC_C_AXIS_BITS01")
  field(FLNK, "$(MOC):AXIS5:status1")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS5:status1") {
  field(INP, "$(MOC):AXIS5:READ_STAT1")
}

record(longin, "$(MOC):AXIS5:READ_STAT2") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),5)PMAC_C_AXIS_BITS02")
  field(FLNK, "$(MOC):AXIS5:status2")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS5:status2") {
  field(INP, "$(MOC):AXIS5:READ_STAT2")
}

record(longin, "$(MOC):AXIS5:READ_STAT3") {
  field(DTYP, "asynInt32")
  field(SCAN, "I/O Intr")
  field(INP, "@asyn($(PORT),5)PMAC_C_AXIS_BITS03")
  field(FLNK, "$(MOC):AXIS5:status3")
}

#% archiver 10 Monitor
record(mbbiDirect, "$(MOC):AXIS5:status3") {
  field(INP, "$(MOC):AXIS5:READ_STAT3")
}
# pmacGlobalStatus.template
# NOTE: this is a re-implementation of the <Dev>:GLOBAL_PROBLEM_RBV PV from the pmac module
# Modifications: alarm severity when PV has value 1 

record(bi, "$(MOC):GlobalProblem") {
    field(DESC, "Pmac Global Problem")
    field(INP,  "$(MOC):GLOBAL_PROBLEM_RBV CP MS")
    field(ZNAM, "Status OK")
    field(ONAM, "Status Problem")
    field(OSV,  "MAJOR")
}

# % macro, PMAC, The P prefix for the motor controller (must match P in pmacController.template)

# this template defines a motor with two names:-
# (1) root is BOD:UNDS:3575:Y:MOTOR, the macros fully define the name of the motor
# (2) 'numbered' names of the form $(MOC):PMAC:Mx where x is the motor number
# names are both assigned to the same PV via aliases in the case basic_asyn_motor
# or simply duplicated for all other included templates, this approach works because
# the PVs all point to the same ASYN parameters and improves maintainability of this
# file

# include motor record from the motor module
#% macro, __doc__, Basic template, including motor record and associated tags
#% macro, P      , Device Prefix
#% macro, M      , Device Suffix
#% macro, PORT   , Asyn port for motor record
#% macro, ADDR   , Address on controller
#% macro, DESC   , Description, displayed on EDM screen
#% macro, MRES   , Motor Step Size (EGU)
#% macro, DTYP   , DTYP of record
#% macro, DIR    , User Direction
#% macro, VBAS   , Base Velocity (EGU/s)
#% macro, VELO   , Velocity (EGU/s)
#% macro, VMAX   , Max Velocity (EGU/s), defaults to VELO
#% macro, ACCL   , Seconds to Velocity
#% macro, BDST   , BL Distance (EGU)
#% macro, BVEL   , BL Velocity (EGU/s)
#% macro, BACC   , BL Seconds to Veloc.
#% macro, PREC   , Display Precision
#% macro, EGU    , Engineering Units
#% macro, DHLM   , Dial High Limit
#% macro, DLLM   , Dial Low Limit
#% macro, HLM    , User High Limit
#% macro, LLM    , User Low Limit
#% macro, HLSV   , HW Lim. Violation Svr
#% macro, INIT   , Startup commands
#% macro, SREV   , Steps per Revolution
#% macro, RRES   , Readback Step Size (EGU)
#% macro, TWV    , Tweak Step Size (EGU)
#% macro, ERES   , Encoder Step Size (EGU)
#% macro, JAR    , Jog Acceleration (EGU/s^2)
#% macro, UEIP   , Use Encoder If Present
#% macro, URIP   , Use RDBL If Present
#% macro, RDBL   , Readback Location, set URIP = 1 if you specify this
#% macro, RLNK   , Readback output link
#% macro, RTRY   , Max retry count
#% macro, DLY    , Readback settle time (s)
#% macro, OFF    , User Offset (EGU)
#% macro, RDBD   , Retry Deadband (EGU)
#% macro, FOFF   , Freeze Offset, 0=variable, 1=frozen
#% macro, ADEL   , Alarm monitor deadband (EGU)
#% macro, NTM    , New Target Monitor, only set to 0 for soft motors
#% macro, FEHIGH , HIGH limit for following error
#% macro, FEHIHI , HIHI limit for following error
#% macro, FEHHSV , HIHI alarm severity for following error
#% macro, FEHSV  , HIGH alarm severity for following error
#% macro, SCALE  , Scale factor, if pmacSetAxisScale is used this should be set
#% macro, HOMEVIS, If 1 then home is visible on the gui
#% macro, HOMEVISSTR, If HOMEVIS=0, then display this text on the gui instead
#% macro, name   , Object name and gui association name
#% macro, alh    , Set this to alh to add the motor to the alarm handler and send emails, 
#                  set to '#' to comment out alh lines
#% macro, gda_name, Name to export this as to GDA
#% macro, gda_desc, Description to export this as to GDA

# AUTOSAVE: level 0 = before record init, level 1 = before AND after record init
#% autosave 0 DVAL OFF
#% autosave 1 DIR DHLM DLLM TWV VBAS VELO ACCL BDST BVEL BACC RDBD EGU RTRY UEIP URIP DLY PREC DISA DISP FOFF OFF FRAC OMSL JVEL JAR ADEL MDEL
#% archiver 0.5 Monitor
#% archiver 0.5 Monitor RBV
#% archiver 10 Monitor OFF
#% archiver 10 Monitor MRES
#% gdatag,template,simpleMotor,,BOD1 Y
#% gdatag,motor,rw,,RECORD,Motor
#% alh
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:Y:MOTOR

# This associates an edm screen with the template
# % gui, M1, edm, motor.edl, motor=BOD:UNDS:3575:Y:MOTOR
# % gui, M1, edmembed, motor-embed-small.edl, motor=BOD:UNDS:3575:Y:MOTOR,filename=motor.edl,box-label=BOD1 Y

# This associates a BOY screen with the template
# % gui, M1, boydetail, motorApp_opi/motor_detail.opi, P=BOD:UNDS:3575:,M=Y:MOTOR,DESC=BOD1 Y, name=M1
# % gui, M1, boyembed, motorApp_opi/motor_embed_box.opi, P=BOD:UNDS:3575:,M=Y:MOTOR,DESC=BOD1 Y, name=M1
# % gui, M1, boyembed, motorApp_opi/motor_embed.opi, P=BOD:UNDS:3575:,M=Y:MOTOR,DESC=BOD1 Y, name=M1
# FIXME: this should be in pmacUtil or tpmac
# % gui, M1, boyembed, motorApp_opi/motor_homed_embed.opi, P=BOD:UNDS:3575:,M=Y:MOTOR

# This makes the component icon reflect the status and severity
# % gui, M1, status, <pv>.MOVN
# % gui, M1, sevr

# These define what PVs a motor detail screen should contain
# % gui, M1, statusbits, Status,  <pv>.MSTA, Direction Positive, Done, High Limit, Home Limit, Unused, Closed Loop, Following Error, At Home, Encoder Present, Problem, Moving, Gain Support, Comms Error, Low Limit, Homed
# % gui, M1, statusbits, ELoss,  <pv>:ELOSS, Encoder Loss, Amplifier Loss, Sys Fail
# % gui, M1, command, ELoss Clear,  <pv>:ELOSSRC.A
# % gui, M1, statusbits, Limit Violation,  <pv>.LVIO, Lim
# % gui, M1, statusbits, At High Limit,  <pv>.HLS, High
# % gui, M1, statusbits, At Low Limit,  <pv>.LLS, Low
# % gui, M1, demand, User High Limit,  <pv>.HLM
# % gui, M1, demand, User Low Limit,  <pv>.LLM
# % gui, M1, demand, Dial High Limit, <pv>.DHLM
# % gui, M1, demand, Dial Low Limit, <pv>.DLLM

# % gui, M1, demand, Motor demand,  <pv>.VAL
# % gui, M1, readback, Motor readback,  <pv>.RBV
# % gui, M1, command, Stop,  <pv>.STOP
# % gui, M1, command, Home Forward,  <pv>.HOMF
# % gui, M1, command, Home Reverse,  <pv>.HOMR
# % gui, M1, command, Jog Forward, <pv>.JOGF
# % gui, M1, command, Jog Reverse, <pv>.JOGR
# % gui, M1, command, Tweak Forward,  <pv>.TWF
# % gui, M1, command, Tweak Reverse,  <pv>.TWR
# % gui, M1, demand, Tweak Step,  <pv>.TWV
# % gui, M1, command, Kill,  <pv>:KILL.PROC, Kill
# % gui, M1, command, Sync VAL=RBV,  <pv>.SYNC

# % gui, M1, enum, Direction,   <pv>.DIR
# % gui, M1, demand, User Offset,  <pv>.OFF
# % gui, M1, enum, Set/Use,   <pv>.SET
# % gui, M1, enum, Offset,   <pv>.FOFF
# % gui, M1, enum, Use Encoder,   <pv>.UEIP

# % gui, M1, demand, Motor Step Size,  <pv>.MRES
# % gui, M1, readback, Steps per Rev,   <pv>.SREV
# % gui, M1, readback, EGUs per Rev,   <pv>.UREV
# % gui, M1, demand, Encoder Step Size,  <pv>.ERES
# % gui, M1, readback, Readback Step Size,   <pv>.RRES
# % gui, M1, readback, Use Encoder if Present,   <pv>.UEIP

# % gui, M1, demand, Max Velocity,  <pv>.VMAX
# % gui, M1, demand, Base Velocity,   <pv>.VBAS
# % gui, M1, demand, Velocity,  <pv>.VELO
# % gui, M1, demand, Secs to Velocity,  <pv>.ACCL
# % gui, M1, demand, JVEL,   <pv>.JVEL
# % gui, M1, demand, Jog Acceleration,   <pv>.JAR
# % gui, M1, demand, Backlash Distance,   <pv>.BDST
# % gui, M1, demand, Backlash Velocity,   <pv>.BVEL
# % gui, M1, demand, Backlash Secs to Vel,   <pv>.BACC
# % gui, M1, demand, Move Fraction,   <pv>.FRAC
# % gui, M1, demand, Retry Deadband,   <pv>.RDBD
# % gui, M1, demand, Max Retrys,   <pv>.RTRY

# % gui, M1, demand, PREC,   <pv>.PREC
# % gui, M1, demand, EGU,   <pv>.EGU

# % gui, M1, demand, Output Specification,   <pv>.OUT
# % gui, M1, readback, Readback Location,   <pv>.RDBL
# % gui, M1, readback, Desired Output Loc,   <pv>.DOL
# % gui, M1, readback, Output Mode Select,   <pv>.OMSL
# % gui, M1, readback, Readback Out Link,   <pv>.RLNK
# % gui, M1, demand, DMOV Input Link,   <pv>.DINP
# % gui, M1, demand, RMP Input Link,   <pv>.RINP
# % gui, M1, demand, Stop Out Link,   <pv>.STOO


record(motor,"BOD:UNDS:3575:Y:MOTOR")
{
	field(DESC,"BOD1 Y")
	field(DTYP,"asynMotor")
	field(DIR,"0")
	field(VELO,"0.05")
	field(VBAS,"0")
	field(ACCL,"0.5")
	field(BDST,"0")
	field(BVEL,"0")
	field(BACC,"")
	field(OUT,"@asyn($(PORT),1)")
	field(MRES,"50e-6")
	field(PREC,"4")
	field(EGU,"mm")
	field(DHLM,"50.0")
	field(DLLM,"0")
	field(HLM,"")
	field(LLM,"")	
	field(HLSV,"MAJOR")
	field(INIT,"")
	field(RTRY,"0")
	field(DLY,"0")	
	field(HVEL,"0")
	field(SREV, "1")
	field(RRES, "")
	field(TWV, "0.1")
	field(ERES, "50e-6")
	field(JVEL, "0.05")
	field(JAR, "")
	field(UEIP, "1")
	field(URIP, "0")	
	field(RDBL, "")		
	field(VMAX, "0.1")
	field(OFF, "0")
	field(RDBD, "")
	field(FOFF, "0")
	field(ADEL, "0")
	field(NTM, "1")
	field(SDIS, "BOD:UNDS:3575:Y:MOTOR:SDIS.VAL")	
	field(RLNK, "")
}

# record that holds the ADDR
record(ao, "BOD:UNDS:3575:Y:MOTOR:ADDR") {
    field(PINI, "YES")
    field(VAL, "1")
}

# record that holds the PORT
record(stringout, "BOD:UNDS:3575:Y:MOTOR:PORT") {
    field(PINI, "YES")
    field(VAL, "$(PORT)")
}

# record that holds the SCALE
record(ao, "BOD:UNDS:3575:Y:MOTOR:SCALE") {
    field(PINI, "YES")
    field(VAL, "20000")
}

# Write a 1 to this record over Channel Access to disable the motor record and prevent CA writes to any field
# including the demand field.
record(ai, "BOD:UNDS:3575:Y:MOTOR:USER:SDIS") {
    field(VAL, "0")
    field(FLNK, "BOD:UNDS:3575:Y:MOTOR:SDIS")
}

# Note: This record forms part of the interface to auto-homing, which uses the "A" field to disable the motor record
# when homing.
# record that will disable and re-enable record if any of its inputs are non-zero
record(calcout, "BOD:UNDS:3575:Y:MOTOR:SDIS") {
  field(DESC, "Disable on non-zero input")
  field(INPL, "BOD:UNDS:3575:Y:MOTOR:USER:SDIS")
  field(CALC, "(A|B|C|D|E|F|G|H|I|J|K|L)>0")
  field(OUT, "BOD:UNDS:3575:Y:MOTOR.DISP PP")
}

# dummy record that looks like a kill command
record(ao, "BOD:UNDS:3575:Y:MOTOR:KILL") {
    field(VAL, "1")
}

# dummy record that looks like eloss status
#% archiver 10 Monitor VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:Y:MOTOR:ELOSS
record(ai, "BOD:UNDS:3575:Y:MOTOR:ELOSS") {
    field(PINI, "YES")
    field(HIHI, "1")
    field(HHSV, "MAJOR")    
}

# dummy record that looks like an eloss reset
record(calcout, "BOD:UNDS:3575:Y:MOTOR:ELOSSRC") {
}

# check if SYNC has been pressed
record(calcout, "BOD:UNDS:3575:Y:MOTOR:CHECK_SYNC") {
  field(INPA, "BOD:UNDS:3575:Y:MOTOR.SYNC CP")  
  field(CALC, "A>0")
  field(OOPT, "When Non-zero")
  field(OUT, "BOD:UNDS:3575:Y:MOTOR:MR_MOVE_WRITE.PROC PP")
}

# dummy record that looks like a record to notify a CS about a raw motor move
record(longout, "BOD:UNDS:3575:Y:MOTOR:MR_MOVE_WRITE") {
}

#Record to calculate the following error
#% archiver 1 Monitor
record(calc, "BOD:UNDS:3575:Y:MOTOR:FERROR")
{
	field(DESC,"Following Error")
	field(INPA,"BOD:UNDS:3575:Y:MOTOR.RMP CP")
	field(INPB,"BOD:UNDS:3575:Y:MOTOR.REP NPP")
	field(INPC,"BOD:UNDS:3575:Y:MOTOR.MRES NPP")
	field(INPD,"BOD:UNDS:3575:Y:MOTOR.ERES NPP")
	field(CALC,"ABS((A*C)-(B*D))")
	field(FLNK,"BOD:UNDS:3575:Y:MOTOR:FERRORMAX")
	field(PREC,"4")
	field(EGU, "mm")
}

#Record to store the maximum following error
#% archiver 10 Monitor VAL
#% autosave 1 VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:Y:MOTOR:FERRORMAX
record(calc, "BOD:UNDS:3575:Y:MOTOR:FERRORMAX")
{
	field(DESC,"Following Error Max")
	field(INPA,"BOD:UNDS:3575:Y:MOTOR:FERROR.VAL")
	field(INPB,"BOD:UNDS:3575:Y:MOTOR:FERRORMAX.VAL")
	field(CALC,"(A>B)?A:B")
	field(HIGH,"0")
	field(HIHI,"0")
	field(HHSV,"NO_ALARM")
	field(HSV, "NO_ALARM")
	field(PREC,"4")
	field(EGU, "mm")
}	

#Record to reset the maximum following error
record(bo, "BOD:UNDS:3575:Y:MOTOR:FEMAXRESET")
{
	field(DESC,"Reset max following error")
	field(DTYP,"Soft Channel")
	field(OUT, "BOD:UNDS:3575:Y:MOTOR:FERRORMAX.VAL")
	field(VAL, "0")
}	


#Record to determin HOME button visibility on the motor.edl screen.
record(bo, "BOD:UNDS:3575:Y:MOTOR:HOMEVIS")
{
	field(DESC,"Home button visibility")
	field(DTYP,"Soft Channel")
	field(VAL, "1")
        field(ZNAM, "Invisible")
	field(ONAM, "Visible")
}	 

#Record to display a message if the HOME buttons are not visible (if HOMEVIS=0).
record(stringout, "BOD:UNDS:3575:Y:MOTOR:HOMEVISSTR")
{
	field(DESC, "Home visibility string")
	field(DTYP,"Soft Channel")
	field(VAL, "")
}


# numbered name aliases for all PVs in basic_asyn_motor.template
alias(BOD:UNDS:3575:Y:MOTOR, $(MOC):PMAC:M1)
alias(BOD:UNDS:3575:Y:MOTOR:ADDR, $(MOC):PMAC:M1:ADDR)
alias(BOD:UNDS:3575:Y:MOTOR:PORT, $(MOC):PMAC:M1:PORT)
alias(BOD:UNDS:3575:Y:MOTOR:SCALE, $(MOC):PMAC:M1:SCALE)
alias(BOD:UNDS:3575:Y:MOTOR:USER:SDIS, $(MOC):PMACM1:USER:SDIS)
alias(BOD:UNDS:3575:Y:MOTOR:SDIS, $(MOC):PMAC:M1:SDIS)
alias(BOD:UNDS:3575:Y:MOTOR:KILL, $(MOC):PMAC:M1:KILL)
alias(BOD:UNDS:3575:Y:MOTOR:ELOSS, $(MOC):PMAC:M1:ELOSS)
alias(BOD:UNDS:3575:Y:MOTOR:ELOSSRC, $(MOC):PMAC:M1:ELOSSRC)
alias(BOD:UNDS:3575:Y:MOTOR:MR_MOVE_WRITE, $(MOC):PMAC:M1:MR_MOVE_WRITE)
alias(BOD:UNDS:3575:Y:MOTOR:CHECK_SYNC, $(MOC):PMAC:M1:CHECK_SYNC)
alias(BOD:UNDS:3575:Y:MOTOR:FERROR, $(MOC):PMAC:M1:FERROR)
alias(BOD:UNDS:3575:Y:MOTOR:FERRORMAX, $(MOC):PMAC:M1:FERRORMAX)
alias(BOD:UNDS:3575:Y:MOTOR:FEMAXRESET, $(MOC):PMAC:M1:FEMAXRESET)
alias(BOD:UNDS:3575:Y:MOTOR:HOMEVIS, $(MOC):PMAC:M1:HOMEVIS)
alias(BOD:UNDS:3575:Y:MOTOR:HOMEVISSTR, $(MOC):PMAC:M1:HOMEVISSTR)


############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "BOD:UNDS:3575:Y:MOTOR:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "BOD:UNDS:3575:Y:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3575:Y:MOTOR.DIR CP")
  field(INPC, "BOD:UNDS:3575:Y:MOTOR:SCALE")
  field(OUT, "BOD:UNDS:3575:Y:MOTOR:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "BOD:UNDS:3575:Y:MOTOR:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+1`")
  field(FLNK, "BOD:UNDS:3575:Y:MOTOR:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "BOD:UNDS:3575:Y:MOTOR:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "BOD:UNDS:3575:Y:MOTOR.OFF CP")
  field(OUT, "BOD:UNDS:3575:Y:MOTOR:OFF_SET PP")
}

record(ao, "BOD:UNDS:3575:Y:MOTOR:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+1`")
  field(FLNK, "BOD:UNDS:3575:Y:MOTOR:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "BOD:UNDS:3575:Y:MOTOR:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),1)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "BOD:UNDS:3575:Y:MOTOR:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),1)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "BOD:UNDS:3575:Y:MOTOR:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),1)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "BOD:UNDS:3575:Y:MOTOR:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),1)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "BOD:UNDS:3575:Y:MOTOR:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),1)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to BOD:UNDS:3575:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "BOD:UNDS:3575:Y:MOTOR:SDIS") {
#  field(INPA, "BOD:UNDS:3575::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "BOD:UNDS:3575::HM:HOMING") {
}

# Monitor homing status
record(bi, "BOD:UNDS:3575:Y:MOTOR:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "BOD:UNDS:3575::HM:HOMING CP")
  field(FLNK, "BOD:UNDS:3575:Y:MOTOR:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, BOD:UNDS:3575:Y:MOTOR:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "BOD:UNDS:3575:Y:MOTOR:HOMING")
    field(CALC, "A")
    field(OUT, "BOD:UNDS:3575:Y:MOTOR:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, BOD:UNDS:3575:Y:MOTOR:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),1)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "BOD:UNDS:3575:Y:MOTOR:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#1P")
  field(DESC, "BOD1 Y")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "BOD:UNDS:3575:Y:MOTOR:HMPOS:CALC") {
  field(INPA, "BOD:UNDS:3575:Y:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3575:Y:MOTOR:SCALE")  
  field(INPC, "BOD:UNDS:3575:Y:MOTOR:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "BOD:UNDS:3575:Y:MOTOR:HMPOS PP")
}

record(ai, "BOD:UNDS:3575:Y:MOTOR:HMPOS") {
  field(DESC, "BOD1 Y")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "BOD:UNDS:3575:Y:MOTOR:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "BOD:UNDS:3575:Y:MOTOR")
  field(OUT, "BOD:UNDS:3575::HM:M1PV PP")
}

record(stringout, "BOD:UNDS:3575::HM:M1PV"){
}

# define a kill record
record(ao, "BOD:UNDS:3575:Y:MOTOR:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),1)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "BOD:UNDS:3575:Y:MOTOR:ELOSSVAR") {
  field(CALC, "464+1")
  field(PINI, "YES")
}

record(ai, "BOD:UNDS:3575:Y:MOTOR:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+1`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "BOD:UNDS:3575:Y:MOTOR:ELOSSRC") {
  field(OUT, "BOD:UNDS:3575:Y:MOTOR:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "BOD:UNDS:3575:Y:MOTOR:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+1`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS1:DESC"){
  field(VAL, "M1 BOD1 Y")
}

#record(bo, "BOD:UNDS:3575:Y:MOTOR:HOMED") {
#  field(DESC, "Set homed status for axis 1")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "BOD:UNDS:3575:Y:MOTOR:HOMEDSET")
#}

#record(calcout, "BOD:UNDS:3575:Y:MOTOR:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "BOD:UNDS:3575:Y:MOTOR:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m145) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "BOD:UNDS:3575:Y:MOTOR:DirectDemand")
{
    field(FLNK, "BOD:UNDS:3575:Y:MOTOR:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "BOD:UNDS:3575:Y:MOTOR:DirectCountsCalc")
{
    field(INPA, "BOD:UNDS:3575:Y:MOTOR:DirectDemand")
    field(INPB, "BOD:UNDS:3575:Y:MOTOR:Mres PP")
    field(INPC, "BOD:UNDS:3575:Y:MOTOR:Offset PP")
    field(INPD, "BOD:UNDS:3575:Y:MOTOR:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "BOD:UNDS:3575:Y:MOTOR:RawDirectDemand PP")
}

record(ao, "BOD:UNDS:3575:Y:MOTOR:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "BOD:UNDS:3575:Y:MOTOR:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),1,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD1 Y")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "BOD:UNDS:3575:Y:MOTOR:DirectMotorSeq")
{
    field(LNK1, "BOD:UNDS:3575:Y:MOTOR:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "BOD:UNDS:3575:Y:MOTOR:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),1,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "BOD:UNDS:3575:Y:MOTOR:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),1,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "BOD:UNDS:3575:Y:MOTOR:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),1,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "BOD:UNDS:3575:Y:MOTOR:MresSource")
}

record(scalcout, BOD:UNDS:3575:Y:MOTOR:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3575:Y:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "BOD:UNDS:3575:Y:MOTOR:Mres.INPA CA")
    field(FLNK, "BOD:UNDS:3575:Y:MOTOR:OffsetSource")
}

record(scalcout, "BOD:UNDS:3575:Y:MOTOR:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3575:Y:MOTOR:MresDrv PP")
    field(PREC, 6)
}

record(ao,"BOD:UNDS:3575:Y:MOTOR:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),1,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, BOD:UNDS:3575:Y:MOTOR:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3575:Y:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "BOD:UNDS:3575:Y:MOTOR:Offset.INPA CA")
}


record(scalcout, BOD:UNDS:3575:Y:MOTOR:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3575:Y:MOTOR:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"BOD:UNDS:3575:Y:MOTOR:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),1,4)PMAC_OFFSET")
}

# add numbered name aliases for above 3 templates
############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "$(MOC):PMAC:M1:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "$(MOC):PMAC:M1.MRES CP")
  field(INPB, "$(MOC):PMAC:M1.DIR CP")
  field(INPC, "$(MOC):PMAC:M1:SCALE")
  field(OUT, "$(MOC):PMAC:M1:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "$(MOC):PMAC:M1:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+1`")
  field(FLNK, "$(MOC):PMAC:M1:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "$(MOC):PMAC:M1:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "$(MOC):PMAC:M1.OFF CP")
  field(OUT, "$(MOC):PMAC:M1:OFF_SET PP")
}

record(ao, "$(MOC):PMAC:M1:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+1`")
  field(FLNK, "$(MOC):PMAC:M1:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "$(MOC):PMAC:M1:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),1)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "$(MOC):PMAC:M1:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),1)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "$(MOC):PMAC:M1:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),1)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "$(MOC):PMAC:M1:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),1)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "$(MOC):PMAC:M1:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),1)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to $(MOC):PMAC:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "$(MOC):PMAC:M1:SDIS") {
#  field(INPA, "$(MOC):PMAC::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "$(MOC):PMAC::HM:HOMING") {
}

# Monitor homing status
record(bi, "$(MOC):PMAC:M1:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "$(MOC):PMAC::HM:HOMING CP")
  field(FLNK, "$(MOC):PMAC:M1:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, $(MOC):PMAC:M1:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "$(MOC):PMAC:M1:HOMING")
    field(CALC, "A")
    field(OUT, "$(MOC):PMAC:M1:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, $(MOC):PMAC:M1:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),1)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "$(MOC):PMAC:M1:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#1P")
  field(DESC, "BOD1 Y")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "$(MOC):PMAC:M1:HMPOS:CALC") {
  field(INPA, "$(MOC):PMAC:M1.MRES CP")
  field(INPB, "$(MOC):PMAC:M1:SCALE")  
  field(INPC, "$(MOC):PMAC:M1:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "$(MOC):PMAC:M1:HMPOS PP")
}

record(ai, "$(MOC):PMAC:M1:HMPOS") {
  field(DESC, "BOD1 Y")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "$(MOC):PMAC:M1:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "$(MOC):PMAC:M1")
  field(OUT, "$(MOC):PMAC::HM:M1PV PP")
}

record(stringout, "$(MOC):PMAC::HM:M1PV"){
}

# define a kill record
record(ao, "$(MOC):PMAC:M1:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),1)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "$(MOC):PMAC:M1:ELOSSVAR") {
  field(CALC, "464+1")
  field(PINI, "YES")
}

record(ai, "$(MOC):PMAC:M1:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+1`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "$(MOC):PMAC:M1:ELOSSRC") {
  field(OUT, "$(MOC):PMAC:M1:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "$(MOC):PMAC:M1:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+1`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS1:DESC"){
  field(VAL, "M1 BOD1 Y")
}

#record(bo, "$(MOC):PMAC:M1:HOMED") {
#  field(DESC, "Set homed status for axis 1")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "$(MOC):PMAC:M1:HOMEDSET")
#}

#record(calcout, "$(MOC):PMAC:M1:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "$(MOC):PMAC:M1:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m145) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "$(MOC):PMAC:M1:DirectDemand")
{
    field(FLNK, "$(MOC):PMAC:M1:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "$(MOC):PMAC:M1:DirectCountsCalc")
{
    field(INPA, "$(MOC):PMAC:M1:DirectDemand")
    field(INPB, "$(MOC):PMAC:M1:Mres PP")
    field(INPC, "$(MOC):PMAC:M1:Offset PP")
    field(INPD, "$(MOC):PMAC:M1:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "$(MOC):PMAC:M1:RawDirectDemand PP")
}

record(ao, "$(MOC):PMAC:M1:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "$(MOC):PMAC:M1:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),1,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD1 Y")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "$(MOC):PMAC:M1:DirectMotorSeq")
{
    field(LNK1, "$(MOC):PMAC:M1:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "$(MOC):PMAC:M1:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),1,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "$(MOC):PMAC:M1:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),1,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "$(MOC):PMAC:M1:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),1,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "$(MOC):PMAC:M1:MresSource")
}

record(scalcout, $(MOC):PMAC:M1:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M1:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "$(MOC):PMAC:M1:Mres.INPA CA")
    field(FLNK, "$(MOC):PMAC:M1:OffsetSource")
}

record(scalcout, "$(MOC):PMAC:M1:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M1:MresDrv PP")
    field(PREC, 6)
}

record(ao,"$(MOC):PMAC:M1:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),1,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, $(MOC):PMAC:M1:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M1:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "$(MOC):PMAC:M1:Offset.INPA CA")
}


record(scalcout, $(MOC):PMAC:M1:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M1:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"$(MOC):PMAC:M1:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),1,4)PMAC_OFFSET")
}# % macro, PMAC, The P prefix for the motor controller (must match P in pmacController.template)

# this template defines a motor with two names:-
# (1) root is BOD:UNDS:3575:X:MOTOR, the macros fully define the name of the motor
# (2) 'numbered' names of the form $(MOC):PMAC:Mx where x is the motor number
# names are both assigned to the same PV via aliases in the case basic_asyn_motor
# or simply duplicated for all other included templates, this approach works because
# the PVs all point to the same ASYN parameters and improves maintainability of this
# file

# include motor record from the motor module
#% macro, __doc__, Basic template, including motor record and associated tags
#% macro, P      , Device Prefix
#% macro, M      , Device Suffix
#% macro, PORT   , Asyn port for motor record
#% macro, ADDR   , Address on controller
#% macro, DESC   , Description, displayed on EDM screen
#% macro, MRES   , Motor Step Size (EGU)
#% macro, DTYP   , DTYP of record
#% macro, DIR    , User Direction
#% macro, VBAS   , Base Velocity (EGU/s)
#% macro, VELO   , Velocity (EGU/s)
#% macro, VMAX   , Max Velocity (EGU/s), defaults to VELO
#% macro, ACCL   , Seconds to Velocity
#% macro, BDST   , BL Distance (EGU)
#% macro, BVEL   , BL Velocity (EGU/s)
#% macro, BACC   , BL Seconds to Veloc.
#% macro, PREC   , Display Precision
#% macro, EGU    , Engineering Units
#% macro, DHLM   , Dial High Limit
#% macro, DLLM   , Dial Low Limit
#% macro, HLM    , User High Limit
#% macro, LLM    , User Low Limit
#% macro, HLSV   , HW Lim. Violation Svr
#% macro, INIT   , Startup commands
#% macro, SREV   , Steps per Revolution
#% macro, RRES   , Readback Step Size (EGU)
#% macro, TWV    , Tweak Step Size (EGU)
#% macro, ERES   , Encoder Step Size (EGU)
#% macro, JAR    , Jog Acceleration (EGU/s^2)
#% macro, UEIP   , Use Encoder If Present
#% macro, URIP   , Use RDBL If Present
#% macro, RDBL   , Readback Location, set URIP = 1 if you specify this
#% macro, RLNK   , Readback output link
#% macro, RTRY   , Max retry count
#% macro, DLY    , Readback settle time (s)
#% macro, OFF    , User Offset (EGU)
#% macro, RDBD   , Retry Deadband (EGU)
#% macro, FOFF   , Freeze Offset, 0=variable, 1=frozen
#% macro, ADEL   , Alarm monitor deadband (EGU)
#% macro, NTM    , New Target Monitor, only set to 0 for soft motors
#% macro, FEHIGH , HIGH limit for following error
#% macro, FEHIHI , HIHI limit for following error
#% macro, FEHHSV , HIHI alarm severity for following error
#% macro, FEHSV  , HIGH alarm severity for following error
#% macro, SCALE  , Scale factor, if pmacSetAxisScale is used this should be set
#% macro, HOMEVIS, If 1 then home is visible on the gui
#% macro, HOMEVISSTR, If HOMEVIS=0, then display this text on the gui instead
#% macro, name   , Object name and gui association name
#% macro, alh    , Set this to alh to add the motor to the alarm handler and send emails, 
#                  set to '#' to comment out alh lines
#% macro, gda_name, Name to export this as to GDA
#% macro, gda_desc, Description to export this as to GDA

# AUTOSAVE: level 0 = before record init, level 1 = before AND after record init
#% autosave 0 DVAL OFF
#% autosave 1 DIR DHLM DLLM TWV VBAS VELO ACCL BDST BVEL BACC RDBD EGU RTRY UEIP URIP DLY PREC DISA DISP FOFF OFF FRAC OMSL JVEL JAR ADEL MDEL
#% archiver 0.5 Monitor
#% archiver 0.5 Monitor RBV
#% archiver 10 Monitor OFF
#% archiver 10 Monitor MRES
#% gdatag,template,simpleMotor,,BOD1 X
#% gdatag,motor,rw,,RECORD,Motor
#% alh
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:X:MOTOR

# This associates an edm screen with the template
# % gui, M2, edm, motor.edl, motor=BOD:UNDS:3575:X:MOTOR
# % gui, M2, edmembed, motor-embed-small.edl, motor=BOD:UNDS:3575:X:MOTOR,filename=motor.edl,box-label=BOD1 X

# This associates a BOY screen with the template
# % gui, M2, boydetail, motorApp_opi/motor_detail.opi, P=BOD:UNDS:3575:,M=X:MOTOR,DESC=BOD1 X, name=M2
# % gui, M2, boyembed, motorApp_opi/motor_embed_box.opi, P=BOD:UNDS:3575:,M=X:MOTOR,DESC=BOD1 X, name=M2
# % gui, M2, boyembed, motorApp_opi/motor_embed.opi, P=BOD:UNDS:3575:,M=X:MOTOR,DESC=BOD1 X, name=M2
# FIXME: this should be in pmacUtil or tpmac
# % gui, M2, boyembed, motorApp_opi/motor_homed_embed.opi, P=BOD:UNDS:3575:,M=X:MOTOR

# This makes the component icon reflect the status and severity
# % gui, M2, status, <pv>.MOVN
# % gui, M2, sevr

# These define what PVs a motor detail screen should contain
# % gui, M2, statusbits, Status,  <pv>.MSTA, Direction Positive, Done, High Limit, Home Limit, Unused, Closed Loop, Following Error, At Home, Encoder Present, Problem, Moving, Gain Support, Comms Error, Low Limit, Homed
# % gui, M2, statusbits, ELoss,  <pv>:ELOSS, Encoder Loss, Amplifier Loss, Sys Fail
# % gui, M2, command, ELoss Clear,  <pv>:ELOSSRC.A
# % gui, M2, statusbits, Limit Violation,  <pv>.LVIO, Lim
# % gui, M2, statusbits, At High Limit,  <pv>.HLS, High
# % gui, M2, statusbits, At Low Limit,  <pv>.LLS, Low
# % gui, M2, demand, User High Limit,  <pv>.HLM
# % gui, M2, demand, User Low Limit,  <pv>.LLM
# % gui, M2, demand, Dial High Limit, <pv>.DHLM
# % gui, M2, demand, Dial Low Limit, <pv>.DLLM

# % gui, M2, demand, Motor demand,  <pv>.VAL
# % gui, M2, readback, Motor readback,  <pv>.RBV
# % gui, M2, command, Stop,  <pv>.STOP
# % gui, M2, command, Home Forward,  <pv>.HOMF
# % gui, M2, command, Home Reverse,  <pv>.HOMR
# % gui, M2, command, Jog Forward, <pv>.JOGF
# % gui, M2, command, Jog Reverse, <pv>.JOGR
# % gui, M2, command, Tweak Forward,  <pv>.TWF
# % gui, M2, command, Tweak Reverse,  <pv>.TWR
# % gui, M2, demand, Tweak Step,  <pv>.TWV
# % gui, M2, command, Kill,  <pv>:KILL.PROC, Kill
# % gui, M2, command, Sync VAL=RBV,  <pv>.SYNC

# % gui, M2, enum, Direction,   <pv>.DIR
# % gui, M2, demand, User Offset,  <pv>.OFF
# % gui, M2, enum, Set/Use,   <pv>.SET
# % gui, M2, enum, Offset,   <pv>.FOFF
# % gui, M2, enum, Use Encoder,   <pv>.UEIP

# % gui, M2, demand, Motor Step Size,  <pv>.MRES
# % gui, M2, readback, Steps per Rev,   <pv>.SREV
# % gui, M2, readback, EGUs per Rev,   <pv>.UREV
# % gui, M2, demand, Encoder Step Size,  <pv>.ERES
# % gui, M2, readback, Readback Step Size,   <pv>.RRES
# % gui, M2, readback, Use Encoder if Present,   <pv>.UEIP

# % gui, M2, demand, Max Velocity,  <pv>.VMAX
# % gui, M2, demand, Base Velocity,   <pv>.VBAS
# % gui, M2, demand, Velocity,  <pv>.VELO
# % gui, M2, demand, Secs to Velocity,  <pv>.ACCL
# % gui, M2, demand, JVEL,   <pv>.JVEL
# % gui, M2, demand, Jog Acceleration,   <pv>.JAR
# % gui, M2, demand, Backlash Distance,   <pv>.BDST
# % gui, M2, demand, Backlash Velocity,   <pv>.BVEL
# % gui, M2, demand, Backlash Secs to Vel,   <pv>.BACC
# % gui, M2, demand, Move Fraction,   <pv>.FRAC
# % gui, M2, demand, Retry Deadband,   <pv>.RDBD
# % gui, M2, demand, Max Retrys,   <pv>.RTRY

# % gui, M2, demand, PREC,   <pv>.PREC
# % gui, M2, demand, EGU,   <pv>.EGU

# % gui, M2, demand, Output Specification,   <pv>.OUT
# % gui, M2, readback, Readback Location,   <pv>.RDBL
# % gui, M2, readback, Desired Output Loc,   <pv>.DOL
# % gui, M2, readback, Output Mode Select,   <pv>.OMSL
# % gui, M2, readback, Readback Out Link,   <pv>.RLNK
# % gui, M2, demand, DMOV Input Link,   <pv>.DINP
# % gui, M2, demand, RMP Input Link,   <pv>.RINP
# % gui, M2, demand, Stop Out Link,   <pv>.STOO


record(motor,"BOD:UNDS:3575:X:MOTOR")
{
	field(DESC,"BOD1 X")
	field(DTYP,"asynMotor")
	field(DIR,"0")
	field(VELO,"0.1")
	field(VBAS,"0")
	field(ACCL,"0.5")
	field(BDST,"0")
	field(BVEL,"0")
	field(BACC,"")
	field(OUT,"@asyn($(PORT),2)")
	field(MRES,"50e-6")
	field(PREC,"4")
	field(EGU,"mm")
	field(DHLM,"70.0")
	field(DLLM,"0")
	field(HLM,"")
	field(LLM,"")	
	field(HLSV,"NO_ALARM")
	field(INIT,"")
	field(RTRY,"0")
	field(DLY,"0")	
	field(HVEL,"0")
	field(SREV, "1")
	field(RRES, "")
	field(TWV, "0.1")
	field(ERES, "50e-6")
	field(JVEL, "0.1")
	field(JAR, "")
	field(UEIP, "1")
	field(URIP, "0")	
	field(RDBL, "")		
	field(VMAX, "0.1")
	field(OFF, "0")
	field(RDBD, "")
	field(FOFF, "0")
	field(ADEL, "0")
	field(NTM, "1")
	field(SDIS, "BOD:UNDS:3575:X:MOTOR:SDIS.VAL")	
	field(RLNK, "")
}

# record that holds the ADDR
record(ao, "BOD:UNDS:3575:X:MOTOR:ADDR") {
    field(PINI, "YES")
    field(VAL, "2")
}

# record that holds the PORT
record(stringout, "BOD:UNDS:3575:X:MOTOR:PORT") {
    field(PINI, "YES")
    field(VAL, "$(PORT)")
}

# record that holds the SCALE
record(ao, "BOD:UNDS:3575:X:MOTOR:SCALE") {
    field(PINI, "YES")
    field(VAL, "20000")
}

# Write a 1 to this record over Channel Access to disable the motor record and prevent CA writes to any field
# including the demand field.
record(ai, "BOD:UNDS:3575:X:MOTOR:USER:SDIS") {
    field(VAL, "0")
    field(FLNK, "BOD:UNDS:3575:X:MOTOR:SDIS")
}

# Note: This record forms part of the interface to auto-homing, which uses the "A" field to disable the motor record
# when homing.
# record that will disable and re-enable record if any of its inputs are non-zero
record(calcout, "BOD:UNDS:3575:X:MOTOR:SDIS") {
  field(DESC, "Disable on non-zero input")
  field(INPL, "BOD:UNDS:3575:X:MOTOR:USER:SDIS")
  field(CALC, "(A|B|C|D|E|F|G|H|I|J|K|L)>0")
  field(OUT, "BOD:UNDS:3575:X:MOTOR.DISP PP")
}

# dummy record that looks like a kill command
record(ao, "BOD:UNDS:3575:X:MOTOR:KILL") {
    field(VAL, "1")
}

# dummy record that looks like eloss status
#% archiver 10 Monitor VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:X:MOTOR:ELOSS
record(ai, "BOD:UNDS:3575:X:MOTOR:ELOSS") {
    field(PINI, "YES")
    field(HIHI, "1")
    field(HHSV, "MAJOR")    
}

# dummy record that looks like an eloss reset
record(calcout, "BOD:UNDS:3575:X:MOTOR:ELOSSRC") {
}

# check if SYNC has been pressed
record(calcout, "BOD:UNDS:3575:X:MOTOR:CHECK_SYNC") {
  field(INPA, "BOD:UNDS:3575:X:MOTOR.SYNC CP")  
  field(CALC, "A>0")
  field(OOPT, "When Non-zero")
  field(OUT, "BOD:UNDS:3575:X:MOTOR:MR_MOVE_WRITE.PROC PP")
}

# dummy record that looks like a record to notify a CS about a raw motor move
record(longout, "BOD:UNDS:3575:X:MOTOR:MR_MOVE_WRITE") {
}

#Record to calculate the following error
#% archiver 1 Monitor
record(calc, "BOD:UNDS:3575:X:MOTOR:FERROR")
{
	field(DESC,"Following Error")
	field(INPA,"BOD:UNDS:3575:X:MOTOR.RMP CP")
	field(INPB,"BOD:UNDS:3575:X:MOTOR.REP NPP")
	field(INPC,"BOD:UNDS:3575:X:MOTOR.MRES NPP")
	field(INPD,"BOD:UNDS:3575:X:MOTOR.ERES NPP")
	field(CALC,"ABS((A*C)-(B*D))")
	field(FLNK,"BOD:UNDS:3575:X:MOTOR:FERRORMAX")
	field(PREC,"4")
	field(EGU, "mm")
}

#Record to store the maximum following error
#% archiver 10 Monitor VAL
#% autosave 1 VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:X:MOTOR:FERRORMAX
record(calc, "BOD:UNDS:3575:X:MOTOR:FERRORMAX")
{
	field(DESC,"Following Error Max")
	field(INPA,"BOD:UNDS:3575:X:MOTOR:FERROR.VAL")
	field(INPB,"BOD:UNDS:3575:X:MOTOR:FERRORMAX.VAL")
	field(CALC,"(A>B)?A:B")
	field(HIGH,"0")
	field(HIHI,"0")
	field(HHSV,"NO_ALARM")
	field(HSV, "NO_ALARM")
	field(PREC,"4")
	field(EGU, "mm")
}	

#Record to reset the maximum following error
record(bo, "BOD:UNDS:3575:X:MOTOR:FEMAXRESET")
{
	field(DESC,"Reset max following error")
	field(DTYP,"Soft Channel")
	field(OUT, "BOD:UNDS:3575:X:MOTOR:FERRORMAX.VAL")
	field(VAL, "0")
}	


#Record to determin HOME button visibility on the motor.edl screen.
record(bo, "BOD:UNDS:3575:X:MOTOR:HOMEVIS")
{
	field(DESC,"Home button visibility")
	field(DTYP,"Soft Channel")
	field(VAL, "1")
        field(ZNAM, "Invisible")
	field(ONAM, "Visible")
}	 

#Record to display a message if the HOME buttons are not visible (if HOMEVIS=0).
record(stringout, "BOD:UNDS:3575:X:MOTOR:HOMEVISSTR")
{
	field(DESC, "Home visibility string")
	field(DTYP,"Soft Channel")
	field(VAL, "")
}


# numbered name aliases for all PVs in basic_asyn_motor.template
alias(BOD:UNDS:3575:X:MOTOR, $(MOC):PMAC:M2)
alias(BOD:UNDS:3575:X:MOTOR:ADDR, $(MOC):PMAC:M2:ADDR)
alias(BOD:UNDS:3575:X:MOTOR:PORT, $(MOC):PMAC:M2:PORT)
alias(BOD:UNDS:3575:X:MOTOR:SCALE, $(MOC):PMAC:M2:SCALE)
alias(BOD:UNDS:3575:X:MOTOR:USER:SDIS, $(MOC):PMACM2:USER:SDIS)
alias(BOD:UNDS:3575:X:MOTOR:SDIS, $(MOC):PMAC:M2:SDIS)
alias(BOD:UNDS:3575:X:MOTOR:KILL, $(MOC):PMAC:M2:KILL)
alias(BOD:UNDS:3575:X:MOTOR:ELOSS, $(MOC):PMAC:M2:ELOSS)
alias(BOD:UNDS:3575:X:MOTOR:ELOSSRC, $(MOC):PMAC:M2:ELOSSRC)
alias(BOD:UNDS:3575:X:MOTOR:MR_MOVE_WRITE, $(MOC):PMAC:M2:MR_MOVE_WRITE)
alias(BOD:UNDS:3575:X:MOTOR:CHECK_SYNC, $(MOC):PMAC:M2:CHECK_SYNC)
alias(BOD:UNDS:3575:X:MOTOR:FERROR, $(MOC):PMAC:M2:FERROR)
alias(BOD:UNDS:3575:X:MOTOR:FERRORMAX, $(MOC):PMAC:M2:FERRORMAX)
alias(BOD:UNDS:3575:X:MOTOR:FEMAXRESET, $(MOC):PMAC:M2:FEMAXRESET)
alias(BOD:UNDS:3575:X:MOTOR:HOMEVIS, $(MOC):PMAC:M2:HOMEVIS)
alias(BOD:UNDS:3575:X:MOTOR:HOMEVISSTR, $(MOC):PMAC:M2:HOMEVISSTR)


############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "BOD:UNDS:3575:X:MOTOR:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "BOD:UNDS:3575:X:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3575:X:MOTOR.DIR CP")
  field(INPC, "BOD:UNDS:3575:X:MOTOR:SCALE")
  field(OUT, "BOD:UNDS:3575:X:MOTOR:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "BOD:UNDS:3575:X:MOTOR:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+2`")
  field(FLNK, "BOD:UNDS:3575:X:MOTOR:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "BOD:UNDS:3575:X:MOTOR:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "BOD:UNDS:3575:X:MOTOR.OFF CP")
  field(OUT, "BOD:UNDS:3575:X:MOTOR:OFF_SET PP")
}

record(ao, "BOD:UNDS:3575:X:MOTOR:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+2`")
  field(FLNK, "BOD:UNDS:3575:X:MOTOR:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "BOD:UNDS:3575:X:MOTOR:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),2)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "BOD:UNDS:3575:X:MOTOR:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),2)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "BOD:UNDS:3575:X:MOTOR:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),2)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "BOD:UNDS:3575:X:MOTOR:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),2)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "BOD:UNDS:3575:X:MOTOR:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),2)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to BOD:UNDS:3575:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "BOD:UNDS:3575:X:MOTOR:SDIS") {
#  field(INPA, "BOD:UNDS:3575::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "BOD:UNDS:3575::HM:HOMING") {
}

# Monitor homing status
record(bi, "BOD:UNDS:3575:X:MOTOR:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "BOD:UNDS:3575::HM:HOMING CP")
  field(FLNK, "BOD:UNDS:3575:X:MOTOR:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, BOD:UNDS:3575:X:MOTOR:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "BOD:UNDS:3575:X:MOTOR:HOMING")
    field(CALC, "A")
    field(OUT, "BOD:UNDS:3575:X:MOTOR:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, BOD:UNDS:3575:X:MOTOR:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),2)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "BOD:UNDS:3575:X:MOTOR:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#2P")
  field(DESC, "BOD1 X")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "BOD:UNDS:3575:X:MOTOR:HMPOS:CALC") {
  field(INPA, "BOD:UNDS:3575:X:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3575:X:MOTOR:SCALE")  
  field(INPC, "BOD:UNDS:3575:X:MOTOR:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "BOD:UNDS:3575:X:MOTOR:HMPOS PP")
}

record(ai, "BOD:UNDS:3575:X:MOTOR:HMPOS") {
  field(DESC, "BOD1 X")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "BOD:UNDS:3575:X:MOTOR:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "BOD:UNDS:3575:X:MOTOR")
  field(OUT, "BOD:UNDS:3575::HM:M2PV PP")
}

record(stringout, "BOD:UNDS:3575::HM:M2PV"){
}

# define a kill record
record(ao, "BOD:UNDS:3575:X:MOTOR:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),2)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "BOD:UNDS:3575:X:MOTOR:ELOSSVAR") {
  field(CALC, "464+2")
  field(PINI, "YES")
}

record(ai, "BOD:UNDS:3575:X:MOTOR:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+2`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "BOD:UNDS:3575:X:MOTOR:ELOSSRC") {
  field(OUT, "BOD:UNDS:3575:X:MOTOR:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "BOD:UNDS:3575:X:MOTOR:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+2`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS2:DESC"){
  field(VAL, "M2 BOD1 X")
}

#record(bo, "BOD:UNDS:3575:X:MOTOR:HOMED") {
#  field(DESC, "Set homed status for axis 2")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "BOD:UNDS:3575:X:MOTOR:HOMEDSET")
#}

#record(calcout, "BOD:UNDS:3575:X:MOTOR:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "BOD:UNDS:3575:X:MOTOR:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m245) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "BOD:UNDS:3575:X:MOTOR:DirectDemand")
{
    field(FLNK, "BOD:UNDS:3575:X:MOTOR:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "BOD:UNDS:3575:X:MOTOR:DirectCountsCalc")
{
    field(INPA, "BOD:UNDS:3575:X:MOTOR:DirectDemand")
    field(INPB, "BOD:UNDS:3575:X:MOTOR:Mres PP")
    field(INPC, "BOD:UNDS:3575:X:MOTOR:Offset PP")
    field(INPD, "BOD:UNDS:3575:X:MOTOR:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "BOD:UNDS:3575:X:MOTOR:RawDirectDemand PP")
}

record(ao, "BOD:UNDS:3575:X:MOTOR:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "BOD:UNDS:3575:X:MOTOR:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),2,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD1 X")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "BOD:UNDS:3575:X:MOTOR:DirectMotorSeq")
{
    field(LNK1, "BOD:UNDS:3575:X:MOTOR:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "BOD:UNDS:3575:X:MOTOR:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),2,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "BOD:UNDS:3575:X:MOTOR:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),2,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "BOD:UNDS:3575:X:MOTOR:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),2,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "BOD:UNDS:3575:X:MOTOR:MresSource")
}

record(scalcout, BOD:UNDS:3575:X:MOTOR:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3575:X:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "BOD:UNDS:3575:X:MOTOR:Mres.INPA CA")
    field(FLNK, "BOD:UNDS:3575:X:MOTOR:OffsetSource")
}

record(scalcout, "BOD:UNDS:3575:X:MOTOR:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3575:X:MOTOR:MresDrv PP")
    field(PREC, 6)
}

record(ao,"BOD:UNDS:3575:X:MOTOR:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),2,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, BOD:UNDS:3575:X:MOTOR:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3575:X:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "BOD:UNDS:3575:X:MOTOR:Offset.INPA CA")
}


record(scalcout, BOD:UNDS:3575:X:MOTOR:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3575:X:MOTOR:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"BOD:UNDS:3575:X:MOTOR:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),2,4)PMAC_OFFSET")
}

# add numbered name aliases for above 3 templates
############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "$(MOC):PMAC:M2:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "$(MOC):PMAC:M2.MRES CP")
  field(INPB, "$(MOC):PMAC:M2.DIR CP")
  field(INPC, "$(MOC):PMAC:M2:SCALE")
  field(OUT, "$(MOC):PMAC:M2:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "$(MOC):PMAC:M2:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+2`")
  field(FLNK, "$(MOC):PMAC:M2:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "$(MOC):PMAC:M2:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "$(MOC):PMAC:M2.OFF CP")
  field(OUT, "$(MOC):PMAC:M2:OFF_SET PP")
}

record(ao, "$(MOC):PMAC:M2:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+2`")
  field(FLNK, "$(MOC):PMAC:M2:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "$(MOC):PMAC:M2:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),2)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "$(MOC):PMAC:M2:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),2)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "$(MOC):PMAC:M2:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),2)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "$(MOC):PMAC:M2:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),2)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "$(MOC):PMAC:M2:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),2)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to $(MOC):PMAC:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "$(MOC):PMAC:M2:SDIS") {
#  field(INPA, "$(MOC):PMAC::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "$(MOC):PMAC::HM:HOMING") {
}

# Monitor homing status
record(bi, "$(MOC):PMAC:M2:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "$(MOC):PMAC::HM:HOMING CP")
  field(FLNK, "$(MOC):PMAC:M2:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, $(MOC):PMAC:M2:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "$(MOC):PMAC:M2:HOMING")
    field(CALC, "A")
    field(OUT, "$(MOC):PMAC:M2:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, $(MOC):PMAC:M2:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),2)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "$(MOC):PMAC:M2:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#2P")
  field(DESC, "BOD1 X")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "$(MOC):PMAC:M2:HMPOS:CALC") {
  field(INPA, "$(MOC):PMAC:M2.MRES CP")
  field(INPB, "$(MOC):PMAC:M2:SCALE")  
  field(INPC, "$(MOC):PMAC:M2:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "$(MOC):PMAC:M2:HMPOS PP")
}

record(ai, "$(MOC):PMAC:M2:HMPOS") {
  field(DESC, "BOD1 X")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "$(MOC):PMAC:M2:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "$(MOC):PMAC:M2")
  field(OUT, "$(MOC):PMAC::HM:M2PV PP")
}

record(stringout, "$(MOC):PMAC::HM:M2PV"){
}

# define a kill record
record(ao, "$(MOC):PMAC:M2:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),2)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "$(MOC):PMAC:M2:ELOSSVAR") {
  field(CALC, "464+2")
  field(PINI, "YES")
}

record(ai, "$(MOC):PMAC:M2:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+2`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "$(MOC):PMAC:M2:ELOSSRC") {
  field(OUT, "$(MOC):PMAC:M2:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "$(MOC):PMAC:M2:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+2`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS2:DESC"){
  field(VAL, "M2 BOD1 X")
}

#record(bo, "$(MOC):PMAC:M2:HOMED") {
#  field(DESC, "Set homed status for axis 2")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "$(MOC):PMAC:M2:HOMEDSET")
#}

#record(calcout, "$(MOC):PMAC:M2:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "$(MOC):PMAC:M2:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m245) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "$(MOC):PMAC:M2:DirectDemand")
{
    field(FLNK, "$(MOC):PMAC:M2:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "$(MOC):PMAC:M2:DirectCountsCalc")
{
    field(INPA, "$(MOC):PMAC:M2:DirectDemand")
    field(INPB, "$(MOC):PMAC:M2:Mres PP")
    field(INPC, "$(MOC):PMAC:M2:Offset PP")
    field(INPD, "$(MOC):PMAC:M2:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "$(MOC):PMAC:M2:RawDirectDemand PP")
}

record(ao, "$(MOC):PMAC:M2:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "$(MOC):PMAC:M2:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),2,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD1 X")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "$(MOC):PMAC:M2:DirectMotorSeq")
{
    field(LNK1, "$(MOC):PMAC:M2:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "$(MOC):PMAC:M2:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),2,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "$(MOC):PMAC:M2:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),2,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "$(MOC):PMAC:M2:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),2,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "$(MOC):PMAC:M2:MresSource")
}

record(scalcout, $(MOC):PMAC:M2:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M2:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "$(MOC):PMAC:M2:Mres.INPA CA")
    field(FLNK, "$(MOC):PMAC:M2:OffsetSource")
}

record(scalcout, "$(MOC):PMAC:M2:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M2:MresDrv PP")
    field(PREC, 6)
}

record(ao,"$(MOC):PMAC:M2:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),2,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, $(MOC):PMAC:M2:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M2:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "$(MOC):PMAC:M2:Offset.INPA CA")
}


record(scalcout, $(MOC):PMAC:M2:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M2:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"$(MOC):PMAC:M2:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),2,4)PMAC_OFFSET")
}# % macro, PMAC, The P prefix for the motor controller (must match P in pmacController.template)

# this template defines a motor with two names:-
# (1) root is BOD:UNDS:3575:ANGLE:MOTOR, the macros fully define the name of the motor
# (2) 'numbered' names of the form $(MOC):PMAC:Mx where x is the motor number
# names are both assigned to the same PV via aliases in the case basic_asyn_motor
# or simply duplicated for all other included templates, this approach works because
# the PVs all point to the same ASYN parameters and improves maintainability of this
# file

# include motor record from the motor module
#% macro, __doc__, Basic template, including motor record and associated tags
#% macro, P      , Device Prefix
#% macro, M      , Device Suffix
#% macro, PORT   , Asyn port for motor record
#% macro, ADDR   , Address on controller
#% macro, DESC   , Description, displayed on EDM screen
#% macro, MRES   , Motor Step Size (EGU)
#% macro, DTYP   , DTYP of record
#% macro, DIR    , User Direction
#% macro, VBAS   , Base Velocity (EGU/s)
#% macro, VELO   , Velocity (EGU/s)
#% macro, VMAX   , Max Velocity (EGU/s), defaults to VELO
#% macro, ACCL   , Seconds to Velocity
#% macro, BDST   , BL Distance (EGU)
#% macro, BVEL   , BL Velocity (EGU/s)
#% macro, BACC   , BL Seconds to Veloc.
#% macro, PREC   , Display Precision
#% macro, EGU    , Engineering Units
#% macro, DHLM   , Dial High Limit
#% macro, DLLM   , Dial Low Limit
#% macro, HLM    , User High Limit
#% macro, LLM    , User Low Limit
#% macro, HLSV   , HW Lim. Violation Svr
#% macro, INIT   , Startup commands
#% macro, SREV   , Steps per Revolution
#% macro, RRES   , Readback Step Size (EGU)
#% macro, TWV    , Tweak Step Size (EGU)
#% macro, ERES   , Encoder Step Size (EGU)
#% macro, JAR    , Jog Acceleration (EGU/s^2)
#% macro, UEIP   , Use Encoder If Present
#% macro, URIP   , Use RDBL If Present
#% macro, RDBL   , Readback Location, set URIP = 1 if you specify this
#% macro, RLNK   , Readback output link
#% macro, RTRY   , Max retry count
#% macro, DLY    , Readback settle time (s)
#% macro, OFF    , User Offset (EGU)
#% macro, RDBD   , Retry Deadband (EGU)
#% macro, FOFF   , Freeze Offset, 0=variable, 1=frozen
#% macro, ADEL   , Alarm monitor deadband (EGU)
#% macro, NTM    , New Target Monitor, only set to 0 for soft motors
#% macro, FEHIGH , HIGH limit for following error
#% macro, FEHIHI , HIHI limit for following error
#% macro, FEHHSV , HIHI alarm severity for following error
#% macro, FEHSV  , HIGH alarm severity for following error
#% macro, SCALE  , Scale factor, if pmacSetAxisScale is used this should be set
#% macro, HOMEVIS, If 1 then home is visible on the gui
#% macro, HOMEVISSTR, If HOMEVIS=0, then display this text on the gui instead
#% macro, name   , Object name and gui association name
#% macro, alh    , Set this to alh to add the motor to the alarm handler and send emails, 
#                  set to '#' to comment out alh lines
#% macro, gda_name, Name to export this as to GDA
#% macro, gda_desc, Description to export this as to GDA

# AUTOSAVE: level 0 = before record init, level 1 = before AND after record init
#% autosave 0 DVAL OFF
#% autosave 1 DIR DHLM DLLM TWV VBAS VELO ACCL BDST BVEL BACC RDBD EGU RTRY UEIP URIP DLY PREC DISA DISP FOFF OFF FRAC OMSL JVEL JAR ADEL MDEL
#% archiver 0.5 Monitor
#% archiver 0.5 Monitor RBV
#% archiver 10 Monitor OFF
#% archiver 10 Monitor MRES
#% gdatag,template,simpleMotor,,BOD1 FW
#% gdatag,motor,rw,,RECORD,Motor
#% alh
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:ANGLE:MOTOR

# This associates an edm screen with the template
# % gui, M3, edm, motor.edl, motor=BOD:UNDS:3575:ANGLE:MOTOR
# % gui, M3, edmembed, motor-embed-small.edl, motor=BOD:UNDS:3575:ANGLE:MOTOR,filename=motor.edl,box-label=BOD1 Filter Wheel

# This associates a BOY screen with the template
# % gui, M3, boydetail, motorApp_opi/motor_detail.opi, P=BOD:UNDS:3575:,M=ANGLE:MOTOR,DESC=BOD1 Filter Wheel, name=M3
# % gui, M3, boyembed, motorApp_opi/motor_embed_box.opi, P=BOD:UNDS:3575:,M=ANGLE:MOTOR,DESC=BOD1 Filter Wheel, name=M3
# % gui, M3, boyembed, motorApp_opi/motor_embed.opi, P=BOD:UNDS:3575:,M=ANGLE:MOTOR,DESC=BOD1 Filter Wheel, name=M3
# FIXME: this should be in pmacUtil or tpmac
# % gui, M3, boyembed, motorApp_opi/motor_homed_embed.opi, P=BOD:UNDS:3575:,M=ANGLE:MOTOR

# This makes the component icon reflect the status and severity
# % gui, M3, status, <pv>.MOVN
# % gui, M3, sevr

# These define what PVs a motor detail screen should contain
# % gui, M3, statusbits, Status,  <pv>.MSTA, Direction Positive, Done, High Limit, Home Limit, Unused, Closed Loop, Following Error, At Home, Encoder Present, Problem, Moving, Gain Support, Comms Error, Low Limit, Homed
# % gui, M3, statusbits, ELoss,  <pv>:ELOSS, Encoder Loss, Amplifier Loss, Sys Fail
# % gui, M3, command, ELoss Clear,  <pv>:ELOSSRC.A
# % gui, M3, statusbits, Limit Violation,  <pv>.LVIO, Lim
# % gui, M3, statusbits, At High Limit,  <pv>.HLS, High
# % gui, M3, statusbits, At Low Limit,  <pv>.LLS, Low
# % gui, M3, demand, User High Limit,  <pv>.HLM
# % gui, M3, demand, User Low Limit,  <pv>.LLM
# % gui, M3, demand, Dial High Limit, <pv>.DHLM
# % gui, M3, demand, Dial Low Limit, <pv>.DLLM

# % gui, M3, demand, Motor demand,  <pv>.VAL
# % gui, M3, readback, Motor readback,  <pv>.RBV
# % gui, M3, command, Stop,  <pv>.STOP
# % gui, M3, command, Home Forward,  <pv>.HOMF
# % gui, M3, command, Home Reverse,  <pv>.HOMR
# % gui, M3, command, Jog Forward, <pv>.JOGF
# % gui, M3, command, Jog Reverse, <pv>.JOGR
# % gui, M3, command, Tweak Forward,  <pv>.TWF
# % gui, M3, command, Tweak Reverse,  <pv>.TWR
# % gui, M3, demand, Tweak Step,  <pv>.TWV
# % gui, M3, command, Kill,  <pv>:KILL.PROC, Kill
# % gui, M3, command, Sync VAL=RBV,  <pv>.SYNC

# % gui, M3, enum, Direction,   <pv>.DIR
# % gui, M3, demand, User Offset,  <pv>.OFF
# % gui, M3, enum, Set/Use,   <pv>.SET
# % gui, M3, enum, Offset,   <pv>.FOFF
# % gui, M3, enum, Use Encoder,   <pv>.UEIP

# % gui, M3, demand, Motor Step Size,  <pv>.MRES
# % gui, M3, readback, Steps per Rev,   <pv>.SREV
# % gui, M3, readback, EGUs per Rev,   <pv>.UREV
# % gui, M3, demand, Encoder Step Size,  <pv>.ERES
# % gui, M3, readback, Readback Step Size,   <pv>.RRES
# % gui, M3, readback, Use Encoder if Present,   <pv>.UEIP

# % gui, M3, demand, Max Velocity,  <pv>.VMAX
# % gui, M3, demand, Base Velocity,   <pv>.VBAS
# % gui, M3, demand, Velocity,  <pv>.VELO
# % gui, M3, demand, Secs to Velocity,  <pv>.ACCL
# % gui, M3, demand, JVEL,   <pv>.JVEL
# % gui, M3, demand, Jog Acceleration,   <pv>.JAR
# % gui, M3, demand, Backlash Distance,   <pv>.BDST
# % gui, M3, demand, Backlash Velocity,   <pv>.BVEL
# % gui, M3, demand, Backlash Secs to Vel,   <pv>.BACC
# % gui, M3, demand, Move Fraction,   <pv>.FRAC
# % gui, M3, demand, Retry Deadband,   <pv>.RDBD
# % gui, M3, demand, Max Retrys,   <pv>.RTRY

# % gui, M3, demand, PREC,   <pv>.PREC
# % gui, M3, demand, EGU,   <pv>.EGU

# % gui, M3, demand, Output Specification,   <pv>.OUT
# % gui, M3, readback, Readback Location,   <pv>.RDBL
# % gui, M3, readback, Desired Output Loc,   <pv>.DOL
# % gui, M3, readback, Output Mode Select,   <pv>.OMSL
# % gui, M3, readback, Readback Out Link,   <pv>.RLNK
# % gui, M3, demand, DMOV Input Link,   <pv>.DINP
# % gui, M3, demand, RMP Input Link,   <pv>.RINP
# % gui, M3, demand, Stop Out Link,   <pv>.STOO


record(motor,"BOD:UNDS:3575:ANGLE:MOTOR")
{
	field(DESC,"BOD1 Filter Wheel")
	field(DTYP,"asynMotor")
	field(DIR,"0")
	field(VELO,"30")
	field(VBAS,"0")
	field(ACCL,"0.5")
	field(BDST,"0")
	field(BVEL,"0")
	field(BACC,"")
	field(OUT,"@asyn($(PORT),3)")
	field(MRES,"0.018")
	field(PREC,"2")
	field(EGU,"deg")
	field(DHLM,"720")
	field(DLLM,"-720")
	field(HLM,"")
	field(LLM,"")	
	field(HLSV,"MAJOR")
	field(INIT,"")
	field(RTRY,"0")
	field(DLY,"0")	
	field(HVEL,"0")
	field(SREV, "1")
	field(RRES, "")
	field(TWV, "0.1")
	field(ERES, "0")
	field(JVEL, "30")
	field(JAR, "")
	field(UEIP, "0")
	field(URIP, "0")	
	field(RDBL, "")		
	field(VMAX, "30")
	field(OFF, "0")
	field(RDBD, "")
	field(FOFF, "0")
	field(ADEL, "0")
	field(NTM, "1")
	field(SDIS, "BOD:UNDS:3575:ANGLE:MOTOR:SDIS.VAL")	
	field(RLNK, "")
}

# record that holds the ADDR
record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:ADDR") {
    field(PINI, "YES")
    field(VAL, "3")
}

# record that holds the PORT
record(stringout, "BOD:UNDS:3575:ANGLE:MOTOR:PORT") {
    field(PINI, "YES")
    field(VAL, "$(PORT)")
}

# record that holds the SCALE
record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:SCALE") {
    field(PINI, "YES")
    field(VAL, "1")
}

# Write a 1 to this record over Channel Access to disable the motor record and prevent CA writes to any field
# including the demand field.
record(ai, "BOD:UNDS:3575:ANGLE:MOTOR:USER:SDIS") {
    field(VAL, "0")
    field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:SDIS")
}

# Note: This record forms part of the interface to auto-homing, which uses the "A" field to disable the motor record
# when homing.
# record that will disable and re-enable record if any of its inputs are non-zero
record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:SDIS") {
  field(DESC, "Disable on non-zero input")
  field(INPL, "BOD:UNDS:3575:ANGLE:MOTOR:USER:SDIS")
  field(CALC, "(A|B|C|D|E|F|G|H|I|J|K|L)>0")
  field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR.DISP PP")
}

# dummy record that looks like a kill command
record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:KILL") {
    field(VAL, "1")
}

# dummy record that looks like eloss status
#% archiver 10 Monitor VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:ANGLE:MOTOR:ELOSS
record(ai, "BOD:UNDS:3575:ANGLE:MOTOR:ELOSS") {
    field(PINI, "YES")
    field(HIHI, "1")
    field(HHSV, "MAJOR")    
}

# dummy record that looks like an eloss reset
record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:ELOSSRC") {
}

# check if SYNC has been pressed
record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:CHECK_SYNC") {
  field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR.SYNC CP")  
  field(CALC, "A>0")
  field(OOPT, "When Non-zero")
  field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:MR_MOVE_WRITE.PROC PP")
}

# dummy record that looks like a record to notify a CS about a raw motor move
record(longout, "BOD:UNDS:3575:ANGLE:MOTOR:MR_MOVE_WRITE") {
}

#Record to calculate the following error
#% archiver 1 Monitor
record(calc, "BOD:UNDS:3575:ANGLE:MOTOR:FERROR")
{
	field(DESC,"Following Error")
	field(INPA,"BOD:UNDS:3575:ANGLE:MOTOR.RMP CP")
	field(INPB,"BOD:UNDS:3575:ANGLE:MOTOR.REP NPP")
	field(INPC,"BOD:UNDS:3575:ANGLE:MOTOR.MRES NPP")
	field(INPD,"BOD:UNDS:3575:ANGLE:MOTOR.ERES NPP")
	field(CALC,"ABS((A*C)-(B*D))")
	field(FLNK,"BOD:UNDS:3575:ANGLE:MOTOR:FERRORMAX")
	field(PREC,"2")
	field(EGU, "deg")
}

#Record to store the maximum following error
#% archiver 10 Monitor VAL
#% autosave 1 VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3575:ANGLE:MOTOR:FERRORMAX
record(calc, "BOD:UNDS:3575:ANGLE:MOTOR:FERRORMAX")
{
	field(DESC,"Following Error Max")
	field(INPA,"BOD:UNDS:3575:ANGLE:MOTOR:FERROR.VAL")
	field(INPB,"BOD:UNDS:3575:ANGLE:MOTOR:FERRORMAX.VAL")
	field(CALC,"(A>B)?A:B")
	field(HIGH,"0")
	field(HIHI,"0")
	field(HHSV,"NO_ALARM")
	field(HSV, "NO_ALARM")
	field(PREC,"2")
	field(EGU, "deg")
}	

#Record to reset the maximum following error
record(bo, "BOD:UNDS:3575:ANGLE:MOTOR:FEMAXRESET")
{
	field(DESC,"Reset max following error")
	field(DTYP,"Soft Channel")
	field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:FERRORMAX.VAL")
	field(VAL, "0")
}	


#Record to determin HOME button visibility on the motor.edl screen.
record(bo, "BOD:UNDS:3575:ANGLE:MOTOR:HOMEVIS")
{
	field(DESC,"Home button visibility")
	field(DTYP,"Soft Channel")
	field(VAL, "1")
        field(ZNAM, "Invisible")
	field(ONAM, "Visible")
}	 

#Record to display a message if the HOME buttons are not visible (if HOMEVIS=0).
record(stringout, "BOD:UNDS:3575:ANGLE:MOTOR:HOMEVISSTR")
{
	field(DESC, "Home visibility string")
	field(DTYP,"Soft Channel")
	field(VAL, "")
}


# numbered name aliases for all PVs in basic_asyn_motor.template
alias(BOD:UNDS:3575:ANGLE:MOTOR, $(MOC):PMAC:M3)
alias(BOD:UNDS:3575:ANGLE:MOTOR:ADDR, $(MOC):PMAC:M3:ADDR)
alias(BOD:UNDS:3575:ANGLE:MOTOR:PORT, $(MOC):PMAC:M3:PORT)
alias(BOD:UNDS:3575:ANGLE:MOTOR:SCALE, $(MOC):PMAC:M3:SCALE)
alias(BOD:UNDS:3575:ANGLE:MOTOR:USER:SDIS, $(MOC):PMACM3:USER:SDIS)
alias(BOD:UNDS:3575:ANGLE:MOTOR:SDIS, $(MOC):PMAC:M3:SDIS)
alias(BOD:UNDS:3575:ANGLE:MOTOR:KILL, $(MOC):PMAC:M3:KILL)
alias(BOD:UNDS:3575:ANGLE:MOTOR:ELOSS, $(MOC):PMAC:M3:ELOSS)
alias(BOD:UNDS:3575:ANGLE:MOTOR:ELOSSRC, $(MOC):PMAC:M3:ELOSSRC)
alias(BOD:UNDS:3575:ANGLE:MOTOR:MR_MOVE_WRITE, $(MOC):PMAC:M3:MR_MOVE_WRITE)
alias(BOD:UNDS:3575:ANGLE:MOTOR:CHECK_SYNC, $(MOC):PMAC:M3:CHECK_SYNC)
alias(BOD:UNDS:3575:ANGLE:MOTOR:FERROR, $(MOC):PMAC:M3:FERROR)
alias(BOD:UNDS:3575:ANGLE:MOTOR:FERRORMAX, $(MOC):PMAC:M3:FERRORMAX)
alias(BOD:UNDS:3575:ANGLE:MOTOR:FEMAXRESET, $(MOC):PMAC:M3:FEMAXRESET)
alias(BOD:UNDS:3575:ANGLE:MOTOR:HOMEVIS, $(MOC):PMAC:M3:HOMEVIS)
alias(BOD:UNDS:3575:ANGLE:MOTOR:HOMEVISSTR, $(MOC):PMAC:M3:HOMEVISSTR)


############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3575:ANGLE:MOTOR.DIR CP")
  field(INPC, "BOD:UNDS:3575:ANGLE:MOTOR:SCALE")
  field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+3`")
  field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR.OFF CP")
  field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:OFF_SET PP")
}

record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+3`")
  field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "BOD:UNDS:3575:ANGLE:MOTOR:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),3)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "BOD:UNDS:3575:ANGLE:MOTOR:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),3)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "BOD:UNDS:3575:ANGLE:MOTOR:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),3)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "BOD:UNDS:3575:ANGLE:MOTOR:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),3)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "BOD:UNDS:3575:ANGLE:MOTOR:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),3)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to BOD:UNDS:3575:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:SDIS") {
#  field(INPA, "BOD:UNDS:3575::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "BOD:UNDS:3575::HM:HOMING") {
}

# Monitor homing status
record(bi, "BOD:UNDS:3575:ANGLE:MOTOR:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "BOD:UNDS:3575::HM:HOMING CP")
  field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, BOD:UNDS:3575:ANGLE:MOTOR:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR:HOMING")
    field(CALC, "A")
    field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, BOD:UNDS:3575:ANGLE:MOTOR:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),3)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "BOD:UNDS:3575:ANGLE:MOTOR:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#3P")
  field(DESC, "BOD1 Filter Wheel")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "2")
  field(EGU, "deg")
}

record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:HMPOS:CALC") {
  field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3575:ANGLE:MOTOR:SCALE")  
  field(INPC, "BOD:UNDS:3575:ANGLE:MOTOR:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:HMPOS PP")
}

record(ai, "BOD:UNDS:3575:ANGLE:MOTOR:HMPOS") {
  field(DESC, "BOD1 Filter Wheel")
  field(PREC, "2")
  field(EGU, "deg")
}

# at startup, tell autohome template about this motor
record(stringout, "BOD:UNDS:3575:ANGLE:MOTOR:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "BOD:UNDS:3575:ANGLE:MOTOR")
  field(OUT, "BOD:UNDS:3575::HM:M3PV PP")
}

record(stringout, "BOD:UNDS:3575::HM:M3PV"){
}

# define a kill record
record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),3)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "BOD:UNDS:3575:ANGLE:MOTOR:ELOSSVAR") {
  field(CALC, "464+3")
  field(PINI, "YES")
}

record(ai, "BOD:UNDS:3575:ANGLE:MOTOR:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+3`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:ELOSSRC") {
  field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+3`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS3:DESC"){
  field(VAL, "M3 BOD1 Filter Wheel")
}

#record(bo, "BOD:UNDS:3575:ANGLE:MOTOR:HOMED") {
#  field(DESC, "Set homed status for axis 3")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:HOMEDSET")
#}

#record(calcout, "BOD:UNDS:3575:ANGLE:MOTOR:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m345) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:DirectDemand")
{
    field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "BOD:UNDS:3575:ANGLE:MOTOR:DirectCountsCalc")
{
    field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR:DirectDemand")
    field(INPB, "BOD:UNDS:3575:ANGLE:MOTOR:Mres PP")
    field(INPC, "BOD:UNDS:3575:ANGLE:MOTOR:Offset PP")
    field(INPD, "BOD:UNDS:3575:ANGLE:MOTOR:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:RawDirectDemand PP")
}

record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),3,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD1 Filter Wheel")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "BOD:UNDS:3575:ANGLE:MOTOR:DirectMotorSeq")
{
    field(LNK1, "BOD:UNDS:3575:ANGLE:MOTOR:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "BOD:UNDS:3575:ANGLE:MOTOR:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),3,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "BOD:UNDS:3575:ANGLE:MOTOR:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),3,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "BOD:UNDS:3575:ANGLE:MOTOR:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),3,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:MresSource")
}

record(scalcout, BOD:UNDS:3575:ANGLE:MOTOR:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:Mres.INPA CA")
    field(FLNK, "BOD:UNDS:3575:ANGLE:MOTOR:OffsetSource")
}

record(scalcout, "BOD:UNDS:3575:ANGLE:MOTOR:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3575:ANGLE:MOTOR:MresDrv PP")
    field(PREC, 6)
}

record(ao,"BOD:UNDS:3575:ANGLE:MOTOR:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),3,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, BOD:UNDS:3575:ANGLE:MOTOR:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3575:ANGLE:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "BOD:UNDS:3575:ANGLE:MOTOR:Offset.INPA CA")
}


record(scalcout, BOD:UNDS:3575:ANGLE:MOTOR:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3575:ANGLE:MOTOR:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"BOD:UNDS:3575:ANGLE:MOTOR:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),3,4)PMAC_OFFSET")
}

# add numbered name aliases for above 3 templates
############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "$(MOC):PMAC:M3:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "$(MOC):PMAC:M3.MRES CP")
  field(INPB, "$(MOC):PMAC:M3.DIR CP")
  field(INPC, "$(MOC):PMAC:M3:SCALE")
  field(OUT, "$(MOC):PMAC:M3:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "$(MOC):PMAC:M3:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+3`")
  field(FLNK, "$(MOC):PMAC:M3:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "$(MOC):PMAC:M3:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "$(MOC):PMAC:M3.OFF CP")
  field(OUT, "$(MOC):PMAC:M3:OFF_SET PP")
}

record(ao, "$(MOC):PMAC:M3:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+3`")
  field(FLNK, "$(MOC):PMAC:M3:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "$(MOC):PMAC:M3:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),3)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "$(MOC):PMAC:M3:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),3)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "$(MOC):PMAC:M3:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),3)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "$(MOC):PMAC:M3:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),3)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "$(MOC):PMAC:M3:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),3)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to $(MOC):PMAC:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "$(MOC):PMAC:M3:SDIS") {
#  field(INPA, "$(MOC):PMAC::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "$(MOC):PMAC::HM:HOMING") {
}

# Monitor homing status
record(bi, "$(MOC):PMAC:M3:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "$(MOC):PMAC::HM:HOMING CP")
  field(FLNK, "$(MOC):PMAC:M3:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, $(MOC):PMAC:M3:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "$(MOC):PMAC:M3:HOMING")
    field(CALC, "A")
    field(OUT, "$(MOC):PMAC:M3:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, $(MOC):PMAC:M3:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),3)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "$(MOC):PMAC:M3:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#3P")
  field(DESC, "BOD1 Filter Wheel")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "2")
  field(EGU, "deg")
}

record(calcout, "$(MOC):PMAC:M3:HMPOS:CALC") {
  field(INPA, "$(MOC):PMAC:M3.MRES CP")
  field(INPB, "$(MOC):PMAC:M3:SCALE")  
  field(INPC, "$(MOC):PMAC:M3:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "$(MOC):PMAC:M3:HMPOS PP")
}

record(ai, "$(MOC):PMAC:M3:HMPOS") {
  field(DESC, "BOD1 Filter Wheel")
  field(PREC, "2")
  field(EGU, "deg")
}

# at startup, tell autohome template about this motor
record(stringout, "$(MOC):PMAC:M3:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "$(MOC):PMAC:M3")
  field(OUT, "$(MOC):PMAC::HM:M3PV PP")
}

record(stringout, "$(MOC):PMAC::HM:M3PV"){
}

# define a kill record
record(ao, "$(MOC):PMAC:M3:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),3)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "$(MOC):PMAC:M3:ELOSSVAR") {
  field(CALC, "464+3")
  field(PINI, "YES")
}

record(ai, "$(MOC):PMAC:M3:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+3`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "$(MOC):PMAC:M3:ELOSSRC") {
  field(OUT, "$(MOC):PMAC:M3:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "$(MOC):PMAC:M3:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+3`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS3:DESC"){
  field(VAL, "M3 BOD1 Filter Wheel")
}

#record(bo, "$(MOC):PMAC:M3:HOMED") {
#  field(DESC, "Set homed status for axis 3")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "$(MOC):PMAC:M3:HOMEDSET")
#}

#record(calcout, "$(MOC):PMAC:M3:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "$(MOC):PMAC:M3:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m345) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "$(MOC):PMAC:M3:DirectDemand")
{
    field(FLNK, "$(MOC):PMAC:M3:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "$(MOC):PMAC:M3:DirectCountsCalc")
{
    field(INPA, "$(MOC):PMAC:M3:DirectDemand")
    field(INPB, "$(MOC):PMAC:M3:Mres PP")
    field(INPC, "$(MOC):PMAC:M3:Offset PP")
    field(INPD, "$(MOC):PMAC:M3:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "$(MOC):PMAC:M3:RawDirectDemand PP")
}

record(ao, "$(MOC):PMAC:M3:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "$(MOC):PMAC:M3:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),3,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD1 Filter Wheel")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "$(MOC):PMAC:M3:DirectMotorSeq")
{
    field(LNK1, "$(MOC):PMAC:M3:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "$(MOC):PMAC:M3:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),3,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "$(MOC):PMAC:M3:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),3,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "$(MOC):PMAC:M3:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),3,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "$(MOC):PMAC:M3:MresSource")
}

record(scalcout, $(MOC):PMAC:M3:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M3:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "$(MOC):PMAC:M3:Mres.INPA CA")
    field(FLNK, "$(MOC):PMAC:M3:OffsetSource")
}

record(scalcout, "$(MOC):PMAC:M3:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M3:MresDrv PP")
    field(PREC, 6)
}

record(ao,"$(MOC):PMAC:M3:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),3,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, $(MOC):PMAC:M3:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M3:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "$(MOC):PMAC:M3:Offset.INPA CA")
}


record(scalcout, $(MOC):PMAC:M3:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M3:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"$(MOC):PMAC:M3:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),3,4)PMAC_OFFSET")
}# % macro, PMAC, The P prefix for the motor controller (must match P in pmacController.template)

# this template defines a motor with two names:-
# (1) root is BOD:UNDS:3795:Y:MOTOR, the macros fully define the name of the motor
# (2) 'numbered' names of the form $(MOC):PMAC:Mx where x is the motor number
# names are both assigned to the same PV via aliases in the case basic_asyn_motor
# or simply duplicated for all other included templates, this approach works because
# the PVs all point to the same ASYN parameters and improves maintainability of this
# file

# include motor record from the motor module
#% macro, __doc__, Basic template, including motor record and associated tags
#% macro, P      , Device Prefix
#% macro, M      , Device Suffix
#% macro, PORT   , Asyn port for motor record
#% macro, ADDR   , Address on controller
#% macro, DESC   , Description, displayed on EDM screen
#% macro, MRES   , Motor Step Size (EGU)
#% macro, DTYP   , DTYP of record
#% macro, DIR    , User Direction
#% macro, VBAS   , Base Velocity (EGU/s)
#% macro, VELO   , Velocity (EGU/s)
#% macro, VMAX   , Max Velocity (EGU/s), defaults to VELO
#% macro, ACCL   , Seconds to Velocity
#% macro, BDST   , BL Distance (EGU)
#% macro, BVEL   , BL Velocity (EGU/s)
#% macro, BACC   , BL Seconds to Veloc.
#% macro, PREC   , Display Precision
#% macro, EGU    , Engineering Units
#% macro, DHLM   , Dial High Limit
#% macro, DLLM   , Dial Low Limit
#% macro, HLM    , User High Limit
#% macro, LLM    , User Low Limit
#% macro, HLSV   , HW Lim. Violation Svr
#% macro, INIT   , Startup commands
#% macro, SREV   , Steps per Revolution
#% macro, RRES   , Readback Step Size (EGU)
#% macro, TWV    , Tweak Step Size (EGU)
#% macro, ERES   , Encoder Step Size (EGU)
#% macro, JAR    , Jog Acceleration (EGU/s^2)
#% macro, UEIP   , Use Encoder If Present
#% macro, URIP   , Use RDBL If Present
#% macro, RDBL   , Readback Location, set URIP = 1 if you specify this
#% macro, RLNK   , Readback output link
#% macro, RTRY   , Max retry count
#% macro, DLY    , Readback settle time (s)
#% macro, OFF    , User Offset (EGU)
#% macro, RDBD   , Retry Deadband (EGU)
#% macro, FOFF   , Freeze Offset, 0=variable, 1=frozen
#% macro, ADEL   , Alarm monitor deadband (EGU)
#% macro, NTM    , New Target Monitor, only set to 0 for soft motors
#% macro, FEHIGH , HIGH limit for following error
#% macro, FEHIHI , HIHI limit for following error
#% macro, FEHHSV , HIHI alarm severity for following error
#% macro, FEHSV  , HIGH alarm severity for following error
#% macro, SCALE  , Scale factor, if pmacSetAxisScale is used this should be set
#% macro, HOMEVIS, If 1 then home is visible on the gui
#% macro, HOMEVISSTR, If HOMEVIS=0, then display this text on the gui instead
#% macro, name   , Object name and gui association name
#% macro, alh    , Set this to alh to add the motor to the alarm handler and send emails, 
#                  set to '#' to comment out alh lines
#% macro, gda_name, Name to export this as to GDA
#% macro, gda_desc, Description to export this as to GDA

# AUTOSAVE: level 0 = before record init, level 1 = before AND after record init
#% autosave 0 DVAL OFF
#% autosave 1 DIR DHLM DLLM TWV VBAS VELO ACCL BDST BVEL BACC RDBD EGU RTRY UEIP URIP DLY PREC DISA DISP FOFF OFF FRAC OMSL JVEL JAR ADEL MDEL
#% archiver 0.5 Monitor
#% archiver 0.5 Monitor RBV
#% archiver 10 Monitor OFF
#% archiver 10 Monitor MRES
#% gdatag,template,simpleMotor,,BOD2 Y
#% gdatag,motor,rw,,RECORD,Motor
#% alh
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3795:Y:MOTOR

# This associates an edm screen with the template
# % gui, M4, edm, motor.edl, motor=BOD:UNDS:3795:Y:MOTOR
# % gui, M4, edmembed, motor-embed-small.edl, motor=BOD:UNDS:3795:Y:MOTOR,filename=motor.edl,box-label=BOD2 Y

# This associates a BOY screen with the template
# % gui, M4, boydetail, motorApp_opi/motor_detail.opi, P=BOD:UNDS:3795:,M=Y:MOTOR,DESC=BOD2 Y, name=M4
# % gui, M4, boyembed, motorApp_opi/motor_embed_box.opi, P=BOD:UNDS:3795:,M=Y:MOTOR,DESC=BOD2 Y, name=M4
# % gui, M4, boyembed, motorApp_opi/motor_embed.opi, P=BOD:UNDS:3795:,M=Y:MOTOR,DESC=BOD2 Y, name=M4
# FIXME: this should be in pmacUtil or tpmac
# % gui, M4, boyembed, motorApp_opi/motor_homed_embed.opi, P=BOD:UNDS:3795:,M=Y:MOTOR

# This makes the component icon reflect the status and severity
# % gui, M4, status, <pv>.MOVN
# % gui, M4, sevr

# These define what PVs a motor detail screen should contain
# % gui, M4, statusbits, Status,  <pv>.MSTA, Direction Positive, Done, High Limit, Home Limit, Unused, Closed Loop, Following Error, At Home, Encoder Present, Problem, Moving, Gain Support, Comms Error, Low Limit, Homed
# % gui, M4, statusbits, ELoss,  <pv>:ELOSS, Encoder Loss, Amplifier Loss, Sys Fail
# % gui, M4, command, ELoss Clear,  <pv>:ELOSSRC.A
# % gui, M4, statusbits, Limit Violation,  <pv>.LVIO, Lim
# % gui, M4, statusbits, At High Limit,  <pv>.HLS, High
# % gui, M4, statusbits, At Low Limit,  <pv>.LLS, Low
# % gui, M4, demand, User High Limit,  <pv>.HLM
# % gui, M4, demand, User Low Limit,  <pv>.LLM
# % gui, M4, demand, Dial High Limit, <pv>.DHLM
# % gui, M4, demand, Dial Low Limit, <pv>.DLLM

# % gui, M4, demand, Motor demand,  <pv>.VAL
# % gui, M4, readback, Motor readback,  <pv>.RBV
# % gui, M4, command, Stop,  <pv>.STOP
# % gui, M4, command, Home Forward,  <pv>.HOMF
# % gui, M4, command, Home Reverse,  <pv>.HOMR
# % gui, M4, command, Jog Forward, <pv>.JOGF
# % gui, M4, command, Jog Reverse, <pv>.JOGR
# % gui, M4, command, Tweak Forward,  <pv>.TWF
# % gui, M4, command, Tweak Reverse,  <pv>.TWR
# % gui, M4, demand, Tweak Step,  <pv>.TWV
# % gui, M4, command, Kill,  <pv>:KILL.PROC, Kill
# % gui, M4, command, Sync VAL=RBV,  <pv>.SYNC

# % gui, M4, enum, Direction,   <pv>.DIR
# % gui, M4, demand, User Offset,  <pv>.OFF
# % gui, M4, enum, Set/Use,   <pv>.SET
# % gui, M4, enum, Offset,   <pv>.FOFF
# % gui, M4, enum, Use Encoder,   <pv>.UEIP

# % gui, M4, demand, Motor Step Size,  <pv>.MRES
# % gui, M4, readback, Steps per Rev,   <pv>.SREV
# % gui, M4, readback, EGUs per Rev,   <pv>.UREV
# % gui, M4, demand, Encoder Step Size,  <pv>.ERES
# % gui, M4, readback, Readback Step Size,   <pv>.RRES
# % gui, M4, readback, Use Encoder if Present,   <pv>.UEIP

# % gui, M4, demand, Max Velocity,  <pv>.VMAX
# % gui, M4, demand, Base Velocity,   <pv>.VBAS
# % gui, M4, demand, Velocity,  <pv>.VELO
# % gui, M4, demand, Secs to Velocity,  <pv>.ACCL
# % gui, M4, demand, JVEL,   <pv>.JVEL
# % gui, M4, demand, Jog Acceleration,   <pv>.JAR
# % gui, M4, demand, Backlash Distance,   <pv>.BDST
# % gui, M4, demand, Backlash Velocity,   <pv>.BVEL
# % gui, M4, demand, Backlash Secs to Vel,   <pv>.BACC
# % gui, M4, demand, Move Fraction,   <pv>.FRAC
# % gui, M4, demand, Retry Deadband,   <pv>.RDBD
# % gui, M4, demand, Max Retrys,   <pv>.RTRY

# % gui, M4, demand, PREC,   <pv>.PREC
# % gui, M4, demand, EGU,   <pv>.EGU

# % gui, M4, demand, Output Specification,   <pv>.OUT
# % gui, M4, readback, Readback Location,   <pv>.RDBL
# % gui, M4, readback, Desired Output Loc,   <pv>.DOL
# % gui, M4, readback, Output Mode Select,   <pv>.OMSL
# % gui, M4, readback, Readback Out Link,   <pv>.RLNK
# % gui, M4, demand, DMOV Input Link,   <pv>.DINP
# % gui, M4, demand, RMP Input Link,   <pv>.RINP
# % gui, M4, demand, Stop Out Link,   <pv>.STOO


record(motor,"BOD:UNDS:3795:Y:MOTOR")
{
	field(DESC,"BOD2 Y")
	field(DTYP,"asynMotor")
	field(DIR,"0")
	field(VELO,"0.05")
	field(VBAS,"0")
	field(ACCL,"0.5")
	field(BDST,"0")
	field(BVEL,"0")
	field(BACC,"")
	field(OUT,"@asyn($(PORT),4)")
	field(MRES,"50e-6")
	field(PREC,"4")
	field(EGU,"mm")
	field(DHLM,"50.0")
	field(DLLM,"0")
	field(HLM,"")
	field(LLM,"")	
	field(HLSV,"MAJOR")
	field(INIT,"")
	field(RTRY,"0")
	field(DLY,"0")	
	field(HVEL,"0")
	field(SREV, "1")
	field(RRES, "")
	field(TWV, "0.1")
	field(ERES, "50e-6")
	field(JVEL, "0.05")
	field(JAR, "")
	field(UEIP, "1")
	field(URIP, "0")	
	field(RDBL, "")		
	field(VMAX, "0.1")
	field(OFF, "0")
	field(RDBD, "")
	field(FOFF, "0")
	field(ADEL, "0")
	field(NTM, "1")
	field(SDIS, "BOD:UNDS:3795:Y:MOTOR:SDIS.VAL")	
	field(RLNK, "")
}

# record that holds the ADDR
record(ao, "BOD:UNDS:3795:Y:MOTOR:ADDR") {
    field(PINI, "YES")
    field(VAL, "4")
}

# record that holds the PORT
record(stringout, "BOD:UNDS:3795:Y:MOTOR:PORT") {
    field(PINI, "YES")
    field(VAL, "$(PORT)")
}

# record that holds the SCALE
record(ao, "BOD:UNDS:3795:Y:MOTOR:SCALE") {
    field(PINI, "YES")
    field(VAL, "20000")
}

# Write a 1 to this record over Channel Access to disable the motor record and prevent CA writes to any field
# including the demand field.
record(ai, "BOD:UNDS:3795:Y:MOTOR:USER:SDIS") {
    field(VAL, "0")
    field(FLNK, "BOD:UNDS:3795:Y:MOTOR:SDIS")
}

# Note: This record forms part of the interface to auto-homing, which uses the "A" field to disable the motor record
# when homing.
# record that will disable and re-enable record if any of its inputs are non-zero
record(calcout, "BOD:UNDS:3795:Y:MOTOR:SDIS") {
  field(DESC, "Disable on non-zero input")
  field(INPL, "BOD:UNDS:3795:Y:MOTOR:USER:SDIS")
  field(CALC, "(A|B|C|D|E|F|G|H|I|J|K|L)>0")
  field(OUT, "BOD:UNDS:3795:Y:MOTOR.DISP PP")
}

# dummy record that looks like a kill command
record(ao, "BOD:UNDS:3795:Y:MOTOR:KILL") {
    field(VAL, "1")
}

# dummy record that looks like eloss status
#% archiver 10 Monitor VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3795:Y:MOTOR:ELOSS
record(ai, "BOD:UNDS:3795:Y:MOTOR:ELOSS") {
    field(PINI, "YES")
    field(HIHI, "1")
    field(HHSV, "MAJOR")    
}

# dummy record that looks like an eloss reset
record(calcout, "BOD:UNDS:3795:Y:MOTOR:ELOSSRC") {
}

# check if SYNC has been pressed
record(calcout, "BOD:UNDS:3795:Y:MOTOR:CHECK_SYNC") {
  field(INPA, "BOD:UNDS:3795:Y:MOTOR.SYNC CP")  
  field(CALC, "A>0")
  field(OOPT, "When Non-zero")
  field(OUT, "BOD:UNDS:3795:Y:MOTOR:MR_MOVE_WRITE.PROC PP")
}

# dummy record that looks like a record to notify a CS about a raw motor move
record(longout, "BOD:UNDS:3795:Y:MOTOR:MR_MOVE_WRITE") {
}

#Record to calculate the following error
#% archiver 1 Monitor
record(calc, "BOD:UNDS:3795:Y:MOTOR:FERROR")
{
	field(DESC,"Following Error")
	field(INPA,"BOD:UNDS:3795:Y:MOTOR.RMP CP")
	field(INPB,"BOD:UNDS:3795:Y:MOTOR.REP NPP")
	field(INPC,"BOD:UNDS:3795:Y:MOTOR.MRES NPP")
	field(INPD,"BOD:UNDS:3795:Y:MOTOR.ERES NPP")
	field(CALC,"ABS((A*C)-(B*D))")
	field(FLNK,"BOD:UNDS:3795:Y:MOTOR:FERRORMAX")
	field(PREC,"4")
	field(EGU, "mm")
}

#Record to store the maximum following error
#% archiver 10 Monitor VAL
#% autosave 1 VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3795:Y:MOTOR:FERRORMAX
record(calc, "BOD:UNDS:3795:Y:MOTOR:FERRORMAX")
{
	field(DESC,"Following Error Max")
	field(INPA,"BOD:UNDS:3795:Y:MOTOR:FERROR.VAL")
	field(INPB,"BOD:UNDS:3795:Y:MOTOR:FERRORMAX.VAL")
	field(CALC,"(A>B)?A:B")
	field(HIGH,"0")
	field(HIHI,"0")
	field(HHSV,"NO_ALARM")
	field(HSV, "NO_ALARM")
	field(PREC,"4")
	field(EGU, "mm")
}	

#Record to reset the maximum following error
record(bo, "BOD:UNDS:3795:Y:MOTOR:FEMAXRESET")
{
	field(DESC,"Reset max following error")
	field(DTYP,"Soft Channel")
	field(OUT, "BOD:UNDS:3795:Y:MOTOR:FERRORMAX.VAL")
	field(VAL, "0")
}	


#Record to determin HOME button visibility on the motor.edl screen.
record(bo, "BOD:UNDS:3795:Y:MOTOR:HOMEVIS")
{
	field(DESC,"Home button visibility")
	field(DTYP,"Soft Channel")
	field(VAL, "1")
        field(ZNAM, "Invisible")
	field(ONAM, "Visible")
}	 

#Record to display a message if the HOME buttons are not visible (if HOMEVIS=0).
record(stringout, "BOD:UNDS:3795:Y:MOTOR:HOMEVISSTR")
{
	field(DESC, "Home visibility string")
	field(DTYP,"Soft Channel")
	field(VAL, "")
}


# numbered name aliases for all PVs in basic_asyn_motor.template
alias(BOD:UNDS:3795:Y:MOTOR, $(MOC):PMAC:M4)
alias(BOD:UNDS:3795:Y:MOTOR:ADDR, $(MOC):PMAC:M4:ADDR)
alias(BOD:UNDS:3795:Y:MOTOR:PORT, $(MOC):PMAC:M4:PORT)
alias(BOD:UNDS:3795:Y:MOTOR:SCALE, $(MOC):PMAC:M4:SCALE)
alias(BOD:UNDS:3795:Y:MOTOR:USER:SDIS, $(MOC):PMACM4:USER:SDIS)
alias(BOD:UNDS:3795:Y:MOTOR:SDIS, $(MOC):PMAC:M4:SDIS)
alias(BOD:UNDS:3795:Y:MOTOR:KILL, $(MOC):PMAC:M4:KILL)
alias(BOD:UNDS:3795:Y:MOTOR:ELOSS, $(MOC):PMAC:M4:ELOSS)
alias(BOD:UNDS:3795:Y:MOTOR:ELOSSRC, $(MOC):PMAC:M4:ELOSSRC)
alias(BOD:UNDS:3795:Y:MOTOR:MR_MOVE_WRITE, $(MOC):PMAC:M4:MR_MOVE_WRITE)
alias(BOD:UNDS:3795:Y:MOTOR:CHECK_SYNC, $(MOC):PMAC:M4:CHECK_SYNC)
alias(BOD:UNDS:3795:Y:MOTOR:FERROR, $(MOC):PMAC:M4:FERROR)
alias(BOD:UNDS:3795:Y:MOTOR:FERRORMAX, $(MOC):PMAC:M4:FERRORMAX)
alias(BOD:UNDS:3795:Y:MOTOR:FEMAXRESET, $(MOC):PMAC:M4:FEMAXRESET)
alias(BOD:UNDS:3795:Y:MOTOR:HOMEVIS, $(MOC):PMAC:M4:HOMEVIS)
alias(BOD:UNDS:3795:Y:MOTOR:HOMEVISSTR, $(MOC):PMAC:M4:HOMEVISSTR)


############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "BOD:UNDS:3795:Y:MOTOR:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "BOD:UNDS:3795:Y:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3795:Y:MOTOR.DIR CP")
  field(INPC, "BOD:UNDS:3795:Y:MOTOR:SCALE")
  field(OUT, "BOD:UNDS:3795:Y:MOTOR:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "BOD:UNDS:3795:Y:MOTOR:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+4`")
  field(FLNK, "BOD:UNDS:3795:Y:MOTOR:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "BOD:UNDS:3795:Y:MOTOR:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "BOD:UNDS:3795:Y:MOTOR.OFF CP")
  field(OUT, "BOD:UNDS:3795:Y:MOTOR:OFF_SET PP")
}

record(ao, "BOD:UNDS:3795:Y:MOTOR:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+4`")
  field(FLNK, "BOD:UNDS:3795:Y:MOTOR:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "BOD:UNDS:3795:Y:MOTOR:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),4)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "BOD:UNDS:3795:Y:MOTOR:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),4)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "BOD:UNDS:3795:Y:MOTOR:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),4)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "BOD:UNDS:3795:Y:MOTOR:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),4)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "BOD:UNDS:3795:Y:MOTOR:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),4)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to BOD:UNDS:3795:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "BOD:UNDS:3795:Y:MOTOR:SDIS") {
#  field(INPA, "BOD:UNDS:3795::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "BOD:UNDS:3795::HM:HOMING") {
}

# Monitor homing status
record(bi, "BOD:UNDS:3795:Y:MOTOR:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "BOD:UNDS:3795::HM:HOMING CP")
  field(FLNK, "BOD:UNDS:3795:Y:MOTOR:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, BOD:UNDS:3795:Y:MOTOR:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "BOD:UNDS:3795:Y:MOTOR:HOMING")
    field(CALC, "A")
    field(OUT, "BOD:UNDS:3795:Y:MOTOR:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, BOD:UNDS:3795:Y:MOTOR:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),4)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "BOD:UNDS:3795:Y:MOTOR:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#4P")
  field(DESC, "BOD2 Y")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "BOD:UNDS:3795:Y:MOTOR:HMPOS:CALC") {
  field(INPA, "BOD:UNDS:3795:Y:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3795:Y:MOTOR:SCALE")  
  field(INPC, "BOD:UNDS:3795:Y:MOTOR:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "BOD:UNDS:3795:Y:MOTOR:HMPOS PP")
}

record(ai, "BOD:UNDS:3795:Y:MOTOR:HMPOS") {
  field(DESC, "BOD2 Y")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "BOD:UNDS:3795:Y:MOTOR:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "BOD:UNDS:3795:Y:MOTOR")
  field(OUT, "BOD:UNDS:3795::HM:M4PV PP")
}

record(stringout, "BOD:UNDS:3795::HM:M4PV"){
}

# define a kill record
record(ao, "BOD:UNDS:3795:Y:MOTOR:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),4)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "BOD:UNDS:3795:Y:MOTOR:ELOSSVAR") {
  field(CALC, "464+4")
  field(PINI, "YES")
}

record(ai, "BOD:UNDS:3795:Y:MOTOR:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+4`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "BOD:UNDS:3795:Y:MOTOR:ELOSSRC") {
  field(OUT, "BOD:UNDS:3795:Y:MOTOR:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "BOD:UNDS:3795:Y:MOTOR:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+4`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS4:DESC"){
  field(VAL, "M4 BOD2 Y")
}

#record(bo, "BOD:UNDS:3795:Y:MOTOR:HOMED") {
#  field(DESC, "Set homed status for axis 4")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "BOD:UNDS:3795:Y:MOTOR:HOMEDSET")
#}

#record(calcout, "BOD:UNDS:3795:Y:MOTOR:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "BOD:UNDS:3795:Y:MOTOR:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m445) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "BOD:UNDS:3795:Y:MOTOR:DirectDemand")
{
    field(FLNK, "BOD:UNDS:3795:Y:MOTOR:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "BOD:UNDS:3795:Y:MOTOR:DirectCountsCalc")
{
    field(INPA, "BOD:UNDS:3795:Y:MOTOR:DirectDemand")
    field(INPB, "BOD:UNDS:3795:Y:MOTOR:Mres PP")
    field(INPC, "BOD:UNDS:3795:Y:MOTOR:Offset PP")
    field(INPD, "BOD:UNDS:3795:Y:MOTOR:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "BOD:UNDS:3795:Y:MOTOR:RawDirectDemand PP")
}

record(ao, "BOD:UNDS:3795:Y:MOTOR:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "BOD:UNDS:3795:Y:MOTOR:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),4,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD2 Y")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "BOD:UNDS:3795:Y:MOTOR:DirectMotorSeq")
{
    field(LNK1, "BOD:UNDS:3795:Y:MOTOR:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "BOD:UNDS:3795:Y:MOTOR:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),4,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "BOD:UNDS:3795:Y:MOTOR:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),4,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "BOD:UNDS:3795:Y:MOTOR:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),4,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "BOD:UNDS:3795:Y:MOTOR:MresSource")
}

record(scalcout, BOD:UNDS:3795:Y:MOTOR:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3795:Y:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "BOD:UNDS:3795:Y:MOTOR:Mres.INPA CA")
    field(FLNK, "BOD:UNDS:3795:Y:MOTOR:OffsetSource")
}

record(scalcout, "BOD:UNDS:3795:Y:MOTOR:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3795:Y:MOTOR:MresDrv PP")
    field(PREC, 6)
}

record(ao,"BOD:UNDS:3795:Y:MOTOR:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),4,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, BOD:UNDS:3795:Y:MOTOR:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3795:Y:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "BOD:UNDS:3795:Y:MOTOR:Offset.INPA CA")
}


record(scalcout, BOD:UNDS:3795:Y:MOTOR:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3795:Y:MOTOR:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"BOD:UNDS:3795:Y:MOTOR:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),4,4)PMAC_OFFSET")
}

# add numbered name aliases for above 3 templates
############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "$(MOC):PMAC:M4:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "$(MOC):PMAC:M4.MRES CP")
  field(INPB, "$(MOC):PMAC:M4.DIR CP")
  field(INPC, "$(MOC):PMAC:M4:SCALE")
  field(OUT, "$(MOC):PMAC:M4:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "$(MOC):PMAC:M4:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+4`")
  field(FLNK, "$(MOC):PMAC:M4:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "$(MOC):PMAC:M4:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "$(MOC):PMAC:M4.OFF CP")
  field(OUT, "$(MOC):PMAC:M4:OFF_SET PP")
}

record(ao, "$(MOC):PMAC:M4:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+4`")
  field(FLNK, "$(MOC):PMAC:M4:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "$(MOC):PMAC:M4:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),4)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "$(MOC):PMAC:M4:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),4)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "$(MOC):PMAC:M4:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),4)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "$(MOC):PMAC:M4:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),4)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "$(MOC):PMAC:M4:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),4)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to $(MOC):PMAC:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "$(MOC):PMAC:M4:SDIS") {
#  field(INPA, "$(MOC):PMAC::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "$(MOC):PMAC::HM:HOMING") {
}

# Monitor homing status
record(bi, "$(MOC):PMAC:M4:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "$(MOC):PMAC::HM:HOMING CP")
  field(FLNK, "$(MOC):PMAC:M4:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, $(MOC):PMAC:M4:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "$(MOC):PMAC:M4:HOMING")
    field(CALC, "A")
    field(OUT, "$(MOC):PMAC:M4:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, $(MOC):PMAC:M4:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),4)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "$(MOC):PMAC:M4:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#4P")
  field(DESC, "BOD2 Y")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "$(MOC):PMAC:M4:HMPOS:CALC") {
  field(INPA, "$(MOC):PMAC:M4.MRES CP")
  field(INPB, "$(MOC):PMAC:M4:SCALE")  
  field(INPC, "$(MOC):PMAC:M4:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "$(MOC):PMAC:M4:HMPOS PP")
}

record(ai, "$(MOC):PMAC:M4:HMPOS") {
  field(DESC, "BOD2 Y")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "$(MOC):PMAC:M4:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "$(MOC):PMAC:M4")
  field(OUT, "$(MOC):PMAC::HM:M4PV PP")
}

record(stringout, "$(MOC):PMAC::HM:M4PV"){
}

# define a kill record
record(ao, "$(MOC):PMAC:M4:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),4)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "$(MOC):PMAC:M4:ELOSSVAR") {
  field(CALC, "464+4")
  field(PINI, "YES")
}

record(ai, "$(MOC):PMAC:M4:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+4`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "$(MOC):PMAC:M4:ELOSSRC") {
  field(OUT, "$(MOC):PMAC:M4:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "$(MOC):PMAC:M4:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+4`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS4:DESC"){
  field(VAL, "M4 BOD2 Y")
}

#record(bo, "$(MOC):PMAC:M4:HOMED") {
#  field(DESC, "Set homed status for axis 4")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "$(MOC):PMAC:M4:HOMEDSET")
#}

#record(calcout, "$(MOC):PMAC:M4:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "$(MOC):PMAC:M4:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m445) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "$(MOC):PMAC:M4:DirectDemand")
{
    field(FLNK, "$(MOC):PMAC:M4:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "$(MOC):PMAC:M4:DirectCountsCalc")
{
    field(INPA, "$(MOC):PMAC:M4:DirectDemand")
    field(INPB, "$(MOC):PMAC:M4:Mres PP")
    field(INPC, "$(MOC):PMAC:M4:Offset PP")
    field(INPD, "$(MOC):PMAC:M4:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "$(MOC):PMAC:M4:RawDirectDemand PP")
}

record(ao, "$(MOC):PMAC:M4:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "$(MOC):PMAC:M4:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),4,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD2 Y")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "$(MOC):PMAC:M4:DirectMotorSeq")
{
    field(LNK1, "$(MOC):PMAC:M4:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "$(MOC):PMAC:M4:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),4,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "$(MOC):PMAC:M4:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),4,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "$(MOC):PMAC:M4:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),4,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "$(MOC):PMAC:M4:MresSource")
}

record(scalcout, $(MOC):PMAC:M4:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M4:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "$(MOC):PMAC:M4:Mres.INPA CA")
    field(FLNK, "$(MOC):PMAC:M4:OffsetSource")
}

record(scalcout, "$(MOC):PMAC:M4:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M4:MresDrv PP")
    field(PREC, 6)
}

record(ao,"$(MOC):PMAC:M4:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),4,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, $(MOC):PMAC:M4:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M4:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "$(MOC):PMAC:M4:Offset.INPA CA")
}


record(scalcout, $(MOC):PMAC:M4:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M4:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"$(MOC):PMAC:M4:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),4,4)PMAC_OFFSET")
}# % macro, PMAC, The P prefix for the motor controller (must match P in pmacController.template)

# this template defines a motor with two names:-
# (1) root is BOD:UNDS:3795:X:MOTOR, the macros fully define the name of the motor
# (2) 'numbered' names of the form $(MOC):PMAC:Mx where x is the motor number
# names are both assigned to the same PV via aliases in the case basic_asyn_motor
# or simply duplicated for all other included templates, this approach works because
# the PVs all point to the same ASYN parameters and improves maintainability of this
# file

# include motor record from the motor module
#% macro, __doc__, Basic template, including motor record and associated tags
#% macro, P      , Device Prefix
#% macro, M      , Device Suffix
#% macro, PORT   , Asyn port for motor record
#% macro, ADDR   , Address on controller
#% macro, DESC   , Description, displayed on EDM screen
#% macro, MRES   , Motor Step Size (EGU)
#% macro, DTYP   , DTYP of record
#% macro, DIR    , User Direction
#% macro, VBAS   , Base Velocity (EGU/s)
#% macro, VELO   , Velocity (EGU/s)
#% macro, VMAX   , Max Velocity (EGU/s), defaults to VELO
#% macro, ACCL   , Seconds to Velocity
#% macro, BDST   , BL Distance (EGU)
#% macro, BVEL   , BL Velocity (EGU/s)
#% macro, BACC   , BL Seconds to Veloc.
#% macro, PREC   , Display Precision
#% macro, EGU    , Engineering Units
#% macro, DHLM   , Dial High Limit
#% macro, DLLM   , Dial Low Limit
#% macro, HLM    , User High Limit
#% macro, LLM    , User Low Limit
#% macro, HLSV   , HW Lim. Violation Svr
#% macro, INIT   , Startup commands
#% macro, SREV   , Steps per Revolution
#% macro, RRES   , Readback Step Size (EGU)
#% macro, TWV    , Tweak Step Size (EGU)
#% macro, ERES   , Encoder Step Size (EGU)
#% macro, JAR    , Jog Acceleration (EGU/s^2)
#% macro, UEIP   , Use Encoder If Present
#% macro, URIP   , Use RDBL If Present
#% macro, RDBL   , Readback Location, set URIP = 1 if you specify this
#% macro, RLNK   , Readback output link
#% macro, RTRY   , Max retry count
#% macro, DLY    , Readback settle time (s)
#% macro, OFF    , User Offset (EGU)
#% macro, RDBD   , Retry Deadband (EGU)
#% macro, FOFF   , Freeze Offset, 0=variable, 1=frozen
#% macro, ADEL   , Alarm monitor deadband (EGU)
#% macro, NTM    , New Target Monitor, only set to 0 for soft motors
#% macro, FEHIGH , HIGH limit for following error
#% macro, FEHIHI , HIHI limit for following error
#% macro, FEHHSV , HIHI alarm severity for following error
#% macro, FEHSV  , HIGH alarm severity for following error
#% macro, SCALE  , Scale factor, if pmacSetAxisScale is used this should be set
#% macro, HOMEVIS, If 1 then home is visible on the gui
#% macro, HOMEVISSTR, If HOMEVIS=0, then display this text on the gui instead
#% macro, name   , Object name and gui association name
#% macro, alh    , Set this to alh to add the motor to the alarm handler and send emails, 
#                  set to '#' to comment out alh lines
#% macro, gda_name, Name to export this as to GDA
#% macro, gda_desc, Description to export this as to GDA

# AUTOSAVE: level 0 = before record init, level 1 = before AND after record init
#% autosave 0 DVAL OFF
#% autosave 1 DIR DHLM DLLM TWV VBAS VELO ACCL BDST BVEL BACC RDBD EGU RTRY UEIP URIP DLY PREC DISA DISP FOFF OFF FRAC OMSL JVEL JAR ADEL MDEL
#% archiver 0.5 Monitor
#% archiver 0.5 Monitor RBV
#% archiver 10 Monitor OFF
#% archiver 10 Monitor MRES
#% gdatag,template,simpleMotor,,BOD2 X
#% gdatag,motor,rw,,RECORD,Motor
#% alh
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3795:X:MOTOR

# This associates an edm screen with the template
# % gui, M5, edm, motor.edl, motor=BOD:UNDS:3795:X:MOTOR
# % gui, M5, edmembed, motor-embed-small.edl, motor=BOD:UNDS:3795:X:MOTOR,filename=motor.edl,box-label=BOD2 X

# This associates a BOY screen with the template
# % gui, M5, boydetail, motorApp_opi/motor_detail.opi, P=BOD:UNDS:3795:,M=X:MOTOR,DESC=BOD2 X, name=M5
# % gui, M5, boyembed, motorApp_opi/motor_embed_box.opi, P=BOD:UNDS:3795:,M=X:MOTOR,DESC=BOD2 X, name=M5
# % gui, M5, boyembed, motorApp_opi/motor_embed.opi, P=BOD:UNDS:3795:,M=X:MOTOR,DESC=BOD2 X, name=M5
# FIXME: this should be in pmacUtil or tpmac
# % gui, M5, boyembed, motorApp_opi/motor_homed_embed.opi, P=BOD:UNDS:3795:,M=X:MOTOR

# This makes the component icon reflect the status and severity
# % gui, M5, status, <pv>.MOVN
# % gui, M5, sevr

# These define what PVs a motor detail screen should contain
# % gui, M5, statusbits, Status,  <pv>.MSTA, Direction Positive, Done, High Limit, Home Limit, Unused, Closed Loop, Following Error, At Home, Encoder Present, Problem, Moving, Gain Support, Comms Error, Low Limit, Homed
# % gui, M5, statusbits, ELoss,  <pv>:ELOSS, Encoder Loss, Amplifier Loss, Sys Fail
# % gui, M5, command, ELoss Clear,  <pv>:ELOSSRC.A
# % gui, M5, statusbits, Limit Violation,  <pv>.LVIO, Lim
# % gui, M5, statusbits, At High Limit,  <pv>.HLS, High
# % gui, M5, statusbits, At Low Limit,  <pv>.LLS, Low
# % gui, M5, demand, User High Limit,  <pv>.HLM
# % gui, M5, demand, User Low Limit,  <pv>.LLM
# % gui, M5, demand, Dial High Limit, <pv>.DHLM
# % gui, M5, demand, Dial Low Limit, <pv>.DLLM

# % gui, M5, demand, Motor demand,  <pv>.VAL
# % gui, M5, readback, Motor readback,  <pv>.RBV
# % gui, M5, command, Stop,  <pv>.STOP
# % gui, M5, command, Home Forward,  <pv>.HOMF
# % gui, M5, command, Home Reverse,  <pv>.HOMR
# % gui, M5, command, Jog Forward, <pv>.JOGF
# % gui, M5, command, Jog Reverse, <pv>.JOGR
# % gui, M5, command, Tweak Forward,  <pv>.TWF
# % gui, M5, command, Tweak Reverse,  <pv>.TWR
# % gui, M5, demand, Tweak Step,  <pv>.TWV
# % gui, M5, command, Kill,  <pv>:KILL.PROC, Kill
# % gui, M5, command, Sync VAL=RBV,  <pv>.SYNC

# % gui, M5, enum, Direction,   <pv>.DIR
# % gui, M5, demand, User Offset,  <pv>.OFF
# % gui, M5, enum, Set/Use,   <pv>.SET
# % gui, M5, enum, Offset,   <pv>.FOFF
# % gui, M5, enum, Use Encoder,   <pv>.UEIP

# % gui, M5, demand, Motor Step Size,  <pv>.MRES
# % gui, M5, readback, Steps per Rev,   <pv>.SREV
# % gui, M5, readback, EGUs per Rev,   <pv>.UREV
# % gui, M5, demand, Encoder Step Size,  <pv>.ERES
# % gui, M5, readback, Readback Step Size,   <pv>.RRES
# % gui, M5, readback, Use Encoder if Present,   <pv>.UEIP

# % gui, M5, demand, Max Velocity,  <pv>.VMAX
# % gui, M5, demand, Base Velocity,   <pv>.VBAS
# % gui, M5, demand, Velocity,  <pv>.VELO
# % gui, M5, demand, Secs to Velocity,  <pv>.ACCL
# % gui, M5, demand, JVEL,   <pv>.JVEL
# % gui, M5, demand, Jog Acceleration,   <pv>.JAR
# % gui, M5, demand, Backlash Distance,   <pv>.BDST
# % gui, M5, demand, Backlash Velocity,   <pv>.BVEL
# % gui, M5, demand, Backlash Secs to Vel,   <pv>.BACC
# % gui, M5, demand, Move Fraction,   <pv>.FRAC
# % gui, M5, demand, Retry Deadband,   <pv>.RDBD
# % gui, M5, demand, Max Retrys,   <pv>.RTRY

# % gui, M5, demand, PREC,   <pv>.PREC
# % gui, M5, demand, EGU,   <pv>.EGU

# % gui, M5, demand, Output Specification,   <pv>.OUT
# % gui, M5, readback, Readback Location,   <pv>.RDBL
# % gui, M5, readback, Desired Output Loc,   <pv>.DOL
# % gui, M5, readback, Output Mode Select,   <pv>.OMSL
# % gui, M5, readback, Readback Out Link,   <pv>.RLNK
# % gui, M5, demand, DMOV Input Link,   <pv>.DINP
# % gui, M5, demand, RMP Input Link,   <pv>.RINP
# % gui, M5, demand, Stop Out Link,   <pv>.STOO


record(motor,"BOD:UNDS:3795:X:MOTOR")
{
	field(DESC,"BOD2 X")
	field(DTYP,"asynMotor")
	field(DIR,"0")
	field(VELO,"0.1")
	field(VBAS,"0")
	field(ACCL,"0.5")
	field(BDST,"0")
	field(BVEL,"0")
	field(BACC,"")
	field(OUT,"@asyn($(PORT),5)")
	field(MRES,"50e-6")
	field(PREC,"4")
	field(EGU,"mm")
	field(DHLM,"70.0")
	field(DLLM,"0")
	field(HLM,"")
	field(LLM,"")	
	field(HLSV,"NO_ALARM")
	field(INIT,"")
	field(RTRY,"0")
	field(DLY,"0")	
	field(HVEL,"0")
	field(SREV, "1")
	field(RRES, "")
	field(TWV, "0.1")
	field(ERES, "50e-6")
	field(JVEL, "0.1")
	field(JAR, "")
	field(UEIP, "1")
	field(URIP, "0")	
	field(RDBL, "")		
	field(VMAX, "0.1")
	field(OFF, "0")
	field(RDBD, "")
	field(FOFF, "0")
	field(ADEL, "0")
	field(NTM, "1")
	field(SDIS, "BOD:UNDS:3795:X:MOTOR:SDIS.VAL")	
	field(RLNK, "")
}

# record that holds the ADDR
record(ao, "BOD:UNDS:3795:X:MOTOR:ADDR") {
    field(PINI, "YES")
    field(VAL, "5")
}

# record that holds the PORT
record(stringout, "BOD:UNDS:3795:X:MOTOR:PORT") {
    field(PINI, "YES")
    field(VAL, "$(PORT)")
}

# record that holds the SCALE
record(ao, "BOD:UNDS:3795:X:MOTOR:SCALE") {
    field(PINI, "YES")
    field(VAL, "20000")
}

# Write a 1 to this record over Channel Access to disable the motor record and prevent CA writes to any field
# including the demand field.
record(ai, "BOD:UNDS:3795:X:MOTOR:USER:SDIS") {
    field(VAL, "0")
    field(FLNK, "BOD:UNDS:3795:X:MOTOR:SDIS")
}

# Note: This record forms part of the interface to auto-homing, which uses the "A" field to disable the motor record
# when homing.
# record that will disable and re-enable record if any of its inputs are non-zero
record(calcout, "BOD:UNDS:3795:X:MOTOR:SDIS") {
  field(DESC, "Disable on non-zero input")
  field(INPL, "BOD:UNDS:3795:X:MOTOR:USER:SDIS")
  field(CALC, "(A|B|C|D|E|F|G|H|I|J|K|L)>0")
  field(OUT, "BOD:UNDS:3795:X:MOTOR.DISP PP")
}

# dummy record that looks like a kill command
record(ao, "BOD:UNDS:3795:X:MOTOR:KILL") {
    field(VAL, "1")
}

# dummy record that looks like eloss status
#% archiver 10 Monitor VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3795:X:MOTOR:ELOSS
record(ai, "BOD:UNDS:3795:X:MOTOR:ELOSS") {
    field(PINI, "YES")
    field(HIHI, "1")
    field(HHSV, "MAJOR")    
}

# dummy record that looks like an eloss reset
record(calcout, "BOD:UNDS:3795:X:MOTOR:ELOSSRC") {
}

# check if SYNC has been pressed
record(calcout, "BOD:UNDS:3795:X:MOTOR:CHECK_SYNC") {
  field(INPA, "BOD:UNDS:3795:X:MOTOR.SYNC CP")  
  field(CALC, "A>0")
  field(OOPT, "When Non-zero")
  field(OUT, "BOD:UNDS:3795:X:MOTOR:MR_MOVE_WRITE.PROC PP")
}

# dummy record that looks like a record to notify a CS about a raw motor move
record(longout, "BOD:UNDS:3795:X:MOTOR:MR_MOVE_WRITE") {
}

#Record to calculate the following error
#% archiver 1 Monitor
record(calc, "BOD:UNDS:3795:X:MOTOR:FERROR")
{
	field(DESC,"Following Error")
	field(INPA,"BOD:UNDS:3795:X:MOTOR.RMP CP")
	field(INPB,"BOD:UNDS:3795:X:MOTOR.REP NPP")
	field(INPC,"BOD:UNDS:3795:X:MOTOR.MRES NPP")
	field(INPD,"BOD:UNDS:3795:X:MOTOR.ERES NPP")
	field(CALC,"ABS((A*C)-(B*D))")
	field(FLNK,"BOD:UNDS:3795:X:MOTOR:FERRORMAX")
	field(PREC,"4")
	field(EGU, "mm")
}

#Record to store the maximum following error
#% archiver 10 Monitor VAL
#% autosave 1 VAL
#% None $SEVRCOMMAND UP_ANY dls-alh-handler.py BOD:UNDS:3795:X:MOTOR:FERRORMAX
record(calc, "BOD:UNDS:3795:X:MOTOR:FERRORMAX")
{
	field(DESC,"Following Error Max")
	field(INPA,"BOD:UNDS:3795:X:MOTOR:FERROR.VAL")
	field(INPB,"BOD:UNDS:3795:X:MOTOR:FERRORMAX.VAL")
	field(CALC,"(A>B)?A:B")
	field(HIGH,"0")
	field(HIHI,"0")
	field(HHSV,"NO_ALARM")
	field(HSV, "NO_ALARM")
	field(PREC,"4")
	field(EGU, "mm")
}	

#Record to reset the maximum following error
record(bo, "BOD:UNDS:3795:X:MOTOR:FEMAXRESET")
{
	field(DESC,"Reset max following error")
	field(DTYP,"Soft Channel")
	field(OUT, "BOD:UNDS:3795:X:MOTOR:FERRORMAX.VAL")
	field(VAL, "0")
}	


#Record to determin HOME button visibility on the motor.edl screen.
record(bo, "BOD:UNDS:3795:X:MOTOR:HOMEVIS")
{
	field(DESC,"Home button visibility")
	field(DTYP,"Soft Channel")
	field(VAL, "1")
        field(ZNAM, "Invisible")
	field(ONAM, "Visible")
}	 

#Record to display a message if the HOME buttons are not visible (if HOMEVIS=0).
record(stringout, "BOD:UNDS:3795:X:MOTOR:HOMEVISSTR")
{
	field(DESC, "Home visibility string")
	field(DTYP,"Soft Channel")
	field(VAL, "")
}


# numbered name aliases for all PVs in basic_asyn_motor.template
alias(BOD:UNDS:3795:X:MOTOR, $(MOC):PMAC:M5)
alias(BOD:UNDS:3795:X:MOTOR:ADDR, $(MOC):PMAC:M5:ADDR)
alias(BOD:UNDS:3795:X:MOTOR:PORT, $(MOC):PMAC:M5:PORT)
alias(BOD:UNDS:3795:X:MOTOR:SCALE, $(MOC):PMAC:M5:SCALE)
alias(BOD:UNDS:3795:X:MOTOR:USER:SDIS, $(MOC):PMACM5:USER:SDIS)
alias(BOD:UNDS:3795:X:MOTOR:SDIS, $(MOC):PMAC:M5:SDIS)
alias(BOD:UNDS:3795:X:MOTOR:KILL, $(MOC):PMAC:M5:KILL)
alias(BOD:UNDS:3795:X:MOTOR:ELOSS, $(MOC):PMAC:M5:ELOSS)
alias(BOD:UNDS:3795:X:MOTOR:ELOSSRC, $(MOC):PMAC:M5:ELOSSRC)
alias(BOD:UNDS:3795:X:MOTOR:MR_MOVE_WRITE, $(MOC):PMAC:M5:MR_MOVE_WRITE)
alias(BOD:UNDS:3795:X:MOTOR:CHECK_SYNC, $(MOC):PMAC:M5:CHECK_SYNC)
alias(BOD:UNDS:3795:X:MOTOR:FERROR, $(MOC):PMAC:M5:FERROR)
alias(BOD:UNDS:3795:X:MOTOR:FERRORMAX, $(MOC):PMAC:M5:FERRORMAX)
alias(BOD:UNDS:3795:X:MOTOR:FEMAXRESET, $(MOC):PMAC:M5:FEMAXRESET)
alias(BOD:UNDS:3795:X:MOTOR:HOMEVIS, $(MOC):PMAC:M5:HOMEVIS)
alias(BOD:UNDS:3795:X:MOTOR:HOMEVISSTR, $(MOC):PMAC:M5:HOMEVISSTR)


############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "BOD:UNDS:3795:X:MOTOR:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "BOD:UNDS:3795:X:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3795:X:MOTOR.DIR CP")
  field(INPC, "BOD:UNDS:3795:X:MOTOR:SCALE")
  field(OUT, "BOD:UNDS:3795:X:MOTOR:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "BOD:UNDS:3795:X:MOTOR:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+5`")
  field(FLNK, "BOD:UNDS:3795:X:MOTOR:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "BOD:UNDS:3795:X:MOTOR:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "BOD:UNDS:3795:X:MOTOR.OFF CP")
  field(OUT, "BOD:UNDS:3795:X:MOTOR:OFF_SET PP")
}

record(ao, "BOD:UNDS:3795:X:MOTOR:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+5`")
  field(FLNK, "BOD:UNDS:3795:X:MOTOR:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "BOD:UNDS:3795:X:MOTOR:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),5)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "BOD:UNDS:3795:X:MOTOR:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),5)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "BOD:UNDS:3795:X:MOTOR:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),5)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "BOD:UNDS:3795:X:MOTOR:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),5)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "BOD:UNDS:3795:X:MOTOR:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),5)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to BOD:UNDS:3795:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "BOD:UNDS:3795:X:MOTOR:SDIS") {
#  field(INPA, "BOD:UNDS:3795::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "BOD:UNDS:3795::HM:HOMING") {
}

# Monitor homing status
record(bi, "BOD:UNDS:3795:X:MOTOR:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "BOD:UNDS:3795::HM:HOMING CP")
  field(FLNK, "BOD:UNDS:3795:X:MOTOR:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, BOD:UNDS:3795:X:MOTOR:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "BOD:UNDS:3795:X:MOTOR:HOMING")
    field(CALC, "A")
    field(OUT, "BOD:UNDS:3795:X:MOTOR:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, BOD:UNDS:3795:X:MOTOR:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),5)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "BOD:UNDS:3795:X:MOTOR:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#5P")
  field(DESC, "BOD2 X")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "BOD:UNDS:3795:X:MOTOR:HMPOS:CALC") {
  field(INPA, "BOD:UNDS:3795:X:MOTOR.MRES CP")
  field(INPB, "BOD:UNDS:3795:X:MOTOR:SCALE")  
  field(INPC, "BOD:UNDS:3795:X:MOTOR:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "BOD:UNDS:3795:X:MOTOR:HMPOS PP")
}

record(ai, "BOD:UNDS:3795:X:MOTOR:HMPOS") {
  field(DESC, "BOD2 X")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "BOD:UNDS:3795:X:MOTOR:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "BOD:UNDS:3795:X:MOTOR")
  field(OUT, "BOD:UNDS:3795::HM:M5PV PP")
}

record(stringout, "BOD:UNDS:3795::HM:M5PV"){
}

# define a kill record
record(ao, "BOD:UNDS:3795:X:MOTOR:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),5)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "BOD:UNDS:3795:X:MOTOR:ELOSSVAR") {
  field(CALC, "464+5")
  field(PINI, "YES")
}

record(ai, "BOD:UNDS:3795:X:MOTOR:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+5`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "BOD:UNDS:3795:X:MOTOR:ELOSSRC") {
  field(OUT, "BOD:UNDS:3795:X:MOTOR:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "BOD:UNDS:3795:X:MOTOR:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+5`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS5:DESC"){
  field(VAL, "M5 BOD2 X")
}

#record(bo, "BOD:UNDS:3795:X:MOTOR:HOMED") {
#  field(DESC, "Set homed status for axis 5")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "BOD:UNDS:3795:X:MOTOR:HOMEDSET")
#}

#record(calcout, "BOD:UNDS:3795:X:MOTOR:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "BOD:UNDS:3795:X:MOTOR:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m545) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "BOD:UNDS:3795:X:MOTOR:DirectDemand")
{
    field(FLNK, "BOD:UNDS:3795:X:MOTOR:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "BOD:UNDS:3795:X:MOTOR:DirectCountsCalc")
{
    field(INPA, "BOD:UNDS:3795:X:MOTOR:DirectDemand")
    field(INPB, "BOD:UNDS:3795:X:MOTOR:Mres PP")
    field(INPC, "BOD:UNDS:3795:X:MOTOR:Offset PP")
    field(INPD, "BOD:UNDS:3795:X:MOTOR:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "BOD:UNDS:3795:X:MOTOR:RawDirectDemand PP")
}

record(ao, "BOD:UNDS:3795:X:MOTOR:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "BOD:UNDS:3795:X:MOTOR:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),5,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD2 X")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "BOD:UNDS:3795:X:MOTOR:DirectMotorSeq")
{
    field(LNK1, "BOD:UNDS:3795:X:MOTOR:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "BOD:UNDS:3795:X:MOTOR:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),5,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "BOD:UNDS:3795:X:MOTOR:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),5,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "BOD:UNDS:3795:X:MOTOR:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),5,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "BOD:UNDS:3795:X:MOTOR:MresSource")
}

record(scalcout, BOD:UNDS:3795:X:MOTOR:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3795:X:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "BOD:UNDS:3795:X:MOTOR:Mres.INPA CA")
    field(FLNK, "BOD:UNDS:3795:X:MOTOR:OffsetSource")
}

record(scalcout, "BOD:UNDS:3795:X:MOTOR:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3795:X:MOTOR:MresDrv PP")
    field(PREC, 6)
}

record(ao,"BOD:UNDS:3795:X:MOTOR:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),5,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, BOD:UNDS:3795:X:MOTOR:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "BOD:UNDS:3795:X:MOTOR:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "BOD:UNDS:3795:X:MOTOR:Offset.INPA CA")
}


record(scalcout, BOD:UNDS:3795:X:MOTOR:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "BOD:UNDS:3795:X:MOTOR:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"BOD:UNDS:3795:X:MOTOR:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),5,4)PMAC_OFFSET")
}

# add numbered name aliases for above 3 templates
############################################################
#
# Template to allow coordinate system kinematics to know
# the resolution of each real motor
#
# Also to allow control of CS mappings of real motors
#
###############################################################


###############################################################
# Mres and Offset
###############################################################

#calc mres to be written
record(calcout, "$(MOC):PMAC:M5:MRES_CALC") {
  field(CALC, "B?-A*C:A*C")
  field(INPA, "$(MOC):PMAC:M5.MRES CP")
  field(INPB, "$(MOC):PMAC:M5.DIR CP")
  field(INPC, "$(MOC):PMAC:M5:SCALE")
  field(OUT, "$(MOC):PMAC:M5:MRES_SET PP")
  field(OOPT, "When Non-zero")
}

record(ao, "$(MOC):PMAC:M5:MRES_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4800+5`")
  field(FLNK, "$(MOC):PMAC:M5:MR_MOVE_WRITE")
}

#calc offset to be written
record(calcout, "$(MOC):PMAC:M5:OFF_SET_CALC") {
  field(CALC, "A")
  field(INPA, "$(MOC):PMAC:M5.OFF CP")
  field(OUT, "$(MOC):PMAC:M5:OFF_SET PP")
}

record(ao, "$(MOC):PMAC:M5:OFF_SET") {
  field(DTYP, "asynFloat64")
  field(OUT, "@asyn($(PORT),0)PMAC_WD_P`4900+5`")
  field(FLNK, "$(MOC):PMAC:M5:MR_MOVE_WRITE")
}


###############################################################
# Coordinate System mapping control
###############################################################

# Read back which CS number the axis is in
record(ai, "$(MOC):PMAC:M5:CsRaw_RBV")
{
    field(DTYP, "asynInt32")
    field(INP, "@asyn($(PORT),5)PMAC_C_AXIS_CS")
    field(PINI, "NO")
    field(SCAN, "I/O Intr")
}

# Set which CS the axis is in via CSController Port Name
record(mbbo, "$(MOC):PMAC:M5:CsPort") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),5)PMAC_C_GROUP_CS_PORT")
}

record(mbbi, "$(MOC):PMAC:M5:CsPort_RBV") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),5)PMAC_C_GROUP_CS_PORT_RBV")
  field(SCAN, "I/O Intr")
}

# set the axis' CS assignment (one of A B C U V W X Y Z I)
record(stringout, "$(MOC):PMAC:M5:CsAxis") {
  field(DTYP, "asynOctetWrite")
  field(OUT, "@asyn($(PORT),5)PMAC_C_GROUP_ASSIGN")
}

record(stringin, "$(MOC):PMAC:M5:CsAxis_RBV") {
  field(DTYP, "asynOctetRead")
  field(INP, "@asyn($(PORT),5)PMAC_C_GROUP_ASSIGN_RBV")
  field(SCAN, "I/O Intr")
}# These are the supporting records for doing kill, eloss, and adding autohome 
# support to a motor record

#% macro, SPORT, Asyn port for eloss commands 
# usually PMAC_S<n> for pmacs, the name of the pmacAsynIpPort for bricks
#% macro, HOME, Prefix for autohome instance. Defaults to $(MOC):PMAC:. If unspecified,
# the template contains enough records to keep the motor record happy
#% macro, PMAC, Prefix for pmacStatus instance. Needed to get axis descriptions
# onto pmacStatusAxis screen
#% macro, ALLOW_HOMED_SET, Set to a blank to allow this axis to have its homed
# status manually.  This is set to a comment character by default.

# disable motor when homing
record(calcout, "$(MOC):PMAC:M5:SDIS") {
#  field(INPA, "$(MOC):PMAC::HM:HOMING CP")
}

# dummy record in case we aren't homing
record(busy, "$(MOC):PMAC::HM:HOMING") {
}

# Monitor homing status
record(bi, "$(MOC):PMAC:M5:HOMING") {
  field(DESC, "Monitor homing status of autohome")
  field(INP, "$(MOC):PMAC::HM:HOMING CP")
  field(FLNK, "$(MOC):PMAC:M5:HOMING_CALC")
  field(ZNAM, "Done")
  field(ONAM, "Busy")
}

# report to the driver if this motor is being autohomed
record(calcout, $(MOC):PMAC:M5:HOMING_CALC)
{
    field(PINI, 1)
    field(INPA, "$(MOC):PMAC:M5:HOMING")
    field(CALC, "A")
    field(OUT, "$(MOC):PMAC:M5:HOMING_STATUS PP")
}

# report to the driver if this motor is being autohomed
record(ao, $(MOC):PMAC:M5:HOMING_STATUS)
{
    field(DESC, "driver update on autohome")
    field(DTYP, "asynInt32")
    field(OUT, "@asyn($(PORT),5)HOMING_STATUS")
}


# reports position of motor without offset
record(ai, "$(MOC):PMAC:M5:HMPOS_RBV") {
  field(DTYP, "asynFloat64")
  field(INP, "@asyn($(PORT),0)PMAC_VDF_#5P")
  field(DESC, "BOD2 X")
  field(SCAN, "I/O Intr")
  field(PINI, "YES")
  field(PREC, "4")
  field(EGU, "mm")
}

record(calcout, "$(MOC):PMAC:M5:HMPOS:CALC") {
  field(INPA, "$(MOC):PMAC:M5.MRES CP")
  field(INPB, "$(MOC):PMAC:M5:SCALE")  
  field(INPC, "$(MOC):PMAC:M5:HMPOS_RBV CP")  
  field(CALC, "A*B*C")
  field(OUT, "$(MOC):PMAC:M5:HMPOS PP")
}

record(ai, "$(MOC):PMAC:M5:HMPOS") {
  field(DESC, "BOD2 X")
  field(PREC, "4")
  field(EGU, "mm")
}

# at startup, tell autohome template about this motor
record(stringout, "$(MOC):PMAC:M5:SETHMPV") {
  field(PINI, "YES")
  field(VAL, "$(MOC):PMAC:M5")
  field(OUT, "$(MOC):PMAC::HM:M5PV PP")
}

record(stringout, "$(MOC):PMAC::HM:M5PV"){
}

# define a kill record
record(ao, "$(MOC):PMAC:M5:KILL") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),5)PMAC_C_KILL_AXIS")  
  field(VAL, "0")
}

record(calc, "$(MOC):PMAC:M5:ELOSSVAR") {
  field(CALC, "464+5")
  field(PINI, "YES")
}

record(ai, "$(MOC):PMAC:M5:ELOSS") {
  field(DTYP, "asynInt32")
  field(INP, "@asyn($(PORT),0)PMAC_VIS_P`464+5`")  
  field(SCAN, "I/O Intr")  
}

record(calcout, "$(MOC):PMAC:M5:ELOSSRC") {
  field(OUT, "$(MOC):PMAC:M5:SEND_ELOSSRC PP") 
  field(DESC, "Write 0 to A to reset Eloss")
  field(CALC, "A")
  field(A, "0")  
}

record(ao, "$(MOC):PMAC:M5:SEND_ELOSSRC") {
  field(DTYP, "asynInt32")
  field(OUT, "@asyn($(PORT),0)PMAC_VIS_P`464+5`")  
}

# Overwrite the description of pmacStatus
record(stringout, "$(MOC):PMAC:AXIS5:DESC"){
  field(VAL, "M5 BOD2 X")
}

#record(bo, "$(MOC):PMAC:M5:HOMED") {
#  field(DESC, "Set homed status for axis 5")
#  field(ZNAM, "NOT HOMED")
#  field(ONAM, "HOMED")
#  field(FLNK, "$(MOC):PMAC:M5:HOMEDSET")
#}

#record(calcout, "$(MOC):PMAC:M5:HOMEDSET") {
#  field(DESC, "Homed status")
#  field(DTYP, "stream")
#  field(CALC, "A")
#  field(INPA, "$(MOC):PMAC:M5:HOMED")
#  field(OUT, "@pmac.proto setIntVar(m545) ")
#}
##############################################################
#
# A record to sit along side each motor record and give direct
# access to the driver without any motor record logic
# may be used with virtual and real motors
#
# This template requires that all real motors mentioned have
# a real underlying motor record (to determine MRES) this is
# not required for virtual motors
#
# This currently only supports demand but could be extended
# to totally replace motor record (perhaps)
#
##############################################################

# % macro, PORT, The asyn port (motor controller or CS controller)
# % macro, ADDR, The asyn address (real or CS axis number)
# % macro, DESC, Description
# % macro, PMAC, PV Prefix for the motor controller
# % macro, P, PV Prefix for direct motors
# % macro, M, Motor PV suffix ('Mx' for real and 'CSx:My' for CS axes)

##############################################################
# Set immediate demand for this motor
##############################################################
#% archiver 0.5 Monitor
record(ao, "$(MOC):PMAC:M5:DirectDemand")
{
    field(FLNK, "$(MOC):PMAC:M5:DirectCountsCalc.PROC")
    field(PREC, 4)
}

record(scalcout, "$(MOC):PMAC:M5:DirectCountsCalc")
{
    field(INPA, "$(MOC):PMAC:M5:DirectDemand")
    field(INPB, "$(MOC):PMAC:M5:Mres PP")
    field(INPC, "$(MOC):PMAC:M5:Offset PP")
    field(INPD, "$(MOC):PMAC:M5:SCALE")
    field(CALC, "(A-C)/B*D")
    field(OUT, "$(MOC):PMAC:M5:RawDirectDemand PP")
}

record(ao, "$(MOC):PMAC:M5:RawDirectDemand")
{
    field(DTYP, "asynFloat64")
    field(FLNK, "$(MOC):PMAC:M5:DirectMotorSeq")
    field(OUT,  "@asyn($(PORT),5,4)MOTOR_MOVE_ABS")
    field(DESC, "BOD2 X")
}

# busy record to make ca_put_callback return only once the motor is done moving
record(seq, "$(MOC):PMAC:M5:DirectMotorSeq")
{
    field(LNK1, "$(MOC):PMAC:M5:DirectMotorBusy PP")
    field(DO1, "1")
}

record(busy, "$(MOC):PMAC:M5:DirectMotorBusy")
{
   field(DTYP, "asynInt32")
   field(OUT, "@asyn($(PORT),5,4)MOTOR_STATUS_MOVING")
   field(ZNAM, "Idle")
   field(ONAM, "Moving")
   field(PINI, "1")
   field(VAL,  "0")
}


# record that holds the SCALE
# this overrides the definition in basic_asyn_motor.template and connects it to the
# asyn parameter that tracks the scale factor in the driver. This is set by calling
# pmacSetAxisScale (for CS axes this function is exposed to startup via
# pmacSetCoordStepsPerUnit)

record(ao, "$(MOC):PMAC:M5:SCALE") {
    field(DTYP, "asynInt32")
    field(OUT,  "@asyn($(PORT),5,4)PMAC_MOTOR_SCALE")
}

##############################################################
# Database logic to set the correct ASLO and OFF on
# DirectDemand so that it can be commanded in EGUs
#
# The Mres and Offset are copied into ALSO and OFF from
# the appropriate real motor determined by DirectMotorNumber.
#
# For CS motors DirectMotorNumber is set by the driver when it
# maps a CS motor directly to a real motor.
#
# If as CS motor is not directly mapped then it wants an
# mres of 1 but the scaling means this usually translates
# to 0.0001.
#
# For real motors DirectMotorNumber is always reported as the
# motor's own number
#
# The calculation in MresSource handles linking in the
# correct real_motor mres or the value 1 for kinematic CS axes
#
##############################################################
record(longin, "$(MOC):PMAC:M5:DirectMotorNumber")
{
    field(PINI, 1)
    field(VAL, 0)
    field(DTYP, "asynInt32")
    field(SCAN, "I/O Intr")
    field(INP,  "@asyn($(PORT),5,4)PMAC_REAL_MOTOR_NUMBER")
    field(FLNK, "$(MOC):PMAC:M5:MresSource")
}

record(scalcout, $(MOC):PMAC:M5:MresSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M5:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.MRES CPP',A)):1")
    field(OUT, "$(MOC):PMAC:M5:Mres.INPA CA")
    field(FLNK, "$(MOC):PMAC:M5:OffsetSource")
}

record(scalcout, "$(MOC):PMAC:M5:Mres")
{
    field(INPA, "1") # to be overwritten by MresSource
    field(CALC, "A")
    field(VAL, 1)
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M5:MresDrv PP")
    field(PREC, 6)
}

record(ao,"$(MOC):PMAC:M5:MresDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),5,4)PMAC_MRES")
}

# todo non zero offset in CS kinematic axes no supported
record(scalcout, $(MOC):PMAC:M5:OffsetSource)
{
    field(DTYP, "Soft Channel")
    field(INPA, "$(MOC):PMAC:M5:DirectMotorNumber")
    field(CALC, "A?(PRINTF('$(MOC):PMAC:M%d.OFF CPP',A)):0")
    field(OUT, "$(MOC):PMAC:M5:Offset.INPA CA")
}


record(scalcout, $(MOC):PMAC:M5:Offset)
{
    field(INPA, "0") # to be overwritten by OffsetSource
    field(CALC, "A")
    field(PINI, "YES")
    field(OUT,  "$(MOC):PMAC:M5:OffsetDrv PP")
    field(PREC, 4)
}


record(ao,"$(MOC):PMAC:M5:OffsetDrv") {
    field(DTYP, "asynFloat64")
    field(OUT,  "@asyn($(PORT),5,4)PMAC_OFFSET")
}# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:Y:INPOSITION") {
    field(DESC, "IN position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:Y:IN") {
    field(DESC, "Move to IN position")
    field(DOL1, "BOD:UNDS:3575:Y:INPOSITION")
    field(LNK1, "BOD:UNDS:3575:Y:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:Y:OUTPOSITION") {
    field(DESC, "OUT position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:Y:OUT") {
    field(DESC, "Move to OUT position")
    field(DOL1, "BOD:UNDS:3575:Y:OUTPOSITION")
    field(LNK1, "BOD:UNDS:3575:Y:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:X:INPOSITION") {
    field(DESC, "IN position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:X:IN") {
    field(DESC, "Move to IN position")
    field(DOL1, "BOD:UNDS:3575:X:INPOSITION")
    field(LNK1, "BOD:UNDS:3575:X:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:X:OUTPOSITION") {
    field(DESC, "OUT position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:X:OUT") {
    field(DESC, "Move to OUT position")
    field(DOL1, "BOD:UNDS:3575:X:OUTPOSITION")
    field(LNK1, "BOD:UNDS:3575:X:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:ANGLE:F1POSITION") {
    field(DESC, "F1 position")
    field(EGU,  "deg")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:ANGLE:F1") {
    field(DESC, "Move to F1 position")
    field(DOL1, "BOD:UNDS:3575:ANGLE:F1POSITION")
    field(LNK1, "BOD:UNDS:3575:ANGLE:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:ANGLE:F2POSITION") {
    field(DESC, "F2 position")
    field(EGU,  "deg")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "72")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:ANGLE:F2") {
    field(DESC, "Move to F2 position")
    field(DOL1, "BOD:UNDS:3575:ANGLE:F2POSITION")
    field(LNK1, "BOD:UNDS:3575:ANGLE:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:ANGLE:F3POSITION") {
    field(DESC, "F3 position")
    field(EGU,  "deg")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "144")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:ANGLE:F3") {
    field(DESC, "Move to F3 position")
    field(DOL1, "BOD:UNDS:3575:ANGLE:F3POSITION")
    field(LNK1, "BOD:UNDS:3575:ANGLE:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:ANGLE:F4POSITION") {
    field(DESC, "F4 position")
    field(EGU,  "deg")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "216")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:ANGLE:F4") {
    field(DESC, "Move to F4 position")
    field(DOL1, "BOD:UNDS:3575:ANGLE:F4POSITION")
    field(LNK1, "BOD:UNDS:3575:ANGLE:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3575:ANGLE:F5POSITION") {
    field(DESC, "F5 position")
    field(EGU,  "deg")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "288")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3575:ANGLE:F5") {
    field(DESC, "Move to F5 position")
    field(DOL1, "BOD:UNDS:3575:ANGLE:F5POSITION")
    field(LNK1, "BOD:UNDS:3575:ANGLE:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3795:Y:INPOSITION") {
    field(DESC, "IN position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3795:Y:IN") {
    field(DESC, "Move to IN position")
    field(DOL1, "BOD:UNDS:3795:Y:INPOSITION")
    field(LNK1, "BOD:UNDS:3795:Y:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3795:Y:OUTPOSITION") {
    field(DESC, "OUT position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3795:Y:OUT") {
    field(DESC, "Move to OUT position")
    field(DOL1, "BOD:UNDS:3795:Y:OUTPOSITION")
    field(LNK1, "BOD:UNDS:3795:Y:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3795:X:INPOSITION") {
    field(DESC, "IN position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3795:X:IN") {
    field(DESC, "Move to IN position")
    field(DOL1, "BOD:UNDS:3795:X:INPOSITION")
    field(LNK1, "BOD:UNDS:3795:X:MOTOR PP")
}

# inOut.template
#
# Store and move to in or out position.

record(ao, "BOD:UNDS:3795:X:OUTPOSITION") {
    field(DESC, "OUT position")
    field(EGU,  "mm")
    field(PREC, "4")
    field(LOPR, "")
    field(HOPR, "")
    field(DRVL, "")
    field(DRVH, "")
    field(VAL,  "0")
    field(PINI, "1")
    info(autosaveFields, "VAL EGU PREC")
}

record(seq, "BOD:UNDS:3795:X:OUT") {
    field(DESC, "Move to OUT position")
    field(DOL1, "BOD:UNDS:3795:X:OUTPOSITION")
    field(LNK1, "BOD:UNDS:3795:X:MOTOR PP")
}

