Middle-Out Focusing

Got an idea for something that SharpCap should do? Share it here.
Forum rules
'+1' posts are welcome in this area of the forums to indicate your support for a particular feature suggestion. Suggestions that get the most +1's will be seriously considered for inclusion in future versions of SharpCap.
metastable
Posts: 48
Joined: Thu Oct 26, 2023 1:24 am

Middle-Out Focusing

#1

Post by metastable »

Hey Robin,

I was experimenting with a new script because I usually start each night fairly close to focus and I find it somewhat cumbersome to move out of focus so that I can rescan for focus using Multi-Star FWHM, but this would work for other methods as well, so I thought of a "retreating zig-zag" or "middle-out" method that starts close to focus and just progressively defocuses back and forth between negative and positive adjustments. In theory it works just fine but due to the way the curve detector sees positive and negative adjustments separately it doesn't work in practice. Think it might be worth the effort to add something like this? The script I made below might be clearer than my explanation above.

Code: Select all

from time import sleep

STEP_SIZE = 15
STEPS = 10
EXP_MS = 2000
DELAY_MS = 500

def move_to_position_and_wait(pos):
	SharpCap.Focusers.SelectedFocuser.Position = pos
	while SharpCap.Focusers.SelectedFocuser.Position != pos:
		sleep(DELAY_MS / 1000)

	sleep(DELAY_MS / 1000)

SharpCap.Transforms.SelectTransform("Multi-Star FWHM Measurement")

starting_position = SharpCap.Focusers.SelectedFocuser.Position
starting_exposure = SharpCap.SelectedCamera.Controls.Exposure.Value
print(starting_position)

offset = 0

current_position = SharpCap.Focusers.SelectedFocuser.Position
SharpCap.SelectedCamera.Controls.Exposure.Value = EXP_MS / 1000
SharpCap.SelectedCamera.TakeFramingShot()
sleep(1)

for i in range(STEPS):
	move_to_position_and_wait(SharpCap.Focusers.SelectedFocuser.Position + STEP_SIZE + offset)
	print("half positive step: {}".format(SharpCap.Focusers.SelectedFocuser.Position))
	SharpCap.SelectedCamera.TakeFramingShot()
	sleep(1)
	move_to_position_and_wait(SharpCap.Focusers.SelectedFocuser.Position - STEP_SIZE * 2 - offset)
	print("half negative step: {}".format(SharpCap.Focusers.SelectedFocuser.Position))
	SharpCap.SelectedCamera.TakeFramingShot()
	sleep(1)
	offset += STEP_SIZE * 2
	print("next offset: {}".format(offset))

move_to_position_and_wait(starting_position)
SharpCap.SelectedCamera.TakeFramingShot()
sleep(1)
SharpCap.SelectedCamera.Controls.Exposure.Value = starting_exposure
Thanks,
Alejandro
User avatar
admin
Site Admin
Posts: 13483
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Middle-Out Focusing

#2

Post by admin »

Hi Alejandro,

the reasoning behind the 'one direction only' for focus curve measurements is that it's impossible to be sure how much/little backlash is happening for focuser movements, even in cases where backlash is supposed to be compensated out. If there is any backlash then data points measured in the opposite direction have to be considered as noise, since they could be offset by an arbitrary amount in focuser position due to backlash.

I would worry that any 'back and forward' measurement technique for focus would be prone to the same sort of problem if any backlash is present.

cheers,

Robin
metastable
Posts: 48
Joined: Thu Oct 26, 2023 1:24 am

Re: Middle-Out Focusing

#3

Post by metastable »

Hey Robin,

I had considered backlash as a possible issue. I figured overshooting and then returning would be able to compensate for that issue. Ultimately the goal of the feature would be a one button "focus for me because I'm lazy". :lol: While the script does work, and gives you the points, the focus tool doesn't provide the curve and the best position to move to. I might be able to accomplish this full in script. I'll investigate a bit more and make a new post in the scripting section if I run into any questions.

Thanks,
Alejandro
Borodog
Posts: 351
Joined: Fri Jan 01, 2021 7:25 pm

Re: Middle-Out Focusing

#4

