#! Generated by VisualDCT v2.6
#! DBDSTART
#! DBD("../../../../dbd/mgntSoft.dbd")
#! DBDEND



record(seq, "$(DEV):STDZINIT") {
  field(DESC, "Proc. Stdz PVs on boot")
  field(PINI, "YES")
  field(DOL1,  "1")
  field(DOL2,  "1")
  field(DOL3,  "1")
  field(LNK1, "$(DEV):DEBUG.PROC PP")
  field(LNK2, "$(DEV):STDZCYCLENUM.PROC PP")
  field(LNK3, "$(DEV):STDZDISABLE.PROC PP")
  field(LNK4, "$(DEV):STDZSUB.DISV PP")
  field(ASG,  "Internal")
}

# Inputs:
#  A - minimum current set point
#  B - maximum current set point
#  C - final current set point
#  D - Number of cycles, where one cycle is min to max for stdzdirect=up (0)
#      or from imax to imin for stdzdirect=down (1). 
#         The total number of set points would be 2*D+1
#  E - Standardize direction (up=0,down=1). The standardize direction
#      is defined by magnet measurements (MMF). For lcls, all magnets 
#      are standardized in the up direction, which means that the standardize
#      cycle would starts at imin to imax. If the direction was down, then the standardize
#      cycle would start from imax to imin.
#  F - Standardize disabled (0=enabled, 1=disabled)
#  G - Debug flag from the pv <device>:DEBUG
#
# The standardize function is available for all controllable devices, but it is
# typically only used for larger magnets (steel),  powered by high current
# supplies. These magnets tend to build up a residual field over time. This would
# typically dipoles and quadrupoles. Saying this, the undulator quadrupoles, powered by 2A supplies
# and these magnets are standardized. Therefore, to get a complete list of magnets that
# should bestandardized for consulte the LCLS PRD 1.1-015, which  provides a list, This
# document also provides a list of standardize magnet groups, number of cycles, standardize
# ramprates and settle times.
#
#  A: <dev>:STDZIMIN
#  B: <dev>:STDZIMAX 
#  C: <dev>:STDZIFINAL
#  D: <dev>:STDZCYCLENUM
#  E: <dev>:STDZDIRECT
#  F: <dev>:STDZDISABLE
#  G: <dev>:DEBUG
#
# The success or failure of the standardize function is set 
# in the pv <dev>:STDZOK, and the magnet is restored to the
# desired b-field <dev>:BDES.
#
#  <dev>:STDZOK  (1=successfuly completed, 0=failed)
#
# The standardize function is available for individual magnets,
# or the main (defined in db) of a string. Some indiviaul magnets are grouped
# with a magnet string, such as boosts, trims or sometimes magnets
# nearby a large magnet string. These groupings are defined in the
# PRD 1.1-015. 
#
# This PV is processed by <dev>:STDZIFINAL
record(aSub, "$(DEV):STDZSUB") {
  field(DESC, "Stdz subroutine")
  field(INAM, "mgntStdzInit")
  field(SNAM, "mgntStdz")
  field(BRSV, "INVALID")
  
  field(INPA, "$(DEV):STDZIMIN NPP NMS")
  field(INPB, "$(DEV):STDZIMAX NPP NMS")
  field(INPC, "$(DEV):STDZIFINAL NPP NMS")
  field(INPD, "$(DEV):STDZCYCLENUM NPP NMS")
  field(INPE, "$(DEV):STDZDIRECT.RVAL NPP NMS")
  field(INPF, "$(DEV):STDZDISABLE.RVAL CPP NMS")
  field(INPG, "$(DEV):DEBUG.RVAL NPP NMS")
  field(FTA, "DOUBLE")
  field(FTB, "DOUBLE")
  field(FTC, "DOUBLE")
  field(FTD, "LONG")
  field(FTE, "ULONG")
  field(FTE, "ULONG")
  field(FTG, "ULONG")

# Outputs:
#  OUTA: set point waveform
#  OUTB: mgntStdz status message
# Note: the aSub sets the NORD field fo the
# waveform record based on the value in NOVA.
#
# If NOVA changes, make sure that <dev>:STDZWF.NELM
# is equal or greater than <dev>:STDZSUB.NOVA
#
  field(OUTA, "$(DEV):STDZWF PP")
  field(OUTB, "$(DEV):STDZMSG PP")
  field(FTVA, "DOUBLE")
  field(FTVB, "USHORT")
  field(NOVA, "21")

# On boot disable this PV from processing,
# enabled processing from the pv
# <dev>:STDZINIT, after all input pvs
# have been processed.
  field(DISV, "0")
  field(FLNK, "$(DEV):STDZWF")
}

