-
Notifications
You must be signed in to change notification settings - Fork 2
/
sport.c
471 lines (451 loc) · 19.1 KB
/
sport.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
#include <gtk/gtk.h>
#include <sys/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include <sched.h>
#include <fcntl.h>
#include <errno.h>
#include "d1cons.h"
#include "d1proto.h"
#include "d1typ.h"
#include "d1glob.h"
void azel(double az, double el)
// command antenna movement
{
int n, ix, iy, midxr, ixe, yr, da, hr, mn, sc;
static int kk;
double azz, ell, ra, dec, x, y;
GdkColor color;
char str[80], recv[256], txt[80];
azz = ell = 0;
d1.slew = 0;
ix = midx * 1.55;
ixe = midx * 0.25;
midxr = midx * 2 - ix;
if (d1.lat >= 0.0)
sprintf(txt, "%s %4.1fN %5.1fW", d1.statnam, d1.lat * 180.0 / PI, d1.lon * 180.0 / PI);
else
sprintf(txt, "%s %4.1fS %5.1fW", d1.statnam, -d1.lat * 180.0 / PI, d1.lon * 180.0 / PI);
iy = midy * 0.05;
if (d1.displ) {
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ix,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
}
sprintf(txt, "cmd %5.1f %4.1f deg", az, el);
iy = midy * 0.15;
if (d1.displ)
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
else if (d1.debug) {
toyrday(d1.secs, &yr, &da, &hr, &mn, &sc);
printf("%4d:%03d:%02d:%02d:%02d %3s ", yr, da, hr, mn, sc, d1.timsource);
printf("%s\n", txt);
}
sprintf(txt, "offsets %5.1f %4.1f deg", d1.azoff, d1.eloff);
iy = midy * 0.25;
if (d1.displ)
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
azz = az;
ell = el;
if (!d1.azelsim) {
d1.comerr = rot2(&azz, &ell, 1, recv); // initial read return if antenna at correct position
if (d1.comerr == -1) {
printf("can't talk to antenna controller\n");
return;
}
} else {
azz = d1.azprev;
ell = d1.elprev;
}
d1.aznow = azz;
d1.elnow = ell;
if (d1.debug)
printf("aznow_after_read %3.0f elnow %3.0f\n", d1.aznow, d1.elnow);
if ((fabs(d1.aznow - d1.azcmd) > 0.5 || fabs(d1.elnow - d1.elcmd) > 0.5) // was 1 should get within 0.5 deg
&& (az >= d1.azlim1 && az < d1.azlim2 && el >= d1.ellim1 && el < d1.ellim2)) {
azz = az;
ell = el;
if (!d1.azelsim) {
d1.comerr = rot2(&azz, &ell, 2, recv); // command move
d1.comerr = rot2(&azz, &ell, 1, recv); // read
if (d1.comerr == -1) {
printf("Can't talk to antenna controller\n");
return;
}
} else {
azz = (d1.azcmd + d1.azprev) * 0.5;
ell = (d1.elcmd + d1.elprev) * 0.5;
if (fabs(d1.aznow - d1.azcmd) < 2)
azz = d1.azcmd;
if (fabs(d1.elnow - d1.elcmd) < 2)
ell = d1.elcmd;
}
d1.azprev = d1.aznow = azz;
d1.elprev = d1.elnow = ell;
if (d1.debug)
printf("aznow_after_cmd %3.0f elnow %3.0f cmd %3.0f %3.0f %3.0f %3.0f\n", d1.aznow, d1.elnow,
d1.azcmd, d1.elcmd, az, el);
}
if (d1.azelsim)
sprintf(str, "antenna drive status:");
else
sprintf(str, "antenna drive simulated:");
if (d1.comerr > 0) {
sprintf(txt, " comerr= %d", d1.comerr);
iy = midy * 0.1;
if (d1.displ) {
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ixe,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ixe, iy, txt, strlen(txt));
}
}
if (az < d1.azlim1 || az > d1.azlim2 || el < d1.ellim1 || el > d1.ellim2) {
sprintf(txt, "cmd out of limits");
printf("cmd out of limits\n");
iy = midy * 0.10;
if (d1.displ) {
color.red = 0xffff;
color.green = color.blue = 0;
gdk_color_parse("red", &color);
gtk_widget_modify_fg(drawing_area, GTK_STATE_NORMAL, &color);
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ix,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->fg_gc[GTK_STATE_NORMAL], ix,
iy, txt, strlen(txt));
}
d1.track = 0;
if (d1.printout) {
toyrday(d1.secs, &yr, &da, &hr, &mn, &sc);
printf("%4d:%03d:%02d:%02d:%02d %3s ", yr, da, hr, mn, sc, d1.timsource);
printf("cmd out of limits az %f el %f\n", az, el);
}
sprintf(d1.recnote, "* cmd out of limits az %f el %f\n", az, el);
outfile(d1.recnote);
if (d1.stow != -1) {
d1.stow = 1;
if (d1.stowatlim) {
d1.elcmd = d1.ellim1;
d1.azcmd = d1.azlim1;
} else {
d1.elcmd = d1.stowel;
d1.azcmd = d1.stowaz;
}
}
return;
}
if ((fabs(d1.aznow - d1.azcmd) > 1.0 || fabs(d1.elnow - d1.elcmd) > 1.0) && d1.track != -1) {
if ((fabs(d1.aznow - d1.azcmd) > 1.0 || fabs(d1.elnow - d1.elcmd) > 1.0) && d1.stow != -1) {
d1.slew = 1;
sprintf(txt, "ant slewing");
if (d1.printout) {
toyrday(d1.secs, &yr, &da, &hr, &mn, &sc);
printf("%4d:%03d:%02d:%02d:%02d %3s ", yr, da, hr, mn, sc, d1.timsource);
if (soutrack[0]) {
printf("ant slewing to %s\n", soutrack);
}
if (d1.stow == 1) {
printf("ant slewing to stow\n");
}
}
iy = midy * 0.1;
if (d1.displ) {
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc,
TRUE, ix, iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
}
}
if (d1.displ) {
x = d1.azcmd * midx / 180.0;
if (d1.south == 0)
x -= midx;
if (x < 0)
x += midx * 2;
if (x > midx * 2)
x -= midx * 2;
y = midy * 2 - d1.elcmd * midy * 2.0 / 180.0;
color.red = 0;
color.green = 0;
color.blue = 0xffff;
gdk_color_parse("blue", &color);
gtk_widget_modify_fg(drawing_area, GTK_STATE_NORMAL, &color);
gdk_draw_line(pixmap, drawing_area->style->fg_gc[GTK_STATE_NORMAL], x - 4, y, x + 4, y);
gdk_draw_line(pixmap, drawing_area->style->fg_gc[GTK_STATE_NORMAL], x, y - 4, x, y + 4);
}
n = 0;
kk = 0;
while (kk < 100 && n == 0) {
azz = d1.aznow;
ell = d1.elnow;
if (!d1.azelsim) {
d1.comerr = rot2(&azz, &ell, 1, recv);
if (d1.comerr == -1) {
printf("can't talk2 to antenna controller\n");
return;
}
} else {
azz = (d1.azcmd + d1.azprev) * 0.5;
ell = (d1.elcmd + d1.elprev) * 0.5;
if (fabs(d1.aznow - d1.azcmd) < 2)
azz = d1.azcmd;
if (fabs(d1.elnow - d1.elcmd) < 2)
ell = d1.elcmd;
}
d1.azprev = d1.aznow = azz;
d1.elprev = d1.elnow = ell;
if (d1.printout)
printf("aznow %3.0f elnow %3.0f k %d\n", d1.aznow, d1.elnow, kk);
if (fabs(d1.aznow - d1.azcmd) > 1.0 || fabs(d1.elnow - d1.elcmd) > 1.0) {
if (d1.printout)
printf("waiting on antenna cmd %3.0f %3.0f now %3.0f %3.0f kk %d\n", d1.azcmd, d1.elcmd,
d1.aznow, d1.elnow, kk);
sprintf(txt, "waiting on antenna %d ", kk);
d1.slew = 1;
if (d1.displ) {
ix = midx * 1.55;
midxr = midx * 2 - ix;
// cleararea();
x = d1.azcmd * midx / 180.0;
if (d1.south == 0)
x -= midx;
if (x < 0)
x += midx * 2;
if (x > midx * 2)
x -= midx * 2;
y = midy * 2 - d1.elcmd * midy * 2.0 / 180.0;
color.red = 0;
color.green = 0;
color.blue = 0xffff;
gdk_color_parse("blue", &color);
gtk_widget_modify_fg(drawing_area, GTK_STATE_NORMAL, &color);
gdk_draw_line(pixmap, drawing_area->style->fg_gc[GTK_STATE_NORMAL], x - 4, y, x + 4, y);
gdk_draw_line(pixmap, drawing_area->style->fg_gc[GTK_STATE_NORMAL], x, y - 4, x, y + 4);
iy = midy * 0.1;
// gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ix,
// iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ix,
iy - midy * 0.04, midxr, midy * 0.25);
gdk_draw_text(pixmap, fixed_font,
drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
sprintf(txt, "cmd %5.1f %4.1f deg", d1.azcmd, d1.elcmd);
iy = midy * 0.15;
gdk_draw_text(pixmap, fixed_font,
drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
sprintf(txt, "azel %5.1f %4.1f deg", d1.aznow, d1.elnow);
iy = midy * 0.20;
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy,
txt, strlen(txt));
sprintf(txt, "offsets %5.1f %4.1f deg", d1.azoff, d1.eloff);
iy = midy * 0.25;
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy,
txt, strlen(txt));
azel_to_radec(d1.secs, d1.aznow, d1.elnow, &ra, &dec);
sprintf(txt, "ra %5.1f hr %4.1f deg", ra * 12.0 / PI, dec * 180.0 / PI);
iy = midy * 0.30;
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy,
txt, strlen(txt));
color.green = color.red = color.blue = 0xffff;
gtk_widget_modify_bg(button_stow, GTK_STATE_NORMAL, &color);
// if (!d1.plot) {
Repaint();
// }
// cleararea();
while (gtk_events_pending() || d1.stopproc) {
gtk_main_iteration();
// d1.plot = 0;
}
} else if (d1.debug)
printf("%s\n", txt);
sleep(1);
} else
n = 1;
kk++;
}
if (d1.debug)
printf("recv %s\n", recv);
if (d1.comerr) {
sprintf(txt, "comerr %d", d1.comerr);
iy = midy * 0.1;
if (d1.displ) {
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ixe,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ixe, iy, txt, strlen(txt));
}
if (d1.mainten == 0)
d1.stow = 1;
return;
}
sprintf(txt, "recv %s", recv);
iy = midy * 0.05;
if (d1.displ) {
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE,
ix, iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt) - 1);
}
if (fabs(d1.aznow - d1.azcmd) > 1.0 || fabs(d1.elnow - d1.elcmd) > 1.0) {
// printf("lost count\n");
sprintf(txt, "lost count goto Stow");
d1.limiterr = 1;
soutrack[0] = 0;
iy = midy * 0.1;
if (d1.displ) {
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ixe,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ixe, iy, txt, strlen(txt));
sprintf(txt, "ERROR: stopped at %3.0f %2.0f", d1.aznow, d1.elnow);
iy = midy * 0.15;
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ixe,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ixe, iy, txt, strlen(txt));
sprintf(txt, "while slewing");
iy = midy * 0.20;
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ixe,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ixe, iy, txt, strlen(txt));
sprintf(txt, "motor stalled or limit prematurely reached");
iy = midy * 0.25;
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ixe,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ixe, iy, txt, strlen(txt));
}
if (d1.mainten == 0) {
if (d1.azcmd < d1.azlim1 + 1 || d1.elcmd < d1.ellim1 + 1) // could hit limit at source set
{
d1.elnow = d1.ellim1;
}
d1.stow = 1;
d1.azcmd = d1.azlim1;
d1.elcmd = d1.ellim1;
}
return;
}
if (d1.comerr == -1) {
printf("timeout from antenna\n");
sprintf(txt, "timeout from antenna");
iy = midy * 0.1;
if (d1.displ) {
gdk_draw_rectangle(pixmap, drawing_area->style->white_gc, TRUE, ix,
iy - midy * 0.04, midxr, midy * 0.05);
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
}
}
if (d1.comerr == 0) { // normal response
}
if (d1.slew)
sleep(1);
}
if (d1.track != -1) {
if (d1.slew == 1)
d1.track = 0;
else
d1.track = 1;
}
x = (int) (d1.aznow * 640.0 / 360.0);
if (d1.south == 0) {
x -= 320;
if (x < 0)
x += 640;
}
if ((fabs(d1.aznow - d1.azlim1) < 1e-6 && fabs(d1.elnow - d1.ellim1) < 1e-6 && d1.stowatlim) ||
(fabs(d1.aznow - d1.stowaz) < 1e-6 && fabs(d1.elnow - d1.stowel) < 1e-6 && !d1.stowatlim)) {
if (d1.displ) {
color.green = 0xffff;
color.red = color.blue = 0;
gdk_color_parse("green", &color);
gtk_widget_modify_bg(button_stow, GTK_STATE_NORMAL, &color);
gtk_tooltips_set_tip(tooltips, button_stow, "antenna at stow", NULL);
gtk_tooltips_set_tip(tooltips, button_exit, "click to exit program", NULL);
// printf("in green\n");
}
d1.stow = -1; // at stow
} else {
if (d1.displ) {
color.green = color.red = color.blue = 0xffff;
gtk_widget_modify_bg(button_stow, GTK_STATE_NORMAL, &color);
gtk_tooltips_set_tip(tooltips, button_stow, "click to stow antenna", NULL);
if (d1.track != -1)
gtk_tooltips_set_tip(tooltips, button_exit, "go to stow first", NULL);
}
if (d1.stow == -1)
d1.stow = 0;
}
if (d1.stow != 0) {
d1.track = 0;
}
if (d1.displ) {
sprintf(txt, "azel %5.1f %4.1f deg", d1.aznow, d1.elnow);
iy = midy * 0.20;
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
azel_to_radec(d1.secs, d1.aznow, d1.elnow, &ra, &dec);
sprintf(txt, "ra %5.1f hr %4.1f deg", ra * 12.0 / PI, dec * 180.0 / PI);
iy = midy * 0.30;
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
sprintf(txt, "cmd %5.1f %4.1f deg", d1.azcmd, d1.elcmd);
iy = midy * 0.15;
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
sprintf(txt, "offsets %5.1f %4.1f deg", d1.azoff, d1.eloff);
iy = midy * 0.25;
gdk_draw_text(pixmap, fixed_font, drawing_area->style->black_gc, ix, iy, txt, strlen(txt));
} else if (d1.debug) {
toyrday(d1.secs, &yr, &da, &hr, &mn, &sc);
printf("%4d:%03d:%02d:%02d:%02d %3s ", yr, da, hr, mn, sc, d1.timsource);
printf("now %s tant %5.1f Source: %s\n", txt, d1.tant, soutrack);
}
return;
}
int rot2(double *az, double *el, int cmd, char *resp)
{
int usbdev, status, i, rstatus;
double azz, ell;
char command[13];
if (d1.stowatlim) {
azz = *az - d1.azlim1;
ell = *el - d1.ellim1;
} // allows controller to be set to az=0 el=0 at stow
else {
azz = *az;
ell = *el;
} // if controller is set to read correct az and el
// for perm add to dialout group
i = rstatus = 0;
if (cmd == -1) {
system("stty -F /dev/ttyUSB0 600 raw -echo -icanon min 0 time 20"); // needed to make timeout work
// system("stty -F /dev/ttyUSB0 600 raw -echo -icanon min 12 time 20"); // reads min of 12 chars 10 = up 10x100ms to between chars BUT timeout doesn't work
return 0;
}
usbdev = 0; // cmd 0x0f=stop 0x1f=status 0x2f=set
usbdev = open("/dev/ttyUSB0", O_RDWR, O_NONBLOCK);
cmd = cmd * 16 + 0xf;
sprintf(command, "W%04d%c%04d%c%c ", (int) (azz + 360.5), 1, (int) (ell + 360.5), 1, cmd); // round to nearest degree
// for(i=0;i<13;i++) printf("isend=%d ch=%2x\n",i,command[i]);
status = write(usbdev, command, 13);
if (d1.debug)
printf("write status %d cmd %x usbdev %d\n", status, cmd, usbdev);
sleep(1); // sleep needed
if (cmd != 0x1f)
sleep(1); // extra sleep needed
if (cmd == 0x1f) {
rstatus = read(usbdev, resp, 12);
if (d1.debug)
printf("read status %d\n", rstatus);
// for(i=0;i<12;i++) printf("irec=%d ch=%2x\n",i,resp[i]);
azz = resp[1] * 100 + resp[2] * 10 + resp[3] - 360;
ell = resp[6] * 100 + resp[7] * 10 + resp[8] - 360;
if (d1.stowatlim) {
*az = azz + d1.azlim1;
*el = ell + d1.ellim1;
} else {
*az = azz;
*el = ell;
}
}
close(usbdev);
if (rstatus != 12)
return -1;
else
return 0;
}