Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1659 | giacomo | 1 | // framegrabber stuffs |
2 | |||
3 | /* File name ......... : ELABOR.C |
||
4 | * Project............ : |
||
5 | * Object ............ : |
||
6 | * Author ............ : Facchinetti Tullio |
||
7 | * Language .......... : C |
||
8 | * Compiler .......... : GNU C |
||
9 | * Operative system .. : MS-DOS/HARTIK |
||
10 | * Creation data ..... : 04/03/2000 |
||
11 | * Last modify ....... : 19/11/99 |
||
12 | */ |
||
13 | |||
14 | #include "demo.h" |
||
15 | #include "pclab.h" |
||
16 | |||
17 | #include <kernel/func.h> |
||
18 | #include <modules/cabs.h> |
||
19 | #include <stdio.h> |
||
20 | #include <drivers/pxc.h> |
||
21 | |||
22 | #include "string.h" |
||
23 | #include "ll/i386/x-dos.h" |
||
24 | #include "modules/hartport.h" |
||
25 | |||
26 | |||
27 | #define LUNGH 35. |
||
28 | |||
29 | |||
30 | // extern struct Data_cab2 tmp; |
||
31 | |||
32 | |||
33 | |||
34 | |||
35 | |||
36 | static CAB frameCAB; // CAB di deposito dgelle immagini |
||
37 | static CAB trackingCAB; // CAB di deposito delle info di tracking |
||
38 | |||
39 | |||
40 | int img_border = 10; // 10; |
||
41 | int img_border_y = 130; // new!! |
||
42 | int window_width = 40; //40; |
||
43 | int window_height = 40; //40; |
||
44 | TPixel pix_threshold = 128; |
||
45 | |||
46 | // a 256 GRAYscale palette |
||
47 | WORD GRAY_palette[256]; |
||
48 | |||
49 | // the image to be put on the screen |
||
50 | WORD converted_image[IMG_COL * IMG_ROW]; |
||
51 | |||
52 | TDataObj sequence[N_FRAMES]; |
||
53 | |||
54 | |||
55 | char scan_window_frame(TDataObj *data, TPixel *in_frame, \ |
||
56 | unsigned int xc, unsigned int yc, int border, int border_y) |
||
57 | { |
||
58 | unsigned long int offset; |
||
59 | unsigned int i, j; |
||
60 | TPixel pix; |
||
61 | double sum_x = 0.0, sum_y = 0.0; |
||
62 | unsigned int n_pix = 0; |
||
63 | int x1, y1, x2, y2; // Must be int!!! |
||
64 | char found; |
||
65 | |||
66 | data->x1 = N_COL; |
||
67 | data->y1 = N_ROW; |
||
68 | data->x2 = data->y2 = 0; |
||
69 | data->xb = data->yb = -1; |
||
70 | data->time_stamp = -1; |
||
71 | |||
72 | found = 0; |
||
73 | |||
74 | x1 = MAX_NUM((xc - window_width / 2), (border)); |
||
75 | y1 = MAX_NUM((yc - window_height / 2), (border_y)); |
||
76 | x2 = MIN_NUM((xc + window_width / 2), (N_COL - border)); |
||
77 | y2 = MIN_NUM((yc + window_height / 2), (N_ROW - border_y)); |
||
78 | |||
79 | for (i = y1; i < y2; i++) { |
||
80 | for (j = x1; j < x2; j++) { |
||
81 | offset = i * N_COL + j; |
||
82 | pix = *(in_frame + offset); |
||
83 | |||
84 | #ifdef __BLACK_ON_WHITE |
||
85 | // Pixel found (object is BLACK, background is WHITE) |
||
86 | if (pix < pix_threshold) { |
||
87 | #else |
||
88 | // Pixel found (object is WHITE, background is BLACK) |
||
89 | if (pix > pix_threshold) { |
||
90 | #endif |
||
91 | data->time_stamp = sys_gettime(NULL); |
||
92 | found = 1; |
||
93 | n_pix++; |
||
94 | sum_x += j; |
||
95 | sum_y += i; |
||
96 | // *(in_frame + offset) = 0; |
||
97 | if (i < data->y1) |
||
98 | data->y1 = i; |
||
99 | if (i > data->y2) |
||
100 | data->y2 = i; |
||
101 | if (j < data->x1) |
||
102 | data->x1 = j; |
||
103 | if (j > data->x2) |
||
104 | data->x2 = j; |
||
105 | |||
106 | } else { |
||
107 | // *(in_frame + offset) = 255; |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | data->xb = sum_x / n_pix; |
||
112 | data->yb = sum_y / n_pix; |
||
113 | return(found); |
||
114 | } |
||
115 | |||
116 | char scan_all_frame(TDataObj *data, TPixel *in_frame) |
||
117 | { |
||
118 | unsigned long int offset; |
||
119 | unsigned int i, j; |
||
120 | TPixel pix; |
||
121 | double sum_x = 0.0, sum_y = 0.0; |
||
122 | unsigned int n_pix = 0; |
||
123 | char found; |
||
124 | |||
125 | data->x1 = N_COL; |
||
126 | data->y1 = N_ROW; |
||
127 | data->x2 = data->y2 = 0; |
||
128 | data->xb = data->yb = -1; |
||
129 | data->time_stamp = -1; |
||
130 | |||
131 | found = 0; |
||
132 | |||
133 | // In a single image scanning it performs thresholding and computation |
||
134 | for (i = img_border_y; i < N_ROW - img_border_y; i++) { |
||
135 | for (j = img_border; j < N_COL - img_border; j++) { |
||
136 | offset = i * N_COL + j; |
||
137 | pix = *(in_frame + offset); |
||
138 | |||
139 | #ifdef __BLACK_ON_WHITE |
||
140 | // Pixel found (object is BLACK, background is WHITE) |
||
141 | if (pix < pix_threshold) { |
||
142 | #else |
||
143 | // Pixel found (object is WHITE, background is BLACK) |
||
144 | if (pix > pix_threshold) { |
||
145 | #endif |
||
146 | data->time_stamp = sys_gettime(NULL); |
||
147 | found = 1; |
||
148 | n_pix++; |
||
149 | sum_x += j; |
||
150 | sum_y += i; |
||
151 | // *(in_frame + offset) = 0; |
||
152 | if (i < data->y1) |
||
153 | data->y1 = i; |
||
154 | if (i > data->y2) |
||
155 | data->y2 = i; |
||
156 | if (j < data->x1) |
||
157 | data->x1 = j; |
||
158 | if (j > data->x2) |
||
159 | data->x2 = j; |
||
160 | |||
161 | } else { |
||
162 | // *(in_frame + offset) = 255; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | data->xb = sum_x / n_pix; |
||
167 | data->yb = sum_y / n_pix; |
||
168 | return(found); |
||
169 | } |
||
170 | |||
171 | void tracking(int top_frame, int *track_x, int *track_y, int *int_vx, int *int_vy, int time_to) |
||
172 | { |
||
173 | float vx, vy; |
||
174 | |||
175 | vx = (float)(sequence[top_frame - 1].xb - sequence[top_frame - 2].xb) / |
||
176 | (float)(sequence[top_frame - 1].time_stamp - sequence[top_frame - 2].time_stamp); |
||
177 | vx *= 1000; |
||
178 | |||
179 | vy = (float)(sequence[top_frame - 1].yb - sequence[top_frame - 2].yb) / |
||
180 | (float)(sequence[top_frame - 1].time_stamp - sequence[top_frame - 2].time_stamp); |
||
181 | vy *= 1000; |
||
182 | |||
183 | *track_x = sequence[top_frame - 1].xb + vx * time_to; |
||
184 | *track_y = sequence[top_frame - 1].yb + vy * time_to; |
||
185 | |||
186 | *int_vx = vx * 1000; |
||
187 | *int_vy = vy * 1000; |
||
188 | } |
||
189 | |||
190 | char found; |
||
191 | TPixel *grabber_frame; |
||
192 | int top_frame = 0; |
||
193 | TDataObj current; |
||
194 | TTracking *track; |
||
195 | void init_tracking_task(void) |
||
196 | { |
||
197 | frameCAB = PXC_GetCab(); |
||
198 | |||
199 | grabber_frame = cab_getmes(frameCAB); |
||
200 | |||
201 | // Executes first time |
||
202 | found = scan_all_frame(¤t, grabber_frame); |
||
203 | if (found) { |
||
204 | memcpy(&sequence[top_frame], ¤t, sizeof(TDataObj)); |
||
205 | top_frame++; |
||
206 | } |
||
207 | |||
208 | cab_unget(frameCAB, grabber_frame); |
||
209 | } |
||
210 | TASK tracking_task(void *arg) |
||
211 | { |
||
212 | // static unsigned int n_frame = 0; |
||
213 | /* char found; |
||
214 | TPixel *grabber_frame; |
||
215 | int top_frame = 0; |
||
216 | TDataObj current; |
||
217 | TTracking *track; |
||
218 | */ |
||
219 | /* frameCAB = PXC_GetCab(); |
||
220 | |||
221 | grabber_frame = cab_getmes(frameCAB); |
||
222 | |||
223 | // Executes first time |
||
224 | found = scan_all_frame(¤t, grabber_frame); |
||
225 | if (found) { |
||
226 | memcpy(&sequence[top_frame], ¤t, sizeof(TDataObj)); |
||
227 | top_frame++; |
||
228 | } |
||
229 | cab_unget(frameCAB, grabber_frame); |
||
230 | task_endcycle(); |
||
231 | */ |
||
232 | |||
233 | while (1) { |
||
234 | // Acquisizione immagine corrente |
||
235 | grabber_frame = (TPixel *)cab_getmes(frameCAB); |
||
236 | track = (TTracking *)cab_reserve(trackingCAB); |
||
237 | |||
238 | // Estrazione della nuova trasformata sul frame corrente |
||
239 | if (found) { |
||
240 | found = scan_window_frame(¤t, grabber_frame, current.xb, current.yb, img_border,img_border_y); |
||
241 | } else { |
||
242 | found = scan_all_frame(¤t, grabber_frame); |
||
243 | } |
||
244 | |||
245 | track->found = found; |
||
246 | |||
247 | if (found) { |
||
248 | if (top_frame < N_FRAMES) { |
||
249 | memcpy(&sequence[top_frame], ¤t, sizeof(TDataObj)); |
||
250 | top_frame++; |
||
251 | } else { |
||
252 | top_frame = 0; |
||
253 | memcpy(&sequence[top_frame], ¤t, sizeof(TDataObj)); |
||
254 | } |
||
255 | |||
256 | track->top_frame = top_frame; |
||
257 | memcpy(&track->current, ¤t, sizeof(TDataObj)); |
||
258 | |||
259 | if (top_frame > 1) { |
||
260 | tracking(top_frame, &track->predx, &track->predy, |
||
261 | &track->vx, &track->vy, 100); |
||
262 | } |
||
263 | } else { |
||
264 | track->top_frame = top_frame = 0; |
||
265 | } |
||
266 | |||
267 | |||
268 | |||
269 | |||
270 | // parte di acquisizione dal sensore x |
||
271 | { |
||
272 | float y[2], yp[2],x3ist=0; |
||
273 | long int k=0; |
||
274 | char *pun2; |
||
275 | struct Data_cab2 posiz; |
||
276 | float yout1; |
||
277 | |||
278 | |||
279 | task_nopreempt(); |
||
280 | yout1=ad_conv(11); |
||
281 | task_preempt(); |
||
282 | |||
283 | { // filtraggio dati ottenuti dal sensore di x |
||
284 | #define AVR 10 |
||
285 | int i; |
||
286 | static float oyout1[AVR]; |
||
287 | float avy; |
||
288 | static int index=0, flag=1; |
||
289 | |||
290 | if(flag==1) { |
||
291 | for(i=0; i<AVR;++i) { oyout1[i]=yout1;} |
||
292 | flag=0; |
||
293 | } |
||
294 | avy=0; |
||
295 | for(i=0;i<AVR;++i) { avy += oyout1[i]; } |
||
296 | avy /= AVR; |
||
297 | if(fabs(yout1-avy)>=prm.NOISE) { yout1=avy;} |
||
298 | oyout1[index]=yout1; |
||
299 | index = (index+1) % AVR ; |
||
300 | } |
||
301 | |||
302 | #ifndef CAMERA_X |
||
303 | x3ist=v2x(yout1); |
||
304 | y[0]=bass1(x3ist); |
||
305 | yp[0] = bass2(dx(y[0])); |
||
306 | posiz.x = y[0] ; posiz.y = yp[0] ; |
||
307 | #else |
||
308 | posiz.x=bass1( -(LUNGH/2.0)+ ((float)(current.x1 + current.x2))*LUNGH/( 2.0 *N_COL) ); |
||
309 | posiz.y=bass2(dx(posiz.x)); |
||
310 | // posiz.y=((float) (track->vx *LUNGH )) /(N_COL); |
||
311 | #endif |
||
312 | pun2 = cab_reserve(cab2); |
||
313 | memcpy(pun2, &posiz, sizeof(struct Data_cab2)); |
||
314 | cab_putmes(cab2, pun2); |
||
315 | } |
||
316 | |||
317 | // Release CABs |
||
318 | cab_putmes(trackingCAB, (char *)track); |
||
319 | cab_unget(frameCAB, grabber_frame); |
||
320 | |||
321 | |||
322 | task_endcycle(); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | |||
327 | |||
328 | |||
329 | |||
330 | |||
331 | |||
332 | /* |
||
333 | * |
||
334 | * |
||
335 | * |
||
336 | * Camera task |
||
337 | * |
||
338 | * |
||
339 | * |
||
340 | * |
||
341 | */ |
||
342 | |||
343 | |||
344 | TASK camera_task(void *arg) |
||
345 | { |
||
346 | register int i,j,col,row; |
||
347 | static unsigned int n_frame = 0; |
||
348 | TPixel *grabber_frame; |
||
349 | TTracking *track; |
||
350 | // char st[600]; |
||
351 | |||
352 | // Inizializzazione del task |
||
353 | frameCAB = PXC_GetCab(); |
||
354 | |||
355 | while (1) { |
||
356 | n_frame++; |
||
357 | |||
358 | |||
359 | /* Acquisizione immagine corrente */ |
||
360 | grabber_frame = cab_getmes(frameCAB); |
||
361 | |||
362 | |||
363 | |||
364 | for (i=0; i<IMG_ROW; i++) |
||
365 | for (j=0; j<IMG_COL; j++) { |
||
366 | col = (j*(N_COL-1))/(IMG_COL-1); |
||
367 | row = (i*(N_ROW-1))/(IMG_ROW-1); |
||
368 | converted_image[i*IMG_COL+j] = GRAY_palette[*(grabber_frame+row*N_COL+col)]; |
||
369 | } |
||
370 | |||
371 | // Release CAB |
||
372 | cab_unget(frameCAB, grabber_frame); |
||
373 | |||
374 | for (j=0; j<IMG_COL; j++) { |
||
375 | converted_image[j] = GRAY_palette[0]; |
||
376 | converted_image[(IMG_ROW-1)*IMG_COL+j] = GRAY_palette[0]; |
||
377 | } |
||
378 | /* scrive l'immagine della camera su video!!! */ |
||
379 | #ifdef PLOTIMG |
||
380 | mutex_lock(&mutex); |
||
381 | grx_putimage(IMG_X, IMG_Y, IMG_X+IMG_COL-1, IMG_Y+IMG_ROW-1,(BYTE *)converted_image); |
||
382 | mutex_unlock(&mutex); |
||
383 | #endif |
||
384 | |||
385 | mutex_lock(&mutex); |
||
386 | /* prende dati sul tracking !!! */ |
||
387 | track = (TTracking *)cab_getmes(trackingCAB); |
||
388 | /* scrive l'immagine del tracking su video!!! */ |
||
389 | if (track->found) { |
||
390 | |||
391 | if (track->top_frame > 1) { |
||
392 | int px, py; |
||
393 | if (track->predx < img_border) |
||
394 | px = img_border; |
||
395 | else if (track->predx > N_COL-img_border) |
||
396 | px = N_COL-img_border; |
||
397 | else |
||
398 | px = track->predx; |
||
399 | |||
400 | if (track->predy < img_border_y) |
||
401 | py = img_border_y; |
||
402 | else if (track->predy > N_ROW-img_border_y) |
||
403 | py = N_ROW-img_border_y; |
||
404 | else |
||
405 | py = track->predy; |
||
406 | #ifdef PLOTIMG |
||
407 | grx_disc(IMG_X+(px*IMG_COL)/N_COL, IMG_Y+(py*IMG_ROW)/N_ROW, 3, 127); |
||
408 | grx_rect(IMG_X+(track->current.x1*IMG_COL)/N_COL, IMG_Y+(track->current.y1*IMG_ROW)/N_ROW, IMG_X+(track->current.x2*IMG_COL)/N_COL, IMG_Y+(track->current.y2*IMG_ROW)/N_ROW, 127); |
||
409 | |||
410 | |||
411 | // grx_disc(IMG_X+(px*2)/3, IMG_Y+(py*2)/3, 3, 127); |
||
412 | // grx_rect(IMG_X+(track->current.x1*2)/3, IMG_Y+(track->current.y1*2)/3, IMG_X+(track->current.x2*2)/3, IMG_Y+(track->current.y2*2)/3, 127); |
||
413 | |||
414 | #endif |
||
415 | } |
||
416 | } |
||
417 | cab_unget(trackingCAB, (char *)track); |
||
418 | mutex_unlock(&mutex); |
||
419 | |||
420 | task_endcycle(); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | |||
425 | /* |
||
426 | * |
||
427 | * |
||
428 | * |
||
429 | * Framegrabber Initialization |
||
430 | * |
||
431 | * |
||
432 | * |
||
433 | * |
||
434 | */ |
||
435 | |||
436 | void start_listener(void); //(TIME p); |
||
437 | |||
438 | void framegrabber_close(void *arg) |
||
439 | { |
||
440 | PXC_Close(); |
||
441 | } |
||
442 | |||
443 | void init_framegrabber(void) |
||
444 | { |
||
445 | int i; |
||
446 | KEY_EVT my_key; |
||
447 | TIME period; |
||
448 | |||
449 | // Aggiusta la palette |
||
450 | for (i = 0; i < 256; i++) |
||
451 | GRAY_palette[i] = rgb16(i,i,i); |
||
452 | period = PXC_Initiate(4); |
||
453 | |||
454 | if (!period) { |
||
455 | grx_close(); |
||
456 | cprintf("Problemi nell'inizializz. del framegrabber. premi un tasto \n"); |
||
457 | keyb_getchar(); |
||
458 | halt(); |
||
459 | sys_end(); |
||
460 | } |
||
461 | else { |
||
462 | TTracking *trdata; |
||
463 | // tracking CAB init |
||
464 | trackingCAB = cab_create("trackingCAB", sizeof(TTracking), 3); |
||
465 | trdata = (TTracking *)cab_reserve(trackingCAB); |
||
466 | trdata->found = 0; |
||
467 | cab_putmes(trackingCAB, (char *)trdata); |
||
468 | |||
469 | |||
470 | PXC_Start(); |
||
471 | |||
472 | |||
473 | init_tracking_task(); |
||
474 | // start_listener(); |
||
475 | } |
||
476 | sys_atrunlevel(framegrabber_close, NULL, RUNLEVEL_BEFORE_EXIT); |
||
477 | } |
||
478 | |||
479 | /* crea i TASK tracking_task e camera_task */ |
||
480 | |||
481 | /* |
||
482 | void start_listener(void) //(TIME period) |
||
483 | { |
||
484 | SOFT_TASK_MODEL m1, m2; |
||
485 | HARD_TASK_MODEL m3; |
||
486 | |||
487 | |||
488 | |||
489 | #ifdef SOFTPROG |
||
490 | soft_task_default_model(m1); |
||
491 | soft_task_def_level(m1,1); |
||
492 | soft_task_def_met(m1,WCET_TRACKING); |
||
493 | soft_task_def_usemath(m1); |
||
494 | |||
495 | soft_task_def_period(m1,(PERIOD_TRACKING)); |
||
496 | soft_task_def_group(m1,1); |
||
497 | soft_task_def_ctrl_jet(m1); |
||
498 | // soft_task_def_skip_arrivals(m1); |
||
499 | tracking_PID = task_create("track", tracking_task, &m1, NULL); |
||
500 | if (tracking_PID == -1) { |
||
501 | sys_end(); |
||
502 | exit(4); |
||
503 | } |
||
504 | #else |
||
505 | hard_task_default_model(m3); |
||
506 | //hard_task_def_level(m3,1); |
||
507 | hard_task_def_wcet(m3,WCET_TRACKING); |
||
508 | hard_task_def_mit(m3,(PERIOD_TRACKING)); |
||
509 | hard_task_def_usemath(m3); |
||
510 | |||
511 | hard_task_def_group(m3,1); |
||
512 | hard_task_def_ctrl_jet(m3); |
||
513 | tracking_PID = task_create("track", tracking_task, &m3, NULL); |
||
514 | if (tracking_PID == -1) { |
||
515 | sys_end(); |
||
516 | exit(4); |
||
517 | } |
||
518 | #endif |
||
519 | |||
520 | |||
521 | soft_task_default_model(m2); |
||
522 | soft_task_def_level(m2,1); |
||
523 | soft_task_def_met(m2,WCET_CAMERA); |
||
524 | soft_task_def_usemath(m2); |
||
525 | |||
526 | soft_task_def_period(m2,PERIOD_CAMERA); |
||
527 | soft_task_def_group(m2,1); |
||
528 | soft_task_def_ctrl_jet(m2); |
||
529 | |||
530 | camera_PID = task_create("cam", camera_task, &m2, NULL); |
||
531 | if (camera_PID == -1) { |
||
532 | sys_end(); |
||
533 | exit(4); |
||
534 | } |
||
535 | |||
536 | |||
537 | } |
||
538 | |||
539 | |||
540 | */ |