openni_node.cpp
Go to the documentation of this file.
00001 #include "openni_cam.h"
00002 
00003 #include <ros/ros.h>
00004 #include <image_transport/image_transport.h>
00005 #include <sensor_msgs/image_encodings.h>
00006 
00007 #include <tf/tf.h>
00008 #include <tf/transform_listener.h>
00009 #include <tf/transform_datatypes.h>
00010 #include <tf/transform_broadcaster.h>
00011 
00012 #include <stdio.h>
00013 //#include "error.h"
00014 #include <cmath>
00015 
00016 image_transport::Publisher publisher1;
00017 image_transport::Publisher publisher2;
00018 
00019 int main(int argc, char **argv) {
00020     ros::init(argc, argv, "openni_node");
00021     ros::NodeHandle nh;
00022     image_transport::ImageTransport it(nh);
00023     publisher1 = it.advertise("openni_camera/rgb", 1);
00024     publisher2 = it.advertise("openni_camera/depth", 1);
00025     ros::Publisher publisher_rgb_info = nh.advertise<sensor_msgs::CameraInfo>("openni_camera/camera_info", 1);
00026     //
00027     openni_cam::openni_cam cam;
00028     cam.init();
00029     ros::Rate r(5);
00030     int count = 0;
00031     printf("openni_node %4.4i  ",(count%10000)); fflush(stdout);
00032     fflush(stdout);
00033     while (ros::ok()) {
00034         ros::spinOnce();
00035         cam.get_image();
00036         ++count;
00037         printf("\b\b\b\b\b\b%4.4i  ",(count%10000));
00038         fflush(stdout);
00039         ros::Time t1 = ros::Time::now();
00040         //
00041         {
00042             sensor_msgs::ImagePtr msg(new sensor_msgs::Image());
00043             msg->header.stamp = t1;
00044             int num_bytes = cam.rgb.byte_size();
00045             msg->data.resize(num_bytes);
00046             memcpy(&msg->data[0],cam.rgb.begin(),num_bytes);
00047             msg->encoding = sensor_msgs::image_encodings::RGB8;
00048             msg->step = cam.rgb.byte_stride(0,0,1);
00049             msg->width = cam.rgb.size()[1];
00050             msg->height = cam.rgb.size()[2];
00051             msg->header.frame_id = "openni_camera";
00052             msg->header.seq = count;
00053             publisher1.publish(msg);
00054 //
00055             sensor_msgs::CameraInfoPtr ci(new sensor_msgs::CameraInfo());
00056             ci->header = msg->header;
00057             ci->height = msg->height;
00058             ci->width = msg->width;
00059             for(int i=0; i<3; ++i) {
00060                 ci->P[i*4 + 3] = 0;
00061                 for(int j=0; j<3; ++j) {
00062                     ci->R[i*3+j] = (i==j);
00063                     ci->P[i*4+j] = cam.K(i,j);
00064                     ci->K[i*3+j] = cam.K(i,j);
00065                 };
00066             }
00067             ci->distortion_model = "";//"plumb_bob";
00068             //
00069             ci->roi.height = msg->height;
00070             ci->roi.width = msg->width;
00071             ci->binning_x = 1;
00072             ci->binning_y = 1;
00073             publisher_rgb_info.publish(ci);
00074         };
00075         {
00076             sensor_msgs::ImagePtr msg(new sensor_msgs::Image());
00077             msg->header.stamp = t1;
00078             int num_bytes = cam.depth.byte_size();
00079             msg->data.resize(num_bytes);
00080             memcpy(&msg->data[0],cam.depth.begin(),num_bytes);
00081             msg->encoding = sensor_msgs::image_encodings::MONO16;
00082             msg->step = cam.depth.byte_stride(0,1);
00083             msg->width = cam.depth.size()[0];
00084             msg->height = cam.depth.size()[1];
00085             msg->header.frame_id = "openni_camera";
00086             msg->header.seq = count;
00087             publisher2.publish(msg);
00088         };
00089         static tf::TransformBroadcaster br;
00090         tf::Transform transform;
00091         transform.setOrigin( tf::Vector3(0, 0, 0) );
00092         transform.setRotation( tf::Quaternion(0, 0, 0) );
00093         br.sendTransform(tf::StampedTransform(transform, t1, "/ptu_mounting", "/openni_camera"));
00094         r.sleep();
00095         //usleep(1000);
00096     };
00097 };
 All Classes Namespaces Files Functions Variables Typedefs Defines


openni_cam
Author(s): Alexander Shekhovtsov
autogenerated on Sun Dec 1 2013 17:19:20