dt_ctrl.c 3.29 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
Jeroen Vreeken's avatar
Jeroen Vreeken committed
2
3
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007 - 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

	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/>.

 */

20
21
#define _GNU_SOURCE

Jeroen Vreeken's avatar
Jeroen Vreeken committed
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

28
#include "dynarg.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
29
30
31
32
33
34
35
36
37
38

#include "controller_block.h"
#include "controller_trace.h"
#include "controller_sample.h"
#include "controller_dumpdot.h"
#include "controller_load.h"
#include "shell.h"
#include "ec.h"
#include "log.h"

39
#include "dt_port_numbers.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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


/********************************************
 *	Shell interface:
 */

int sample_timing(char *args, char *out, int *outlen)
{
	if (args && !strcmp(args, "print")) {
		*outlen = sprintf(out, "Sample timing:\n");
		*outlen += controller_sample_timing_snprintf(out + *outlen, 2000);
	} else if (args && !strcmp(args, "reset")) {
		controller_sample_timing_reset();
		*outlen = sprintf(out, "Sample timing has been cleared.\n");
	} else {
		*outlen = sprintf(out,
		     "Valid arguments are: 'reset' and 'print'\n");
	}

	return *outlen;
}


int main(int argc, char **argv)
{
65
66
67
	char *ctrl_filename;
	char *dot_filename;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
68
69
	controller_block_param_init();
	
70
71
72
73
	if (argc < 2) {
		printf("Usage: %s <controllerfile>\n", argv[0]);
		printf("\n");
		printf("E.g.: %s dt_ctrl.ctrl\n", argv[0]);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
74
75
		return 1;
	}
76
	
77
78
	ctrl_filename = argv[1];
	
79
	/* Create and link blocks */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
80

81
	if (controller_load(ctrl_filename)) {
82
		printf("Could load controller file\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
83
84
85
86
87
88
89
		return 1;
	}

	controller_trace_server_start(CTRL_TRACE_PORT, 100);

	/* Start command shell */
	
90
	shell_cmd_add(&(struct shell_cmd){"timing", "reset/print sample timing", sample_timing });
Jeroen Vreeken's avatar
Jeroen Vreeken committed
91
92
93
94
95
96
97
98
99
100
	
	controller_load_shell_add();

	shell_intro = "Dwingeloo Telescope controller\n";
	shell_prompt = "DT>";
	
	shell_server_start(CTRL_SHELL_PORT, 10);

	log_server_start(CTRL_LOG_PORT);

101
102
103
	asprintf(&dot_filename, "%s.dot", ctrl_filename);
	controller_dumpdot(dot_filename);
	free(dot_filename);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
104
105
106
107
108
109
110
111
112
113
114
115

	/* Start 'sample' */

	controller_block_sample_init();

	printf("Starting sample thread\n");
	controller_sample_start();

	while (1) {
		float az_pos;
		float el_pos;
		float el_tor;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
116

Jeroen Vreeken's avatar
Jeroen Vreeken committed
117
118
119
120
121
122
123
		if (controller_block_output_get_float("elevation_input_matrix", "out0", &el_pos))
			log_send(LOG_T_ERROR, "Getting elevation_input_matrix.out0 failed\n");
		if (controller_block_output_get_float("elevation_input_matrix", "out1", &el_tor))
			log_send(LOG_T_ERROR, "Getting elevation_input_matrix.out1 failed\n");
		if (controller_block_output_get_float("dt_az", "position", &az_pos))
			log_send(LOG_T_ERROR, "Getting dt_az.position failed\n");

Jeroen Vreeken's avatar
Jeroen Vreeken committed
124
		log_send(LOG_T_INFO, "Elevation: %e (Torsion: %e) Azimuth: %e",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
125
126
127
128
129
130
131
132
		    el_pos, el_tor, az_pos);
		
		/* Log once every 15 minutes */
		sleep(15 * 60);
	}

	return 0;
}