-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathgtk-rot-ctrl.c
More file actions
1786 lines (1526 loc) · 55.6 KB
/
gtk-rot-ctrl.c
File metadata and controls
1786 lines (1526 loc) · 55.6 KB
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Gpredict: Real-time satellite tracking and orbit prediction program
Copyright (C) 2001-2017 Alexandru Csete, OZ9AEC
Copyright (C) 2011 Charles Suprin, AA1VS
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 2 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, visit http://www.fsf.org/
*/
/*
* Antenna rotator control window.
*
* The master rotator control UI is implemented as a Gtk+ Widget in order
* to allow multiple instances. The widget is created from the module
* popup menu and each module can have several rotator control windows
* attached to it. Note, however, that current implementation only
* allows one rotor control window per module.
*
*/
#ifdef HAVE_CONFIG_H
#include <build-config.h>
#endif
/* NETWORK */
#ifndef WIN32
#include <arpa/inet.h> /* htons() */
#include <netdb.h> /* gethostbyname() */
#include <netinet/in.h> /* struct sockaddr_in */
#include <sys/socket.h> /* socket(), connect(), send() */
#else
#include <winsock2.h>
#endif
#include <errno.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <math.h>
#include <string.h> /* strerror() */
#include "compat.h"
#include "gpredict-utils.h"
#include "gtk-polar-plot.h"
#include "gtk-rot-knob.h"
#include "gtk-rot-ctrl.h"
#include "predict-tools.h"
#include "sat-log.h"
#define FMTSTR "%7.2f\302\260"
#define MAX_ERROR_COUNT 5
static GtkVBoxClass *parent_class = NULL;
/* Open the rotcld socket. Returns file descriptor or -1 if an error occurs */
static gint rotctld_socket_open(const gchar * host, gint port)
{
struct sockaddr_in ServAddr;
struct hostent *h;
gint sock;
gint status;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("Failed to create rotctl socket: %s"), strerror(errno));
return sock;
}
sat_log_log(SAT_LOG_LEVEL_DEBUG,
_("%s: Network socket created successfully"), __func__);
memset(&ServAddr, 0, sizeof(ServAddr));
ServAddr.sin_family = AF_INET; /* Internet address family */
h = gethostbyname(host);
if (h == NULL)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("Name resolution of rotctld server %s failed."), host);
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
return -1;
}
memcpy((char *)&ServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
ServAddr.sin_port = htons(port); /* Server port */
/* establish connection */
status = connect(sock, (struct sockaddr *)&ServAddr, sizeof(ServAddr));
if (status == -1)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("Connection to rotctld server at %s:%d failed: %s"),
host, port, strerror(errno));
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
return -1;
}
sat_log_log(SAT_LOG_LEVEL_DEBUG, _("%s: Connection opened to %s:%d"),
__func__, host, port);
return sock;
}
/* Close a rotcld socket. First send a q command to cleanly shut down rotctld */
static void rotctld_socket_close(gint * sock)
{
gint written;
/*shutdown the rotctld connect */
written = send(*sock, "q\x0a", 2, 0);
if (written != 2)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s:%s: Sent 2 bytes but sent %d."),
__FILE__, __func__, written);
}
#ifndef WIN32
shutdown(*sock, SHUT_RDWR);
close(*sock);
#else
shutdown(*sock, SD_BOTH);
closesocket(*sock);
#endif
*sock = -1;
}
/*
* Send a command to rotctld and read the response.
*
* Inputs are the socket, a string command, and a buffer and length for
* returning the output from rotctld.
*/
static gboolean rotctld_socket_rw(gint sock, gchar * buff, gchar * buffout,
gint sizeout)
{
gint written;
gint size;
size = strlen(buff);
/* send command */
written = send(sock, buff, size, 0);
if (written != size)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s: SIZE ERROR %d / %d"), __func__, written, size);
}
if (written == -1)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s: rotctld Socket Down"), __func__);
return FALSE;
}
/* try to read answer */
size = recv(sock, buffout, sizeout, 0);
if (size == -1)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s: rotctld Socket Down"), __func__);
return FALSE;
}
buffout[size] = '\0';
if (size == 0)
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s:%s: Got 0 bytes from rotctld"), __FILE__, __func__);
}
return TRUE;
}
static gint sat_name_compare(sat_t * a, sat_t * b)
{
return (gpredict_strcmp(a->nickname, b->nickname));
}
static gint rot_name_compare(const gchar * a, const gchar * b)
{
return (gpredict_strcmp(a, b));
}
static gdouble central_angle(gdouble az0, gdouble el0, gdouble az1, gdouble el1)
{
gdouble angle;
az0 = Radians(az0);
az1 = Radians(az1);
el0 = Radians(el0);
el1 = Radians(el1);
/* Spherical law of cosines, azimuth == longitude, elevation == latitude */
angle = acos(sin(el0) * sin(el1) + cos(el0) * cos(el1) * cos(az0 - az1));
return Degrees(angle);
}
static gboolean is_zenith_nearby(gdouble elevation, gdouble tolerance)
{
if (fabs(90.0 - elevation) <= tolerance * 2.0)
return TRUE;
else
return FALSE;
}
static void flip(rot_az_type_t aztype, gdouble *azimuth, gdouble *elevation)
{
*elevation = 180.0 - *elevation;
if (*azimuth > 180.0)
*azimuth -= 180.0;
else
*azimuth += 180.0;
if ((aztype == ROT_AZ_TYPE_180) && (*azimuth > 180.0))
*azimuth = *azimuth - 360.0;
}
/**
* Calculate shortest path between two azimuths, ignoring rotator end stop.
*
* \param az0 First azimuth.
* \param az1 Second azimuth.
* \return Angle between azimuths.
*/
static gdouble shortest_azimuth_path(gdouble az0, gdouble az1)
{
gdouble result = fabs(az0 - az1);
if (result > 180.0)
result = 360.0 - result;
return result;
}
/**
* Calculate angle between 2 azimuths, bypassing rotator end stop.
*
* \param pos0 First position.
* \param pos1 Second position.
* \param stoppos Rotator end stop position.
* \return Angle between positions.
*/
static gdouble stoppos_bypass_angle(gdouble pos0, gdouble pos1, gdouble stoppos)
{
gdouble pos0_to_pos1 = shortest_azimuth_path(pos0, pos1);
gdouble pos0_to_stoppos = shortest_azimuth_path(pos0, stoppos);
gdouble pos1_to_stoppos = shortest_azimuth_path(pos1, stoppos);
if (pos0 == pos1)
return 0.0;
if (pos0 == stoppos || pos1 == stoppos)
return pos0_to_pos1;
/* If end stop is in the middle of shortest path between positions */
if ((pos0_to_stoppos < pos0_to_pos1) && (pos1_to_stoppos < pos0_to_pos1))
return 360.0 - pos0_to_pos1; /* Go other way */
return pos0_to_pos1;
}
static void handle_zenith_flip(GtkRotCtrl *ctrl, gdouble rotaz, gdouble rotel,
gdouble *setaz, gdouble *setel)
{
gdouble rotangle_wout_flip, rotangle_with_flip, azstop;
/* If rotator doesn't support elevation flip */
if (ctrl->conf->maxel < 180.0)
return;
azstop = ctrl->conf->azstoppos;
/* Note: We use the sum of azimuth and elevation rotations as metric of how
* efficient the maneuver is. There are also other options to optimize
* speed or wearing: max(az_rotation, el_rotation), weighting, etc. */
/* Calculate amount of rotation without extra flip at zenith */
rotangle_wout_flip = fabs(rotel - (*setel));
rotangle_wout_flip += stoppos_bypass_angle(rotaz, *setaz, azstop);
/* Calculate amount of rotation with extra flip at zenith */
flip(ctrl->conf->aztype, setaz, setel);
rotangle_with_flip = fabs(rotel - (*setel));
rotangle_with_flip += stoppos_bypass_angle(rotaz, *setaz, azstop);
/* Go with flip if it's more efficient */
if (rotangle_with_flip < rotangle_wout_flip)
ctrl->flipped = !ctrl->flipped; /* Save new flip status */
else
flip(ctrl->conf->aztype, setaz, setel); /* Undo flip */
}
static gboolean is_flipped_pass(pass_t * pass, rot_az_type_t type,
gdouble azstoppos)
{
gdouble max_az = 0, min_az = 0, offset = 0;
gdouble caz, last_az = pass->aos_az;
guint num, i;
pass_detail_t *detail;
gboolean retval = FALSE;
num = g_slist_length(pass->details);
if (type == ROT_AZ_TYPE_360)
{
min_az = 0;
max_az = 360;
}
else if (type == ROT_AZ_TYPE_180)
{
min_az = -180;
max_az = 180;
}
/* Offset by (azstoppos-min_az) to handle
* rotators with non-default positions.
* Note that the default positions of the rotator stops
* (eg. -180 for ROT_AZ_TYPE_180, and 0 for
* ROT_AZ_TYPE_360) will create an offset of 0, which
* seems like a pretty sane default. */
offset = azstoppos - min_az;
min_az += offset;
max_az += offset;
/* Assume that min_az and max_az are atleat 360 degrees apart
get the azimuth that is in a settable range */
while (last_az > max_az)
last_az -= 360;
while (last_az < min_az)
last_az += 360;
if (num > 1)
{
for (i = 1; i < num - 1; i++)
{
detail = PASS_DETAIL(g_slist_nth_data(pass->details, i));
caz = detail->az;
while (caz > max_az)
caz -= 360;
while (caz < min_az)
caz += 360;
if (fabs(caz - last_az) > 180)
retval = TRUE;
last_az = caz;
}
}
caz = pass->los_az;
while (caz > max_az)
caz -= 360;
while (caz < min_az)
caz += 360;
if (fabs(caz - last_az) > 180)
retval = TRUE;
return retval;
}
static inline void set_flipped_pass(GtkRotCtrl * ctrl)
{
if (ctrl->conf && ctrl->pass)
ctrl->flipped = is_flipped_pass(ctrl->pass, ctrl->conf->aztype,
ctrl->conf->azstoppos);
}
/**
* Read rotator position from device.
*
* \param ctrl Pointer to the GtkRotCtrl widget.
* \param az The current Az as read from the device
* \param el The current El as read from the device
* \return TRUE if the position was successfully retrieved, FALSE if an
* error occurred.
*/
static gboolean get_pos(GtkRotCtrl * ctrl, gdouble * az, gdouble * el)
{
gchar *buff, **vbuff;
gchar buffback[128];
gboolean retcode;
if ((az == NULL) || (el == NULL))
{
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s:%d: NULL storage."), __FILE__, __LINE__);
return FALSE;
}
/* send command */
buff = g_strdup_printf("p\x0a");
retcode = rotctld_socket_rw(ctrl->client.socket, buff, buffback, 128);
/* try to parse answer */
if (retcode)
{
if (strncmp(buffback, "RPRT", 4) == 0)
{
g_strstrip(buffback);
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s:%d: rotctld returned error (%s)"),
__FILE__, __LINE__, buffback);
retcode = FALSE;
}
else
{
vbuff = g_strsplit(buffback, "\n", 3);
if ((vbuff[0] != NULL) && (vbuff[1] != NULL))
{
*az = g_strtod(vbuff[0], NULL);
*el = g_strtod(vbuff[1], NULL);
}
else
{
g_strstrip(buffback);
sat_log_log(SAT_LOG_LEVEL_ERROR,
_("%s:%d: rotctld returned bad response (%s)"),
__FILE__, __LINE__, buffback);
retcode = FALSE;
}
g_strfreev(vbuff);
}
}
g_free(buff);
return retcode;
}
/**
* Send new position to rotator device
*
* \param ctrl Pointer to the GtkRotCtrl widget
* \param az The new Azimuth
* \param el The new Elevation
* \return TRUE if the new position has been sent successfully
* FALSE if an error occurred
*
* \note The function does not perform any range check since the GtkRotKnob
* should always keep its value within range.
*/
static gboolean set_pos(GtkRotCtrl * ctrl, gdouble az, gdouble el)
{
gchar *buff;
gchar buffback[128];
gboolean retcode;
gint retval;
/* send command */
buff = g_strdup_printf("P %.2f %.2f\x0a", az, el);
retcode = rotctld_socket_rw(ctrl->client.socket, buff, buffback, 128);
g_free(buff);
if (retcode == TRUE)
{
/* treat errors as soft errors */
retval = (gint) g_strtod(buffback + 4, NULL);
if (retval != 0)
{
g_strstrip(buffback);
sat_log_log(SAT_LOG_LEVEL_ERROR,
_
("%s:%d: rotctld returned error %d with az %f el %f(%s)"),
__FILE__, __LINE__, retval, az, el, buffback);
retcode = FALSE;
}
}
return (retcode);
}
/* Rotctl client thread */
static gpointer rotctld_client_thread(gpointer data)
{
gdouble elapsed_time;
gdouble azi = 0.0;
gdouble ele = 0.0;
gboolean new_trg = FALSE;
gboolean io_error = FALSE;
GtkRotCtrl *ctrl = GTK_ROT_CTRL(data);
g_print("Starting rotctld client thread\n");
ctrl->client.socket = rotctld_socket_open(ctrl->conf->host,
ctrl->conf->port);
if (ctrl->client.socket == -1)
return GINT_TO_POINTER(-1);
ctrl->client.timer = g_timer_new();
ctrl->client.new_trg = FALSE;
ctrl->client.running = TRUE;
while (ctrl->client.running)
{
g_timer_start(ctrl->client.timer);
io_error = FALSE;
g_mutex_lock(&ctrl->client.mutex);
if (ctrl->client.new_trg)
{
azi = ctrl->client.azi_out;
ele = ctrl->client.ele_out;
new_trg = ctrl->client.new_trg;
}
g_mutex_unlock(&ctrl->client.mutex);
if (new_trg && !ctrl->monitor)
{
if (set_pos(ctrl, azi, ele))
new_trg = FALSE;
else
io_error = TRUE;
}
/* wait 100 ms before sending new command */
g_usleep(100000);
if (!get_pos(ctrl, &azi, &ele))
io_error = TRUE;
g_mutex_lock(&ctrl->client.mutex);
ctrl->client.azi_in = azi;
ctrl->client.ele_in = ele;
ctrl->client.new_trg = new_trg;
ctrl->client.io_error = io_error;
g_mutex_unlock(&ctrl->client.mutex);
/* ensure rotctl duty cycle stays below 50%, but wait at least 700 ms (TBC) */
elapsed_time = MAX(g_timer_elapsed(ctrl->client.timer, NULL), 0.7);
g_usleep(elapsed_time * 1e6);
}
g_print("Stopping rotctld client thread\n");
g_timer_destroy(ctrl->client.timer);
rotctld_socket_close(&ctrl->client.socket);
return GINT_TO_POINTER(0);
}
/**
* Update count down label.
*
* \param ctrl Pointer to the RotCtrl widget.
* \param t The current time.
*
* This function calculates the new time to AOS/LOS of the currently
* selected target and updates the ctrl->SatCnt label widget.
*/
static void update_count_down(GtkRotCtrl * ctrl, gdouble t)
{
gdouble targettime;
gdouble delta;
gchar *buff;
guint h, m, s;
/* select AOS or LOS time depending on target elevation */
if (ctrl->target->el < 0.0)
targettime = ctrl->target->aos;
else
targettime = ctrl->target->los;
delta = targettime - t;
/* convert julian date to seconds */
s = (guint) (delta * 86400);
/* extract hours */
h = (guint) floor(s / 3600);
s -= 3600 * h;
/* extract minutes */
m = (guint) floor(s / 60);
s -= 60 * m;
if (h > 0)
buff = g_strdup_printf("%02d:%02d:%02d", h, m, s);
else
buff = g_strdup_printf("%02d:%02d", m, s);
gtk_label_set_text(GTK_LABEL(ctrl->SatCnt), buff);
g_free(buff);
}
/*
* Update rotator control state.
*
* This function is called by the parent, i.e. GtkSatModule, indicating that
* the satellite data has been updated. The function updates the internal state
* of the controller and the rotator.
*/
void gtk_rot_ctrl_update(GtkRotCtrl * ctrl, gdouble t)
{
gchar *buff;
ctrl->t = t;
if (ctrl->target)
{
/* update target displays */
buff = g_strdup_printf(FMTSTR, ctrl->target->az);
gtk_label_set_text(GTK_LABEL(ctrl->AzSat), buff);
g_free(buff);
buff = g_strdup_printf(FMTSTR, ctrl->target->el);
gtk_label_set_text(GTK_LABEL(ctrl->ElSat), buff);
g_free(buff);
update_count_down(ctrl, t);
/*if the current pass is too far away */
if ((ctrl->pass != NULL))
if (qth_small_dist(ctrl->qth, ctrl->pass->qth_comp) > 1.0)
{
free_pass(ctrl->pass);
ctrl->pass = NULL;
ctrl->pass = get_pass(ctrl->target, ctrl->qth, t, 3.0);
if (ctrl->pass)
{
set_flipped_pass(ctrl);
/* update polar plot */
gtk_polar_plot_set_pass(GTK_POLAR_PLOT(ctrl->plot),
ctrl->pass);
}
}
/* update next pass if necessary */
if (ctrl->pass != NULL)
{
/* if we are not in the current pass */
if ((ctrl->pass->aos > t) || (ctrl->pass->los < t))
{
/* the pass may not have met the minimum
elevation, calculate the pass and plot it */
if (ctrl->target->el >= 0.0)
{
/* inside an unexpected/unpredicted pass */
free_pass(ctrl->pass);
ctrl->pass = NULL;
ctrl->pass = get_current_pass(ctrl->target, ctrl->qth, t);
set_flipped_pass(ctrl);
gtk_polar_plot_set_pass(GTK_POLAR_PLOT(ctrl->plot),
ctrl->pass);
}
else if ((ctrl->target->aos - ctrl->pass->aos) >
(ctrl->delay / secday / 1000 / 4.0))
{
/* the target is expected to appear in a new pass
sufficiently later after the current pass says */
/* converted milliseconds to gpredict time and took a
fraction of it as a threshold for deciding a new pass */
/* if the next pass is not the one for the target */
free_pass(ctrl->pass);
ctrl->pass = NULL;
ctrl->pass = get_pass(ctrl->target, ctrl->qth, t, 3.0);
set_flipped_pass(ctrl);
/* update polar plot */
gtk_polar_plot_set_pass(GTK_POLAR_PLOT(ctrl->plot),
ctrl->pass);
}
}
else
{
/* inside a pass and target dropped below the
horizon so look for a new pass */
if (ctrl->target->el < 0.0)
{
free_pass(ctrl->pass);
ctrl->pass = NULL;
ctrl->pass = get_pass(ctrl->target, ctrl->qth, t, 3.0);
set_flipped_pass(ctrl);
/* update polar plot */
gtk_polar_plot_set_pass(GTK_POLAR_PLOT(ctrl->plot),
ctrl->pass);
}
}
}
else
{
/* we don't have any current pass; store the current one */
if (ctrl->target->el > 0.0)
ctrl->pass = get_current_pass(ctrl->target, ctrl->qth, t);
else
ctrl->pass = get_pass(ctrl->target, ctrl->qth, t, 3.0);
set_flipped_pass(ctrl);
/* update polar plot */
gtk_polar_plot_set_pass(GTK_POLAR_PLOT(ctrl->plot), ctrl->pass);
}
}
}
/* Select a satellite. */
void gtk_rot_ctrl_select_sat(GtkRotCtrl * ctrl, gint catnum)
{
sat_t *sat;
int i, n;
/* find index in satellite list */
n = g_slist_length(ctrl->sats);
for (i = 0; i < n; i++)
{
sat = SAT(g_slist_nth_data(ctrl->sats, i));
if (sat && sat->tle.catnr == catnum)
{
/* assume the index is the same in sat selector */
gtk_combo_box_set_active(GTK_COMBO_BOX(ctrl->SatSel), i);
break;
}
}
}
/*
* Create azimuth control widgets.
*
* This function creates and initialises the widgets for controlling the
* azimuth of the the rotator.
*/
static GtkWidget *create_az_widgets(GtkRotCtrl * ctrl)
{
GtkWidget *frame;
GtkWidget *table;
GtkWidget *label;
frame = gtk_frame_new(_("Azimuth"));
table = gtk_grid_new();
gtk_container_set_border_width(GTK_CONTAINER(table), 5);
gtk_grid_set_column_spacing(GTK_GRID(table), 5);
gtk_grid_set_row_spacing(GTK_GRID(table), 5);
gtk_container_add(GTK_CONTAINER(frame), table);
ctrl->AzSet = gtk_rot_knob_new(0.0, 360.0, 180.0);
gtk_grid_attach(GTK_GRID(table), ctrl->AzSet, 0, 0, 3, 1);
label = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(label), _("Read:"));
g_object_set(label, "xalign", 1.0f, "yalign", 0.5f, NULL);
gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
ctrl->AzRead = gtk_label_new(" --- ");
g_object_set(ctrl->AzRead, "xalign", 0.0f, "yalign", 0.5f, NULL);
gtk_grid_attach(GTK_GRID(table), ctrl->AzRead, 1, 1, 1, 1);
return frame;
}
/*
* Create elevation control widgets.
*
* This function creates and initialises the widgets for controlling the
* elevation of the the rotator.
*/
static GtkWidget *create_el_widgets(GtkRotCtrl * ctrl)
{
GtkWidget *frame;
GtkWidget *table;
GtkWidget *label;
frame = gtk_frame_new(_("Elevation"));
table = gtk_grid_new();
gtk_container_set_border_width(GTK_CONTAINER(table), 5);
gtk_grid_set_column_spacing(GTK_GRID(table), 5);
gtk_grid_set_row_spacing(GTK_GRID(table), 5);
gtk_container_add(GTK_CONTAINER(frame), table);
ctrl->ElSet = gtk_rot_knob_new(0.0, 90.0, 45.0);
gtk_grid_attach(GTK_GRID(table), ctrl->ElSet, 0, 0, 3, 1);
label = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(label), _("Read: "));
g_object_set(label, "xalign", 1.0f, "yalign", 0.5f, NULL);
gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
ctrl->ElRead = gtk_label_new(" --- ");
g_object_set(ctrl->ElRead, "xalign", 0.0f, "yalign", 0.5f, NULL);
gtk_grid_attach(GTK_GRID(table), ctrl->ElRead, 1, 1, 1, 1);
return frame;
}
/**
* Manage toggle signals (tracking)
*
* \param button Pointer to the GtkToggle button.
* \param data Pointer to the GtkRotCtrl widget.
*/
static void track_toggle_cb(GtkToggleButton * button, gpointer data)
{
GtkRotCtrl *ctrl = GTK_ROT_CTRL(data);
gboolean locked;
locked = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ctrl->LockBut));
ctrl->tracking = gtk_toggle_button_get_active(button);
gtk_widget_set_sensitive(ctrl->MonitorCheckBox,
!(ctrl->tracking || locked));
gtk_widget_set_sensitive(ctrl->AzSet, !ctrl->tracking);
gtk_widget_set_sensitive(ctrl->ElSet, !ctrl->tracking);
}
/**
* Rotator controller timeout function
*
* \param data Pointer to the GtkRotCtrl widget.
* \return Always TRUE to let the timer continue.
*/
static gboolean rot_ctrl_timeout_cb(gpointer data)
{
GtkRotCtrl *ctrl = GTK_ROT_CTRL(data);
gdouble rotaz = 0.0, rotel = 0.0;
gdouble setaz = 0.0, setel = 45.0;
gchar *text;
gboolean error = FALSE;
sat_t sat_working, *sat;
/* parameters for path predictions */
gdouble time_delta;
gdouble step_size;
gboolean tol_exceeded_az, tol_exceeded_el;
gboolean tol_exceeded_central_angle;
/* If we are tracking and the target satellite is within
range, set the rotor position controller knob values to
the target values. If the target satellite is out of range
set the rotor controller to 0 deg El and to the Az where the
target sat is expected to come up or where it last went down
*/
if (ctrl->tracking && ctrl->target)
{
if (ctrl->target->el < 0.0)
{
if (ctrl->pass != NULL)
{
if (ctrl->t < ctrl->pass->aos)
{
setaz = ctrl->pass->aos_az;
setel = 0;
}
else if (ctrl->t > ctrl->pass->los)
{
setaz = ctrl->pass->los_az;
setel = 0;
}
}
}
else
{
setaz = ctrl->target->az;
setel = ctrl->target->el;
}
/* if this is a flipped pass and the rotor supports it */
if ((ctrl->flipped) && (ctrl->conf->maxel >= 180.0))
{
flip(ctrl->conf->aztype, &setaz, &setel);
while (setaz > ctrl->conf->maxaz)
setaz -= 360;
while (setaz < ctrl->conf->minaz)
setaz += 360;
}
if ((ctrl->conf->aztype == ROT_AZ_TYPE_180) && (setaz > 180.0))
setaz = setaz - 360.0;
if (!(ctrl->engaged))
{
gtk_rot_knob_set_value(GTK_ROT_KNOB(ctrl->AzSet), setaz);
gtk_rot_knob_set_value(GTK_ROT_KNOB(ctrl->ElSet), setel);
}
}
else
{
setaz = gtk_rot_knob_get_value(GTK_ROT_KNOB(ctrl->AzSet));
setel = gtk_rot_knob_get_value(GTK_ROT_KNOB(ctrl->ElSet));
}
if ((ctrl->engaged) && (ctrl->conf != NULL))
{
if (g_mutex_trylock(&ctrl->client.mutex))
{
error = ctrl->client.io_error;
rotaz = ctrl->client.azi_in;
rotel = ctrl->client.ele_in;
g_mutex_unlock(&ctrl->client.mutex);
if (error)
{
gtk_label_set_text(GTK_LABEL(ctrl->AzRead), _("ERROR"));
gtk_label_set_text(GTK_LABEL(ctrl->ElRead), _("ERROR"));
gtk_polar_plot_set_rotor_pos(GTK_POLAR_PLOT(ctrl->plot),
-10.0, -10.0);
}
else
{
/* update display widgets */
text = g_strdup_printf("%.2f\302\260", rotaz);
gtk_label_set_text(GTK_LABEL(ctrl->AzRead), text);
g_free(text);
text = g_strdup_printf("%.2f\302\260", rotel);
gtk_label_set_text(GTK_LABEL(ctrl->ElRead), text);
g_free(text);
if ((ctrl->conf->aztype == ROT_AZ_TYPE_180) && (rotaz < 0.0))
{
gtk_polar_plot_set_rotor_pos(GTK_POLAR_PLOT(ctrl->plot),
rotaz + 360.0, rotel);
}
else
{
gtk_polar_plot_set_rotor_pos(GTK_POLAR_PLOT(ctrl->plot),
rotaz, rotel);
}
}
}
/* Use separate az/el tolerance calculations for manual control */
tol_exceeded_az = fabs(setaz - rotaz) > ctrl->tolerance;
tol_exceeded_el = fabs(setel - rotel) > ctrl->tolerance;
/* Use central angle in tolerance calculations for auto tracking */
tol_exceeded_central_angle = central_angle(setaz, setel, rotaz, rotel) > ctrl->tolerance;
/* if tolerance exceeded */
if (( ctrl->tracking && tol_exceeded_central_angle) ||
(!ctrl->tracking && (tol_exceeded_az || tol_exceeded_el)))
{
if (ctrl->tracking)
{
/* if we are in a pass try to lead the satellite
some so we are not always chasing it */
if (ctrl->target && ctrl->target->el > 0.0)
{
/* starting the rotator moving while we do some computation
* can lead to errors later */
/* compute a time in the future when the position is
within tolerance so and send the rotor there.
*/
/* use a working copy so data does not get corrupted */
sat = memcpy(&(sat_working), ctrl->target, sizeof(sat_t));
/* compute az/el in the future that is past end of pass
or exceeds tolerance
*/
if (ctrl->pass)
{
/* the next point is before the end of the pass
if there is one. */
time_delta = ctrl->pass->los - ctrl->t;
}
else
{
/* otherwise look 20 minutes into the future */
time_delta = 1.0 / 72.0;
}
/* have a minimum time delta */
step_size = time_delta / 2.0;
if (step_size < ctrl->delay / 1000.0 / (secday))
{
step_size = ctrl->delay / 1000.0 / (secday);
}
/* find a time when satellite is above horizon and at the
edge of tolerance. the final step size needs to be smaller
than delay. otherwise the az/el could be further away than
tolerance the next time we enter the loop and we end up
pushing ourselves away from the satellite.