QHY 174 GPS Calibration LED issue // USB Traffic weirdness

Post Reply
User avatar
admin
Site Admin
Posts: 13271
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#61

Post by admin »

Hi,

Just to confirm that I've made this change and that it will be available in the next SharpCap 3.2 update.

Cheers, Robin
Kai Getrost
Posts: 21
Joined: Sun Jun 07, 2020 2:36 pm
Location: US

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#62

Post by Kai Getrost »

admin wrote: Fri Apr 24, 2020 6:32 pm ... After a lot of investigation we discovered that if you don't have accurate calibration of the end and begin points then the output of the GPS system can be badly off (much more than you would think if you had the idea that the calibration was just fine tuning mechanism). ... The 'bad data' status means that the time that came out of the GPS reading is more than 24 hours different from the PCs system clock.

Cheers, Robin
Following up on this statement from back in April. How bad is "badly off"? I've noticed two consequences to bad calibration, depending on how severely off it is:
  1. Completely frozen (unchanging) GPS time and BadCalibrationReduceEndPos (or sometimes BadData) error, or
  2. GPS window reported exposure (`Exp. (us)') differing greatly from the GUI-set Exposure -- like by a factor of 1.5x, 2x, even 0x different from what was set
Are there other known consequences, especially ones that might not be easily noticed? Thanks.

-Kai
User avatar
admin
Site Admin
Posts: 13271
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#63

Post by admin »

Hi,

the only other ones I can think of is that the calibration values control the reported timestamps of the start and end of the frame relative to the GPS clock. As you noticed the calibration values are counters against 75 MHz clock, so if both were set 7,500,000 too high for instance then the frame duration may come out correct but the timestamps would be off by 0.1 second.

Cheers, Robin
User avatar
admin
Site Admin
Posts: 13271
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#64

Post by admin »

Hi,

I've had all sorts of fun and games with my main PC over the last week – in the end I think I've tracked the problem down to a misbehaving USB add-in card which is now removed - but while I had the problem I could only get my QHY 174 GPS to work on USB2.

What I've noticed while running the camera on USB 2 is that the calibration values differ from those we've measured on USB 3 – for instance if you select a 0.5 second exposure at the default USB traffic value of 50 on USB3, the correct start calibration value is about 62,000. Switch to USB2 and the correct values about 215,000.

Firstly, can anyone else confirm this discrepancy to be sure that it's not just my camera giving weird results. I'm still on the old firmware (really must get round to upgrading that sometime soon).

Secondly, the offset involved in getting the calibration wrong by this amount is about 2 ms – is this significant? I'm sure we could redo our measurements of the calibration values for USB 2, but I'm not sure that SharpCap can tell the difference between the camera connected over USB 3 and the camera is connected over USB 2 reliably, which would be a problem!

Cheers, Robin
procyon12
Posts: 255
Joined: Tue Jan 14, 2020 11:32 am

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#65

Post by procyon12 »

Hi Robin,

tested under W7-64 with the latest SC 32bit Pro I find:

With USB2, 500ms, traffic 50, full frame, 16bit, 1x1 I get with the default auto-calibration values a GPS-lock and, as indicated in the GPS status window, the right exposure time.

[QHY174M]
Output Format=SER file (*.ser)
Binning=1x1
Capture Area=1920x1200
Colour Space=MONO16
Pan=0
Tilt=0
Force Still Mode=Off
Enable Live Broadcast=Off
USB Traffic=50
Offset=0
Amp Noise Reduction=On
Frame Rate Limit=Maximum
Gain=480
Exposure=500
Calibration End Pos Adjust=37546447
Calibration Start Pos Adjust=62301
GPS Calibration LED=On
GPS Freq Stabilization=Off
GPS=On
Timestamp Frames=Off
Contrast=0
Brightness=0
Gamma=1
Temperature=34.9
Target Temperature=0
Cooler Power=255(Auto)
Banding Threshold=35
Banding Suppression=0
Apply Flat=None
Subtract Dark=None
#Black Point
Display Black Point=0
#MidTone Point
Display MidTone Point=0.5
#White Point
Display White Point=1
Notes=
TimeStamp=2020-08-03T13:28:30.0568282Z
SharpCapVersion=3.2.6396.0


However, If I try from this locked state a manual change/calibration of the StartPos, I can run into some problems.

Enhance the StartPos to maximum 9999.... and then reduce it until the LED goes on and then off, you will probably find several points where this occurs (and also sometimes a point, where the LED is unusual bright).
This occurs with USB3 and USB2. It is something like over-determination - I always choose the minimum value or the auto-calibration, this works. Criterion is the locked state and the indication of the right exposure.
In the above bad cases the exposure often differs to the chosen one.
See also http://www.occultations.org/meetings/NA ... trost.pptx, Kai Getrost found this problem too and calls it "Third-transition issue", slide 50 ff (You will find there some additional things ...). There is also a youtube recording of Kai's presentation: https://www.youtube.com/watch?v=1myioXA ... ex=19&t=0s

Generally I think the camera is not intended for USB2 use. Especially for our occultation work USB2 is not an option. So, from my point of view, a deep USB2 research is not absolutely necessary.

Cheers, Christian
Kai Getrost
Posts: 21
Joined: Sun Jun 07, 2020 2:36 pm
Location: US

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#66

Post by Kai Getrost »

procyon12 wrote: Mon Aug 03, 2020 2:15 pm Enhance the StartPos to maximum 9999.... and then reduce it until the LED goes on and then off, you will probably find several points where this occurs [...]
[...] Kai Getrost found this problem too and calls it "Third-transition issue",
Hadn't thought to explore it from the high (999...) end on down as well, to see if there are even more than the 3 transitions I saw. Only looked from 0 on up until I could calibrate both Start and End.
procyon12 wrote: Mon Aug 03, 2020 2:15 pm ...
Generally I think the camera is not intended for USB2 use. Especially for our occultation work USB2 is not an option. So, from my point of view, a deep USB2 research is not absolutely necessary.
Agreed. We are experiencing enough issues with the intended USB3 usage. Speaking of which, I'd like to explore some of my suggested fixes/workarounds, as mentioned in the second-to-last slide of my presentation that Christian mentioned (at http://www.occultations.org/meetings/NA ... trost.pptx).

The first priority for me: being able to adjust the auto-preset formula for Start and End Pos that you and Christian developed. It works for most folks, but a number of us find that it's just a bit off for our cameras -- typically a few thousand for me. That's not much of an issue from an accuracy standpoint, but the problem is when the auto-preset lands too high -- it triggers what I call the `End Pos cliff', where timestamps freeze and `BadCalibrationReduceEndPos' is displayed.

And for me, this happens for nearly every manual Exposure change -- so I must calibrate all possible Exposures I want to use beforehand. If the auto-preset formulas could be shown in SharpCap Settings -- and we could change the factors and offsets in it -- then I could probably tune it once to err on the low side of the End Pos cliff, and not trigger BadCal... all the time -- nor have to calibrate as much (especially when just playing around with different Exposures). Sort of like an uber-calibration.

Anyone else have the auto-presets for End Pos trigger the BadCalibrationReduceEndPos error (and/or freeze timestamps) often, when changing Exposure? I hate to complicate Settings even more, but currently most of the time I can't change Exposure without calibrating. Since the auto-presets seem to work for most people, this would need to be a setting rather than a permanent fixed change.
User avatar
admin
Site Admin
Posts: 13271
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#67

Post by admin »

Hi,

I'm certainly open to tweaking the calibration algorithm further – either bringing down the calculated end position values globally to ones that don't trigger the frozen timestamp bug or adding an offset that can be adjusted and is applied to the calculated calibration values. Since I only develop the software rather than use it in anger I'm open to suggestions on this.

On the subject of the multiple calibration values, my assumption here is that they will be found throughout the range going up in steps that roughly match the exposure time – that is, if you set the start calibration much too high then you will start firing the LED during the course of following frame, or the one after that. I have also seen cases where the LED shows when the start calibration is set to values very close to 0 – I think this arises because the LED is being turned on before the previous frame has finished. I'm assuming that the correct calibration values are:

Start POS : first dark to light transition above zero when increasing the start pos value
End POS : first light to dark transition above the start pos value when increasing the end pos value.

Thanks, Robin
User avatar
admin
Site Admin
Posts: 13271
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#68

Post by admin »

Hi,

is anyone able to validate that they still get the same calibration results that we measured back in February to produce the auto calibration formula?

I've been in discussion with QHY and they are convinced that our calculations are wrong. I've been testing over the last day or so and I'm certainly seeing different patterns to the ones I would expect based on what we discovered earlier this year. However, I'm unable to make a direct check of the earlier results since my camera has not worked on USB3 since I upgraded the firmware :(

The results I'm seeing on USB 2.0 seem to indicate constant start and end calibration positions with changing exposure – it least over a range of exposures. For instance, at full resolution, MONO8, USB Traffic 0, I get start = ~103000, end = ~31000 for exposure = 160ms, 320ms, 640ms. I can't get a good calibration at 80ms or less with those settings.

I know that a number of things have changed since Feb:

* PC reinstalled, so potentially different QHY drivers
* Camera upgraded firmware
* UBS2 now not USB3
* Newer QHY SDK in SharpCap

I did try going back to a version of SharpCap from February, but that didn't seem to make any difference to the results I was getting.

Cheers, Robin
procyon12
Posts: 255
Joined: Tue Jan 14, 2020 11:32 am

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#69

Post by procyon12 »

Hi Robin,

This would be strange.

Actually I didn't reproduce your tests, but from my expierience there is no hint on this - the "auto-calibration" works very well.

I work with a firmware upgraded QHY under USB3 on W7 and also W10 / latest SC and also latest SC 3.3. And I did not hear anything regarding the issue from the occultation community. Hoewever, I will share a link to your post in our mailing lists.

Cheers

Christian
Jean-Francois
Posts: 382
Joined: Sun Oct 13, 2019 10:52 am
Location: Germany

Re: QHY 174 GPS Calibration LED issue // USB Traffic weirdness

#70

Post by Jean-Francois »

Hello Robin and Christian,

I start my laptop and I will test it with USB 2 and USB 3. I will use my script on the laptop after I send this message.
On my desktop computer (where I do all my tests in the past), I have only USB 3 connectors.

Note: I try to insert some PDF documents with the results of my analysis ... but I have each time an error that the file is too large.
What is the limit ? I have 4 files with ~ 1.1 to 1.2 MB.

In the mean time, you can try my last version of the automatic LED settings.
Please, modify the "Time_List" variable with the time you want.
Please, modify the directory for the result file in code line = 384.
This new version of the script ignores the SC automatic LED parameters. It uses my calculation.

Code: Select all

import time
import math
import System

from System.IO import Directory

directory = Directory.GetCurrentDirectory()

# (Begin) - Parameter to be changed by the user
search_loop = 20						# Number of fine search
power_threshold = 4						# (10**power_threshold) digit
slope = 1.4				                # value between 1.2 and 1.5

#Time_List = [3] # [ms]

#Time_List = [1.0,1.05,1.1,1.15,1.2,1.25,1.3,1.35,1.4,1.45,1.5,1.55,1.6,1.65,1.7,1.75,1.8,1.85,1.9,1.95,2.0] # [ms]
#Time_List = [1.0,1.2,1.4,1.6,1.8,2.0,2.2,2.4,2.6,2.8,3.0,3.2,3.4,3.6,3.8,4.0,4.2,4.4,4.6,4.8,5.0,5.2,5.4,5.6,5.8,6.0,6.2,6.4,6.6,6.8,7.0,7.2,7.4,7.6,7.8,8.0,8.2,8.4,8.6,8.8,9.0,9.2,9.4,9.6,9.8,10.0] # [ms]
#Time_List = [7.05,7.1,7.15,7.2,7.25,7.3,7.35,7.4,7.45,7.5,7.55,7.6,7.65,7.7,7.75,7.8,7.85,7.9,7.95] # [ms]
#Time_List = [1,1.5,2,2.5,3,3.5,4,5,6,7,7.2,7.4,7.6,7.8,8,9,10,11,12,13,14,15,20,40,60,80,100,150,200,250,500,750,1000] # [ms]
#Time_List = [10,20,30,40,50,60,70,80,90,100]
Time_List = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,32,34,36,38,40,42,44,46,48,50] # [ms]
#Time_List = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50] # [ms]
#Time_List = [51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100] # [ms]
#Time_List = [12.1,12.2,12.3,12.4,12.5,12.6,12.7,12.8,12.9] # [ms]
#Time_List = [5,10,20,30,40,50,60,70,80,90,100,150,200,250,300,350,400,450,500,600,800,1000,1300,2000,3000] # [ms]

# (End)   - Parameter to be changed by the user

SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = Time_List[0]
Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
Colour_Space = SharpCap.SelectedCamera.Controls.ColourSpace.Value

Resolution = SharpCap.SelectedCamera.Controls[2].Value
Height = Resolution.split("x")[1]
H = float(Height)

usb = SharpCap.SelectedCamera.Controls[10].Value

LED_status = SharpCap.SelectedCamera.Controls[21].Value
if (LED_status == "Off"):
    SharpCap.SelectedCamera.Controls[21].Value = "On"

print "Date:",  time.ctime()
print SharpCap.SelectedCamera.Controls[18]
print("GPS Calibration LED on/off  : %s") % (SharpCap.SelectedCamera.Controls[21].Value)
print("GPS Freq Stabilization      : %s") % (SharpCap.SelectedCamera.Controls[22].Value)
print("GPS                         : %s") % (SharpCap.SelectedCamera.Controls[23].Value)

cloneRect = SharpCap.Transforms.SelectionRect
print("Rectangle Selection         : "), cloneRect
print

def framehandler(sender, args):
    if (dumpdata):
        global Mean
        cutout = args.Frame.CutROI(SharpCap.Transforms.SelectionRect)
        Stat = cutout.GetStats()
        Mean = Stat.Item1
        cutout.Release()

def evthandler(sender, args):
    if (SharpCap.SelectedCamera != None):
        SharpCap.SelectedCamera.FrameCaptured -= framehandler

def monitorFrames():
    SharpCap.SelectedCamera.FrameCaptured += framehandler

def Search_next_L(M, a):
    k = 0
    while (M[k][1] < a):
        k = k + 1
    return(math.floor((M[k][0] + M[k-1][0]) / 2.0))

def Search_next_R(M, a):
    k = 0
    while (M[k][1] > a):
        k = k + 1
    return(math.floor((M[k][0] + M[k-1][0]) / 2.0))

def LED_Start_search(Pos, LED, n):
    P = Pos
    L = LED
    for i in range(0,n):
        average = (min(L) + max(L)) / 2.0
        M = zip(P,L)
        M.sort()
        Pnew = Search_next_L(M,average)
        SharpCap.SelectedCamera.Controls[20].Value = int(Pnew)
        time.sleep(wait_time)
        P.append(Pnew)
        L.append(Mean)
#        print("%i   %7.3f") % (P[-1], L[-1])
        if (abs((L[-1] - average)/average) < 0.05):
            break
    M = zip(P,L)
    M.sort()
    return(M)

def LED_End_search(Pos, LED, n):
    P = Pos
    L = LED
    for i in range(0,n):
        average = (min(L) + max(L)) / 2.0
        M = zip(P,L)
        M.sort()
        Pnew = Search_next_R(M,average)
        SharpCap.SelectedCamera.Controls[19].Value = int(Pnew)
        time.sleep(wait_time)
        P.append(Pnew)
        L.append(Mean)
#        print("%i   %7.3f") % (P[-1], L[-1])
        if (abs((L[-1] - average)/average) < 0.05):
            break
    M = zip(P,L)
    M.sort()
    return(M)


# LED Calibration Start Position Adjustment
def LED_Start(expos_ms):
    Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
    if (Colour_Space == "MONO16"):
        cal_step = math.floor(math.log10(Cal_Start_Pos)) - 1
    if (Colour_Space == "MONO8"):
        cal_step = math.floor(math.log10(Cal_Start_Pos)) - 1

    Pos = [Cal_Start_Pos]
    if (cal_step > power_threshold):
        cal_step = power_threshold
    print
    print("Digit position selection:  "),
    for p in range(int(cal_step), 0, -1):
        print 10**p,
        Pos.append(Cal_Start_Pos + 10**p)
        Pos.append(Cal_Start_Pos - 10**p)

    Pos.sort()
    print
    print("Cal_Start_Pos list: "), Pos

    LED = []
    loop = 1000
    for L in range(0,len(Pos)):
        SharpCap.SelectedCamera.Controls[20].Value = int(Pos[L])
        
        time.sleep(wait_time)
        LED.append(Mean)
        
        if (L==0):
            print("%i   %8.3f") % (int(Pos[L]), LED[L])
        else:
            print("%i   %8.3f") % (int(Pos[L]), LED[L])
            average = (LED[L]+LED[L-1])/2.0
            diff = LED[L]-LED[L-1]
            if (abs(diff / average) > slope):
                loop = L
        if (loop == (L - 2)):
            print "Break"
            break

    print("Min / Max  : %6.3f   %6.3f") % (min(LED), max(LED))
    print("Half height: %6.3f") % ((min(LED) + max(LED)) / 2.0)
    Pos = Pos[0 : len(LED)]
    M = zip(Pos, LED)
    M.sort()
    time.sleep(wait_time)
    Result = LED_Start_search(Pos, LED, search_loop)
    Pos, LED = zip(*Result)
    print
    mitte = (min(LED) + max(LED)) / 2.0
    l_min = mitte - (mitte - min(LED))*0.9
    l_max = mitte + (max(LED) - mitte)*0.9
    #l_min = min(LED) * 1.1
    #l_max = max(LED) * 0.95
    Pos_m = []
    LED_m = []
    Y_m = []
    for i in range(0,len(Pos)):
        if (LED[i]>l_min) & (LED[i]<l_max):
            Pos_m.append(Pos[i])
            LED_m.append(LED[i])
            Y_m.append(-(math.log((max(LED) - min(LED)) / (LED[i] - min(LED)) - 1.0)))
            print("%i %10.4f") % (Pos_m[-1], LED_m[-1])
    if (len(Pos_m) < 2):
#        SharpCap.SelectedCamera.Controls[20].Value = int(Pos[-1])
        print("Direct value:")
        print("%i %10.4f") % (Pos_m[-1], LED_m[-1])
        return(Pos_m[-1])
    print
    if (len(Pos_m) > 1.5):
        Sx2 = 0
        Sx = 0
        Vx2 = []
        Vx = []
        n = len(Pos_m)

        for i in range(0,len(Pos_m)):
            xi = Pos_m[i] - Pos_m[0]
            Vx.append(xi)
            Vx2.append(xi**2)
            Sx = Sx + xi
            Sx2 = Sx2 + xi**2

        Det = float(Sx2 * n - Sx * Sx)
        Inv = [[n / Det,-Sx / Det],[-Sx / Det,Sx2 / Det]]

        a = 0
        b = 0
        for i in range(0,len(Pos_m)):
            a = a + (Inv[0][0]*Vx2[i] + Inv[0][1]) * LED_m[i]
            b = b + (Inv[1][0]*Vx2[i] + Inv[1][1]) * LED_m[i]
        k = a
        m = -b / a
        Cal_Start_Pos = round(Pos_m[0] + m)
        print("Optimisation: n = %i, k = %10.6f, m = %10.6f, Cal Start Pos = %i") % (n,k,m,Cal_Start_Pos)
        SharpCap.SelectedCamera.Controls[20].Value = int(Cal_Start_Pos)
        return(Cal_Start_Pos)


# LED Calibration End Position Adjustment
def LED_End(expos_ms):
    Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
    SharpCap.SelectedCamera.Controls[19].Value = Cal_End_Pos + 1
    SharpCap.SelectedCamera.Controls[19].Value = Cal_End_Pos - 1

    cal_step = math.floor(math.log10(Cal_End_Pos)) - 1
    Pos = [Cal_End_Pos]
    if (cal_step > power_threshold):
        cal_step = power_threshold
    print
    print("Digit position selection:  "),
    for p in range(int(cal_step), 0, -1):
        print 10**p,
        Pos.append(Cal_End_Pos + 10**p)
        Pos.append(Cal_End_Pos - 10**p)

    Pos.sort()
    print
    print("Cal_End_Pos list: "), Pos

    LED = []
    loop = 1000
    for L in range(0,len(Pos)):
        SharpCap.SelectedCamera.Controls[19].Value = int(Pos[L])

        time.sleep(wait_time)
        LED.append(Mean)

        if (L==0):
            print("%i   %8.3f") % (Pos[L], LED[L])
        else:
            print("%i   %8.3f") % (Pos[L], LED[L])
            average = (LED[L]+LED[L-1])/2.0
            diff = LED[L]-LED[L-1]
            if (abs(diff / average) > slope):
                loop = L
        if (loop == (L - 2)):
            print "Break"
            break

    print("Min / Max  : %6.3f   %6.3f") % (min(LED), max(LED))
    print("Half height: %6.3f") % ((min(LED) + max(LED)) / 2.0)
    Pos = Pos[0 : len(LED)]
    M = zip(Pos, LED)
    M.sort()

    time.sleep(wait_time)
    Result = LED_End_search(Pos, LED, search_loop)
    Pos, LED = zip(*Result)
    print
    mitte = (min(LED) + max(LED)) / 2.0
    l_min = mitte - (mitte - min(LED))*0.9
    l_max = mitte + (max(LED) - mitte)*0.9
    #l_min = min(LED) * 1.1
    #l_max = max(LED) * 0.95
    Pos_m = []
    LED_m = []
    Y_m = []
    for i in range(0,len(Pos)):
        if (LED[i]>l_min) & (LED[i]<l_max):
            Pos_m.append(Pos[i])
            LED_m.append(LED[i])
            Y_m.append((math.log((max(LED) - min(LED)) / (LED[i] - min(LED)) - 1.0)))
            print("%i %10.6f") % (Pos_m[-1], LED_m[-1])
    print
    if (len(Pos_m) < 2):
#        SharpCap.SelectedCamera.Controls[19].Value = int(Pos[-1])
        print("Direct value:")
        print("%i %10.4f") % (Pos_m[-1], LED_m[-1])
        return(Pos_m[-1])

    if (len(Pos_m) > 1.5):
        Sx2 = 0
        Sx = 0
        Vx2 = []
        Vx = []
        n = len(Pos_m)

        for i in range(0,len(Pos_m)):
            xi = Pos_m[i] - Pos_m[0]
            Vx.append(xi)
            Vx2.append(xi**2)
            Sx = Sx + xi
            Sx2 = Sx2 + xi**2

        Det = float(Sx2 * n - Sx * Sx)
        Inv = [[n / Det,-Sx / Det],[-Sx / Det,Sx2 / Det]]

        a = 0
        b = 0
        for i in range(0,len(Pos_m)):
            a = a + (Inv[0][0]*Vx2[i] + Inv[0][1]) * LED_m[i]
            b = b + (Inv[1][0]*Vx2[i] + Inv[1][1]) * LED_m[i]
        k = a
        m = -b / a
        Cal_End_Pos = round(Pos_m[0] + m)
        print("Optimisation: n = %i, k = %10.6f, m = %10.6f, Cal End Pos = %i") % (n,k,m,Cal_End_Pos)
        SharpCap.SelectedCamera.Controls[19].Value = int(Cal_End_Pos)
        return(Cal_End_Pos)


# *****************************************************************************
print
print "Time sequence  [ms] :", Time_List
Cal_Start = []
Cal_End = []
SC_Start = []
SC_End = []
time_start = time.clock()

dumpdata = True
SharpCap.CaptureEvent += evthandler
monitorFrames()

for T in range(0,len(Time_List)):
    SharpCap.SelectedCamera.Controls.Exposure.ExposureMs = Time_List[T]
    expos_ms = SharpCap.SelectedCamera.Controls.Exposure.ExposureMs
    wait_time = 2.1 *(expos_ms / 1000.0) + 0.2
    Cal_End_Pos = SharpCap.SelectedCamera.Controls[19].Value
    Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value

    if (Colour_Space == "MONO16"):
        Time_limit = (usb + 9.76) * (0.001067 * H + 0.0435)
        if (Time_limit > expos_ms):
            StartPos = int(((0.0000390615*H - 0.0707123)*H - 74967.6652)*expos_ms + ((-0.0035966154*H + 86.4884205)*H + 1635.68176)*usb + (-0.0344038914*H + 842.6668063)*H + 16080.628624)
            EndPos = int(((-0.0035558392*H + 86.4002017)*H + 1360.09956)*usb + (-0.0347116673*H + 843.4836578)*H + 13250.53035)
        if (Time_limit <= expos_ms):
            StartPos = int(1120 * usb + 10939)
            EndPos = int(((0.0000163613*H - 0.01334320)*H + 74999.19033)*expos_ms +((0.0000067656*H - 0.01705296)*H + 735.04566)*usb + (-0.0007104888*H + 0.710555)*H + 7515.87826)

    if (Colour_Space == "MONO8"):
        Time_limit = (usb + 5.60) * (0.001067 * H + 0.0421)
        if (Time_limit > expos_ms):
            StartPos = int(((0.0000637116*H - 0.1436327)*H - 74917.10660)*expos_ms + ((-0.00358619*H+86.459717)*H + 1607.70533)*usb + (-0.019717898*H+483.579932)*H + 9199.23345)
            EndPos = int(((-0.0035558*H + 86.4002344)*H + 1360.007826)*usb + (-0.0199132*H + 483.84284)*H + 7591.707861)
        if (Time_limit <= expos_ms):
            StartPos = int(1120 * usb + 6276)
            EndPos = int(((-0.0000118685*H + 0.0201366)*H + 74992.7219)*expos_ms +((0.0000136924*H - 0.022276)*H + 773.9233)*usb + (0.0003354606*H - 0.5725338)*H + 4421.8013)

    Cal_Start_Pos = StartPos
    Cal_End_Pos = EndPos

    SharpCap.SelectedCamera.Controls[19].Value = int(Cal_End_Pos)
    SharpCap.SelectedCamera.Controls[20].Value = int(Cal_Start_Pos)

    SC_End.append(int(Cal_End_Pos))
    SC_Start.append(int(Cal_Start_Pos))
    print
    print
    print "Exposure Time: ", Time_List[T], " ms"
    print("=================================")
    Cal_Start.append(int(LED_Start(Time_List[T])))
    Cal_End.append(int(LED_End(Time_List[T])))
    SharpCap.SelectedCamera.Controls[19].Value = Cal_End[-1]
    SharpCap.SelectedCamera.Controls[20].Value = Cal_Start[-1]

time_end = time.clock()
date = time.localtime()
file_date = time.strftime("_%Y%m%d_%H%M%S",date)

#file_name = directory + '\\LED_'
file_name = 'D:\\Scripts\\GPS_control\\LED_'

file_name = file_name + "_" + str(SharpCap.SelectedCamera.Controls.ColourSpace.Value)
file_name = file_name + "_" + str(SharpCap.SelectedCamera.Controls.Resolution.Value)
file_name = file_name + "_" + str(SharpCap.SelectedCamera.Controls.Binning.Value)
file_name = file_name + "_USB" + str(SharpCap.SelectedCamera.Controls.Usb.Value)
file_name = file_name + file_date + '_.txt'

print
print
with open(file_name, "w") as myfile:
    print("Camera Settings:")
    myfile.writelines("Camera Settings:" + "\n")
    print("========================================================")
    print("Colour Space : "), SharpCap.SelectedCamera.Controls.ColourSpace.Value
    myfile.writelines(("Colour Space :, %s \n") % (str(SharpCap.SelectedCamera.Controls.ColourSpace.Value)))
    print("Binning      : "), SharpCap.SelectedCamera.Controls.Binning.Value
    myfile.writelines(("Binning      :, %s \n") % (str(SharpCap.SelectedCamera.Controls.Binning.Value)))
    print("USB Traffic  : "), SharpCap.SelectedCamera.Controls.Usb.Value
    myfile.writelines(("USB Traffic  :, %s \n") % (str(SharpCap.SelectedCamera.Controls.Usb.Value)))
    print
    print "Calculation time: ", round((time_end - time_start)/60, 2), " minutes"
    myfile.writelines(("Calculation time:, %s \n") % (str(round((time_end - time_start)/60, 2))))
    print
    print(" [ms]  Cal_Start    Cal_End       Exp.   LED-time    Delta   SC_Start     SC_End ")
    myfile.writelines("[ms],Cal_Start,Cal_End,Exp.,LED-time,Delta,SC_Start,SC_End" + "\n")
    print("================================================================================")
    for T in range(0,len(Time_List)):
        LED_time = (Cal_End[T] - Cal_Start[T]) / 75000.0
        Diff_time = Time_List[T] - LED_time
        print("%5s %10s %10s %10.2f %10.3f %8.3f") % (Time_List[T],Cal_Start[T],Cal_End[T],Time_List[T],LED_time,Diff_time),
        print("%10s %10s") % (SC_Start[T],SC_End[T])
        myfile.writelines(("%5s,%10s,%10s,%10.2f,%10.3f,%8.3f,%10s,%10s") % (Time_List[T],Cal_Start[T],Cal_End[T],Time_List[T],LED_time,Diff_time,SC_Start[T],SC_End[T]) + "\n")

dumpdata = False

myfile.close()

Cal_Start_Pos = SharpCap.SelectedCamera.Controls[20].Value
SharpCap.SelectedCamera.Controls[20].Value = int(Cal_Start_Pos)

if (LED_status == "Off"):
    SharpCap.SelectedCamera.Controls[21].Value = "Off"


Regards,
Jean-Francois
Post Reply