38 #include <boost/date_time/posix_time/posix_time.hpp> 39 #include <boost/thread/thread.hpp> 49 data_skip_ir_counter_(0),
50 data_skip_color_counter_(0),
51 data_skip_depth_counter_ (0),
52 ir_subscribers_(false),
53 color_subscribers_(false),
54 depth_subscribers_(false),
55 depth_raw_subscribers_(false),
56 enable_reconnect_(false)
71 ROS_DEBUG(
"Waiting for dynamic reconfigure configuration.");
72 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
74 ROS_DEBUG(
"Dynamic reconfigure configuration received.");
81 <<
"should be plugged into each bus");
145 std::string serial_number =
device_->getStringID();
146 std::string color_name, ir_name;
148 color_name =
"rgb_" + serial_number;
149 ir_name =
"depth_" + serial_number;
166 bool stream_reset =
false;
179 ROS_ERROR(
"Undefined IR video mode received from dynamic reconfigure");
185 ROS_ERROR(
"Undefined color video mode received from dynamic reconfigure");
191 ROS_ERROR(
"Undefined depth video mode received from dynamic reconfigure");
221 if (
device_->isIRVideoModeSupported(ir_video_mode))
223 if (ir_video_mode !=
device_->getIRVideoMode())
225 device_->setIRVideoMode(ir_video_mode);
236 if (
device_->isColorVideoModeSupported(color_video_mode))
238 if (color_video_mode !=
device_->getColorVideoMode())
240 device_->setColorVideoMode(color_video_mode);
250 if (
device_->isDepthVideoModeSupported(depth_video_mode))
252 if (depth_video_mode !=
device_->getDepthVideoMode())
254 device_->setDepthVideoMode(depth_video_mode);
274 if (
device_->isImageRegistrationModeSupported())
283 ROS_ERROR(
"Could not set image registration. Reason: %s", exception.
what());
294 ROS_ERROR(
"Could not set color depth synchronization. Reason: %s", exception.
what());
304 ROS_ERROR(
"Could not set auto exposure. Reason: %s", exception.
what());
314 ROS_ERROR(
"Could not set auto white balance. Reason: %s", exception.
what());
323 ROS_INFO_STREAM(
"Forcing exposure set, when auto exposure/white balance disabled");
336 ROS_ERROR(
"Could not set exposure. Reason: %s", exception.
what());
347 int current_exposure_ =
device_->getExposure();
365 ROS_ERROR(
"Could not set exposure. Reason: %s", exception.
what());
373 ROS_WARN_STREAM(
"Callback in " << __FUNCTION__ <<
"failed due to null device");
383 if (
device_->isIRStreamStarted())
385 ROS_ERROR(
"Cannot stream RGB and IR at the same time. Streaming RGB only.");
400 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
412 if (need_ir && !
device_->isIRStreamStarted())
426 ROS_WARN_STREAM(
"Callback in " << __FUNCTION__ <<
"failed due to null device");
437 if (need_depth && !
device_->isDepthStreamStarted())
444 else if (!need_depth &&
device_->isDepthStreamStarted())
455 ROS_WARN_STREAM(
"Callback in " << __FUNCTION__ <<
"failed due to null device");
465 if (
device_->isColorStreamStarted())
467 ROS_ERROR(
"Cannot stream RGB and IR at the same time. Streaming RGB only.");
529 uint16_t* data =
reinterpret_cast<uint16_t*
>(&image->data[0]);
530 for (
unsigned int i = 0; i < image->width * image->height; ++i)
537 uint16_t* data =
reinterpret_cast<uint16_t*
>(&image->data[0]);
538 for (
unsigned int i = 0; i < image->width * image->height; ++i)
540 data[i] =
static_cast<uint16_t
>(data[i] *
z_scaling_);
543 sensor_msgs::CameraInfoPtr cam_info;
578 sensor_msgs::CameraInfoPtr info = boost::make_shared<sensor_msgs::CameraInfo>();
581 info->height = height;
584 info->D.resize(5, 0.0);
589 info->K[0] = info->K[4] = f;
590 info->K[2] = (width / 2) - 0.5;
593 info->K[5] = (width * (3./8.)) - 0.5;
598 info->R[0] = info->R[4] = info->R[8] = 1.0;
602 info->P[0] = info->P[5] = f;
603 info->P[2] = info->K[2];
604 info->P[6] = info->K[5];
613 sensor_msgs::CameraInfoPtr info;
618 if ( info->width != width )
621 ROS_WARN_ONCE(
"Image resolution doesn't match calibration of the RGB camera. Using default parameters.");
632 info->header.stamp = time;
641 sensor_msgs::CameraInfoPtr info;
645 info = boost::make_shared<sensor_msgs::CameraInfo>(
ir_info_manager_->getCameraInfo());
646 if ( info->width != width )
649 ROS_WARN_ONCE(
"Image resolution doesn't match calibration of the IR camera. Using default parameters.");
660 info->header.stamp = time;
672 double scaling = (double)width / 640;
674 sensor_msgs::CameraInfoPtr info =
getIRCameraInfo(width, height, time);
691 info->P[3] = -
device_->getBaseline() * info->P[0];
699 ROS_WARN (
"~device_id is not set! Using first device.");
727 if (device_id.size() > 1 && device_id[0] ==
'#')
729 std::istringstream device_number_str(device_id.substr(1));
731 device_number_str >> device_number;
732 int device_index = device_number - 1;
733 if (device_index >= available_device_URIs->size() || device_index < 0)
736 "Invalid device number %i, there are %zu devices connected.",
737 device_number, available_device_URIs->size());
741 return available_device_URIs->at(device_index);
748 else if (device_id.size() > 1 && device_id.find(
'@') != std::string::npos && device_id.find(
'/') == std::string::npos)
751 size_t index = device_id.find(
'@');
755 "%s is not a valid device URI, you must give the bus number before the @.",
758 if (index >= device_id.size() - 1)
761 "%s is not a valid device URI, you must give the device number after the @, specify 0 for any device on this bus",
766 std::istringstream device_number_str(device_id.substr(index+1));
768 device_number_str >> device_number;
771 std::string bus = device_id.substr(0, index);
774 for (
size_t i = 0; i < available_device_URIs->size(); ++i)
776 std::string
s = (*available_device_URIs)[i];
777 if (s.find(bus) != std::string::npos)
780 std::ostringstream ss;
781 ss << bus <<
'/' << device_number;
782 if (device_number == 0 || s.find(ss.str()) != std::string::npos)
792 for(std::vector<std::string>::const_iterator it = available_device_URIs->begin();
793 it != available_device_URIs->end(); ++it)
797 if (serial.size() > 0 && device_id == serial)
802 ROS_WARN(
"Could not query serial number of device \"%s\":", exception.
what());
807 bool match_found =
false;
808 std::string matched_uri;
809 for (
size_t i = 0; i < available_device_URIs->size(); ++i)
811 std::string
s = (*available_device_URIs)[i];
812 if (s.find(device_id) != std::string::npos)
822 THROW_OPENNI_EXCEPTION(
"Two devices match the given device id '%s': %s and %s.", device_id.c_str(), matched_uri.c_str(), s.c_str());
847 ROS_INFO(
"No matching device found.... waiting for devices. Reason: %s", exception.
what());
848 boost::this_thread::sleep(boost::posix_time::seconds(3));
853 ROS_ERROR(
"Could not retrieve device. Reason: %s", exception.
what());
861 ROS_DEBUG(
"Waiting for device initialization..");
862 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
871 unsigned first = uri.find(
'@');
872 unsigned last = uri.find(
'/', first);
873 std::string bus_id = uri.substr (first+1,last-first-1);
874 int rtn = atoi(bus_id.c_str());
886 for (std::size_t i = 0; i != list->size(); ++i)
915 ROS_INFO(
"Waiting for device initialization, before configuring and restarting publishers");
916 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
935 <<
"/white balance are disabled. Temporarily working" 936 <<
" around this issue");
937 ROS_WARN_STREAM(
"Toggling exposure and white balance to auto on re-connect" 938 <<
", otherwise image will be very dark");
939 device_->setAutoExposure(
true);
940 device_->setAutoWhiteBalance(
true);
943 boost::this_thread::sleep(boost::posix_time::milliseconds(2500));
944 ROS_WARN_STREAM(
"Resetting auto exposure and white balance to previous values");
960 <<
", reason: " << exception.
what());
967 ROS_WARN_STREAM(
"Detected loss of connection. Stopping all streams and resetting device");
1087 std::map<int, OpenNI2VideoMode>::const_iterator it;
1093 video_mode = it->second;
1102 static const float bad_point = std::numeric_limits<float>::quiet_NaN ();
1104 sensor_msgs::ImagePtr new_image = boost::make_shared<sensor_msgs::Image>();
1106 new_image->header = raw_image->header;
1107 new_image->width = raw_image->width;
1108 new_image->height = raw_image->height;
1109 new_image->is_bigendian = 0;
1111 new_image->step =
sizeof(float)*raw_image->width;
1113 std::size_t data_size = new_image->width*new_image->height;
1114 new_image->data.resize(data_size*
sizeof(
float));
1116 const unsigned short* in_ptr =
reinterpret_cast<const unsigned short*
>(&raw_image->data[0]);
1117 float* out_ptr =
reinterpret_cast<float*
>(&new_image->data[0]);
1119 for (std::size_t i = 0; i<data_size; ++i, ++in_ptr, ++out_ptr)
1121 if (*in_ptr==0 || *in_ptr==0x7FF)
1123 *out_ptr = bad_point;
1126 *out_ptr =
static_cast<float>(*in_ptr)/1000.0f;
boost::shared_ptr< ReconfigureServer > reconfigure_server_
reconfigure server
sensor_msgs::CameraInfoPtr getDefaultCameraInfo(int width, int height, double f) const
boost::shared_ptr< camera_info_manager::CameraInfoManager > ir_info_manager_
boost::mutex connect_mutex_
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
bool enable_reconnect_
indicates if reconnect logic is enabled.
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
std::string resolveDeviceURI(const std::string &device_id)
void readConfigFromParameterServer()
void publish(const boost::shared_ptr< M > &message) const
OpenNI2VideoMode ir_video_mode_
ros::ServiceServer get_serial_server
get_serial server
std::string color_info_url_
bool projector_info_subscribers_
ros::Publisher pub_projector_info_
ros::Duration color_time_offset_
uint32_t getNumSubscribers() const
boost::shared_ptr< OpenNI2Device > device_
bool getSerialCb(openni2_camera::GetSerialRequest &req, openni2_camera::GetSerialResponse &res)
sensor_msgs::ImageConstPtr rawToFloatingPointConversion(sensor_msgs::ImageConstPtr raw_image)
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
openni2_camera::OpenNI2Config Config
void setColorVideoMode(const OpenNI2VideoMode &color_video_mode)
#define THROW_OPENNI_EXCEPTION(format,...)
void advertiseROSTopics()
void setDepthVideoMode(const OpenNI2VideoMode &depth_video_mode)
image_transport::CameraPublisher pub_depth_
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
PixelFormat pixel_format_
boost::shared_ptr< OpenNI2DeviceManager > device_manager_
int lookupVideoModeFromDynConfig(int mode_nr, OpenNI2VideoMode &video_mode)
std::size_t x_resolution_
void applyConfigToOpenNIDevice()
image_transport::CameraPublisher pub_ir_
CameraPublisher advertiseCamera(const std::string &base_topic, uint32_t queue_size, bool latch=false)
bool depth_raw_subscribers_
std::string color_frame_id_
boost::shared_ptr< camera_info_manager::CameraInfoManager > color_info_manager_
Camera info manager objects.
int data_skip_ir_counter_
void newColorFrameCallback(sensor_msgs::ImagePtr image)
sensor_msgs::CameraInfoPtr getProjectorCameraInfo(int width, int height, ros::Time time) const
#define ROS_WARN_ONCE(...)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
const std::string TYPE_32FC1
const std::string PLUMB_BOB
sensor_msgs::CameraInfoPtr getDepthCameraInfo(int width, int height, ros::Time time) const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
image_transport::CameraPublisher pub_depth_raw_
std::map< int, OpenNI2VideoMode > video_modes_lookup_
image_transport::CameraPublisher pub_color_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
sensor_msgs::CameraInfoPtr getColorCameraInfo(int width, int height, ros::Time time) const
OpenNI2Driver(ros::NodeHandle &n, ros::NodeHandle &pnh)
#define ROS_WARN_STREAM(args)
ros::Duration depth_time_offset_
void monitorConnection(const ros::TimerEvent &event)
virtual const char * what() const
std::string depth_frame_id_
uint32_t getNumSubscribers() const
#define ROS_INFO_STREAM(args)
int data_skip_color_counter_
void newIRFrameCallback(sensor_msgs::ImagePtr image)
std::size_t y_resolution_
bool getParam(const std::string &key, std::string &s) const
int data_skip_depth_counter_
dynamic_reconfigure::Server< Config > ReconfigureServer
sensor_msgs::CameraInfoPtr getIRCameraInfo(int width, int height, ros::Time time) const
ros::Duration ir_time_offset_
int extractBusID(const std::string &uri) const
#define ROS_ERROR_STREAM(args)
ros::Timer timer_
timer for connection monitoring thread
double depth_ir_offset_y_
double depth_ir_offset_x_
void genVideoModeTableMap()
void setIRVideoMode(const OpenNI2VideoMode &ir_video_mode)
bool color_depth_synchronization_
OpenNI2VideoMode depth_video_mode_
OpenNI2VideoMode color_video_mode_
void newDepthFrameCallback(sensor_msgs::ImagePtr image)
void configCb(Config &config, uint32_t level)