-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarGoMount.cs
301 lines (254 loc) · 7.75 KB
/
StarGoMount.cs
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
using ASCOM.DriverAccess;
using System.Globalization;
using System;
using System.Threading;
namespace AutoPolarAlign
{
public class StarGoMount : IPolarAlignmentMount
{
public int Aux1Steps { get; set; } = 380;
public int Aux2Steps { get; set; } = 570;
public int Aux1Speed { get; set; } = 9;
public int Aux2Speed { get; set; } = 9;
public bool SetAuxSpeed { get; set; } = true;
public bool ReverseAltitude { get; set; } = false;
public bool ReverseAzimuth { get; set; } = false;
public double SettlingTime { get; set; } = 1.0;
private Telescope telescope;
public void Connect()
{
try
{
if (telescope == null)
{
telescope = new Telescope("ASCOM.AvalonStarGo.NET.Telescope");
}
telescope.Connected = true;
Thread.Sleep(TimeSpan.FromSeconds(1));
}
catch (Exception ex)
{
throw new Exception("Failed connecting to StarGo", ex);
}
}
public void Disconnect()
{
if (telescope != null)
{
telescope.Connected = false;
}
}
public void Dispose()
{
telescope?.Dispose();
}
private double EstimateDurationForMove(double amount, int auxsteps, int auxspeed)
{
int stepsPerSecond;
switch (auxspeed)
{
case 10:
stepsPerSecond = 2000;
break;
case 9:
stepsPerSecond = 1200;
break;
case 8:
stepsPerSecond = 500;
break;
default:
stepsPerSecond = 250;
break;
}
int totalSteps = (int)Math.Ceiling(Math.Abs(amount * auxsteps));
return totalSteps / (double)stepsPerSecond + 1 /* Compensate ramp up time */ + SettlingTime;
}
private void SendSetSpeedAltitude(int auxspeed)
{
string cmd;
switch (auxspeed)
{
case 10:
cmd = ":X1D0060*50#";
break;
case 9:
cmd = ":X1D0100*40#";
break;
case 8:
cmd = ":X1D0250*30#";
break;
case 7:
cmd = ":X1D0500*20#";
break;
case 6:
cmd = ":X1D0750*10#";
break;
case 5:
cmd = ":X1D1000*05#";
break;
case 4:
cmd = ":X1D2500*01#";
break;
case 3:
cmd = ":X1D4000*01#";
break;
case 2:
cmd = ":X1D6000*01#";
break;
default:
cmd = ":X1D9000*01#";
break;
}
SendCommand(cmd);
}
private void SendSyncAltitude()
{
SendCommand(":X11500001#");
}
private double SendPositionAltitude(double pos)
{
double steps = pos * Aux2Steps;
if (Math.Abs(steps) > 99998)
{
steps = Math.Sign(steps) * 99998;
pos -= steps / Aux2Steps;
}
else
{
pos = 0;
}
if (ReverseAltitude)
{
steps = -steps;
}
if (steps > 0)
{
SendCommand(string.Format(CultureInfo.InvariantCulture, ":X175{0:D5}#", (int)Math.Ceiling(steps)));
}
else
{
SendCommand(string.Format(CultureInfo.InvariantCulture, ":X174{0:D5}#", (int)Math.Floor(100000 + steps)));
}
return pos;
}
public void MoveAltitude(double amount)
{
while (Math.Abs(amount) > double.Epsilon)
{
SendSyncAltitude();
if (SetAuxSpeed)
{
SendSetSpeedAltitude(Aux2Speed);
}
double startAmount = amount;
amount = SendPositionAltitude(amount);
double moveAmount = amount - startAmount;
Thread.Sleep(TimeSpan.FromSeconds(EstimateDurationForMove(moveAmount, Aux2Steps, Aux2Speed)));
StopAltitude();
}
}
public void StopAltitude()
{
SendCommand(":X0FAUX2ST#");
}
private void SendSetSpeedAzimuth(int auxspeed)
{
string cmd;
switch (auxspeed)
{
case 10:
cmd = ":X1C0060*50#";
break;
case 9:
cmd = ":X1C0100*40#";
break;
case 8:
cmd = ":X1C0250*30#";
break;
case 7:
cmd = ":X1C0500*20#";
break;
case 6:
cmd = ":X1C0750*10#";
break;
case 5:
cmd = ":X1C1000*05#";
break;
case 4:
cmd = ":X1C2500*01#";
break;
case 3:
cmd = ":X1C4000*01#";
break;
case 2:
cmd = ":X1C6000*01#";
break;
default:
cmd = ":X1C9000*01#";
break;
}
SendCommand(cmd);
}
private void SendSyncAzimuth()
{
SendCommand(":X0C500001#");
}
private double SendPositionAzimuth(double pos)
{
double steps = pos * Aux1Steps;
if (Math.Abs(steps) > 99998)
{
steps = Math.Sign(steps) * 99998;
pos -= steps / Aux1Steps;
}
else
{
pos = 0;
}
if (ReverseAzimuth)
{
steps = -steps;
}
if (steps > 0)
{
SendCommand(string.Format(CultureInfo.InvariantCulture, ":X165{0:D5}#", (int)Math.Ceiling(steps)));
}
else
{
SendCommand(string.Format(CultureInfo.InvariantCulture, ":X164{0:D5}#", (int)Math.Floor(100000 + steps)));
}
return pos;
}
public void MoveAzimuth(double amount)
{
while (Math.Abs(amount) > double.Epsilon)
{
SendSyncAzimuth();
if (SetAuxSpeed)
{
SendSetSpeedAzimuth(Aux1Speed);
}
double startAmount = amount;
amount = SendPositionAzimuth(amount);
double moveAmount = amount - startAmount;
Thread.Sleep(TimeSpan.FromSeconds(EstimateDurationForMove(moveAmount, Aux1Steps, Aux1Speed)));
StopAzimuth();
}
}
public void StopAzimuth()
{
SendCommand(":X0AAUX1ST#");
}
private void SendCommand(string command)
{
try
{
telescope.CommandString(command, false);
}
catch (Exception ex)
{
throw new Exception("Failed sending command to mount", ex);
}
}
}
}