Revision: 6674
          http://playerstage.svn.sourceforge.net/playerstage/?rev=6674&view=rev
Author:   gbiggs
Date:     2008-06-24 06:54:42 -0700 (Tue, 24 Jun 2008)

Log Message:
-----------
Added cvcam driver from Paul Osmialowski

Modified Paths:
--------------
    code/player/trunk/config/CMakeLists.txt
    code/player/trunk/server/drivers/camera/CMakeLists.txt

Added Paths:
-----------
    code/player/trunk/config/cvcam.cfg
    code/player/trunk/server/drivers/camera/cvcam/
    code/player/trunk/server/drivers/camera/cvcam/CMakeLists.txt
    code/player/trunk/server/drivers/camera/cvcam/cvcam.cc

Modified: code/player/trunk/config/CMakeLists.txt
===================================================================
--- code/player/trunk/config/CMakeLists.txt     2008-06-24 12:00:04 UTC (rev 
6673)
+++ code/player/trunk/config/CMakeLists.txt     2008-06-24 13:54:42 UTC (rev 
6674)
@@ -2,6 +2,7 @@
                         amigobot_tcp.cfg
                         amtecM5.cfg
                         b21r_rflex_lms200.cfg
+                        cvcam.cfg
                         dummy.cfg
                         erratic.cfg
                         gazebo.cfg

Added: code/player/trunk/config/cvcam.cfg
===================================================================
--- code/player/trunk/config/cvcam.cfg                          (rev 0)
+++ code/player/trunk/config/cvcam.cfg  2008-06-24 13:54:42 UTC (rev 6674)
@@ -0,0 +1,13 @@
+driver
+(
+ name "cvcam"
+ plugin "cvcam.so"
+ provides ["camera:1"]
+ size [384 288]
+)
+driver
+(
+ name "cameracompress"
+ requires ["camera:1"]
+ provides ["camera:0"]
+)

Modified: code/player/trunk/server/drivers/camera/CMakeLists.txt
===================================================================
--- code/player/trunk/server/drivers/camera/CMakeLists.txt      2008-06-24 
12:00:04 UTC (rev 6673)
+++ code/player/trunk/server/drivers/camera/CMakeLists.txt      2008-06-24 
13:54:42 UTC (rev 6674)
@@ -1,6 +1,7 @@
 ADD_SUBDIRECTORY (v4l)
 ADD_SUBDIRECTORY (1394)
 ADD_SUBDIRECTORY (compress)
+ADD_SUBDIRECTORY (cvcam)
 ADD_SUBDIRECTORY (imageseq)
 ADD_SUBDIRECTORY (sphere)
 ADD_SUBDIRECTORY (uvc)

Added: code/player/trunk/server/drivers/camera/cvcam/CMakeLists.txt
===================================================================
--- code/player/trunk/server/drivers/camera/cvcam/CMakeLists.txt                
                (rev 0)
+++ code/player/trunk/server/drivers/camera/cvcam/CMakeLists.txt        
2008-06-24 13:54:42 UTC (rev 6674)
@@ -0,0 +1,7 @@
+PLAYERDRIVER_OPTION (cvcam build_cvcam ON)
+PLAYERDRIVER_REQUIRE_PKG (cvcam build_cvcam opencv cvcam_includeDir
+    cvcam_libDir cvcam_linkFlags cvcam_cFlags)
+PLAYERDRIVER_ADD_DRIVER (cvcam build_cvcam
+    INCLUDEDIRS "${cvcam_includeDir}" LIBDIRS "${cvcam_libDir}"
+    LINKFLAGS "${cvcam_linkFlags}" CFLAGS "${cvcam_cFlags}"
+    SOURCES cvcam.cc)

Added: code/player/trunk/server/drivers/camera/cvcam/cvcam.cc
===================================================================
--- code/player/trunk/server/drivers/camera/cvcam/cvcam.cc                      
        (rev 0)
