Point Cloud Library (PCL)  1.8.0
dinast_grabber.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #ifndef PCL_IO_DINAST_GRABBER_
41 #define PCL_IO_DINAST_GRABBER_
42 
43 #include <pcl/point_types.h>
44 #include <pcl/point_cloud.h>
45 #include <pcl/io/grabber.h>
46 #include <pcl/common/time.h>
47 #include <pcl/console/print.h>
48 #include <libusb-1.0/libusb.h>
49 #include <boost/circular_buffer.hpp>
50 
51 namespace pcl
52 {
53  /** \brief Grabber for DINAST devices (i.e., IPA-1002, IPA-1110, IPA-2001)
54  * \author Marco A. Gutierrez <marcog@unex.es>
55  * \ingroup io
56  */
57  class PCL_EXPORTS DinastGrabber: public Grabber
58  {
59  // Define callback signature typedefs
60  typedef void (sig_cb_dinast_point_cloud) (const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZI> >&);
61 
62  public:
63  /** \brief Constructor that sets up the grabber constants.
64  * \param[in] device_position Number corresponding the device to grab
65  */
66  DinastGrabber (const int device_position=1);
67 
68  /** \brief Destructor. It never throws. */
69  virtual ~DinastGrabber () throw ();
70 
71  /** \brief Check if the grabber is running
72  * \return true if grabber is running / streaming. False otherwise.
73  */
74  virtual bool
75  isRunning () const;
76 
77  /** \brief Returns the name of the concrete subclass, DinastGrabber.
78  * \return DinastGrabber.
79  */
80  virtual std::string
81  getName () const
82  { return (std::string ("DinastGrabber")); }
83 
84  /** \brief Start the data acquisition process.
85  */
86  virtual void
87  start ();
88 
89  /** \brief Stop the data acquisition process.
90  */
91  virtual void
92  stop ();
93 
94  /** \brief Obtain the number of frames per second (FPS). */
95  virtual float
96  getFramesPerSecond () const;
97 
98  /** \brief Get the version number of the currently opened device
99  */
100  std::string
101  getDeviceVersion ();
102 
103  protected:
104 
105  /** \brief On initialization processing. */
106  void
107  onInit (const int device_id);
108 
109  /** \brief Setup a Dinast 3D camera device
110  * \param[in] device_position Number corresponding the device to grab
111  * \param[in] id_vendor The ID of the camera vendor (should be 0x18d1)
112  * \param[in] id_product The ID of the product (should be 0x1402)
113  */
114  void
115  setupDevice (int device_position,
116  const int id_vendor = 0x18d1,
117  const int id_product = 0x1402);
118 
119  /** \brief Send a RX data packet request
120  * \param[in] req_code the request to send (the request field for the setup packet)
121  * \param buffer
122  * \param[in] length the length field for the setup packet. The data buffer should be at least this size.
123  */
124  bool
125  USBRxControlData (const unsigned char req_code,
126  unsigned char *buffer,
127  int length);
128 
129  /** \brief Send a TX data packet request
130  * \param[in] req_code the request to send (the request field for the setup packet)
131  * \param buffer
132  * \param[in] length the length field for the setup packet. The data buffer should be at least this size.
133  */
134  bool
135  USBTxControlData (const unsigned char req_code,
136  unsigned char *buffer,
137  int length);
138 
139  /** \brief Check if we have a header in the global buffer, and return the position of the next valid image.
140  * \note If the image in the buffer is partial, return -1, as we have to wait until we add more data to it.
141  * \return the position of the next valid image (i.e., right after a valid header) or -1 in case the buffer
142  * either doesn't have an image or has a partial image
143  */
144  int
145  checkHeader ();
146 
147  /** \brief Read image data and leaves it on image_
148  */
149  void
150  readImage ();
151 
152  /** \brief Obtains XYZI Point Cloud from the image of the camera
153  * \return the point cloud from the image data
154  */
156  getXYZIPointCloud ();
157 
158  /** \brief The function in charge of getting the data from the camera
159  */
160  void
161  captureThreadFunction ();
162 
163  /** \brief Width of image */
165 
166  /** \brief Height of image */
168 
169  /** \brief Total size of image */
171 
172  /** \brief Length of a sync packet */
174 
175  double dist_max_2d_;
176 
177  /** \brief diagonal Field of View*/
178  double fov_;
179 
180  /** \brief Size of pixel */
181  enum pixel_size { RAW8=1, RGB16=2, RGB24=3, RGB32=4 };
182 
183  /** \brief The libusb context*/
184  libusb_context *context_;
185 
186  /** \brief the actual device_handle for the camera */
187  struct libusb_device_handle *device_handle_;
188 
189  /** \brief Temporary USB read buffer, since we read two RGB16 images at a time size is the double of two images
190  * plus a sync packet.
191  */
192  unsigned char *raw_buffer_ ;
193 
194  /** \brief Global circular buffer */
195  boost::circular_buffer<unsigned char> g_buffer_;
196 
197  /** \brief Bulk endpoint address value */
198  unsigned char bulk_ep_;
199 
200  /** \brief Device command values */
201  enum { CMD_READ_START=0xC7, CMD_READ_STOP=0xC8, CMD_GET_VERSION=0xDC, CMD_SEND_DATA=0xDE };
202 
203  unsigned char *image_;
204 
205  /** \brief Since there is no header after the first image, we need to save the state */
207 
208  bool running_;
209 
210  boost::thread capture_thread_;
211 
212  mutable boost::mutex capture_mutex_;
213  boost::signals2::signal<sig_cb_dinast_point_cloud>* point_cloud_signal_;
214  };
215 } //namespace pcl
216 
217 #endif // PCL_IO_DINAST_GRABBER_
struct libusb_device_handle * device_handle_
the actual device_handle for the camera
libusb_context * context_
The libusb context.
boost::thread capture_thread_
boost::mutex capture_mutex_
virtual std::string getName() const
Returns the name of the concrete subclass, DinastGrabber.
int image_width_
Width of image.
boost::circular_buffer< unsigned char > g_buffer_
Global circular buffer.
int image_height_
Height of image.
Grabber for DINAST devices (i.e., IPA-1002, IPA-1110, IPA-2001)
Grabber interface for PCL 1.x device drivers.
Definition: grabber.h:58
bool second_image_
Since there is no header after the first image, we need to save the state.
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
Defines all the PCL implemented PointT point type structures.
unsigned char bulk_ep_
Bulk endpoint address value.
double fov_
diagonal Field of View
int sync_packet_size_
Length of a sync packet.
unsigned char * raw_buffer_
Temporary USB read buffer, since we read two RGB16 images at a time size is the double of two images ...
unsigned char * image_
boost::signals2::signal< sig_cb_dinast_point_cloud > * point_cloud_signal_
pixel_size
Size of pixel.
Define methods for measuring time spent in code blocks.
int image_size_
Total size of image.