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