mono_camera.cpp
Go to the documentation of this file.
1 
34 
35 #define DEBUG_PRINTS 1
36 
37 namespace avt_vimba_camera {
38 
39 MonoCamera::MonoCamera(ros::NodeHandle nh, ros::NodeHandle nhp) : nh_(nh), nhp_(nhp), it_(nhp), cam_(ros::this_node::getName()) {
40  // Prepare node handle for the camera
41  // TODO use nodelets with getMTNodeHandle()
42 
43  // Start Vimba & list all available cameras
44  api_.start();
45 
46  // Set the image publisher before the streaming
47  pub_ = it_.advertiseCamera("image_raw", 1);
48 
49  // Set the frame callback
51 
52  // Set the params
53  nhp_.param("ip", ip_, std::string(""));
54  nhp_.param("guid", guid_, std::string(""));
55  nhp_.param("camera_info_url", camera_info_url_, std::string(""));
56  std::string frame_id;
57  nhp_.param("frame_id", frame_id, std::string(""));
58  nhp_.param("show_debug_prints", show_debug_prints_, false);
59 
60  // Set camera info manager
62 
63  // Start dynamic_reconfigure & run configure()
64  reconfigure_server_.setCallback(boost::bind(&avt_vimba_camera::MonoCamera::configure, this, _1, _2));
65 }
66 
68  cam_.stop();
69  pub_.shutdown();
70 }
71 
72 void MonoCamera::frameCallback(const FramePtr& vimba_frame_ptr) {
73  ros::Time ros_time = ros::Time::now();
74  if (pub_.getNumSubscribers() > 0) {
75  sensor_msgs::Image img;
76  if (api_.frameToImage(vimba_frame_ptr, img)) {
77  sensor_msgs::CameraInfo ci = info_man_->getCameraInfo();
78  ci.header.stamp = img.header.stamp = ros_time;
79  img.header.frame_id = ci.header.frame_id;
80  pub_.publish(img, ci);
81  } else {
82  ROS_WARN_STREAM("Function frameToImage returned 0. No image published.");
83  }
84  }
85  // updater_.update();
86 }
87 
97 void MonoCamera::configure(Config& newconfig, uint32_t level) {
98  try {
99  // resolve frame ID using tf_prefix parameter
100  if (newconfig.frame_id == "") {
101  newconfig.frame_id = "camera";
102  }
103  // The camera already stops & starts acquisition
104  // so there's no problem on changing any feature.
105  if (!cam_.isOpened()) {
107  }
108 
109  Config config = newconfig;
110  cam_.updateConfig(newconfig);
111  updateCameraInfo(config);
112  } catch (const std::exception& e) {
113  ROS_ERROR_STREAM("Error reconfiguring mono_camera node : " << e.what());
114  }
115 }
116 
117 void MonoCamera::updateCameraInfo(const avt_vimba_camera::AvtVimbaCameraConfig& config) {
118 
119  // Get camera_info from the manager
120  sensor_msgs::CameraInfo ci = info_man_->getCameraInfo();
121 
122  // Set the frame id
123  ci.header.frame_id = config.frame_id;
124 
125  // Set the operational parameters in CameraInfo (binning, ROI)
126  int binning_or_decimation_x = std::max(config.binning_x, config.decimation_x);
127  int binning_or_decimation_y = std::max(config.binning_y, config.decimation_y);
128 
129  // Set the operational parameters in CameraInfo (binning, ROI)
130  ci.height = config.height;
131  ci.width = config.width;
132  ci.binning_x = binning_or_decimation_x;
133  ci.binning_y = binning_or_decimation_y;
134 
135  // ROI in CameraInfo is in unbinned coordinates, need to scale up
136  ci.roi.x_offset = config.roi_offset_x;
137  ci.roi.y_offset = config.roi_offset_y;
138  ci.roi.height = config.roi_height;
139  ci.roi.width = config.roi_width;
140 
141  // set the new URL and load CameraInfo (if any) from it
142  std::string camera_info_url;
143  nhp_.getParam("camera_info_url", camera_info_url);
144  if (camera_info_url != camera_info_url_) {
145  info_man_->setCameraName(config.frame_id);
146  if (info_man_->validateURL(camera_info_url)) {
147  info_man_->loadCameraInfo(camera_info_url);
148  ci = info_man_->getCameraInfo();
149  } else {
150  ROS_WARN_STREAM("Camera info URL not valid: " << camera_info_url);
151  }
152  }
153 
154  bool roiMatchesCalibration = (ci.height == config.roi_height
155  && ci.width == config.roi_width);
156  bool resolutionMatchesCalibration = (ci.width == config.width
157  && ci.height == config.height);
158  // check
159  ci.roi.do_rectify = roiMatchesCalibration || resolutionMatchesCalibration;
160 
161  // push the changes to manager
162  info_man_->setCameraInfo(ci);
163 }
164 
165 };
void configure(Config &newconfig, uint32_t level)
Definition: mono_camera.cpp:97
ReconfigureServer reconfigure_server_
Definition: mono_camera.h:82
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
boost::shared_ptr< camera_info_manager::CameraInfoManager > info_man_
Definition: mono_camera.h:77
NetPointer< Frame, AVT::VmbAPINET::Frame > FramePtr
image_transport::ImageTransport it_
Definition: mono_camera.h:70
uint32_t getNumSubscribers() const
std::string getName(void *handle)
void setCallback(frameCallbackFunc callback=&avt_vimba_camera::AvtVimbaCamera::defaultFrameCallback)
bool frameToImage(const FramePtr vimba_frame_ptr, sensor_msgs::Image &image)
avt_vimba_camera::AvtVimbaCameraConfig Config
Definition: mono_camera.h:80
CameraPublisher advertiseCamera(const std::string &base_topic, uint32_t queue_size, bool latch=false)
void start(std::string ip_str, std::string guid_str, bool debug_prints=true)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
MonoCamera(ros::NodeHandle nh, ros::NodeHandle nhp)
Definition: mono_camera.cpp:39
image_transport::CameraPublisher pub_
Definition: mono_camera.h:72
void updateCameraInfo(const Config &config)
#define ROS_WARN_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
static Time now()
void frameCallback(const FramePtr &vimba_frame_ptr)
Definition: mono_camera.cpp:72
#define ROS_ERROR_STREAM(args)


avt_vimba_camera
Author(s): Miquel Massot , Allied Vision Technologies
autogenerated on Wed Jun 5 2019 22:22:40