Post by Borodog »

Why is pre-moving to inside focus cumbersome? It's a single click if you have your course focus step set correctly, no?
metastable
Posts: 48
Joined: Thu Oct 26, 2023 1:24 am

Re: Middle-Out Focusing

#5

Post by metastable »

It's probably just me, but I find it difficult to determine how many steps and at what step size. I've noticed that if I go too far out in either direction, that the stars begin to show the secondary obstruction and the focus algorithm gets confused and starts to actually read better (lower, in the case of FWHM) values.
User avatar
admin
Site Admin
Posts: 13483
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Middle-Out Focusing

#6

Post by admin »

Hi,

why do you not use the 'Automatic Refocus' button? That's in the tools menu and also in the focus assistant window, and becomes enabled once you have used the assisted scan+goto focus tool successfully at least once (it works based on the settings that you used to scan last time).

The refocus button moves by 1/2 the length of the scan, then scans, then returns to best focus, all with one click.

cheers,

Robin
metastable
Posts: 48
Joined: Thu Oct 26, 2023 1:24 am

Re: Middle-Out Focusing

#7

Post by metastable »

Hey Robin,

I actually tried that yesterday and for some reason it kept failing. The last time I focused was the night before so perhaps things moved around too much to be able to reproduce the focus. I'll try again tonight if the weather is clear enough.

Thanks,
Alejandro
Borodog
Posts: 351
Joined: Fri Jan 01, 2021 7:25 pm

Re: Middle-Out Focusing

#8

Post by Borodog »

metastable wrote: Fri Dec 15, 2023 5:04 am It's probably just me, but I find it difficult to determine how many steps and at what step size. I've noticed that if I go too far out in either direction, that the stars begin to show the secondary obstruction and the focus algorithm gets confused and starts to actually read better (lower, in the case of FWHM) values.
This is a feature, not a problem. When this starts to happen, the wings on either side around critical focus will begin to bottom out. You want your scan to start in the region where the focus score is beginning to bottom out, or just inside of it. The reason is that you want the change to the focus score to be dominated by the change in focus, not noise in the sampling due to seeing. If you use too small of a focus scan, you are sampling a smaller, flatter section of the parabola around critical focus, and you become more sensitive to noise due to seeing.
User avatar
admin
Site Admin
Posts: 13483
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Middle-Out Focusing

#9

Post by admin »

Hi,

yes, that's a good point from @Borodog - SharpCap looks quite carefully at the statistics of the measured data points and will reject any 'focus curves' where the signal (between best focus and worst focus on either side of the center) is too small compared to the variability in the individual scores. The first astro capture application that had autofocus that I ever used was initially hit by the problem of interpreting noise in the focus measurements as a signal and performed terribly until the problem was fixed, so I learnt an important lesson about autofocus there :)

I should note that in most cases the curve is very close to symmetric, so there is no need to measure in detail near the minimum, as the position of the minimum can be calculated from the broad shape of the curve. 9 to 11 points well spaced so that there is clear defocus on both sides usually suffices.

cheers,

Robin
metastable
Posts: 48
Joined: Thu Oct 26, 2023 1:24 am

Re: Middle-Out Focusing

#10

Post by metastable »

Borodog wrote: Sat Dec 16, 2023 9:50 pm
metastable wrote: Fri Dec 15, 2023 5:04 am It's probably just me, but I find it difficult to determine how many steps and at what step size. I've noticed that if I go too far out in either direction, that the stars begin to show the secondary obstruction and the focus algorithm gets confused and starts to actually read better (lower, in the case of FWHM) values.
This is a feature, not a problem. When this starts to happen, the wings on either side around critical focus will begin to bottom out. You want your scan to start in the region where the focus score is beginning to bottom out, or just inside of it. The reason is that you want the change to the focus score to be dominated by the change in focus, not noise in the sampling due to seeing. If you use too small of a focus scan, you are sampling a smaller, flatter section of the parabola around critical focus, and you become more sensitive to noise due to seeing.
What I mean is I actually start to get more of an "M" shape with steep "improvements" in focus at the edges. I'll try to get an example of it once the weather clears up.
Post Reply