Kstars

focusadvisor.h
1/*
2 SPDX-FileCopyrightText: 2024 John Evans <john.e.evans.email@googlemail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "focus.h"
10#include "ui_focusadvisor.h"
11#include "ui_focusadvisorhelp.h"
12
13namespace Ekos
14{
15
16// FocusAdvisor is setup as a friend class of Focus because it is tightly coupled to Focus relying on many
17// members of Focus, both variables and functions.
18//
19// FocusAdvisor is designed to assist the user with focus. Its main purpose is to setup Focus for new users
20// who may not be familiar with the many options that the Focus module offers. It will attempt to find reasonable focus
21// rather than the "best" setup which will require some trial and error by the user.
22// The main features are:
23// - Parameter setup - FocusAdvisor recommends a setup using well tried combinations including Linear 1 Pass
24// Hperbolic curve fitting and HFR measure. A help dialog displays the current and proposed parameter
25// settings.
26// - Find stars - Searches the range of travel of the focuser to find some stars. If the user knows roughtly where they
27// are then set the focuser to this position and Find Stars will search around this area first.
28// - Coarse adjustment - This tool will attempt to adjust parameters (step size and backlash) in order get a reasonable V-curve
29// without curve fitting. This is a preparatory step before running Autofocus to make sure parameters are
30// good enough to allow Autofocus to complete.
31// - Fine adjustment - This tool runs Autofocus and adjusts step size and backlash by analysing the results of Autofocus. If
32// Autofocus is not within its tolerance paraneters it will keep adjusting and retrying.
33//
34
35class FocusAdvisor : public QDialog, public Ui::focusAdvisorDialog
36{
38
39 public:
40
41 FocusAdvisor(QWidget *parent = nullptr);
42 ~FocusAdvisor();
43
44 typedef enum
45 {
46 Idle,
47 UpdatingParameters,
48 FindingStars,
49 CoarseAdjustments,
50 FineAdjustments
51 } Stage;
52
53 /**
54 * @brief Initialise the Focus Advisor Control
55 */
56 void initControl();
57
58 /**
59 * @brief Focus Advisor Control function
60 */
61 void control();
62
63 /**
64 * @brief Focus Advisor analysis of Autofocus run
65 */
66 bool analyseAF();
67
68 /**
69 * @brief Update parameters based on Focus Advisor recommendations
70 */
71 void updateParams();
72
73 /**
74 * @brief Setup the Focus Advisor recommendations
75 * @param Optical Train name
76 */
77 void setupParams(const QString &OTName);
78
79 /**
80 * @brief Return whether Focus Advisor is running
81 * @return inFocusAdvisor
82 */
83 bool inFocusAdvisor()
84 {
85 return m_inFocusAdvisor;
86 }
87
88 /**
89 * @brief Set the value of m_inFocusAdvisor
90 * @param value
91 */
92 void setInFocusAdvisor(bool value);
93
94 /**
95 * @brief Return prefix to use on saved off focus frame
96 * @return prefix
97 */
98 QString getFocusFramePrefix();
99
100 /**
101 * @brief set the dialog buttons
102 * @param Focus Advisor running (or not)
103 */
104 void setButtons(const bool running);
105
106 /**
107 * @brief Reset Focus Advisor
108 */
109 Q_INVOKABLE void reset();
110
111 /**
112 * @brief Launch the focus advisor algorithm
113 */
114 Q_INVOKABLE bool start();
115
116 /**
117 * @brief Stop the focus advisor algorithm
118 */
119 Q_INVOKABLE void stop();
120
121
122 private:
123
124 /**
125 * @brief setup the dialog UI
126 */
127 void processUI();
128
129 /**
130 * @brief setup the help dialog table
131 */
132 void setupHelpTable();
133
134 /**
135 * @brief Focus Advisor help window
136 */
137 void help();
138
139 /**
140 * @brief Returns whether Focus Advisor can run with the currently set parameters
141 */
142 bool canFocusAdvisorRun();
143
144 /**
145 * @brief addSectionToHelpTable
146 * @param last used row in table
147 * @param section name
148 */
149 void addSectionToHelpTable(int &row, const QString &section);
150
151 /**
152 * @brief addParamToHelpTable
153 * @param last used row in table
154 * @param parameter name
155 * @param currentValue of parameter
156 * @param newValue of parameter
157 */
158 void addParamToHelpTable(int &row, const QString &parameter, const QString &currentValue, const QString &newValue);
159
160 /**
161 * @brief resizeHelpDialog based on contents
162 */
163 void resizeHelpDialog();
164
165 /**
166 * @brief return text for flag for display in help dialog
167 * @param flag
168 */
169 QString boolText(const bool flag);
170
171 /**
172 * @brief return whether stars exist in the latest focus frame
173 * @return stars found
174 */
175 bool starsFound();
176
177 /**
178 * @brief Process the passed in focus parameter
179 * @param defaultValue of the parameter
180 * @param Parameter map
181 * @param widget associated with the parameter
182 * @param text to display in help dialog for this parameter
183 */
184 template <typename T>
185 void processParam(T defaultValue, int &row, QVariantMap &map, const QWidget *widget, const QString text)
186 {
187 const QSpinBox *sb = dynamic_cast<const QSpinBox*>(widget);
188 const QDoubleSpinBox *dsb = dynamic_cast<const QDoubleSpinBox*>(widget);
189 const QCheckBox *chb = dynamic_cast<const QCheckBox*>(widget);
190 const QComboBox *cb = dynamic_cast<const QComboBox*>(widget);
191 const QRadioButton *rb = dynamic_cast<const QRadioButton*>(widget);
192 const QGroupBox *gb = dynamic_cast<const QGroupBox*>(widget);
193 int dp = 0;
194 QString currentText, suffix;
195
196 if (sb)
197 {
198 suffix = sb->suffix();
199 currentText = QString("%1%2").arg(sb->value()).arg(suffix);
200 }
201 else if (dsb)
202 {
203 dp = dsb->decimals();
204 suffix = dsb->suffix();
205 currentText = QString("%1%2").arg(dsb->value(), 0, 'f', dp).arg(suffix);
206 }
207 else if (chb)
208 currentText = QString("%1").arg(boolText(chb->isChecked()));
209 else if (cb)
210 currentText = cb->currentText();
211 else if (rb)
212 currentText = QString("%1").arg(boolText(rb->isChecked()));
213 else if (gb)
214 currentText = QString("%1").arg(boolText(gb->isChecked()));
215 else
216 {
217 qCDebug(KSTARS_EKOS_FOCUS) << "Bad parameter widget" << widget->objectName() << "passed to" << __FUNCTION__;
218 return;
219 }
220
221 QString newText;
222
223 map.insert(widget->objectName(), defaultValue);
224
225 if constexpr(std::is_same_v<T, int>)
226 newText = QString("%1%2").arg(defaultValue).arg(suffix);
227 else if constexpr(std::is_same_v<T, double>)
228 newText = QString("%1%2").arg(defaultValue, 0, 'f', dp).arg(suffix);
229 else if constexpr(std::is_same_v<T, bool>)
230 newText = QString("%1").arg(boolText(defaultValue));
231 else if constexpr(std::is_same_v<T, QString>)
232 newText = defaultValue;
233 else
234 {
235 qCDebug(KSTARS_EKOS_FOCUS) << "Bad template parameter" << defaultValue << "passed to" << __FUNCTION__;
236 return;
237 }
238 addParamToHelpTable(row, text, currentText, newText);
239 }
240
241 /**
242 * @brief Look at similar Optical Trains to get parameters
243 * @param Optical Train name
244 * @return Parameter map
245 */
246 QVariantMap getOTDefaults(const QString &OTName);
247
248 /**
249 * @brief returns whether the optical train telescope has a central obstruction
250 * @param scopeType is the type of telescope
251 * @return whether scope has an obstruction
252 */
253 bool scopeHasObstruction(const QString &scopeType);
254
255 /**
256 * @brief Initialise the find stars algorithm
257 * @param startPos is the starting position to use
258 */
259 void initFindStars(const int startPos);
260
261 /**
262 * @brief Find stars algorithm to scan Focuser movement range to find stars
263 */
264 void findStars();
265
266 /**
267 * @brief Initialise the pre-Autofocus adjustment algorithm
268 * @param startPos is the starting position to use
269 */
270 void initPreAFAdj(const int startPos);
271
272 /**
273 * @brief Algorithm for coarse parameter adjustment prior to running Autofocus
274 */
275 void preAFAdj();
276
277 /**
278 * @brief Check the Max / Min star measure ratio against the limit
279 * @param limit to check against
280 * @param maxMinRatio
281 * @return Check passed or not
282 */
283 bool maxMinRatioOK(const double limit, const double maxMinRatio);
284
285 /**
286 * @brief Return min position where stars exist
287 */
288 int minStarsLimit();
289
290 /**
291 * @brief Return max position where stars exist
292 */
293 int maxStarsLimit();
294
295 /**
296 * @brief Initialise the Autofocus adjustment algorithm
297 * @param startPos is the starting position to use
298 * @param retryOverscan calculation
299 */
300 void initAFAdj(const int startPos, const bool retryOverscan);
301
302 /**
303 * @brief Start Autofocus
304 * @param startPos is the starting position to use
305 */
306 void startAF(const int startPos);
307
308 /**
309 * @brief Abort Focus Advisor
310 * @param failCode
311 * @param message msg
312 */
313 void abort(const QString &msg);
314
315 /**
316 * @brief Abort Focus Advisor
317 * @param whether Autofocus was run
318 * @param message msg
319 */
320 void complete(const bool autofocus, const QString &msg);
321
322 /**
323 * @brief Reset settings temporarily adjusted by Focus Advisor
324 * @param success of Focus Advisor
325 */
326 void resetSavedSettings(const bool success);
327
328 Focus *m_focus { nullptr };
329 QVariantMap m_map;
330
331 // Help popup
332 std::unique_ptr<Ui::focusAdvisorHelpDialog> m_helpUI;
333 QPointer<QDialog> m_helpDialog;
334
335 // Buttons
336 QPushButton *m_runButton;
337 QPushButton *m_stopButton;
338 QPushButton *m_helpButton;
339
340 // Control bit for Focus Advisor
341 bool m_inFocusAdvisor = false;
342
343 // Initial parameter settings restore in case Focus Advisor fails
344 int m_initialStepSize = -1;
345 int m_initialBacklash = -1;
346 int m_initialAFOverscan = -1;
347 bool m_initialUseWeights = false;
348 bool m_initialScanStartPos = false;
349
350 // Vectors to use to analyse position/measure results
351 QVector<int> m_position;
352 QVector<double> m_measure;
353
354 // Control bits for Focus Advisor algorithm stages
355 bool m_inFindStars = false;
356 bool m_inPreAFAdj = false;
357 bool m_inAFAdj = false;
358
359 // Find Stars Algorithm
360 bool m_findStarsIn = false;
361 bool m_findStarsMaxBound = false;
362 bool m_findStarsMinBound = false;
363 bool m_findStarsRange = false;
364 bool m_findStarsRangeIn = false;
365 bool m_findStarsFindInEdge = false;
366 int m_findStarsSector = 0;
367 int m_findStarsJumpsInSector = 0;
368 int m_findStarsRunNum = 0;
369 int m_findStarsInRange = -1;
370 int m_findStarsOutRange = -1;
371
372 // Pre-Autofocus Algorithm
373 int m_preAFInner = 0;
374 bool m_preAFNoStarsOut = false;
375 bool m_preAFNoStarsIn = false;
376 int m_preAFRunNum = 0;
377 bool m_preAFMaxRange = false;
378
379 // Find Stars & Pre-Autofocus Algorithms
380 int m_jumpsToGo = 0;
381 int m_jumpSize = 0;
382
383 // Autofocus algorithm analysis
384 bool m_minOverscan = false;
385 bool m_runAgainR2 = false;
386 bool m_nearFocus = false;
387
388 // Help table Cols
389 typedef enum
390 {
391 HELP_PARAMETER = 0,
392 HELP_CURRENT_VALUE,
393 HELP_NEW_VALUE,
394 HELP_MAX_COLS
395 } HelpColID;
396
397 signals:
398 void newMessage(QString);
399 void newStage(Stage);
400};
401
402}
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:79
bool isChecked() const const
bool isChecked() const const
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
QObject * parent() const const
QString arg(Args &&... args) const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:59:51 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.