Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish projector/camera_info (fixes disparity img) #48

Merged
merged 2 commits into from
Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/openni2_camera/openni2_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class OpenNI2Device
float getIRFocalLength (int output_y_resolution) const;
float getColorFocalLength (int output_y_resolution) const;
float getDepthFocalLength (int output_y_resolution) const;
float getBaseline () const;

void setAutoExposure(bool enable) throw (OpenNI2Exception);
void setAutoWhiteBalance(bool enable) throw (OpenNI2Exception);
Expand Down
2 changes: 2 additions & 0 deletions include/openni2_camera/openni2_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class OpenNI2Driver
sensor_msgs::CameraInfoPtr getColorCameraInfo(int width, int height, ros::Time time) const;
sensor_msgs::CameraInfoPtr getIRCameraInfo(int width, int height, ros::Time time) const;
sensor_msgs::CameraInfoPtr getDepthCameraInfo(int width, int height, ros::Time time) const;
sensor_msgs::CameraInfoPtr getProjectorCameraInfo(int width, int height, ros::Time time) const;

void readConfigFromParameterServer();

Expand Down Expand Up @@ -169,6 +170,7 @@ class OpenNI2Driver
bool color_subscribers_;
bool depth_subscribers_;
bool depth_raw_subscribers_;
bool projector_info_subscribers_;

bool use_device_time_;

Expand Down
15 changes: 15 additions & 0 deletions src/openni2_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include "OpenNI.h"
#include <PS1080.h> // For XN_STREAM_PROPERTY_EMITTER_DCMOS_DISTANCE property

#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/replace.hpp>
Expand Down Expand Up @@ -171,6 +172,20 @@ float OpenNI2Device::getDepthFocalLength(int output_y_resolution) const
return focal_length;
}

float OpenNI2Device::getBaseline() const
{
float baseline = 0.075f;
boost::shared_ptr<openni::VideoStream> stream = getDepthVideoStream();

if (stream && stream->isPropertySupported(XN_STREAM_PROPERTY_EMITTER_DCMOS_DISTANCE))
{
double baseline_meters;
stream->getProperty(XN_STREAM_PROPERTY_EMITTER_DCMOS_DISTANCE, &baseline_meters); // Device specific -- from PS1080.h
baseline = static_cast<float>(baseline_meters * 0.01f); // baseline from cm -> meters
}
return baseline;
}

bool OpenNI2Device::isIRVideoModeSupported(const OpenNI2VideoMode& video_mode) const
{
getSupportedIRVideoModes();
Expand Down
22 changes: 21 additions & 1 deletion src/openni2_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void OpenNI2Driver::advertiseROSTopics()
image_transport::ImageTransport depth_it(depth_nh);
ros::NodeHandle depth_raw_nh(nh_, "depth");
image_transport::ImageTransport depth_raw_it(depth_raw_nh);
ros::NodeHandle projector_nh(nh_, "projector");
// Advertise all published topics

// Prevent connection callbacks from executing until we've set all the publishers. Otherwise
Expand Down Expand Up @@ -117,6 +118,7 @@ void OpenNI2Driver::advertiseROSTopics()
ros::SubscriberStatusCallback rssc = boost::bind(&OpenNI2Driver::depthConnectCb, this);
pub_depth_raw_ = depth_it.advertiseCamera("image_raw", 1, itssc, itssc, rssc, rssc);
pub_depth_ = depth_raw_it.advertiseCamera("image", 1, itssc, itssc, rssc, rssc);
pub_projector_info_ = projector_nh.advertise<sensor_msgs::CameraInfo>("camera_info", 1, rssc, rssc);
}

////////// CAMERA INFO MANAGER
Expand Down Expand Up @@ -360,6 +362,7 @@ void OpenNI2Driver::depthConnectCb()

depth_subscribers_ = pub_depth_.getNumSubscribers() > 0;
depth_raw_subscribers_ = pub_depth_raw_.getNumSubscribers() > 0;
projector_info_subscribers_ = pub_projector_info_.getNumSubscribers() > 0;

bool need_depth = depth_subscribers_ || depth_raw_subscribers_;

Expand Down Expand Up @@ -444,7 +447,7 @@ void OpenNI2Driver::newDepthFrameCallback(sensor_msgs::ImagePtr image)

data_skip_depth_counter_ = 0;

if (depth_raw_subscribers_||depth_subscribers_)
if (depth_raw_subscribers_||depth_subscribers_||projector_info_subscribers_)
{
image->header.stamp = image->header.stamp + depth_time_offset_;

Expand Down Expand Up @@ -486,6 +489,12 @@ void OpenNI2Driver::newDepthFrameCallback(sensor_msgs::ImagePtr image)
sensor_msgs::ImageConstPtr floating_point_image = rawToFloatingPointConversion(image);
pub_depth_.publish(floating_point_image, cam_info);
}

// Projector "info" probably only useful for working with disparity images
if (projector_info_subscribers_)
{
pub_projector_info_.publish(getProjectorCameraInfo(image->width, image->height, image->header.stamp));
}
}
}
}
Expand Down Expand Up @@ -599,6 +608,17 @@ sensor_msgs::CameraInfoPtr OpenNI2Driver::getDepthCameraInfo(int width, int heig
return info;
}

sensor_msgs::CameraInfoPtr OpenNI2Driver::getProjectorCameraInfo(int width, int height, ros::Time time) const
{
// The projector info is simply the depth info with the baseline encoded in the P matrix.
// It's only purpose is to be the "right" camera info to the depth camera's "left" for
// processing disparity images.
sensor_msgs::CameraInfoPtr info = getDepthCameraInfo(width, height, time);
// Tx = -baseline * fx
info->P[3] = -device_->getBaseline() * info->P[0];
return info;
}

void OpenNI2Driver::readConfigFromParameterServer()
{
if (!pnh_.getParam("device_id", device_id_))
Expand Down