-
Notifications
You must be signed in to change notification settings - Fork 3
/
pimoco_mount_limits.cpp
188 lines (150 loc) · 7.59 KB
/
pimoco_mount_limits.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
/*
PiMoCo: Raspberry Pi Telescope Mount and Focuser Control
Copyright (C) 2021 Markus Noga
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "pimoco_mount.h"
#include <libindi/indilogger.h>
#include <libindi/indicom.h> // for rangeHA etc.
#include <libnova/julian_day.h>
#include <math.h> // for tan()
#define cotan(x) (1.0/tan(x))
void PimocoMount::equatorialFromDevice(double *equRA, double *equDec, TelescopePierSide *equPS, double deviceHA, double deviceDec, double lst) {
double equHA;
// Conversion to equatorial coordinates per the ASCOM definition. However, note that
// ASCOM encodes pointing state (normal/beyond the pole) in the side of pier field.
// See https://ascom-standards.org/Help/Platform/html/P_ASCOM_DeviceInterface_ITelescopeV3_SideOfPier.htm
// Indi needs physical sie of pier reporting (scope is east/west of pier), so we
// override the ASCOM definition of the field.
if(abs(deviceDec)<=90) {
// normal pointing state, east pointing west
//*equPS =PIER_EAST; // ignore ASCOM definition
equHA =rangeHA(deviceHA);
*equDec=rangeDec(deviceDec);
} else {
// above the pole pointing state, west pointing east
//*equPS =PIER_WEST; // ignore ASCOM definition
equHA =rangeHA(deviceHA+12);
*equDec=rangeDec(180.0-deviceDec);
}
// set side of pier based on physical telescope position
const auto dHA=rangeHA(deviceHA);
*equPS = (dHA>-6 && dHA<6) ? PIER_WEST : PIER_EAST;
if(lst<0)
lst=getLocalSiderealTime();
*equRA=range24(lst - equHA);
if(stepperHA.getDebugLevel()>=Stepper::TMC_DEBUG_DEBUG)
LOGF_DEBUG("eqFromDev: device HA %f Dec %f >> equ HA %f RA %f Dec %f pier %d %s @ lst %f",
deviceHA, deviceDec, equHA, *equRA, *equDec, *equPS, getPierSideStr(*equPS), lst);
}
bool PimocoMount::deviceFromEquatorial(double *deviceHA, double *deviceDec, double equRA, double equDec, TelescopePierSide equPS, double lst) {
if(lst<0)
lst=getLocalSiderealTime();
double equHA=rangeHA(lst - equRA);
const auto impliedPS=(equHA>-6 && equHA<6) ? PIER_WEST : PIER_EAST;
if(impliedPS==equPS) {
//if(equPS==PIER_EAST || equPS==PIER_UNKNOWN) {
// normal pointing state, east pointing west
*deviceHA =rangeHA(equHA);
*deviceDec=rangeDec(equDec);
} else {
// above the pole pointing state, west pointing east
*deviceHA =rangeHA(equHA-12);
*deviceDec=180.0-rangeDec(equDec);
}
// bring position into mount limits, if possible
while(*deviceHA<HALimitsN[0].value)
(*deviceHA)+=24;
while(*deviceHA>HALimitsN[1].value)
(*deviceHA)-=24;
bool valid=((*deviceHA)>=HALimitsN[0].value) && ((*deviceHA)<=HALimitsN[1].value);
if(!valid)
if(stepperHA.getDebugLevel()>=Stepper::TMC_DEBUG_DEBUG)
LOGF_DEBUG("devFromEq: device HA %f Dec %f from equ HA %f RA %f Dec %f pier %d %s @ lst %f",
*deviceHA, *deviceDec, equHA, equRA, equDec, equPS, getPierSideStr(equPS), lst);
return valid;
}
void PimocoMount::horizonFromEquatorial(double *horAlt, double *horAz, double equRA, double equDec, double jd) {
struct ln_equ_posn equ_t0={equRA*(360.0/24.0), equDec};
if(jd<0)
jd=ln_get_julian_from_sys();
struct ln_hrz_posn hor;
get_hrz_from_equ(&equ_t0, &lnobserver, jd, &hor);
*horAlt=hor.alt;
*horAz =hor.az;
}
double refractionArcminsFromApparentAltitude(double appAltDegrees, double pressureMillibars, double tempCelsius) {
// see https://en.wikipedia.org/wiki/Atmospheric_refraction
double arg=appAltDegrees + 7.31/(appAltDegrees + 4.4);
double argRad=arg*M_PI/180.0;
double r=cotan(argRad);
double correction=(pressureMillibars/1013.25)*(283.0/(273.0+tempCelsius));
return r*correction;
}
double refractionArcminsFromTrueAltitude(double trueAltDegrees, double pressureMillibars, double tempCelsius) {
// see https://en.wikipedia.org/wiki/Atmospheric_refraction
double arg=trueAltDegrees+ 10.3/(trueAltDegrees+5.11);
double argRad=arg*M_PI/180.0;
double val=1.02*cotan(argRad);
double correction=(pressureMillibars/1013.25)*(283.0/(273.0+tempCelsius));
return val*correction;
}
bool PimocoMount::checkLimitsHA(double deviceHA) {
bool inside=(deviceHA>=HALimitsN[0].value) && (deviceHA<=HALimitsN[1].value);
if(!inside)
LOGF_ERROR("Device HA %f outside limits [%f, %f]", deviceHA, HALimitsN[0].value, HALimitsN[1].value);
return inside;
}
bool PimocoMount::checkLimitsHA(double deviceHA, double arcsecPerSecHA) {
bool inside=(deviceHA>=HALimitsN[0].value) && (deviceHA<=HALimitsN[1].value);
if(!inside)
inside= (deviceHA<HALimitsN[0].value && arcsecPerSecHA>0) ||
(deviceHA>HALimitsN[1].value && arcsecPerSecHA<0) ;
if(!inside)
LOGF_ERROR("Device HA %f speed %f outside limits [%f, %f]", deviceHA, arcsecPerSecHA, HALimitsN[0].value, HALimitsN[1].value);
return inside;
}
bool PimocoMount::checkLimitsAlt(double horAlt) {
bool inside=(horAlt>=AltLimitsN[0].value) && (horAlt<=AltLimitsN[1].value);
if(!inside)
LOGF_ERROR("Altitude %f outside limits [%f, %f]", horAlt, AltLimitsN[0].value, AltLimitsN[1].value);
return inside;
}
bool PimocoMount::checkLimitsAlt(double horAlt, double deviceHA, double deviceDec, double arcsecPerSecHA, double arcsecPerSecDec, double jd, double lst) {
bool inside=(horAlt>=AltLimitsN[0].value) && (horAlt<=AltLimitsN[1].value);
if(!inside) {
// check where we will be in one second
double deviceHA2 =deviceHA + arcsecPerSecHA /(15*60);
double deviceDec2=deviceDec + arcsecPerSecDec /(60*60);
double jd2 =jd + 1.0/(24.0*60.0*60.0);
double lst2 =lst + 1.0/( 60.0*60.0);
double equRA2, equDec2; // RA in hours, declination in degrees
TelescopePierSide equPS2;
equatorialFromDevice(&equRA2, &equDec2, &equPS2, deviceHA2, deviceDec2, lst2);
double horAlt2, horAz2;
horizonFromEquatorial(&horAlt2, &horAz2, equRA2, equDec2, jd2);
// will motion get us at least 0.1 arcsec closer to a compliant state?
inside=((horAlt < AltLimitsN[0].value) && (horAlt2 > horAlt + 0.1/(60.0*60.0))) ||
((horAlt > AltLimitsN[1].value) && (horAlt2 < horAlt - 0.1/(60.0*60.0))) ;
}
if(!inside)
LOGF_ERROR("Altitude %f outside limits [%f, %f]", horAlt, AltLimitsN[0].value, AltLimitsN[1].value);
return inside;
}
bool PimocoMount::applyLimits(double arcsecPerSecHA, double arcsecPerSecDec) {
if(!checkLimitsAlt(AltAzN[0].value, DeviceCoordN[0].value, DeviceCoordN[1].value, arcsecPerSecHA, arcsecPerSecDec, TimeN[0].value, TimeN[1].value) ||
!checkLimitsHA(DeviceCoordN[0].value, arcsecPerSecHA) ) {
Abort();
return false;
}
return true;
}