# Sequence To Move Motors to Real Space
record(seq, "$(Device):REALSPACE"){
  field(DESC, "Realspace Sequence")
  field(DOL1, "$(Device):L3_CALCULATED CA")
  field(LNK1, "$(Device):3:MOTR PP")
  field(DOL2, "$(Device):L2_CALCULATED CA")
  field(LNK2, "$(Device):2:MOTR PP")
  field(DOL3, "$(Device):L1_CALCULATED CA")
  field(LNK3, "$(Device):1:MOTR PP")
}

# Reverse Calculate Motor 3 Position
record(calc, "$(Device):L3_CALCULATED"){
  field(DESC, "Calculated Desired M3 Position")
  field(CALC, "A+(TAN(C/1000)*(B+$(DY1)))")
  field(INPA, "$(Device):X_REQUESTED")
  field(INPB, "$(Device):Y_REQUESTED")
  field(INPC, "$(Device):T_REQUESTED")
  field(PREC, "4")
  field(EGU, "mm")
}

# Reverse Calculate Motor 2 Position
record(calc, "$(Device):L2_CALCULATED"){
  field(CALC, "(TAN(A/1000)*($(DX1)+$(DX2))-2*B)/(-2)")
  field(INPA, "$(Device):T_REQUESTED")
  field(INPB, "$(Device):Y_REQUESTED")
  field(PREC, "4")
  field(EGU, "mm")
}

# Reverse Calculate Motor 1 Position
record(calc, "$(Device):L1_CALCULATED"){
  field(CALC, "(2*A)-B")
  field(INPA, "$(Device):Y_REQUESTED")
  field(INPB, "$(Device):L2_CALCULATED")
  field(PREC, "4")
  field(EGU, "mm")
}

# Calculate Real Space Y
record(calc, "$(Device):Y_CALCULATED"){
  field(DESC, "Calculated Realspace Y Coordinate")
  field(CALC, "(A+B)/2")
  field(INPA, "$(Device):1:LVPOS")
  field(INPB, "$(Device):2:LVPOS")
  field(FLNK, "$(Device):T_CALCULATED")
  field(SCAN, "1 second")
  field(PREC, "4")
  field(EGU, "mm")
}

# Calculate Real Space Theta
record(calc, "$(Device):T_CALCULATED"){
  field(DESC, "Calculated Realspace Theta Coordinate")
  field(CALC, "1000*(ATAN((A-B)/($(DX1)+$(DX2))))")
  field(INPA, "$(Device):1:LVPOS")
  field(INPB, "$(Device):2:LVPOS")
  field(FLNK, "$(Device):X_CALCULATED")
  field(EGU, "mrad")
  field(PREC, "4")
}

# Calculate Real Space X
record(calc, "$(Device):X_CALCULATED"){
  field(DESC, "Calculated Realspace X Coordinate")
  field(CALC, "C-(TAN(A/1000)*(B+$(DY1)))")
  field(INPA, "$(Device):T_CALCULATED")
  field(INPB, "$(Device):Y_CALCULATED")
  field(INPC, "$(Device):3:LVPOS")
  field(PREC, "4")
  field(EGU, "mm")
}

# User Request for Real Space X
record(ai, "$(Device):X_REQUESTED"){
  field(DESC, "Requested Realspace X Coordinate")
  field(LOPR, "-3")
  field(HOPR, "3")
  field(PREC, "4")
  field(EGU, "mm")
  info(autosaveFields, "VAL")
}

# User Request for Real Space Y
record(ai, "$(Device):Y_REQUESTED"){
  field(DESC, "Requested Realspace Y Coordinate")
  field(LOPR, "-3")
  field(HOPR, "3")
  field(PREC, "4")
  field(EGU, "mm")
  info(autosaveFields, "VAL")
}

# User Request for Real Space Theta
record(ai, "$(Device):T_REQUESTED"){
  field(DESC, "Requested Realspace Theta Coordinate")
  field(LOPR, "-3")
  field(HOPR, "3")
  field(PREC, "4")
  field(EGU, "mrad")
  info(autosaveFields, "VAL")
}

# Stop All 3 Motors
record(fanout, "$(Device):STOPMOTORS") {
  field(DESC, "Stop All Motors")
  field(LNK1, "$(Device):1:MOTR.STOP PP")
  field(LNK2, "$(Device):2:MOTR.STOP PP")
  field(LNK3, "$(Device):3:MOTR.STOP PP")
  field(SELM, "All")
}
