lmc_camera_node.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <boost/shared_ptr.hpp>
3 #include <sstream>
4 
5 #include "ros/ros.h"
6 #include "sensor_msgs/Image.h"
7 #include "sensor_msgs/CameraInfo.h"
9 #include "rospack/rospack.h"
10 #include "Leap.h"
11 
12 
13 #define targetWidth 500
14 #define targetHeight 500
15 #define cutWidth 280
16 #define cutHeight 220
17 #define startX 110
18 #define startY 140
19 
20 using namespace Leap;
21 using namespace std;
22 
23 class CameraListener : public Listener {
24  public:
27 
28  ros::Publisher _pub_image_left;
29  ros::Publisher _pub_info_left;
30  ros::Publisher _pub_image_right;
31  ros::Publisher _pub_info_right;
34 
35  bool enable_controller_info = false;
36 
37  unsigned int seq;
38  virtual void onInit(const Controller&);
39  virtual void onConnect(const Controller&);
40  virtual void onDisconnect(const Controller&);
41  virtual void onExit(const Controller&);
42  virtual void onFrame(const Controller&);
43  virtual void onFocusGained(const Controller&);
44  virtual void onFocusLost(const Controller&);
45  virtual void onDeviceChange(const Controller&);
46  virtual void onServiceConnect(const Controller&);
47  virtual void onServiceDisconnect(const Controller&);
48 };
49 
50 
51 void CameraListener::onInit(const Controller& controller){
52 
53  _left_node = boost::make_shared<ros::NodeHandle>("left");
54  _right_node = boost::make_shared<ros::NodeHandle>("right");
55 
56  _pub_image_left = _left_node->advertise<sensor_msgs::Image>("image_raw", 1);
57  _pub_image_right = _right_node->advertise<sensor_msgs::Image>("image_raw", 1);
58 
59  _pub_info_left = _left_node->advertise<sensor_msgs::CameraInfo>("camera_info", 1);
60  _pub_info_right = _right_node->advertise<sensor_msgs::CameraInfo>("camera_info", 1);
61 
62  seq = 0;
64  std::string path;
65  std::string default_l_info_filename;
66  std::string default_r_info_filename;
67  std::vector<std::string> search_path;
68 
69  rp.getSearchPathFromEnv(search_path);
70  rp.crawl(search_path, 1);
71 
72  if ( rp.find("leap_motion",path) == true)
73  {
74  default_l_info_filename = path + std::string("/config/camera_info/leap_cal_left.yml");
75  default_r_info_filename = path + std::string("/config/camera_info/leap_cal_right.yml");
76  }
77  else
78  {
79  default_l_info_filename = "";
80  default_r_info_filename = "";
81  }
82 
83  ros::NodeHandle local_nh("~");
84  std::string l_info_filename;
85  std::string r_info_filename;
86 
87  local_nh.param("template_filename_left", l_info_filename, default_l_info_filename);
88  local_nh.param("template_filename_right", r_info_filename, default_r_info_filename);
89 
90  l_info_filename = std::string("file://") + l_info_filename;
91  r_info_filename = std::string("file://") + r_info_filename;
92 
93  info_mgr_left = new camera_info_manager::CameraInfoManager(*_left_node, "left", l_info_filename);
94  info_mgr_right = new camera_info_manager::CameraInfoManager(*_right_node, "right", r_info_filename);
95 
97  {
98  ROS_INFO("CameraListener initialized");
99  }
100 }
101 
102 void CameraListener::onConnect(const Controller& controller) {
104  {
105  ROS_INFO("CameraListener connected");
106  }
107 }
108 
109 void CameraListener::onDisconnect(const Controller& controller) {
111  {
112  ROS_INFO("CameraListener disconnected");
113  }
114 }
115 
116 void CameraListener::onExit(const Controller& controller) {
118  {
119  ROS_INFO("CameraListener exited");
120  }
121 }
122 
123 void CameraListener::onFrame(const Controller& controller) {
124  // Get the most recent frame and report some basic information
125  const Frame frame = controller.frame();
126  // The list of IR images from the Leap Motion cameras.
127  ImageList images = frame.images();
128  // http://docs.ros.org/api/sensor_msgs/html/msg/Image.html
129  sensor_msgs::Image image_msg;
130 
131  image_msg.header.seq = seq++;
132  image_msg.header.stamp = ros::Time::now();
133  // Encoding of pixels -- channel meaning, ordering, size
134  // taken from the list of strings in include/sensor_msgs/image_encodings.h
135  image_msg.encoding = "mono8";
136  image_msg.is_bigendian = 0;
137 
138  for(int camera_num = 0; camera_num < 2; camera_num++){
139  Image image = images[camera_num];
140 
141  // image width, that is, number of columns
142  image_msg.width = cutWidth;
143  // image height, that is, number of rows
144  image_msg.height = cutHeight;
145  // Full row length in bytes
146  image_msg.step = cutWidth;
147  image_msg.data.resize(cutWidth * cutHeight);
148 
149  // The parallel construct forms a team of threads and starts parallel execution.
150  // The loop construct specifies that the iterations of loops will be distributed
151  // among and executed by the encountering team of threads.
152  #pragma omp parallel for
153 
154  for(int i = 0; i < cutWidth; i++)
155  {
156  for(int j = 0; j < cutHeight; j++)
157  {
158  Vector input = Vector((float)(i + startX)/targetWidth, (float)(j + startY)/targetHeight, 0);
159  input.x = (input.x - image.rayOffsetX()) / image.rayScaleX();
160  input.y = (input.y - image.rayOffsetY()) / image.rayScaleY();
161 
162  Vector pixel = image.warp(input);
163  if(pixel.x >= 0 && pixel.x < image.width() && pixel.y >= 0 && pixel.y < image.height())
164  {
165  int data_index = floor(pixel.y) * image.width() + floor(pixel.x);
166  image_msg.data[cutWidth*j+i] = image.data()[data_index];
167  }
168  else
169  image_msg.data[cutWidth*j+i] = 0;
170  }
171  }
172 
173  if(camera_num == 0)
174  {
175  sensor_msgs::CameraInfoPtr info_msg(new sensor_msgs::CameraInfo(info_mgr_left -> getCameraInfo() ) );
176 
177  image_msg.header.frame_id = info_msg->header.frame_id ="leap_pointcloud";
178  info_msg->width = image_msg.width;
179  info_msg->height = image_msg.height;
180  info_msg->header.stamp = image_msg.header.stamp;
181  info_msg->header.seq = image_msg.header.seq;
182  _pub_image_left.publish(image_msg);
183  _pub_info_left.publish(*info_msg);
184  }
185  else
186  {
187  sensor_msgs::CameraInfoPtr info_msg(new sensor_msgs::CameraInfo(info_mgr_right->getCameraInfo()));
188  image_msg.header.frame_id = info_msg->header.frame_id = "lmc_";
189  info_msg->width = image_msg.width;
190  info_msg->height = image_msg.height;
191  info_msg->header.stamp = image_msg.header.stamp;
192  info_msg->header.seq = image_msg.header.seq;
193  _pub_image_right.publish(image_msg);
194  _pub_info_right.publish(*info_msg);
195  }
196  }
197 }
198 
199 void CameraListener::onFocusGained(const Controller& controller) {
201  {
202  ROS_INFO("CameraListener gained focus");
203  }
204 }
205 
206 void CameraListener::onFocusLost(const Controller& controller) {
208  {
209  ROS_INFO("CameraListener lost focus");
210  }
211 }
212 
213 void CameraListener::onDeviceChange(const Controller& controller) {
215  {
216  ROS_INFO("CameraListener device changed");
217  const DeviceList devices = controller.devices();
218  for (int i = 0; i < devices.count(); ++i) {
219  ROS_INFO( "id: %s", devices[i].toString().c_str() );
220  ROS_INFO(" isStreaming: %s", (devices[i].isStreaming() ? "true" : "false") );
221  }
222  }
223 }
224 
225 void CameraListener::onServiceConnect(const Controller& controller) {
227  {
228  ROS_INFO("CameraListener service connected");
229  }
230 }
231 
232 void CameraListener::onServiceDisconnect(const Controller& controller) {
234  {
235  ROS_INFO("CameraListener service disconnected");
236  }
237 }
238 
239 int main(int argc, char** argv) {
240 
241  ros::init(argc, argv, "leap_motion");
242 
244  Controller controller;
245  controller.addListener(listener);
246  controller.setPolicyFlags(static_cast<Leap::Controller::PolicyFlag> (Leap::Controller::POLICY_IMAGES));
247 
248  ros::spin();
249  controller.removeListener(listener);
250 
251  return 0;
252 }
LEAP_EXPORT void setPolicyFlags(PolicyFlag flags) const
LEAP_EXPORT bool addListener(Listener &listener)
virtual void onFrame(const Controller &)
#define targetWidth
virtual void onExit(const Controller &)
#define cutHeight
virtual void onDeviceChange(const Controller &)
#define targetHeight
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
def listener()
Definition: subscriber.py:23
virtual void onInit(const Controller &)
Definition: leap_camera.cpp:56
virtual void onServiceDisconnect(const Controller &)
ROSCPP_DECL void spin(Spinner &spinner)
Definition: Leap.h:48
LEAP_EXPORT Vector warp(const Vector &xy) const
#define startX
LEAP_EXPORT float rayScaleX() const
#define ROS_INFO(...)
LEAP_EXPORT float rayOffsetY() const
#define cutWidth
bool getSearchPathFromEnv(std::vector< std::string > &sp)
LEAP_EXPORT int count() const
void crawl(std::vector< std::string > search_path, bool force)
int main(int argc, char **argv)
virtual void onFocusGained(const Controller &)
bool find(const std::string &name, std::string &path)
static Time now()
LEAP_EXPORT bool removeListener(Listener &listener)
virtual void onConnect(const Controller &)
Definition: leap_camera.cpp:92
LEAP_EXPORT float rayOffsetX() const
LEAP_EXPORT Frame frame(int history=0) const
virtual void onServiceConnect(const Controller &)
LEAP_EXPORT float rayScaleY() const
virtual void onFocusLost(const Controller &)
virtual void onDisconnect(const Controller &)
#define startY


leap_motion
Author(s): Florian Lier , Mirza Shah , Isaac IY Saito
autogenerated on Tue Jun 2 2020 03:58:01