console_moontracker.c 8.3 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
/*
	Command generator for moon tracking

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

	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>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
38
39
40
41
42
#include <stdbool.h>

#include <libnova/julian_day.h>
#include <libnova/refraction.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
43
44
45
46
47
48
49

#include "setpoint.h"
#include "tcp_connect.h"
#include "tcp_listen.h"

#include "aalib.h"
#include "weather.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
50
#include "dt_model.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
51
52
53
54
55

#include "dt_port_numbers.h"

char *command_host = "localhost";
int command_port = CONSOLE_COMMAND_PORT;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
56
57
int stat_port = CONSOLE_MOON_STAT_PORT;
int cmd_port = CONSOLE_MOON_CMD_PORT;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
59
60
char *az_command_spg = "Azimuth_Setpoint";
char *el_command_spg = "Elevation_Setpoint";

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
bool dt_model_enable = false;
bool switch_enabled = false;

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

struct compensation_switch compensation_switches[] = {
	{ "enabled", &switch_enabled },
	{ "dt_model", &dt_model_enable },
	{ NULL, NULL }
};

struct weather *weather;

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;

struct stat_client {
	struct stat_client *next;
	int fd;
};

struct stat_client *stat_clients = NULL;

struct cmd_client {
	struct cmd_client *next;
	int fd;
	char buffer[200];
	int buflen;
};

struct cmd_client *cmd_clients = NULL;



void new_stat_client(int fd_stat)
{
	struct stat_client *new_stat;
	int fd;
	
	fd = tcp_accept(fd_stat);
	if (fd < 0)
		return;
	ioctl(fd, FIONBIO, &(int){ 1 });
	
	new_stat = malloc(sizeof(struct stat_client));
	if (!new_stat) {
		close(fd);
		return;
	}
	
	new_stat->fd = fd;
	new_stat->next = stat_clients;
	stat_clients = new_stat;
}

void new_cmd_client(int fd_cmd)
{
	struct cmd_client *new_cmd;
	int fd;
	
	fd = tcp_accept(fd_cmd);
	if (fd < 0)
		return;
	ioctl(fd, FIONBIO, &(int){ 1 });
	
	new_cmd = malloc(sizeof(struct cmd_client));
	if (!new_cmd) {
		close(fd);
		return;
	}
	
	new_cmd->fd = fd;
	new_cmd->buflen = 0;
	new_cmd->next = cmd_clients;
	cmd_clients = new_cmd;
}

void fdset_cmd_clients(fd_set *fdset_rx, int *high)
{
	struct cmd_client *cmd;
	
	for (cmd = cmd_clients; cmd; cmd = cmd->next) {
		FD_SET(cmd->fd, fdset_rx);
		if (cmd->fd >= *high)
			*high = cmd->fd + 1;
	}
}

void handle_cmd(struct cmd_client *cmd)
{
	char *switches, *ptr;
	switches = cmd->buffer;
	
	printf("command: %s\n", switches);
	if (switches) {
		char *name, *val;
		int i;
		name = strtok_r(switches, ",=", &ptr);
		val = strtok_r(NULL, ",=", &ptr);
		printf("name %s value %s\n", name, val);
		for (i = 0; compensation_switches[i].name; i++) {
			if (!strcmp(name, compensation_switches[i].name)) {
				printf("found switch %s\n", name);
				if (val[0] == '1') {
					*compensation_switches[i].value = true;
				} else {
					*compensation_switches[i].value = false;
				}
			}
		}
	}
}

void check_cmd_clients(fd_set *fdset_rx)
{
	struct cmd_client **cmdp, **nextp = NULL;
	
	for (cmdp = &cmd_clients; *cmdp; cmdp = nextp) {
		int ret;
		
		nextp = &(*cmdp)->next;
		if (!FD_ISSET((*cmdp)->fd, fdset_rx))
			continue;
		
		do {
			ret = read((*cmdp)->fd, (*cmdp)->buffer+(*cmdp)->buflen, 1);
			if ((ret < 0 && errno != EAGAIN) || ret == 0) {
				struct cmd_client *tmp;
				
				tmp = *cmdp;
				close((*cmdp)->fd);
				*cmdp = (*cmdp)->next;
				free(tmp);
				nextp = cmdp;
			} else if (ret == 1) {
				(*cmdp)->buflen += 1;
				if ((*cmdp)->buffer[(*cmdp)->buflen-1] == '\n' ||
				    (*cmdp)->buffer[(*cmdp)->buflen-1] == '\r') {
					if ((*cmdp)->buflen > 1) {
						(*cmdp)->buffer[(*cmdp)->buflen] = 0;
						handle_cmd(*cmdp);
						(*cmdp)->buffer[0] = '9';
					}
					(*cmdp)->buflen = 0;
				}
				if ((*cmdp)->buflen >= 200)
					(*cmdp)->buflen = 0;
			}
		} while (ret > 0);
	}
}

void output(void)
{
	static time_t last = 0;
	time_t now;
	struct stat_client **stat_clientp, **nextp;
	char statline[800];
	int ret, i;
	char *statlinepos;
	
	now = time(NULL);
	if (now <= last)
		return;
	
	last = now;
	
	statlinepos = statline;
	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");

	for (stat_clientp = &stat_clients; *stat_clientp; stat_clientp = nextp){
		int ret;
		
		ret = write((*stat_clientp)->fd, statline, strlen(statline));
		if (ret <= 0) {
			struct stat_client *tmp;
			
			tmp = *stat_clientp;
			close((*stat_clientp)->fd);
			*stat_clientp = (*stat_clientp)->next;
			free(tmp);
			nextp = stat_clientp;
		} else {
			nextp = &(*stat_clientp)->next;
		}
	}
}


Jeroen Vreeken's avatar
Jeroen Vreeken committed
269
270
271
272
273
274
275


int main(int argc, char **argv)
{
	struct setpoint_command *sp_command_az = NULL;
	struct setpoint_command *sp_command_el = NULL;
	struct weather *weather;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
276
	int fd_stat, fd_cmd;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
277
278
279
280
281

	time_t lastt = 0;

	signal(SIGPIPE, SIG_IGN);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
	do {
		sp_command_az = setpoint_command_init(command_host, command_port,
		    az_command_spg, "console/moontracker");
		if (!sp_command_az) {
			printf("Could not open connection for az commands\n");
			sleep(1);
		}
	} while (!sp_command_az);
	do {
		sp_command_el = setpoint_command_init(command_host, command_port,
		    el_command_spg, "console/moontracker");
		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
298
299
300


	aalib_init();
Jeroen Vreeken's avatar
Jeroen Vreeken committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314

	fd_cmd = tcp_listen(cmd_port, 0, 100);
	if (fd_cmd < 0) {
		printf("Could not open listen port for commands\n");
	} else {
		ioctl(fd_cmd, FIONBIO, &(int){ 1 });
	}
	fd_stat = tcp_listen(stat_port, 0, 100);
	if (fd_stat < 0) {
		printf("Could not open listen port for status\n");
	} else {
		ioctl(fd_stat, FIONBIO, &(int){ 1 });
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
315
	weather = weather_create("Dwingeloo");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
316
317
318
319
	if (!weather) {
		fprintf(stderr, "Could not create weather handle\n");
		exit(-1);
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
320
321
322
323

	while (1)
	{
		time_t t;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
324
325
326
327
		int high = 0;
		fd_set fdset_rx;
		struct timeval tv;
		double az, el;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
		
		t = time(NULL);
		if (t != lastt) {
			lastt = t;
			t += 5;
		
//			jul 26 2008 5:39 utc
//			t =  1217050740;
		
			aalib(3, t, 
			    weather_get_temperature(weather),
			    weather_get_pressure(weather),
			    &az, &el);

			az -= 180.0;

			printf("%s", ctime(&t));
			printf("Setpoint: %d %f %f\n\n",
			    (unsigned int)t, az, el);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
			if (dt_model_enable) {
				double daz, del;
				
				daz = dt_model_azimuth_delta(az, el);
				del = dt_model_elevation_delta(az, el);
				printf("model: %g %g\n", daz, del);
				az += daz;
				el += del;
			}

			if (switch_enabled) {
				setpoint_command_setpoint_time(sp_command_az,
				    az * 2 * M_PI / 360.0, t);
				setpoint_command_setpoint_time(sp_command_el,
				    el * 2 * M_PI / 360.0, t);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
362
363
364
365
366
			} else {
				/* not enabled, send idle just in case we were
				   still moving */
				setpoint_command_speed(sp_command_az, 0.0);
				setpoint_command_speed(sp_command_el, 0.0);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
			}
		}

		FD_ZERO(&fdset_rx);
	
		if (fd_stat >= 0) {
			FD_SET(fd_stat, &fdset_rx);
			if (fd_stat >= high)
				high = fd_stat + 1;
		}
		if (fd_cmd >= 0) {
			FD_SET(fd_cmd, &fdset_rx);
			if (fd_cmd >= high)
				high = fd_cmd + 1;
		}
		fdset_cmd_clients(&fdset_rx, &high);
		
		tv.tv_sec = 1;
		tv.tv_usec = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
386

Jeroen Vreeken's avatar
Jeroen Vreeken committed
387
		select(high, &fdset_rx, NULL, NULL, &tv);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
388

Jeroen Vreeken's avatar
Jeroen Vreeken committed
389
390
		if (FD_ISSET(fd_stat, &fdset_rx)) {
			new_stat_client(fd_stat);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
391
		}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
392
393
394
395
396
397
		if (FD_ISSET(fd_cmd, &fdset_rx)) {
			new_cmd_client(fd_cmd);
		}
		check_cmd_clients(&fdset_rx);
		
		output();
Jeroen Vreeken's avatar
Jeroen Vreeken committed
398
399
400
401

	}

}