source: BAORadio/libindi/v1/drivers/telescope/lx200apdriver.c@ 689

Last change on this file since 689 was 490, checked in by campagne, 15 years ago

import libindi (JEC)

File size: 13.1 KB
Line 
1#if 0
2 LX200 Astro-Physics Driver
3 Copyright (C) 2007 Markus Wildi
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19#endif
20
21/*ToDo: compare the routes with the new ones from lx200driver.c r111 */
22
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdarg.h>
28#include <math.h>
29#include <sys/time.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <time.h>
33
34#include "indicom.h"
35#include "indidevapi.h"
36#include "lx200driver.h"
37#include "lx200apdriver.h"
38
39#ifndef _WIN32
40#include <termios.h>
41#endif
42
43#define LX200_TIMEOUT 5 /* FD timeout in seconds */
44int check_lx200ap_connection(int fd)
45{
46
47 int i=0;
48/* char ack[1] = { (char) 0x06 }; does not work for AP moung */
49 char temp_string[64];
50 int error_type;
51 int nbytes_write=0;
52 int nbytes_read=0;
53
54 #ifdef INDI_DEBUG
55 IDLog("Testing telescope's connection using #:GG#...\n");
56 #endif
57
58 if (fd <= 0)
59 {
60 #ifdef INDI_DEBUG
61 IDLog("check_lx200ap_connection: not a valid file descriptor received\n");
62 #endif
63 return -1;
64 }
65 for (i=0; i < 2; i++)
66 {
67 if ( (error_type = tty_write_string(fd, "#:GG#", &nbytes_write)) != TTY_OK)
68 {
69 #ifdef INDI_DEBUG
70 IDLog("check_lx200ap_connection: unsuccessful write to telescope, %d\n", nbytes_write);
71 #endif
72
73 return error_type;
74 }
75 error_type = tty_read_section(fd, temp_string, '#', LX200_TIMEOUT, &nbytes_read) ;
76 tcflush(fd, TCIFLUSH);
77 if (nbytes_read > 1)
78 {
79 temp_string[ nbytes_read -1] = '\0';
80 #ifdef INDI_DEBUG
81 IDLog("check_lx200ap_connection: received bytes %d, [%s]\n", nbytes_write, temp_string);
82 #endif
83 return 0;
84 }
85 usleep(50000);
86 }
87
88 #ifdef INDI_DEBUG
89 IDLog("check_lx200ap_connection: wrote, but nothing received\n");
90 #endif
91 return -1;
92}
93int getAPUTCOffset(int fd, double *value)
94{
95 int error_type;
96 int nbytes_write=0;
97 int nbytes_read=0;
98
99 char temp_string[16];
100
101 if ( (error_type = tty_write_string(fd, "#:GG#", &nbytes_write)) != TTY_OK)
102 return error_type;
103
104 if(( error_type = tty_read_section(fd, temp_string, '#', LX200_TIMEOUT, &nbytes_read)) != TTY_OK)
105 {
106 #ifdef INDI_DEBUG
107 IDLog("getAPUTCOffset: saying good bye %d, %d\n", error_type, nbytes_read);
108 #endif
109
110 return error_type ;
111 }
112 tcflush(fd, TCIFLUSH);
113
114/* Negative offsets, see AP keypad manual p. 77 */
115 if((temp_string[0]== 'A') || ((temp_string[0]== '0')&&(temp_string[1]== '0')) ||(temp_string[0]== '@'))
116 {
117 int i ;
118 for( i=nbytes_read; i > 0; i--)
119 {
120 temp_string[i]= temp_string[i-1] ;
121 }
122 temp_string[0] = '-' ;
123 temp_string[nbytes_read + 1] = '\0' ;
124
125 if( temp_string[1]== 'A')
126 {
127 temp_string[1]= '0' ;
128 switch (temp_string[2])
129 {
130 case '5':
131
132 temp_string[2]= '1' ;
133 break ;
134 case '4':
135
136 temp_string[2]= '2' ;
137 break ;
138 case '3':
139
140 temp_string[2]= '3' ;
141 break ;
142 case '2':
143
144 temp_string[2]= '4' ;
145 break ;
146 case '1':
147
148 temp_string[2]= '5' ;
149 break ;
150 default:
151 #ifdef INDI_DEBUG
152 IDLog("getAPUTCOffset: string not handled %s\n", temp_string);
153 #endif
154 return -1 ;
155 break ;
156 }
157 }
158 else if( temp_string[1]== '0')
159 {
160 temp_string[1]= '0' ;
161 temp_string[2]= '6' ;
162 #ifdef INDI_DEBUG
163 IDLog("getAPUTCOffset: done here %s\n", temp_string);
164 #endif
165
166 }
167 else if( temp_string[1]== '@')
168 {
169 temp_string[1]= '0' ;
170 switch (temp_string[2])
171 {
172 case '9':
173
174 temp_string[2]= '7' ;
175 break ;
176 case '8':
177
178 temp_string[2]= '8' ;
179 break ;
180 case '7':
181
182 temp_string[2]= '9' ;
183 break ;
184 case '6':
185
186 temp_string[2]= '0' ;
187 break ;
188 case '5':
189 temp_string[1]= '1' ;
190 temp_string[2]= '1' ;
191 break ;
192 case '4':
193
194 temp_string[1]= '1' ;
195 temp_string[2]= '2' ;
196 break ;
197 default:
198 #ifdef INDI_DEBUG
199 IDLog("getAPUTCOffset: string not handled %s\n", temp_string);
200 #endif
201 return -1 ;
202 break;
203 }
204 }
205 else
206 {
207 #ifdef INDI_DEBUG
208 IDLog("getAPUTCOffset: string not handled %s\n", temp_string);
209 #endif
210 }
211 }
212 else
213 {
214 temp_string[nbytes_read - 1] = '\0' ;
215 }
216/* #ifdef INDI_DEBUG
217 IDLog("getAPUTCOffset: received string %s\n", temp_string);
218 #endif
219*/
220 if (f_scansexa(temp_string, value))
221 {
222 fprintf(stderr, "getAPUTCOffset: unable to process [%s]\n", temp_string);
223 return -1;
224 }
225 return 0;
226}
227int setAPObjectAZ(int fd, double az)
228{
229 int h, m, s;
230 char temp_string[16];
231
232 getSexComponents(az, &h, &m, &s);
233
234 snprintf(temp_string, sizeof( temp_string ), "#:Sz %03d*%02d:%02d#", h, m, s);
235 #ifdef INDI_DEBUG
236 IDLog("setAPObjectAZ: Set Object AZ String %s\n", temp_string);
237 #endif
238
239 return (setStandardProcedure(fd, temp_string));
240}
241
242/* wildi Valid set Values are positive, add error condition */
243
244int setAPObjectAlt(int fd, double alt)
245{
246 int d, m, s;
247 char temp_string[16];
248
249 getSexComponents(alt, &d, &m, &s);
250
251 /* case with negative zero */
252 if (!d && alt < 0)
253 {
254 snprintf(temp_string, sizeof( temp_string ), "#:Sa -%02d*%02d:%02d#", d, m, s) ;
255 }
256 else
257 {
258 snprintf(temp_string, sizeof( temp_string ), "#:Sa %+02d*%02d:%02d#", d, m, s) ;
259 }
260
261 #ifdef INDI_DEBUG
262 IDLog("setAPObjectAlt: Set Object Alt String %s\n", temp_string);
263 #endif
264 return (setStandardProcedure(fd, temp_string));
265}
266int setAPUTCOffset(int fd, double hours)
267{
268 int h, m, s ;
269
270 char temp_string[16];
271/* To avoid the peculiar output format of AP controller, see p. 77 key pad manual */
272 if( hours < 0.)
273 {
274 hours += 24. ;
275 }
276
277 getSexComponents(hours, &h, &m, &s);
278
279 snprintf(temp_string, sizeof( temp_string ), "#:SG %+03d:%02d:%02d#", h, m, s);
280 #ifdef INDI_DEBUG
281 IDLog("setAPUTCOffset: %s\n", temp_string);
282 #endif
283
284
285 return (setStandardProcedure(fd, temp_string));
286}
287int APSyncCM(int fd, char *matchedObject)
288{
289 int error_type;
290 int nbytes_write=0;
291 int nbytes_read=0;
292
293 #ifdef INDI_DEBUG
294 IDLog("APSyncCM\n");
295 #endif
296 if ( (error_type = tty_write_string(fd, "#:CM#", &nbytes_write)) != TTY_OK)
297 return error_type ;
298
299 if(( error_type = tty_read_section(fd, matchedObject, '#', LX200_TIMEOUT, &nbytes_read)) != TTY_OK)
300 return error_type ;
301
302 matchedObject[nbytes_read-1] = '\0';
303
304 /* Sleep 10ms before flushing. This solves some issues with LX200 compatible devices. */
305 usleep(10000);
306
307 tcflush(fd, TCIFLUSH);
308
309 return 0;
310}
311int APSyncCMR(int fd, char *matchedObject)
312{
313 int error_type;
314 int nbytes_write=0;
315 int nbytes_read=0;
316
317 #ifdef INDI_DEBUG
318 IDLog("APSyncCMR\n");
319 #endif
320 if ( (error_type = tty_write_string(fd, "#:CMR#", &nbytes_write)) != TTY_OK)
321 return error_type;
322
323 /* read_ret = portRead(matchedObject, -1, LX200_TIMEOUT); */
324 if(( error_type = tty_read_section(fd, matchedObject, '#', LX200_TIMEOUT, &nbytes_read)) != TTY_OK)
325 return error_type ;
326
327 matchedObject[nbytes_read-1] = '\0';
328
329 /* Sleep 10ms before flushing. This solves some issues with LX200 compatible devices. */
330 usleep(10000);
331
332 tcflush(fd, TCIFLUSH);
333
334 return 0;
335}
336int selectAPMoveToRate(int fd, int moveToRate)
337{
338 int error_type;
339 int nbytes_write=0;
340
341 switch (moveToRate)
342 {
343 /* 1200x */
344 case 0:
345 #ifdef INDI_DEBUG
346 IDLog("selectAPMoveToRate: Setting move to rate to 1200x.\n");
347 #endif
348 if ( (error_type = tty_write_string(fd, "#:RC3#", &nbytes_write)) != TTY_OK)
349 return error_type;
350 break;
351
352 /* 600x */
353 case 1:
354 #ifdef INDI_DEBUG
355 IDLog("selectAPMoveToRate: Setting move to rate to 600x.\n");
356 #endif
357 if ( (error_type = tty_write_string(fd, "#:RC2#", &nbytes_write)) != TTY_OK)
358 return error_type;
359 break;
360
361 /* 64x */
362 case 2:
363 #ifdef INDI_DEBUG
364 IDLog("selectAPMoveToRate: Setting move to rate to 64x.\n");
365 #endif
366 if ( (error_type = tty_write_string(fd, "#:RC1#", &nbytes_write)) != TTY_OK)
367 return error_type;
368 break;
369 /* 12x*/
370 case 3:
371 #ifdef INDI_DEBUG
372 IDLog("selectAPMoveToRate: Setting move to rate to 12x.\n");
373 #endif
374 if ( (error_type = tty_write_string(fd, "#:RC0#", &nbytes_write)) != TTY_OK)
375 return error_type;
376 break;
377
378 default:
379 return -1;
380 break;
381 }
382 return 0;
383}
384int selectAPSlewRate(int fd, int slewRate)
385{
386 int error_type;
387 int nbytes_write=0;
388 switch (slewRate)
389 {
390 /* 1200x */
391 case 0:
392 #ifdef INDI_DEBUG
393 IDLog("selectAPSlewRate: Setting slew rate to 1200x.\n");
394 #endif
395 if ( (error_type = tty_write_string(fd, "#:RS2#", &nbytes_write)) != TTY_OK)
396 return error_type;
397 break;
398
399 /* 900x */
400 case 1:
401 #ifdef INDI_DEBUG
402 IDLog("selectAPSlewRate: Setting slew rate to 900x.\n");
403 #endif
404 if ( (error_type = tty_write_string(fd, "#:RS1#", &nbytes_write)) != TTY_OK)
405 return error_type;
406 break;
407
408 /* 600x */
409 case 2:
410 #ifdef INDI_DEBUG
411 IDLog("selectAPSlewRate: Setting slew rate to 600x.\n");
412 #endif
413 if ( (error_type = tty_write_string(fd, "#:RS0#", &nbytes_write)) != TTY_OK)
414 return error_type;
415 break;
416
417 default:
418 return -1;
419 break;
420 }
421 return 0;
422}
423int selectAPTrackingMode(int fd, int trackMode)
424{
425 int error_type;
426 int nbytes_write=0;
427
428 switch (trackMode)
429 {
430 /* Lunar */
431 case 0:
432 #ifdef INDI_DEBUG
433 IDLog("selectAPTrackingMode: Setting tracking mode to lunar.\n");
434 #endif
435 if ( (error_type = tty_write_string(fd, "#:RT0#", &nbytes_write)) != TTY_OK)
436 return error_type;
437 break;
438
439 /* Solar */
440 case 1:
441 #ifdef INDI_DEBUG
442 IDLog("selectAPTrackingMode: Setting tracking mode to solar.\n");
443 #endif
444 if ( (error_type = tty_write_string(fd, "#:RT1#", &nbytes_write)) != TTY_OK)
445 return error_type;
446 break;
447
448 /* Sidereal */
449 case 2:
450 #ifdef INDI_DEBUG
451 IDLog("selectAPTrackingMode: Setting tracking mode to sidereal.\n");
452 #endif
453 if ( (error_type = tty_write_string(fd, "#:RT2#", &nbytes_write)) != TTY_OK)
454 return error_type;
455 break;
456
457 /* Zero */
458 case 3:
459 #ifdef INDI_DEBUG
460 IDLog("selectAPTrackingMode: Setting tracking mode to zero.\n");
461 #endif
462 if ( (error_type = tty_write_string(fd, "#:RT9#", &nbytes_write)) != TTY_OK)
463 return error_type;
464 break;
465
466 default:
467 return -1;
468 break;
469 }
470 return 0;
471}
472int swapAPButtons(int fd, int currentSwap)
473{
474 int error_type;
475 int nbytes_write=0;
476
477 switch (currentSwap)
478 {
479 case 0:
480 #ifdef INDI_DEBUG
481 IDLog("#:NS#\n");
482 #endif
483 if ( (error_type = tty_write_string(fd, "#:NS#", &nbytes_write)) != TTY_OK)
484 return error_type;
485 break;
486
487 case 1:
488 #ifdef INDI_DEBUG
489 IDLog("#:EW#\n");
490 #endif
491 if ( (error_type = tty_write_string(fd, "#:EW#", &nbytes_write)) != TTY_OK)
492 return error_type;
493 break;
494
495 default:
496 return -1;
497 break;
498 }
499 return 0;
500}
501int setAPObjectRA(int fd, double ra)
502{
503/*ToDo AP accepts "#:Sr %02d:%02d:%02d.%1d#"*/
504 int h, m, s;
505 char temp_string[16];
506
507 getSexComponents(ra, &h, &m, &s);
508
509 snprintf(temp_string, sizeof( temp_string ), "#:Sr %02d:%02d:%02d#", h, m, s);
510
511 #ifdef INDI_DEBUG
512 IDLog("setAPObjectRA: Set Object RA String %s, %f\n", temp_string, ra);
513 #endif
514 return (setStandardProcedure(fd, temp_string));
515}
516
517int setAPObjectDEC(int fd, double dec)
518{
519 int d, m, s;
520 char temp_string[16];
521
522 getSexComponents(dec, &d, &m, &s);
523 /* case with negative zero */
524 if (!d && dec < 0)
525 {
526 snprintf(temp_string, sizeof( temp_string ), "#:Sd -%02d*%02d:%02d#", d, m, s);
527 }
528 else
529 {
530 snprintf(temp_string, sizeof( temp_string ), "#:Sd %+03d*%02d:%02d#", d, m, s);
531 }
532 #ifdef INDI_DEBUG
533 IDLog("setAPObjectDEC: Set Object DEC String %s\n", temp_string) ;
534 #endif
535 return (setStandardProcedure(fd, temp_string));
536}
537int setAPSiteLongitude(int fd, double Long)
538{
539 int d, m, s;
540 char temp_string[32];
541
542 getSexComponents(Long, &d, &m, &s);
543 snprintf(temp_string, sizeof( temp_string ), "#:Sg %03d*%02d:%02d#", d, m, s);
544 return (setStandardProcedure(fd, temp_string));
545}
546
547int setAPSiteLatitude(int fd, double Lat)
548{
549 int d, m, s;
550 char temp_string[32];
551
552 getSexComponents(Lat, &d, &m, &s);
553 snprintf(temp_string, sizeof( temp_string ), "#:St %+03d*%02d:%02d#", d, m, s);
554 return (setStandardProcedure(fd, temp_string));
555}
Note: See TracBrowser for help on using the repository browser.