' ---------------- ' Global variables ' ---------------- ' ' Observing run parameters ' Const imageDir = "d:\Jan2300\" 'include ending \ ' Where the images are to be saved Const logDir = "D:" ' Log file goes here Dim testit ' if testit = 0, CCD Camera is connected and used. if 1, not testit = 0 ' ' Camera & exposure data ' Const cameraTemp = -15.0 ' Run the cam at 0.0 Const regExpInterval = 20 ' Aiming image exposure interval Const astExpInterval = 300 ' Astrometry image exposure interval Const GuiderExp = 2.5 ' Guider exposure Const UseGuider = 1 ' 1 uses guider during astrometric exposures1 ' 0 does not use guider Const AcceptableFocus = 0.05 ' Maxumum percent change in star max to be ' considered acceptable focus and stop. ' 5 images averaged Const FocusMoveTime = 200 ' time to move focuser in msec DEGRAD = pi / 180.0 Dim StarsSolved ' Number of stars solved by Charon Dim cam ' "The" Camera object Dim tgtName ' Current target object name Dim tgtRA ' Current target object RA Dim tgtDec ' Current target object Dec Dim last_Dec Dim last_RA Dim Found_RA, Found_Dec ' values found by Charon Dim control_code, RA_code, Dec_code Dim Input_File Dim Temp Dim value, RAE_rate, RAW_rate, Dec_Rate, RAE_Code, RAW_Code Dim DecN_Code, DecS_Code, time2 ' ' Logging file ' Dim logFSO ' Logging file Dim logStream ' Logging text stream 'Charon data transfer file Dim CharonFSO Dim Charon Set CharonFSO = CreateObject("Scripting.FileSystemObject") ' ' Specific to the Target List. ' Dim obsList ' Target object list (CAA plan format) Dim fso ' Target list FileSystemObject Dim infile ' Target list text stream Dim Message ' ' ----- end of user configuration section ----- ' dim move_time dim value2 if Testit = 3 then move_time = inputbox("Enter number of seconds: ") value2 = 5 do while value2 >= 0 if value2 <0 then stop value2 = inputbox("Enter value to pass") if value2 >= 0 then Game.AddScore(value2 + move_time * 256000) ' 25600000=100 sec Loop tweedle=false End If 'if testit = 3 then dim direction direction = inputbox("Enter direction: 0 = none, 1=N, 2=S, 3=E, 4=W") do while direction > 0 value2 = inputbox("Enter number of degrees(dec) or minutes (RA): ") if direction = 1 then move_time = 1000 * value2/0.02743 Game.AddScore(128 + int(move_time) * 256) elseif direction = 2 then move_time = 1000 * value2/0.02743 Game.AddScore(144 + int(move_time) * 256) elseif direction = 3 then move_time = 1000 * value2/(0.07020*4) Game.AddScore(10 + int(move_time) * 256) elseif direction = 4 then move_time = 1000 * value2/(0.06127*4) Game.AddScore(9 + int(move_time) * 256) End If direction = inputbox("Enter direction: 0 = none, 1=N, 2=S, 3=E, 4=W") Loop ' tweedle=false 'crash to end! Clever, but works! 'End If '''''Move scope parameters If Testit = 0 Then value = inputbox("Enter control speed: 4, 8 or 16: ") If Testit = 1 then value = 8 if value = 8 then RAE_rate = 0.0360000 ' degrees per second RAW_rate = 0.028800 ' degrees per second Dec_rate = 0.013715 ' degrees per second RAE_Code = 6 RAW_Code = 5 DecS_Code = 80 DecN_Code = 96 end if if value = 4 then RAE_rate = 0.02070000 ' degrees per second RAW_rate = 0.012200 ' degrees per second Dec_rate = 0.0068575 ' degrees per second RAE_Code = 12 RAW_Code = 15 DecS_Code = 240 DecN_Code = 224 end if if value = 16 then RAE_rate = 0.07020 ' degrees per second RAW_rate = 0.06127 ' degrees per second Dec_rate = 0.02743 ' degrees per second RAW_Code = 9 RAE_Code = 10 DecS_Code = 144 DecN_Code = 128 End if '''Find Guidestar code parameters: speed = 8, includes direction and time Dim MoveGuider(28) MoveGuider(0) = 0 ' don't move MoveGuider(1) = 1401440 ' N MoveGuider(2) = 666629 ' W MoveGuider(3) = 1401424 ' S MoveGuider(4) = 1401424 ' S MoveGuider(5) = 533254 ' E MoveGuider(6) = 533254 ' E MoveGuider(7) = 1401440 ' N MoveGuider(8) = 1401440 ' N MoveGuider(9) = 1401440 ' N MoveGuider(10) = 666629 ' W MoveGuider(11) = 666629 ' W MoveGuider(12) = 666629 ' W MoveGuider(13) = 1401424 ' S MoveGuider(14) = 1401424 ' S MoveGuider(15) = 1401424 ' S MoveGuider(16) = 1401424 ' S MoveGuider(17) = 533254 ' E MoveGuider(18) = 533254 ' E MoveGuider(19) = 533254 ' E MoveGuider(20) = 533254 ' E MoveGuider(21) = 1401440 ' N MoveGuider(22) = 1401440 ' N MoveGuider(23) = 1401440 ' N MoveGuider(24) = 1401440 ' N '------------------------------------------------------------------------------ ' ' ================ ' MAIN ENTRY POINT ' ================ ' 'Sub Main() Dim fd ' initialize scope drive Sub Game_NewGame Game.AddScore 256*30 + 7 + 112 ' load pulse codes, 30 msec If Testit <> 1 Then Input_File = inputbox("Enter the name of the Coordinate File:") If Testit = 1 Then Input_File = "d:\b" ' ' Open up the log file. The file name designates the ' start UTC of the run ' Set logFSO = CreateObject("Scripting.FileSystemObject") fn = logDir & "\" & Month(Now) & Day(now) & Year(now) & ".log" Caption = "Opening Log File" Set logStream = logFSO.CreateTextFile(fn, false) If Testit = 0 Then ' ' Connect to the camera & set the temp and 2:1 binning ' Set cam = CreateObject("MaxIm.CCDCamera") cam.LinkEnabled = True if Not cam.LinkEnabled Then msgbox("Failed to start camera.") ' Exit Sub End If Log "Camera is connected. Please wait..." Delay(5) cam.CoolerOn = True cam.TemperatureSetpoint = cameraTemp Log "Waiting for cooler to start..." Caption = "Waiting for cooler to start." Delay(10) Log "Camera is ready, starting observing run." Caption = "Camera is ready. Starting run." cam.BinY = 2 cam.BinX = 2 cam.GuiderAutoSelectStar = True End If '''''''''''''''''''''''''''''''''''''''''''''''''''''' '' Read initial target star. '' solve plate '' set initial RA, Dec OpenTargetList ' Open the target list file AND DO INITAL POSITIONING Caption = "get first object" startTime = Now() ' Mark the start time of this set Log "Starting target set " ' ' -------------------------------- ' For each target in the list ' -------------------------------- ' Do While NextTarget() ' Sets tgtName, tgtRA, tgtDec ' ' First, aim the scope/camera at the object ' Log " " Log "Target " & tgtName Log " Aiming " Message = "Aiming " & tgtName Caption = Message AimCamera tgtRA, tgtDec 'Take Astrometry image temp = InStr(tgtName, " ") - 1 ' remove following spaces fn = imageDir & Left(tgtName,Temp) & ".fts" ' set file name, removeing spaces in name Log " Take Astrometry Image " & fn Message = "Taking Astrometric Image " & fn Caption = Message TakePicture astExpInterval, fn, useGuider ' 1 uses guider Log " Completed image " & fn Loop ' For each observation/object Log "Completed target set " & setNum Caption = "Completed target set " CloseTargetList ' ' Close out the Log file ' Log "All " & obsPerObject & " sets are complete. End of run." Caption = "All sets are complete. End of run." ' ' shut down. ' Log "Shutting down camera. Please wait..." If Testit = 0 Then Caption = "Shutting down camera. Please wait." ' cam.CoolerOn = False Delay(10) cam.LinkEnabled = False End If '''''''''''''' stop RA drive ( go East, rate=1) Game.AddScore 2560000000+2 ' turn of RA drive for 10,000 seconds Log "Shutdown complete." logStream.Close End Sub ' ' ----------- ' AimCamera() - Point the scope right at the object ' ----------- ' Function AimCamera(RA, Dec) ' Returns True/False Dim dRA, dDec, code ''''''''''''''''''''''''''''''''''' 'message = "" 'msgbox(message) message = "Aiming " & tgtName Caption = message 'Game.AddScore 256*100 + 7 + 112 ' load pulse codes 'Game.AddScore 119 dRA = 15 * (tgtRA - last_RA) ' tgtRA, Last_ra in decimal hours; convert to degrees dDec = (tgtDec - last_Dec) ' delta in dec Flag = 1 ' following usefull only if there is no searching for guide star ' if (dDec < 1 AND dRA < 1) then flag = 0 ' don't take pointing exposure! if dRA >= 0 Then RA_code = RAE_Code dRA = int(1000*dRA/RAE_rate) '' msec to move End If If dRA < 0 then RA_code = RAW_Code dRA = int(-1000*dRA/RAW_rate) '' msec to move End if dDec = int(1000 * dDec / Dec_rate) '' msec to move in dec if dDec >= 0 then control_code = RA_code + DecN_Code Dec_code = DecN_Code End if if dDec < 0 then control_code = RA_code + DecS_Code Dec_code = DecS_Code dDec = dDec * -1.0000 End if message = tgtname & "dRA " & int(dRA/1000) & ", dDec = " & int(dDec/1000) & " " & control_code caption = message log tgtname & " dRA = " & dRA/1000 & ", dDec = " & dDec/1000 & " control = " & control_code if dRA < dDec then code = control_code + dRA * 256 Game.AddScore code control_code = Dec_code time2 = dDec - dRa code = control_code + time2 * 256 Game.AddScore code End If if dRA > dDec then code = control_code + dDec * 256 Game.AddScore code control_code = RA_code time2 = dRa - dDec code = control_code + time2 * 256 Game.AddScore code End If message = "Remainder " & " Time = " & time2/1000 & " " & control_code caption = message log " Remainder " & tgtname & " Time = " & time2/1000 & " control = " & control_code If Flag = 1 Then 'Moving more than 1 degree in RA or Dec; take pointing exposure ' ' Now take the exposure to be used for pointing. Save the pointing ' exposures for later analysis, in case some of them don't solve. ' The pointing image file names are formed similarly to the main ' ones. See the notes in main() about parenthesis removal. ' temp = InStr(tgtName, " ") -1 ' to remove following spaces fn = imageDir & "\p-" & Left(tgtName,Temp) & ".fts" Caption = "Take pointing exposure for " & fn Log " Take pointing exposure for " & fn temp=0 ' if temp = 0, does not start guider TakePicture regExpInterval, fn, 0 temp=1 ' temp = 1, so starts guider in takePicture ' ' Use Charon (modified) to plate-solve the image and get back the ' real center point RA/Dec (J2000). ' Log " plate-solve image, find real RA and Dec" ' write file which has image data for Charon: ' Filename ' TargetRA (decimal hours) ' TargetDec (decimal degrees) ' Num_fit ( return only....number of stars fit) Set Charon = CharonFSO.CreateTextFile("c:\charon\Charon.pos", True) ' True so will overwrite Charon.WriteLine fn Charon.WriteLine tgtRA Charon.WriteLine tgtDec Charon.Close message = "Solving " & tgtName Caption = message ' now send over to Charon to return position on error resume Next Game.NextLevel ' Yes it is a stupid name, but this is really charon in disguise on error goto 0 ' Clean up ' now read back solved data Set Charon = CharonFSO.OpenTextFile("c:\charon\Charon.pos", 1) ' 1 is for reading Charon.ReadLine Found_Ra = Charon.ReadLine Found_Dec = Charon.ReadLine StarsSolved = Charon.ReadLine Charon.Close If StarsSolved < 4 Then Log " Not solved! " & StarsSolved & " stars matched" log " position at RA " & Last_RA & " dec " & Last_Dec Else ' Fine Tune scope pointing if solved Last_RA = Found_RA Last_Dec = Found_Dec Log " ok! " & StarsSolved & " stars matched" log " Position at RA " & Last_RA & " dec " & Last_Dec log " Wanted RA = " & tgtRA & " Dec = " & tgtDec ''''''''''''''''''''''''''''''''''' dRA = 15 * (tgtRA - last_RA) 'x15 to convert decimal hours into decimal degrees if dRA >= 0 Then RA_code = RAE_Code dRA = int(1000*dRA/RAE_rate) '' msec to move End If If dRA < 0 then RA_code = RAW_Code dRA = int(-1000*dRA/RAW_rate) '' msec to move End if dDec = int(1000 * (tgtDec - last_Dec) / Dec_rate) '' msec to move in dec if dDec >= 0 then control_code = RA_code + DecN_Code '80 '240 Dec_code = DecN_Code '80 '240 End if if dDec < 0 then control_code = RA_code + DecS_Code Dec_code = DecS_Code dDec = -1.0000 * dDec End if message = "Refine " & tgtname & "RA " & int(dRA/1000) & ", Dec " & int(dDec/1000) & " code=" & control_code caption = message log " Refine " & tgtname & "dRA = " & dRA/1000 & ", dDec = " & dDec/1000 & " code=" & control_code if dRA < dDec then code = control_code + dRA * 256 Game.AddScore code control_code = Dec_code time2 = dDec - dRa code = control_code + time2 * 256 Game.AddScore code End If if dRA > dDec then code = control_code + dDec * 256 Game.AddScore code control_code = RA_code time2 = dRa - dDec code = control_code + time2 * 256 Game.AddScore code End If message = "Remainder " & " Time = " & time2/1000 & " " & control_code caption = message log " Remainder " & tgtname & " Time = " & time2/1000 & " code= " & control_code Log " Camera aimed at the target" End If ' stars solved >= 4, so moved scope End If ' for flag for motion < 1 degree ''''''''' assume pointed correctly last_RA = tgtRA last_Dec = tgtDec End Function ' ------------- ' TakePicture() - Take a picture, save as FITS ' ------------- ' ' Any problems with the camera are fatal, will cause an error ' to be raised. This shouldn't happen unless there's some ' real trouble with the camera or the connection, so we might ' as well just halt the proceedings anyway. ' ' This may be called while On Error Resume Next is effective, ' so we also return a boolean for manual checking. ' ' If Interval is negative, it means take a dark frame. ' Function TakePicture(Interval, File, GuiderValue) ' guider_value=1 uses Const CamTimeout = 60 ' Timeout (added to interval) Dim Timeout Dim Light Dim ReturnValue If Interval < 0 Then Light = 0 Interval = -Interval Else Light = 1 End If ' ' Start the exposure ' If Testit = 0 Then If GuiderValue = 1 Then ' 1 means use guider during exposure cam.GuiderDeclination = tgtDec cam.GuiderAutoSelectStar = True Temp = 0 ' Search for guide star..... On Error Resume Next ' Failure here should not be fatal do Game.AddScore MoveGuider(temp) ReturnValue = cam.GuiderExpose(GuiderExp) if ReturnValue = false then RestartCamera on error goto 0 ' Failure here should be fatal Cam.GuiderExpose(GuiderExp) On Error Resume Next end if do while cam.GuiderRunning loop temp = temp + 1 Caption = "Search for guide star, try " & temp Log " Search for guide star, try " & temp err.clear ' clear any error codes cam.GuiderTrack(GuiderExp) ' if fails (no guide star) err.Number != 0 loop while ((err.Number <> 0 ) AND (Temp < 25)) On Error Goto 0 Caption = "Guider started" Log " Guider started" Delay(2*GuiderExp + 10) End If If cam.XYBinning Then cam.BinY = 2 'returns true if x and y binning can be separate cam.BinX = 2 ' set x or X and Y binning dim responce log " Beginning astrometric exposure of " & tgtName on error resume next responce = cam.Expose(Interval, 1, 0) if responce = false then RestartCamera on error goto 0 ' Failure here should be fatal temp = cam.Expose(Interval, 1, 0) On Error Resume Next End If caption = "Exposing " & tgtName & " " & responce Log " Exposure started " & responce ' If Not cam.Expose(Interval, 1, 0) Then ' TakePicture = False ' Err.Raise 32768, "MaxIm.CCDCamera", _ ' "Failed to start the exposure" ' End If ' ' Wait till it's finished or times out ' Timeout = CamTimeout + Interval ' Total timeout, sec. log " Starting timeout loop." Do While Not cam.ImageReady And Timeout > 0 Delay(1) Timeout = Timeout - 1 Loop log " Finished timeout loop or exposure. Time left = " & timeout If Timeout = 0 Then ' Timed out, failed TakePicture = False Err.Raise 32768, "MaxIm.CCDCamera", _ "Exposure failed after being successfully started." log "###### Exposure failed after being successfully started." End If ' ' Save the image and return success ' End If Caption = File TakePicture = True If Testit = 0 Then cam.SaveImage(File) ' CStr() SHOULD NOT BE NEEDED! cam.GuiderStop End IF End Function ' ' ---------------- ' OpenTargetList() - Open the target list file ' ---------------- ' Sub OpenTargetList() Const ForReading = 1, ForWriting = 2 ' ' First time through, we create a FileSystemObject. We leave this ' laying around so as not to re-create it every pass. When the ' script exits, this thing will get destroyed at that time. ' If Not IsObject(fso) Then Set fso = CreateObject("Scripting.FileSystemObject") 'Set tlFSO = CreateObject("Scripting.FileSystemObject") End If ''' open file Set infile = fso.OpenTextFile(Input_File, ForReading) ' take initial pointing exposure to starting location; log position to last_RA, last_dec NextTarget() ' get first position Log " Take INITIAL pointing exposure" & tgtName temp=InStr(tgtName, " ") -1 fn = imageDir & "p-" & left(tgtName,temp) & ".fts" TakePicture regExpInterval, fn, 0 Log " Find image RA and Dec" ' write coordinate and file data for Charon Set Charon = CharonFSO.CreateTextFile("c:\charon\Charon.pos", True) ' True so will overwrite Charon.WriteLine fn Charon.WriteLine tgtRA Charon.WriteLine tgtDec Charon.Close message = "Solving " & tgtName Caption = message ' now send over to Charon to return date Game.NextLevel ' Yes it is a stupid name, but this is really charon in disguise ' now read back solved data Set Charon = CharonFSO.OpenTextFile("c:\charon\Charon.pos", 1) ' 1 is for reading Charon.ReadLine Last_Ra = Charon.ReadLine Last_Dec = Charon.ReadLine StarsSolved = Charon.ReadLine Charon.Close if StarsSolved < 4 Then Caption = "Did not solve image. Crash" AimCamera = False End If Log " Ok! " & StarsSolved & " stars matched" log " Initial position at RA " & Last_RA & " dec " & Last_Dec on error goto 0 ' Clean up Message = "At " & last_RA & ", " & last_Dec Caption = Message 'msgbox(last_RA) End Sub ' ------------ ' NextTarget() - Read the next target, return False at EOF ' ------------ ' ' Places the target Name/RA/Dec into tgtName, tgtRA, tgtDec, and ' returns True, or returns False if at EOF. ' File format is from Lowell Observatory's SLOP page: each line has object ' name (plus identifying letter so not all 3 images saved in the same file!) ' in characters 7-14, RA in 76-81, and Dec in 83-88 (with sign if necessary) ' Function NextTarget() dim WaitTime, current Do Do While Not infile.AtEndOfStream buf = infile.ReadLine ' Get next target list line if buf <> "" Then Exit Do ' Non-blank line, press on Loop ' Loop to skip blank lines If infile.AtEndOfStream Then ' At EOF NextTarget = False ' Return False Exit Function ' DONE End If Message = buf Caption = Message Log Message tgtName = Mid(buf, 7, 17) ' 7-24 is the name 'test if "focus" If 0 = StrComp(Mid(buf,7,5), "focus", 1) Then ' search for FOCUS in name. if not there returns 0 focus() ' if it is, then focus, and read next parameters CAPTION = "FOCUSING!" ' 'test if a time ElseIf 0 = StrComp(Mid(buf,9,1), ":", 1) Then 'it is a time WaitTime = Mid(buff, buf, 5) ' time in columns 7-11 XX:XX do current = FormatDateTime(TimeUTC(), 4) message = "Wait = " & waittime & " current = " & current loop while StrComp(WaitTime, current, 1) > 0 End If loop while 0=StrComp(Mid(buf,7,5),"focus",1) OR 0=StrComp(Mid(buf,9,1),":",1) ' ' Get the RA/Dec (J2000) of the object IN DECIMAL HOURS AND DEGREES ' tgtRA = CDbl(Mid(buf, 76, 6)) ' following for convert min to decimal hours+ (CDbl(Mid(buf, 14, 5)) / 60.0) ' decimal hours tgtDec = CDbl(Mid(buf, 83, 6)) ' Degrees (need to keep sign) Caption = tgtName 'msgbox(tgtRA) 'msgbox(tgtDec) NextTarget = True End Function ' ----------------- ' CloseTargetList() ' ----------------- ' Sub CloseTargetList() infile.Close Set infile = Nothing End Sub ' ======================= ' Miscellaneous Functions ' ======================= ' ----- ' Log() - Log a line of text with a UTC timestamp, echo to console & voice ' ----- Sub Log(t) ' Caption = t logStream.WriteLine FormatDateTime(TimeUTC(), 4) & " " & t ' If Voice.Active Then Voice.Speak t End Sub ' --------- ' UTCTime() - Return current UTC time of day in OLE Date format ' --------- Function TimeUTC() TimeUTC = CDate(Time) End Function Function delay(N) ' delay N seconds Dim StartTime, EndTime StartTime = Timer EndTime = Timer + N ' Msgbox("N, StartTime, EndTime " &Endtime) Do ' Msgbox(Timer) Loop while EndTime > Timer End Function sub RestartCamera() log "######### Exposure start failed. Restarting camera. ########" cam.LinkEnabled = True if Not cam.LinkEnabled Then msgbox("Failed to start camera.") ' Exit Sub End If Log " Camera is connected. Please wait..." Delay(5) cam.CoolerOn = True cam.TemperatureSetpoint = cameraTemp Log " Waiting for cooler to start..." Caption = "Waiting for cooler to start." Delay(60) Log " Camera is ready, restarting observing run." responce = cam.Expose(Interval, 1, 0) End Sub '--------------------------- 'Sub Focus() '--------------------------- Sub Focus() Dim XSize, YSize Dim ImageF ImageF = Array(510,340) Dim Count, x, y, FocusLoops Dim FocusDir ' Focus direction = 1 or -1 msec Dim Flux, PrevFlux, DeltaFlux, value Dim PosX, PosY, Bkgnd Dim FocusExposure FocusLoops = 0 FocusDir = -1 FocusExposure = 5 Log " Starting Focus" Count = 0 PrevFlux = 10 dim Movetime movetime = FocusMoveTime cam.BinY = 3 cam.BinX = 3 cam.StartX = 0 cam.StartY = 0 cam.NumX = 510 cam.NumY = 340 x = cam.Expose(FocusExposure, 1, 0) if x = false then RestartCamera x = cam.Expose(FocusExposure, 1, 0) End If do while NOT cam.ImageReady loop ImageF = cam.ImageArray ' find brightest value = 0 Caption = " start scan image " For x = 100 to 350 For y = 75 to 250 If ImageF(x,y) > value then value = ImageF(x,y) PosX=x PosY=y End If Next Next Caption = "End scan" Log " Star of brightness " & value & " at " & PosX & ", " & PosY Caption = "Brightness " & value & " at " & PosX & ", " & PosY ' set coodrinates cam.BinY = 1 cam.BinX = 1 'CHANGE TO THIS FOR REAL CAMERA cam.StartX = 3*(PosX ) - 20 ' star will drift to higher x cam.StartY = 3*(PosY) - 25 ' THIS FOR SIMULATOR ONLY 'cam.StartX = (PosX + 100) - 30 'cam.StartY = (PosY + 75) - 25 cam.NumX = 120 cam.NumY = 50 x = cam.Expose(FocusExposure, 1, 0) do while NOT cam.ImageReady loop ImageF = cam.ImageArray ' find brightest value = 0 For x = 11 to 30 For y = 15 to 35 If ImageF(x,y) > value then value = ImageF(x,y) PosX=x PosY=y End If Next Next Log " Star of brightness " & value & " at " & PosX & ", " & PosY Caption = "Brightness " & value & " at " & PosX & ", " & PosY bkgnd = (ImageF(PosX-10,PosY) +ImageF(PosX+10,PosY)+ImageF(PosX,PosY-10)+ImageF(PosX,PosY+10))/4 ' set FocusExposure time to a reasonable value FocusExposure = FocusExposure * 1500/(value-bkgnd) if focusexposure >10 then focusexposure = 10 log " FocusExposure set to " & FocusExposure Caption = "FocusExposure set to " & FocusExposure Do ' Average 5 ImageFs before changing focus flux = 0 ' total brightness For Count = 1 to 5 x = cam.Expose(FocusExposure, 1, 0) do while NOT cam.ImageReady loop ImageF = cam.ImageArray ' find brightest value = 0 For x = PosX-8 to PosX + 15 For y = PosY-8 to PosY+10 If ImageF(x,y) > value then value = ImageF(x,y) PosX=x PosY=y If PosX < 9 then PosX = 9 If PosY < 9 then PosY = 9 If PosX > 104 then PosX = 104 If PosY > 39 then PosY = 39 End If Next Next caption = "Pass " & Count & " Brightness " & value flux = flux + value Next ' now change focus DeltaFlux = Flux - PrevFlux PrevFlux = Flux If DeltaFlux < -0.015 then ' allow for atmosphere problems FocusDir = -1 * FocusDir ' change direction If FocusDir > 0 then MoveTime = int(movetime/2) ' only halve from one direction End If log " Brightness " & int(Flux/5) & " at " & PosX & ", " & PosY & "; focus moved "& FocusDir Caption = "Bright " & int(Flux/5) & " at " & PosX & ", " & PosY & " focus "& FocusDir If ((abs(DeltaFlux/Flux) < AcceptableFocus) AND movetime < 75 ) then Exit Do If FocusDir = 1 Then Game.AddScore MoveTime * 256 + 160 Else Game.AddScore MoveTime * 256 + 176 End If ' if star too far from center, recenter! If ((PosX < 20) OR (PosX > 55) OR PosY < 12 or PosY > 38) then cam.StartX = cam.StartX - (20-PosX) cam.StartY = cam.StartY - (25-PosY) log " Recentered Star; at " & cam.StartX & ", " & cam.StartY caption = "Recentered star" x = cam.Expose(FocusExposure, 1, 0) do while NOT cam.ImageReady loop ImageF = cam.ImageArray ' find brightest value = 0 For x = 11 to 54 For y = 11 to 29 If ImageF(x,y) > value then value = ImageF(x,y) PosX=x PosY=y End If Next Next End If FocusLoops = FocusLoops + 1 Loop While (FocusLoops < 30) log " Finished Focusing. Improvement ratio = " & Deltaflux/flux Caption = "Finished Focus. ratio = " & Deltaflux/flux cam.SetFullFrame() cam.BinY = 2 cam.BinX = 2 End Sub