1 /*
2     SPDX-FileCopyrightText: 2020 Hy Murveit <hy@murveit.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QElapsedTimer>
10 #include <QFile>
11 
12 #include "indi/indicommon.h"
13 #include "indi/inditelescope.h"
14 
15 // This class will help write guide log files, using the PHD2 guide log format.
16 
17 class GuideLog
18 {
19     public:
20         class GuideInfo
21         {
22             public:
23                 double pixelScale = 0;                             // arcseconds/pixel
24                 int binning = 1;
25                 double focalLength = 0;                            // millimeters
26                 // Recent mount position.
27                 double ra = 0, dec = 0, azimuth = 0, altitude = 0; // degrees
28                 ISD::Telescope::PierSide pierSide = ISD::Telescope::PierSide::PIER_UNKNOWN;
29                 double xangle = 0.0, yangle = 0.0;                 // degrees, x,y axis vs ra,dec.
30                 double xrate = 1.0, yrate = 1.0;                   // pixels/second of pulsing.
31         };
32 
33         class GuideData
34         {
35             public:
36                 enum GuideDataType { MOUNT, DROP };
37                 GuideDataType type = MOUNT;
38                 double dx = 0, dy = 0;                            // Should be in units of pixels.
39                 double raDistance = 0, decDistance = 0;           // Should be in units of arcseconds.
40                 double raGuideDistance = 0, decGuideDistance = 0; // Should be in units of arcseconds.
41                 int raDuration = 0, decDuration = 0;              // Should be in units of milliseconds.
42                 GuideDirection raDirection, decDirection = NO_DIR;
43                 double mass = 0;
44                 double snr = 0;
45                 // From https://openphdguiding.org/PHD2_User_Guide.pdf and logs
46                 enum ErrorCode
47                 {
48                     NO_ERRORS = 0,
49                     STAR_SATURATED = 1,
50                     LOW_SNR = 2,
51                     STAR_LOST_LOW_MASS = 3,
52                     EDGE_OF_FRAME = 4,
53                     STAR_MASS_CHANGED = 5,
54                     STAR_LOST_MASS_CHANGED = 6,
55                     NO_STAR_FOUND = 7
56                 };
57                 ErrorCode code = NO_ERRORS;
58         };
59 
60         GuideLog();
61         ~GuideLog();
62 
63         // Won't log unless enable() is called.
enable()64         void enable()
65         {
66             enabled = true;
67         }
disable()68         void disable()
69         {
70             enabled = false;
71         }
72 
73         // These are called for each guiding session.
74         void startGuiding(const GuideInfo &info);
75         void addGuideData(const GuideData &data);
76         void endGuiding();
77 
78         // These are called for each calibration session.
79         void startCalibration(const GuideInfo &info);
80         void addCalibrationData(GuideDirection direction, double x, double y, double xOrigin, double yOrigin);
81         void endCalibrationSection(GuideDirection direction, double degrees);
82         void endCalibration(double raSpeed, double decSpeed);
83 
84         // INFO messages
85         void ditherInfo(double dx, double dy, double x, double y);
86         void pauseInfo();
87         void resumeInfo();
88         void settleStartedInfo();
89         void settleCompletedInfo();
90 
91         // Deal with suspend, resume, dither, ...
92     private:
93         // Write the file header and footer.
94         void startLog();
95         void endLog();
96         void appendToLog(const QString &lines);
97 
98         // Log file info.
99         QFile logFile;
100         QString logFileName;
101 
102         // Message indeces and timers.
103         int guideIndex = 1;
104         int calibrationIndex = 1;
105         QElapsedTimer timer;
106 
107         // Used to write and end-of-guiding message on exit, if this was not called.
108         bool isGuiding = false;
109 
110         // Variable used to detect calibration change of direction.
111         GuideDirection lastCalibrationDirection = NO_DIR;
112 
113         // If false, no logging will occur.
114         bool enabled = false;
115 
116         // True means the filename was created and the log's header has been written.
117         bool initialized = false;
118 };
119