-
Notifications
You must be signed in to change notification settings - Fork 61
/
buzz_closures.cpp
359 lines (291 loc) · 11.3 KB
/
buzz_closures.cpp
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
#include "buzz_controller_drone_sim.h"
#include "utils/buzz_utils.h"
using namespace buzz_utils;
namespace buzz_drone_sim {
/****************************************/
/****************************************/
static int BuzzRandUniform(buzzvm_t vm){
buzzvm_lload(vm, 1);
buzzvm_lload(vm, 2);
buzzobj_t buzz_range_lowest = buzzvm_stack_at(vm, 2);
buzzobj_t buzz_range_highest = buzzvm_stack_at(vm, 1);
float range_lowest, range_highest;
if (buzz_range_lowest->o.type == BUZZTYPE_FLOAT) {
range_lowest = buzz_range_lowest->f.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"uniform(x,y): expected %s, got %s in first argument",
buzztype_desc[BUZZTYPE_FLOAT],
buzztype_desc[buzz_range_lowest->o.type]
);
return vm->state;
}
if (buzz_range_highest->o.type == BUZZTYPE_FLOAT) {
range_highest = buzz_range_highest->f.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"uniform(x,y): expected %s, got %s in first argument",
buzztype_desc[BUZZTYPE_FLOAT],
buzztype_desc[buzz_range_highest->o.type]
);
return vm->state;
}
std::uniform_real_distribution<float> distribution(range_lowest, range_highest);
/* Get pointer to the controller */
buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1));
buzzvm_gload(vm);
/* Call function */
float random_value = distribution(reinterpret_cast<CBuzzControllerDroneSim*>(buzzvm_stack_at(vm, 1)->u.value)->GetRandomEngine());
buzzvm_pushf(vm, random_value);
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
static int BuzzRandGauss(buzzvm_t vm){
buzzvm_lload(vm, 1);
buzzvm_lload(vm, 2);
buzzobj_t buzz_mean = buzzvm_stack_at(vm, 2);
buzzobj_t buzz_stdev = buzzvm_stack_at(vm, 1);
int mean, stdev;
if (buzz_mean->o.type == BUZZTYPE_INT) {
mean = buzz_mean->i.value;
} else if (buzz_mean->o.type == BUZZTYPE_FLOAT) {
mean = (int)buzz_mean->f.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"gauss(x,y): expected %s, got %s in first argument",
buzztype_desc[BUZZTYPE_INT],
buzztype_desc[buzz_mean->o.type]
);
return vm->state;
}
if (buzz_stdev->o.type == BUZZTYPE_INT) {
stdev = buzz_stdev->i.value;
} else if (buzz_stdev->o.type == BUZZTYPE_FLOAT) {
stdev = (int)buzz_stdev->f.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"gauss(x,y): expected %s, got %s in first argument",
buzztype_desc[BUZZTYPE_INT],
buzztype_desc[buzz_stdev->o.type]
);
return vm->state;
}
std::normal_distribution<float> distribution(mean, stdev);
/* Get pointer to the controller */
buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1));
buzzvm_gload(vm);
/* Call function */
float random_value = distribution(reinterpret_cast<CBuzzControllerDroneSim*>(buzzvm_stack_at(vm, 1)->u.value)->GetRandomEngine());
buzzvm_pushf(vm, random_value);
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
static int BuzzHasReached(buzzvm_t vm) {
/* Push the vector components */
buzzvm_lload(vm, 1);
buzzvm_lload(vm, 2);
buzzvm_lload(vm, 3);
/* Create a new vector with that */
CVector2 position;
float delta;
buzzobj_t tX = buzzvm_stack_at(vm, 3);
buzzobj_t tY = buzzvm_stack_at(vm, 2);
buzzobj_t tDelta = buzzvm_stack_at(vm, 1);
if (tX->o.type == BUZZTYPE_FLOAT) {
position.SetX(tX->f.value);
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"goto_abs(x,y): expected %s, got %s in first argument",
buzztype_desc[BUZZTYPE_FLOAT],
buzztype_desc[tX->o.type]
);
return vm->state;
}
if (tY->o.type == BUZZTYPE_FLOAT) {
position.SetY(tY->f.value);
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"goto_abs(x,y): expected %s, got %s in second argument",
buzztype_desc[BUZZTYPE_FLOAT],
buzztype_desc[tY->o.type]
);
return vm->state;
}
if (tDelta->o.type == BUZZTYPE_FLOAT) {
delta = tDelta->f.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"goto_abs(x,y): expected %s, got %s in second argument",
buzztype_desc[BUZZTYPE_FLOAT],
buzztype_desc[tDelta->o.type]
);
return vm->state;
}
/* Get pointer to the controller */
buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1));
buzzvm_gload(vm);
/* Call function */
bool has_reached = reinterpret_cast<CBuzzControllerDroneSim*>(buzzvm_stack_at(vm, 1)->u.value)->HasReached(position, delta);
buzzvm_pushi(vm, (int) has_reached);
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
static int BuzzGetCurrentKey(buzzvm_t vm) {
/* Get pointer to the controller */
buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1));
buzzvm_gload(vm);
/* Call function */
std::string key_value = reinterpret_cast<CBuzzControllerDroneSim*>(buzzvm_stack_at(vm, 1)->u.value)->GetCurrentKey();
buzzvm_pushs(vm, buzzvm_string_register(vm, key_value.c_str(), 1));
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
static int BuzzGetRadiationIntensity(buzzvm_t vm) {
/* Get pointer to the controller */
buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1));
buzzvm_gload(vm);
/* Call the function */
float radiationIntensity = reinterpret_cast<CBuzzControllerDroneSim*>(buzzvm_stack_at(vm, 1)->u.value)->GetRadiationIntensity();
/* Push the result to the VM */
buzzvm_pushf(vm, radiationIntensity);
return buzzvm_ret1(vm);
}
/****************************************/
/****************************************/
static int BuzzLogDatum(buzzvm_t vm){
/* Push the vector components */
buzzvm_lload(vm, 1);
buzzvm_lload(vm, 2);
buzzvm_lload(vm, 3);
/* Create a new vector with that */
std::string key;
float data;
int step;
buzzobj_t tkey = buzzvm_stack_at(vm, 3);
buzzobj_t tdata = buzzvm_stack_at(vm, 2);
buzzobj_t tstep = buzzvm_stack_at(vm, 1);
if (tkey->o.type == BUZZTYPE_STRING) {
key = tkey->s.value.str;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"log_datum(key,data,step): expected %s, got %s in first argument",
buzztype_desc[BUZZTYPE_STRING],
buzztype_desc[tkey->o.type]
);
return vm->state;
}
if (tdata->o.type == BUZZTYPE_FLOAT) {
data = tdata->f.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"log_datum(key,data,step): expected %s, got %s in second argument",
buzztype_desc[BUZZTYPE_FLOAT],
buzztype_desc[tdata->o.type]
);
return vm->state;
}
if (tstep->o.type == BUZZTYPE_INT) {
step = tstep->i.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"log_datum(key,data,step): expected %s, got %s in second argument",
buzztype_desc[BUZZTYPE_INT],
buzztype_desc[tstep->o.type]
);
return vm->state;
}
/* Get pointer to the controller */
buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1));
buzzvm_gload(vm);
/* Call function */
reinterpret_cast<CBuzzControllerDroneSim*>(buzzvm_stack_at(vm, 1)->u.value)->LogDatum(key, data, step);
return buzzvm_ret0(vm);
}
/****************************************/
/****************************************/
static int BuzzLogDataSize(buzzvm_t vm){
/* Push the vector components */
buzzvm_lload(vm, 1);
buzzvm_lload(vm, 2);
/* Create a new vector with that */
std::string key;
float data;
int step;
int total_data;
buzzobj_t ttotal = buzzvm_stack_at(vm, 2);
buzzobj_t tstep = buzzvm_stack_at(vm, 1);
if (tstep->o.type == BUZZTYPE_INT) {
step = tstep->i.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"log_datum(key,data,step): expected %s, got %s in second argument",
buzztype_desc[BUZZTYPE_INT],
buzztype_desc[tstep->o.type]
);
return vm->state;
}
if (ttotal->o.type == BUZZTYPE_INT) {
total_data = ttotal->i.value;
} else {
buzzvm_seterror(vm,
BUZZVM_ERROR_TYPE,
"log_datum(key,data,step): expected %s, got %s in second argument",
buzztype_desc[BUZZTYPE_INT],
buzztype_desc[ttotal->o.type]
);
return vm->state;
}
/* Get pointer to the controller */
buzzvm_pushs(vm, buzzvm_string_register(vm, "controller", 1));
buzzvm_gload(vm);
/* Call function */
reinterpret_cast<CBuzzControllerDroneSim*>(buzzvm_stack_at(vm, 1)->u.value)->LogDataSize(total_data, step);
return buzzvm_ret0(vm);
}
/****************************************/
/************ Registration **************/
/****************************************/
buzzvm_state CBuzzControllerDroneSim::RegisterFunctions() {
CBuzzControllerKheperaIV::RegisterFunctions();
buzzvm_pushs(m_tBuzzVM, buzzvm_string_register(m_tBuzzVM, "uniform", 1));
buzzvm_pushcc(m_tBuzzVM, buzzvm_function_register(m_tBuzzVM, BuzzRandUniform));
buzzvm_gstore(m_tBuzzVM);
buzzvm_pushs(m_tBuzzVM, buzzvm_string_register(m_tBuzzVM, "gauss", 1));
buzzvm_pushcc(m_tBuzzVM, buzzvm_function_register(m_tBuzzVM, BuzzRandGauss));
buzzvm_gstore(m_tBuzzVM);
buzzvm_pushs(m_tBuzzVM, buzzvm_string_register(m_tBuzzVM, "has_reached", 1));
buzzvm_pushcc(m_tBuzzVM, buzzvm_function_register(m_tBuzzVM, BuzzHasReached));
buzzvm_gstore(m_tBuzzVM);
buzzvm_pushs(m_tBuzzVM, buzzvm_string_register(m_tBuzzVM, "get_current_key", 1));
buzzvm_pushcc(m_tBuzzVM, buzzvm_function_register(m_tBuzzVM, BuzzGetCurrentKey));
buzzvm_gstore(m_tBuzzVM);
buzzvm_pushs(m_tBuzzVM, buzzvm_string_register(m_tBuzzVM, "get_radiation_intensity", 1));
buzzvm_pushcc(m_tBuzzVM, buzzvm_function_register(m_tBuzzVM, BuzzGetRadiationIntensity));
buzzvm_gstore(m_tBuzzVM);
buzzvm_pushs(m_tBuzzVM, buzzvm_string_register(m_tBuzzVM, "log_datum", 1));
buzzvm_pushcc(m_tBuzzVM, buzzvm_function_register(m_tBuzzVM, BuzzLogDatum));
buzzvm_gstore(m_tBuzzVM);
buzzvm_pushs(m_tBuzzVM, buzzvm_string_register(m_tBuzzVM, "log_datasize", 1));
buzzvm_pushcc(m_tBuzzVM, buzzvm_function_register(m_tBuzzVM, BuzzLogDataSize));
buzzvm_gstore(m_tBuzzVM);
return m_tBuzzVM->state;
}
/****************************************/
/****************************************/
REGISTER_CONTROLLER(CBuzzControllerDroneSim, "buzz_controller_drone_sim");
}