Robin if this is not implemented (surely in a more efficient way by you) in 3.3 as a sequencer step, Will I be able to call it as a scheduled script pasing a param in the new sequencer?
Code: Select all
import time
import sys
from System import Activator
from System import Type
from System.Environment import GetFolderPath, SpecialFolder
from SharpCap.Base import NotificationStatus
############ EXPOSURE SHOULD BE INTRODUCED AS EXTERNAL PARAMS
#exposure_ms = sys.argv[1]
exposure_ms = 500
#################################################
focus_steps = 500
settle_time = 2
average_frames = 10
extra_process_time_ms = 1200
wait_fwhm = (exposure_ms + extra_process_time_ms) / 1000
SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = exposure_ms
SharpCap.SelectedCamera.RestartPreview()
TemperHum = Activator.CreateInstance(Type.GetTypeFromProgID("ASCOM.OCH.ObservingConditions"))
TemperHum.Connected = True
counter = 0
while TemperHum.Temperature is 0 and counter < 5:
time.sleep(0.01)
counter += 1
temperature = round(TemperHum.Temperature, 2)
SharpCap.Focusers.SelectedFocuser.Connected = True
pos_2 = SharpCap.Focusers.SelectedFocuser.Position
pos_1 = pos_2 - focus_steps
pos_3 = pos_2 + focus_steps
SharpCap.Transforms.SelectTransform("FWHM Measurement")
SharpCap.ShowNotification('Acquiring FWHM Center')
fwhm_2 = 0.0
for repeats in range(average_frames):
fwhm_2 += SharpCap.Transforms.SelectedTransform.FrameTransform.CurrentScore
time.sleep(wait_fwhm)
fwhm_2 /= average_frames
SharpCap.ShowNotification('Moving In')
SharpCap.Focusers.SelectedFocuser.Move(pos_1)
while SharpCap.Focusers.SelectedFocuser.IsMoving:
time.sleep(1)
Application.DoEvents()
time.sleep(settle_time)
SharpCap.ShowNotification('Acquiring FWHM In')
fwhm_1 = 0.0
for repeats in range(average_frames):
fwhm_1 += SharpCap.Transforms.SelectedTransform.FrameTransform.CurrentScore
time.sleep(wait_fwhm)
Application.DoEvents()
fwhm_1 /= average_frames
SharpCap.ShowNotification('Moving Out')
SharpCap.Focusers.SelectedFocuser.Move(pos_3)
while SharpCap.Focusers.SelectedFocuser.IsMoving:
time.sleep(1)
Application.DoEvents()
time.sleep(settle_time)
SharpCap.ShowNotification('Acquiring FWHM Out')
fwhm_3 = 0.0
for repeats in range(average_frames):
fwhm_3 += SharpCap.Transforms.SelectedTransform.FrameTransform.CurrentScore
time.sleep(wait_fwhm)
Application.DoEvents()
fwhm_3 /= average_frames
if fwhm_1 < fwhm_2 < fwhm_3:
SharpCap.ShowNotification('Focus below In point. Moving to In point.', NotificationStatus.Error)
SharpCap.Focusers.SelectedFocuser.Move(pos_1)
while SharpCap.Focusers.SelectedFocuser.IsMoving:
Application.DoEvents()
time.sleep(1)
SharpCap.ShowNotification('Redo.', NotificationStatus.Error)
else:
if fwhm_1 > fwhm_2 > fwhm_3:
SharpCap.ShowNotification('Focus above Out point. Moving to Out point.', NotificationStatus.Error)
SharpCap.Focusers.SelectedFocuser.Move(pos_3)
while SharpCap.Focusers.SelectedFocuser.IsMoving:
Application.DoEvents()
time.sleep(1)
SharpCap.ShowNotification('Redo.', NotificationStatus.Error)
else:
if fwhm_1 > fwhm_2 < fwhm_3:
target = - int((pos_3**2 * (fwhm_1 - fwhm_2) + pos_2**2 * (fwhm_3 - fwhm_1) + pos_1**2 * (fwhm_2 - fwhm_3)) / (2 * (pos_3 * (fwhm_2 - fwhm_1) + pos_2 * (fwhm_1 - fwhm_3) + pos_1 * (fwhm_3 - fwhm_2))))
archivolog = GetFolderPath(SpecialFolder.Desktop) + "\FOCUSLOG\LOG.txt"
logfile = open(archivolog, "a")
logfile.write(time.strftime("%Y%m%d") + "/" + time.strftime("%H%M%S") + " - " + str(pos_2) + " > " + str(target) + " - " + str(round(temperature,2)))
logfile.write(" - " + str(focus_steps) + " - " + str(round(fwhm_1, 2)) + " - " + str(round(fwhm_2, 2)) + " - " + str(round(fwhm_3, 2)) + "\n")
logfile.close()
SharpCap.ShowNotification('Moving to new focus point')
SharpCap.Focusers.SelectedFocuser.Move(target)
while SharpCap.Focusers.SelectedFocuser.IsMoving:
Application.DoEvents()
time.sleep(1)
SharpCap.ShowNotification('New focus position: ' + str(target) + '. Focus moved ' + str((target - pos_2)) + ' steps.')
else:
SharpCap.ShowNotification('Invalid data ' + str(round(fwhm_1, 2)) + " - " + str(round(fwhm_2, 2)) + " - " + str(round(fwhm_3, 2)) + '. Moving to Center point.', NotificationStatus.Error)
SharpCap.Focusers.SelectedFocuser.Move(pos_2)
while SharpCap.Focusers.SelectedFocuser.IsMoving:
Application.DoEvents()
time.sleep(1)
SharpCap.ShowNotification('Redo.', NotificationStatus.Error)