+++ code/player/trunk/server/drivers/camera/cvcam/cvcam.cc      2008-06-24 
13:54:42 UTC (rev 6674)
@@ -0,0 +1,265 @@
+/*
+ *  Player - One Hell of a Robot Server
+ *  Copyright (C) 2000  Brian Gerkey et al.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+///////////////////////////////////////////////////////////////////////////
+//
+// Desc: OpenCV capture driver
+// Author: Paul Osmialowski
+// Date: 24 Jun 2008
+//
+///////////////////////////////////////////////////////////////////////////
+
+/** @ingroup drivers */
+/** @{ */
+/** @defgroup driver_cvcam cvcam
+ * @brief OpenCV camera capture
+
+The cvcam driver captures images from cameras through OpenCV infrastructure.
+
[EMAIL PROTECTED] Compile-time dependencies
+
+- OpenCV
+
[EMAIL PROTECTED] Provides
+
+- @ref interface_camera
+
[EMAIL PROTECTED] Requires
+
+- none
+
[EMAIL PROTECTED] Configuration requests
+
+- none
+
[EMAIL PROTECTED] Configuration file options
+
+- camindex (integer)
+  - Default: CV_CAP_ANY
+  - Index of camera image source (OpenCV specific)
+
+- size (integer tuple)
+  - Default: [0 0]
+  - Desired image size.   This may not be honoured if the underlying driver 
does
+    not support the requested size.
+    Size [0 0] denotes default device image size.
+
+- sleep_nsec (integer)
+  - Default: 10000000 (=10ms which gives max 100 fps)
+  - timespec value for nanosleep()
+
[EMAIL PROTECTED] Example
+
[EMAIL PROTECTED]
+driver
+(
+  name "cvcam"
+  provides ["camera:0"]
+)
[EMAIL PROTECTED]
+
[EMAIL PROTECTED] Paul Osmialowski
+
+*/
+/** @} */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include <pthread.h>
+#include <libplayercore/playercore.h>
+
+#include <opencv/cv.h>
+#include <opencv/highgui.h>
+
+//---------------------------------
+
+class CvCam : public Driver
+{
+  public: CvCam(ConfigFile * cf, int section);
+  public: virtual ~CvCam();
+
+  public: virtual int Setup();
+  public: virtual int Shutdown();
+
+  // This method will be invoked on each incoming message
+  public: virtual int ProcessMessage(QueuePointer & resp_queue,
+                                     player_msghdr * hdr,
+                                    void * data);
+
+  private: virtual void Main();
+
+  private: CvCapture * capture;
+  private: int camindex;
+  private: int desired_width;
+  private: int desired_height;
+  private: int sleep_nsec;
+};
+
+Driver * CvCam_Init(ConfigFile * cf, int section)
+{
+  return reinterpret_cast<Driver *>(new CvCam(cf, section));
+}
+
+void cvcam_Register(DriverTable *table)
+{
+  table->AddDriver("cvcam", CvCam_Init);
+}
+
+CvCam::CvCam(ConfigFile * cf, int section)
+  : Driver(cf, section, true, PLAYER_MSGQUEUE_DEFAULT_MAXLEN, 
PLAYER_CAMERA_CODE)
+{
+  this->capture = NULL;
+  this->camindex = cf->ReadInt(section, "camindex", CV_CAP_ANY);
+  this->desired_width = cf->ReadTupleInt(section, "size", 0, 0);
+  this->desired_height = cf->ReadTupleInt(section, "size", 1, 0);
+  if ((this->desired_width < 0) || (this->desired_height < 0))
+  {
+    PLAYER_ERROR("Wrong size");
+    this->SetError(-1);
+    return;
+  }
+  this->sleep_nsec = cf->ReadInt(section, "sleep_nsec", 10000000);
+}
+
+CvCam::~CvCam()
+{
+  if (this->capture) cvReleaseCapture(&(this->capture));
+}
+
+int CvCam::Setup()
+{
+  if (this->capture) cvReleaseCapture(&(this->capture));
+  this->capture = cvCaptureFromCAM(this->camindex);
+  this->StartThread();
+  return 0;
+}
+
+int CvCam::Shutdown()
+{
+  StopThread();
+  if (this->capture) cvReleaseCapture(&(this->capture));
+  this->capture = NULL;
+  return 0;
+}
+
+void CvCam::Main()
+{
+  struct timespec tspec;
+  IplImage * frame;
+  player_camera_data_t * data;
+  int i;
+
+  if ((this->desired_width) > 0)
+  {
+    PLAYER_WARN1("Setting capture width %.4f", 
static_cast<double>(this->desired_width));
+    cvSetCaptureProperty(this->capture, CV_CAP_PROP_FRAME_WIDTH, 
static_cast<double>(this->desired_width));
+  }
+  if ((this->desired_height) > 0)
+  {
+    PLAYER_WARN1("Setting capture height %.4f", 
static_cast<double>(this->desired_height));
+    cvSetCaptureProperty(this->capture, CV_CAP_PROP_FRAME_HEIGHT, 
static_cast<double>(this->desired_height));
+  }
+  PLAYER_WARN2("Achieved capture size %.4f x %.4f", 
cvGetCaptureProperty(this->capture, CV_CAP_PROP_FRAME_WIDTH), 
cvGetCaptureProperty(this->capture, CV_CAP_PROP_FRAME_HEIGHT));
+  for (;;)
+  {
+    // Go to sleep for a while (this is a polling loop).
+    tspec.tv_sec = 0;
+    tspec.tv_nsec = this->sleep_nsec;
+    nanosleep(&tspec, NULL);
+
+    // Test if we are supposed to cancel this thread.
+    pthread_testcancel();
+
+    ProcessMessages();
+    pthread_testcancel();
+
+    frame = cvQueryFrame(this->capture);
+    if (!frame)
+    {
+      PLAYER_ERROR("No frame!");
+      continue;
+    }
+    if (frame->depth != IPL_DEPTH_8U)
+    {
+      PLAYER_ERROR1("Unsupported depth %d", frame->depth);
+      continue;
+    }
+    data = reinterpret_cast<player_camera_data_t 
*>(malloc(sizeof(player_camera_data_t)));
+    if (!data)
+    {
+      PLAYER_ERROR("Out of memory");
+      continue;
+    }
+    assert((frame->nChannels) > 0);
+    data->image_count = frame->width * frame->height * frame->nChannels;
+    assert(data->image_count > 0);
+    data->image = reinterpret_cast<unsigned char *>(malloc(data->image_count));
+    if (!(data->image))
+    {
+      PLAYER_ERROR("Out of memory");
+      continue;
+    }
+    data->width = frame->width;
+    data->height = frame->height;
+    data->bpp = frame->nChannels * 8;
+    data->fdiv = 0;
+    switch (data->bpp)
+    {
+    case 8:
+      data->format = PLAYER_CAMERA_FORMAT_MONO8;
+      memcpy(data->image, frame->imageData, data->image_count);
+      break;
+    case 24:
+      data->format = PLAYER_CAMERA_FORMAT_RGB888;
+      for (i = 0; i < static_cast<int>(data->image_count); i += 
(frame->nChannels))
+      {
+        data->image[i] = frame->imageData[i + 2];
+        data->image[i + 1] = frame->imageData[i + 1];
+        data->image[i + 2] = frame->imageData[i];
+      }
+    case 32:
+      data->format = PLAYER_CAMERA_FORMAT_RGB888;
+      for (i = 0; i < static_cast<int>(data->image_count); i += 
(frame->nChannels))
+      {
+        data->image[i] = frame->imageData[i + 2];
+        data->image[i + 1] = frame->imageData[i + 1];
+        data->image[i + 2] = frame->imageData[i];
+        data->image[i + 3] = frame->imageData[i + 3];
+      }
+      break;
+    default:
+      PLAYER_ERROR1("Unsupported image depth %d", data->bpp);
+      data->bpp = 0;
+    }
+    assert(data->bpp > 0);
+    data->compression = PLAYER_CAMERA_COMPRESS_RAW;
+    this->Publish(device_addr, PLAYER_MSGTYPE_DATA, PLAYER_CAMERA_DATA_STATE, 
reinterpret_cast<void *>(data), 0, NULL, false);
+    // copy = false, don't dispose anything here
+    pthread_testcancel();
+  }
+}
+
+int CvCam::ProcessMessage(QueuePointer & resp_queue, player_msghdr * hdr, void 
* data)
+{
+  assert(hdr);
+  return -1;
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Playerstage-commit mailing list
Playerstage-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to