Point Cloud Library (PCL)  1.7.1
robot_eye_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  */
37 
38 #include "pcl/pcl_config.h"
39 
40 #ifndef PCL_IO_ROBOT_EYE_GRABBER_H_
41 #define PCL_IO_ROBOT_EYE_GRABBER_H_
42 
43 #include <pcl/io/grabber.h>
44 #include <pcl/io/impl/synchronized_queue.hpp>
45 #include <pcl/point_types.h>
46 #include <pcl/point_cloud.h>
47 #include <boost/asio.hpp>
48 #include <boost/thread/thread.hpp>
49 
50 namespace pcl
51 {
52 
53  /** \brief Grabber for the Ocular Robotics RobotEye sensor.
54  * \ingroup io
55  */
56  class PCL_EXPORTS RobotEyeGrabber : public Grabber
57  {
58  public:
59 
60  /** \brief Signal used for the point cloud callback.
61  * This signal is sent when the accumulated number of points reaches
62  * the limit specified by setSignalPointCloudSize().
63  */
64  typedef void (sig_cb_robot_eye_point_cloud_xyzi) (
65  const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZI> >&);
66 
67  /** \brief RobotEyeGrabber default constructor. */
68  RobotEyeGrabber ();
69 
70  /** \brief RobotEyeGrabber constructor taking a specified IP address and data port. */
71  RobotEyeGrabber (const boost::asio::ip::address& ipAddress, unsigned short port=443);
72 
73  /** \brief virtual Destructor inherited from the Grabber interface. It never throws. */
74  virtual ~RobotEyeGrabber () throw ();
75 
76  /** \brief Starts the RobotEye grabber.
77  * The grabber runs on a separate thread, this call will return without blocking. */
78  virtual void start ();
79 
80  /** \brief Stops the RobotEye grabber. */
81  virtual void stop ();
82 
83  /** \brief Obtains the name of this I/O Grabber
84  * \return The name of the grabber
85  */
86  virtual std::string getName () const;
87 
88  /** \brief Check if the grabber is still running.
89  * \return TRUE if the grabber is running, FALSE otherwise
90  */
91  virtual bool isRunning () const;
92 
93  /** \brief Returns the number of frames per second.
94  */
95  virtual float getFramesPerSecond () const;
96 
97  /** \brief Set/get ip address of the sensor that sends the data.
98  * The default is address_v4::any ().
99  */
100  void setSensorAddress (const boost::asio::ip::address& ipAddress);
101  const boost::asio::ip::address& getSensorAddress () const;
102 
103  /** \brief Set/get the port number which receives data from the sensor.
104  * The default is 443.
105  */
106  void setDataPort (unsigned short port);
107  unsigned short getDataPort () const;
108 
109  /** \brief Set/get the number of points to accumulate before the grabber
110  * callback is signaled. The default is 1000.
111  */
112  void setSignalPointCloudSize (std::size_t numerOfPoints);
113  std::size_t getSignalPointCloudSize () const;
114 
115  /** \brief Returns the point cloud with point accumulated by the grabber.
116  * It is not safe to access this point cloud except if the grabber is
117  * stopped or during the grabber callback.
118  */
119  boost::shared_ptr<pcl::PointCloud<pcl::PointXYZI> > getPointCloud() const;
120 
121  private:
122 
123  bool terminate_thread_;
124  size_t signal_point_cloud_size_;
125  unsigned short data_port_;
126  unsigned char receive_buffer_[500];
127 
128  boost::asio::ip::address sensor_address_;
129  boost::asio::ip::udp::endpoint sender_endpoint_;
130  boost::asio::io_service io_service_;
131  boost::shared_ptr<boost::asio::ip::udp::socket> socket_;
132  boost::shared_ptr<boost::thread> socket_thread_;
133  boost::shared_ptr<boost::thread> consumer_thread_;
134 
135  pcl::SynchronizedQueue<boost::shared_array<unsigned char> > packet_queue_;
136  boost::shared_ptr<pcl::PointCloud<pcl::PointXYZI> > point_cloud_xyzi_;
137  boost::signals2::signal<sig_cb_robot_eye_point_cloud_xyzi>* point_cloud_signal_;
138 
139  void consumerThreadLoop ();
140  void socketThreadLoop ();
141  void asyncSocketReceive ();
142  void resetPointCloud ();
143  void socketCallback (const boost::system::error_code& error, std::size_t numberOfBytes);
144  void convertPacketData (unsigned char *dataPacket, size_t length);
145  void computeXYZI (pcl::PointXYZI& pointXYZI, unsigned char* pointData);
146  };
147 }
148 
149 #endif /* PCL_IO_ROBOT_EYE_GRABBER_H_ */
Grabber interface for PCL 1.x device drivers.
Definition: grabber.h:58
PointCloud represents the base class in PCL for storing collections of 3D points. ...
Grabber for the Ocular Robotics RobotEye sensor.