1 /*
2  *  backlash_comp.h
3  *  PHD Guiding
4  *
5  *  Created by Bruce Waddington
6  *  Copyright (c) 2015 Bruce Waddington and Andy Galasso
7  *  All rights reserved.
8  *
9  *  This source code is distributed under the following "BSD" license
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions are met:
12  *    Redistributions of source code must retain the above copyright notice,
13  *     this list of conditions and the following disclaimer.
14  *    Redistributions in binary form must reproduce the above copyright notice,
15  *     this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *    Neither the name of Bret McKee, Dad Dog Development,
18  *     Craig Stark, Stark Labs nor the names of its
19  *     contributors may be used to endorse or promote products derived from
20  *     this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #ifndef BACKLASH_COMP_H_INCLUDED
37 #define BACKLASH_COMP_H_INCLUDED
38 
39 #include "guiding_stats.h"
40 
41 class Scope;
42 class BLCHistory;
43 
44 struct RunningStats
45 {
46     int count;
47     double currentSS;            // Sum of squares
48     double currentMean;
49 
50     RunningStats();
51     void AddDelta(double val);
52     void Reset();
53 };
54 
55 // Encapsulated class for handling Dec backlash measurement
56 class BacklashTool
57 {
58     int m_pulseWidth;
59     int m_stepCount;
60     int m_northPulseCount;
61     int m_restoreCount;
62     int m_acceptedMoves;
63     double m_lastClearRslt;
64     double m_lastDecGuideRate;
65     double m_backlashResultPx;                // units of pixels
66     double m_cumClearingDistance;
67     bool m_backlashExemption;
68     int m_backlashResultMs;
69     double m_northRate;
70     PHD_Point m_lastMountLocation;
71     PHD_Point m_startingPoint;
72     PHD_Point m_markerPoint;
73     PHD_Point m_endSouth;
74     wxString m_lastStatus;                          // Translated for UI
75     wxString m_lastStatusDebug;                     // Always English for debug log
76     Scope *m_scope;
77     std::vector<double> m_northBLSteps;
78     std::vector<double> m_southBLSteps;
79     double m_driftPerSec;
80     AxisStats m_northStats;
81     wxLongLong_t m_msmtStartTime;
82     wxLongLong_t m_msmtEndTime;
83     double GetLastDecGuideRate();
84 
85 public:
86     enum BLT_STATE
87     {
88         BLT_STATE_INITIALIZE,
89         BLT_STATE_CLEAR_NORTH,
90         BLT_STATE_STEP_NORTH,
91         BLT_STATE_STEP_SOUTH,
92         BLT_STATE_ABORTED,
93         BLT_STATE_TEST_CORRECTION,
94         BLT_STATE_RESTORE,
95         BLT_STATE_WRAPUP,
96         BLT_STATE_COMPLETED
97     } m_bltState;
98 
99     enum MeasurementConstants               // To control the behavior of the measurement process
100     {
101         BACKLASH_MIN_COUNT = 3,
102         BACKLASH_EXPECTED_DISTANCE = 4,
103         BACKLASH_EXEMPTION_DISTANCE = 40,
104         MAX_CLEARING_STEPS = 100,
105         NORTH_PULSE_SIZE = 500,
106         MAX_NORTH_PULSES = 8000,
107         TRIAL_TOLERANCE_AS = 2              // arc-secs
108     };
109 
110     enum MeasurementResults
111     {
112         MEASUREMENT_TOO_FEW_NORTH,
113         MEASUREMENT_TOO_FEW_SOUTH,
114         MEASUREMENT_BL_NOT_CLEARED,
115         MEASUREMENT_SANITY,
116         MEASUREMENT_VALID
117     } m_Rslt;
118 
119 private:
120     MeasurementResults ComputeBacklashPx(double* bltPx, int* bltMs, double* northRate);
121 
122 public:
123 
124     BacklashTool();
125     ~BacklashTool();
126     void StartMeasurement(double DriftPerMin);
127     void StopMeasurement();
128     void DecMeasurementStep(const PHD_Point& currentLoc);
129     void CleanUp();
GetBltState()130     BLT_STATE GetBltState() const { return m_bltState; }
GetMeasurementQuality()131     MeasurementResults GetMeasurementQuality() const { return m_Rslt; }
GetBLTMsmtPulseSize()132     int GetBLTMsmtPulseSize() const { return m_pulseWidth; }
GetBacklashResultPx()133     double GetBacklashResultPx() const { return m_backlashResultPx; }
GetBacklashResultMs()134     int GetBacklashResultMs() const { return m_backlashResultMs; }
135     void GetBacklashSigma(double* SigmaPx, double* SigmaMs);
GetBacklashExempted()136     bool GetBacklashExempted() const { return m_backlashExemption; }
GetLastStatus()137     wxString GetLastStatus() const { return m_lastStatus; }
138     void SetBacklashPulse(int amt);
139     void ShowGraph(wxDialog *pGA, const std::vector<double> &northSteps, const std::vector<double> &southSteps, int PulseSize);
140     bool IsGraphable();
GetNorthSteps()141     const std::vector<double>& GetNorthSteps() const { return m_northBLSteps; }
GetSouthSteps()142     const std::vector<double>& GetSouthSteps() const { return m_southBLSteps; }
143 };
144 
145 class BacklashComp
146 {
147     bool m_compActive;
148     GUIDE_DIRECTION m_lastDirection;
149     int m_adjustmentFloor;
150     int m_adjustmentCeiling;
151     int m_pulseWidth;
152     bool m_fixedSize;
153     ArrayOfDbl m_residualOffsets;
154     Scope *m_pScope;
155     BLCHistory *m_pHistory;
156 
157     void SetCompValues(int requestSize, int floor, int ceiling);
158 
159 public:
160 
161     BacklashComp(Scope *scope);
162     ~BacklashComp();
163 
164     static int GetBacklashPulseMinValue();
165     static int GetBacklashPulseMaxValue();
166 
167     void GetBacklashCompSettings(int *pulseWidth, int *floor, int *ceiling) const;
GetBacklashPulseWidth()168     int GetBacklashPulseWidth() const { return m_pulseWidth; }
169     void SetBacklashPulseWidth(int ms, int floor, int ceiling);
170     void EnableBacklashComp(bool enable);
IsEnabled()171     bool IsEnabled() const { return m_compActive; }
172 
173     // notify BLC about current raw dec offset, the result of prior moves
174     void TrackBLCResults(unsigned int moveOptions, double yRawOffset);
175 
176     // apply a BLC adjustment to the given guide pulse (yAmount) if needed
177     void ApplyBacklashComp(unsigned int moveOptions, double yGuideDistance, int *yAmount);
178 
179     void ResetBLCState();
180 };
181 
182 #endif
183