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 results table
131 */
132 void setupResultsTable();
133
134 /**
135 * @brief setup the help dialog table
136 */
137 void setupHelpTable();
138
139 /**
140 * @brief Focus Advisor help window
141 */
142 void help();
143
144 /**
145 * @brief Returns whether Focus Advisor can run with the currently set parameters
146 */
147 bool canFocusAdvisorRun();
148
149 /**
150 * @brief addSectionToHelpTable
151 * @param last used row in table
152 * @param section name
153 */
154 void addSectionToHelpTable(int &row, const QString &section);
155
156 /**
157 * @brief addParamToHelpTable
158 * @param last used row in table
159 * @param parameter name
160 * @param currentValue of parameter
161 * @param newValue of parameter
162 */
163 void addParamToHelpTable(int &row, const QString &parameter, const QString &currentValue, const QString &newValue);
164
165 /**
166 * @brief resizeHelpDialog based on contents
167 */
168 void resizeHelpDialog();
169
170 /**
171 * @brief return text for flag for display in help dialog
172 * @param flag
173 */
174 QString boolText(const bool flag);
175
176 /**
177 * @brief return whether stars exist in the latest focus frame
178 * @return stars found
179 */
180 bool starsFound();
181
182 /**
183 * @brief Process the passed in focus parameter
184 * @param defaultValue of the parameter
185 * @param Parameter map
186 * @param widget associated with the parameter
187 * @param text to display in help dialog for this parameter
188 */
189 template <typename T>
190 void processParam(T defaultValue, int &row, QVariantMap &map, const QWidget *widget, const QString text)
191 {
192 const QSpinBox *sb = dynamic_cast<const QSpinBox*>(widget);
193 const QDoubleSpinBox *dsb = dynamic_cast<const QDoubleSpinBox*>(widget);
194 const QCheckBox *chb = dynamic_cast<const QCheckBox*>(widget);
195 const QComboBox *cb = dynamic_cast<const QComboBox*>(widget);
196 const QRadioButton *rb = dynamic_cast<const QRadioButton*>(widget);
197 const QGroupBox *gb = dynamic_cast<const QGroupBox*>(widget);
198 int dp = 0;
199 QString currentText, suffix;
200
201 if (sb)
202 {
203 suffix = sb->suffix();
204 currentText = QString("%1%2").arg(sb->value()).arg(suffix);
205 }
206 else if (dsb)
207 {
208 dp = dsb->decimals();
209 suffix = dsb->suffix();
210 currentText = QString("%1%2").arg(dsb->value(), 0, 'f', dp).arg(suffix);
211 }
212 else if (chb)
213 currentText = QString("%1").arg(boolText(chb->isChecked()));
214 else if (cb)
215 currentText = cb->currentText();
216 else if (rb)
217 currentText = QString("%1").arg(boolText(rb->isChecked()));
218 else if (gb)
219 currentText = QString("%1").arg(boolText(gb->isChecked()));
220 else
221 {
222 qCDebug(KSTARS_EKOS_FOCUS) << "Bad parameter widget" << widget->objectName() << "passed to" << __FUNCTION__;
223 return;
224 }
225
226 QString newText;
227
228 map.insert(widget->objectName(), defaultValue);
229
230 if constexpr(std::is_same_v<T, int>)
231 newText = QString("%1%2").arg(defaultValue).arg(suffix);
232 else if constexpr(std::is_same_v<T, double>)
233 newText = QString("%1%2").arg(defaultValue, 0, 'f', dp).arg(suffix);
234 else if constexpr(std::is_same_v<T, bool>)
235 newText = QString("%1").arg(boolText(defaultValue));
236 else if constexpr(std::is_same_v<T, QString>)
237 newText = defaultValue;
238 else
239 {
240 qCDebug(KSTARS_EKOS_FOCUS) << "Bad template parameter" << defaultValue << "passed to" << __FUNCTION__;
241 return;
242 }
243 addParamToHelpTable(row, text, currentText, newText);
244 }
245
246 /**
247 * @brief Look at similar Optical Trains to get parameters
248 * @param Optical Train name
249 * @return Parameter map
250 */
251 QVariantMap getOTDefaults(const QString &OTName);
252
253 /**
254 * @brief returns whether the optical train telescope has a central obstruction
255 * @param scopeType is the type of telescope
256 * @return whether scope has an obstruction
257 */
258 bool scopeHasObstruction(const QString &scopeType);
259
260 /**
261 * @brief Initialise the find stars algorithm
262 * @param startPos is the starting position to use
263 */
264 void initFindStars(const int startPos);
265
266 /**
267 * @brief Find stars algorithm to scan Focuser movement range to find stars
268 */
269 void findStars();
270
271 /**
272 * @brief Initialise the pre-Autofocus adjustment algorithm
273 * @param startPos is the starting position to use
274 */
275 void initPreAFAdj(const int startPos);
276
277 /**
278 * @brief Algorithm for coarse parameter adjustment prior to running Autofocus
279 */
280 void preAFAdj();
281
282 /**
283 * @brief Check the Max / Min star measure ratio against the limit
284 * @param limit to check against
285 * @param maxMinRatio
286 * @return Check passed or not
287 */
288 bool maxMinRatioOK(const double limit, const double maxMinRatio);
289
290 /**
291 * @brief Return min position where stars exist
292 */
293 int minStarsLimit();
294
295 /**
296 * @brief Return max position where stars exist
297 */
298 int maxStarsLimit();
299
300 /**
301 * @brief Initialise the Autofocus adjustment algorithm
302 * @param startPos is the starting position to use
303 * @param retryOverscan calculation
304 */
305 void initAFAdj(const int startPos, const bool retryOverscan);
306
307 /**
308 * @brief Start Autofocus
309 * @param startPos is the starting position to use
310 */
311 void startAF(const int startPos);
312
313 /**
314 * @brief Abort Focus Advisor
315 * @param failCode
316 * @param message msg
317 */
318 void abort(const QString &msg);
319
320 /**
321 * @brief Abort Focus Advisor
322 * @param whether Autofocus was run
323 * @param message msg
324 */
325 void complete(const bool autofocus, const QString &msg);
326
327 /**
328 * @brief Reset settings temporarily adjusted by Focus Advisor
329 * @param success of Focus Advisor
330 */
331 void resetSavedSettings(const bool success);
332
333 /**
334 * @brief Add a row to the results table
335 * @param section of Focus Advisor being run, e.g. find stars
336 * @param run number
337 * @param startPos of the focuser
338 * @param stepSize being used
339 * @param overscan value being used
340 * @param additional text for the row
341 */
342 void addResultsTable(QString section, int run, int startPos, int stepSize, int overscan, QString text);
343
344 /**
345 * @brief Update the text of the current row in the results table
346 * @param text to update
347 */
348 void updateResultsTable(QString text);
349
350 /**
351 * @brief Resize the dialog box
352 */
353 void resizeDialog();
354
355 Focus *m_focus { nullptr };
356 QVariantMap m_map;
357
358 // Help popup
359 std::unique_ptr<Ui::focusAdvisorHelpDialog> m_helpUI;
360 QPointer<QDialog> m_helpDialog;
361
362 // Buttons
363 QPushButton *m_runButton;
364 QPushButton *m_stopButton;
365 QPushButton *m_helpButton;
366
367 // Control bit for Focus Advisor
368 bool m_inFocusAdvisor = false;
369
370 // Initial parameter settings restore in case Focus Advisor fails
371 int m_initialStepSize = -1;
372 int m_initialBacklash = -1;
373 int m_initialAFOverscan = -1;
374 bool m_initialUseWeights = false;
375 bool m_initialScanStartPos = false;
376
377 // Vectors to use to analyse position/measure results
378 QVector<int> m_position;
379 QVector<double> m_measure;
380
381 // Control bits for Focus Advisor algorithm stages
382 bool m_inFindStars = false;
383 bool m_inPreAFAdj = false;
384 bool m_inAFAdj = false;
385
386 // Find Stars Algorithm
387 bool m_findStarsIn = false;
388 bool m_findStarsMaxBound = false;
389 bool m_findStarsMinBound = false;
390 bool m_findStarsRange = false;
391 bool m_findStarsRangeIn = false;
392 bool m_findStarsFindInEdge = false;
393 int m_findStarsSector = 0;
394 int m_findStarsJumpsInSector = 0;
395 int m_findStarsRunNum = 0;
396 int m_findStarsInRange = -1;
397 int m_findStarsOutRange = -1;
398
399 // Pre-Autofocus Algorithm
400 int m_preAFInner = 0;
401 bool m_preAFNoStarsOut = false;
402 bool m_preAFNoStarsIn = false;
403 int m_preAFRunNum = 0;
404 bool m_preAFMaxRange = false;
405
406 // Find Stars & Pre-Autofocus Algorithms
407 int m_jumpsToGo = 0;
408 int m_jumpSize = 0;
409
410 // Autofocus algorithm analysis
411 bool m_overscanFound = false;
412 bool m_runAgainR2 = false;
413 bool m_nearFocus = false;
414 int m_AFRunCount = 0;
415
416 // Results table Cols
417 typedef enum
418 {
419 RESULTS_SECTION = 0,
420 RESULTS_RUN_NUMBER,
421 RESULTS_START_POSITION,
422 RESULTS_STEP_SIZE,
423 RESULTS_AFOVERSCAN,
424 RESULTS_TEXT,
425 RESULTS_MAX_COLS
426 } ResultsColID;
427
428 // Help table Cols
429 typedef enum
430 {
431 HELP_PARAMETER = 0,
432 HELP_CURRENT_VALUE,
433 HELP_NEW_VALUE,
434 HELP_MAX_COLS
435 } HelpColID;
436
437 signals:
438 void newMessage(QString);
439 void newStage(Stage);
440};
441
442}
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:83
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 Mon Nov 18 2024 12:16:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.