#!../../bin/linux-x86/umc

## You may have to change AerotechEnsembleMP to something else
## everywhere it appears in this file

< /usr/local/lcls/epics/iocCommon/All/Prod/soft_pre_st.cmd.lbl

< envPaths
cd ${TOP}

## --------------------------------------------------------------------------------

## Register all support components
dbLoadDatabase "dbd/umc.dbd"
umc_registerRecordDeviceDriver pdbbase

## --------------------------------------------------------------------------------

# Stream device protocol file path
epicsEnvSet( "STREAM_PROTOCOL_PATH", "$(TOP)/umcApp/src" )

## Aerotech Ensemble MP controller configuration

# To properly configure controller read the README file in
#    $(EPICS_MODULES_TOP)/motor/$(MOTOR_MODULE_VERSION)/motorApp/AerotechSrc/

## Asyn IP port configuration
#     (1) ASYN port name
#     (2) server info (ipAddres:port)
#     (3) priority
#     (4) noAutoConnect
#     (5) noProcessEos
drvAsynIPPortConfigure("EnsemblePort","192.168.1.116:8000", 0, 0, 0)

## Aerotech Ensemble digital servo controller
#     (1) maximum number of controllers in system
EnsembleAsynSetup(1)

## The Ensemble driver does not set end of string (EOS).
asynOctetSetInputEos("EnsemblePort",0,"\n")
asynOctetSetOutputEos("EnsemblePort",0,"\n")

## Initialization of Aerotech Ensemble controller
#     (1) Controller number being configured
#     (2) ASYN port name
#     (3) ASYN address (GPIB only)
#     (4) Number of axes this controller supports
#     (5) Time to poll (msec) when an axis is in motion
#     (6) Time to poll (msec) when an axis is idle. 0 for no polling
EnsembleAsynConfig(0, "EnsemblePort", 0, 4, 100, 1000)

## Asyn-based Motor Record support
#   (1) Asyn port
#   (2) Driver name
#   (3) Controller index
#   (4) Max. number of axes
drvAsynMotorConfigure("AeroE1","motorEnsemble",0,4)

## --------------------------------------------------------------------------------

## Load record instances

dbLoadRecords(db/SIOC-LBNL-MC02-motors.db)
dbLoadRecords(db/asynRecord.db, "P=AEROTECH:,R=MOTOR:ASYN,PORT=EnsemblePort,ADDR=0,IMAX=100,OMAX=100")

##dbLoadRecords(db/encoder.db)
##dbLoadRecords(db/SIOC-LBNL-MC01-motors_new.db)
##dbLoadRecords(db/SIOC-LBNL-MC01-motors_gantry.db)
##dbLoadRecords(db/two_motor.db)
##dbLoadRecords(db/gap.db)

# ============================================================
# If all PVs don't connect continue anyway
# ============================================================
save_restoreSet_IncompleteSetsOk(1)

# ============================================================
# created save/restore backup files with date string
# useful for recovery.
# ============================================================
#save_restoreSet_DatedBackupFiles(0)

# ============================================================
# Where to find the list of PVs to save
# ============================================================
set_requestfile_path("${IOC_DATA}/${IOC}/autosave-req")

# ============================================================
# Where to write the save files that will be used to restore
# ============================================================
set_savefile_path("${IOC_DATA}/${IOC}", "autosave")

# ============================================================
# Prefix that is use to update save/restore status database
# records
# ============================================================
#save_restoreSet_status_prefix("SIOC:LBNL:MMF:")

# Restore datasets
#set_pass0_restoreFile("info_positions.sav")
#set_pass0_restoreFile("info_settings.sav")
set_pass1_restoreFile("info_settings.sav")

## --------------------------------------------------------------------------------

cd ${TOP}/iocBoot/${IOC}
iocInit

## ===========================================================
## Start autosave routines to save our data
## ===========================================================
# optional, needed if the IOC takes a very long time to boot.
epicsThreadSleep( 1.0)

cd ${IOC_DATA}/${IOC}/autosave-req
iocshCmd("makeAutosaveFiles()")

## Start autosave process:
create_monitor_set("info_settings.req",30,"")
create_monitor_set("info_positions.req",20,"")

## --------------------------------------------------------------------------------

## Start any sequence programs
seq( &setGap, "DEV=USEG:LBNL_MMF:1, PORT=EnsemblePort, M1=USEG:LBNL_MMF:1:X, M2=USEG:LBNL_MMF:1:Y, M3=USEG:LBNL_MMF:1:Z, M4=USEG:LBNL_MMF:1:U")
#seq( &savePos, "MOTOR_DEV=USEG:LBNL_MMF:1:TOP")
#seq( &savePos, "MOTOR_DEV=USEG:LBNL_MMF:1:BOT")

## --------------------------------------------------------------------------------

## simple workaround to enable the controller automatically...
#dbpf(AEROTECH:MOTOR:ASYN.AOUT,"PROGRAM STOP 1")
#dbpf(AEROTECH:MOTOR:ASYN.AOUT,"ENABLE 0")
#dbpf(AEROTECH:MOTOR:ASYN.AOUT,"ENABLE 2")
#dbpf(AEROTECH:MOTOR:ASYN.AOUT,'PROGRAM RUN 1, "encoder_to_global.bcx"')

dbpf(USEG:LBNL_MMF:1:DSBOT:MOTR.FOFF,1)
dbpf(USEG:LBNL_MMF:1:USBOT:MOTR.FOFF,1)
dbpf(USEG:LBNL_MMF:1:DSTOP:MOTR.FOFF,1)
dbpf(USEG:LBNL_MMF:1:USTOP:MOTR.FOFF,1)
dbpf(USEG:LBNL_MMF:1:AUTOBRAKE,1)
dbpf(USEG:LBNL_MMF:1:SimMode,0)
dbpf(USEG:LBNL_MMF:1:SINGLE_APPROACH,0)

