packet_controllers.hpp
Go to the documentation of this file.
1 #ifndef USB_CAM_CONTROLLERS_PACKET_CONTROLLERS
2 #define USB_CAM_CONTROLLERS_PACKET_CONTROLLERS
3 
4 #include <cmath>
5 #include <string>
6 
7 #include <cv_bridge/cv_bridge.h>
10 #include <ros/duration.h>
11 #include <ros/node_handle.h>
12 #include <ros/publisher.h>
13 #include <ros/time.h>
14 #include <sensor_msgs/CompressedImage.h>
18 
19 #include <opencv2/core/core.hpp>
20 
21 namespace usb_cam_controllers {
22 
24 public:
26 
27  virtual ~PacketController() {}
28 
29 protected:
31  ros::NodeHandle &controller_nh) {
32  width_ = controller_nh.param("image_width", 640);
33  height_ = controller_nh.param("image_height", 480);
34  encoding_ = controller_nh.param< std::string >("encoding", sensor_msgs::image_encodings::BGR8);
35  skip_max_ = std::max(controller_nh.param("skip", /* pub every packet */ 0), 0);
36 
38  publisher_ = image_transport::ImageTransport(controller_nh).advertise("image", 1);
39 
40  return true;
41  }
42 
43  virtual void startingImpl(const ros::Time &time) {
44  // nothig to do
45  }
46 
47  virtual void updateImpl(const ros::Time &time, const ros::Duration &period) {
48  // publish the current packet if enough number of previous packets are skipped
49  if (skip_cnt_ >= skip_max_) {
50  // reset skip count
51  skip_cnt_ = 0;
52 
53  // publish the packet
55  out.header.stamp = packet_iface_.getStamp();
56  out.encoding = encoding_;
57  out.image = cv::Mat(height_, width_, CV_8UC(packet_iface_.getLength() / (height_ * width_)),
58  const_cast< uint8_t * >(packet_iface_.getStartAs< uint8_t >()));
60  } else {
61  // increment skip count if the current packet is skipped
62  ++skip_cnt_;
63  }
64  }
65 
66  virtual void stoppingImpl(const ros::Time &time) {
67  // nothing to do
68  }
69 
70 private:
71  std::string encoding_;
73  int skip_max_;
74 
75  int skip_cnt_;
77 };
78 
80 public:
82 
84 
85 protected:
87  ros::NodeHandle &controller_nh) {
88  format_ = controller_nh.param< std::string >("format", "jpeg");
89  skip_max_ = std::max(controller_nh.param("skip", /* pub every packet */ 0), 0);
90 
92  publisher_ = controller_nh.advertise< sensor_msgs::CompressedImage >("packet", 1);
93 
94  return true;
95  }
96 
97  virtual void startingImpl(const ros::Time &time) {
98  // nothig to do
99  }
100 
101  virtual void updateImpl(const ros::Time &time, const ros::Duration &period) {
102  // publish the current packet if enough number of previous packets are skipped
103  if (skip_cnt_ >= skip_max_) {
104  // reset skip count
105  skip_cnt_ = 0;
106 
107  // publish the packet
108  const sensor_msgs::CompressedImagePtr msg(new sensor_msgs::CompressedImage());
109  msg->header.stamp = packet_iface_.getStamp();
110  msg->format = format_;
111  msg->data.assign(packet_iface_.getStartAs< uint8_t >(),
113  publisher_.publish(msg);
114  } else {
115  // increment skip count if the current packet is skipped
116  ++skip_cnt_;
117  }
118  }
119 
120  virtual void stoppingImpl(const ros::Time &time) {
121  // nothing to do
122  }
123 
124 private:
125  std::string format_;
127 
130 };
131 
132 } // namespace usb_cam_controllers
133 
134 #endif
virtual void startingImpl(const ros::Time &time)
virtual bool initImpl(usb_cam_hardware_interface::PacketInterface *hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh)
std::string encoding
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
virtual void updateImpl(const ros::Time &time, const ros::Duration &period)
virtual void stoppingImpl(const ros::Time &time)
void publish(const sensor_msgs::Image &message) const
bool param(const std::string &param_name, T &param_val, const T &default_val) const
virtual void updateImpl(const ros::Time &time, const ros::Duration &period)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
virtual void startingImpl(const ros::Time &time)
usb_cam_hardware_interface::PacketHandle packet_iface_
virtual bool initImpl(usb_cam_hardware_interface::PacketInterface *hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh)
virtual void stoppingImpl(const ros::Time &time)
sensor_msgs::ImagePtr toImageMsg() const
std_msgs::Header header


usb_cam_controllers
Author(s):
autogenerated on Tue Jul 14 2020 03:12:08