Fawkes API  Fawkes Development Version
pipeline_thread.cpp
1 
2 /***************************************************************************
3  * pipeline_thread.cpp - SwissRanger Save Pipeline Thread
4  *
5  * Created: Fri Jan 22 10:50:13 2010
6  * Copyright 2005-2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "pipeline_thread.h"
24 
25 #include <fvcams/camera.h>
26 
27 #include <sys/time.h>
28 #include <stdlib.h>
29 #include <cstdio>
30 
31 using namespace fawkes;
32 
33 /** @class FvSrSavePipelineThread "pipeline_thread.h"
34  * SrSave vision image processing pipeline.
35  * This thread implements an image processing pipeline that uses a colormodel and
36  * classifier to determine regions of interest (ROI) which contain a significant
37  * amount with "pixels of ball color". The best ROI is then filtered for edge detection.
38  * On the edges a circle shape detection is carried out to confirm the result and to
39  * get the required data to calculate the relative and global position of the ball.
40  *
41  * @author Tim Niemueller
42  */
43 
44 
45 /** Constructor. */
47  : Thread("FvSrSavePipelineThread", Thread::OPMODE_WAITFORWAKEUP),
48  VisionAspect(VisionAspect::CYCLIC)
49 {
50 }
51 
52 
53 /** Destructor. */
55 {
56 }
57 
58 
59 /** Initialize the pipeline thread.
60  * Camera is requested, config parameters are obtained from the config db, and
61  * other miscellaneous init stuff is done here.
62  */
63 void
65 {
66  try {
67  __cam = vision_master->register_for_raw_camera("swissranger:any:mode=CARTESIAN_FLOAT", this );
68  } catch (Exception& e) {
69  e.append("FvSrSavePipelineThread::init() failed since no camera is specified");
70  throw;
71  }
72 }
73 
74 
75 /** Thread finalization. */
76 void
78 {
80 }
81 
82 /** A new image is retrieved from the camera and the classifier looks for a ball
83  * in the image */
84 void
86 {
87  __cam->capture();
88 
89  const unsigned int width = __cam->pixel_width();
90  const unsigned int height = __cam->pixel_height();
91 
92  float *fbuf = (float *)__cam->buffer();
93  float *x = fbuf;
94  float *y = x + width * height;
95  float *z = y + width * height;
96 
97  char *filename;
98  if (asprintf(&filename, "swissranger-%05u.pts", __frame_i++) != -1) {
99  FILE *f = fopen(filename, "w");
100 
101  for (unsigned int h = 0; h < height; ++h) {
102  for (unsigned int w = 0; w < width; ++w) {
103  fprintf(f, "%f %f %f 128 128 128\n",
104  *x++ * 2000., *y++ * 2000., *z++ * 2000.);
105  }
106  }
107 
108  fclose(f);
109  free(filename);
110  } else {
111  logger->log_warn(name(), "Failed to allocate filename");
112  }
113 
114 
115  __cam->dispose_buffer();
116 }
117 
FvSrSavePipelineThread()
Constructor.
virtual void finalize()
Thread finalization.
Fawkes library namespace.
firevision::VisionMaster * vision_master
Vision master.
Definition: vision.h:53
virtual unsigned int pixel_width()=0
Width of image in pixels.
virtual void loop()
A new image is retrieved from the camera and the classifier looks for a ball in the image...
Thread class encapsulation of pthreads.
Definition: thread.h:42
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
virtual void unregister_thread(fawkes::Thread *thread)=0
Unregister a thread.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void capture()=0
Capture an image.
Thread aspect to use in FireVision apps.
Definition: vision.h:35
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void init()
Initialize the pipeline thread.
virtual unsigned char * buffer()=0
Get access to current image buffer.
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual Camera * register_for_raw_camera(const char *camera_string, fawkes::Thread *thread)=0
Register thread for camera.
virtual ~FvSrSavePipelineThread()
Destructor.
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341
virtual void dispose_buffer()=0
Dispose current buffer.