Jump to content

Banner.jpg.b83b14cd4142fe10848741bb2a14c66b.jpg

Auto focus algorithm


SteveBz

Recommended Posts

Hi People,


My Arduino-based focuser is now humming quietly to itself, but essentially it's still manual, ie Bahtinov Mask, snap, adjust, snap, adjust etc.

Does anyone know of an algorithm to produce a self-refining HFD focus as in the v-model. I'm using python, but it could be in any language.

Regards,

Steve.

Link to comment
Share on other sites

Not really familiar with focusing algorithms, but this is what I would do if I was to implement one:

1. You need star detection and gaussian fitting for stars (might not need gaussian fitting, you can probably use peak value, but not sure if that is going to be precise enough)

2. Use some sort of "slope descent" algorithm

You identify stars in sub, fit gaussian and take average value of sigma for each star (take care of saturated stars and such), choose random direction of focuser travel and do another measurement. Compare two average values and move in direction in which sigma is going from higher to lower value. Finish at some predefined condition (like sigma difference being less than ...).

You can make a few modifications to above idea to make it work better - step size should be proportional to sigma difference divided with step size (High slope - move more, shallow slope - move in small steps). Try to avoid overshoot - you can get wrong movement if two sigmas are sampled at different side of local minimum. You can combine with binary search - sample at point 1, sample at point 2, sample at midway between points 1 and 2. Depending on slopes, choose next pair of values to sample between.

I think there should be robust algorithm for slope descent - let me check it - it can be used in combination with gaussian fitting.

Take a look at this (there is some python code):

https://en.wikipedia.org/wiki/Gradient_descent

 

Link to comment
Share on other sites

Nice, I'll look at it.  There is an opencv function called stardetector that strangely is not designed for stars, but coincidentaly works with them. My real problem is deciding which direction to go first.

I'm using a pwm motor and I can change the pulse width with each run.  There will be some slip in the focuser which I need to account for when I change direction.

There are just not enough clear nights to test!

Link to comment
Share on other sites

Do you have any sort of position measurement? Do you know if you are full in or full out focus, or is it just two directions and no start/end position - you can go either way indefinitely but focuser will just slip or something?

I thought a bit more about the algorithm, do you have like a minimum step?

If you have minimum step (because it is stepper, so single micro step or something like that) would be a good thing to exploit for finding slope - take a measurement, randomly decide to go either way single step - do another measurement and figure out slope - then move in direction of slope until slope changes. Since you don't have infinite precision and are dealing with sort of integer precision steps, I guess that is the easiest way.

Oh, I just saw in your post, so it is not stepper it is pwm controlled motor, maybe define what is the smallest step with enough precision that you can move and use that as single step.

Link to comment
Share on other sites

Like this. The Arduino is on a tripod leg and does lots of stuff.  There is an ota hub that controls focus, dew heaters, ota fans and DSLR.

The motor is a standard DC motor. It doesn't slip because of the cogs.

IMG_20170819_100846.jpg

IMG_20170804_082833.jpg

IMG_20170819_100816.jpg

Link to comment
Share on other sites

44 minutes ago, vlaiv said:

Do you have any sort of position measurement? Do you know if you are full in or full out focus, or is it just two directions and no start/end position - you can go either way indefinitely but focuser will just slip or something?

I thought a bit more about the algorithm, do you have like a minimum step?

If you have minimum step (because it is stepper, so single micro step or something like that) would be a good thing to exploit for finding slope - take a measurement, randomly decide to go either way single step - do another measurement and figure out slope - then move in direction of slope until slope changes. Since you don't have infinite precision and are dealing with sort of integer precision steps, I guess that is the easiest way.

Oh, I just saw in your post, so it is not stepper it is pwm controlled motor, maybe define what is the smallest step with enough precision that you can move and use that as single step.

Hi Vlaiv,

If I assume it was in focus at end of last session, so focal space out and in.

If I always start by taking a photo then set pwm at 50 % and move out for 1 secs. Then take another photo and compare. Based on this, choose direction, change pulse to 10 % at .2 secs and step until calculation changes direction.  Maybe move on 2 more to account for slop, change to 5 %, reverse and try again. When no difference, stop.

Link to comment
Share on other sites

I think better approach would be like this (assuming it is left in last position).

First move it in out direction for one second, or what ever time you think it will definitively defocus it. If it is left in position from last session, you don't really know where you are in relation to perfect focus. You might need to move it in a bit, or maybe out a bit, so you are not sure (depends on temperature and whatever else).

So best to start in position you know is out of focus, then take an image and do star calc, then move it opposite direction for certain step size. Now, step size although there are infinite positions because system is not "digital" but "analogue", should be of known certain size. This certain size should depend on couple of factors: how precisely can you do timing, and probably more important - how much movement will overcome measurement error. If you make too small step due to measurement error of star gaussian sigma it can look as if you are "traveling" the wrong way - you have not moved enough to overcome this randomness (both in measurement and in current seeing, although that will be averaged out if you take large number of stars, still there is always some residual error, however small it may be).

Can you tell how much focuser is moving for given pwm setting and time interval? It would be good idea to take "step" size to be in relation to perfect focus zone of scope (that depends on F/ratio of telescope, I'm sure there is a formula online to calculate focus zone in micrometers or something). So probably step should be a half of focus zone, or a quarter.

So to go back at algorithm:

1. Do large out focus and "record" direction (it will be out at this point)

2. take image

3. do "single step" in current direction.

4. take another image

5. If no significant change in sigma - slope close to 0 - done, else - calculate slope and based on slope magnitude and direction apply movement. Remember direction. Go to step 2.

I think above algorithm should work well :D

 

Link to comment
Share on other sites

One more thing, I forgot to add - so depending on slope magnitude - which is calculated something like - (sigma1 - sigma2) / step size, you can take a coefficient (again something you can fiddle around and get good value) that will

determine next movement size so it will be coeff * calculated slope. Mind you if you always want direction + magnitude use ABS value, and sign will be direction.

Link to comment
Share on other sites

1 hour ago, hughgilhespie said:

Hi

http://www.ccdware.com/Files/ITS Paper.pdf

Might be helpful.

Regards, Hugh

Hi Hugh, it's excellent. Obviously I need to make some adjustments to account for the fact that I don't have the same motor. I might see if I could swap the DC motor for a proper stepper motor.

I'm also using a DSLR so I can't take a proportion of the image, but certainly it will improve my current algorithm.

I'm traveling until Tuesday so I'll have a look when I get home.

Tx,

Steve.

Link to comment
Share on other sites

1 hour ago, hughgilhespie said:

Hi

http://www.ccdware.com/Files/ITS Paper.pdf

Might be helpful.

Regards, Hugh

Really interesting paper!

It does point out two major things - metric to use to calculate defocus - gaussian/fwhm and peak seem not to be so good with large defocus.

Second thing is also interesting - I always "imagined" that defocus curve is closer to parabola then to V shape presented in the paper. With parabola kind of curve you need multiple steps and measurements to home in on local minimum, but with V shaped curve, if lines are indeed sort of straight lines - just a couple of measurements can be enough to approximate slope of straight line and move to correct focus position.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.