Kstars

focusalgorithms.h
1/*
2 SPDX-FileCopyrightText: 2019 Hy Murveit <hy-1@murveit.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QString>
10#include <QList>
11#include "focus.h"
12#include "curvefit.h"
13#include "focusutils.h"
14#include "../../auxiliary/robuststatistics.h"
15#include "../../auxiliary/gslhelpers.h"
16#include <gsl/gsl_sf_erf.h>
17
18class Edge;
19
20namespace Ekos
21{
22
23/**
24 * @class FocusAlgorithmInterface
25 * @short Interface intender for autofocus algorithms.
26 *
27 * @author Hy Murveit
28 * @version 1.1
29 */
31{
32 public:
33 struct FocusParams
34 {
35 // Curve Fitting object ptr. Create object if nullptr passed in
36 CurveFitting *curveFitting;
37 // Maximum movement from current position allowed for the algorithm.
38 int maxTravel;
39 // Initial sampling interval for the algorithm.
40 int initialStepSize;
41 // Absolute position of the focuser when the algorithm starts.
42 int startPosition;
43 // Minimum position the focuser is allowed to reach.
44 int minPositionAllowed;
45 // Maximum position the focuser is allowed to reach.
46 int maxPositionAllowed;
47 // Maximum number of iterations (captures) the focuser may try.
48 int maxIterations;
49 // The focus algorithm may terminate if it gets within this fraction of the best focus, e.g. 0.10.
50 double focusTolerance;
51 // The name of the filter used, if any.
52 QString filterName;
53 // The temperature measured when starting the focus (from focuser or observatory).
54 double temperature;
55 // The number of outward steps taken at the start of the algorithm.
56 double initialOutwardSteps;
57 // The number of steps
58 int numSteps;
59 // The focus algo
60 Focus::Algorithm focusAlgorithm;
61 // The user defined focuser backlash value
62 // The value does not need to be exact but needs to be >= focuser backlash
63 int backlash;
64 // Curve fit is the type of curve to fit to the data
65 CurveFitting::CurveFit curveFit;
66 // Whether we want to use weightings of datapoints in the curve fitting process
67 bool useWeights;
68 // Which star measure to use, e.g. HFR, FWHM, etc.
69 Focus::StarMeasure starMeasure;
70 // If using FWHM, which PSF to use.
71 Focus::StarPSF starPSF;
72 // After pass1 re-evaluate the curve fit to remove outliers
73 bool refineCurveFit;
74 // The type of focus walk
75 Focus::FocusWalk focusWalk;
76 // Whether to use donut busting functionality
77 bool donutBuster;
78 // The degree of outlier rejection aggressiveness to apply
79 double outlierRejection;
80 // Whether we want to minimise or maximise the focus measurement statistic
81 CurveFitting::OptimisationDirection optimisationDirection;
82 // How to assign weights to focus measurements
83 Mathematics::RobustStatistics::ScaleCalculation scaleCalculation;
84
85 FocusParams(CurveFitting *_curveFitting, int _maxTravel, int _initialStepSize, int _startPosition,
86 int _minPositionAllowed, int _maxPositionAllowed, int _maxIterations,
87 double _focusTolerance, const QString &filterName_, double _temperature,
88 double _initialOutwardSteps, int _numSteps, Focus::Algorithm _focusAlgorithm, int _backlash,
89 CurveFitting::CurveFit _curveFit, bool _useWeights, Focus::StarMeasure _starMeasure,
90 Focus::StarPSF _starPSF, bool _refineCurveFit, Focus::FocusWalk _focusWalk, bool _donutBuster,
91 double _outlierRejection, CurveFitting::OptimisationDirection _optimisationDirection,
92 Mathematics::RobustStatistics::ScaleCalculation _scaleCalculation) :
93 curveFitting(_curveFitting), maxTravel(_maxTravel), initialStepSize(_initialStepSize),
94 startPosition(_startPosition), minPositionAllowed(_minPositionAllowed),
95 maxPositionAllowed(_maxPositionAllowed), maxIterations(_maxIterations),
96 focusTolerance(_focusTolerance), filterName(filterName_),
97 temperature(_temperature), initialOutwardSteps(_initialOutwardSteps), numSteps(_numSteps),
98 focusAlgorithm(_focusAlgorithm), backlash(_backlash), curveFit(_curveFit),
99 useWeights(_useWeights), starMeasure(_starMeasure), starPSF(_starPSF),
100 refineCurveFit(_refineCurveFit), focusWalk(_focusWalk), donutBuster(_donutBuster),
101 outlierRejection(_outlierRejection), optimisationDirection(_optimisationDirection),
102 scaleCalculation(_scaleCalculation) {}
103 };
104
105 // Constructor initializes an autofocus algorithm from the input params.
106 FocusAlgorithmInterface(const FocusParams &_params) : params(_params)
107 {
108 // Either a curve fitting object ptr is passed in or the constructor will create its own
109 if (params.curveFitting == nullptr)
110 params.curveFitting = new CurveFitting();
111 }
112 virtual ~FocusAlgorithmInterface() {}
113
114 // After construction, this should be called to get the initial position desired by the
115 // focus algorithm. It returns the start position passed to the constructor if
116 // it has no movement request.
117 virtual int initialPosition() = 0;
118
119 // Pass in the recent measurement. Returns the position for the next measurement,
120 // or -1 if the algorithms done or if there's an error.
121 // If stars is not nullptr, then the they may be used to modify the HFR value.
122 virtual int newMeasurement(int position, double value, const double starWeight, const QList<Edge*> *stars = nullptr) = 0;
123
124 // Returns true if the algorithm has terminated either successfully or in error.
125 bool isDone() const
126 {
127 return done;
128 }
129
130 // Returns the best position. Should be called after isDone() returns true.
131 // Returns -1 if there's an error.
132 int solution() const
133 {
134 return focusSolution;
135 }
136
137 // Returns the value for best solution. Should be called after isDone() returns true.
138 // Returns -1 if there's an error.
139 double solutionValue() const
140 {
141 return focusValue;
142 }
143
144 // Returns the weight for best solution. Should be called after isDone() returns true.
145 // Returns -1 if there's an error.
146 double solutionWeight() const
147 {
148 return focusWeight;
149 }
150
151 // Returns human-readable extra error information about why the algorithm is done.
152 QString doneReason() const
153 {
154 return doneString;
155 }
156
157 // Returns failure code. Call after algorithm fails to get the reason code. Passed to Analyze
158 AutofocusFailReason getFailCode() const
159 {
160 return failCode;
161 }
162
163 // Returns the params used to construct this object.
164 const FocusParams &getParams() const
165 {
166 return params;
167 }
168
169 virtual double latestValue() const = 0;
170
171 virtual void getMeasurements(QVector<int> *positions, QVector<double> *values, QVector<double> *scale) const = 0;
172 virtual void getPass1Measurements(QVector<int> *positions, QVector<double> *values, QVector<double> *scale,
173 QVector<bool> *out) const = 0;
174
175 virtual QString getTextStatus(double R2 = 0) const = 0;
176
177 // For Linear and L1P returns whether focuser is inFirstPass, other algos return true
178 virtual bool isInFirstPass() const = 0;
179
180 // For Linear and L1P returns the focuser step
181 virtual int currentStep() const = 0;
182
183 // For testing.
184 virtual FocusAlgorithmInterface *Copy() = 0;
185
186 protected:
187 FocusParams params;
188 bool done = false;
189 int focusSolution = -1;
190 double focusValue = -1;
191 double focusWeight = -1;
192 QString doneString;
193 AutofocusFailReason failCode = Ekos::FOCUS_FAIL_NONE;
194};
195
196// Creates a LinearFocuser. Caller responsible for the memory.
197FocusAlgorithmInterface *MakeLinearFocuser(const FocusAlgorithmInterface::FocusParams &params);
198}
199
Interface intender for autofocus algorithms.
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:83
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:09:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.