]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/camera/barcam/barcam.cxx
0fd5bec944894c91fc7ca0b20f377f424862d12d
[eurobot/public.git] / src / camera / barcam / barcam.cxx
1 /*******************************************************************************
2  * 
3  * barcam - OpenCV based camera recognition program.
4  *
5  * Created for Eurobot 2010 competition.
6  *
7  * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
8  * rewriten by Michal Vokac (vokac.m@gmail.com), for Eurobot 2011
9  *
10  * This program is used to recognize type of playing elements (pawns, qeens,
11  * kings) in Eurobot 2011 competition. 
12  * Program can switch between several modes, also depending on platform - on PPC,
13  * only modes RECOGNIZE and WAIT are present, on PC, there are also VIDEO and
14  * IMAGE modes. Lets introduce them:
15  *  RECOGNIZE: Camera image is processed and playing element type is recognized.
16  *    The results are published continually using orte. On PC, the result and
17  *    some subresults are displayed graphically.
18  *  WAIT: Program waits until orte.camera_control.on is set to true, or, on PC,
19  *    waiting is interrupted by W key. This is the default mode for PPC.
20  *  VIDEO (PC only): Video from camera is just displayed in a window. Default
21  *    mode for PC. Use R key to switch to RECOGNIZE mode.
22  *  IMAGE (PC only): Pseudo-mode used to analyze *one* image loaded from file.
23  *    Is useful also for finding a good threshold value, using +/- keys. To use
24  *    this mode, start the program with -i argument (see below). There is no way
25  *    to switch from this mode to another one.
26  * 
27  * +++ PC +++
28  * Usage: ./barcam [-i filename]
29  *  -i:
30  *   Start in image mode, load filename to analyze.
31  *
32  * +++ PPC +++
33  * Usage: ./barcam
34  *   No arguments possible.
35  * 
36  ******************************************************************************/
37 #define BARCAM_DEBUG
38
39 #ifdef BARCAM_DEBUG
40 #define WITH_GUI
41 #endif
42
43 /******************************************************************************/
44
45 #include <inttypes.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <sys/stat.h>
49 #include <semaphore.h>
50 #include <getopt.h>
51 #include <unistd.h>
52 #include <fftw3.h>
53 #include <string>
54
55 #include <opencv/cv.h>
56 #include <opencv/highgui.h>
57
58 // gnuplot fifo plot
59 #include <c2gnuplot.h>
60
61 extern "C" {
62 #include <roboorte_robottype.h>
63 //#include <robot.h>
64 }
65
66 /******************************************************************************/
67 /***************************** macro definitions ******************************/
68 /******************************************************************************/
69
70 //uncomment next line to "log" the output frames - save them as pnm to the working directory
71 //#define PPC_DEBUG
72
73 // modes definitions
74 #define MODE_QUIT         0x01
75 #define MODE_VIDEO        0x02
76 #define MODE_RECOGNIZE    0x04
77 #define MODE_WAIT         0x08
78 #define MODE_IMAGE        0x10
79
80 // highgui windows names
81 #define WINDOW_ORIG       "BARCAM original scene"
82 #define WINDOW_PROD       "BARCAM recognition product"
83
84 // size of graphical windows
85 #define WINDOW_WIDTH      640
86 #define WINDOW_HEIGHT     480
87
88 //TODO tune size of region of interest (crop camera view)
89 #define ROI_X      0 
90 #define ROI_Y      0
91 #define ROI_WIDTH      WINDOW_WIDTH
92 #define ROI_HEIGHT     WINDOW_HEIGHT 
93
94 // values of keys
95 #define KEY_ESC           0x1B
96 #define KEY_SPACE         0x20
97 #define KEY_O             0x4f
98 #define KEY_P             0x50
99 #define KEY_R             0x52
100 #define KEY_S             0x53
101 #define KEY_V             0x56
102 #define KEY_W             0x57
103 #define KEY_o             0x6f
104 #define KEY_p             0x70
105 #define KEY_r             0x72
106 #define KEY_s             0x73
107 #define KEY_v             0x76
108 #define KEY_w             0x77
109 #define KEY_PLUS          0x2B
110 #define KEY_MINUS         0x2D
111 #define KEY_1             0x31
112 #define KEY_2             0x32
113 #define KEY_3             0x33
114 #define KEY_4             0x34
115 #define KEY_5             0x35
116 #define KEY_6             0x36
117 #define KEY_7             0x37
118 #define KEY_8             0x38
119
120
121 // filename pattern of snapshots (PPC does not support png)
122 #ifdef WITH_GUI
123 #define SNAPSHOTFILENAME  "snapshot%02d.png"
124 #else /* PPC */
125 #define SNAPSHOTFILENAME  "snapshot%02d.pnm"
126 #endif /* WITH_GUI */
127
128
129 /******************************************************************************/
130 /************************** function declarations *****************************/
131 /******************************************************************************/
132
133 /********************** multimode graphical functions *************************/
134 int saveFrame(const CvArr *img);
135 void selectROI(CvCapture* capture, CvRect *roi);
136
137 /******************** multimode computational functions ***********************/
138 int countThreshold(const IplImage *frame);
139 int recognize(const CvMat *frame);
140
141 /********************************** MODE_IMAGE ********************************/
142 int modeImage(char *imageFilename);
143
144 /********************************* MODE_VIDEO *********************************/
145 int modeVideo(CvCapture* capture, CvRect *roi);
146
147 /****************************** MODE_RECOGNIZE ********************************/
148 int recognizeMode_processKeys(IplImage *frame);
149 void displayFrames(IplImage *frame);
150 int modeRecognize(CvCapture* capture, CvRect *roi);
151
152 /********************************* MODE_WAIT **********************************/
153 int waitMode_processKeys(int previousMode);
154 int modeWait(int previousMode);
155
156 /******************************** mode manager ********************************/
157 void setAnalyticWindowsVisible(bool visible);
158 void destroyGUI(void);
159 int modeManager(int defaultMode, char *paramC = NULL);
160
161 /*********************************** orte *************************************/
162 bool getCameraControlOn(void);
163 void send_cmr_res_cb(const ORTESendInfo *info, void *vinstance, void *recvCallBackParam);
164 void rcv_cmr_ctrl_cb(const ORTERecvInfo *info, void *vinstance, void *recvCallBackParam);
165 void rcv_robot_switches_cb(const ORTERecvInfo *info, void *vinstance, void *recvCallBackParam);
166
167 /********************************* application ********************************/
168 int getParamI(int argc, char *argv[], char *imgFilename);
169 int main(int argc, char *argv[]);
170
171
172 /******************************************************************************/
173 /**************************** variable declarations ***************************/
174 /******************************************************************************/
175
176 struct robottype_orte_data orte;
177 bool camera_control_on = false;
178
179 CvFont fontLarge;
180
181
182 /******************************************************************************/
183 /********************** multimode graphical functions *************************/
184 /******************************************************************************/
185
186 /** Saves current image to a new file in the current directory.
187   * @param img Image to save to file.
188   * @return Value returned by cvSaveImage. */
189 int saveFrame(const CvArr *img)
190 {
191         struct stat stFileInfo;
192         char filename[30];
193         int i = 0;
194
195         //find a non-existent filename (e.g. "snapshot13.png")
196         do {
197                 sprintf(filename, SNAPSHOTFILENAME, i++);
198         } while (!stat(filename, &stFileInfo));
199
200         fprintf(stdout, "Saving frame to %s...\n", filename);
201         return cvSaveImage(filename, img);
202 }
203
204 /******************************************************************************/
205
206 /** Setting region of interest in the ROI window.
207   * @param capture Pointer to a camera capture.
208   * @param roi Pointer to ROI rectangle.   */
209 #ifdef WITH_GUI
210 void selectROI(CvCapture* capture, CvRect *roi)
211 {
212   IplImage* frame = NULL;
213   int step = 10;
214   
215   while (1) {
216     // wait 10ms for an event
217     switch(cvWaitKey(10) & 0xFF) {
218       // exit ROI setting
219       case KEY_ESC:
220         return;
221         
222       case KEY_PLUS:
223         // increase ROI step
224         step++;
225         break;
226         
227       case KEY_MINUS:
228         // decrease ROI step
229         step--;
230         break;
231         
232       case KEY_R:
233       case KEY_r:
234         // reset ROI to full frame
235         roi->x = 0;
236         roi->y = 0;
237         roi->width = WINDOW_WIDTH;
238         roi->height = WINDOW_HEIGHT;
239         break;
240         
241       case KEY_1:
242         // increase left border
243         roi->x -= step;
244         roi->width += step;
245         break;
246         
247       case KEY_2:
248         // decrease left border
249         roi->x += step;
250         roi->width -= step;
251         break;
252         
253       case KEY_3:
254         // increase upper border
255         roi->y -= step;
256         roi->height += step;
257         break;
258         
259       case KEY_4:
260         // decrease upper border
261         roi->y += step;
262         roi->height -= step;
263         break;
264         
265       case KEY_5:
266         // decrease lower border
267         roi->height -= step;
268         break;
269         
270       case KEY_6:
271         // increase lower border
272         roi->height += step;
273         break;
274         
275       case KEY_7:
276         // decrease right border
277         roi->width -= step;
278         break;
279         
280       case KEY_8:
281         // increase right border
282         roi->width += step;
283         break;
284     }
285     
286     step = (step < 0 ? 0 : step);
287     
288     fprintf(stderr, "ROI step: %d x: %d y: %d width: %d height: %d\n",
289             step, roi->x, roi->y, roi->width, roi->height);
290     
291     frame = cvQueryFrame(capture);
292     
293     cvPutText(frame, "Select ROI", cvPoint(frame->width/2 -60,
294                                            frame->height - 50),
295               &fontLarge, cvScalar(0,0,255));
296     
297     cvRectangle(frame, cvPoint(roi->x, roi->y), cvPoint(roi->width + roi->x,
298                                                         roi->height + roi->y),
299                 cvScalar(0,0,255), 2, 8, 0);
300     cvShowImage(WINDOW_ORIG, frame);
301       
302   }
303 }
304 #else /* PPC */
305
306 void selectROI(CvCapture *capture, CvRect *roi) {}
307 #endif /* WITH_GUI */
308
309 /******************************************************************************/
310 /******************** multimode computational functions ***********************/
311 /******************************************************************************/
312
313 /** Returns a threshold computed from the frame.
314   * @param frame Frame to compute a threshold from.
315   * @return Mean of all pixels of the frame. */
316 int countThreshold(const IplImage *frame)
317 {
318         return cvAvg(frame).val[0];
319 }
320
321 /******************************************************************************/
322
323 /** Returns an ordinary number of recognized configuration.
324   * @param frame Frame to be processed.
325   * @return  */
326 int recognize(IplImage* gray_frame) {
327   
328   // create gnuplot window
329   static gnuplot_window window;
330    
331   int i = 0;
332   int n = gray_frame->height;
333   int m = 0;
334   double absval = 0;
335   double cc = 0;
336   double correction = (double)255 / (double)n;
337   
338   double *in, *out;
339   fftw_plan rplan;
340    
341   in = (double*) malloc(sizeof(double) * n);
342   out = (double*) malloc(sizeof(double) * n);
343   
344   rplan = fftw_plan_r2r_1d(n, in, out, FFTW_R2HC, FFTW_ESTIMATE); 
345
346   // we have no imaginary data, so clear idata
347  // memset((void *)out, 0, n * sizeof(double));
348
349   // fill rdata with actual data
350   for (i = 0; i < n; i++) {
351     in[i] = ((uchar*)(gray_frame->imageData + i*gray_frame->widthStep))[(gray_frame->width/2)*3];
352     
353     // draw black line in the midle of the image where DFT is computed
354     ((uchar*)(gray_frame->imageData + i*gray_frame->widthStep))[(gray_frame->width/2)*3] = 0;
355   }
356   
357   fftw_execute(rplan);
358  
359   // set plot parameters
360   window.plot_data("u 1:2 w p ps 2");
361
362   // post-process FFT data: make absolute values, and calculate
363   //   real frequency of each power line in the spectrum
364   for (i = 1; i < (n/2 - 1); i++) {
365     absval = sqrt((out[i] * out[i]) + (out[n - i] * out[n - i])) / n;
366     cc = (double)m * correction;
367     m++;
368     
369     // plot fft data, do not plot DC value
370     window.data("%f, %f",cc,absval);
371   }
372   
373   // plot values from FIFO
374   window.flush();
375   
376   //usleep(50000);
377
378   fftw_destroy_plan(rplan);
379   free(in);
380   free(out);  
381
382   return 0;
383 }
384
385
386 /******************************************************************************/
387 /********************************** MODE_IMAGE ********************************/
388 /******************************************************************************/
389
390 /** Simulates MODE_RECOGNIZE over one specified image filename (PC only).
391   * @param imageFilename Image to load and recognize.
392   * @return Code of mode to switch to. */
393 #ifdef WITH_GUI
394 int modeImage(char *imageFilename)
395 {
396   IplImage *frame = NULL;
397   
398   frame = cvLoadImage(imageFilename);
399   
400   if (!frame) {
401     fprintf(stderr, "File %s cannot be loaded as image.\n", imageFilename);
402     return MODE_QUIT;
403   }
404  
405   while (1) {
406
407     
408     // wait infinitely for a keyboard event
409     switch((char)(cvWaitKey(0) & 0xFF)) {
410     // quit
411     case KEY_ESC:
412       goto _modeImage_end;
413       break;
414     }
415   }
416
417 _modeImage_end:
418   cvReleaseImage(&frame);
419   return MODE_QUIT;
420 }
421 #else /* PPC */
422
423 int modeImage(char *imageFilename) {
424   fprintf(stderr, "Image mode is unsupported on PPC.\nTerminating.\n");
425   return MODE_QUIT;
426 }
427 #endif /* WITH_GUI */
428
429 /******************************************************************************/
430 /********************************* MODE_VIDEO *********************************/
431 /******************************************************************************/
432
433 /** Displays just a real video in a window (only on PC).
434   * @param capture Pointer to a camera capture.
435   * @param roi Pointer to ROI rectangle.
436   * @return Code of mode to switch to. */
437 #ifdef WITH_GUI
438 int modeVideo(CvCapture* capture, CvRect *roi)
439 {
440         IplImage *frame = NULL;
441     
442         while (1) {
443                 // wait 10ms for an event
444                 switch(cvWaitKey(10) & 0xFF) {
445       // stop capturing
446       case KEY_ESC:
447         return MODE_QUIT;
448
449       // switch to recognize mode
450       case KEY_R:
451       case KEY_r:
452         return MODE_RECOGNIZE;
453
454       // pause - switch to wait mode
455       case KEY_P:
456       case KEY_p:
457         return MODE_WAIT;
458
459       // save frame to file
460       case KEY_S:
461       case KEY_s:
462         saveFrame(frame);
463         break;
464         
465       // display ROI select window
466       case KEY_O:
467       case KEY_o:
468         cvResetImageROI(frame);
469         selectROI(capture, roi);
470         cvSetImageROI(frame, *roi);
471         break;
472       
473                 }
474
475                 // get one frame from camera (do not release it!)
476                 frame = cvQueryFrame(capture);
477     
478     if (!frame) {
479       fprintf(stderr, "NULL frame\n");
480       continue;
481     }
482     
483                 cvShowImage(WINDOW_ORIG, frame);
484         }
485         
486         return MODE_QUIT;
487 }
488 #else /* PPC */
489
490 int modeVideo(CvCapture* capture, CvRect *roi) {
491         fprintf(stderr, "Video mode is unsupported on PPC.\nTerminating.\n");
492         return MODE_QUIT;
493 }
494 #endif /* WITH_GUI */
495
496 /******************************************************************************/
497 /****************************** MODE_RECOGNIZE ********************************/
498 /******************************************************************************/
499
500 /** On PC processes keyboard events, on PPC does nothing.
501   * @param frame Last camera frame.
502   * @return MODE_RECOGNIZE or mode to switch to. */
503 #ifdef WITH_GUI
504 int recognizeMode_processKeys(IplImage *frame)
505 {
506   // wait 10ms for an event
507   switch (cvWaitKey(10) & 0xFF) {
508     // stop capturing
509     case KEY_ESC:
510       return MODE_QUIT;
511
512     // video mode
513     case KEY_V:
514     case KEY_v:
515       return MODE_VIDEO;
516
517     // pause - wait mode
518     case KEY_P:
519     case KEY_p:
520       camera_control_on = 0;
521       return MODE_WAIT;
522       
523     // save frame to file
524     case KEY_S:
525     case KEY_s:
526       saveFrame(frame);
527       break;
528   }
529   return MODE_RECOGNIZE;
530 }
531 #else /* PPC */
532
533 int recognizeMode_processKeys(IplImage *frame)
534 {
535   return MODE_RECOGNIZE;
536 }
537 #endif /* WITH_GUI */
538
539 /******************************************************************************/
540
541 /** Displays analytic frames on screen (on PC), or saves (input) frame to file
542   * @param frame Input frame from camera.
543   * @param window_name Name of the window to show frame in.
544   */
545 #ifdef WITH_GUI
546 void displayFrames(const char* window_name, IplImage *frame) {
547   cvShowImage(window_name, frame);
548
549 }
550
551 #else /* PPC */
552
553 void displayFrames(const char* window_name, IplImage *frame) { /* nothing */ }
554 #endif /* WITH_GUI */
555
556 /******************************************************************************/
557
558 /** Implements the camera recognition of corns configurations.
559  * @param capture Pointer to a camera capture.
560  * @param roi Pointer to ROI rectangle.
561  * @return Code of mode to switch to. */
562 int modeRecognize(CvCapture* capture, CvRect *roi)
563 {
564   int ret;
565   IplImage *frame = NULL;
566   IplImage *roi_frame = cvCreateImage(cvSize(roi->width, roi->height), 8, 1);
567  
568   while (camera_control_on) {
569     
570     if ((ret = recognizeMode_processKeys(roi_frame)) != MODE_RECOGNIZE)
571       return ret;
572
573     //get one frame from camera (do not release!)
574     frame = cvQueryFrame(capture);
575     
576     if (!frame) {
577       fprintf(stderr, "NULL frame\n");
578       usleep(100*1000);
579       continue;
580     } //else if(orte.camera_result.error & camera_ERR_NO_FRAME) {
581      // orte.camera_result.error &= ~camera_ERR_NO_FRAME;
582       //ORTEPublicationSend(orte.publication_camera_result);
583    // }
584    
585     // set region of interest on captured image
586     cvSetImageROI(frame, *roi);
587     
588     // copy ROI part of captured image to roi_frame and convert to grayscale
589     cvCvtColor(frame, roi_frame, CV_BGR2GRAY);
590    
591     //TODO call image processing (do something useful with the image, FFT atc.)
592     ret = recognize(roi_frame);
593     
594     displayFrames(WINDOW_ORIG, frame);
595     displayFrames(WINDOW_PROD, roi_frame);
596        
597 //     publish results
598 //     orte.camera_result.side = sideConfig;
599 //     orte.camera_result.center = centerConfig;
600 //     orte.camera_result.sideDist = sideDist;
601 //     orte.camera_result.centerDist = centerDist;
602 //     ORTEPublicationSend(orte.publication_camera_result);
603
604   }
605   return MODE_WAIT;  
606 }
607
608
609 /******************************************************************************/
610 /********************************* MODE_WAIT **********************************/
611 /******************************************************************************/
612
613 /** Returns mode to switch to from wait mode. On PC processes keyboard events,
614   * on PPC just tests for orte.camera_control.on value.
615   * @param previousMode Mode to switch to, if no more waiting is needed.
616   * @return Mode to switch to. */
617 #ifdef WITH_GUI
618 int waitMode_processKeys(int previousMode)
619 {
620         // wait 10ms for an event
621         switch(cvWaitKey(10) & 0xFF) {
622     // exit program
623     case KEY_ESC:
624       return MODE_QUIT;
625
626     // stop waiting
627     case KEY_P:
628     case KEY_p:
629       camera_control_on = 1;
630       return previousMode;
631     
632     // continue waiting
633     default:
634       return MODE_WAIT;
635         }
636 }
637 #else /* PPC */
638
639 int waitMode_processKeys(int previousMode) {
640         return (getCameraControlOn() ? previousMode : MODE_WAIT);
641 }
642 #endif /* WITH_GUI */
643
644 /******************************************************************************/
645
646 /** Waits until orte.camera_control.on is set to true or, on PC P-key
647   * is pressed.
648   * @param previousMode Mode to switch to when waiting stops.
649   * @return Code of mode to switch to. */
650 int modeWait(int previousMode)
651 {
652         int mode;
653
654         while((mode = waitMode_processKeys(previousMode)) == MODE_WAIT) {
655                 fprintf(stderr, "waiting...\n");
656                 sleep(1);
657         }
658         
659         return mode;
660 }
661
662
663 /******************************************************************************/
664 /******************************** mode manager ********************************/
665 /******************************************************************************/
666
667 /** Initializes GUI, displays static video window. */
668 #ifdef WITH_GUI
669 void initGUI(void) {
670   cvNamedWindow(WINDOW_ORIG, CV_WINDOW_AUTOSIZE);
671   cvInitFont(&fontLarge, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 2);
672 }
673 #else /* PPC */
674 void initGUI(void) {}
675 #endif /* WITH_GUI */
676
677 /******************************************************************************/
678
679 /** Destroys highgui windows. */
680 #ifdef WITH_GUI
681 void destroyGUI(void)
682 {
683   cvDestroyWindow(WINDOW_ORIG);
684   cvDestroyWindow(WINDOW_PROD);
685 }
686 #else /* PPC */
687 void destroyGUI(void) {}
688 #endif /* WITH_GUI */
689
690 /******************************************************************************/
691
692 /** Switches between modes of the program.
693   * @param defaultMode The first mode to run.
694   * @param paramC Used only in MODE_IMAGE mode (store filepath here).
695   * @return Zero on success, error number on fail. */
696 int modeManager(int defaultMode, char *paramC)
697 {
698   int mode = defaultMode;
699   int lastMode = MODE_RECOGNIZE;
700   CvCapture* capture = NULL;
701   CvRect roi;
702   
703   roi.x = ROI_X;
704   roi.y = ROI_Y;
705   roi.width = ROI_WIDTH;
706   roi.height = ROI_HEIGHT;
707
708   if (defaultMode == MODE_IMAGE) {
709     fprintf(stderr, "barcam started in image mode\n");
710   } else {
711     // connect to a camera
712     while (!(capture = cvCaptureFromCAM(-1))) {
713       fprintf(stderr, "NULL capture (is camera connected?)\n");
714       //orte.camera_result.error |= camera_ERR_NO_VIDEO;
715       //ORTEPublicationSend(orte.publication_camera_result);
716       usleep(500*1000);
717     }
718     
719     //orte.camera_result.error &= ~camera_ERR_NO_VIDEO;
720     //ORTEPublicationSend(orte.publication_camera_result);
721     fprintf(stderr, "barcam started, camera connected successfully\n");
722   }
723
724   initGUI();
725
726   while(!(mode & MODE_QUIT)) {
727     switch(mode) {
728
729     case MODE_VIDEO:
730       mode = modeVideo(capture, &roi);
731       lastMode = MODE_VIDEO;
732       break;
733       
734     case MODE_RECOGNIZE:
735       mode = modeRecognize(capture, &roi);
736       lastMode = MODE_RECOGNIZE;
737       break;
738
739     case MODE_WAIT:
740       mode = modeWait(lastMode);
741       break;
742
743     case MODE_IMAGE:
744       mode = modeImage(paramC);
745       lastMode = MODE_IMAGE;
746       break;
747
748     // jump out of the loop
749     default:
750       goto _modeManager_end;
751     }
752   }
753
754 _modeManager_end:
755   cvReleaseCapture(&capture);
756   //destroyGUI();
757   return 0;
758 }
759
760
761 /******************************************************************************/
762 /*********************************** orte *************************************/
763 /******************************************************************************/
764
765 /** Returns actual state of orte.camera_control.on.
766   * If value changed from previous call, informative output is printed. */
767 #ifdef WITH_GUI
768 inline bool getCameraControlOn(void) {
769         return camera_control_on;
770 }
771 #else /* PPC */
772
773 inline bool getCameraControlOn(void) {
774         if(orte.camera_control.on!=camera_control_on) {
775                 camera_control_on = orte.camera_control.on;
776                 fprintf(stderr, "orte: camera_control changed: ctrl %d\n",
777                                 camera_control_on);
778         }
779         return camera_control_on;
780 }
781 #endif /* WITH_GUI */
782
783 /******************************************************************************/
784
785 /** Orte camera result callback function. Does nothing. */
786 void send_cmr_res_cb(const ORTESendInfo *info, void *vinstance,
787                 void *recvCallBackParam) { /* nothing */ }
788
789 /******************************************************************************/
790
791 /** Orte camera control callback function.
792   * Sets actual value of orte.camera_control.on to camera_control_on. */
793 void rcv_cmr_ctrl_cb(const ORTERecvInfo *info, void *vinstance, void *recvCallBackParam) {
794         struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
795
796         switch (info->status) {
797                 case NEW_DATA:
798                         fprintf(stderr, "orte: New camera data: ctrl %d\n",
799                                 orte_data->camera_control.on);
800                         getCameraControlOn();
801                         break;
802                 case DEADLINE:
803                         fprintf(stderr, "ORTE deadline occurred - CMR_CTRL receive\n");
804                         break;
805         }
806 }
807
808
809 /******************************************************************************/
810 /********************************* application ********************************/
811 /******************************************************************************/
812
813 /** If "-i filename" is in argv, filename is stored to imgFilename.
814   * @return Mode to switch to. */
815 #ifdef WITH_GUI
816 int getStartMode(int argc, char *argv[], char *imgFilename) {
817         char opt;
818         
819         // scan for program arguments
820         while((opt = getopt(argc, argv, "i:")) != -1) {
821                 switch (opt) {
822                 //"-i filename"
823                 case 'i':
824                         if(optarg) sprintf(imgFilename, "%s", optarg);
825                         else {
826                                 fprintf(stderr, "Specify image filename to process: barcam -i filename\n");
827                                 return MODE_QUIT;
828                         }
829                         return MODE_IMAGE;
830                         break;
831                 }
832         }
833
834         camera_control_on = true;
835         return MODE_VIDEO;
836 }
837 #else /* PPC */
838
839 int getStartMode(int argc, char *argv[], char *imgFilename) {
840         return MODE_WAIT;
841 }
842 #endif /* WITH_GUI */
843
844 /******************************************************************************/
845
846 /** The program may on PC be called with -i argument followed by an image
847   * filename, i.e. rozkuk -i image.png, then the program runs in image mode.
848   * Else call it without arguments. On PC it starts in video mode, to switch
849   * modes keyboard is used (R-recognize, W-wait, Esc-quit). On PPC only two
850   * modes are available - recognize and wait. Orte's camera_control_on variable
851   * is used to switch between them. */
852 int main(int argc, char *argv[])
853 {  
854         char imgFilename[100];
855         int ret;
856
857         //ret = robottype_roboorte_init(&orte);
858         
859 //   if(ret < 0) {
860 //              fprintf(stderr, "robottype_roboorte_init failed\n");
861 //              return ret;
862 //      }
863 //      
864         
865 //      robottype_publisher_camera_result_create(&orte, send_cmr_res_cb, &orte);
866 //      robottype_subscriber_camera_control_create(&orte, rcv_cmr_ctrl_cb, &orte);
867 //      robottype_subscriber_robot_switches_create(&orte, rcv_robot_switches_cb, &orte);
868    
869   ret = getStartMode(argc, argv, imgFilename);
870   modeManager(ret, imgFilename);
871   
872   //ret = robottype_roboorte_destroy(&orte);
873   return ret;
874   
875 }
876