console_j2000tracker.c 15.5 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
/*
	Command generator for J2000.0 positions

4
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2011, 2013, 2014
Jeroen Vreeken's avatar
Jeroen Vreeken committed
5
	Copyright Stichting C.A. Muller Radioastronomiestation, 2008, 2011, 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

	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 3 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, see <http://www.gnu.org/licenses/>.

*/

#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include <ctype.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdbool.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
40
41
42
43
44
45
#include <libnova/transform.h>
#include <libnova/julian_day.h>
#include <libnova/utility.h>
#include <libnova/aberration.h>
#include <libnova/precession.h>
#include <libnova/refraction.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
46

47
48
#include <command/command.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
49
50
51
52
53
54
55
56
#include <utils/weather.h>
#include <utils/dt_model.h>
#include <trace/trace.h>
#include <dt_port_numbers.h>
#include <utils/command_server.h>
#include <utils/status_server.h>
#include <utils/dt_host.h>
#include <log/log.h>
57

Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#define TIME_OFFSET 2

char *command_host = "localhost";
int command_port = CONSOLE_COMMAND_PORT;
char *trace_host = "localhost";
int trace_port = CONSOLE_TRACE_PORT;
int stat_port = CONSOLE_J2000_POS_PORT;
int cmd_port = CONSOLE_J2000_CMD_PORT;
char *az_command_spg = "Azimuth_Setpoint";
char *el_command_spg = "Elevation_Setpoint";
char *az_trace_name = "Azimuth_Position";
char *el_trace_name = "Elevation_Position";

Jeroen Vreeken's avatar
Jeroen Vreeken committed
72
73
74
75
76
static bool tracking_command_send_p180 = false;
static bool tracking_command_last_az_valid = false;
static float tracking_command_last_az = 0;

bool tracking_enabled = false;
77
78
bool refraction_enable = true;
bool dt_model_enable = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
79
80
81
82
83
84
85

struct compensation_switch {
	char *name;
	bool *value;
};

struct compensation_switch compensation_switches[] = {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
86
	{ "enabled", &tracking_enabled },
Jeroen Vreeken's avatar
Jeroen Vreeken committed
87
88
89
90
91
92
93
	{ "refraction", &refraction_enable },
	{ "dt_model", &dt_model_enable },
	{ NULL, NULL }
};

struct weather *weather;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
94
95
struct trace *traceval_az;
struct trace *traceval_el;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
96
97
98
99
100
101
102
103
104
105
106
float traceval_az_deg;

struct lnh_lnlat_posn dwingeloo;
struct ln_lnlat_posn dwingeloo_pos;
struct ln_equ_posn object;
struct ln_equ_posn object_prec;
struct ln_equ_posn object_aber;
struct ln_hms track_hms;
struct ln_dms track_dms;


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Reverse of ln_get_equ_aber() */
static void get_equ_mean_from_aber(struct ln_equ_posn * position, double JD, struct ln_equ_posn * mean_position)
{
	struct ln_equ_posn mean;
	
	ln_get_equ_aber(position, JD, &mean);
	
	mean_position->ra = position->ra - (mean.ra - position->ra);
	mean_position->dec = position->dec - (mean.dec - position->dec);
	
	if (mean_position->dec > 90.0) {
		mean_position->dec = 180.0 - mean_position->dec;
		mean_position->ra -= 180.0;
	}
	while (mean_position->ra < 0.0) {
		mean_position->ra += 360.0;
	}
	while (mean_position->ra >= 360.0) {
		mean_position->ra -= 360.0;
	}
}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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


void a2coord(char *ara, char *adec, struct ln_hms *ra, struct ln_dms *dec)
{
	int i, neg;

	if (strlen(adec) && adec[0] == '-') {
		dec->neg = 1;
		i = 1;
	} else {
		dec->neg = 0;
		i = 0;
	}
	dec->degrees = 0;
	dec->minutes = 0;
	dec->seconds = 0.0;
	
	for (; isdigit(adec[i]) && i < strlen(adec); i++) {
		dec->degrees *= 10;
		dec->degrees += adec[i] - '0';
	}
	for (i++; isdigit(adec[i]) && i < strlen(adec); i++) {
		dec->minutes *= 10;
		dec->minutes += adec[i] - '0';
	}
	i++;
	if (i < strlen(adec)) {
		dec->seconds = atof(adec + i);
	}
	
	ra->hours = 0;
	ra->minutes = 0;
	ra->seconds = 0.0;
	
	if (strlen(ara) && ara[0] == '-') {
		neg = -1;
		i = 1;
	} else {
		neg = 1;
		i = 0;
	}
	for (; isdigit(ara[i]) && i < strlen(ara); i++) {
		ra->hours *= 10;
		ra->hours += (ara[i] - '0') * neg;
	}
	for (i++; isdigit(ara[i]) && i < strlen(ara); i++) {
		ra->minutes *= 10;
		ra->minutes += (ara[i] - '0') * neg;
	}
	i++;
	if (i < strlen(ara)) {
		ra->seconds = atof(ara + i) * neg;
	}
	if (neg < 0) {
		ra->hours += 24;
		ra->minutes += 60;
		ra->seconds += 60.0;
	}
}

