Implementing temperature compensated focusing

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
Borodog
Posts: 487
Joined: Fri Jan 01, 2021 7:25 pm

Implementing temperature compensated focusing

#1

Post by Borodog »

Hello all. I've writen a script to accomplish temperature compensated focusing with the ZWO EAF, which is not implemented in the ASCOM driver.

Any feedback is welcome. This is my first IronPython script in SharpCap. It seems to work in testing, but if there is something I haven't thought of or a way to do this better, please let me know.

You would need to measure the temperature coefficient for your rig. Technically I need to remeasure mine because I have changed the optical train. I will probably write a similar script to do that.

Code: Select all

# IronPython Pad. Write code snippets here and F5 to run. If code is selected, only selection is run.

import time

# Get the ASCOM focuser object (assumes focuser is selected and connected)
#focuser = SharpCap.Focusers.SelectedFocuser.AscomFocuser

# Initial reference values
initial_temp = SharpCap.Focusers.SelectedFocuser.Temperature
initial_pos = SharpCap.Focusers.SelectedFocuser.Position
coefficient = -50.5  # steps per degree C

print("Temperature compensation started.")
print("Initial temperature: {0:.2f} C".format(initial_temp))
print("Initial position: {0}".format(initial_pos))
print("Coefficient: {0} steps/deg C".format(coefficient))

while True:
    current_temp = SharpCap.Focusers.SelectedFocuser.Temperature
    #print("Current temp = {0:.2f}".format(current_temp)) #uncomment to print current temperature
    delta_temp = current_temp - initial_temp
    adjustment = int(round(delta_temp * coefficient))
    new_pos = initial_pos + adjustment

    if abs(new_pos - SharpCap.Focusers.SelectedFocuser.Position) >= 5:
        print("Temperature change detected.")
        print("Moving to {0}".format(new_pos))
        SharpCap.Focusers.SelectedFocuser.Move(new_pos)
        
    time.sleep(60)  # Check every 60 seconds; adjust as needed
Borodog
Posts: 487
Joined: Fri Jan 01, 2021 7:25 pm

Re: Implementing temperature compensated focusing

#2

Post by Borodog »

I believe this script will automatically take data to measure the temperature coefficient, although I cannot test it at the moment. It assumed that you have successfully run an automatic focus assistant and not changed anything. You should have the option to confirm the focus selection box turned off. Because SharpCap.RunRefocus runs asynchronously, you will need to wait for it to complete; hence you need to measure how long a focus scan takes, probably double that for margin, and put that number of seconds into the time.sleep() command. The script will then continuously refocus on the target as the temperature drops, producing a comma separated list that you can copy and paste into a CSV file. From there it's up to you to fit the data with a line and determine the temperature coefficient for your system (i.e. the slope of the line).

If clouds come along you can just stop the script and start it up again after they pass.

Code: Select all

import time

print("temperature, position")

while True:
    current_temp = SharpCap.Focusers.SelectedFocuser.Temperature
    SharpCap.RunRefocus()
    time.sleep(60)  # Set to 2X actual expected refocus duration for safety margin
    final_position = SharpCap.Focusers.SelectedFocuser.Position
    print("{0:.2f},{1}".format(current_temp,final_position))
Borodog
Posts: 487
Joined: Fri Jan 01, 2021 7:25 pm

Re: Implementing temperature compensated focusing

#3

Post by Borodog »

Couple of typos in the last line of the last script. It has an extra tab up front (should be 4 spaces), and the 2 should be a 1:

print("{0:.2f},{1}".format(current_temp,final_position))
User avatar
admin
Site Admin
Posts: 17595
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Implementing temperature compensated focusing

#4

Post by admin »

Hi Mike,

thanks for sharing - I made the edit to correct your second script (unfortunately I had to limit the time that you can edit your post for to comply with our ridiculous online safety laws).

It's also possible to do the correlation inside SharpCap (correlation gets used in a fair number of places, so there is code available to use). As an example:

Code: Select all

>>> from SharpCap.Base.Conics import QuadraticBestFit, Vector2
>>> from System.Collections.Generic import List
>>> list = List[Vector2]()
>>> list.Add(Vector2(1,1))
>>> list.Add(Vector2(2,2))
>>> list.Add(Vector2(3,3.2))
>>> list.Add(Vector2(4,5))
>>> QuadraticBestFit.CalculateLinearBestFit(list)
<SharpCap.Base.Conics.Quadratic object at 0x000000000000002B [0.00000 x^2 + 1.32000 x + -0.50000, r2=0.981, sse=0.168]>
Here we create a list of Vector2 objects, add some data points to the list then pass that list to the LinearBestFit function.

cheers,

Robin
Borodog
Posts: 487
Joined: Fri Jan 01, 2021 7:25 pm

Re: Implementing temperature compensated focusing

#5

Post by Borodog »

Thanks, Robin. That is good to know from a python in SharpCap perspective, but personally I think it's dangerous to blindly fit data without reviewing it. Last night I set the scope on Saturn and just let it run the TCF measurement script for a long time (which eventually seemed to hang SharpCap for reasons unknown, unless it was a coincidence), but in the end the data was unusable because it had been spoiled by thin cloud; will the focuses were adequate (inside the CFZ), the scatter was large and the uncertainty in the slope very large because of it. If I had not plotted the data afterward and seen it, I would not have caught it.

Mike
Post Reply