record(mbbo, "$(DEV):STDZMSG") {
  field(DESC, "STDZSUB pv status")
  field(NOBT, "2")
  field(ZRST, "Success")
  field(ONST, "None")
  field(TWST, "Wf too small")
  field(THST, "Unexepcted data type")  
}

# This is the standardize waveform, generated by
# the <device>:STDZSUB
# Note: <dev>:STDZWF.NELM must be equal or greater than <dev>:STDZSUB.NOVA
record(waveform, "$(DEV):STDZWF") {
  field(DESC, "Stdz Setpts")
  field(NELM, "21")
  field(FTVL, "DOUBLE")
}

#
# METHOD TO RESTORE A MULTI-BIT BINARY OUT and BO's
# 1. DON'T DEFINE DOL
# 2. SET PINI TO YES
# 3. RESTORE THE VAL FIELD
# ON THE VERY FIRST BOOT YOU MUST DO
# A CAPUT TO THE MBBO AND THEN ON REBOOT
# THE UDF WILL BE SET TO 0 AND THE VALUE
# WILL BE RESTORED PROPERLY. 
#
# IMPORTANT:
# IF YOU DO NOT DO A CAPUT ON THE VERY FIRST BOOT, THE UDF
# WILL BE SET TO 1 AND THE STAT/SEVR WILL BE
# SET TO INVALID. THE INVALID STATE IS LOOKED
# AT ON REBOOT BY THE RESTORE PROGRAM AND THE MBBO
# WILL NOT BE RESTORED. SO IT'S VERY IMPORTANT TO DO
# THE INITAL CAPUT. THIS METHOD ALSO WORKS FOR BOs. 
# NOTE THAT ANALOG OUTUPTS ARE ARE RESTORED IN THE
# SAME MANNER SO THE VAL CAN BE RESTORED PROPERLY.
#
record(bo, "$(DEV):STDZMAINPS") {
  field(DESC, "Stdz by main or self")
  field(PINI, "YES")
  field(DTYP, "Soft Channel")
  field(ZNAM, "SELF")
  field(ONAM, "MAIN")
}

record(bo, "$(DEV):STDZPS") {
  field(DESC, "PS state after Stdz")
  field(PINI, "YES")
  field(ZNAM, "On")
  field(ONAM, "Off")
  field(DTYP, "Soft Channel")
}

# This pv is processed when starting and completing a standardize or degauss
# function. At the start of each function the trims and/or related magnets are zeroed (2)
# After the function has completed the trims and/or related magnets are trimmed (1).
#
# For strings with related magnets, such as trim coils or
# nearby magents, specifed in the Magnet standardize PRD.
# In this case, the string main the pv
# STDZSLAVESGO is defined using the template
# mgnt_string_ctrl.template, where the INP argument is set to
# STDZGO.RVAL CPP, processing on change. 
record(mbbo, "$(DEV):STDZGO") {
  field(DESC, "Stdz trims")
  field(NOBT, "2")
  field(ZRST, "Ready")
  field(ONST, "TRIM")
  field(TWST, "DAC_ZERO")
  field(ZRVL, "0")
  field(ONVL, "1")
  field(TWVL, "7")
}

record(bo, "$(DEV):STDZOK") {
  field(DESC, "Standarize Successful")
  field(DTYP, "Raw Soft Channel")
  field(PINI, "YES")
  field(DTYP, "Soft Channel")
  field(ZNAM, "NO")
  field(ONAM, "YES")
  info(autosaveFields,"VAL RVAL")
}

record(stringout, "$(DEV):STDZTOD") {
  field(DESC, "Successful Standardize Time")
  field(PINI, "YES")
  field(DTYP, "Soft Channel")
  field(VAL, " ")
  info(autosaveFields,"VAL")
}

record(stringout, "$(DEV):STDZLOSTTOD") {
  field(DESC, "Standardize Lost Time")
  field(PINI, "YES")
  field(DTYP, "Soft Channel")
  field(VAL, " ")
  info(autosaveFields,"VAL")
}

record(longout, "$(DEV):STDZCYCLECNT") {
  field(DESC, "Cycle Count")
  field(PINI, "YES")
}

