/
guide_algorithm_hysteresis.cpp
executable file
·335 lines (274 loc) · 11.3 KB
/
guide_algorithm_hysteresis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* guide_algorithm_ra.cpp
* PHD Guiding
*
* Created by Bret McKee
* Copyright (c) 2012 Bret McKee
* All rights reserved.
*
* Based upon work by Craig Stark.
* Copyright (c) 2006-2010 Craig Stark.
* All rights reserved.
*
* This source code is distributed under the following "BSD" license
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Bret McKee, Dad Dog Development,
* Craig Stark, Stark Labs nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "phd.h"
static const double DefaultMinMove = 0.2;
static const double DefaultHysteresis = 0.1;
static const double DefaultAggression = 0.7;
static const double MaxAggression = 2.0;
static const double MaxHysteresis = 1.0;
GuideAlgorithmHysteresis::GuideAlgorithmHysteresis(Mount *pMount, GuideAxis axis)
: GuideAlgorithm(pMount, axis)
{
wxString configPath = GetConfigPath();
double minMove = pConfig->Profile.GetDouble(configPath + "/minMove", DefaultMinMove);
SetMinMove(minMove);
double hysteresis = pConfig->Profile.GetDouble(configPath + "/hysteresis", DefaultHysteresis);
SetHysteresis(hysteresis);
double aggression = pConfig->Profile.GetDouble(configPath + "/aggression", DefaultAggression);
SetAggression(aggression);
reset();
}
GuideAlgorithmHysteresis::~GuideAlgorithmHysteresis(void)
{
}
GUIDE_ALGORITHM GuideAlgorithmHysteresis::Algorithm(void)
{
return GUIDE_ALGORITHM_HYSTERESIS;
}
void GuideAlgorithmHysteresis::reset(void)
{
m_lastMove = 0;
}
double GuideAlgorithmHysteresis::result(double input)
{
double dReturn = (1.0 - m_hysteresis) * input + m_hysteresis * m_lastMove;
dReturn *= m_aggression;
if (fabs(input) < m_minMove)
{
dReturn = 0.0;
}
m_lastMove = dReturn;
Debug.Write(wxString::Format("GuideAlgorithmHysteresis::Result() returns %.2f from input %.2f\n", dReturn, input));
return dReturn;
}
double GuideAlgorithmHysteresis::GetMinMove(void)
{
return m_minMove;
}
bool GuideAlgorithmHysteresis::SetMinMove(double minMove)
{
bool bError = false;
try
{
if (minMove < 0.0)
{
throw ERROR_INFO("invalid minMove");
}
m_minMove = minMove;
}
catch (wxString Msg)
{
POSSIBLY_UNUSED(Msg);
bError = true;
m_minMove = DefaultMinMove;
}
pConfig->Profile.SetDouble(GetConfigPath() + "/minMove", m_minMove);
return bError;
}
double GuideAlgorithmHysteresis::GetHysteresis(void)
{
return m_hysteresis;
}
bool GuideAlgorithmHysteresis::SetHysteresis(double hysteresis)
{
bool bError = false;
try
{
if (hysteresis < 0.0 || hysteresis > MaxHysteresis)
{
throw ERROR_INFO("invalid hysteresis");
}
m_hysteresis = hysteresis;
}
catch (wxString Msg)
{
POSSIBLY_UNUSED(Msg);
bError = true;
m_hysteresis = wxMin(wxMax(hysteresis, 0.0), MaxHysteresis);
}
pConfig->Profile.SetDouble(GetConfigPath() + "/hysteresis", m_hysteresis);
return bError;
}
double GuideAlgorithmHysteresis::GetAggression(void)
{
return m_aggression;
}
bool GuideAlgorithmHysteresis::SetAggression(double aggression)
{
bool bError = false;
try
{
if (aggression <= 0.0 || aggression > MaxAggression)
{
throw ERROR_INFO("invalid aggression");
}
m_aggression = aggression;
}
catch (wxString Msg)
{
POSSIBLY_UNUSED(Msg);
bError = true;
m_aggression = wxMin(wxMax(aggression, 0.1), MaxAggression);
}
m_lastMove = 0.0;
pConfig->Profile.SetDouble(GetConfigPath() + "/aggression", m_aggression);
return bError;
}
wxString GuideAlgorithmHysteresis::GetSettingsSummary()
{
// return a loggable summary of current mount settings
return wxString::Format("Hysteresis = %.3f, Aggression = %.3f, Minimum move = %.3f\n",
GetHysteresis(),
GetAggression(),
GetMinMove()
);
}
ConfigDialogPane *GuideAlgorithmHysteresis::GetConfigDialogPane(wxWindow *pParent)
{
return new GuideAlgorithmHysteresisConfigDialogPane(pParent, this);
}
GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisConfigDialogPane::
GuideAlgorithmHysteresisConfigDialogPane(wxWindow *pParent, GuideAlgorithmHysteresis *pGuideAlgorithm)
: ConfigDialogPane(_("Hysteresis Guide Algorithm"), pParent)
{
int width;
m_pGuideAlgorithm = pGuideAlgorithm;
width = StringWidth(_T("000"));
m_pHysteresis = new wxSpinCtrlDouble(pParent, wxID_ANY,_T(""), wxPoint(-1,-1),
wxSize(width + 30, -1), wxSP_ARROW_KEYS, 0.0, MaxHysteresis * 100.0, 0.0, 5.0, _T("Hysteresis"));
m_pHysteresis->SetDigits(0);
DoAdd(_("Hysteresis"), m_pHysteresis,
wxString::Format(_("How much history of previous guide pulses should be applied\nDefault = %.f%%, increase to smooth out guiding commands"), DefaultHysteresis * 100.0));
width = StringWidth(_T("000"));
m_pAggression = new wxSpinCtrlDouble(pParent, wxID_ANY,_T(""), wxPoint(-1,-1),
wxSize(width+30, -1), wxSP_ARROW_KEYS, 0.0, MaxAggression * 100.0, 0.0, 5.0, _T("Aggression"));
m_pAggression->SetDigits(0);
DoAdd(_("Aggression"), m_pAggression,
wxString::Format(_("What percent of the measured error should be applied? Default = %.f%%, adjust if responding too much or too slowly"), DefaultAggression * 100.0));
width = StringWidth(_T("00.00"));
m_pMinMove = new wxSpinCtrlDouble(pParent, wxID_ANY,_T("foo2"), wxPoint(-1,-1),
wxSize(width+30, -1), wxSP_ARROW_KEYS, 0.0, 20.0, 0.0, 0.05, _T("MinMove"));
m_pMinMove->SetDigits(2);
DoAdd(_("Minimum Move (pixels)"), m_pMinMove,
wxString::Format(_("How many (fractional) pixels must the star move to trigger a guide pulse? Default = %.2f"), DefaultMinMove));
}
GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisConfigDialogPane::
~GuideAlgorithmHysteresisConfigDialogPane(void)
{
}
void GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisConfigDialogPane::
LoadValues(void)
{
m_pHysteresis->SetValue(100.0 * m_pGuideAlgorithm->GetHysteresis());
m_pAggression->SetValue(100.0 * m_pGuideAlgorithm->GetAggression());
m_pMinMove->SetValue(m_pGuideAlgorithm->GetMinMove());
}
void GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisConfigDialogPane::
UnloadValues(void)
{
m_pGuideAlgorithm->SetHysteresis(m_pHysteresis->GetValue() / 100.0);
m_pGuideAlgorithm->SetAggression(m_pAggression->GetValue() / 100.0);
m_pGuideAlgorithm->SetMinMove(m_pMinMove->GetValue());
}
GraphControlPane *GuideAlgorithmHysteresis::GetGraphControlPane(wxWindow *pParent, const wxString& label)
{
return new GuideAlgorithmHysteresisGraphControlPane(pParent, this, label);
}
GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisGraphControlPane::
GuideAlgorithmHysteresisGraphControlPane(wxWindow *pParent, GuideAlgorithmHysteresis *pGuideAlgorithm, const wxString& label)
: GraphControlPane(pParent, label)
{
int width;
m_pGuideAlgorithm = pGuideAlgorithm;
// Aggression
width = StringWidth(_T("000"));
m_pAggression = new wxSpinCtrlDouble(this, wxID_ANY,_T(""), wxDefaultPosition,
wxSize(width+30, -1), wxSP_ARROW_KEYS | wxALIGN_RIGHT, 0.0, MaxAggression * 100.0, 0.0, 5.0, _T("Aggression"));
m_pAggression->SetDigits(0);
m_pAggression->Bind(wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, &GuideAlgorithmHysteresis::GuideAlgorithmHysteresisGraphControlPane::OnAggressionSpinCtrlDouble, this);
DoAdd(m_pAggression, _("Agr"));
// Hysteresis
width = StringWidth(_T("000"));
m_pHysteresis = new wxSpinCtrlDouble(this, wxID_ANY,_T(""), wxDefaultPosition,
wxSize(width + 30, -1), wxSP_ARROW_KEYS | wxALIGN_RIGHT, 0.0, MaxHysteresis * 100.0, 0.0, 5.0, _T("Hysteresis"));
m_pHysteresis->SetDigits(0);
m_pHysteresis->Bind(wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, &GuideAlgorithmHysteresis::GuideAlgorithmHysteresisGraphControlPane::OnHysteresisSpinCtrlDouble, this);
DoAdd(m_pHysteresis,_("Hys"));
// Min move
width = StringWidth(_T("00.00"));
m_pMinMove = new wxSpinCtrlDouble(this, wxID_ANY,_T(""), wxPoint(-1,-1),
wxSize(width+30, -1), wxSP_ARROW_KEYS, 0.0, 20.0, 0.0, 0.05,_T("MinMove"));
m_pMinMove->SetDigits(2);
m_pMinMove->Bind(wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, &GuideAlgorithmHysteresis::GuideAlgorithmHysteresisGraphControlPane::OnMinMoveSpinCtrlDouble, this);
DoAdd(m_pMinMove,_("MnMo"));
m_pHysteresis->SetValue(100.0 * m_pGuideAlgorithm->GetHysteresis());
m_pAggression->SetValue(100.0 * m_pGuideAlgorithm->GetAggression());
m_pMinMove->SetValue(m_pGuideAlgorithm->GetMinMove());
}
GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisGraphControlPane::
~GuideAlgorithmHysteresisGraphControlPane(void)
{
}
void GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisGraphControlPane::
OnAggressionSpinCtrlDouble(wxSpinDoubleEvent& WXUNUSED(evt))
{
m_pGuideAlgorithm->SetAggression(m_pAggression->GetValue() / 100.0);
GuideLog.SetGuidingParam(m_pGuideAlgorithm->GetAxis() + " Hysteresis aggression", m_pAggression->GetValue());
}
void GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisGraphControlPane::
OnHysteresisSpinCtrlDouble(wxSpinDoubleEvent& WXUNUSED(evt))
{
m_pGuideAlgorithm->SetHysteresis(this->m_pHysteresis->GetValue() / 100.0);
GuideLog.SetGuidingParam(m_pGuideAlgorithm->GetAxis() + " Hysteresis hysteresis", m_pHysteresis->GetValue());
}
void GuideAlgorithmHysteresis::
GuideAlgorithmHysteresisGraphControlPane::
OnMinMoveSpinCtrlDouble(wxSpinDoubleEvent& WXUNUSED(evt))
{
m_pGuideAlgorithm->SetMinMove(m_pMinMove->GetValue());
GuideLog.SetGuidingParam(m_pGuideAlgorithm->GetAxis() + " Hysteresis minimum move", m_pMinMove->GetValue());
}