TakePicture function

by Jeff Medkeff

This is a modular function for taking images under an ASCOM camera driver. It accepts the duration of the exposure ("interval") and the filename ("NameOfFile") and returns a boolean for success or failure.



Function TakePicture(Interval, NameOfFile)
    Const CamTimeout = 120             ' Timeout (added to interval)
    Dim Timeout
    Dim Light
    Dim Counter
    Dim mns
    Dim scs

    ' Set dark frame if needed
    If Interval < 0 Then 
        Light = 0
        Interval = -Interval
    Else 
        Light = 1
    End If

    ' Get a guide star if needed
    If GuideThisExposure=True Then
       GuideExposure
    End If

    ' Remember that subframe sizes might be set elsewhere. This
    ' uses the camera as it is presently set.
    If Not cam.Expose(Interval, Light, 0) Then
        TakePicture = False
        Donescope   ' we park in case of a fatal error like this.
        Err.Raise 32768, "MaxIm.CCDCamera", _
                    "Failed to start the exposure"
    End If

    ' Wait till it's finished or times out
    Timeout = (CamTimeout * 2) + (Interval * 2.2)  ' Total timeout, half seconds.

    ' Calculate time that image will be ready
    FinishTime = DateAdd("s", Interval, Now)

    Do While Not cam.ImageReady And Timeout > 0

        ' Half second wait
        Util.WaitForMilliseconds 500

        ' Calculate time to finish
        secs = DateDiff("s", Now, FinishTime)

        mns = Int(secs / 60)
        scs = Int(((secs / 60) - mns) * 60)

        ' Keep console up to date.
        Console.Clear
        Console.PrintLine "Imaging " & NameOfFile & ", set " & setNum & ", plan " & plannum & "."
        If GuideThisExposure = True And GuideFail = 0 Then
          Console.PrintLine "Guiding at " & (GuideInterval * 1.25) & " seconds."
        Else
          Console.PrintLine "Unguided."
        End If
        If secs > .9 Then
          Console.PrintLine mns & " minutes, " & scs & " seconds left."
        Else
          Console.PrintLine "DOWNLOADING IMAGE."
        End If

        Timeout = Timeout - 1

    Loop

    If Timeout = 0 Then                 ' Timed out, failed
        TakePicture = False
        Donescope   ' we park in case of a fatal error like this.
        Err.Raise 32768, "MaxIm.CCDCamera", _
                    "Exposure failed after being successfully started."
    End If

    ' Save the image and return success
    cam.SaveImage CStr(NameOfFile)            ' See note below.
    TakePicture = True
    
End Function



Note: In the line cam.SaveImage CStr(NameOfFile), the CStr() is included only for purposes of backward-compatibility. A bug in an early version of the camera interface in Maxim required its use. Later, the programmer who wrote the routine that this routine is based on used file in place of NameOfFile; the problem with this is that file is reserved in VBScript. So under that regime, CStr() was also needed.