record(longout, "$(DEV):STDZCYCLENUM") {
  field(DESC, "No. Standardize Cycles")
  field(VAL,  "$(NCYC=3)")
  field(HOPR, "10")
  field(LOPR, "0")
  field(DRVH, "10")
  field(DRVL, "0")
  field(PINI, "YES")
  field(FLNK, "$(DEV):STDZDIRECT")
}

# The default standardize direction is "UP" for
# all LCLS Magnets..
record(bo, "$(DEV):STDZDIRECT") {
  field(DESC, "Standardize Direction ")
  field(ZNAM, "UP")
  field(ONAM, "DOWN")
  field(VAL,  "0")
  field(FLNK, "$(DEV):STDZIMIN")
}

record(ao, "$(DEV):STDZIMIN") {
  field(DESC, "Min-STDZ Current")
  field(PREC, "1")
  field(EGU,  "A")
  field(HOPR, "$(HIHI)")
  field(LOPR, "$(LOLO)")
  field(VAL,  "$(LOLO)")
  field(FLNK, "$(DEV):STDZIMAX")
}

record(ao, "$(DEV):STDZIMAX") {
  field(DESC, "Max. Standarize Current")
  field(DTYP, "Soft Channel")
  field(PREC, "1")
  field(EGU,  "A")
  field(HOPR, "$(HIHI)")
  field(LOPR, "$(LOLO)")
  field(VAL,  "$(HIHI)")
  field(FLNK, "$(DEV):STDZIFINAL")
}

record(ao, "$(DEV):STDZIFINAL") {
  field(DESC, "Final SDTZ Current")
  field(DTYP, "Soft Channel")
  field(PREC, "1")
  field(EGU,  "A")
  field(HOPR, "$(HIHI)")
  field(LOPR, "$(LOLO)")
  field(VAL,  "$(LOLO)")
  field(FLNK, "$(DEV):STDZSUB")
}

# Loose tolerance on desired current-ADC measured current. 
# used for calibration and standardize. This is an
# absolute tolerance with the default set to 10 percent.
# Note: in slc ATOL(1), absolute tolerance
record(ao, "$(DEV):STDZITOLR") {
  field(DESC, "Stdz tolerance min")
  field(DTYP, "Soft Channel")
  field(PREC, "5")
  field(DRVL, "0.05")
  field(DRVH, "10.0")
  field(VAL,  "0.1")
  field(EGU,  "%")
  field(PINI, "YES")
}

# Loose tolerance on desired current-adc measured current. 
# used for calibration and standardize. The tolerance
# is a percentage (INPC=5%) of the maximum current. Note that
# we have bipolar supplies, so the absolute value of
# the min and max current is taken.
# Note: in slc ATOL(2), fractional tolerance
record(calc, "$(DEV):STDZITOL") {
  field(DESC, "Stdz min tolerance")
  field(PREC, "5")
  field(CALC, "MAX(ABS(A),ABS(B))*C")
  field(INPA, "$(DEV):STDZIMIN CP NMS")
  field(INPB, "$(DEV):STDZIMAX CP MMS")
  field(INPC, "0.05")
  field(INPD, "$(DEV):STDZITOLR CP MMS")
  field(EGU,  "A")
  field(FLNK, "$(DEV):IDELTAS")
  field(PINI, "YES")
}

# D - factor (3.154/2 = 1.577) for EPSC, otherwise, set to 1.0
record(calc, "$(DEV):STDZDLY") {
  field(DESC, "Ramp time for stdz cycle")
  field(CALC, "C>0?MAX(1,ABS((A-B)/C)*D):0")
  field(INPA, "$(DEV):STDZIMIN CP NMS")
  field(INPB, "$(DEV):STDZIMAX CP NMS")
  field(INPC, "$(DEV):TRIMRAMPRATE CP NMS")
  field(INPD, "$(MULT=1.0)")
  field(PREC, "5")
  field(EGU,  "sec")
  field(PINI, "YES")
}

record(calc, "$(DEV):STDZRAMPDLY") {
  field(DESC, "Ramp delay total")
  field(CALC, "MAX(1,((A/100)*B+B))")
  field(PREC, "5")
  field(INPA, "$(DEV):TRIMDLYPCT CP MS")
  field(INPB, "$(DEV):STDZDLY CP MS")
  field(EGU, "sec")
}

