DECLARE SUB TimeStuff (RAFitsLine1$, RAFitsLine$, FileDate$, TimeSinceRAON!, LongitudeCorr!, HomeZenithAngle!)
' ***************************
'   THIS IS TOM'S MARKIV SOFTWARE PROGRAM
'  arne015.bas mod
' ***************************
DECLARE SUB LoadPowerControlRegister (j%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB RAMove (j%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB PulseIt (p%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB MAServo (j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB Motor (Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
DECLARE SUB InitStamp (j%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB LoadDAC (j%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB ReadADC (a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB OutIt (a$, b$, c$, d$, e$, f$, g$, H$)
DECLARE SUB Display (j%, N$(), a!(), d!(), c%(), v!(), p$())
DECLARE SUB Timex (a#)
'this version is two camera software
DEFSNG A-Z
CLS
SCREEN 0, 0
WIDTH 80, 43
Messloc% = 40
Version$ = "B"           'PC Code Version
SubVersion$ = "0"
noadc = 0'0'1 to skip ADC reading
LOCATE 1, 1
noadc = 0
CALL Timex(-1)
TestIt = 1
'******************************************************************
'constant values here
ExposureTime# = 100'0
FocusTime# = 5'was 20
OffTime# = 2
RATimeOn# = 40
RATimeOff# = 5
FocusMove% = 5
FocusSteps% = 5
DecCal = 15  'steps per degree
DecPosition = 50  'degrees starting position above dec home position
DecIncrement = 3.5 'degree steps
DecLimit = .4834'.5'Was .25 Declination when at limit switch
PadTime# = 1  'combine with Saveit1 time to get exposure
LongitudeCorr! = 18.1125'decimal hours East of Greenwich
HomeZenithAngle! = -.6864'(010830)-.'pointing angle East of meridian when RA home. Hours
TimeSinceRAON! = 0'time since turning on RA to correct for RA position
SiteLetterCode$ = "h"
DecLimitSwitch$ = "7:00:00"'-0:16:00" 'Home Declination Position
DecLimitSwitch1! = 7! '(010830)-.2666' -.25'was 0
Decl$ = DecLimitSwitch$'Declination of home.
Decl1! = (INT((DecLimitSwitch1!) * 100)) / 100'decimal dec limit switch
LowWater = 25
'******************************************************************
'Dimension and name all the arrays for data and labels done this
'way for ease in passing to subroutines.
DIM N$(0 TO 100)         'labels for values
DIM a!(0 TO 31, 0 TO 4)  'space for the adc data
DIM d!(0 TO 15)          'DAC command values
DIM c%(100)              'misc integers
DIM v!(100)              'misc reals
DIM p$(0 TO 23, 0 TO 1)  'pulse select path conversion
TYPE foo
LineDataa(0 TO 2042) AS INTEGER   '2063      '1023
END TYPE
TYPE goo
LineDatab(0 TO 2042) AS INTEGER
END TYPE
DIM OutBufa AS foo
DIM OutBufb AS goo
'********************************************************************
'now brute force load up the arrays with their data
'later we can load them from a file we have saved
N$(0) = "Water Temp 0 "
N$(1) = "VCO Temp     "
N$(2) = "VCO Reference"
N$(3) = "Temp 1       "
N$(4) = "Temp 2       "
N$(5) = "Temp 3       "
N$(6) = "Temp C0 DAC15"
N$(7) = "Temp C3 DAC10"
N$(8) = "Ground       "
N$(9) = "-15          "
N$(10) = "-5           "
N$(11) = "+5           "
N$(12) = "+15          "
N$(13) = "VCO     DAC12"
N$(14) = "Temp C1 DAC14"
N$(15) = "Temp C2 DAC11"
N$(16) = "CCD Temp 2   "
N$(17) = "CCD VDD Power"
N$(18) = "CCD Temp 0   "
N$(19) = "CCD Temp 1   "
N$(20) = "ADC Reference"
N$(21) = "VVH     DAC 7"
N$(22) = "VRH     DAC 2"
N$(23) = "VHL     DAC 1"
N$(24) = "TEC Current 2"
N$(25) = "TEC Current 0"
N$(26) = "TEC Current 1"
N$(27) = "TEC Current 3"
N$(28) = "TEC Power    "
N$(29) = "VVL     DAC 6"
N$(30) = "VRL     DAC 3"
N$(31) = "VHH     DAC 0"
N$(32) = "Power Supply Voltages "
N$(33) = "DAC Commands          "
N$(34) = Version$        'pc software version
N$(35) = SubVersion$     'pc software sub version
N$(36) = "    Status            "
N$(37) = "Running ADC Scan      "
N$(38) = "     Done             "
N$(39) = "ADC Channel     Value   Offset  Scale "
N$(40) = "Loading The DACs      "
N$(41) = "  Software Version    "
N$(42) = "PC Version            "
N$(43) = "Stamp Version         "
N$(44) = "x"   'stamp version
N$(45) = "x"   'stamp sub version
N$(64) = "VHH     B  D0"
N$(65) = "VHL     A  D1"
N$(66) = "VRH     J  D2"
N$(67) = "VRL     I  D3"
N$(68) = "TR ADC3 N  D4"
N$(69) = "TR ADC2 M  D5"
N$(70) = "VVL     L  D6"
N$(71) = "VVH     K  D7"
N$(72) = "TR ADC1 P  D8"
N$(73) = "TR ADC0 O  D9"
N$(74) = "T COM3  S D10"
N$(75) = "T COM2  R D11"
N$(76) = "VCO     W D12"
N$(77) = "Spare   V D13"
N$(78) = "T COM1  U D14"
N$(79) = "T COM0  T D15"
'here are the command voltages for the dacs.
d!(0) = 6
d!(1) = -3
d!(2) = 0
d!(3) = 7
d!(4) = 2
d!(5) = 3
d!(6) = -8   ' - 7.5'-8.5' -8.2
d!(7) = 3.9    '8'6.2'6.7
d!(8) = 0
d!(9) = 0
d!(10) = 6
d!(11) = 7
d!(12) = -.66 '3  ' was 1.5, changed by aah
d!(13) = 9
d!(14) = -2'-3  ' was -5, changed by aah
d!(15) = -2'-4 ' was -5, changed by aah
offset! = 0 '.0251
a!(0, 1) = offset! - 2.73
a!(1, 1) = offset! - 2.73
a!(2, 1) = offset!
a!(3, 1) = offset! - 2.73
a!(4, 1) = offset! - 2.73
a!(5, 1) = offset! - 2.73
a!(6, 1) = offset!
a!(7, 1) = offset!
a!(8, 1) = offset!
a!(9, 1) = offset!
a!(10, 1) = offset!
a!(11, 1) = offset!
a!(12, 1) = offset!
a!(13, 1) = offset!
a!(14, 1) = offset!
a!(15, 1) = offset!
a!(16, 1) = offset! - 2.73
a!(17, 1) = offset!
a!(18, 1) = offset! - 2.73
a!(19, 1) = offset! - 2.73
a!(20, 1) = offset!
a!(21, 1) = offset!
a!(22, 1) = offset!
a!(23, 1) = offset!
a!(24, 1) = offset!
a!(25, 1) = offset!
a!(26, 1) = offset!
a!(27, 1) = offset!
a!(28, 1) = offset!
a!(29, 1) = offset!
a!(30, 1) = offset!
a!(31, 1) = offset!
a!(0, 2) = 100
a!(1, 2) = 100
a!(2, 2) = 1
a!(3, 2) = 100
a!(4, 2) = 100
a!(5, 2) = 100
a!(6, 2) = 1
a!(7, 2) = 1
a!(8, 2) = 1
a!(9, 2) = 10
a!(10, 2) = 1
a!(11, 2) = 1
a!(12, 2) = 10
a!(13, 2) = 1
a!(14, 2) = 1
a!(15, 2) = 1
a!(16, 2) = 100
a!(17, 2) = 10
a!(18, 2) = 100
a!(19, 2) = 100
a!(20, 2) = 1
a!(21, 2) = 1
a!(22, 2) = 1
a!(23, 2) = 1
a!(24, 2) = 10
a!(25, 2) = 10
a!(26, 2) = 10
a!(27, 2) = 10
a!(28, 2) = 10
a!(29, 2) = 1
a!(30, 2) = 1
a!(31, 2) = 1
'*************************Init Stuff**************************
'OPEN "COM1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR RANDOM AS #1
GOSUB InitTheRegisters    'do what we can to init
GOSUB TurnRAOff           'turn off the RA motor
GOSUB TurnTheMotorsOff    'turn off the other motors
'CALL Timex(.4)
IF noadc = 0 THEN
GOSUB LoadTheDACs         'load up all the DACs
'GOSUB LoadTheDACs         'firs time misses 0?  &002&
GOSUB ReadTheADCs         'read and put up the display of the voltages
END IF
GOTO TomFollow
'check here that the versions match
'*********************Enter First Menu************************
'This is the first menu which is somewhat different bacause of exit
Menuk = 0
StartMenu:
KEY 1, " Run "
KEY 2, " Test "
KEY 3, "TemPlt"
KEY 4, "SftTst"
KEY 5, "LdADAC"
KEY 6, "TstLog"
KEY 7, "TestIt"    'one time test code in a convenient spot
KEY 8, "RampIt"
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, " Tom "
ON KEY(1) GOSUB Menu16
ON KEY(2) GOSUB Menu2
ON KEY(3) GOSUB DoNotRampIt
ON KEY(4) GOSUB Menu18
ON KEY(5) GOSUB LoadASingleDAC
ON KEY(6) GOSUB TestLogEntry
ON KEY(7) GOSUB TestIt
ON KEY(8) GOSUB RampIt
ON KEY(9) GOSUB BackSet
ON KEY(10) GOSUB Menu19
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO StartMenu
END IF
LOOP
STOP
'this is an exemple of the standard menu structure
'Menuk is index into Keyx& key label matrix  MenunSave holds key
'matrix index of where we came from  GoBack=1 forces return to the
'previous menu.  CameBack=1 caused relabel of keys and resetting
'of the key(n) gosubs.  For a different menu change all the 1's
'to the new menu number - 5 places
Menu1:
Menu1Save = Menuk       'menu index we came from for this menu
Menu1e: Menuk = 10      'menu index for this menu
MenuSave = Menu1Save    'generic "came from" index for back key label
KEY 1, ""
KEY 2, "Menu2"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB Dummy
ON KEY(2) GOSUB Menu2
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu1Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu1e
END IF
LOOP
'end of standard menu structure
Menu2:                   'the entry test menu
Menu2Save = Menuk
Menu2e: Menuk = 20
MenuSave = Menu2Save
KEY 1, "TstADC"       'test monitor ADC
KEY 2, "TstDAC"       'test the DACs
KEY 3, "TstDsp"       'test the display
KEY 4, "TstMem"       'memory tests
KEY 5, "MAServ"       'test MA Servos
KEY 6, "TstMot"       'test motors
KEY 7, "TstPls"       'test pulses
KEY 8, "TstRA"        'test RA drive
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, "RdLim"       'test limit switches
ON KEY(1) GOSUB Menu3
ON KEY(2) GOSUB Menu4
ON KEY(3) GOSUB Menu5
ON KEY(4) GOSUB Menu17
ON KEY(5) GOSUB Menu6
ON KEY(6) GOSUB Menu7
ON KEY(7) GOSUB Menu8       'for test pulses
ON KEY(8) GOSUB Menu9
ON KEY(9) GOSUB BackSet
ON KEY(10) GOSUB Menu10
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu2Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu2e
END IF
LOOP
Menu3:                   'adc test menu
Menu3Save = Menuk
Menu3e: Menuk = 30
MenuSave = Menu3Save
KEY 1, "RdADCs"
KEY 2, "RdChN"
KEY 3, "TstTim"
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB ReadTheADCs
ON KEY(2) GOSUB ReadADCChannelN
ON KEY(3) GOSUB TestTime
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu3Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu3e
END IF
LOOP
Menu4:               'the dac test menu
Menu4Save = Menuk
Menu4e: Menuk = 40
MenuSave = Menu4Save
KEY 1, "LdDACs"
KEY 2, "LdADAC"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB LoadTheDACs
ON KEY(2) GOSUB LoadASingleDAC
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu4Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu4e
END IF
LOOP
Menu5:              'display tests here
Menu5Save = Menuk
Menu5e: Menuk = 50
MenuSave = Menu5Save
KEY 1, "Disply"
KEY 2, "Menu6"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB PutUpDisplay
ON KEY(2) GOSUB Menu6
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu5Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu5e
END IF
LOOP
Menu6: 'MA Servo tests here
Menu6Save = Menuk
Menu6e: Menuk = 60
MenuSave = Menu6Save
KEY 1, "TstMA0"
KEY 2, "TstMA1"
KEY 3, "TstMA2"
KEY 4, ""
KEY 5, "Open0 "
KEY 6, "Close0"
KEY 7, "Open1 "
KEY 8, "Close1"
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB TestMAServo0
ON KEY(2) GOSUB TestMAServo1
ON KEY(3) GOSUB TestMAServo2
ON KEY(5) GOSUB Open0
ON KEY(6) GOSUB Close0
ON KEY(7) GOSUB Open1
ON KEY(8) GOSUB Close1
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu6Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu6e
END IF
LOOP
Menu7:
Menu7Save = Menuk
Menu7e: Menuk = 70
MenuSave = Menu7Save
KEY 1, "V Focs"
KEY 2, "Dec Dr"
KEY 3, "Dec Cl"
KEY 4, "I Focs"
KEY 5, "Specfy"
KEY 6, "MotOff"
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB Menu12  'V focus tests
ON KEY(2) GOSUB Menu13  'Declination tests
ON KEY(3) GOSUB Menu14  'Dec Clamp tests
ON KEY(4) GOSUB Menu15  'I focus tests
ON KEY(5) GOSUB MotorTest
ON KEY(6) GOSUB TurnTheMotorsOff
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu7Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu7e
END IF
LOOP
Menu8:
Menu8Save = Menuk
Menu8e: Menuk = 80
MenuSave = Menu8Save
KEY 1, "PlsSv2" 'pulse MA Servo 2 as a pulse test
KEY 2, "StScan"
KEY 3, "StScnC"
KEY 4, "ComTst"
KEY 5, "PulsNs"
KEY 6, "PulsN1"
KEY 7, "WforBD"
KEY 8, "Chill"
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB PulseServo2
ON KEY(2) GOSUB TestStartScan
ON KEY(3) GOSUB TestStartScanContinuous
ON KEY(4) GOSUB CommunicationsTest
ON KEY(5) GOSUB TestPulsesChannelN
ON KEY(6) GOSUB OnePulseChannelN
ON KEY(7) GOSUB WaitForBlockDone
ON KEY(8) GOSUB TestChillerControl
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu8Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu8e
END IF
LOOP
Menu9:
Menu9Save = Menuk
Menu9e: Menuk = 90
MenuSave = Menu9Save
KEY 1, " RAOn "
KEY 2, "RAOff "
KEY 3, "RABack"
KEY 4, "RAFF"
KEY 5, "RAEx"
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB TurnRAOn
ON KEY(2) GOSUB TurnRAOff
ON KEY(3) GOSUB TurnRAReturn
ON KEY(4) GOSUB TurnRAFastForward
ON KEY(5) GOSUB RAExercise
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu9Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu9e
END IF
LOOP
Menu10:
Menu10Save = Menuk
Menu10e: Menuk = 100
MenuSave = Menu10Save
KEY 1, "RdAll"
KEY 2, "MovLim"
KEY 3, "Clear"
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB ReadTheLimits
ON KEY(2) GOSUB Menu11
ON KEY(3) GOSUB ClearTheScreen
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu10Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu10e
END IF
LOOP
Menu11:
Menu11Save = Menuk
Menu11e: Menuk = 110
MenuSave = Menu11Save
KEY 1, "V Lim "
KEY 2, "I Lim "
KEY 3, "RA Lim"
KEY 4, "DecLim"
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB MoveToVLimit
ON KEY(2) GOSUB MoveToILimit
ON KEY(3) GOSUB MoveToRALimit
ON KEY(4) GOSUB MoveToDECLimit
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu11Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu11e
END IF
LOOP
Menu12:
Menu12Save = Menuk
Menu12e: Menuk = 120
MenuSave = Menu12Save
KEY 1, "Exrciz"
KEY 2, "V +200"
KEY 3, "V -200"
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB TestMotor0
'ON KEY(2) GOSUB MotorZeroForward
'ON KEY(3) GOSUB MotorZeroReverse
ON KEY(4) GOSUB Dummy
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu12Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu12e
END IF
LOOP
Menu13:
Menu13Save = Menuk
Menu13e: Menuk = 130
MenuSave = Menu13Save
KEY 1, "Exrcz1"
KEY 2, "Menu14"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB TestMotor1
ON KEY(2) GOSUB Menu14
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu13Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu13e
END IF
LOOP
Menu14:
Menu14Save = Menuk
Menu14e: Menuk = 140
MenuSave = Menu14Save
KEY 1, "Exrcz2"
KEY 2, "ClmpOn"
KEY 3, "ClmpOf"
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB TestMotor2
ON KEY(2) GOSUB ClampOn
ON KEY(3) GOSUB ClampOff
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu14Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu14e
END IF
LOOP
Menu15:
Menu15Save = Menuk
Menu15e: Menuk = 150
MenuSave = Menu15Save
KEY 1, "Exrcz3"
KEY 2, ""
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB TestMotor3
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu15Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu15e
END IF
LOOP
Menu16:
Menu16Save = Menuk
Menu16e: Menuk = 160
MenuSave = Menu16Save
KEY 1, " Take "
KEY 2, " Arne "
KEY 3, "      "
KEY 4, "ScnSky"
KEY 5, "Expose"
KEY 6, "ScanIt"
KEY 7, "AdjRA"
KEY 8, "Follow"
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, "AutoF"
ON KEY(1) GOSUB Take
ON KEY(2) GOSUB Menu26
'ON KEY(3) GOSUB TakeLightFrame
ON KEY(4) GOSUB Dummy
ON KEY(5) GOSUB Dummy
ON KEY(6) GOSUB ScanIt  'scanit Long, scanit1 long/short
ON KEY(7) GOSUB AdjustRA
ON KEY(8) GOSUB Dummy
ON KEY(9) GOSUB BackSet
ON KEY(10) GOSUB AutoFocusRun
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu16Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu16e
END IF
LOOP
Menu17:
Menu17Save = Menuk
Menu17e: Menuk = 170
MenuSave = Menu17Save
KEY 1, "AllDay"                    'memory tests here
KEY 2, "TstBDn"                    'test block done
KEY 3, "TstScn"                    'test that a scan end sets block done
KEY 4, "NumMem"                    'number the memory cells for tese
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB AllDay
ON KEY(2) GOSUB TestBlockDone
ON KEY(3) GOSUB TestBlockDoneIsSetByScan
ON KEY(4) GOSUB NumberTheMemoryCells
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu17Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu17e
END IF
LOOP
Menu18:      'the set up menu
Menu18Save = Menuk
Menu18e: Menuk = 180
MenuSave = Menu18Save
KEY 1, "RASet "
KEY 2, ""
KEY 3, "RATest"
KEY 4, "Rob"
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB RASetup
ON KEY(3) GOSUB RASetup1
ON KEY(4) GOSUB RobTest
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu18Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu18e
END IF
LOOP
Menu19:   'some programs for TOM to run
Menu19Save = Menuk
Menu19e: Menuk = 190
MenuSave = Menu19Save
KEY 1, "TomScn"
KEY 2, "TFolow"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB TomSimpleScan
ON KEY(2) GOSUB TomFollow
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu19Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu19e
END IF
8 LOOP
Menu20:
Menu20Save = Menuk
Menu20e: Menuk = 200
MenuSave = Menu20Save
KEY 1, ""
KEY 2, "Menu21"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB Dummy
ON KEY(2) GOSUB Menu21
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu20Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu20e
END IF
LOOP
Menu21:
Menu21Save = Menuk
Menu21e: Menuk = 210
MenuSave = Menu21Save
KEY 1, ""
KEY 2, "Menu22"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB Dummy
ON KEY(2) GOSUB Menu22
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu21Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu21e
END IF
LOOP
Menu22:
Menu22Save = Menuk
Menu22e: Menuk = 220
MenuSave = Menu22Save
KEY 1, ""
KEY 2, "Menu23"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB Dummy
ON KEY(2) GOSUB Menu23
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu22Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu22e
END IF
LOOP
Menu23:
Menu23Save = Menuk
Menu23e: Menuk = 230
MenuSave = Menu23Save
KEY 1, ""
KEY 2, "Menu24"
KEY 3, ""
KEY 4, ""
KEY 5, ""
KEY 6, ""
KEY 7, ""
KEY 8, ""
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, ""
ON KEY(1) GOSUB Dummy
ON KEY(2) GOSUB Dummy
ON KEY(9) GOSUB BackSet
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu23Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu23e
END IF
LOOP
TurnKeysOn:
KEY(1) ON
KEY(2) ON
KEY(3) ON
KEY(4) ON
KEY(5) ON
KEY(6) ON
KEY(7) ON
KEY(8) ON
KEY(9) ON
KEY(10) ON
RETURN
BackSet:
GoBack = 1
RETURN

Menu25:
RETURN

' ************ Arne's menu
Menu26:
Menu26Save = Menuk
Menu26e: Menuk = 260
MenuSave = Menu26Save
KEY 1, "Park "
KEY 2, "IniPsn"
KEY 3, "GotoA"
KEY 4, "GotoR"
KEY 5, "Expose"
KEY 6, "ScanIt"
KEY 7, "Sched"
KEY 8, "Follow"
KEY 9, STR$(Menuk / 10) + " EX"
KEY 10, "AutoF"
ON KEY(1) GOSUB Dummy
ON KEY(2) GOSUB Dummy
ON KEY(3) GOSUB Dummy
ON KEY(4) GOSUB Dummy
ON KEY(5) GOSUB Dummy
ON KEY(6) GOSUB Dummy
ON KEY(7) GOSUB Dummy
ON KEY(8) GOSUB Dummy
ON KEY(9) GOSUB BackSet
ON KEY(10) GOSUB AutoFocusRun
GOSUB TurnKeysOn
KEY ON
DO UNTIL hfo = 1
GOSUB WorkLoop
IF GoBack = 1 THEN
GoBack = 0
CameBack = 1
Menuk = Menu26Save
RETURN
END IF
IF CameBack = 1 THEN
CameBack = 0
GOTO Menu26e
END IF
LOOP

PrintMessage:
GOSUB ClearLine
LOCATE Messloc%, 2
PRINT Message$
RETURN

ClearLine:
LOCATE Messloc%, 2
PRINT "                                                                              "
RETURN

SaveFiles:
RETURN

Dummy:
RETURN

WorkLoop:
RETURN

GetNumber:
GOSUB ClearLine
GOSUB PrintMessage
INPUT Number%
RETURN
GetLiteral:
GOSUB ClearLine
GOSUB PrintMessage
INPUT Literal$
RETURN
'***************************************************
'Test Subroutines in alphabetic order

AllDay:  'here we have the first full memory test
REDIM ax%(0 TO 16383)
REDIM bx%(0 TO 16383)
CLS
RANDOMIZE TIMER
size! = 3999'2047
INPUT "Enter Number of Blocks to Test "; blocks!
'blocks! = 7
total! = (size! + 1) * (blocks! + 1)
LOCATE 4, 1
errors! = 0
PRINT "Total Memory Tested = "; total!
FOR I = 0 TO size!
ax%(I) = INT(256 * RND)
IF ax%(I) > 255 THEN ax%(I) = 255
NEXT I
SOUND 400, 1
OUT &H300, &H90
OUT &H300, &H90    'reset MAR and enter cable test mode
'foor = INP(&H300)
LOCATE 8, 1
PRINT "After Set for Write Test, Status Register is "; HEX$(foor)
LOCATE 19, 1
PRINT "Size           Blocks      Errors         Written      Read"
FOR j = 0 TO blocks!
LOCATE 5, 1
PRINT j
FOR I = 0 TO size!
OUT &H301, ax%(I)
NEXT I
NEXT j
OUT &H300, &HA0      'clear test mode
OUT &H300, &H1      'reset MAR
foop = INP(&H300)
LOCATE 9, 1
PRINT "After Write Status Register is "; HEX$(foop)
FOR j = 0 TO blocks!
LOCATE 6, 1
PRINT j
FOR I = 0 TO size!
bx%(I) = INP(&H301)
NEXT I
FOR I = 0 TO size!
IF bx%(I) <> ax%(I) THEN
errors! = errors! + 1
LOCATE 20, 1
PRINT (size! + 1), j, errors!, ax%(I), bx%(I)
'INPUT foox
END IF
NEXT I
NEXT j
SOUND 1000, 1
RETURN

ClampOn:
Action% = 1 'move motor in specified direction
Motorx% = 2 'motor 2
Steps% = 250   'was 250 always more on than off to preserve clamp
Direction$ = "0" 'in towards limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
RETURN

ClampOff:
Action% = 1 'move motor in specified direction
Motorx% = 2 'motor 2
Steps% = 200   'was 200 steps
Direction$ = "1" 'out from limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
RETURN

InitTheRegisters:
j% = 0
CALL InitStamp(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

LoadASingleDAC:
Message$ = "Enter DAC Channel to Load"
GOSUB GetNumber
j% = Number%
CALL LoadDAC(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

LoadTheDACs:
j% = 40  'blinking status loading dacs
CALL Display(j%, N$(), a!(), d!(), c%(), v!(), p$())
j% = 16  'load all the dacs
CALL LoadDAC(j%, N$(), a!(), d!(), c%(), v!(), p$())
j% = 38  'done in status, put up ADC readings
CALL Display(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

MotorTest:
Action% = 1
Message$ = "Enter Motor To Test"
GOSUB GetNumber
Motorx% = Number%
Message$ = "Enter Steps To Take"
GOSUB GetNumber
Steps% = Number%
Message$ = "Enter Direction 0 = In  1 + Out"
GOSUB GetLiteral
Direction$ = Literal$
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB TurnTheMotorsOff
RETURN

MoveToVLimit:  'mover the V lens focus to its limit
Action% = 0'move to limit and out
Motorx% = 0'0 is the V motor
Steps% = 0'0 steps out from limit
Direction$ = "0"
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB TurnTheMotorsOff
RETURN

MoveToILimit:
Action% = 0'move to limit and out
Motorx% = 3' 3 is the I motor
Steps% = 0'0 steps out from limit
Direction$ = "0"
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB TurnTheMotorsOff
RETURN

MoveToRALimit:
GOSUB TurnRAReturn
k% = 0
DO UNTIL k% = 2
a$ = N$(34)
b$ = "6"
c$ = "0"
d$ = "?"
e$ = "?"
f$ = "?"
g$ = "?"
a$ = N$(35)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
LOCATE 20, 1
PRINT d$, e$, f$, g$
k% = ASC(g$) AND 2
LOCATE 21, 1
PRINT k%
LOOP
GOSUB TurnRAOff
RETURN

MoveToDECLimit:
GOSUB ClampOff
Action% = 0'move to limit and out
Motorx% = 1'1 is the RA motor
Steps% = 0'0 steps out from limit
Direction$ = "1"
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB ClampOn
GOSUB TurnTheMotorsOff
Decl$ = DecLimitSwitch$
RETURN

MoveDECRelative:  'enter with Steps% + 16.666 * degrees to move
GOSUB ClampOff
Steps% = Stepsx%
Action% = 1
Motorx% = 1
Direction$ = "1"
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB ClampOn
GOSUB TurnTheMotorsOff
RETURN

NumberTheMemoryCells:
'numbers the pixels in an image from 0 at the left of a line to 2062
'at the right end of a line
SOUND 400, 1
OUT &H300, &H90
OUT &H300, &H90    'reset MAR and enter cable test mode
FOR j = 0 TO 2062
FOR I = 0 TO 2062
a% = INT(I / 256)    'the high order byte
b% = I - 256 * a%       'the low order byte
OUT &H301, a%       ' write high byte
OUT &H301, b%        ' write low byte
NEXT I
LOCATE 20, 1
PRINT j
NEXT j
OUT &H300, &HA0      'clear test mode
OUT &H300, &H1      'reset MAR
SOUND 1000, 1
RETURN

PulseServo2:   'make test pulses at MAServo 2 which is unused
DO UNTIL GoBack = 1
p% = 23
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
LOOP
RETURN

PutUpDisplay:
j% = 0   'put up full display
CALL Display(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

ReadTheADCs:
j% = 37  'put up blinking status  Running ADC Scan
CALL Display(j%, N$(), a!(), d!(), c%(), v!(), p$())
a% = 0
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
j% = 0   'put up full display
CALL Display(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

ReadTheLimits:
hfo = 0
DO UNTIL GoBack = 1
a$ = N$(34)
b$ = "6"
c$ = "0"
d$ = "?"
e$ = "?"
f$ = "?"
g$ = "?"
a$ = N$(35)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
LOCATE 20, 1
PRINT d$, e$, f$, g$
LOOP
RETURN
Test:
j% = 0   'put up full display
CALL Display(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN



TestBlockDone:  'checks that the test block done pulse gets
'through and can be seen at the memory board
xxx1 = INP(&H300)
LOCATE 38, 1
PRINT "Before First clear, Status Register is "; HEX$(xxx1)
OUT &H300, &H1           'Clear the IRQ
xxx1 = INP(&H300)
LOCATE 39, 1
PRINT "At Start, Status Register is "; HEX$(xxx1)
p% = 16'set block done
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
xxx1 = INP(&H300)
LOCATE 40, 1
PRINT "After Block Done, Status Register is "; HEX$(xxx1)
OUT &H300, &H1
xxx1 = INP(&H300)
LOCATE 41, 1
PRINT "After OUT &H300,1, Status Register is "; HEX$(xxx1)
RETURN

TestBlockDoneIsSetByScan:   'tests that the block done from the scanner
'gets through and can be found in the status register of the memory board
LOCATE 37, 1
OUT &H300, 1         'clear the Block Done
xxx1 = INP(&H300)
PRINT "After Clear, Status Register is "; HEX$(xxx1)
LOCATE 38, 1
PRINT "Starting Scan"
p% = 4'start scan
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())

TestReset:
OUT &H300, &H0      'puts memory into test mode(I think)
OUT &H300, &H80
OUT &H300, &H90
OUT &H300, &H80
SOUND 400, 2
RETURN

TimesThrough = 0
xxx2 = 0
DO                             'Block done
xxx2 = INP(&H300)
LOCATE 39, 1
PRINT "Status is "; HEX$(xxx2), xxx2 AND 128
CALL Timex(1)
LOCATE 40, 1
TimesThrough = TimesThrough + 1
PRINT TimesThrough
'IF TimesThrough > 9 THEN
'p% = 16'set block done
'CALL PulseIt(p%, n$(), a!(), d!(), c%(), v!(), p$())
'END IF
xxx4 = xxx2 AND 128
LOOP UNTIL xxx4 = 128
LOCATE 41, 1
PRINT "Scan Is Done"
RETURN

TestMAServo0:
DO UNTIL GoBack = 1
GOSUB Close0
CALL Timex(1)
GOSUB Open0
CALL Timex(1)
LOOP
RETURN

Open0:
Shutter0OpenTime! = TIMER
ZeroDateStart$ = DATE$
ZeroTimeStart$ = TIME$
IF DarkFrameFlag = 1 THEN
j% = 0  'always close
k% = 40' if phased right string polarity
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
ELSE
j% = 0
k% = 255' if strings right on shutter
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
END IF
RETURN

Close0:
Shutter0CloseTime! = TIMER
ZeroTimeOpen! = Shutter0CloseTime! - Shutter0OpenTime!
IF ZeroTimeOpen! < 0 THEN ZeroTimeOpen! = ZeroTimeOpen! + 86400
IF DarkFrameFlag = 1 THEN
j% = 0  'always close
k% = 40' if phased right string polarity
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
ELSE
j% = 0
k% = 40' if phased right string polarity
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
END IF
RETURN

TestMAServo1:
DO UNTIL GoBack = 1
GOSUB Close1
CALL Timex(1)
GOSUB Open1
CALL Timex(1)
LOOP
RETURN

Close1:
Shutter1CloseTime! = TIMER
OneTimeOpen! = Shutter1CloseTime! - Shutter1OpenTime!
IF OneTimeOpen! < 0 THEN OneTimeOpen! = OneTimeOpen! + 86400
IF DarkFrameFlag = 1 THEN
j% = 1
k% = 30
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
ELSE
j% = 1
k% = 30
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
END IF
RETURN

Open1:
Shutter1OpenTime! = TIMER
OneDateStart$ = DATE$
OneTimeStart$ = TIME$
IF DarkFrameFlag = 1 THEN
j% = 1 'if we dont open, we close
k% = 30
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
ELSE
j% = 1
k% = 255
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
END IF
RETURN

TestMAServo2:
DO UNTIL GoBack = 1
j% = 2
k% = 255
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
CALL Timex(1)
j% = 2
k% = 64
CALL MAServo(j%, k%, N$(), a!(), d!(), c%(), v!(), p$())
CALL Timex(1)
LOOP
RETURN

TestMotor0:
DO UNTIL GoBack = 1
Action% = 1 'move motor in specified direction
Motorx% = 0 'motor 0
Steps% = 500   '100 steps
Direction$ = "0" 'in towards limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
Action% = 1 'move motor in specified direction
Motorx% = 0 'motor 0
Steps% = 500   '100 steps
Direction$ = "1" 'out from limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
LOOP
GOSUB TurnTheMotorsOff
RETURN

TestMotor1:       'the declination motor
GOSUB ClampOff
DO UNTIL GoBack = 1
Action% = 1 'move motor in specified direction
Motorx% = 1 'motor 1
Steps% = 500   '100 steps
Direction$ = "1" 'awau from limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
Action% = 1 'move motor in specified direction
Motorx% = 1 'motor 1
Steps% = 500   '100 steps
Direction$ = "0" 'in to limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
LOOP
GOSUB ClampOn
GOSUB TurnTheMotorsOff
RETURN

TestMotor2:   'the clamp motor
DO UNTIL GoBack = 1
Action% = 1 'move motor in specified direction
Motorx% = 2 'motor 2
Steps% = 100   '100 steps
Direction$ = "0" 'in towards limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
Action% = 1 'move motor in specified direction
Motorx% = 2 'motor 2
Steps% = 100   '100 steps
Direction$ = "1" 'out from limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
LOOP
GOSUB TurnTheMotorsOff
RETURN

TestMotor3:
DO UNTIL GoBack = 1
Action% = 1 'move motor in specified direction
Motorx% = 3 'motor 3
Steps% = 500   '100 steps
Direction$ = "0" 'in towards limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
Action% = 1 'move motor in specified direction
Motorx% = 3 'motor 3
Steps% = 500   '100 steps
Direction$ = "1" 'out from limit
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
LOOP
GOSUB TurnTheMotorsOff
RETURN
 
TestStartScan:
p% = 4'start scan
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN
TestStartScanContinuous:
DO UNTIL GoBack = 1
p% = 4'start scan
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
LOOP
RETURN
OnePulseChannelN:
LOCATE 40, 1
INPUT "Enter The Channel to Pulse"; p%
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN
TestPulsesChannelN:
LOCATE 40, 1
INPUT "Enter The Channel to Pulse"; p%
DO UNTIL GoBack = 1
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
LOOP
RETURN
TurnRAOn:
j% = 1
RAOnTime! = TIMER
CALL RAMove(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

TurnRAOff:
j% = 0
CALL RAMove(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

TurnRAReturn:
j% = 2
CALL RAMove(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

TurnRAFastForward:
j% = 3
CALL RAMove(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

TurnTheMotorsOff:
Action% = 2
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
RETURN


'end of alphabetic test subroutines
'*******************************************************
'Start Working Subroutines

OneWord:
a1& = INP(&H301)
a2% = INP(&H301)
IF a1& > 127 THEN
element% = -65536 + 256 * a1& + a2%
ELSE
element% = 256 * a1& + a2%
END IF
RETURN

ReadReset:
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
SOUND 400, 2
RETURN

SaveIt:
LOCATE 1, 1
GOSUB LogEntry
GOSUB ReadReset
GOSUB RobTest
SOUND 400, 2
RETURN

TakeFrame:
CLS
GOSUB Close0
GOSUB Close1
GOSUB InitTheRegisters              'reset everything
LOCATE 37, 1
PRINT "Close the Shutters"
'close shutters to make sure
GOSUB TurnRAOff                     'make sure RA is off
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
GOSUB TurnRAOn
Timexxx! = TIMER
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start clearing scan
LOCATE 38, 1
PRINT "Start the Clearing Scan"
GOSUB WaitForBlockDone
'wait for done
LOCATE 39, 1
PRINT "Clear Complete, Start Exposure"
GOSUB Open0
GOSUB Open1
CALL Timex(ExposureTime#)
'wait exposure time
GOSUB Close0
GOSUB Close1
LOCATE 40, 1
PRINT "Exposure Complete, Start Read Out Scan"
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
GOSUB WaitForBlockDone
' &&&&&
'p% = 5
'CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'p% = 7
'CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
GOSUB TurnRAOff
Timexxy = TIMER
IF Timexxy < Timexxx THEN Timexxy = Timexxy + 86400
Timexxz# = (Timexxy - Timexxx) / 4
GOSUB RASetup
GOSUB TurnRAOff
LOCATE 41, 1
PRINT "Read Out Complete, Dark Exposure Done"
SOUND 400, 10
PRINT "Turning Off RA"
GOSUB SaveIt
LOCATE 41, 1
PRINT "Save Complete, Exposure In Memory"
GOSUB Announce
RETURN

Take:
INPUT "Light(1) or Dark(0) frame: "; ShutterOpen
INPUT "Enter exposure time(sec),0=bias: "; ExposureTime#
IF ShutterOpen = 0 THEN DarkFrameFlag = 1 ELSE DarkFrameFlag = 0
GOSUB TakeFrame
RETURN

TakeDarkFrame:
ShutterOpen = 0
DarkFrameFlag = 1
GOSUB TakeFrame
RETURN
TakeLightFrame:
ShutterOpen = 1
DarkFrameFlag = 0
GOSUB TakeFrame
RETURN

TemperaturePlot:
VoltsDAC! = 4
VoltsDACMinimum! = -5
SCREEN 9, 1
LOCATE 1, 1
PRINT "20 C"
LOCATE 7, 1
PRINT "10 C"
LOCATE 13, 1
PRINT "0 C"
LOCATE 19, 1
PRINT "-10 C"
LOCATE 25, 1
PRINT "-20 C"
LOCATE 31, 1
PRINT "-30 C"

a% = 1
x% = 40
COLOR 13, 1
LOCATE 34, 1
PRINT "Temp CCD 0"
COLOR 9, 1
LOCATE 35, 1
PRINT "Curr CCD 0"
COLOR 14, 1
LOCATE 36, 1
PRINT "Temp CCD 1"
LOCATE 37, 1
COLOR 11, 1
PRINT "Curr CCD 1"
LOCATE 38, 1
COLOR 5, 1
PRINT "Water Temp"
Tloop = 0
DO UNTIL GoBack = 1
b% = 24
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 18 'read temp 0
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 19 'read temp 1
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 25
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 26
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 0
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 6
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 14
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
z% = -INT(4.8 * a!(18, 3))  'temp 0
y% = -INT(4.8 * a!(19, 3))  'temp 1
v% = -INT(48 * a!(25, 3))   'cur 0
w% = -INT(48 * a!(26, 3))    'cur 1
u% = -INT(4.8 * a!(0, 3))    'water temp
LOCATE 34, 12
PRINT USING "####.###"; -(z% / 4.8)
LOCATE 37, 12
PRINT USING "####.###"; -(v% / 48)
LOCATE 36, 12
PRINT USING "####.###"; -(y% / 4.8)
LOCATE 35, 12
PRINT USING "####.###"; -(w% / 48)
LOCATE 38, 12
PRINT USING "####.###"; a!(0, 3)
PSET (x%, z% + 104), 13    'temp 0
PSET (x%, y% + 104), 14    'temp 1
PSET (x%, v% + 240), 11     'cur 1
PSET (x%, w% + 240), 9    'cur 0
PSET (x%, u% + 200), 5    'was 104
LOCATE 10, 10
PRINT u% + 200
LOCATE 39, 1
COLOR 9, 1
PRINT "Command 0  "; ((a!(6, 3) * 3.54) - 4.16)'(a!(6, 3) / 3) * 100 - 273
LOCATE 40, 1
COLOR 11, 1
PRINT "Command 1  "; ((a!(14, 3) * 3.54) - 4.16)'(a!(14, 3) / 3) * 100 - 273
CALL Timex(5)
x% = x% + 1
GOSUB LogEntry
Tloop = Tloop + 1
LOCATE 20, 20
PRINT Tloop, VoltsDAC!, Number2%
IF Tloop MOD 3 = 0 THEN
IF VoltsDAC! > VoltsDACMinimum! THEN
IF RampUp = 1 THEN
VoltsDAC! = VoltsDAC! - .1
GOSUB SetTemperatureCommands
END IF
END IF
END IF
LOOP
'SCREEN 0, 0
RETURN
SetTemperatureCommands:
GOSUB ComputeDACSetting1
ixx = 14
GOSUB LoadADAC1
ixx = 15
GOSUB LoadADAC1
RETURN

WaitForBlockDone:
Countxx = 0
xxx4 = 0
xxx2 = 0
CALL Timex(.1) '? make sure we don't get there too fast??
DO
xxx2 = INP(&H300)
xxx4 = xxx2 AND 128

CALL Timex(1)
Countxx = Countxx + 1
LOCATE 3, 70
PRINT Countxx
LOOP UNTIL (xxx4 = 128) OR (Countxx > 60)
CALL Timex(.2)  'this takes care of the present prom set which puts out
'a block done while a line or two is still reading out
RETURN


TakeFrame1:
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
GOSUB Open0
GOSUB Open1
LOCATE 40, 1
PRINT "Opening the Shutters"
CALL Timex(ExposureTime#)
'wait exposure time
GOSUB Close0
GOSUB Close1
LOCATE 40, 1
PRINT "Closing the Shutters"
CALL Timex(2)
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Read Out Scan"
GOSUB WaitForBlockDone
SOUND 400, 10
'wait for done
LOCATE 40, 1
PRINT "Read Out Scan Completed"
RETURN

SaveIt1:
FileNumber = FileNumber + 1
LOCATE 1, 1
filenamea$ = "HV" + Sub$ + RIGHT$(STR$(FileNumber), 2) + ".fits"
filenameb$ = "HI" + Sub$ + RIGHT$(STR$(FileNumber), 2) + ".fits"
shellit$ = "downfits " + filenamea$ + " " + filenameb$
GOSUB LogEntry
GOSUB ReadReset
GOSUB RobTest1
SOUND 400, 2
LOCATE 40, 1
'PRINT "File Set Saved"
RETURN

Test17:
INPUT "enter value for timex", temp#
SOUND 200, 5
CALL Timex(temp#)
SOUND 400, 5
RETURN

FollowItRaw:
FileNumber = FileNumber - 1
Sub$ = "A"
PadTime# = PadTime# - 52
IF PadTime# < 1 THEN PadTime# = 1
IF ira = 1 THEN
GOSUB RASetup
GOSUB TurnRAFastForward
CALL Timex(4)   'to remove backlash from the rewind
END IF
GOSUB TurnRAOn

'init first frame of follow
GOSUB Close0 'make sure shutters are closed
GOSUB Close1
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4                 ' &&3&&
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start clearing scan
LOCATE 40, 1
PRINT "Starting Clearing Scan - Waiting for Block Done           "
GOSUB WaitForBlockDone
GOSUB Open0
GOSUB Open1
start$ = TIME$
GOSUB TurnRAOn
LOCATE 40, 1
PRINT "Taking First Exposure                                        "
LOCATE 38, 1
PRINT Sub$; "                                                   "
LOCATE 39, 1
PRINT FileNumber; "                                                "
GOSUB SaveIt1
CALL Timex(PadTime#)
GOSUB Close0
GOSUB Close1
End$ = TIME$
FileNumber = FileNumber - 1

DO UNTIL FileNumber >= LastFile
FileNumber = FileNumber + 1
Sub$ = "A"
Secs = VAL(MID$(start$, 7, 2))
Mins = VAL(MID$(start$, 4, 2))
Hr1 = VAL(MID$(start$, 1, 2))
stime = (Mins * 60!) + Secs
Secs = VAL(MID$(End$, 7, 2))
Mins = VAL(MID$(End$, 4, 2))
Hr2 = VAL(MID$(End$, 1, 2))
etime = (Mins * 60!) + Secs
IF Hr2 = Hr1 THEN exptime = etime - stime ELSE exptime = etime + 3600! - stime

GOSUB InitTheRegisters 'this fixes the problem that without it &&1&&
'the start scan pulse does not get sent.  I don't know why yet.
'note that it worked the first time around at &&3&&
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4    'start scan  &&2&&
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start read out of data to memory card
LOCATE 40, 1
PRINT "Reading Exposure " + Sub$ + " to Memory - Waiting for Block Done"
GOSUB WaitForBlockDone

LOCATE 38, 1
PRINT Sub$ + "                                                   "
LOCATE 39, 1
PRINT " " + STR$(FileNumber) + "                                                "

GOSUB Open0
GOSUB Open1
start$ = TIME$
FileNumber = FileNumber - 1
'start read out of data from memory card
LOCATE 40, 1
PRINT "Transferring Exposure " + Sub$ + " to Disk                      "
GOSUB SaveIt1
CALL Timex(PadTime#)
GOSUB Close0
GOSUB Close1
End$ = TIME$
BEEP

LOOP

IF irb = 1 THEN
GOSUB TurnRAOff
GOSUB RASetup
END IF

LOCATE 40, 1
PRINT "Run Completed                                  "
SOUND 400, 10
GOSUB Announce
RETURN

ScanIt:
LOCATE 39, 1
INPUT "Enter Starting File Number "; FileNumber
FileNumber = FileNumber - 1
INPUT "Enter Number of Last File "; LastFile
FirstTimex = 0
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Clearing Scan                           "
GOSUB WaitForBlockDone

DO UNTIL FileNumber = LastFile
IF FileNumber MOD 10 = 0 THEN GOSUB RASetup
TimeOn! = TIMER
GOSUB TurnRAOn
CALL Timex(40)   'to remove backlash from the rewind
LOCATE 36, 1
PRINT "File Number is "; FileNumber + 1; "                    "
'take successive pictures as fast as possible without moving in DEC.
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
ExposureStart! = TIMER
IF FileNumber MOD 10 = 0 THEN
ELSE
GOSUB Open0
GOSUB Open1
END IF
LOCATE 40, 1
PRINT "Opening the Shutters and Reading Out Last Event"
IF FirstTimex = 1 THEN
GOSUB SaveIt1'write out the last event while exposing
ELSE
CALL Timex(100)
END IF
' Shorten the Exposure??CALL Timex(100)  ' ***** was 100 to get aprox 200 sec exp.
FirstTimex = 1
ExposureEnd! = TIMER
Exposure! = ExposureEnd! - ExposureStart!
LOCATE 39, 1
PRINT "Exposure is "; Exposure!
GOSUB Close0
GOSUB Close1
LOCATE 40, 1
PRINT "Closing the Shutters                           "
CALL Timex(1)
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
ScanStart! = TIMER
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Read Out Scan                         "
GOSUB WaitForBlockDone
ScanEndx! = TIMER
ScanTime! = ScanEndx! - ScanStart!
LOCATE 37, 1
PRINT ScanTime!, ScanEndx!, ScanStart!
LOCATE 38, 1
PRINT "Scan Time is "; ScanTime!; "                      "
SOUND 400, 10
'wait for done
LOCATE 40, 1
PRINT "Read Out Scan Completed                        "
TimeOff! = TIMER
GOSUB TurnRAOff
GOSUB RASetup
LOOP

LOCATE 40, 1
PRINT "Run Completed                                  "
SOUND 400, 10
GOSUB Announce
RETURN

AutoFocusRun:
GOSUB RASetup
'take 6 exposures on same frame stopping the RA drive 10 seconds between 1-2,
'2-3, ... and 20 seconds between 5-6.
CLS

LOCATE 37, 1
INPUT "Enter V Focus Starting Position "; VStart
LOCATE 38, 1
INPUT "Enter I Focus Starting Position "; IStart
LOCATE 39, 1
INPUT "Enter Focus Step Size "; FStep


GOSUB MoveToVLimit
GOSUB MoveToILimit
Action% = 1
Direction$ = "1"
Steps% = VStart
Motorx% = 0
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB TurnTheMotorsOff
Action% = 1
Direction$ = "1"
Steps% = IStart
Motorx% = 3
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB TurnTheMotorsOff
'move the focus to the starting positions

GOSUB Close0
GOSUB Close1
GOSUB InitTheRegisters              'reset everything
LOCATE 37, 1
PRINT "Close the Shutters"
'close shutters to make sure
GOSUB TurnRAOff                     'make sure RA is off
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
GOSUB TurnRAOn
Timexxx! = TIMER
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start clearing scan
LOCATE 38, 1
PRINT "Start the Clearing Scan"
GOSUB WaitForBlockDone
'wait for done
LOCATE 39, 1
PRINT "Clear Complete, Start Exposure"

FOR ii = 1 TO 5
GOSUB TurnRAOn
GOSUB Open0
GOSUB Open1
LOCATE 41, 1
PRINT "Open                                                          "
CALL Timex(FocusTime#)
'wait exposure time
GOSUB Close0
GOSUB Close1
LOCATE 41, 1
PRINT "Close and RA Off                                              "
GOSUB TurnRAOff
CALL Timex(OffTime#)
GOSUB MoveToVLimit
GOSUB MoveToILimit
Action% = 1
Steps% = VStart + ii * FStep
Motorx% = 0
Direction$ = "1"
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB TurnTheMotorsOff
Action% = 1
Direction$ = "1"
Steps% = IStart + ii * FStep
Motorx% = 3
CALL Motor(Action%, Motorx%, Steps%, Direction$, LastPosition$, N$())
GOSUB TurnTheMotorsOff
NEXT ii
CALL Timex(OffTime#)
GOSUB TurnRAOn
GOSUB Open0
GOSUB Open1
LOCATE 41, 1
PRINT "Open                                                          "
CALL Timex(FocusTime#)
'wait exposure time
GOSUB Close0
GOSUB Close1
GOSUB TurnRAOff


LOCATE 40, 1
PRINT "Exposure Complete, Start Read Out Scan"
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
GOSUB WaitForBlockDone
GOSUB TurnRAOff
Timexxy = TIMER
IF Timexxy < Timexxx THEN Timexxy = Timexxy + 86400
Timexxz# = (Timexxy - Timexxx) / 4
GOSUB RASetup
GOSUB TurnRAOff
LOCATE 41, 1
PRINT "Read Out Complete, Dark Exposure Done"
SOUND 400, 10
PRINT "Turning Off RA"
GOSUB SaveIt
LOCATE 41, 1
PRINT "Save Complete, Exposure In Memory"
GOSUB RASetup
GOSUB Announce
RETURN

RASetup:
GoBack = 0
TimesAround = 1
'DO UNTIL GoBack = 1
LOCATE 40, 1
PRINT "Seeking RA Home Position                       "
GOSUB MoveToRALimit
LOCATE 40, 1
PRINT "Home Found Moving Out to Starting Position     "
GOSUB TurnRAFastForward
CALL Timex(1)
LOCATE 40, 1
PRINT "RA In Starting Position                        "
GOSUB TurnRAOff
SOUND 400, 1
GOSUB TurnRAOff
TimesAround = TimesAround + 1
'LOCATE 39, 1
'PRINT "TimesAround "; TimesAround
'LOOP
RETURN

RASetup1:
GoBack = 0
TimesAround = 1
DO UNTIL GoBack = 1
LOCATE 40, 1
PRINT "Seeking RA Home Position                       "
GOSUB MoveToRALimit
LOCATE 40, 1
PRINT "Home Found Moving Out to Starting Position     "
GOSUB TurnRAFastForward
CALL Timex(30)
LOCATE 40, 1
PRINT "RA In Starting Position                        "
GOSUB TurnRAOff
SOUND 400, 1
GOSUB TurnRAOff
TimesAround = TimesAround + 1
LOCATE 39, 1
PRINT "TimesAround "; TimesAround
LOOP
RETURN

Announce:
FOR I = 1 TO 3
SOUND 400, 1
SOUND 400, 1
SOUND 440, 2
SOUND 400, 1
SOUND 400, 1
SOUND 360, 2
SOUND 400, 1
SOUND 400, 1
SOUND 440, 2
SOUND 400, 1
SOUND 400, 1
SOUND 500, 2
SOUND 400, 1
SOUND 400, 1
SOUND 320, 2
NEXT I
RETURN

ExposeIt:
' &&&&& here is a program that runs without hanging.  It is the program used
'to take the short long short long exposures

CLS
LOCATE 36, 1
INPUT "Enter Starting File Number "; FileNumber
FileNumber = FileNumber - 1
INPUT "Enter Number of Last File "; LastFile
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Clearing Scan                           "
GOSUB WaitForBlockDone

DO UNTIL FileNumber >= LastFile
FileNumber = FileNumber + 1
IF FileNumber MOD 5 = 0 THEN
DarkFrameFlag = 1
ELSE
DarkFrameFlag = 0
END IF
GOSUB TurnRAFastForward
CALL Timex(5)   'to remove backlash from the rewind
GOSUB TurnRAOn
GOSUB Open0
GOSUB Open1
LOCATE 40, 1
PRINT "Taking Short Exposure                                        "
CALL Timex(15) 'take a short second exposure
GOSUB Close0
GOSUB Close1
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Short Exposure Read Out Scan                         "
GOSUB WaitForBlockDone
LOCATE 40, 1
PRINT "Read Out Short Exposure Scan Completed                        "
GOSUB Open0
GOSUB Open1
LOCATE 40, 1
PRINT "Transferring Short Exposure While Making Long Exposure       "
Sub$ = "A"
LOCATE 38, 1
PRINT Sub$; "                                                   "
LOCATE 39, 1
PRINT FileNumber; "                                                "
FileNumber = FileNumber - 1
GOSUB SaveIt1
CALL Timex(70)
GOSUB Close0
GOSUB Close1
LOCATE 40, 1
PRINT "Exposure and Transfer Complete, Reading out Long Exposure        "
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
GOSUB WaitForBlockDone
LOCATE 40, 1
PRINT "Transferring Long Exposure to Memory                               "
Sub$ = "B"
LOCATE 38, 1
PRINT Sub$; "                                                       "
LOCATE 39, 1
PRINT FileNumber; "                                                   "
FileNumber = FileNumber - 1
GOSUB SaveIt1

GOSUB Open0
GOSUB Open1
LOCATE 40, 1
PRINT "Taking Short Exposure                                        "
CALL Timex(15) 'take a 10 second exposure
GOSUB Close0
GOSUB Close1
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Short Exposure Read Out Scan                         "
GOSUB WaitForBlockDone
LOCATE 40, 1
PRINT "Read Out 10 Second Exposure Scan Completed                        "
GOSUB Open0
GOSUB Open1
LOCATE 40, 1
PRINT "Transferring 10 Second Exposure While Making Long Exposure       "
Sub$ = "C"
LOCATE 38, 1
PRINT Sub$; "                                                   "
LOCATE 39, 1
PRINT FileNumber; "                                                     "
FileNumber = FileNumber - 1
GOSUB SaveIt1
CALL Timex(70)
GOSUB Close0
GOSUB Close1
LOCATE 40, 1
PRINT "Exposure and Transfer Complete, Reading out Long Exposure        "
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
GOSUB WaitForBlockDone
LOCATE 40, 1
PRINT "Transferring Long Exposure to Memory                             "
Sub$ = "D"
LOCATE 38, 1
PRINT Sub$; "                                                "
LOCATE 39, 1
PRINT FileNumber; "                                                         "
FileNumber = FileNumber - 1
GOSUB SaveIt1

GOSUB TurnRAOff
GOSUB RASetup

LOOP

LOCATE 40, 1
PRINT "Run Completed                                  "
SOUND 400, 10
GOSUB Announce
RETURN

ScanIt1:
LOCATE 39, 1
INPUT "Enter Starting File Number "; FileNumber
FileNumber = FileNumber - 1
INPUT "Enter Number of Last File "; LastFile
FirstTimex = 0
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Clearing Scan                           "
GOSUB WaitForBlockDone

DO UNTIL FileNumber >= LastFile
IF FileNumber MOD 10 = 0 THEN GOSUB RASetup
TimeOn! = TIMER
GOSUB TurnRAOn
CALL Timex(40)   'to remove backlash from the rewind
LOCATE 36, 1
PRINT "File Number is "; FileNumber + 1; "                    "
'take successive pictures as fast as possible without moving in DEC.
'reset for fast exposure
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
GOSUB Open0
GOSUB Open1
CALL Timex(20)  'for short exposure
GOSUB Close0
GOSUB Close1
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
GOSUB WaitForBlockDone
'now do long exposure and read out
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
ExposureStart! = TIMER
GOSUB Open0
GOSUB Open1
LOCATE 40, 1
PRINT "Opening the Shutters and Reading Out Last Event"
IF FirstTimex = 1 THEN
GOSUB SaveIt1'write out the last event while exposing
ELSE
CALL Timex(100)
END IF
CALL Timex(100)  'to get aprox 200 sec exp.
FirstTimex = 1
ExposureEnd! = TIMER
Exposure! = ExposureEnd! - ExposureStart!
LOCATE 39, 1
PRINT "Exposure is "; Exposure!
GOSUB Close0
GOSUB Close1
LOCATE 40, 1
PRINT "Closing the Shutters                           "
CALL Timex(1)
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
ScanStart! = TIMER
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
LOCATE 40, 1
PRINT "Starting Read Out Scan                         "
GOSUB WaitForBlockDone
ScanEndx! = TIMER
ScanTime! = ScanEndx! - ScanStart!
LOCATE 37, 1
PRINT ScanTime!, ScanEndx!, ScanStart!
LOCATE 38, 1
PRINT "Scan Time is "; ScanTime!; "                      "
SOUND 400, 10
'wait for done
LOCATE 40, 1
PRINT "Read Out Scan Completed                        "
TimeOff! = TIMER
GOSUB TurnRAOff
IF TimeOff! < TimeOn! THEN TimeOff! = TimeOff! + 86400
TimeToReturn# = (TimeOff! - TimeOn!) / 4
GOSUB TurnRAReturn
LOCATE 40, 1
PRINT "Rewinding the RA Drive                         "
CALL Timex(TimeToReturn#)
GOSUB TurnRAOff
LOCATE 40, 1
GOSUB TurnRAOff
GOSUB SaveIt1   'to read out the second exposure
LOOP

LOCATE 40, 1
PRINT "Run Completed                                  "
SOUND 400, 10
GOSUB Announce
RETURN

CommunicationsTest:
DO UNTIL GoBack = 1
a$ = "B"
b$ = "8"
c$ = "2"
d$ = RIGHT$(STR$(RND * 9), 1)
e$ = RIGHT$(STR$(RND * 9), 1)
f$ = RIGHT$(STR$(RND * 9), 1)
g$ = RIGHT$(STR$(RND * 9), 1)
H$ = "0"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
INPUT foox
LOOP
RETURN

ReadADCChannelN:
'when we get to it
RETURN

TakeBiasFrame:
ExposureTimeSave# = ExposureTime#
ExposureTime# = 1
GOSUB TakeDarkFrame
ExposureTime# = ExposureTimeSave#
RETURN

RAExercise:
DO UNTIL GoBack = 1
GOSUB TurnRAOn
CALL Timex(30)
GOSUB TurnRAOff
GOSUB MoveToRALimit
GOSUB TurnRAOff
GOSUB TurnRAFastForward
CALL Timex(20)
GOSUB TurnRAOff
GOSUB MoveToRALimit
BEEP
CALL Timex(5)
LOOP
RETURN

TurnChillerOn:
j% = 1
CALL LoadPowerControlRegister(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN

TurnChillerOff:
j% = 0
CALL LoadPowerControlRegister(j%, N$(), a!(), d!(), c%(), v!(), p$())
RETURN


TestChillerControl:
DO UNTIL GoBack = 1
GOSUB TurnChillerOn
CALL Timex(2)
GOSUB TurnChillerOff
CALL Timex(2)
BEEP
LOOP
RETURN

LogEntry:
a% = 1
b% = 18 'read temp 0
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 19 'read temp 1
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 0'read water temp
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())
b% = 1'read VCO Temp
CALL ReadADC(a%, b%, N$(), a!(), d!(), c%(), v!(), p$())

LOCATE 1, 1
PRINT FileNumber, DATE$, TIME$
LOCATE 2, 1
PRINT USING "Camera 0 ####.#  Camera 1 ####.#  Water ####.#  VCO ####.#"; a!(18, 3); a!(19, 3); a!(0, 3); a!(1, 3)
IF a!(1, 3) - a!(0, 3) < 10 OR a!(0, 3) > 20 THEN GOSUB TurnChillerOn: ChillerStatus = 1
IF a!(1, 3) - a!(0, 3) > 10 OR a!(0, 3) < 20 THEN GOSUB TurnChillerOff: ChillerStatus = 0
IF ChillerStatus = 1 THEN
LOCATE 3, 1
PRINT "Chiller Is On"
ELSE
LOCATE 3, 1
PRINT "Chiller Is Off"
END IF
RETURN

TestLogEntry:
CLS
RETURN
ClearTheScreen:
CLS
RETURN


ReadOut:
GOSUB InitTheRegisters 'this fixes the problem that without it &&1&&
'the start scan pulse does not get sent.  I don't know why yet.
'note that it worked the first time around at &&3&&
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4                 ' &&3&&
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
GOSUB WaitForBlockDone
RETURN


TestIt:
CLS
FileNumber = 1
LastFile = 2
DO UNTIL FileNumber >= LastFile
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start clearing scan
LOCATE 40, 1
PRINT "Starting Clearing Scan - Waiting for Block Done           "
GOSUB WaitForBlockDone

FOR I = 1 TO 2 'declination steps - secondary loop
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4    'start scan
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start read out of data to memory card
LOCATE 40, 1
PRINT "Reading Exposure " + Sub$ + " to Memory - Waiting for Block Done"
GOSUB WaitForBlockDone
NEXT I

GOSUB TurnRAOff
GOSUB RASetup
FileNumber = FileNumber + 1
LOOP

LOCATE 40, 1
PRINT "Run Completed                                  "
SOUND 400, 10
GOSUB Announce
RETURN
AdjustRA:
GOSUB RASetup
'take 6 exposures on same frame stopping the RA drive 10 seconds between 1-2,
'2-3, ... and 20 seconds between 5-6.  Hold focus position but adjust the
'RA drive clock
CLS

LOCATE 37, 1
INPUT "Enter RA drive most negative value "; RAStart
LOCATE 38, 1
INPUT "Enter RA drive step "; RAStep

GOSUB Close0
GOSUB Close1
GOSUB InitTheRegisters              'reset everything
LOCATE 37, 1
PRINT "Close the Shutters"
'close shutters to make sure
GOSUB TurnRAOff                     'make sure RA is off
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
GOSUB TurnRAOn
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start clearing scan
LOCATE 38, 1
PRINT "Start the Clearing Scan"
GOSUB WaitForBlockDone
'wait for done
LOCATE 39, 1
PRINT "Clear Complete, Start Exposure"
'int the RA drive voltage
ixx = 12
VoltsDAC! = RAStart
GOSUB ComputeDACSetting1
GOSUB LoadADAC1

FOR ii = 1 TO 5
GOSUB TurnRAOn
GOSUB Open0
GOSUB Open1
LOCATE 41, 1
PRINT "Open                                                          "
CALL Timex(RATimeOn#)
'wait exposure time
GOSUB Close0
GOSUB Close1
LOCATE 41, 1
PRINT "Close and RA Off                                              "
GOSUB TurnRAOff
'here we change the RA drive value
ixx = 12   'the RA dac address
VoltsDAC! = RAStart + (ii * RAStep)
LOCATE 5, 70
PRINT VoltsDAC!
GOSUB ComputeDACSetting1
GOSUB LoadADAC1
CALL Timex(RATimeOff#)
NEXT ii

GOSUB TurnRAOn
GOSUB Open0
GOSUB Open1
LOCATE 41, 1
PRINT "Open                                                          "
CALL Timex(RATimeOn#)
CALL Timex(RATimeOn#)
'wait exposure time
GOSUB Close0
GOSUB Close1
GOSUB TurnRAOff


LOCATE 40, 1
PRINT "Exposure Complete, Start Read Out Scan"
OUT &H300, 0                        'general reset the memory
p% = 17      'reset the mar
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
p% = 4
CALL PulseIt(p%, N$(), a!(), d!(), c%(), v!(), p$())
'start scan
GOSUB WaitForBlockDone
GOSUB TurnRAOff
GOSUB RASetup
GOSUB TurnRAOff
LOCATE 41, 1
PRINT "Read Out Complete, Dark Exposure Done"
SOUND 400, 10
PRINT "Turning Off RA"
GOSUB SaveIt
LOCATE 41, 1
PRINT "Save Complete, Exposure In Memory"
GOSUB RASetup
GOSUB Announce
RETURN

LoadADAC1:
a$ = N$(34)
H$ = N$(35)
b$ = "1"
SELECT CASE ixx
CASE IS < 8
c$ = "0"
CASE IS > 7
c$ = "1"
CASE ELSE
END SELECT
Number% = ixx MOD 8      'get value to load in dac address positions
'PRINT Number%
'INPUT foox
e$ = CHR$(2 * Number% + &H30)     'shift to proper position
IF Number2% > 255 THEN Number2% = 255
IF Number2% < 0 THEN Number2% = 0
temp3 = INT(Number2% / 16)
temp4 = INT(Number2% - (16 * temp3))
'LOCATE 20, 20
'PRINT temp3, temp4
'INPUT foox
f$ = CHR$(temp3 + &H30)
g$ = CHR$(temp4 + &H30)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN
ComputeDACSetting1:
Number2% = 140 - (VoltsDAC! - .05) / .0974
'PRINT "bits"; i, Number2%
RETURN
RampIt:
RampUp = 1
GOSUB TemperaturePlot
RETURN
DoNotRampIt:
RampUp = 0
GOSUB TemperaturePlot
RETURN

RobTest:
GOSUB WritePlateFiles
GOSUB WriteConstant
GOSUB WritePlateV
GOSUB WritePlateI
SHELL FileCall$
SHELL "mondown5"
RETURN

RobTest1:
GOSUB WritePlateFiles
GOSUB WriteConstant
GOSUB WritePlateV
GOSUB WritePlateI
SHELL FileCall$
SHELL "mondown5"
RETURN

WritePlateFiles:
ZeroExposureFits$ = "EXPTIME = " + STR$(ZeroTimeOpen!) + "   /Open Time for Camera 0 usually V"
OneExposureFits$ = "EXPTIME = " + STR$(OneTimeOpen!) + "   /Open Time for Camera 1 usually I"
TimeSinceRAON! = TIMER - RAOnTime!
IF TimeSinceRAON! < 0 THEN TimeSinceRAON! = TimeSinceRAON! + 86400
CALL TimeStuff(RAFitsLine1$, RAFitsLine$, FileDate$, TimeSinceRAON!, LongitudeCorr!, HomeZenithAngle!)
FileCall$ = "downfits" + " 2064 2037 " + SiteLetterCode$ + "vra" + FileDate$ + ".fits" + " " + SiteLetterCode$ + "ira" + FileDate$ + ".fits"
RETURN

WriteConstant:
SHELL "copy constant.bak constant"
OPEN "Constant" FOR APPEND AS #3
PRINT #3, "COMMENT  Additions to Constant File"
PRINT #3, RAFitsLine$
'LOCATE 20, 1
'PRINT RAFitsLine$
PRINT #3, "RATIME  =   '" + STR$(TimeSinceRAON!) + "' / Seconds since turning on RA drive"
IF FileNumber = 0 THEN
PRINT #3, "IMAGETYP=              'bias' /bias frame"
END IF
PRINT #3, "IMAGETYP=            'flat' /flat frame"
PRINT #3, "TELESCOP=               'TOM1' /telescope"
PRINT #3, "OBSERVER=        'Tom Droege' /Observer"
PRINT #3, "OBSERVAT=       'Batavia, IL' /Location      "
PRINT #3, "WATERTMP=   '" + STR$(a!(0, 3)) + "' / Cooling water temperature"
PRINT #3, "VCOTEMP =   '" + STR$(a!(1, 3)) + "'    / VCO Temperature"
PRINT #3, "DEC     =      '" + Decl$ + "' / Place Keeper for DEC of field center"
IF Decl1! <> 0 THEN
PRINT #3, "CRVAL1  =      " + STR$(Decl1!) + " / Place Keeper for DEC of field center"
ELSE
PRINT #3, "CRVAL1  =    0.0"
END IF
PRINT #3, RAFitsLine1$     'CRVAL2 in degrees
'PRINT RAFitsLine1$
'INPUT foox
PRINT #3, "CTYPE1  = 'DEC--TAN'"
PRINT #3, "CTYPE2  = 'RA---TAN'"
PRINT #3, "CRPIX1  = 1016"
PRINT #3, "CD1_1   = 0.002126"
PRINT #3, "CRPIX2  = 1015"
PRINT #3, "CD2_2   = -0.002126"
PRINT #3, "CDELT1  = 0.002126"
PRINT #3, "CDELT2  = -0.002126"
PRINT #3, "CROTA1  = 0.0"
PRINT #3, "CROTA2  = 0.0"
PRINT #3, "UT      = '" + ZeroTimeStart$ + "' /Universal time of #0 exposure (V) start"
CLOSE #3
RETURN

WritePlateI:
DateObsFitsLineI$ = "DATE-OBS ='" + RIGHT$(OneDateStart$, 4) + "-" + LEFT$(OneDateStart$, 2) + "-" + MID$(OneDateStart$, 4, 2) + "T" + OneTimeStart$ + "'" + "        /UT date-time of exposure start"
OPEN "PLATE_I" FOR OUTPUT AS #3
PRINT #3, "COMMENT This is the #1 Camera File"
PRINT #3, DateObsFitsLineI$
PRINT #3, "UTSTART  = '" + OneTimeStart$ + "' /Universal time of #1 exposure (I) start"
PRINT #3, OneExposureFits$
PRINT #3, "GAIN    =                  2.5 / Place Keeper for Gain in electrons per adu"
PRINT #3, "RDNOISE =                   25 / Place Keeper for Read Noise in electrons"
PRINT #3, "CCDTEMP =   '" + STR$(a!(19, 3)) + "'    / CCD Temperature Camera 1"
CLOSE #3
RETURN

WritePlateV:
DateObsFitsLineV$ = "DATE-OBS ='" + RIGHT$(ZeroDateStart$, 4) + "-" + LEFT$(ZeroDateStart$, 2) + "-" + MID$(ZeroDateStart$, 4, 2) + "T" + ZeroTimeStart$ + "'" + "    /UT date-time of exposure start"
OPEN "PLATE_V" FOR OUTPUT AS #3
PRINT #3, "COMMENT This is the #0 Camera File"
PRINT #3, DateObsFitsLineV$
PRINT #3, "UTSTART  = '" + ZeroTimeStart$ + "' /Universal time of #0 exposure (V) start"
PRINT #3, ZeroExposureFits$
PRINT #3, "GAIN    =                  2.5 / Place Keeper for Gain in electrons per adu"
PRINT #3, "RDNOISE =                   25 / Place Keeper for Read Noise in electrons"
PRINT #3, "CCDTEMP =   '" + STR$(a!(18, 3)) + "'    / CCD Temperature Camera 0"
CLOSE #3
RETURN


TestTime:
GOSUB WritePlateV
GOSUB WritePlateI
GOSUB WritePlateFiles
RETURN

TomSimpleScan: 'start at RA limit, take one exposure, back to limit.  Every
'eighth exposure is a dark frame.
CLS
ON TIMER(1) GOSUB ScheduleIt
TIMER ON
LOCATE 36, 1
INPUT "Enter Starting File Number "; FileNumber
INPUT "Enter Number of Last File "; LastFile
LastFile = LastFile + 1'to give the number of files requested
INPUT "Enter exposure time (>50sec): "; PadTime#
IF FileNumber = 0 THEN
ExposureTimeSave# = ExpossureTime#
ExposureTime# = 0
GOSUB TakeDarkFrame
ExposureTime# = ExposureTimeSave#
END IF
GOSUB ScnSkyRaw1
GOSUB Announce
TIMER OFF
RETURN

ScnSkyRaw1:
StartFile = FileNumber
FileNumber = FileNumber - 1
TimeSinceOpen# = 0
OpenShutterFlag# = 0
'make sure shutters are closed
GOSUB Close0
GOSUB Close1
LOCATE 10, 10
GOSUB ReadOut  'initial clear
FirstTimeThrough = 0

DO UNTIL FileNumber >= LastFile
FileNumber = FileNumber + 1
'home RA
GOSUB RASetup
GOSUB TurnRAOn
CALL Timex(2) 'to get the RA Drive started
IF FileNumber MOD 8 = 0 THEN DarkFrameFlag = 1 ELSE DarkFrameFlag = 0
' now start first frame of sweep sequence
OpenShutterFlag = 1   'time 1
DO UNTIL OpenShutterFlag = 0'wait until interrupt opens the shutters
LOOP
GOSUB TurnRAOn 'kludge to ensure RA moving
LOCATE 40, 1
PRINT "Taking First Exposure                                        "
LOCATE 39, 1
PRINT "The File Number Is "; FileNumber
LOCATE 38, 1
PRINT "DarkFrameFlag "; DarkFrameFlag
FileNumber = FileNumber - 1
' SaveIt1 increments filenumber
IF FirstTimeThrough = 0 THEN
FirstTimeThrough = 1'don't save first time through
FileNumber = FileNumber + 1'to fix file number
CALL Timex(50)
ELSE
GOSUB SaveIt1
END IF
ShuttersClosed = 0
DO UNTIL ShuttersClosed = 1 'wait until interupt loop closes the shutters
LOOP
GOSUB ReadOut
LOOP

GOSUB RASetup
LOCATE 40, 1
PRINT "Run Completed                                  "
RETURN

ScheduleIt:
IF OpenShutterFlag = 1 THEN
OpenScheduleTime# = TIMER
OpenedOnce = 1
GOSUB Open0
GOSUB Open1
OpenShutterFlag = 0
ELSE
END IF

CloseScheduleTime# = TIMER

IF OpenedOnce = 1 AND OpenShutterFlag = 0 AND ShuttersClosed = 0 THEN
TimeSinceOpen# = CloseScheduleTime# - OpenScheduleTime#
IF TimeSinceOpen# < 0 THEN TimeSinceOpen# = TimeSinceOpen# + 86400

IF PadTime# < TimeSinceOpen# OR ABS(PadTime# - TimeSinceOpen#) < .5 THEN
LOCATE 36, 1
PRINT "PadTime# "; PadTime#; "  FileNumber  "; FileNumber; "  Follow"
PRINT "TimeSinceOpen# "; TimeSinceOpen#; "              "
'INPUT foox
GOSUB Close0
GOSUB Close1
ShuttersClosed = 1
END IF

END IF
RETURN

TomFollow:
'start at RA limit, take one continuous exposures tracking the sky. First
'exposure is a dark frame.
CLS
ON TIMER(1) GOSUB ScheduleIt
TIMER ON
LOCATE 36, 1
FileNumber = 1
LastFile = 20
LastFile = LastFile + 1'to give the number of files requested
PadTime# = 100
IF FileNumber = 0 THEN
ExposureTimeSave# = ExpossureTime#
ExposureTime# = 0
GOSUB TakeDarkFrame
ExposureTime# = ExposureTimeSave#
END IF
FileNumberSave = FileNumber
GOSUB ScnSkyRaw2
FileNumber = FileNumberSave
GOSUB ScnSkyRaw2
GOSUB Announce
TIMER OFF
SYSTEM

ScnSkyRaw2:
StartFile = FileNumber
FileNumber = FileNumber - 1
TimeSinceOpen# = 0
OpenShutterFlag# = 0
'make sure shutters are closed
GOSUB Close0
GOSUB Close1
LOCATE 10, 10
GOSUB ReadOut  'initial clear
FirstTimeThrough = 0

GOSUB RASetup
GOSUB TurnRAOn
CALL Timex(2) 'to get the RA Drive started
DO UNTIL FileNumber >= LastFile
FileNumber = FileNumber + 1
'home RA
IF FileNumber = 0 THEN DarkFrameFlag = 1 ELSE DarkFrameFlag = 0
' now start first frame of sweep sequence
OpenShutterFlag = 1   'time 1
DO UNTIL OpenShutterFlag = 0'wait until interrupt opens the shutters
LOOP
LOCATE 40, 1
PRINT "Taking First Exposure                                        "
LOCATE 39, 1
PRINT "The File Number Is "; FileNumber
LOCATE 38, 1
PRINT "DarkFrameFlag "; DarkFrameFlag
FileNumber = FileNumber - 1
' SaveIt1 increments filenumber
IF FirstTimeThrough = 0 THEN
FirstTimeThrough = 1'don't save first time through
FileNumber = FileNumber + 1'to fix file number
CALL Timex(50)
ELSE
GOSUB SaveIt1
END IF
ShuttersClosed = 0
DO UNTIL ShuttersClosed = 1 'wait until interupt loop closes the shutters
LOOP
GOSUB ReadOut
LOOP
GOSUB RASetup
LOCATE 40, 1
PRINT "Run Completed                                  "
RETURN

SUB Display (j%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
SELECT CASE j%
'j%=0 put up power supply voltages and DAC commands
'j%=38 puts up 32 ADC readings
'j%=others  put up blinking status from n$(j)
CASE 0
LOCATE 1, 1
COLOR 15, 13
PRINT N$(32)
COLOR 15, 9
LOCATE 2, 1
PRINT N$(28)      'TEC Power
LOCATE 2, 14
PRINT USING "###.###"; a!(28, 3)
LOCATE 3, 1
PRINT N$(12)      '+15
LOCATE 3, 14
PRINT USING "###.###"; a!(12, 3)

LOCATE 4, 1
PRINT N$(11)       '+5
LOCATE 4, 14
PRINT USING "###.###"; a!(11, 3)

LOCATE 5, 1
PRINT N$(8)        'ground
LOCATE 5, 14
PRINT USING "###.###"; a!(8, 3)


LOCATE 6, 1
PRINT N$(10)        '-5
LOCATE 6, 14
PRINT USING "###.###"; a!(10, 3)

LOCATE 7, 1
PRINT N$(9)         '-15
LOCATE 7, 14
PRINT USING "###.###"; a!(9, 3)

LOCATE 8, 1
PRINT N$(2)          'VCO Ref
LOCATE 8, 14
PRINT USING "###.###"; a!(2, 3)

LOCATE 9, 1
PRINT N$(20)
LOCATE 9, 14
PRINT USING "###.###"; a!(20, 3)

LOCATE 10, 1
COLOR 15, 12
PRINT N$(33)
COLOR 15, 9
FOR I = 0 TO 15
LOCATE I + 11, 1
PRINT N$(I + 64)
NEXT I
FOR I = 0 TO 15
LOCATE I + 11, 15
PRINT USING "##.###"; d!(I)
NEXT I
LOCATE 27, 1
COLOR 15, 12
PRINT N$(36)
LOCATE 29, 1
COLOR 15, 12
PRINT N$(41)
COLOR 15, 9
LOCATE 30, 1
PRINT N$(42)
LOCATE 30, 21
PRINT N$(34)
LOCATE 30, 22
PRINT N$(35)
LOCATE 31, 1
PRINT N$(43)
LOCATE 31, 21
PRINT N$(44)
LOCATE 31, 22
PRINT N$(45)
CASE 38
LOCATE 28, 1
COLOR 15, 9
PRINT N$(j%)
LOCATE 1, 42
COLOR 15, 12
PRINT N$(39)
COLOR 15, 9

FOR I = 0 TO 31
LOCATE I + 2, 42
PRINT USING "\                    \"; N$(I)
LOCATE I + 2, 56
PRINT USING "####.### "; a!(I, 3)
LOCATE I + 2, 65
PRINT USING "##.### "; a!(I, 1)
LOCATE I + 2, 72
PRINT USING "####.##"; a!(I, 2)
NEXT I

CASE ELSE
LOCATE 28, 1
'COLOR 31, 9
PRINT N$(j%)
END SELECT
END SUB

SUB InitStamp (j%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
DEFSNG A-Z
SELECT CASE j%
CASE 0
GOSUB SetA16
GOSUB StepperOff
CASE ELSE
END SELECT
EXIT SUB
SetA16:
a$ = N$(34)
b$ = "8"      'test
c$ = "1"      'load a register
d$ = "?"      'set to load mpx register
e$ = "2"
f$ = "0"      'with all zeros, thus zero for eprom a-16
g$ = "0"
H$ = N$(35)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN
StepperOff:
'turns off all the motors
a$ = N$(34)
b$ = "8"       'test
c$ = "1"       'load a register
d$ = "5"       'motor enable register
e$ = "9"
f$ = "?"   'all ones disables everyone
g$ = "?"
H$ = N$(35)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
N$(44) = a$
N$(45) = H$
RETURN
END SUB

SUB LoadDAC (j%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
DEFSNG A-Z
'j%>15  load all the dacs
'j%=n  load dac n

IF j% > 15 THEN
FOR I = 0 TO 15
VoltsDAC! = d!(I)
'PRINT "Volts Commanded"; d!(i)
GOSUB ComputeDACSetting
GOSUB LoadADAC
NEXT I
ELSE
LOCATE 40, 1
INPUT "Enter DAC Voltage -10 to 10"; VoltsDAC!
I = j%
GOSUB ComputeDACSetting
GOSUB LoadADAC
END IF

EXIT SUB

ComputeDACSetting:
Number2% = 140 - (VoltsDAC! - .05) / .0974
'PRINT "bits"; i, Number2%
RETURN

LoadADAC:
a$ = N$(34)
H$ = N$(35)
b$ = "1"
SELECT CASE I
CASE IS < 8
c$ = "0"
d$ = "0"     'to fix first DAC load problem &002&
CASE IS > 7
c$ = "1"
d$ = "0"     'to fix first DAC load problem &002&
CASE ELSE
END SELECT
Number% = I MOD 8      'get value to load in dac address positions
'PRINT Number%
'INPUT foox
e$ = CHR$(2 * Number% + &H30)     'shift to proper position
IF Number2% > 255 THEN Number2% = 255
IF Number2% < 0 THEN Number2% = 0
temp3 = INT(Number2% / 16)
temp4 = INT(Number2% - (16 * temp3))
'LOCATE 20, 20
'PRINT temp3, temp4
'INPUT foox
f$ = CHR$(temp3 + &H30)
g$ = CHR$(temp4 + &H30)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN


END SUB

SUB LoadPowerControlRegister (j%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
DEFSNG A-Z
'if we use a second bit we have to set up to or bits for proper control
SELECT CASE j%
CASE 0 'turn off chiller
f$ = "0"
g$ = "0"
GOSUB LoadIt
CASE 1 'turn off chiller
f$ = "0"
g$ = "1"
GOSUB LoadIt
CASE ELSE
END SELECT
EXIT SUB
LoadIt:
a$ = N$(34)
b$ = "8"      'test
c$ = "1"      'load a register
d$ = "5"      'set to load power control register
e$ = "1"
H$ = N$(35)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN

END SUB

SUB MAServo (j%, k%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
'j% channel number
'k% position
'k%=255 full closed
'k%=64 full open
DEFSNG A-Z
a$ = N$(34)
H$ = N$(35)
SELECT CASE j%
CASE 0
b$ = "7"   ' &001&  to fix bad initiaization causing no end of scan seen
c$ = "0"
d$ = "="   'set for MAS channel 0 = pulse channel 22
e$ = ":"
CASE 1
b$ = "7"   ' &001&  to fix bad initiaization causing no end of scan seen
c$ = "0"
d$ = "9"   'set for MAS channel 1 = pulse channel 14
e$ = ";"
CASE 2
b$ = "7"   ' &001&  to fix bad initiaization causing no end of scan seen
c$ = "0"
d$ = "?"
e$ = ":"
END SELECT
temp1% = INT(k% / 16)
temp2% = k% - 16 * temp1%
f$ = CHR$(temp1% + 48)
g$ = CHR$(temp2% + 48)
b$ = "7"   'select MAServo stamp instruction
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
f$ = "0"
g$ = "0"

EXIT SUB
'Open0:
b$ = "7"   'select MAServo stamp instruction
d$ = "="   'set for MAS channel 0 = pulse channel 22
e$ = ":"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN
'Close0:
b$ = "7"   'select MAServo stamp instruction
d$ = "="   'set for MAS channel 0 = pulse channel 22
e$ = ":"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN

END SUB

SUB Motor (Action%, Motorx%, Steps%, Direction$, LastPosition$, N$()) STATIC
DEFINT A-Z
'Action
'0  Move to limit and out n steps (n can be zero)
'1  move n steps in specified direction
'2  just turn the motors off
'Motor number
'0  Focus V            limit sw pos 0, mask 0001
'2  Declination Clamp  no limit switch
'1  Declination Drive  limit sw pos 3, mask 0040
'3  Focus I            limit sw pos 1, mask 0080
'Direction
'0   Toward limit switch
'1   Away from limit switch
'Last Position  (in step sequence after last move) 0-3 can be saved
'and restored so that there is not a jump when a motor is turned on.
a$ = N$(34)
b$ = "0"     ' &002& ,make sure x$'s have initial value
c$ = "0"
d$ = "0"
e$ = "0"
f$ = "0"
g$ = "0"
H$ = N$(35)
SELECT CASE Action
CASE 0
Direction$ = "0"  'direction always in to limit.  Phase at  motor.
GOSUB SetupMotor  'load Direction into d$ and LastPosition$ e$
GOSUB MotorSelect 'get f$ and g$ to set motor on path
GOSUB MotorOn     'd$,e$ are register path and f$,g$ motor on bit
GOSUB GetMask     'get d$e$f$g$ limit mask from motor number
GOSUB MoveToLimit 'instruction 9,3 with mask in d$,e$,f$,g$
Direction$ = "1"  'now set direction away from limit sw.
GOSUB SetupMotor  'load direction - we have stopped position
GOSUB MoveNSteps  'instruction 9,2 and number of steps in d$,e$,f$,g$
CASE 1
GOSUB SetupMotor 'load Direction into d$ and LastPosition$ e$
GOSUB MotorSelect 'get f$ and g$ to set motor on path
GOSUB MotorOn     'd$,e$ are register path and f$,g$ motor on bit
GOSUB MoveNSteps  'instruction 9,2 and number of steps in d$,e$,f$,g$
CASE 2
GOSUB MotorsOff   'just turn off the motors
END SELECT

EXIT SUB

GetMask:
SELECT CASE Motorx%
CASE 0       'the V focus motor
d$ = "0"
e$ = "0"
f$ = "0"
g$ = "1"
CASE 1       'DEC motor
d$ = "0"
e$ = "0"
f$ = "4"
g$ = "0"
CASE 2       'Dec cloamp motor, no limit sw
d$ = "0"
e$ = "0"
f$ = "0"
g$ = "0"
CASE 3       'I focus motor
d$ = "0"
e$ = "0"
f$ = "8"
g$ = "0"
CASE ELSE
END SELECT
RETURN


MotorOn:  'turns on the motor selected by f$ and g$
'load register bit to turn on motor
b$ = "8"         'test
c$ = "1"         'load register
d$ = "5"         'motor enable register
e$ = "9"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)  'to turn on selected motor
RETURN
MotorSelect:  'loads f$ and g$ to select the particular motor.  Enter with
'motor number in Motor%
SELECT CASE Motorx%
CASE 0
f$ = "7"
g$ = "?"
CASE 1
f$ = "9"  'was ;  9 turns on both motors
g$ = "?"
CASE 2
f$ = "9"  ' was =  9 turns on both motors
g$ = "?"
CASE 3
f$ = ">"
g$ = "?"
CASE ELSE
END SELECT
RETURN
MotorsOff:
b$ = "8"
c$ = "1"
d$ = "5"
e$ = "9"
f$ = "?"          'all high = motors off
g$ = "?"  ' was ? for all high = motors off.  This means that the
' Dec motor is always on.  Should be ok as the current draw is
' low.  There is the possibility of noise from a motor running,
' but I doubt it as the RA drive motor is running all the time too.
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)  'to turn off motors
RETURN
MoveNSteps:
b$ = "9"
c$ = "2"
GOSUB StepsTodefg
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)  'to move n steps
RETURN
MoveToLimit:
b$ = "9"
c$ = "3"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)  'move to the limit switch
RETURN
SetupMotor: 'here we load the direction and the last position for this motor
'note that the motor remembers where it is, so we really need to save motor
'positions between power downs.  This means a file somewhere.
b$ = "9"
c$ = "4"
d$ = Direction$
e$ = CHR$(Motorx%)
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)  'to setup motor
RETURN
StepsTodefg:
temp1 = Steps% \ 4096
temp2 = (Steps% - temp1 * 4096) \ 256
temp3 = (Steps% - temp1 * 4096 - temp2 * 256) \ 16
temp4 = (Steps% - temp1 * 4096 - temp2 * 256 - temp3 * 16)
d$ = CHR$(temp1 + &H30)
e$ = CHR$(temp2 + &H30)
f$ = CHR$(temp3 + &H30)
g$ = CHR$(temp4 + &H30)
LOCATE 1, 1
PRINT d$; e$; f$; g$
RETURN

END SUB

DEFSNG A-Z
SUB OutIt (a$, b$, c$, d$, e$, f$, g$, H$) STATIC
DEFINT A-Z
IF OutItInProgress = 1 THEN PRINT "OutIt Error": EXIT SUB
OutItInProgress = 1
ON LOCAL ERROR GOTO NoteTheError
REDIM QQ$(0 TO 7)
REDIM hh$(0 TO 7)

start:
ok = 0
DO
a$ = CHR$(RND * 9 + 48)
H$ = CHR$(RND * 9 + 48)
check1$ = a$
check2$ = H$
OPEN "COM1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR RANDOM AS #1
stuffout$ = a$ + b$ + c$ + d$ + e$ + f$ + g$ + H$
'PRINT a$; b$; c$; d$; d$; f$; g$; h$
'INPUT foox
zz$ = LEFT$(stuffout$, 8) 'was 8
PRINT #1, zz$
'CLOSE #1
'OPEN "COM1:9600,N,8,1,BIN,CD0,CS0,DS0,OP0" FOR INPUT AS #1
'COLOR 15, 9
LOCATE 38, 50
PRINT "Sent     "; zz$
'xx$ = "cccccccccc"
ss$ = ""
readserial:

ss$ = INPUT$(1, #1)
IF ss$ <> "S" THEN GOTO readserial
xx$ = INPUT$(8, #1)
CLOSE #1
'use 8 under dos 9 with windows
'this gets rid of the cr that some versions of windows insert

LOCATE 39, 50
xx$ = MID$(xx$, 1, 8)     '1,8 under dos   2,8 windows
'avoid the leading CR
PRINT "Received "; xx$
a$ = MID$(xx$, 1, 1)
H$ = MID$(xx$, 8, 1)
LOCATE 20, 1
IF a$ = check1$ AND H$ = check2$ THEN
ok = 1
'SOUND 2000, 1
CALL Timex(.05)
ELSE
SOUND 100, 2
CALL Timex(.1)
END IF
LOOP UNTIL ok = 1
b$ = MID$(xx$, 2, 1)
c$ = MID$(xx$, 3, 1)
d$ = MID$(xx$, 4, 1)
e$ = MID$(xx$, 5, 1)
f$ = MID$(xx$, 6, 1)
g$ = MID$(xx$, 7, 1)
OutItInProgress = 0
EXIT SUB
NoteTheError:
ErrorCount! = ErrorCount! + 1
LOCATE 20, 20
PRINT "Loop Errors "; ErrorCount!
SOUND 50, 20
RESUME NEXT
RETURN
END SUB

DEFSNG A-Z
SUB PulseIt (p%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
DEFSNG A-Z
'enter with p% the channel number to pulse
Number% = p%
GOSUB PulseSelectRegisterSet
a$ = N$(34)
b$ = "3"
c$ = "0"
f$ = "0"
g$ = "0"
H$ = N$(35)
'LOCATE 20, 1
'PRINT p%; a$; b$; c$; d$; e$; f$; g$; h$
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
EXIT SUB
PulseSelectRegisterSet:
'brute force conversion of channel to pulse to Pulse Select Register
'contents
SELECT CASE Number%
CASE 0
d$ = "5"
e$ = "1"
CASE 1
d$ = "7"
e$ = "1"
CASE 2
d$ = "5"
e$ = "9"
CASE 3
d$ = "7"
e$ = "9"
CASE 4
d$ = "5"
e$ = "3"
CASE 5
d$ = "7"
e$ = "3"
CASE 6
d$ = "5"
e$ = ";"
CASE 7
d$ = "7"
e$ = ";"
CASE 8
d$ = "9"
e$ = "1"
CASE 9
d$ = ";"
e$ = "1"
CASE 10
d$ = "9"
e$ = "9"
CASE 11
d$ = ";"
e$ = "9"
CASE 12
d$ = "9"
e$ = "3"
CASE 13
d$ = ";"
e$ = "3"
CASE 14
d$ = "9"
e$ = ";"
CASE 15
d$ = ";"
e$ = ";"
CASE 16
d$ = "="
e$ = "0"
CASE 17
d$ = "?"
e$ = "0"
CASE 18
d$ = "="
e$ = "8"
CASE 19
d$ = "?"
e$ = "8"
CASE 20
d$ = "="
e$ = "2"
CASE 21
d$ = "?"
e$ = "2"
CASE 22
d$ = "="
e$ = ":"
CASE 23
d$ = "?"
e$ = ":"
CASE ELSE
END SELECT
RETURN
END SUB

SUB RAMove (j%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
DEFSNG A-Z
a$ = N$(34)
H$ = N$(35)
SELECT CASE j%
'f$ contains the speed.  Low order bit is the direction, three high order
'bits are speed.  Highest speed 111d allows pulse X10 to drive the RA.
CASE 0
GOSUB RAOff
CASE 1
GOSUB RAOn
CASE 2
GOSUB RABack
CASE 3
GOSUB RAFF
CASE ELSE
END SELECT
EXIT SUB
RAOn:
b$ = "8"
c$ = "1"
d$ = ";"
e$ = ";"
f$ = "0"  'was 4 for -014 motorcount down factors of two and direction  uuuu
g$ = "0"   '0 turns on motor current
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN
'ra speed control f$  - period for control bits in f$
'0,1     0.1422'2,3     0.071149
'4,5     0.035574
'6,7     0.017787
'8,9     0.008893
':,;     0.004446
'<,=     0.002223
'>,?     External

RAOff:
'loads 111 in count down selection,
b$ = "8"
c$ = "1"
d$ = ";"
e$ = ";"
f$ = "?"   'was 5
g$ = "8"   '8 turns off motor current
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN
RABack:
b$ = "8"
c$ = "1"
d$ = ";"
e$ = ";"
f$ = "9" 'was 7    7 is 8x
g$ = "0"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
f$ = ";"  '; is faster but not enough torque
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)

RETURN
RAFF:
b$ = "8"
c$ = "1"
d$ = ";"
e$ = ";"
f$ = "8"   'was 6         6 is 8x
g$ = "0"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
f$ = ":"  ': is faster but not quite enough torque
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
RETURN


END SUB

SUB ReadADC (a%, b%, N$(), a!(), d!(), c%(), v!(), p$()) STATIC
'a%  thing to do   0 read all  1 read single channel
'b%  channel
DEFSNG A-Z
SELECT CASE a%
CASE 0
GOSUB ReadADC
j% = 38
CALL Display(j%, N$(), a!(), d!(), c%(), v!(), p$())
EXIT SUB
CASE 1
ki = b%
GOSUB ReadAChannel
EXIT SUB
CASE ELSE
EXIT SUB
END SELECT

ReadADC:
ki = 0
'FOR i = 1 TO 3
'GOSUB ReadAChannel  'read the first channel twice to reset it
'NEXT i
FOR ki = 0 TO 31
GOSUB ReadAChannel
NEXT ki
FOR ki = 0 TO 31 'fixup all channels only after data is taken on all
GOSUB FIXUP
NEXT ki
RETURN

ReadAChannel:
a$ = N$(34)
H$ = N$(35)
b$ = "2"                            'instruction = read ADC
c$ = "0"                            'sub instruction
IF ki > 15 THEN d$ = CHR$(50) ELSE d$ = CHR$(49)
e$ = CHR$((ki MOD 16) + 48)
f$ = "0"
g$ = "0"
CALL OutIt(a$, b$, c$, d$, e$, f$, g$, H$)
T3! = ASC(d$) - 48
T2! = ASC(e$) - 48
T1! = ASC(f$) - 48
T0! = ASC(g$) - 48
'LOCATE 30, 1
'PRINT T3!
'PRINT T2!
'PRINT T1!
'PRINT T0!
IF T3! > 7 THEN
T4! = 4096 * T3! + 256 * T2! + 16 * T1! + T0!
T4! = T4! - 65536
ELSE
T4! = 4096 * T3! + 256 * T2! + 16 * T1! + T0!
END IF

a!(ki, 3) = ((T4! / 3276.8) + a!(ki, 1)) * a!(ki, 2)
IF a% = 1 THEN GOSUB FIXUP 'to fixup a single channel when only one
                           'channel is measured

RETURN
FIXUP:
'try to fix offset from various cards from STAMP
IF ki = 0 THEN
a!(0, 3) = a!(0, 3) - 273 - a!(3, 3)
END IF
IF ki = 1 THEN
a!(1, 3) = a!(1, 3) - 273 - a!(3, 3)
END IF
IF ki = 18 THEN
a!(18, 3) = a!(18, 3) - 273 - a!(16, 3)
END IF
IF ki = 19 THEN
a!(19, 3) = a!(19, 3) - 273 - a!(16, 3)
END IF
IF ki = 25 THEN
a!(25, 3) = a!(25, 3) - a!(24, 3)
END IF
IF ki = 26 THEN
a!(26, 3) = a!(26, 3) - a!(24, 3)
END IF
RETURN

END SUB

SUB SensIt (j%, k%(), N$(), a!(), d!(), c%(), v!(), p$()) STATIC
DEFSNG A-Z
'k%() contains the status bits
END SUB

DEFINT A-Z
SUB TimeStuff (RAFitsLine1$, RAFitsLine$, FileDate$, TimeSinceRAON!, LongitudeCorr!, HomeZenithAngle!) STATIC
'take date string apart and use in Jeff Beish's formula
CLS
Year! = VAL(RIGHT$(DATE$, 4))
Month! = VAL(LEFT$(DATE$, 2))
Day! = VAL(MID$(DATE$, 4, 2))
'PRINT "Year"; Year!
'PRINT "Month"; Month!
'PRINT "Day"; Day!
IF Month! < 3 THEN Y1! = Year! - 1 ELSE Y1! = Year!
JDay# = 367 * Year! - INT(7 * (INT((Month! + 9) / 12) + Year!) / 4) + INT(275 * Month! / 9) + 1721029 - INT(INT(.75 * (Y1! / 100) + 1)) - .5 + Day!
'PRINT "Jday"; JDay#
'take time string apart and add to give fractional Julian Day

Hours = VAL(LEFT$(TIME$, 2))
Minutes = VAL(MID$(TIME$, 4, 2))
Seconds = VAL(RIGHT$(TIME$, 2))
'PRINT "Hours"; Hours
'PRINT "Minutes"; Minutes
'PRINT "Seconds"; Seconds
JulianDay# = JDay# + (Hours + Minutes / 60 + Seconds / 3600) / 24
FileDate$ = RIGHT$(STR$(INT(JulianDay# * 1000)), 7)
'PRINT FileDate$
'PRINT "JulianDay"; JulianDay#
'use Jim Wittes' method to get the R. A. of the local meridian


TilNow# = JulianDay# - 2451544.5#       ' Days since Jan 1, 2000 midnight
f# = INT(TilNow#)                       ' integer days since Jan 1, 2000
H# = (TilNow# - f#) * 24                 ' current decimal hour = fract day * 24
f# = f# + .5                            ' add back the half day from TilNow calc.
'PRINT "H"; H#
'PRINT "F"; F#

G0# = (6.69737456# + f# * .0657098232#) / 24' day of aries at Greenwich at midnight
G0# = G0# - INT(G0#)                     ' fractional day of aries
G0# = G0# * 24                         ' hour angle of aries at Greenwich midnt
'PRINT "G0#"; G0#

GMST# = (G0# + H# * 1.00273791#) / 24' add sidereal corrected day since midnight
'PRINT "GMST0"; GMST#
GMST# = GMST# - INT(GMST#)       ' fractional sidereal day
'PRINT "Gmst1"; GMST#
GMST# = GMST# * 24              ' Greenwich Mean Sidereal Time (fractional hours
'PRINT "GMST"; GMST#
LMST# = (GMST# + LongitudeCorr!) / 24' add correction for local longitude
LMST# = LMST# - INT(LMST#)       ' fractional sidereal day
LMST# = LMST# * 24             ' Local Mean Sidereal Time (fractional hours)
RAx# = LMST# + HomeZenithAngle! - (TimeSinceRAON! / 3600)
'PRINT "LMST"; LMST#
'PRINT "RAx"; RAx#
IF RAx# < 0 THEN RAx# = RAx# + 24  'make sure RA is +
RAxh = INT(RAx#)  'integer pointing RA hours
RAxhh$ = RIGHT$(("10" + STR$(RAxh)), 2)
RAxf! = RAx# - RAxh 'fractional hours
RAxg! = 60 * RAxf! 'total minutes
RAxm = INT(RAxg!) 'pointing RA integer minutes
RAxmm$ = RIGHT$(STR$(RAxm), 2)
RAxr! = RAxg! - RAxm  'fractional minutes
RAxs! = 60 * RAxr! 'total Seconds
RAxs! = INT(100 * RAxs!)
RAxs! = RAxs! / 100
RAxss$ = RIGHT$(STR$(RAxs!), 5)
'PRINT RAx#
'PRINT RAxhh$
'PRINT RAxmm$
'PRINT RAxss$
'INPUT foox



RAFitsLine$ = "RA =    " + "'" + RAxhh$ + ":" + RAxmm$ + ":" + RAxss$ + "'" + "      / RA of Field Center "
RAx1# = INT(150000# * RAx#) / 10000#
RAFitsLine1$ = "CRVAL2 =    " + STR$(RAx1#) + "      / RA of Field Center "

'PRINT RAFitsLine$
END SUB

SUB Timex (a#) STATIC
              IF a# < 0 THEN
              TIMEXD# = TIMER
              FOR I# = 1 TO 1000000#
              NEXT I#
              TIMEXE# = TIMER
              IF TIMEXE# < TIMEXD# THEN TIMEXE# = TIMEXE# + 86400
              TIMEXF# = TIMEXE# - TIMEXD#
              TIMFAC# = 1000000 / TIMEXF#
              PRINT "ITERATIONS PER SECOND= "; TIMFAC#
              ELSE
              LOOPX# = a# * TIMFAC#
              FOR I# = 1 TO LOOPX#
              NEXT I#
              END IF

END SUB

