lmc_camera_node.cpp
Go to the documentation of this file.
00001 #include <string.h>
00002 #include <boost/shared_ptr.hpp>
00003 #include <sstream>
00004 
00005 #include "ros/ros.h"
00006 #include "sensor_msgs/Image.h"
00007 #include "sensor_msgs/CameraInfo.h"
00008 #include "camera_info_manager/camera_info_manager.h"
00009 #include "rospack/rospack.h"
00010 #include "Leap.h"
00011 
00012 
00013 #define targetWidth 500
00014 #define targetHeight 500
00015 #define cutWidth 280
00016 #define cutHeight 220
00017 #define startX 110
00018 #define startY 140
00019 
00020 using namespace Leap;
00021 using namespace std;
00022 
00023 class CameraListener : public Listener {
00024   public:
00025     boost::shared_ptr <ros::NodeHandle> _left_node;
00026     boost::shared_ptr <ros::NodeHandle> _right_node;
00027 
00028     ros::Publisher _pub_image_left;
00029     ros::Publisher _pub_info_left;
00030     ros::Publisher _pub_image_right;
00031     ros::Publisher _pub_info_right;
00032     camera_info_manager::CameraInfoManager* info_mgr_right;
00033     camera_info_manager::CameraInfoManager* info_mgr_left;
00034     
00035     bool enable_controller_info = false;
00036 
00037     unsigned int seq;
00038     virtual void onInit(const Controller&);
00039     virtual void onConnect(const Controller&);
00040     virtual void onDisconnect(const Controller&);
00041     virtual void onExit(const Controller&);
00042     virtual void onFrame(const Controller&);
00043     virtual void onFocusGained(const Controller&);
00044     virtual void onFocusLost(const Controller&);
00045     virtual void onDeviceChange(const Controller&);
00046     virtual void onServiceConnect(const Controller&);
00047     virtual void onServiceDisconnect(const Controller&);  
00048 };
00049 
00050 
00051 void CameraListener::onInit(const Controller& controller){
00052 
00053     _left_node = boost::make_shared<ros::NodeHandle>("left");
00054     _right_node = boost::make_shared<ros::NodeHandle>("right");
00055 
00056     _pub_image_left = _left_node->advertise<sensor_msgs::Image>("image_raw", 1);
00057     _pub_image_right = _right_node->advertise<sensor_msgs::Image>("image_raw", 1);
00058 
00059     _pub_info_left = _left_node->advertise<sensor_msgs::CameraInfo>("camera_info", 1);
00060     _pub_info_right = _right_node->advertise<sensor_msgs::CameraInfo>("camera_info", 1);
00061     
00062     seq = 0;
00063     rospack::Rospack rp;
00064     std::string path;
00065     std::string default_l_info_filename;
00066     std::string default_r_info_filename;
00067     std::vector<std::string> search_path;
00068 
00069     rp.getSearchPathFromEnv(search_path);
00070     rp.crawl(search_path, 1);
00071 
00072     if ( rp.find("leap_motion",path) == true) 
00073     {
00074         default_l_info_filename = path + std::string("/config/camera_info/leap_cal_left.yml");
00075         default_r_info_filename = path + std::string("/config/camera_info/leap_cal_right.yml");
00076     }
00077     else
00078     {
00079         default_l_info_filename = "";
00080         default_r_info_filename = "";
00081     }
00082 
00083     ros::NodeHandle local_nh("~");
00084     std::string l_info_filename;
00085     std::string r_info_filename;
00086 
00087     local_nh.param("template_filename_left", l_info_filename, default_l_info_filename);
00088     local_nh.param("template_filename_right", r_info_filename, default_r_info_filename);
00089     
00090     l_info_filename = std::string("file://") + l_info_filename;
00091     r_info_filename = std::string("file://") + r_info_filename;
00092     
00093     info_mgr_left = new camera_info_manager::CameraInfoManager(*_left_node, "left", l_info_filename);
00094     info_mgr_right = new camera_info_manager::CameraInfoManager(*_right_node, "right", r_info_filename);
00095 
00096     if(CameraListener::enable_controller_info)
00097     {  
00098         ROS_INFO("CameraListener initialized");
00099     }
00100 }
00101 
00102 void CameraListener::onConnect(const Controller& controller) {
00103     if(CameraListener::enable_controller_info)
00104     {  
00105         ROS_INFO("CameraListener connected");
00106     }
00107 }
00108 
00109 void CameraListener::onDisconnect(const Controller& controller) {
00110     if(CameraListener::enable_controller_info)
00111     {
00112         ROS_INFO("CameraListener disconnected");
00113     }
00114 }
00115 
00116 void CameraListener::onExit(const Controller& controller) {
00117     if(CameraListener::enable_controller_info)
00118     {  
00119         ROS_INFO("CameraListener exited");
00120     }
00121 }
00122 
00123 void CameraListener::onFrame(const Controller& controller) {
00124   // Get the most recent frame and report some basic information
00125   const Frame frame = controller.frame();  
00126   // The list of IR images from the Leap Motion cameras. 
00127   ImageList images = frame.images();
00128   // http://docs.ros.org/api/sensor_msgs/html/msg/Image.html
00129   sensor_msgs::Image image_msg;
00130 
00131   image_msg.header.seq = seq++;
00132   image_msg.header.stamp = ros::Time::now();
00133   // Encoding of pixels -- channel meaning, ordering, size
00134   // taken from the list of strings in include/sensor_msgs/image_encodings.h
00135   image_msg.encoding = "mono8";
00136   image_msg.is_bigendian = 0;
00137   
00138   for(int camera_num = 0; camera_num < 2; camera_num++){
00139     Image image = images[camera_num];
00140     
00141     // image width, that is, number of columns
00142     image_msg.width =  cutWidth;
00143     // image height, that is, number of rows
00144     image_msg.height = cutHeight;
00145     // Full row length in bytes
00146     image_msg.step = cutWidth;
00147     image_msg.data.resize(cutWidth * cutHeight);
00148 
00149     // The parallel construct forms a team of threads and starts parallel execution.
00150     // The loop construct specifies that the iterations of loops will be distributed 
00151     // among and executed by the encountering team of threads.
00152     #pragma omp parallel for
00153     
00154     for(int i = 0; i < cutWidth; i++)
00155     {
00156       for(int j = 0; j < cutHeight; j++)
00157       {
00158         Vector input = Vector((float)(i + startX)/targetWidth, (float)(j + startY)/targetHeight, 0);
00159         input.x = (input.x - image.rayOffsetX()) / image.rayScaleX();
00160         input.y = (input.y - image.rayOffsetY()) / image.rayScaleY();
00161 
00162         Vector pixel = image.warp(input);
00163         if(pixel.x >= 0 && pixel.x < image.width() && pixel.y >= 0 && pixel.y < image.height()) 
00164         {
00165           int data_index = floor(pixel.y) * image.width() + floor(pixel.x);
00166           image_msg.data[cutWidth*j+i] = image.data()[data_index];
00167         } 
00168         else
00169           image_msg.data[cutWidth*j+i] = 0;
00170       }
00171     }
00172 
00173     if(camera_num == 0)
00174     {
00175       sensor_msgs::CameraInfoPtr info_msg(new sensor_msgs::CameraInfo(info_mgr_left -> getCameraInfo() ) );
00176       
00177       image_msg.header.frame_id = info_msg->header.frame_id ="leap_pointcloud";
00178       info_msg->width = image_msg.width;
00179       info_msg->height = image_msg.height;
00180       info_msg->header.stamp = image_msg.header.stamp;
00181       info_msg->header.seq = image_msg.header.seq;
00182       _pub_image_left.publish(image_msg);
00183       _pub_info_left.publish(*info_msg);
00184     }
00185     else
00186     {
00187       sensor_msgs::CameraInfoPtr info_msg(new sensor_msgs::CameraInfo(info_mgr_right->getCameraInfo()));
00188       image_msg.header.frame_id = info_msg->header.frame_id = "lmc_";
00189       info_msg->width = image_msg.width;
00190       info_msg->height = image_msg.height;
00191       info_msg->header.stamp = image_msg.header.stamp;
00192       info_msg->header.seq = image_msg.header.seq;
00193       _pub_image_right.publish(image_msg);
00194       _pub_info_right.publish(*info_msg);
00195     }
00196   }
00197 }
00198 
00199 void CameraListener::onFocusGained(const Controller& controller) {
00200     if(CameraListener::enable_controller_info)
00201     {  
00202         ROS_INFO("CameraListener gained focus");
00203     }
00204 }
00205 
00206 void CameraListener::onFocusLost(const Controller& controller) {
00207     if(CameraListener::enable_controller_info)
00208     {  
00209         ROS_INFO("CameraListener lost focus");
00210     }
00211 }
00212 
00213 void CameraListener::onDeviceChange(const Controller& controller) {
00214     if(CameraListener::enable_controller_info)
00215     {  
00216         ROS_INFO("CameraListener device changed");
00217         const DeviceList devices = controller.devices();
00218         for (int i = 0; i < devices.count(); ++i) {
00219             ROS_INFO( "id: %s", devices[i].toString().c_str() );
00220             ROS_INFO("  isStreaming: %s", (devices[i].isStreaming() ? "true" : "false") );
00221         }
00222     }
00223 }
00224 
00225 void CameraListener::onServiceConnect(const Controller& controller) {
00226     if(CameraListener::enable_controller_info)
00227     {
00228         ROS_INFO("CameraListener service connected");
00229     }
00230 }
00231 
00232 void CameraListener::onServiceDisconnect(const Controller& controller) {
00233     if(CameraListener::enable_controller_info)
00234     {
00235         ROS_INFO("CameraListener service disconnected");
00236     }
00237 }
00238 
00239 int main(int argc, char** argv) {
00240 
00241   ros::init(argc, argv, "leap_motion");
00242   
00243   CameraListener listener;
00244   Controller controller;
00245   controller.addListener(listener);
00246   controller.setPolicyFlags(static_cast<Leap::Controller::PolicyFlag> (Leap::Controller::POLICY_IMAGES));
00247   
00248   ros::spin();
00249   controller.removeListener(listener);
00250 
00251   return 0;
00252 }


leap_motion
Author(s): Florian Lier , Mirza Shah , Isaac IY Saito
autogenerated on Sat Jun 8 2019 18:47:25