block_trajectplayer.c 7.38 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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2011
	Copyright Stichting C.A. Muller Radioastronomiestation, 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 "controller_block.h"
#include "controller_sample.h"
#include "block_trajectplayer.h"
#include "log.h"

/*
       inputs               outputs
       nr name   type       nr name     type

     ------------------------------------------
     |                                        |
  ---| 0  start  bool       0  position float |----
     |	                                      |
  ---| 1  stop   bool       1  playing  bool  |----
     |                                        |
  ---| 2  select int        2  done     bool  |----
     |	                                      |
     ------------------------------------------
     
 */

struct controller_block_private {
	bool *start;
	bool *stop;
	uint32_t *select;
	
	float position;
	bool playing;
	bool done;
	
	bool reset_on_stop;
	
	int cur;
	float playtime;
	
	int nr;
	struct block_trajectplayer_param **trajectories;
};

static int findtraject(struct controller_block_private *priv, int select)
{
	int i;
	
	for (i = 0; i < priv->nr; i++) {
		if (priv->trajectories[i]->nr == select)
			break;
	}
	return i;
}

static void calculate(struct controller_block *player)
{     
	struct controller_block_private *priv = player->private;
	double playtime = priv->playtime;
	struct block_trajectplayer_param *traject;


	if (*priv->stop) {
		priv->playing = false;
		playtime = 0.0;
		priv->done = false;
		if (priv->reset_on_stop) {
			priv->position = 0.0;
		}
	} else if (priv->playing) {
		traject = priv->trajectories[priv->cur];
		playtime += controller_sample_period();
		if (priv->playtime >= traject->time[traject->len - 1]) {
			priv->playing = false;
			priv->done = true;
			priv->position = traject->position[traject->len - 1];
			playtime = 0.0;
		} else {
			int t1, t2;
			float posdiff;
			float tdiff;
			float tinter;
			
			for (t1 = 0; t1 < traject->len; t1++) {
				if (traject->time[t1] >= priv->playtime) {
					t1--;
					break;
				}
			}
			if (t1 < 0)
				t1 = 0;
			t2 = t1 + 1;
			if (t2 >= traject->len)
				t2 = traject->len;
			tdiff = traject->time[t2] - traject->time[t1];
			tinter = playtime - traject->time[t1];
			posdiff = traject->position[t2] - traject->position[t1];
			priv->position =
			    traject->position[t1] + posdiff * tinter / tdiff;
			priv->playing = true;
			priv->done = false;
		}
	} else {
		if (*priv->start) {
			int cur = findtraject(priv, *priv->select);
			if (cur >= priv->nr) {
				log_send(LOG_T_ERROR,
				     "Illegal select, trajectory not available!");
			} else {
				priv->cur = cur;
				traject = priv->trajectories[cur];
				priv->playing = true;
				priv->done = false;
				playtime = 0.0;
				priv->position = traject->position[0];
			}
		}
	}
	
	priv->playtime = playtime;
}

static struct controller_block_param_list params[] = {
	{ "traject",       true },
	{ "reset_on_stop", true },
	{ NULL },
};

static void param_get(struct controller_block *player, int param, void *val)
{
	//TODO
}

static void param_set(struct controller_block *player, int param, va_list ap)
{
	struct controller_block_private *priv = player->private;
	struct block_trajectplayer_param traject;
	int traject_nr;
	
	switch (param) {
		case 0: {
			traject.nr = va_arg(ap, int);
			traject.len = va_arg(ap, int);
			traject.position = va_arg(ap, float *);
			traject.time = va_arg(ap, float *);
			
			traject_nr = findtraject(priv, traject.nr);
			
			if (traject_nr == priv->nr) {
				priv->trajectories = realloc(priv->trajectories,
				    sizeof(struct block_trajectplayer_param *) *
				    priv->nr);
				priv->trajectories[priv->nr] = calloc(
				    sizeof(struct block_trajectplayer_param), 1);
				priv->nr++;
				priv->trajectories[traject_nr]->nr = traject.nr;
			}
			priv->trajectories[traject_nr]->len = traject.len;
			priv->trajectories[traject_nr]->position = realloc(
			    priv->trajectories[traject_nr]->position,
			    sizeof(float) * traject.len);
			priv->trajectories[traject_nr]->time = realloc(
			    priv->trajectories[traject_nr]->time,
			    sizeof(float) * traject.len);
			memcpy(priv->trajectories[traject_nr]->position,
			    traject.position, sizeof(float) * traject.len);
			memcpy(priv->trajectories[traject_nr]->time,
			    traject.time, sizeof(float) * traject.len);
			
			
			break;
		}
		case 1: {
			priv->reset_on_stop = va_arg(ap, int);
			break;
		}
		default:
			break;
	}
}

struct controller_block * block_trajectplayer_create(char *name)
{
	struct controller_block *player;
	
	player = malloc(sizeof(struct controller_block));
	if (!player)
		return NULL;

	player->type = "trajectplayer";
	player->name = malloc(strlen(name)+1);
	if (!player->name)
		goto err_player;
	strcpy(player->name, name);

	player->private = malloc(sizeof(struct controller_block_private));
	if (!player->private)
		goto err_name;
	player->private->playing = false;
	player->private->done = false;
	player->private->position = 0.0;

	player->private->reset_on_stop = false;
	player->private->nr = 0;
	player->private->cur = 0;
	player->private->trajectories = NULL;

	player->inputs = 3;
	player->input = malloc(sizeof(struct controller_block_interm) * 3);
	if (!player->input)
		goto err_private;
	player->input[0].name = "start";
	player->input[0].type = CONTROLLER_BLOCK_TERM_BOOL;
	player->input[0].value.b = &player->private->start;
	player->input[0].ghostof = NULL;
	player->input[1].name = "stop";
	player->input[1].type = CONTROLLER_BLOCK_TERM_BOOL;
	player->input[1].value.b = &player->private->stop;
	player->input[1].ghostof = NULL;
	player->input[2].name = "select";
	player->input[2].type = CONTROLLER_BLOCK_TERM_UINT32;
	player->input[2].value.u32 = &player->private->select;
	player->input[2].ghostof = NULL;

	player->outputs = 3;
	player->output = malloc(sizeof(struct controller_block_outterm) * 3);
	if (!player->output)
		goto err_input;
	player->output[0].name = "position";
	player->output[0].type = CONTROLLER_BLOCK_TERM_FLOAT;
	player->output[0].value.f = &player->private->position;
	player->output[0].source = player;
	player->output[1].name = "playing";
	player->output[1].type = CONTROLLER_BLOCK_TERM_BOOL;
	player->output[1].value.b = &player->private->playing;
	player->output[1].source = player;
	player->output[2].name = "done";
	player->output[2].type = CONTROLLER_BLOCK_TERM_BOOL;
	player->output[2].value.b = &player->private->done;
	player->output[2].source = player;

	player->calculate = calculate;

	if (controller_block_param_list_init(player, params))
		goto err_output;

	player->param_get = param_get;
	player->param_set = param_set;

	controller_block_add(player);
	return player;

err_output:
	free(player->output);
err_input:
	free(player->input);
err_private:
	free(player->private);
err_name:
	free(player->name);
err_player:
	free(player);
	return NULL;
}