forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagellan1.cpp
485 lines (398 loc) · 13.8 KB
/
magellan1.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#if 0
MAGELLAN Generic
Copyright (C) 2011 Onno Hommes ([email protected])
This library is free software;
you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library;
if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
#endif
#include "magellan1.h"
#include "magellandriver.h"
Magellan1 *telescope = nullptr;
extern char *me;
#define COMM_GROUP "Communication"
#define BASIC_GROUP "Position"
#define MAGELLAN_TRACK 0
#define MAGELLAN_SYNC 1
/* Handy Macros */
#define currentRA EquatorialCoordsRN[0].value
#define currentDEC EquatorialCoordsRN[1].value
static void ISPoll(void *);
static void retryConnection(void *);
/*INDI Propertries */
/**********************************************************************************************/
/************************************ GROUP: Communication ************************************/
/**********************************************************************************************/
/********************************************
Property: Connection
*********************************************/
static ISwitch ConnectS[] = { { "CONNECT", "Connect", ISS_OFF, 0, 0 }, { "DISCONNECT", "Disconnect", ISS_ON, 0, 0 } };
ISwitchVectorProperty ConnectSP = { mydev, "CONNECTION", "Connection", COMM_GROUP, IP_RW, ISR_1OFMANY,
0, IPS_IDLE, ConnectS, NARRAY(ConnectS), "", 0 };
/********************************************
Property: Device Port
*********************************************/
/*wildi removed static */
static IText PortT[] = { { "PORT", "Port", 0, 0, 0, 0 } };
ITextVectorProperty PortTP = { mydev, "DEVICE_PORT", "Ports", COMM_GROUP, IP_RW, 0,
IPS_IDLE, PortT, NARRAY(PortT), "", 0 };
/**********************************************************************************************/
/************************************ GROUP: Position Display**********************************/
/**********************************************************************************************/
/********************************************
Property: Equatorial Coordinates JNow
Perm: RO
*********************************************/
INumber EquatorialCoordsRN[] = { { "RA", "RA H:M:S", "%10.6m", 0., 24., 0., 0., 0, 0, 0 },
{ "DEC", "Dec D:M:S", "%10.6m", -90., 90., 0., 0., 0, 0, 0 } };
INumberVectorProperty EquatorialCoordsRNP = {
mydev, "EQUATORIAL_EOD_COORD", "Equatorial JNow", BASIC_GROUP, IP_RO, 120,
IPS_IDLE, EquatorialCoordsRN, NARRAY(EquatorialCoordsRN), "", 0
};
/*****************************************************************************************************/
/**************************************** END PROPERTIES *********************************************/
/*****************************************************************************************************/
/* send client definitions of all properties */
void ISInit()
{
static int isInit = 0;
if (isInit)
return;
if (telescope == nullptr)
{
IUSaveText(&PortT[0], "/dev/ttyS0");
telescope = new Magellan1();
telescope->setCurrentDeviceName(mydev);
}
isInit = 1;
IEAddTimer(POLLMS, ISPoll, nullptr);
}
void ISGetProperties(const char *dev)
{
ISInit();
telescope->ISGetProperties(dev);
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
ISInit();
telescope->ISNewSwitch(dev, name, states, names, n);
}
void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
{
ISInit();
telescope->ISNewText(dev, name, texts, names, n);
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
ISInit();
telescope->ISNewNumber(dev, name, values, names, n);
}
void ISPoll(void *p)
{
telescope->ISPoll();
IEAddTimer(POLLMS, ISPoll, nullptr);
p = p;
}
void ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[],
char *names[], int n)
{
INDI_UNUSED(dev);
INDI_UNUSED(name);
INDI_UNUSED(sizes);
INDI_UNUSED(blobsizes);
INDI_UNUSED(blobs);
INDI_UNUSED(formats);
INDI_UNUSED(names);
INDI_UNUSED(n);
}
void ISSnoopDevice(XMLEle *root)
{
telescope->ISSnoopDevice(root);
}
/**************************************************
*** MAGELLAN1 Implementation
***************************************************/
Magellan1::Magellan1()
{
currentSiteNum = 1;
trackingMode = MAGELLAN_TRACK_DEFAULT;
lastSet = -1;
fault = false;
simulation = false;
currentSet = 0;
fd = -1;
// Children call parent routines, this is the default
IDLog("Initializing from MAGELLAN device...\n");
IDLog("Driver Version: 2011-07-28\n");
}
Magellan1::~Magellan1()
{
}
void Magellan1::setCurrentDeviceName(const char *devName)
{
strcpy(thisDevice, devName);
}
void Magellan1::ISGetProperties(const char *dev)
{
if (dev != nullptr && strcmp(thisDevice, dev))
return;
// COMM_GROUP
IDDefSwitch(&ConnectSP, nullptr);
IDDefText(&PortTP, nullptr);
// POSITION_GROUP
IDDefNumber(&EquatorialCoordsRNP, nullptr);
/* Send the basic data to the new client if the previous client(s) are already connected. */
if (ConnectSP.s == IPS_OK)
getBasicData();
}
void Magellan1::ISSnoopDevice(XMLEle *root)
{
INDI_UNUSED(root);
}
void Magellan1::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
{
IText *tp;
/* Ignore other devices */
if (strcmp(dev, thisDevice))
return;
/* See if the port is updated */
if (!strcmp(name, PortTP.name))
{
PortTP.s = IPS_OK;
tp = IUFindText(&PortTP, names[0]);
if (!tp)
return;
IUSaveText(&PortTP.tp[0], texts[0]);
IDSetText(&PortTP, nullptr);
return;
}
}
void Magellan1::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
INDI_UNUSED(dev);
INDI_UNUSED(name);
INDI_UNUSED(values);
INDI_UNUSED(names);
INDI_UNUSED(n);
}
void Magellan1::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
INDI_UNUSED(names);
/* Ignore other devices */
if (strcmp(thisDevice, dev))
return;
// FIRST Switch ALWAYS for Connection
if (!strcmp(name, ConnectSP.name))
{
bool connectionEstablished = (ConnectS[0].s == ISS_ON);
if (IUUpdateSwitch(&ConnectSP, states, names, n) < 0)
return;
if ((connectionEstablished && ConnectS[0].s == ISS_ON) || (!connectionEstablished && ConnectS[1].s == ISS_ON))
{
ConnectSP.s = IPS_OK;
IDSetSwitch(&ConnectSP, nullptr);
return;
}
connectTelescope();
return;
}
}
void Magellan1::handleError(ISwitchVectorProperty *svp, int err, const char *msg)
{
svp->s = IPS_ALERT;
/* First check to see if the telescope is connected */
if (check_magellan_connection(fd))
{
/* The telescope is off locally */
ConnectS[0].s = ISS_OFF;
ConnectS[1].s = ISS_ON;
ConnectSP.s = IPS_BUSY;
IDSetSwitch(&ConnectSP, "Telescope is not responding to commands, will retry in 10 seconds.");
IDSetSwitch(svp, nullptr);
IEAddTimer(10000, retryConnection, &fd);
return;
}
/* If the error is a time out, then the device doesn't support this property or busy*/
if (err == -2)
{
svp->s = IPS_ALERT;
IDSetSwitch(svp, "Device timed out. Current device may be busy or does not support %s. Will retry again.", msg);
}
else
/* Changing property failed, user should retry. */
IDSetSwitch(svp, "%s failed.", msg);
fault = true;
}
void Magellan1::handleError(INumberVectorProperty *nvp, int err, const char *msg)
{
nvp->s = IPS_ALERT;
/* First check to see if the telescope is connected */
if (check_magellan_connection(fd))
{
/* The telescope is off locally */
ConnectS[0].s = ISS_OFF;
ConnectS[1].s = ISS_ON;
ConnectSP.s = IPS_BUSY;
IDSetSwitch(&ConnectSP, "Telescope is not responding to commands, will retry in 10 seconds.");
IDSetNumber(nvp, nullptr);
IEAddTimer(10000, retryConnection, &fd);
return;
}
/* If the error is a time out, then the device doesn't support this property */
if (err == -2)
{
nvp->s = IPS_ALERT;
IDSetNumber(nvp, "Device timed out. Current device may be busy or does not support %s. Will retry again.", msg);
}
else
/* Changing property failed, user should retry. */
IDSetNumber(nvp, "%s failed.", msg);
fault = true;
}
void Magellan1::handleError(ITextVectorProperty *tvp, int err, const char *msg)
{
tvp->s = IPS_ALERT;
/* First check to see if the telescope is connected */
if (check_magellan_connection(fd))
{
/* The telescope is off locally */
ConnectS[0].s = ISS_OFF;
ConnectS[1].s = ISS_ON;
ConnectSP.s = IPS_BUSY;
IDSetSwitch(&ConnectSP, "Telescope is not responding to commands, will retry in 10 seconds.");
IDSetText(tvp, nullptr);
IEAddTimer(10000, retryConnection, &fd);
return;
}
/* If the error is a time out, then the device doesn't support this property */
if (err == -2)
{
tvp->s = IPS_ALERT;
IDSetText(tvp, "Device timed out. Current device may be busy or does not support %s. Will retry again.", msg);
}
else
{
/* Changing property failed, user should retry. */
IDSetText(tvp, "%s failed.", msg);
}
fault = true;
}
void Magellan1::correctFault()
{
fault = false;
IDMessage(thisDevice, "Telescope is online.");
}
bool Magellan1::isTelescopeOn()
{
return (ConnectSP.sp[0].s == ISS_ON);
}
static void retryConnection(void *p)
{
int fd = *((int *)p);
if (check_magellan_connection(fd))
{
ConnectSP.s = IPS_IDLE;
IDSetSwitch(&ConnectSP, "The connection to the telescope is lost.");
return;
}
ConnectS[0].s = ISS_ON;
ConnectS[1].s = ISS_OFF;
ConnectSP.s = IPS_OK;
IDSetSwitch(&ConnectSP, "The connection to the telescope has been resumed.");
}
void Magellan1::ISPoll()
{
int err = 0;
if (!isTelescopeOn())
return;
if ((err = getMAGELLANRA(fd, ¤tRA)) < 0 || (err = getMAGELLANDEC(fd, ¤tDEC)) < 0)
{
EquatorialCoordsRNP.s = IPS_ALERT;
IDSetNumber(&EquatorialCoordsRNP, nullptr);
handleError(&EquatorialCoordsRNP, err, "Getting RA/DEC");
return;
}
if (fault)
correctFault();
EquatorialCoordsRNP.s = IPS_OK;
lastRA = currentRA;
lastDEC = currentDEC;
IDSetNumber(&EquatorialCoordsRNP, nullptr);
}
void Magellan1::getBasicData()
{
char calendarDate[32];
int err;
/* Magellan 1 Get Calendar Date As a Test (always 1/1/96) */
if ((err = getCalendarDate(fd, calendarDate)) < 0)
IDMessage(thisDevice, "Failed to retrieve calendar date from device.");
else
IDMessage(thisDevice, "Successfully retrieved calendar date from device.");
/* Only 24 Time format on Magellan and you cant get to the local time
which you can set in Magellan I */
timeFormat = MAGELLAN_24;
if ((err = getMAGELLANRA(fd, &targetRA)) < 0 || (err = getMAGELLANDEC(fd, &targetDEC)) < 0)
{
EquatorialCoordsRNP.s = IPS_ALERT;
IDSetNumber(&EquatorialCoordsRNP, nullptr);
handleError(&EquatorialCoordsRNP, err, "Getting RA/DEC");
return;
}
if (fault)
correctFault();
EquatorialCoordsRNP.np[0].value = targetRA;
EquatorialCoordsRNP.np[1].value = targetDEC;
EquatorialCoordsRNP.s = IPS_OK;
IDSetNumber(&EquatorialCoordsRNP, nullptr);
}
void Magellan1::connectTelescope()
{
switch (ConnectSP.sp[0].s)
{
case ISS_ON:
/* Magellan I only has 1200 buad, 8 data bits, 0 parity and 1 stop bit */
if (tty_connect(PortTP.tp[0].text, 1200, 8, 0, 1, &fd) != TTY_OK)
{
ConnectS[0].s = ISS_OFF;
ConnectS[1].s = ISS_ON;
IDSetSwitch(
&ConnectSP,
"Error connecting to port %s. Make sure you have BOTH write and read permission to your port.\n",
PortTP.tp[0].text);
return;
}
if (check_magellan_connection(fd))
{
ConnectS[0].s = ISS_OFF;
ConnectS[1].s = ISS_ON;
IDSetSwitch(&ConnectSP, "Error connecting to Telescope. Telescope is offline.");
return;
}
#ifdef INDI_DEBUG
IDLog("Telescope test successful.\n");
#endif
ConnectSP.s = IPS_OK;
IDSetSwitch(&ConnectSP, "Telescope is online. Retrieving basic data...");
getBasicData();
break;
case ISS_OFF:
ConnectS[0].s = ISS_OFF;
ConnectS[1].s = ISS_ON;
ConnectSP.s = IPS_IDLE;
IDSetSwitch(&ConnectSP, "Telescope is offline.");
IDLog("Telescope is offline.");
tty_disconnect(fd);
break;
}
}