1 /*
2     SPDX-FileCopyrightText: 2016 Jasem Mutlaq <mutlaqja@ikarustech.com>.
3 
4     Based on lin_guider
5 
6     SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 
9 #pragma once
10 
11 #include "matr.h"
12 #include "fitsviewer/fitsdata.h"
13 #include "indi/indicommon.h"
14 #include "../guideinterface.h"
15 #include "guidelog.h"
16 #include "calibration.h"
17 #include "calibrationprocess.h"
18 
19 #include <QFile>
20 #include <QPointer>
21 #include <QQueue>
22 #include <QTime>
23 
24 #include <memory>
25 
26 class QVector3D;
27 
28 class cgmath;
29 class GuideView;
30 class Edge;
31 
32 namespace Ekos
33 {
34 class InternalGuider : public GuideInterface
35 {
36         Q_OBJECT
37 
38     public:
39         InternalGuider();
40 
Connect()41         bool Connect() override
42         {
43             return true;
44         }
Disconnect()45         bool Disconnect() override
46         {
47             return true;
48         }
isConnected()49         bool isConnected() override
50         {
51             return true;
52         }
53 
54         bool calibrate() override;
55         bool guide() override;
56         bool abort() override;
57         bool suspend() override;
58         bool resume() override;
59 
60         bool dither(double pixels) override;
61         bool ditherXY(double x, double y);
62 
63         bool clearCalibration() override;
64         bool restoreCalibration();
65 
66         bool reacquire() override;
67 
68         bool setFrameParams(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t binX, uint16_t binY) override;
69         bool setGuiderParams(double ccdPixelSizeX, double ccdPixelSizeY, double mountAperture,
70                              double mountFocalLength) override;
71 
72         // Set Star Position
73         void setStarPosition(QVector3D &starCenter) override;
74 
75         // Select algorithm
76         void setSquareAlgorithm(int index);
77 
78         // Reticle Parameters
79         void setReticleParameters(double x, double y);
80         bool getReticleParameters(double *x, double *y);
81 
82         // Guide Square Box Size
setGuideBoxSize(uint32_t value)83         void setGuideBoxSize(uint32_t value)
84         {
85             guideBoxSize = value;
86         }
87 
88         // Guide View
89         void setGuideView(GuideView *guideView);
90         // Image Data
91         void setImageData(const QSharedPointer<FITSData> &data);
92 
93         bool start();
94 
95         bool isGuiding(void) const;
96         void setInterface(void);
97 
setSubFramed(bool enable)98         void setSubFramed(bool enable)
99         {
100             m_isSubFramed = enable;
101         }
102 
103         bool useSubFrame();
104 
105         const Calibration &getCalibration() const;
106 
107         // Select a guide star automatically
108         bool selectAutoStar();
109         bool selectAutoStarSEPMultistar();
110         bool SEPMultiStarEnabled();
111 
112         // Manual Dither
113         bool processManualDithering();
114 
115         void updateGPGParameters();
116         void resetGPG() override;
117 
118     public slots:
119         void setDECSwap(bool enable);
120 
121     protected slots:
122         void trackingStarSelected(int x, int y);
123         void setDitherSettled();
124 
125     signals:
126         void newPulse(GuideDirection ra_dir, int ra_msecs, GuideDirection dec_dir, int dec_msecs);
127         void newPulse(GuideDirection dir, int msecs);
128         //void newStarPosition(QVector3D, bool);
129         void DESwapChanged(bool enable);
130 
131     private:
132         // Guiding
133         bool processGuiding();
134 
135         bool abortDither();
136 
137         void reset();
138 
139         // Logging
140         void fillGuideInfo(GuideLog::GuideInfo *info);
141 
142         std::unique_ptr<cgmath> pmath;
143         QPointer<GuideView> guideFrame;
144         QSharedPointer<FITSData> m_ImageData;
145         bool m_isStarted { false };
146         bool m_isSubFramed { false };
147         bool m_isFirstFrame { false };
148         int m_starLostCounter { 0 };
149 
150         QFile logFile;
151         uint32_t guideBoxSize { 32 };
152 
153         GuiderUtils::Vector m_DitherTargetPosition;
154         uint8_t m_DitherRetries {0};
155 
156         QElapsedTimer reacquireTimer;
157         int m_highRMSCounter {0};
158 
159         GuiderUtils::Matrix ROT_Z;
160         Ekos::GuideState rememberState { GUIDE_IDLE };
161 
162         // Progressive Manual Dither
163         QQueue<GuiderUtils::Vector> m_ProgressiveDither;
164 
165         // How many high RMS pulses before we stop
166         static const uint8_t MAX_RMS_THRESHOLD = 10;
167         // How many lost stars before we stop
168         static const uint8_t MAX_LOST_STAR_THRESHOLD = 5;
169 
170         // Maximum pulse time limit for immediate capture. Any pulses longer that this
171         // will be delayed until pulse is over
172         static const uint16_t MAX_IMMEDIATE_CAPTURE = 250;
173         // When to start capture before pulse delay is over
174         static const uint16_t PROPAGATION_DELAY = 100;
175 
176         // How many 'random' pixels can we move before we have to force direction reversal?
177         static const uint8_t MAX_DITHER_TRAVEL = 15;
178 
179         QPair<double, double> accumulator;
180         GuideLog guideLog;
181 
182         void iterateCalibration();
183         std::unique_ptr<CalibrationProcess> calibrationProcess;
184         double calibrationStartX = 0;
185         double calibrationStartY = 0;
186 };
187 }
188