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

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

 */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
19
20
21
22
23
24
25
26
27
/*
	dt_ctr.c
	
	Main loop for the DT controller.
	Here the controller network is initialized.
	(By loading a .ctrl file supplied on the command line).
	After initialization the sample thread is started and this function's
	work is done.
 */
Jeroen Vreeken's avatar
Jeroen Vreeken committed
28

29
30
#define _GNU_SOURCE

Jeroen Vreeken's avatar
Jeroen Vreeken committed
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
root's avatar
root committed
36
#include <signal.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
37

38
#include "dynarg.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
39

Jeroen Vreeken's avatar
Jeroen Vreeken committed
40
41
42
43
44
45
46
47
48
49
#include <controller/controller_block.h>
#include <controller/controller_trace.h>
#include <controller/controller_sample.h>
#include <controller/controller_dumpdot.h>
#include <controller/controller_load.h>
#include <shell/shell.h>
#include <ec/ec.h>
#include <log/log.h>

#include <dt_port_numbers.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
50
51
52
53
54



int main(int argc, char **argv)
{
55
56
	char *ctrl_filename;
	char *dot_filename;
root's avatar
root committed
57
58
59
60
61
62
	sigset_t sigset;

	sigemptyset (&sigset);
	sigaddset(&sigset, SIGALRM);
	sigprocmask(SIG_BLOCK, &sigset, NULL);

63
64
	log_server_start(CTRL_LOG_PORT, LOG_T_DEBUG, LOG_T_INFO);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
65
66
	controller_block_param_init();
	
67
68
69
70
	if (argc < 2) {
		printf("Usage: %s <controllerfile>\n", argv[0]);
		printf("\n");
		printf("E.g.: %s dt_ctrl.ctrl\n", argv[0]);
71
		goto err_init;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
72
	}
73
	
74
75
	ctrl_filename = argv[1];
	
76
	/* Create and link blocks */
77
78
	log_send(LOG_T_DEBUG, "Going to load controller file '%s'", 
	    ctrl_filename);
79
	if (controller_load(ctrl_filename)) {
80
		log_send(LOG_T_ERROR, "Could not load controller file");
81
		goto err_init;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
82
83
84
85
86
87
	}

	controller_trace_server_start(CTRL_TRACE_PORT, 100);

	/* Start command shell */
	
88
	controller_sample_shell_add();
Jeroen Vreeken's avatar
Jeroen Vreeken committed
89
90
91
92
93
94
95
	controller_load_shell_add();

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

96
97
98
	asprintf(&dot_filename, "%s.dot", ctrl_filename);
	controller_dumpdot(dot_filename);
	free(dot_filename);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
99
100
101

	/* Start 'sample' */

102
	if (controller_block_sample_init()) {
103
		goto err_init;
104
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
105
106
107
108
109
110
111

	controller_sample_start();

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

Jeroen Vreeken's avatar
Jeroen Vreeken committed
113
114
115
116
117
118
119
		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
120
		log_send(LOG_T_INFO, "Elevation: %e (Torsion: %e) Azimuth: %e",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
121
122
123
124
125
126
127
		    el_pos, el_tor, az_pos);
		
		/* Log once every 15 minutes */
		sleep(15 * 60);
	}

	return 0;
128
129
130
131

err_init:
	log_server_flush();
	return 1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
132
}