dt_ctrl.c 10.7 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2011
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007 - 2011

	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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

#include <dynarg.h>

#include "controller_block.h"
#include "controller_trace.h"
#include "controller_setpoint_command.h"
#include "controller_sample.h"
#include "controller_dumpdot.h"
#include "controller_load.h"
#include "block_filter_iir.h"
#include "block_matrix_2x2.h"
#include "shell.h"
#include "ec.h"
#include "block_ec.h"
#include "log.h"

#include "dt_az.h"
#include "dt_az_model.h"
#include "dt_az_ec.h"
#include "dt_el.h"
#include "dt_el_model.h"
#include "dt_el_ec.h"

#include "dt_az_safety.h"
#include "dt_el_safety.h"

#include "dt_port_numbers.h"

#define ETHERCAT_IF "eth0"

#if !defined(USE_AZ_SIM) || !defined(USE_EL_SIM)
#define USE_ETHERCAT
#endif

/* Azimuth gain, basicly gear ratio */
#define AZ_GAIN 15006.75


static struct controller_spg_name dt_spg_list[] = {
	{ "Azimuth_Setpoint",           "rad", "azimuth_spg"           },

	{ "Elevation_Setpoint",         "rad", "elevation_spg"         },
	{ "Elevation_Torsion_Setpoint", "rad", "elevation_torsion_spg" },

	{ NULL, NULL, NULL },
};


/********************************************
 *	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 azimuth_enable(char *args, char *out, int *outlen)
{
	va_list ap;

	if (args && !strcmp(args, "enable")) {
		va_new(ap);
		va_add(ap, int, true);
		if (controller_block_param_set("azimuth_servo_state", "enabled", ap))
			printf("Failed to set servo_state to enabled\n");
		va_free(ap);

		*outlen = sprintf(out, "Azimuth enabled\n");
		log_send(LOG_T_INFO, out);
		
	} else if (args && !strcmp(args, "disable")) {
		va_new(ap);
		va_add(ap, int, false);
		if (controller_block_param_set("azimuth_servo_state", "enabled", ap))
			printf("Failed to set servo_state to enabled\n");
		va_free(ap);

		*outlen = sprintf(out, "Azimuth disabled\n");
		log_send(LOG_T_INFO, out);
		
	} else if (args && !strcmp(args, "reinit")) {
#ifdef USE_ETHERCAT
		ec_tx_pdo_enable(0);
		ec_rx_pdo_enable(0);
		sleep(1);
#endif
		va_new(ap);
		if (controller_block_param_set("dt_az", "re_init", ap))
			printf("re-initializing dt_az failed\n");
		va_free(ap);

		*outlen = sprintf(out, "Azimuth initialized\n");
		log_send(LOG_T_INFO, out);
		
	} else {
		*outlen = sprintf(out,
		     "Valid arguments are: 'enable' and 'disable'\n");
	}

	return *outlen;
}

int elevation_enable(char *args, char *out, int *outlen)
{
	va_list ap;

	if (args && !strcmp(args, "enable")) {
		va_new(ap);
		va_add(ap, int, true);
		if (controller_block_param_set("elevation_servo_state", "enabled", ap))
			printf("Failed to set servo_state to enabled\n");
		va_free(ap);

		va_new(ap);
		if (controller_block_param_set("elevation_torsion_pid", "reset", ap))
			printf("resetting elevation_torsion_pid failed\n");
		va_free(ap);

		va_new(ap);
		va_add(ap, int, 0);
		if (controller_block_param_set("elevation_safety", "recover", ap))
			printf("end recovering elevation_safety failed\n");
		va_free(ap);

		*outlen = sprintf(out, "Elevation enabled\n");
		log_send(LOG_T_INFO, out);
		
	} else if (args && !strcmp(args, "recover")) {
		va_new(ap);
		va_add(ap, int, 1);
		if (controller_block_param_set("elevation_safety", "recover", ap))
			printf("recovering elevation_safety failed\n");
		va_free(ap);

		*outlen = sprintf(out, "Elevation in recover mode\n");
		log_send(LOG_T_INFO, out);
		
	} else if (args && !strcmp(args, "disable")) {
		va_new(ap);
		va_add(ap, int, false);
		if (controller_block_param_set("elevation_servo_state", "enabled", ap))
			printf("Failed to set servo_state to disabled\n");
		va_free(ap);

		va_new(ap);
		va_add(ap, int, 0);
		if (controller_block_param_set("elevation_safety", "recover", ap))
			printf("disabling elevation_safety failed\n");
		va_free(ap);

		*outlen = sprintf(out, "Elevation disabled\n");
		log_send(LOG_T_INFO, out);
		
	} else if (args && !strcmp(args, "reinit")) {
#ifdef USE_ETHERCAT
		ec_tx_pdo_enable(0);
		ec_rx_pdo_enable(0);
		sleep(1);
#endif
		va_new(ap);
		if (controller_block_param_set("dt_el", "re_init", ap))
			printf("re-initializing dt_el failed\n");
		va_free(ap);

		*outlen = sprintf(out, "Elevation initialized\n");
		log_send(LOG_T_INFO, out);
		
	} else {
		*outlen = sprintf(out,
		     "Valid arguments are: 'enable', 'disable', 'reinit', 'recover' and 'holding'\n");
	}

	return *outlen;
}

#ifdef USE_ETHERCAT
int ethercat_enable(char *args, char *out, int *outlen)
{
	if (args && !strcmp(args, "enable")) {
		ec_rx_pdo_enable(1);
		ec_tx_pdo_enable(1);
		*outlen = sprintf(out, "Ethercat pdo packets enabled\n");
		log_send(LOG_T_INFO, out);
	} else if (args && !strcmp(args, "disable")) {
		ec_tx_pdo_enable(0);
		ec_rx_pdo_enable(0);
		*outlen = sprintf(out, "Ethercat pdo packets disabled\n");
		log_send(LOG_T_INFO, out);
	} else {
		*outlen = sprintf(out,
		     "Valid arguments are: 'enable' and 'disable'\n");
	}

	return *outlen;
}
#endif /* USE_ETHERCAT */

