leap_camera.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string.h>
3 #include "Leap.h"
4 #include "ros/ros.h"
5 #include "sensor_msgs/Image.h"
6 #include "sensor_msgs/CameraInfo.h"
8 #include "rospack/rospack.h"
9 
10 #include <boost/shared_ptr.hpp>
11 #include <sstream>
12 
13 /* ##################################################
14  Deprecated and will be removed in the future
15 ################################################## */
16 
17 #define targetWidth 500
18 #define targetHeight 500
19 #define cutWidth 280
20 #define cutHeight 220
21 #define startX 110
22 #define startY 140
23 
24 using namespace Leap;
25 using namespace std;
26 
27 
28 
29 class CameraListener : public Listener {
30  public:
31  //ros::NodeHandle _node;
34 
41  unsigned int seq;
42  virtual void onInit(const Controller&);
43  virtual void onConnect(const Controller&);
44  virtual void onDisconnect(const Controller&);
45  virtual void onExit(const Controller&);
46  virtual void onFrame(const Controller&);
47  virtual void onFocusGained(const Controller&);
48  virtual void onFocusLost(const Controller&);
49  virtual void onDeviceChange(const Controller&);
50  virtual void onServiceConnect(const Controller&);
51  virtual void onServiceDisconnect(const Controller&);
52  private:
53 };
54 
55 
56 void CameraListener::onInit(const Controller& controller){
57 
58  _left_node = boost::make_shared<ros::NodeHandle>("left");
59  _right_node = boost::make_shared<ros::NodeHandle>("right");
60  std::cout << "Initialized" << std::endl;
61  _pub_image_left = _left_node->advertise<sensor_msgs::Image>("image_raw", 1);
62  _pub_info_left = _left_node->advertise<sensor_msgs::CameraInfo>("camera_info", 1);
63  _pub_image_right = _right_node->advertise<sensor_msgs::Image>("image_raw", 1);
64  _pub_info_right = _right_node->advertise<sensor_msgs::CameraInfo>("camera_info", 1);
65  seq = 0;
66  std::string default_l_info_filename;
67  std::string default_r_info_filename;
69  std::vector<std::string> search_path;
70  rp.getSearchPathFromEnv(search_path);
71  rp.crawl(search_path, 1);
72  std::string path;
73  if (rp.find("leap_motion",path)==true) {
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  default_l_info_filename = "";
79  default_r_info_filename = "";
80  }
81  ros::NodeHandle local_nh("~");
82  std::string l_info_filename;
83  std::string r_info_filename;
84  local_nh.param("template_filename_left", l_info_filename, default_l_info_filename);
85  local_nh.param("template_filename_right", r_info_filename, default_r_info_filename);
86  l_info_filename = std::string("file://") + l_info_filename;
87  r_info_filename = std::string("file://") + r_info_filename;
88  info_mgr_left = new camera_info_manager::CameraInfoManager(*_left_node, "left", l_info_filename);
89  info_mgr_right = new camera_info_manager::CameraInfoManager(*_right_node, "right", r_info_filename);
90 }
91 
92 void CameraListener::onConnect(const Controller& controller) {
93  std::cout << "Connected" << std::endl;
98 }
99 
100 void CameraListener::onDisconnect(const Controller& controller) {
101  // Note: not dispatched when running in a debugger.
102  std::cout << "Disconnected" << std::endl;
103 }
104 
105 void CameraListener::onExit(const Controller& controller) {
106  std::cout << "Exited" << std::endl;
107 }
108 
109 void CameraListener::onFrame(const Controller& controller) {
110  // Get the most recent frame and report some basic information
111  const Frame frame = controller.frame();
112  ImageList images = frame.images();
113 
114  sensor_msgs::Image image_msg;
115  image_msg.header.seq = seq++;
116  image_msg.header.stamp =ros::Time::now();
117  image_msg.encoding ="mono8";
118  image_msg.is_bigendian = 0;
119  for(int camera_num=0; camera_num<2; camera_num++){
120  Image image = images[camera_num];
121  image_msg.width = cutWidth;
122  image_msg.height = cutHeight;
123  image_msg.step = cutWidth;
124  image_msg.data.resize(cutWidth*cutHeight);
125 #pragma omp parallel for
126  for(int i=0; i<cutWidth; i++){
127  for(int j=0; j<cutHeight; j++){
128  Vector input = Vector((float)(i+startX)/targetWidth, (float)(j+startY)/targetHeight, 0);
129  input.x = (input.x - image.rayOffsetX()) / image.rayScaleX();
130  input.y = (input.y - image.rayOffsetY()) / image.rayScaleY();
131  Vector pixel = image.warp(input);
132  if(pixel.x >= 0 && pixel.x < image.width() && pixel.y >= 0 && pixel.y < image.height()) {
133  int data_index = floor(pixel.y) * image.width() + floor(pixel.x);
134  image_msg.data[cutWidth*j+i] = image.data()[data_index];
135  } else {
136  image_msg.data[cutWidth*j+i] = 0;
137  }
138  }
139  }
140  if(camera_num==0){
141  sensor_msgs::CameraInfoPtr info_msg(new sensor_msgs::CameraInfo(info_mgr_left->getCameraInfo()));
142  image_msg.header.frame_id = info_msg->header.frame_id ="leap_optical_frame";
143  info_msg->width = image_msg.width;
144  info_msg->height = image_msg.height;
145  info_msg->header.stamp = image_msg.header.stamp;
146  info_msg->header.seq = image_msg.header.seq;
147  _pub_image_left.publish(image_msg);
148  _pub_info_left.publish(*info_msg);
149  }else{
150  sensor_msgs::CameraInfoPtr info_msg(new sensor_msgs::CameraInfo(info_mgr_right->getCameraInfo()));
151  image_msg.header.frame_id = info_msg->header.frame_id = "leap_optical_frame";
152  info_msg->width = image_msg.width;
153  info_msg->height = image_msg.height;
154  info_msg->header.stamp = image_msg.header.stamp;
155  info_msg->header.seq = image_msg.header.seq;
156  _pub_image_right.publish(image_msg);
157  _pub_info_right.publish(*info_msg);
158  }
159  }
160  // test
161 
162  //to do
163  // int start_x = 100;
164  // int start_y = 100;
165  // int end_x = 300;
166  // int end_y = 300;
167  // int width_x = end_x - start_x;
168  // int width_y = end_y - start_y;
169  // } else {
170  // image_msg.data[width_x*(j-start_y)+(i-start_x)] = 255;
171  // }
172  // }
173  // }
174  //end for test
175 }
176 
177 void CameraListener::onFocusGained(const Controller& controller) {
178  std::cout << "Focus Gained" << std::endl;
179 }
180 
181 void CameraListener::onFocusLost(const Controller& controller) {
182  std::cout << "Focus Lost" << std::endl;
183 }
184 
186  std::cout << "Device Changed" << std::endl;
187  const DeviceList devices = controller.devices();
188 
189  for (int i = 0; i < devices.count(); ++i) {
190  std::cout << "id: " << devices[i].toString() << std::endl;
191  std::cout << " isStreaming: " << (devices[i].isStreaming() ? "true" : "false") << std::endl;
192  }
193 }
194 
196  std::cout << "Service Connected" << std::endl;
197 }
198 
200  std::cout << "Service Disconnected" << std::endl;
201 }
202 
203 int main(int argc, char** argv) {
204  ros::init(argc, argv, "leap_sender");
205  // Create a sample listener and controller
207  Controller controller;
208 
209 
210 
211  // Have the sample listener receive events from the controller
212  controller.addListener(listener);
213 
214  controller.setPolicyFlags(static_cast<Leap::Controller::PolicyFlag> (Leap::Controller::POLICY_IMAGES));
215  ros::spin();
216  // Remove the sample listener when done
217  controller.removeListener(listener);
218 
219  return 0;
220 }
#define cutHeight
Definition: leap_camera.cpp:20
LEAP_EXPORT void setPolicyFlags(PolicyFlag flags) const
camera_info_manager::CameraInfoManager * info_mgr_right
Definition: leap_camera.cpp:39
LEAP_EXPORT void enableGesture(Gesture::Type type, bool enable=true) const
LEAP_EXPORT bool addListener(Listener &listener)
virtual void onFrame(const Controller &)
virtual void onExit(const Controller &)
virtual void onDeviceChange(const Controller &)
#define targetWidth
Definition: leap_camera.cpp:17
#define startY
Definition: leap_camera.cpp:22
#define cutWidth
Definition: leap_camera.cpp:19
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
ros::Publisher _pub_image_left
Definition: leap_camera.cpp:35
ros::Publisher _pub_image_right
Definition: leap_camera.cpp:37
unsigned int seq
Definition: leap_camera.cpp:41
LEAP_EXPORT float rayScaleX() const
ros::Publisher _pub_info_right
Definition: leap_camera.cpp:38
bool param(const std::string &param_name, T &param_val, const T &default_val) const
#define startX
Definition: leap_camera.cpp:21
LEAP_EXPORT float rayOffsetY() const
int main(int argc, char **argv)
boost::shared_ptr< ros::NodeHandle > _right_node
Definition: leap_camera.cpp:33
bool getSearchPathFromEnv(std::vector< std::string > &sp)
boost::shared_ptr< ros::NodeHandle > _left_node
Definition: leap_camera.cpp:32
LEAP_EXPORT int count() const
ros::Publisher _pub_info_left
Definition: leap_camera.cpp:36
void crawl(std::vector< std::string > search_path, bool force)
virtual void onFocusGained(const Controller &)
#define targetHeight
Definition: leap_camera.cpp:18
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 &)
camera_info_manager::CameraInfoManager * info_mgr_left
Definition: leap_camera.cpp:40


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