dt_az_stoeber_sim.c 4.27 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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2013

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

#include "controller_block.h"
#include "esc.h"
#include "ec_stoeber.h"

struct controller_block_private {
28
29
	bool *ba1;
	bool *ba2;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
32
33
34
35
36
37
38
39
40
41
42
};

struct ghost_term {
	char *name;
	char *realblock;
	char *realname;
};

static struct ghost_term ghost_outputs[] = {
	{ "position", 	"dt_az_pos_quantize", 	"out"    },
	{ "speed",    	"dt_az_accelerate",   	"out"    },
	{ "torque",   	"dt_az_torque_limit", 	"out"    },
	{ "enabled", 	"dt_az_enabled",      	"output" },
43
44
45
46
47
	{ "be1",	"dt_az_safety_n_not",	"output" },
	{ "be2",	"dt_az_safety_n",	"value"  },
	{ "be3",	"dt_az_safety_p_not",	"output" },
	{ "be4",	"dt_az_safety_p",	"value"  },
	{ "be5",	"dt_az_bex",		"value"  },
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	{ "ae1", 	"dt_az_focusbox",	"value"  },
};

static struct ghost_term ghost_inputs[] = {
	{ "speed",	"dt_az_speed_limit",  	"in" },
	{ "torque",	"dt_az_torque_limit", 	"limit" },
	{ "enable",	"dt_az_enabled",      	"input" },
	{ NULL },
};

struct controller_block *block_dt_az_stoeber_sim_create(char *name, va_list ap)
{
	struct controller_block *stoeber;
	struct controller_block_private *private;
	int i;
	
	stoeber = malloc(sizeof(struct controller_block));
	if (!stoeber)
		goto err_malloc;
	
	stoeber->type = "dt_az_stoeber_sim";

	stoeber->name = strdup(name);
	if (!stoeber->name)
		goto err_name;

	private = calloc(1, sizeof(struct controller_block_private));
	if (!private)
		goto err_private;
	stoeber->private = private;

	for (i = 0; ghost_outputs[i].name; i++);
	stoeber->outputs = i;
	stoeber->output = malloc(sizeof(struct controller_block_outterm)*i);
	if (!stoeber->output)
		goto err_output;
	for (i = 0; ghost_outputs[i].name; i++) {
		struct controller_block *subblock;
		int j;
		
		stoeber->output[i].name = ghost_outputs[i].name;
		subblock = controller_block_find(ghost_outputs[i].realblock);
		if (!subblock) {
			printf("block %s not found", ghost_outputs[i].realblock);
		}
		for (j = 0; j < subblock->outputs; j++) {
			if (!strcmp(subblock->output[j].name,
			    ghost_outputs[i].realname)) {
				stoeber->output[i].value =
				    subblock->output[j].value;
				stoeber->output[i].type =
				    subblock->output[j].type;
				stoeber->output[i].source = subblock->output[j].source;
				stoeber->output[i].sourceterm = &subblock->output[j];
				break;
			}
		}
	}

	for (i = 0; ghost_inputs[i].name; i++);
108
109
	stoeber->inputs = i + 2;
	stoeber->input = malloc(sizeof(struct controller_block_interm)*stoeber->inputs);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	if (!stoeber->input)
		goto err_input;
	for (i = 0; ghost_inputs[i].name; i++) {
		struct controller_block *subblock;
		int j;
		
		stoeber->input[i].name = ghost_inputs[i].name;
		subblock = controller_block_find(ghost_inputs[i].realblock);
		for (j = 0; j < subblock->inputs; j++) {
			if (!strcmp(subblock->input[j].name,
			    ghost_inputs[i].realname)) {
				stoeber->input[i].value =
				    subblock->input[j].value;
				stoeber->input[i].type =
				    subblock->input[j].type;
				stoeber->input[i].ghostof =
				    &subblock->input[j];
				break;
			}
		}
	}
131
132
133
134
135
136
137
138
139
	stoeber->input[i].name = "ba1";
	stoeber->input[i].value.b = &stoeber->private->ba1;
	stoeber->input[i].type = CONTROLLER_BLOCK_TERM_BOOL;
	stoeber->input[i].ghostof = NULL;
	i++;
	stoeber->input[i].name = "ba2";
	stoeber->input[i].value.b = &stoeber->private->ba2;
	stoeber->input[i].type = CONTROLLER_BLOCK_TERM_BOOL;
	stoeber->input[i].ghostof = NULL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

	stoeber->params = 0;

	stoeber->calculate = NULL;

	controller_block_add(stoeber);

	return stoeber;

err_input:
	free(stoeber->output);
err_output:
	free(private);
err_private:
	free(stoeber->name);
err_name:
	free(stoeber);
err_malloc:
	return NULL;
}