-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexstar.c
796 lines (682 loc) · 17.9 KB
/
nexstar.c
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*+--------------------------------------------------------------+
| SYSTEM INCLUDES |
+--------------------------------------------------------------+*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringSerial.h>
/*+--------------------------------------------------------------+
| SPECIFIC INCLUDES |
+--------------------------------------------------------------+*/
#include "debug.h" //file where all debug const are defined
#include "const.h"
#include "automate.h"
#include "nexstar.h"
#include "stellarium.h"
#include "screen.h"
/*+--------------------------------------------------------------+
| LOCAL CONSTANTS |
+--------------------------------------------------------------+*/
#define DEBUG 0
#define DEVICE "/dev/ttyUSB0"
#define UART_NEXSTAR_SPEED 9600
#define BUFFER_SIZE 40
#define TIMEOUT 10
#define NEXSTAR_CMD_ECHO "KA" //Echo : useful to check communication | cmd : “K” & chr(x) | reply : chr(x) & “#”
#define NEXSTAR_ECHO 'A'
#define BINTOASCII(x) (((x)<10)?('0'+(x)):('A'+(x)-10))
#define ASCIITOBIN(x) (((x)<'A')?((x)-'0'):((x)-'A'+10))
/*+--------------------------------------------------------------+
| GLOBAL VARIABLES |
+--------------------------------------------------------------+*/
/*+--------------------------------------------------------------+
| LOCAL VARIABLES |
+--------------------------------------------------------------+*/
static int fd ; //uart file descriptor
static char buffer[BUFFER_SIZE] ;
static int flag_data_received ;
void nexstar_init()
{
flag_data_received = FALSE ;
trace("nexstar_server| starting...") ;
{
char tmp_str[80] ;
sprintf(tmp_str, "nexstar_init| try to open uart on %s...", DEVICE) ;
trace(tmp_str) ;
}
fd = serialOpen(DEVICE, UART_NEXSTAR_SPEED ) ;
if (fd == -1)
{
perror("nexstar_init|serialOpen");
}
else
{ // serial port is open
#if DEBUG
{
char tmp_str[80] ;
sprintf(tmp_str,"nextar_init| uart open on %s, uart_fd: %d", DEVICE, fd) ;
trace(tmp_str) ;
}
#endif
serialFlush(fd) ;
} // serial port is open
}
/*+--------------------------------------------------------------
| Function name : nexstar_listen
| Parameters :
| Description : wait for nextar telescope answer upto the '#' character
| Description :
* Return : 0 if nothing has been received and timeout occurs
* : 1 if only the '#' character has been received
* : x (x>1) the number of received characters including '#'
*--------------------------------------------------------------
*/
int nexstar_listen()
{
int ret = 0 ;
int timeout = TIMEOUT ;
int i_buffer = 0 ;
do
{ // waiting for answer coming from telescope
int data_length = serialDataAvail(fd) ;
if (data_length > 0 )
{ // some data are available
char car ;
do
{
car = serialGetchar(fd) ;
if (car == -1) break ;
buffer[i_buffer++] = car ;
if (i_buffer > BUFFER_SIZE)
{ // to protect against buffer overflow
#if DEBUG
printf("nextar_listen|ERROR Buffer overflow !\n") ;
#endif
return 0 ;
} // to protect against buffer overflow
}
while (--data_length) ;
if (car == '#')
{ // last received car is "#"
buffer[i_buffer] = '\0' ;
ret = i_buffer ;
#if DEBUG
// printf("nexstar_listen|%s\n", buffer) ;
#endif
break ;
} // last received car is "#"
} // some data are available
//sleep(1) ;
delay(100) ;
} // waiting for answer coming from telescope
while (timeout--) ;
return ret ;
}
/*+--------------------------------------------------------------+
| Function name : nexstar_send_to_telescope
| Parameters : none
| Description :
| Description :
+--------------------------------------------------------------+*/
void nexstar_send_to_telescope(char* cmd)
{
#if DEBUG
//printf("nexstar_send| %s\n", cmd) ;
#endif
if (fd != -1)
{
serialPrintf (fd, "%s", cmd) ;
}
}
/*
--------------------------------------------------------------
* Function name : nexstar_get_precise_ra_dec
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
int nexstar_get_precise_ra_dec(int* p_ra, int* p_dec)
{
int ra, dec ;
nexstar_send_to_telescope("e") ;
if (nexstar_listen() == 18)
{
#if DEBUG
//printf("nexstar_get_precise_ra_dec|%s\n", buffer) ;
#endif
ra = 0 ;
dec = 0 ;
int i ;
for (i=0; i<6; i++)
{
ra += (unsigned int) ASCIITOBIN( buffer[i] );
ra <<= 4 ;
}
ra <<= 4 ;
for (i=9; i<15; i++)
{
dec += (unsigned int) ASCIITOBIN(buffer[i]);
dec <<= 4 ;
}
dec <<= 4 ;
#if DEBUG
//printf("nexstar_get_precise_ra_dec|ra: %08X\n", ra) ;
//printf("nexstar_get_precise_ra_dec|ra: %08X\n", dec) ;
//printf("nexstar_get_precise_ra_dec|ra: %d, dec: %d \n", ra, dec) ;
#endif
}
*p_ra = ra ;
*p_dec = dec ;
return 1 ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_goto_precise_ra_dec
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_goto_precise_ra_dec(int ra, int dec)
{
char cmd[20] ;
int i ;
#if DEBUG
printf("nexstar_goto_precise_ra_dec|ra: %d, dec:%d\n", ra, dec) ;
#endif
cmd[0] = 'r' ;
ra &= 0xFFFFFF00 ;
for (i=8; i>0; i--)
{
cmd[i] = BINTOASCII((unsigned int) (ra & 0x0000000F)) ;
ra >>= 4 ;
}
cmd[9] = ',' ;
dec &= 0xFFFFFF00 ;
for (i=17; i>9; i--)
{
cmd[i] = BINTOASCII((unsigned int) (dec & 0x0000000F)) ;
dec >>= 4 ;
}
cmd[18] = '\0' ;
//printf("cmd: %s\n", cmd) ;
nexstar_send_to_telescope((char*)cmd) ;
// wait for the ack car '#'
if (nexstar_listen() == 1)
{ // ack car '#'
#if DEBUG
printf("nexstar_goto_precise_ra_dec|ACK received\n") ;
#endif
} // ack car '#'
}
/*
--------------------------------------------------------------
* Function name : nexstar_sync_precise_ra_dec
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_sync_precise_ra_dec(long ra, long dec)
{
// TOTO make the cmd
nexstar_send_to_telescope("s xxxx") ;
// wait for the ack car '#'
if (nexstar_listen() == 1)
{ // ack car '#'
#if DEBUG
printf("nexstar_sync_precise_ra_dec|ACK received\n") ;
#endif
} // ack car '#'
}
/*
--------------------------------------------------------------
* Function name : nexstar_get_tracking_mode
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
int nexstar_get_tracking_mode()
{
int mode = 1 ;
// TOTO make the cmd
nexstar_send_to_telescope("t") ;
// wait for the ack car '#'
if (nexstar_listen() == 2)
{ // ack car '#'
#if DEBUG
printf("nexstar_get_tracking_mode|tracking: %x\n", buffer[0]) ;
#endif
} // ack car '#'
return mode ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_set_tracking_mode
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_set_tracking_mode(int mode)
{
char cmd[3] ;
cmd[0] = 'T' ;
cmd[1] = '0' ;
nexstar_send_to_telescope(cmd) ;
// wait for the ack car '#'
if (nexstar_listen() == 1)
{ // ack car '#'
#if DEBUG
printf("nexstar_set_tracking_mode|ACK received\n") ;
#endif
} // ack car '#'
}
/*
--------------------------------------------------------------
* Function name : nexstar_get_location
* Parameters : none
* Description :
* Description :
*
* The format of the location commands is: ABCDEFGH, where:
* A is the number of degrees of latitude.
* B is the number of minutes of latitude.
* C is the number of seconds of latitude.
* D is 0 for north and 1 for south.
* E is the number of degrees of longitude.
* F is the number of minutes of longitude.
* G is the number of seconds of longitude.
* H is 0 for east and 1 for west.
*
*
*
*
* 21 32 29 0 76 14 11 1 23
--------------------------------------------------------------
*/
int nexstar_get_location()
{
int ret = FALSE ;
nexstar_send_to_telescope("w") ;
if (nexstar_listen() == 9)
{ // ack car '#'
#if DEBUG
printf("nexstar_get_location|lattitude: %02d° %02d' %02d\" %c\n", buffer[0], buffer[1], buffer[2], (buffer[3]?'S':'N')) ;
printf("nexstar_get_location|longitude: %02d° %02d' %02d\" %c\n", buffer[4], buffer[5], buffer[6], (buffer[7]?'W':'E')) ;
#endif
ret = TRUE ;
#if DEBUG
printf("nexstar_get_location|ACK received\n") ;
#endif
} // ack car '#'
return ret ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_set_location
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_set_location(int lad, int lam, int las, int ns, int lod, int lom, int los, int ew)
{
char cmd[10] ;
#ifdef DEBUG
//printf("nexstar_set_location|lattitude: %02d° %02d' %02d\" %c\n",lad, lam, las, (ns?'S':'N')) ;
//printf("nexstar_set_location|longitude: %02d° %02d' %02d\" %c\n",lod, lom, los, (ew?'W':'E')) ;
#endif
cmd[0] = 'W' ;
cmd[1] = lad ;
cmd[2] = lam ;
cmd[3] = las ;
cmd[4] = ns ;
cmd[5] = lod ;
cmd[6] = lom ;
cmd[7] = los ;
cmd[8] = ew ;
int i ;
for (i=0; i<9; i++) serialPutchar(fd, cmd[i]) ;
#if DEBUG3
printf("nexstar_set_location|") ;
for (i=0; i<9; i++)
{
printf(" %02x ", cmd[i]);
}
printf("\n");
#endif
// wait for the ack car '#'
if (nexstar_listen() == 1)
{ // ack car '#'
#if DEBUG
// printf("nexstar_set_location|ACK received\n") ;
#endif
} // ack car '#'
}
/*
--------------------------------------------------------------
* Function name : nexstar_get_time
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
int nexstar_get_time()
{
int ret = FALSE ;
nexstar_send_to_telescope("h") ;
if (nexstar_listen() == 9)
{ // ack car '#'
#if DEBUG
printf("nexstar_get_time|%02d/%02d/20%02d %02d:%02d:%02d GMT+%d %s\n", buffer[4], buffer[3], buffer[5], buffer[0], buffer[1], buffer[2], buffer[6], (buffer[7]?"Daylight Savings":"Standard Time")) ;
#endif
} // ack car '#'
return ret ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_set_time
* Parameters : none
* Description :
* Description :
*
* The format of the time commands is: QRSTUVWX, where:
* Q is the hour (24 hour clock).
* R is the minutes.
* S is the seconds.
* T is the month.
* U is the day.
* V is the year (century assumed as 20).
* W is the offset from GMT for the time zone. Note: if zone is negative, use 256-zone.
* X is 1 to enable Daylight Savings and 0 for Standard Time.
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_set_time(int day, int month, int year, int hour, int minute, int second)
{
char cmd[10] ;
#ifdef DEBUG
//printf("nexstar_set_time|%02d/%02d/%02d %02d:%02d:%02d\n",day, month, year, hour, minute, second) ;
#endif
cmd[0] = 'H' ;
cmd[1] = hour ;
cmd[2] = minute ;
cmd[3] = second ;
cmd[4] = month ;
cmd[5] = day ;
cmd[6] = year ;
cmd[7] = 0 ;
cmd[8] = 0 ;
int i ;
for (i=0; i<9; i++) serialPutchar(fd, cmd[i]) ;
#if DEBUG3
printf("nexstar_set_time|") ;
for (i=0; i<9; i++)
{
printf(" %02x ", cmd[i]);
}
printf("\n");
#endif
// wait for the ack car '#'
if (nexstar_listen() == 1)
{ // ack car '#'
#if DEBUG
printf("nexstar_set_time|ACK received\n") ;
#endif
} // ack car '#'
}
/*
--------------------------------------------------------------
* Function name : nexstar_get_version
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_get_version()
{
nexstar_send_to_telescope("V") ;
if (nexstar_listen() == 3)
{ // nexstar version received
#if DEBUG
printf("nexstar_get_version| %d.%d\n",buffer[0],buffer[1]) ;
#endif
} // nexstar version received
}
/*
--------------------------------------------------------------
* Function name : nexstar_get_device_version
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_get_device_version(int device)
{
// TOTO make the cmd
nexstar_send_to_telescope("V xxxx") ;
// wait for the ack car '#'
if (nexstar_listen() == 3)
{ // version received
#if DEBUG
printf("nexstar_get_device_version|ACK received\n") ;
#endif
} // version received
}
/*
--------------------------------------------------------------
* Function name : nexstar_get_model
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
int nexstar_get_model()
{
int ret ;
// TOTO make the cmd
nexstar_send_to_telescope("m") ;
if (nexstar_listen() == 2)
{ //
ret = buffer[0] ;
#if DEBUG
printf("nexstar_get_model| %d\n", ret) ;
#endif
} //
return ret ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_echo
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
int nexstar_echo()
{
int ret = FALSE ;
nexstar_send_to_telescope("Kz") ;
// wait for the ack car '#'
if (nexstar_listen() == 2)
{ // echo response received
if (buffer[0] == 'z')
{
ret = TRUE ;
screen_telescope_status("OK") ;
#if DEBUG
printf("nexstar_echo|ACK received\n") ;
#endif
}
} // echo response received
else
{
screen_telescope_status("--") ;
}
return ret ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_is_alignment_complete
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
int nexstar_is_alignment_complete()
{
int ret ;
nexstar_send_to_telescope("J") ;
if (nexstar_listen() == 2)
{ //
#if DEBUG
// printf("nexstar_is_alignment_complete|%s\n",buffer[0]?"Yes":"No") ;
#endif
ret = buffer[0] ;
} //
return ret ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_is_goto_in_progress
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
int nexstar_is_goto_in_progress()
{
int ret = FALSE ;
nexstar_send_to_telescope("L") ;
if (nexstar_listen() == 2)
{ // ack car '#'
#if DEBUG
printf("nexstar_is_goto_in_progress|ACK received\n") ;
#endif
} // ack car '#'
return ret ;
}
/*
--------------------------------------------------------------
* Function name : nexstar_cancel_goto
* Parameters : none
* Description :
* Description :
*
*
*
*
*
*
*
--------------------------------------------------------------
*/
void nexstar_cancel_goto()
{
nexstar_send_to_telescope("M") ;
if (nexstar_listen() == 1)
{ // ack car '#'
#if DEBUG
printf("nexstar_cancel_goto|ACK received\n") ;
#endif
} // ack car '#'
}