37 #include <boost/make_shared.hpp> 54 return (di1.
uri_.compare(di2.
uri_) < 0);
58 typedef std::set<OpenNI2DeviceInfo, OpenNI2DeviceInfoComparator>
DeviceSet;
61 public openni::OpenNI::DeviceDisconnectedListener,
62 public openni::OpenNI::DeviceStateChangedListener
66 openni::OpenNI::DeviceConnectedListener(),
67 openni::OpenNI::DeviceDisconnectedListener(),
68 openni::OpenNI::DeviceStateChangedListener()
70 openni::OpenNI::addDeviceConnectedListener(
this);
71 openni::OpenNI::addDeviceDisconnectedListener(
this);
72 openni::OpenNI::addDeviceStateChangedListener(
this);
75 openni::Array<openni::DeviceInfo> device_info_list;
76 openni::OpenNI::enumerateDevices(&device_info_list);
78 for (
int i = 0; i < device_info_list.getSize(); ++i)
80 onDeviceConnected(&device_info_list[i]);
86 openni::OpenNI::removeDeviceConnectedListener(
this);
87 openni::OpenNI::removeDeviceDisconnectedListener(
this);
88 openni::OpenNI::removeDeviceStateChangedListener(
this);
93 ROS_INFO(
"Device \"%s\" error state changed to %d\n", pInfo->getUri(), state);
97 case openni::DEVICE_STATE_OK:
98 onDeviceConnected(pInfo);
100 case openni::DEVICE_STATE_ERROR:
101 case openni::DEVICE_STATE_NOT_READY:
102 case openni::DEVICE_STATE_EOF:
104 onDeviceDisconnected(pInfo);
111 boost::mutex::scoped_lock l(device_mutex_);
115 ROS_INFO(
"Device \"%s\" found.", pInfo->getUri());
118 device_set_.erase(device_info_wrapped);
119 device_set_.insert(device_info_wrapped);
125 boost::mutex::scoped_lock l(device_mutex_);
127 ROS_WARN(
"Device \"%s\" disconnected\n", pInfo->getUri());
130 device_set_.erase(device_info_wrapped);
135 boost::mutex::scoped_lock l(device_mutex_);
139 result->reserve(device_set_.size());
141 std::set<OpenNI2DeviceInfo, OpenNI2DeviceInfoComparator>::const_iterator it;
142 std::set<OpenNI2DeviceInfo, OpenNI2DeviceInfoComparator>::const_iterator it_end = device_set_.end();
144 for (it = device_set_.begin(); it != it_end; ++it)
145 result->push_back(it->uri_);
152 boost::mutex::scoped_lock l(device_mutex_);
156 result->reserve(device_set_.size());
158 DeviceSet::const_iterator it;
159 DeviceSet::const_iterator it_end = device_set_.end();
161 for (it = device_set_.begin(); it != it_end; ++it)
162 result->push_back(*it);
169 boost::mutex::scoped_lock l(device_mutex_);
171 return device_set_.size();
184 openni::Status rc = openni::OpenNI::initialize();
185 if (rc != openni::STATUS_OK)
188 device_listener_ = boost::make_shared<OpenNI2DeviceListener>();
197 if (singelton_.get()==0)
198 singelton_ = boost::make_shared<OpenNI2DeviceManager>();
205 return device_listener_->getConnectedDeviceInfos();
210 return device_listener_->getConnectedDeviceURIs();
215 return device_listener_->getNumOfConnectedDevices();
220 openni::Device openni_device;
224 if (Uri.length() > 0 && openni_device.open(Uri.c_str()) == openni::STATUS_OK)
226 int serial_len = 100;
227 char serial[serial_len];
229 openni::Status rc = openni_device.getProperty(openni::DEVICE_PROPERTY_SERIAL_NUMBER, serial, &serial_len);
230 if (rc == openni::STATUS_OK)
237 openni_device.close();
248 return boost::make_shared<OpenNI2Device>(
"");
252 return boost::make_shared<OpenNI2Device>(device_URI);
260 std::vector<OpenNI2DeviceInfo>::const_iterator it;
261 std::vector<OpenNI2DeviceInfo>::const_iterator it_end = device_info->end();
263 for (it = device_info->begin(); it != it_end; ++it)
265 stream <<
"Uri: " << it->uri_ <<
" (Vendor: " << it->vendor_ <<
266 ", Name: " << it->name_ <<
267 ", Vendor ID: " << it->vendor_id_ <<
268 ", Product ID: " << it->product_id_ <<
boost::shared_ptr< std::vector< std::string > > getConnectedDeviceURIs()
std::ostream & operator<<(std::ostream &stream, const OpenNI2Device &device)
std::set< OpenNI2DeviceInfo, OpenNI2DeviceInfoComparator > DeviceSet
bool operator()(const OpenNI2DeviceInfo &di1, const OpenNI2DeviceInfo &di2)
#define THROW_OPENNI_EXCEPTION(format,...)
virtual ~OpenNI2DeviceManager()
virtual void onDeviceDisconnected(const openni::DeviceInfo *pInfo)
boost::shared_ptr< OpenNI2Device > getAnyDevice()
std::size_t getNumOfConnectedDevices()
static boost::shared_ptr< OpenNI2DeviceManager > getSingelton()
virtual void onDeviceStateChanged(const openni::DeviceInfo *pInfo, openni::DeviceState state)
boost::mutex device_mutex_
boost::shared_ptr< std::vector< std::string > > getConnectedDeviceURIs() const
boost::shared_ptr< OpenNI2Device > getDevice(const std::string &device_URI)
std::size_t getNumOfConnectedDevices() const
std::string getSerial(const std::string &device_URI) const
virtual void onDeviceConnected(const openni::DeviceInfo *pInfo)
boost::shared_ptr< std::vector< OpenNI2DeviceInfo > > getConnectedDeviceInfos()
const OpenNI2DeviceInfo openni2_convert(const openni::DeviceInfo *pInfo)
boost::shared_ptr< std::vector< OpenNI2DeviceInfo > > getConnectedDeviceInfos() const
static boost::shared_ptr< OpenNI2DeviceManager > singelton_