# Meeting 4/22/2011 Loos, Sullivan decided to wait 1 sec
# Note: see note in mgntCtrl.st, mgntCtrl_settle_wait
record(calc, "$(DEV):STDZMINIDLY") {
  field(DESC, "Mini-stdz delay")
  field(CALC, "A")
  field(PREC, "5")
  field(A,    "1.0")
  field(EGU, "sec")
  field(PINI, "YES")
}

record(ao, "$(DEV):STDZMINIPCT") {
  field(DESC, "Mini Stdz percent")
  field(DTYP, "Soft Channel")
  field(VAL, "5.0")
  field(EGU, "%")
  field(PREC, "2")
  field(DRVH, "15.0")
  field(DRVL, "0.0")
  field(HOPR, "15.0")
  field(LOPR, "0.0")
  field(PINI, "YES")
}

record(calc, "$(DEV):STDZMINIDELTA") {
  field(DESC, "Stdz mini % of IMAX")
  field(CALC, "(B/100)*(A-C)")
  field(INPA, "$(DEV):IDES.HOPR")
  field(INPB, "$(DEV):STDZMINIPCT CPP NMS")
  field(INPC, "$(DEV):IDES.LOPR")
  field(PREC, "5")
}

record(calc, "$(DEV):STDZMINISETPT") {
  field(DESC, "Mini Stdz delta")
  field(CALC, "C==0?MAX(E-D,B):MIN(E+D,A)")
  field(INPA, "$(DEV):IDES.HOPR")
  field(INPB, "$(DEV):IDES.LOPR")
  field(INPC, "$(DEV):STDZDIRECT.RVAL")
  field(INPD, "$(DEV):STDZMINIDELTA")
  field(INPE, "$(DEV):IDES")
  field(PREC, "5")
  field(FLNK, "$(DEV):STDZMINISETPTOOR")
}

# This PV is 1 if STDZMINISETPT is out of range (greater than
# IDES.HOPR or less than IDES.LOPR) and 0 otherwise.
record(calc, "$(DEV):STDZMINISETPTOOR") {
  field(DESC, "Mini Stdz setpoint out of range")
  field(CALC, "C==0?(E-D<B):(E+D>A)")
  field(INPA, "$(DEV):IDES.HOPR")
  field(INPB, "$(DEV):IDES.LOPR")
  field(INPC, "$(DEV):STDZDIRECT.RVAL")
  field(INPD, "$(DEV):STDZMINIDELTA")
  field(INPE, "$(DEV):IDES")
  field(PREC, "5")
}

# DR 4/2011 add these 2 records for perturb slider
# If currently standardized and we are moving opposite
# to the direction of standardization, then clear STDZOK
# and not the time that standardize was lost in STDZTOD.
# However, if standardization has already been lost don't
# change the standardize lost date or bother to clear STDZOK.
# To enable STDZ, STDZDISABLE = 0 = Enabled
#
# AL 7/2022 lose standardize only if out of tolerance
# AL 10/2022 do not lose standardize if IACT is invalid
# 
# This PV is processed by <dev>:BCTRLGO and a change in <dev>:STATE
record(calcout, "$(DEV):CHECKSTDZLOST") {
  field(DESC, "Check if stdz is lost")

  field(CALC, "(G=3||I=3)?0:C=1&&D=0&&H=1&&(((A<B)||(A>B))||(E=0&&G<3))?3:0")
  field(INPA, "$(DEV):IADJUST.RVAL")
  field(INPB, "$(DEV):STDZDIRECT.RVAL")
  field(INPC, "$(DEV):STDZOK.RVAL")
  field(INPD, "$(DEV):STDZDISABLE.RVAL")
  field(INPE, "$(DEV):STATE.RVAL CPP NMS")
  field(INPF, "$(DEV):ICTRL.RVAL")
  field(INPG, "$(DEV):STATE.SEVR")
  field(INPH, "$(DEV):BDELTAS.M")
  field(INPI, "$(DEV):IACT.SEVR")
  field(OUT,  "$(DEV):STDZLOSTGO.SELN PP NMS")
  field(OOPT, "Transition To Non-zero")
  field(FLNK, "$(DEV):STDZLOSTGO")
}

record(seq, "$(DEV):STDZLOSTGO") {
  field(DESC, "Process lost stdz")
  field(SELM, "Mask")
  field(SELL, "$(DEV):CHECKSTDZLOST")
  field(DOL1, "1")
  field(DOL2, "0")
  field(LNK1, "$(DEV):STDZLOSTTOD.PROC")
  field(LNK2, "$(DEV):STDZOK PP")
}