int elevation_torsion(char *argc, char *out, int *outlen)
{
	float torque;
	
	if (controller_block_output_get_float("elevation_torsion_torque", "difference", &torque)) {
		*outlen = sprintf(out, "Error: Could not get elevation torsion torque\n");
		log_send(LOG_T_ERROR, out);
	} else {
		*outlen = sprintf(out, "Elevation torsion torque: %e Nm\n", torque);
	}

	return *outlen;
}


/********************************************
 *	Params:
 */

int param_load(void)
{
	FILE *fd_param;
	char buffer[101];
	
	fd_param = fopen("dt_ctrl.param", "r");
	if (!fd_param) {
		log_send(LOG_T_ERROR, "Could not open param file!");
		return -1;
	}
	
	while (fgets(buffer, 100, fd_param)) {
		char *type;
		char *name;
		char *param;
		char *value;
		char *ptr;
		
		type = strtok_r(buffer, " \t", &ptr);
		name = strtok_r(NULL, " \t", &ptr);
		param = strtok_r(NULL, " \t", &ptr);
		value = strtok_r(NULL, "\n", &ptr);
		
		if (!strcmp(type, "float")) {
			float val;
			va_list ap;
			
			sscanf(value, "%f", &val);
			va_new(ap);
			va_add(ap, double, val);
			if (controller_block_param_set(name, param, ap)) {
				char msg[100];
				printf(msg, "Setting %s.%s failed\n", name, param);
				log_send(LOG_T_ERROR, msg);
			}
			va_free(ap);
		}
	}
	return 0;
}

int param_reload(char *argc, char *out, int *outlen)
{
	if (param_load()) {
		*outlen = sprintf(out, "Reloading parameters failed!\n");
		log_send(LOG_T_ERROR, "Reloading parameters failed!\n");
	} else {
		*outlen = sprintf(out, "Reloaded parameters\n");
		log_send(LOG_T_INFO, "Reloaded parameters\n");
	}

	return *outlen;
}

int main(int argc, char **argv)
{
	int i;
	
	controller_block_param_init();
	
#ifdef USE_EL_SIM
	if (controller_load("dt_ctrl_el_sim.ctrl")) {
		printf("Could not link blocks\n");
		return 1;
	}
#else
	if (controller_load("dt_ctrl_el.ctrl")) {
		printf("Could not link blocks\n");
		return 1;
	}
#endif /* USE_EL_SIM */

#ifdef USE_AZ_SIM
	if (controller_load("dt_ctrl_az_sim.ctrl")) {
		printf("Could not link blocks\n");
		return 1;
	}
#else
	if (controller_load("dt_ctrl_az.ctrl")) {
		printf("Could not link blocks\n");
		return 1;
	}
#endif /* USE_AZ_SIM */

	/* Create and link blocks */

	if (controller_load("dt_ctrl.ctrl")) {
		printf("Could not link blocks\n");
		return 1;
	}


	param_load();



#ifdef USE_AZ_SIM
	/* Enable noise injection */
//	if (dt_az_model_torque_noise_set_type(dt_az,
//	    DT_AZ_TORQUE_NOISE_SINE)) {
//		printf("Could not set noise injection type\n");
//	}
//	if (dt_az_model_torque_noise_set_freq(dt_az, 5.0)) {
//		printf("Could not set noise frequency\n");
//	}
//	if (dt_az_model_torque_noise_set_gain(dt_az, /*1*/0.0)) {
//		printf("Could not set noise gain\n");
//	}
#endif /* USE_AZ_SIM */	
	
	
	controller_trace_server_start(CTRL_TRACE_PORT, 100);

	/* Add spg command handlers */

	for (i = 0; dt_spg_list[i].name; i++)
		controller_setpoint_command_server_add(&dt_spg_list[i]);

	controller_setpoint_command_server_start(CTRL_COMMAND_PORT, i * 2);

	/* Start command shell */
	
	shell_cmd_add(&(struct shell_cmd){"timing",    "reset/print sample timing",           sample_timing     });
	shell_cmd_add(&(struct shell_cmd){"azimuth",   "enable/disable azimuth controller",   azimuth_enable    });
	shell_cmd_add(&(struct shell_cmd){"elevation", "enable/disable elevation controller", elevation_enable  });
	shell_cmd_add(&(struct shell_cmd){"elevation_torque", "get elevation torsion torque", elevation_torsion });
#ifdef USE_ETHERCAT
	shell_cmd_add(&(struct shell_cmd){"ethercat",  "enable/disable ethercat pdo packets", ethercat_enable   });
#endif /* USE_ETHERCAT */
	shell_cmd_add(&(struct shell_cmd){"reload",    "parameter reload",                    param_reload      });
	
	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);

	controller_dumpdot("dt_ctrl.dot");

	/* 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;
		char msg[100];
		
		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");

		sprintf(msg, "Elevation: %e (Torsion: %e) Azimuth: %e\n",
		    el_pos, el_tor, az_pos);
		log_send(LOG_T_INFO, msg);
		
		/* Log once every 15 minutes */
		sleep(15 * 60);
	}

	return 0;
}