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