diff options
author | Leo Chan <leochanj@live.unc.edu> | 2020-10-22 01:53:21 -0400 |
---|---|---|
committer | Joshua Bakita <jbakita@cs.unc.edu> | 2020-10-22 01:56:35 -0400 |
commit | d17b33131c14864bd1eae275f49a3f148e21cf29 (patch) | |
tree | 0d8f77922e8d193cb0f6edab83018f057aad64a0 /SD-VBS/benchmarks/localization/src/c/script_localization.c | |
parent | 601ed25a4c5b66cb75315832c15613a727db2c26 (diff) |
Squashed commit of the sb-vbs branch.
Includes the SD-VBS benchmarks modified to:
- Use libextra to loop as realtime jobs
- Preallocate memory before starting their main computation
- Accept input via stdin instead of via argc
Does not include the SD-VBS matlab code.
Fixes libextra execution in LITMUS^RT.
Diffstat (limited to 'SD-VBS/benchmarks/localization/src/c/script_localization.c')
-rw-r--r-- | SD-VBS/benchmarks/localization/src/c/script_localization.c | 534 |
1 files changed, 534 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/localization/src/c/script_localization.c b/SD-VBS/benchmarks/localization/src/c/script_localization.c new file mode 100644 index 0000000..3392320 --- /dev/null +++ b/SD-VBS/benchmarks/localization/src/c/script_localization.c | |||
@@ -0,0 +1,534 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include <stdio.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <malloc.h> | ||
8 | #include "localization.h" | ||
9 | #include "extra.h" | ||
10 | #define LOCOLIZATION_MEM 1<<24 | ||
11 | int main(int argc, char* argv[]) | ||
12 | { | ||
13 | SET_UP | ||
14 | mallopt(M_TOP_PAD, LOCOLIZATION_MEM); | ||
15 | mallopt(M_MMAP_MAX, 0); | ||
16 | int n, i, j, k, icount=-1; | ||
17 | F2D* fid; | ||
18 | float gyroTimeInterval=0.01; | ||
19 | float acclTimeInterval=0.01; | ||
20 | |||
21 | float STDDEV_GPSVel=0.5; | ||
22 | float STDDEV_ODOVel=0.1; | ||
23 | float STDDEV_ACCL=1; | ||
24 | float M_STDDEV_GYRO=0.1; | ||
25 | float M_STDDEV_POS=0.1; | ||
26 | float M_STDDEV_VEL=0.02; | ||
27 | |||
28 | F2D *pos, *vel; | ||
29 | float pi = 3.1416; | ||
30 | F2D *eul1, *eul2, *quat; | ||
31 | F2D *sData, *gyro, *norm_gyro, *angleAlpha; | ||
32 | F2D *quatDelta, *Opos, *temp_STDDEV_GPSPos, *w; | ||
33 | F2D *qConj, *orgWorld, *accl, *gtemp; | ||
34 | F2D *gravity, *t1; | ||
35 | I2D *tStamp, *sType, *isEOF; | ||
36 | I2D *index; | ||
37 | int rows, cols; | ||
38 | F2D *resultMat; | ||
39 | F2D *STDDEV_GPSPos; | ||
40 | F2D *ones, *randW; | ||
41 | |||
42 | char im1[100]; | ||
43 | |||
44 | printf("Input txt File: "); | ||
45 | scanf("%s", im1); | ||
46 | |||
47 | fid = readFile(im1); | ||
48 | |||
49 | n = 1000; | ||
50 | |||
51 | #ifdef test | ||
52 | n = 3; | ||
53 | gyroTimeInterval = 0.1; | ||
54 | acclTimeInterval = 0.1; | ||
55 | M_STDDEV_VEL = 0.2; | ||
56 | #endif | ||
57 | #ifdef sim_fast | ||
58 | n = 3; | ||
59 | #endif | ||
60 | #ifdef sim | ||
61 | n = 10; | ||
62 | #endif | ||
63 | #ifdef sqcif | ||
64 | n = 800; | ||
65 | #endif | ||
66 | #ifdef qcif | ||
67 | n = 500; | ||
68 | #endif | ||
69 | #ifdef vga | ||
70 | n = 2000; | ||
71 | #endif | ||
72 | #ifdef wuxga | ||
73 | n = 3000; | ||
74 | #endif | ||
75 | |||
76 | resultMat = fSetArray(3,fid->height, 0); | ||
77 | |||
78 | pos = fSetArray(n, 3, 0); | ||
79 | vel = fSetArray(n, 3, 0); | ||
80 | ones = fSetArray(n,1,1); | ||
81 | |||
82 | |||
83 | F2D *randn; | ||
84 | |||
85 | randn = randWrapper(n,3); | ||
86 | printf("start\n"); | ||
87 | for_each_job{ | ||
88 | fResetArray(pos,n, 3, 0); | ||
89 | fResetArray(vel,n, 3, 0); | ||
90 | fResetArray(ones,n,1,1); | ||
91 | |||
92 | { | ||
93 | int j; | ||
94 | for(i=0; i<n; i++) | ||
95 | for(j=0; j<3; j++) | ||
96 | subsref(vel, i, j) += subsref(randn,i,j) * STDDEV_ODOVel; | ||
97 | |||
98 | } | ||
99 | |||
100 | |||
101 | |||
102 | { | ||
103 | F2D *eulAngle, *randn; | ||
104 | eulAngle = fSetArray(n, 3, 0); | ||
105 | randn = randWrapper(n,1); | ||
106 | |||
107 | for(i=0; i<n; i++) | ||
108 | { | ||
109 | subsref(eulAngle, i, 2) = subsref(randn, i, 0) * 2 * pi; | ||
110 | } | ||
111 | |||
112 | eul1 = eul2quat(eulAngle); | ||
113 | fFreeHandle(eulAngle); | ||
114 | |||
115 | eulAngle = fSetArray(1, 3, 0); | ||
116 | subsref(eulAngle, 0, 0) = pi; | ||
117 | eul2 = eul2quat(eulAngle); | ||
118 | |||
119 | fFreeHandle(randn); | ||
120 | fFreeHandle(eulAngle); | ||
121 | } | ||
122 | |||
123 | quat = quatMul(eul1, eul2); | ||
124 | fFreeHandle(eul1); | ||
125 | fFreeHandle(eul2); | ||
126 | |||
127 | i=0; | ||
128 | index = iSetArray(1,1,-1); | ||
129 | sType = iSetArray(1,1,-1); | ||
130 | isEOF = iSetArray(1,1,-1); | ||
131 | |||
132 | |||
133 | rows =0; | ||
134 | cols = 5; | ||
135 | STDDEV_GPSPos = fSetArray(3,3,0); | ||
136 | randW = randnWrapper(n,3); | ||
137 | icount=-1; | ||
138 | while(1) | ||
139 | { | ||
140 | icount=icount+1; | ||
141 | |||
142 | /* | ||
143 | S = 4 | ||
144 | A = 3 | ||
145 | G = 2 | ||
146 | V = 1 | ||
147 | */ | ||
148 | |||
149 | sData = readSensorData(index, fid, sType, isEOF); | ||
150 | rows++; | ||
151 | |||
152 | |||
153 | |||
154 | if( asubsref(sType,0) ==2) | ||
155 | { | ||
156 | |||
157 | //Motion model | ||
158 | |||
159 | { | ||
160 | int i; | ||
161 | F2D *t, *t1; | ||
162 | F2D *abc, *abcd; | ||
163 | int qD_r=0, qD_c=0; | ||
164 | F2D *cosA, *sinA; | ||
165 | |||
166 | t = fDeepCopyRange(sData, 0, 1, 0, 3); | ||
167 | gyro = fMtimes(ones, t); | ||
168 | abc = fMallocHandle(gyro->height, gyro->width); | ||
169 | t1 = fDeepCopy(randW); | ||
170 | |||
171 | for(i=0; i<(n*3); i++) | ||
172 | { | ||
173 | asubsref(t1,i) = asubsref(randW,i) * M_STDDEV_GYRO; | ||
174 | asubsref(gyro, i) += asubsref(t1,i); | ||
175 | asubsref(abc, i) = pow(asubsref(gyro, i), 2); | ||
176 | } | ||
177 | fFreeHandle(t1); | ||
178 | abcd = fSum2(abc, 2); | ||
179 | |||
180 | norm_gyro = fMallocHandle(abcd->height,abcd->width); | ||
181 | angleAlpha = fMallocHandle(abcd->height, abcd->width); | ||
182 | |||
183 | for(i=0; i<(abcd->height*abcd->width); i++) | ||
184 | { | ||
185 | asubsref(norm_gyro, i) = sqrt(asubsref(abcd,i)); | ||
186 | asubsref(angleAlpha,i) = asubsref(norm_gyro,i) * gyroTimeInterval; | ||
187 | } | ||
188 | |||
189 | qD_r += angleAlpha->height + gyro->height; | ||
190 | qD_c += angleAlpha->width + 3; | ||
191 | |||
192 | fFreeHandle(t); | ||
193 | fFreeHandle(abcd); | ||
194 | |||
195 | cosA = fSetArray(angleAlpha->height, angleAlpha->width, 0); | ||
196 | sinA = fSetArray(angleAlpha->height, angleAlpha->width, 0); | ||
197 | |||
198 | for(i=0; i<(cosA->height*cosA->width); i++) | ||
199 | asubsref(cosA,i) = cos( asubsref(angleAlpha,i) /2 ); | ||
200 | |||
201 | for(i=0; i<(sinA->height*sinA->width); i++) | ||
202 | asubsref(sinA,i) = sin( asubsref(angleAlpha,i) /2 ); | ||
203 | |||
204 | |||
205 | fFreeHandle(abc); | ||
206 | abc = fSetArray(1,3,1); | ||
207 | t1 = fMtimes(norm_gyro, abc); | ||
208 | t = ffDivide(gyro, t1); | ||
209 | fFreeHandle(t1); | ||
210 | |||
211 | abcd = fMtimes(sinA, abc); | ||
212 | t1 = fTimes(t, abcd); | ||
213 | quatDelta = fHorzcat(cosA, t1); | ||
214 | |||
215 | fFreeHandle(abcd); | ||
216 | fFreeHandle(t); | ||
217 | fFreeHandle(t1); | ||
218 | fFreeHandle(abc); | ||
219 | |||
220 | t = quatMul(quat, quatDelta); | ||
221 | fFreeHandle(quat); | ||
222 | fFreeHandle(quatDelta); | ||
223 | quat = fDeepCopy(t); | ||
224 | |||
225 | fFreeHandle(t); | ||
226 | fFreeHandle(norm_gyro); | ||
227 | fFreeHandle(gyro); | ||
228 | fFreeHandle(angleAlpha); | ||
229 | fFreeHandle(cosA); | ||
230 | fFreeHandle(sinA); | ||
231 | |||
232 | } | ||
233 | } | ||
234 | |||
235 | if( asubsref(sType,0) ==4) | ||
236 | { | ||
237 | //Observation | ||
238 | |||
239 | float tempSum=0; | ||
240 | F2D *Ovel; | ||
241 | float OvelNorm; | ||
242 | int i; | ||
243 | |||
244 | |||
245 | asubsref(STDDEV_GPSPos, 0) = asubsref(sData, 6); | ||
246 | asubsref(STDDEV_GPSPos, 4) = asubsref(sData,7); | ||
247 | asubsref(STDDEV_GPSPos, 8) = 15; | ||
248 | |||
249 | Opos = fDeepCopyRange(sData, 0, 1, 0, 3); | ||
250 | |||
251 | //Initialize | ||
252 | |||
253 | for(i=0; i<(pos->height*pos->width); i++) | ||
254 | tempSum += asubsref(pos,i); | ||
255 | |||
256 | if(tempSum == 0) | ||
257 | { | ||
258 | F2D *t, *t1; | ||
259 | t = fMtimes( randW, STDDEV_GPSPos); | ||
260 | t1 = fMtimes(ones, Opos); | ||
261 | |||
262 | for(i=0; i<(pos->height*pos->width); i++) | ||
263 | asubsref(pos,i) = asubsref(t,i) + asubsref(t1,i); | ||
264 | |||
265 | fFreeHandle(t); | ||
266 | fFreeHandle(t1); | ||
267 | } | ||
268 | else | ||
269 | { | ||
270 | int rows, cols; | ||
271 | int mnrows, mncols; | ||
272 | |||
273 | rows = STDDEV_GPSPos->height; | ||
274 | cols = STDDEV_GPSPos->width; | ||
275 | |||
276 | temp_STDDEV_GPSPos = fSetArray(rows,cols,1); | ||
277 | for( mnrows=0; mnrows<rows; mnrows++) | ||
278 | { | ||
279 | for( mncols=0; mncols<cols; mncols++) | ||
280 | { | ||
281 | subsref(temp_STDDEV_GPSPos,mnrows,mncols) = pow(subsref(STDDEV_GPSPos,mnrows,mncols),-1); | ||
282 | } | ||
283 | } | ||
284 | |||
285 | w = mcl(pos, Opos , temp_STDDEV_GPSPos); | ||
286 | generateSample(w, quat, vel, pos); | ||
287 | } | ||
288 | fFreeHandle(Opos); | ||
289 | |||
290 | //compare direction | ||
291 | Ovel = fDeepCopyRange(sData, 0, 1, 3, 3); | ||
292 | OvelNorm=2;//1.1169e+09; | ||
293 | |||
294 | if (OvelNorm>0.5) | ||
295 | { | ||
296 | F2D *t; | ||
297 | t = fDeepCopy(Ovel); | ||
298 | fFreeHandle(Ovel); | ||
299 | /* This is a double precision division */ | ||
300 | Ovel = fDivide(t, OvelNorm); | ||
301 | qConj = quatConj(quat); | ||
302 | fFreeHandle(t); | ||
303 | |||
304 | { | ||
305 | t = fSetArray(1,3,0); | ||
306 | subsref(t,0,0) = 1; | ||
307 | orgWorld = quatRot(t, qConj); | ||
308 | fFreeHandle(t); | ||
309 | fFreeHandle(qConj); | ||
310 | t = fSetArray(3,3,0); | ||
311 | asubsref(t,0) = 1; | ||
312 | asubsref(t,4) = 1; | ||
313 | asubsref(t,8) = 1; | ||
314 | |||
315 | { | ||
316 | int i; | ||
317 | for(i=0; i<(t->height*t->width); i++) | ||
318 | asubsref(t, i) = asubsref(t,i)/STDDEV_GPSVel; | ||
319 | w = mcl( orgWorld, Ovel, t); | ||
320 | generateSample(w, quat, vel, pos); | ||
321 | } | ||
322 | |||
323 | fFreeHandle(t); | ||
324 | fFreeHandle(w); | ||
325 | fFreeHandle(orgWorld); | ||
326 | } | ||
327 | } | ||
328 | fFreeHandle(Ovel); | ||
329 | } | ||
330 | |||
331 | if( asubsref(sType,0) ==1) | ||
332 | { | ||
333 | //Observation | ||
334 | F2D *Ovel; | ||
335 | F2D *t, *t1, *t2; | ||
336 | float valVel; | ||
337 | |||
338 | t = fSetArray(vel->height, 1, 0); | ||
339 | |||
340 | for(i=0; i<vel->height; i++) | ||
341 | { | ||
342 | subsref(t,i,0) = sqrt(pow(subsref(vel,i,0),2) + pow(subsref(vel,i,1),2) + pow(subsref(vel,i,2),2)); | ||
343 | } | ||
344 | |||
345 | Ovel = fSetArray(1, 1, asubsref(sData,0)); | ||
346 | valVel = 1.0/STDDEV_ODOVel; | ||
347 | |||
348 | t1 = fSetArray(1,1,(1.0/STDDEV_ODOVel)); | ||
349 | w = mcl (t, Ovel, t1); | ||
350 | generateSample(w, quat, vel, pos); | ||
351 | |||
352 | |||
353 | fFreeHandle(w); | ||
354 | fFreeHandle(t); | ||
355 | fFreeHandle(t1); | ||
356 | fFreeHandle(Ovel); | ||
357 | } | ||
358 | |||
359 | if( asubsref(sType,0) ==3) | ||
360 | { | ||
361 | //Observation | ||
362 | F2D *t; | ||
363 | t = fSetArray(1, 3, 0); | ||
364 | asubsref(t,2) = -9.8; | ||
365 | |||
366 | accl = fDeepCopyRange(sData, 0, 1, 0, 3); | ||
367 | gtemp = fMtimes( ones, t); | ||
368 | gravity = quatRot(gtemp, quat); | ||
369 | |||
370 | fFreeHandle(gtemp); | ||
371 | fFreeHandle(t); | ||
372 | t = fSetArray(3,3,0); | ||
373 | asubsref(t,0) = 1; | ||
374 | asubsref(t,4) = 1; | ||
375 | asubsref(t,8) = 1; | ||
376 | |||
377 | { | ||
378 | int i; | ||
379 | for(i=0; i<(t->height*t->width); i++) | ||
380 | asubsref(t,i) = asubsref(t,i)/STDDEV_ACCL; | ||
381 | w = mcl( gravity, accl, t); | ||
382 | } | ||
383 | |||
384 | generateSample(w, quat, vel, pos); | ||
385 | fFreeHandle(t); | ||
386 | //Motion model | ||
387 | t = fMtimes(ones, accl); | ||
388 | fFreeHandle(accl); | ||
389 | |||
390 | accl = fMinus(t, gravity); | ||
391 | |||
392 | fFreeHandle(w); | ||
393 | fFreeHandle(gravity); | ||
394 | fFreeHandle(t); | ||
395 | |||
396 | { | ||
397 | //pos=pos+quatRot(vel,quatConj(quat))*acclTimeInterval+1/2*quatRot(accl,quatConj(quat))*acclTimeInterval^2+randn(n,3)*M_STDDEV_POS; | ||
398 | |||
399 | F2D *s, *is; | ||
400 | int i; | ||
401 | is = quatConj(quat); | ||
402 | s = quatRot(vel, is); | ||
403 | fFreeHandle(is); | ||
404 | |||
405 | for(i=0; i<(s->height*s->width); i++) | ||
406 | { | ||
407 | asubsref(s,i) = asubsref(s,i)*acclTimeInterval; //+(1/2); | ||
408 | } | ||
409 | is = fPlus(pos, s); | ||
410 | fFreeHandle(pos); | ||
411 | pos = fDeepCopy(is); | ||
412 | fFreeHandle(is); | ||
413 | fFreeHandle(s); | ||
414 | |||
415 | /** pos_ above stores: pos+quatRot(vel,quatConj(quat))*acclTimeInterval **/ | ||
416 | |||
417 | is = quatConj(quat); | ||
418 | s = quatRot(accl, is); | ||
419 | t = fDeepCopy(s); | ||
420 | for(i=0; i<(s->height*s->width); i++) | ||
421 | { | ||
422 | asubsref(t,i) = 1/2*asubsref(s,i)*acclTimeInterval*acclTimeInterval; | ||
423 | } | ||
424 | |||
425 | /** t_ above stores: 1/2*quatRot(accl,quatCong(quat))*acclTimeInterval^2 **/ | ||
426 | |||
427 | fFreeHandle(s); | ||
428 | fFreeHandle(is); | ||
429 | s = randnWrapper(n,3); | ||
430 | |||
431 | for(i=0; i<(s->height*s->width); i++) | ||
432 | { | ||
433 | asubsref(s,i) = asubsref(s,i) * M_STDDEV_POS; | ||
434 | } | ||
435 | |||
436 | /** s_ above stores: randn(n,3)*M_STDDEV_POS **/ | ||
437 | |||
438 | is = fPlus(pos, t); | ||
439 | fFreeHandle(pos); | ||
440 | pos = fPlus(is, s); | ||
441 | |||
442 | fFreeHandle(s); | ||
443 | fFreeHandle(t); | ||
444 | fFreeHandle(is); | ||
445 | |||
446 | // vel=vel+accl*acclTimeInterval+randn(n,3)*M_STDDEV_VEL; %?? | ||
447 | |||
448 | t = fDeepCopy(accl); | ||
449 | for(i=0; i<(accl->height*accl->width); i++) | ||
450 | asubsref(t,i) = asubsref(accl,i) * acclTimeInterval; | ||
451 | |||
452 | is = fPlus(vel, t); | ||
453 | |||
454 | fFreeHandle(accl); | ||
455 | fFreeHandle(t); | ||
456 | s = randnWrapper(n,3); | ||
457 | for(i=0; i<(s->height*s->width); i++) | ||
458 | { | ||
459 | asubsref(s,i) = asubsref(s,i) * M_STDDEV_VEL; | ||
460 | } | ||
461 | |||
462 | fFreeHandle(vel); | ||
463 | vel = fPlus(is, s); | ||
464 | fFreeHandle(is); | ||
465 | fFreeHandle(s); | ||
466 | } | ||
467 | |||
468 | } | ||
469 | |||
470 | // Self check | ||
471 | { | ||
472 | F2D* temp; | ||
473 | float quatOut=0, velOut=0, posOut=0; | ||
474 | int i; | ||
475 | |||
476 | for(i=0; i<(quat->height*quat->width); i++) | ||
477 | quatOut += asubsref(quat, i); | ||
478 | |||
479 | for(i=0; i<(vel->height*vel->width); i++) | ||
480 | velOut += asubsref(vel, i); | ||
481 | |||
482 | for(i=0; i<(pos->height*pos->width); i++) | ||
483 | posOut += asubsref(pos, i); | ||
484 | |||
485 | subsref(resultMat,0,icount) = quatOut; | ||
486 | subsref(resultMat,1,icount) = velOut; | ||
487 | subsref(resultMat,2,icount) = posOut; | ||
488 | } | ||
489 | |||
490 | fFreeHandle(sData); | ||
491 | |||
492 | |||
493 | if (asubsref(isEOF,0) == 1) | ||
494 | break; | ||
495 | } | ||
496 | } | ||
497 | printf("end..\n"); | ||
498 | |||
499 | #ifdef CHECK | ||
500 | |||
501 | // Self checking - use expected.txt from data directory | ||
502 | { | ||
503 | int ret=0; | ||
504 | float tol = 2.0; | ||
505 | #ifdef GENERATE_OUTPUT | ||
506 | fWriteMatrix(resultMat, argv[1]); | ||
507 | #endif | ||
508 | ret = fSelfCheck(resultMat, "./expected_C.txt", tol); | ||
509 | if (ret == -1) | ||
510 | printf("Error in Localization\n"); | ||
511 | } | ||
512 | // Self checking done | ||
513 | #endif | ||
514 | |||
515 | |||
516 | fFreeHandle(STDDEV_GPSPos); | ||
517 | iFreeHandle(index); | ||
518 | iFreeHandle(sType); | ||
519 | iFreeHandle(isEOF); | ||
520 | fFreeHandle(fid); | ||
521 | fFreeHandle(resultMat); | ||
522 | fFreeHandle(pos); | ||
523 | fFreeHandle(vel); | ||
524 | fFreeHandle(quat); | ||
525 | fFreeHandle(ones); | ||
526 | fFreeHandle(randW); | ||
527 | fFreeHandle(randn); | ||
528 | WRITE_TO_FILE | ||
529 | return 0; | ||
530 | |||
531 | } | ||
532 | |||
533 | |||
534 | |||