188
int handle_cmd(char *command)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
189
190
{
	char *ara, *adec, *switches, *ptr;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
191
	double oldra, olddec;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
192
	
193
	ara = strtok_r(command, " \t", &ptr);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
194
195
196
197
	adec = strtok_r(NULL, " \t", &ptr);
	switches = strtok_r(NULL, " \t", &ptr);
	
	if (!ara || !adec)
198
		return -1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
199
200
201
202
203
204
	
	while (adec[strlen(adec)-1] == '\n' ||
	    adec[strlen(adec)-1] == '\r')
		adec[strlen(adec)-1] = 0;
	
	a2coord(ara, adec, &track_hms, &track_dms);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
205
206
	oldra = object.ra;
	olddec = object.dec;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
207
208
209
	object.ra = ln_hms_to_deg(&track_hms);
	object.dec = ln_dms_to_deg(&track_dms);
	
210
211
212
	if (oldra != object.ra || olddec != object.dec) {
		log_send(LOG_T_INFO, "New J2000 coordinates: ra=%f, dec=%f",
		    object.ra, object.dec);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
213
		tracking_command_last_az_valid = false;
214
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
215
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	if (switches) {
		char *name, *val;
		int i;
		name = strtok_r(switches, ",=", &ptr);
		val = strtok_r(NULL, ",=", &ptr);
		for (i = 0; compensation_switches[i].name; i++) {
			if (!strcmp(name, compensation_switches[i].name)) {
				if (val[0] == '1') {
					*compensation_switches[i].value = true;
				} else {
					*compensation_switches[i].value = false;
				}
			}
		}
	}
231
	return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
232
233
}

234
void output(struct status_server *stat_srv)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
235
236
237
{
	static time_t last = 0;
	time_t now;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
238
	time_t now_t = time(NULL);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
239
240
241
242
243
244
245
246
247
248
249
250
	struct ln_hrz_posn hrz;
	struct ln_date date;
	double JD;
	struct ln_equ_posn equ_pos, equ_pos_prec, equ_pos_aber;
	struct ln_hms hms;
	struct ln_dms dms;
	char statline[200];
	int ret, i;
	char *statlinepos;
	int do_output = 1;
	double pressure, temperature, alt_adj;
	
251
252
253
	if (traceval_az->value.t.tv_sec != traceval_el->value.t.tv_sec) {
		do_output = 0;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
254
	now = traceval_az->value.t.tv_sec;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
255
256
257
258
	if (now_t - now > 2) {
		printf("No trace for more than 2 seconds, using last known position\n");
		now = now_t;
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
259
	
260
	if (now <= last) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
261
		do_output = 0;
262
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
263
264
	
	if (do_output == 0) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
265
		return;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
266
267
268
269
	} else {
		last = now;
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
270
	hrz.az = traceval_az->value.value.f * 360.0 / (2*M_PI);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
271
	traceval_az_deg = hrz.az;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
272
	hrz.alt = traceval_el->value.value.f * 360.0 / (2*M_PI);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
273
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
274
	ln_get_date_from_timet(&now, &date);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
	JD = ln_get_julian_day(&date);

	if (dt_model_enable) {
		double daz, del;
				
		daz = dt_model_azimuth_delta_rev(hrz.az, hrz.alt);
		del = dt_model_elevation_delta_rev(hrz.az, hrz.alt);
		printf("rev(%f, %f): %g %g\n", hrz.az, hrz.alt, daz, del);
		hrz.az += daz;
		hrz.alt += del;
	}

	if (refraction_enable) {
		/* Get current weather */
		temperature = weather_get_temperature(weather);
		pressure = weather_get_pressure(weather);
		/* Refraction adjustment */
		alt_adj = ln_get_refraction_adj(hrz.alt, pressure, temperature);
		printf("Refraction: %f @ %fC,%fmbar\n", -alt_adj, temperature, pressure);
		hrz.alt -= alt_adj;
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
297
298
299
300
	/* libnova can't handle anything above 90.0 */
	if (hrz.alt > 90.0)
		hrz.alt = 90.0;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
301
302
303
	ln_get_equ_from_hrz (&hrz, &dwingeloo_pos, JD, &equ_pos_prec);
	/* Calculate inverse precession */
	ln_get_equ_prec2(&equ_pos_prec, JD, JD2000, &equ_pos_aber);
304
	get_equ_mean_from_aber(&equ_pos_aber, JD, &equ_pos);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

	ln_deg_to_hms(equ_pos.ra, &hms);
	ln_deg_to_dms(equ_pos.dec, &dms);

	printf("Trace  %f %f : %02dh%02dm%04.1f %s%03dd%02dm%04.1f\n",
	    hrz.az, hrz.alt,
	    hms.hours, hms.minutes, hms.seconds,
	    dms.neg ? "-" : " ", dms.degrees, dms.minutes, dms.seconds);

	ret = sprintf(statline, 
	    "%02dh%02dm%04.1f %s%03dd%02dm%04.1f %02dh%02dm%04.1f %s%03dd%02dm%04.1f %ld ",
	    track_hms.hours, track_hms.minutes, track_hms.seconds,
	    track_dms.neg ? "-" : "", 
	    track_dms.degrees, track_dms.minutes, track_dms.seconds,
	    hms.hours, hms.minutes, hms.seconds,
	    dms.neg ? "-" : "",
	    dms.degrees, dms.minutes, dms.seconds,
Jeroen Vreeken's avatar
Jeroen Vreeken committed
322
	    (unsigned long)now);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	statlinepos = statline + ret;
	for (i = 0; compensation_switches[i].name; i++) {
		if (i) {
			ret = sprintf(statlinepos, ",");
			statlinepos += ret;
		}
		ret = sprintf(statlinepos,
		    "%s=%d",
		    compensation_switches[i].name,
		    *compensation_switches[i].value);
		statlinepos += ret;
	}
	sprintf(statlinepos, "\n");

337
	status_server_send(stat_srv, statline);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
338
339
}

340
341
static void handler_interval(struct trace *trace, 
    struct timespec *interval, enum trace_interval_type type)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
342
{
343
	printf("interval:  %ld.%09ld\n", interval->tv_sec, interval->tv_nsec);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
344
345
346
}


Jeroen Vreeken's avatar
Jeroen Vreeken committed
347
348
int main(int argc, char **argv)
{
349
350
	struct command *sp_command_az = NULL;
	struct command *sp_command_el = NULL;
351
	struct command_server *cmd_srv;
352
	struct status_server *stat_srv;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
353
	time_t lastt = 0;
354
	struct timespec t_int;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
355

356
357
	dt_model_init();

Jeroen Vreeken's avatar
Jeroen Vreeken committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
	printf("libnova version: %s\n", ln_get_version());

	dwingeloo.lng.neg = 0;
	dwingeloo.lng.degrees = 6;
	dwingeloo.lng.minutes = 23;
	dwingeloo.lng.seconds = 46.21;
	dwingeloo.lat.neg = 0;
	dwingeloo.lat.degrees = 52;
	dwingeloo.lat.minutes = 48;
	dwingeloo.lat.seconds = 43.27;
	ln_hlnlat_to_lnlat(&dwingeloo, &dwingeloo_pos);

	track_hms.hours = 23;
	track_hms.minutes = 23;
	track_hms.seconds = 26;
	track_dms.neg = 0;
	track_dms.degrees = 58;
	track_dms.minutes = 48;
	track_dms.seconds = 0.0;

	signal(SIGPIPE, SIG_IGN);

380
381
382
	log_client_start(dt_host_console(), CONSOLE_LOG_PORT_IN, 
	    LOG_T_DEBUG, LOG_T_INFO, "console/j2000tracker");

Jeroen Vreeken's avatar
Jeroen Vreeken committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
	if (argc == 2 || argc == 4) {
		command_host = argv[argc - 1];
		trace_host = argv[argc - 1];
	}
	if (argc == 3 || argc == 4) {
		printf("Coords from command line\n");
		a2coord(argv[1], argv[2], &track_hms, &track_dms);
	}
	object.ra = ln_hms_to_deg(&track_hms);
	object.dec = ln_dms_to_deg(&track_dms);

	ln_deg_to_hms(object.ra, &track_hms);
	ln_deg_to_dms(object.dec, &track_dms);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
397
	do {
398
399
400
		sp_command_az = command_open_simple(command_host, command_port,
		    az_command_spg, "console/j2000tracker",
		    COMMAND_VALUE_TYPE_FLOAT);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
401
402
403
404
405
406
		if (!sp_command_az) {
			printf("Could not open connection for az commands\n");
			sleep(1);
		}
	} while (!sp_command_az);
	do {
407
408
409
		sp_command_el = command_open_simple(command_host, command_port,
		    el_command_spg, "console/j2000tracker",
		    COMMAND_VALUE_TYPE_FLOAT);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
410
411
412
413
414
415
		if (!sp_command_el) {
			printf("Could not open connection for el commands\n");
			sleep(1);
		}
	} while (!sp_command_el);
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
416
417
	traceval_az = trace_open(trace_host, trace_port);
	traceval_el = trace_open(trace_host, trace_port);
418
419
	trace_name_set(traceval_az, 0, az_trace_name);
	trace_name_set(traceval_el, 0, el_trace_name);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
420
421
	trace_autorecover(traceval_az, true);
	trace_autorecover(traceval_el, true);
422
423
424
425
426
	traceval_az->handler_interval = handler_interval;
	traceval_el->handler_interval = handler_interval;

	t_int.tv_sec = 1;
	t_int.tv_nsec = 0;
427
428
	trace_interval_set(traceval_az, &t_int, TRACE_INTERVAL_TYPE_INTERVAL);
	trace_interval_set(traceval_el, &t_int, TRACE_INTERVAL_TYPE_INTERVAL);
429

Jeroen Vreeken's avatar
Jeroen Vreeken committed
430

431
432
	cmd_srv = command_server_create(cmd_port, 0, 100);
	if (!cmd_srv) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
433
434
		printf("Could not open listen port for commands\n");
	} else {
435
		command_server_handler_raw_set(cmd_srv, handle_cmd);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
436
	}
437
438
	stat_srv = status_server_create(stat_port, 0, 100);
	if (!cmd_srv) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
		printf("Could not open listen port for status\n");
	}

	weather = weather_create("Dwingeloo");
	if (!weather) {
		fprintf(stderr, "Could not create weather handle\n");
		exit(-1);
	}

	while (1)
	{
		time_t t;
		struct ln_date date;
		double JD;
		struct ln_hrz_posn hrz;
		int high = 0;
		fd_set fdset_rx;
		struct timeval tv;
		double pressure, temperature;
		double alt_adj;
		
		t = time(NULL);
		if (t != lastt) {
			lastt = t;
			t += TIME_OFFSET;
		
			ln_get_date_from_timet(&t, &date);
			JD = ln_get_julian_day(&date);

			/* Calculate aberration */
			ln_get_equ_aber(&object, JD, &object_aber);
			/* Calculate precession */
			ln_get_equ_prec(&object_aber, JD, &object_prec);
			/* Convert to Alt/Az */
			ln_get_hrz_from_equ (&object_prec, &dwingeloo_pos, JD, &hrz);

			if (refraction_enable) {
				/* Get current weather */
				temperature = weather_get_temperature(weather);
				pressure = weather_get_pressure(weather);
				/* Refraction adjustment */
				alt_adj = ln_get_refraction_adj(hrz.alt, pressure, temperature);
				printf("Refraction: %f @ %fC,%fmbar\n", alt_adj, temperature, pressure);
				hrz.alt += alt_adj;
			}
			
			/* libnova has an az range from 0 to 360 with 0 being South.
			   Dwingeloo has an az range from -270 to 270 with 0 being South
			   
			   Stars that stay north of dwingeloo (incl circumpolar)
			   can be tracked in either positive or negative north.
			   So we take the nearest.
			   Other stars pass through south.
			 */
			printf("dec: %f Dwingeloo lat: %f => ",
			    object.dec, dwingeloo_pos.lat);
			if (object.dec > dwingeloo_pos.lat) {
				printf("Always north of zenith\n");
				if (traceval_az_deg < 0.0)
					hrz.az -= 360.0;
			} else {
				printf("Passes south of zenith\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
501
502
503
504
505
506
507
508
509
510
				if (hrz.az > 180.0) {
					if (traceval_az_deg > 150.0 &&
					    hrz.az < 270.0) {
						printf("Stay on positive side to prevent jump\n");
					} else {
						if (traceval_az_deg > 180.0 &&
						    hrz.az > 270.0 &&
						    tracking_command_send_p180 &&
						    tracking_enabled) {
							tracking_enabled = false;
511
512
							log_send(LOG_T_WARNING,
							    "Going over 270 degree border, disabling");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
513
514
515
516
517
518
519
520
521
522
						}
						hrz.az -= 360.0;
					}
				} else {
					if (traceval_az_deg < -150.0 &&
					    hrz.az >= 90.0) {
						printf("Already on negative side, stay there\n");
						hrz.az -= 360.0;
					}
				}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
			}
			
			if (dt_model_enable) {
				double daz, del;
				
				daz = dt_model_azimuth_delta(hrz.az, hrz.alt);
				del = dt_model_elevation_delta(hrz.az, hrz.alt);
				printf("model: %g %g\n", daz, del);
				hrz.az += daz;
				hrz.alt += del;
			}

			printf("Setpoint: %02dh%02dm%04.1f %s%03dd%02dm%04.1f %d %f %f\n",
			    track_hms.hours, track_hms.minutes, track_hms.seconds,
			    track_dms.neg ? "-" : " ",
			    track_dms.degrees, track_dms.minutes, track_dms.seconds,
			    (unsigned int)t, hrz.az, hrz.alt);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
541
542
			if (tracking_enabled && tracking_command_last_az_valid) {
				if (fabs(hrz.az - tracking_command_last_az) > 1.0) {
543
544
					struct command_entry entry;
					
545
546
					log_send(LOG_T_WARNING,
					    "Az going to fast to track (%e), disable tracking",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
547
548
					    fabs(hrz.az-tracking_command_last_az));
					tracking_enabled = false;
549
550
551
552
553
					
					entry.type = COMMAND_PTYPE_SPEED;
					entry.value.f = 0.0;
					command_send(sp_command_az, &entry);
					command_send(sp_command_el, &entry);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
554
555
556
557
				}
			}

			if (tracking_enabled) {
558
559
				struct command_entry entry;
				
Jeroen Vreeken's avatar
Jeroen Vreeken committed
560
561
				if (hrz.az > 180.0)
					tracking_command_send_p180 = true;
562
563
564
565
566
567
568
569
				
				entry.type = COMMAND_PTYPE_SETPOINT_TIME;
				entry.t.tv_sec = t;
				entry.t.tv_nsec = 0;
				entry.value.f = hrz.az * 2 * M_PI / 360.0;
				command_send(sp_command_az, &entry);
				entry.value.f = hrz.alt * 2 * M_PI / 360.0;
				command_send(sp_command_el, &entry);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
570
571
572
573
574
575
576
577

				tracking_command_last_az_valid = true;
				tracking_command_last_az = hrz.az;
			} else {
				tracking_command_send_p180 = false;
				tracking_command_last_az_valid = false;
				tracking_command_last_az = 0.0;
			}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
578
579
580
581
		}

		FD_ZERO(&fdset_rx);
	
582
		status_server_fdset_add(stat_srv, &fdset_rx, &high);
583
		command_server_fdset_add(cmd_srv, &fdset_rx, &high);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
584
585
		trace_fd_set(traceval_el, &fdset_rx, &high);
		trace_fd_set(traceval_az, &fdset_rx, &high);
586

Jeroen Vreeken's avatar
Jeroen Vreeken committed
587
588
589
		tv.tv_sec = 1;
		tv.tv_usec = 0;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
590
		select(high + 1, &fdset_rx, NULL, NULL, &tv);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
591

Jeroen Vreeken's avatar
Jeroen Vreeken committed
592
593
		trace_handle(traceval_az, &fdset_rx);
		trace_handle(traceval_el, &fdset_rx);
594

595
		command_server_fdset_handle(cmd_srv, &fdset_rx);
596
		status_server_fdset_handle(stat_srv, &fdset_rx);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
597
		
598
		output(stat_srv);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
599
600
601
	}

}