raspicam_ros_pub.cpp
Go to the documentation of this file.
1 //
2 // Created by acs-lab on 8/9/17.
3 //
4 
5 #include <ctime>
6 #include <iostream>
7 #include <ros/ros.h>
8 #include <raspicam/raspicam_cv.h>
9 #include <image_transport/image_transport.h>
10 #include <cv_bridge/cv_bridge.h>
12 #include <opencv2/highgui/highgui.hpp>
13 
14 int main(int argc, char **argv) {
15  // Variables
16  raspicam::RaspiCam_Cv Camera;
17  cv::Mat cv_image;
18  char c;
19  int delay = 30; // 30ms delay
20  const char *WIN_NAME = "Test Window";
21 
22  // Create ROS Node and Publisher
23  ros::init(argc, argv, "raspicam_publisher");
24  ros::NodeHandle node_obj;
25  image_transpor::ImageTransport it(node_obj);
26  image_transport::Publisher pub = it.advertise("camera/image", 1);
27  ros::Rate loop_rate(30);
28 
29  // Create Message Variable
30  sensor_msgs::ImagePtr msg;
31 
32  // Set camera parameters
33  Camera.set(CV_CAP_PROP_FRAME_WIDTH, 640); // Default 1280
34  Camera.set(CV_CAP_PROP_FRAME_HEIGHT, 480); // Default 960
35  // Camera.set(CV_CAP_PROP_BRIGHTNESS, 50); // Default 50
36  // Camera.set(CV_CAP_PROP_CONTRAST, 50); // Default 50
37  // Camera.set(CV_CAP_PROP_SATURATION, 50); // Default 50
38  // Camera.set(CV_CAP_PROP_GAIN, 50); // Default 50
39  // Camera.set(CV_CAP_PROP_FORMAT, CV_8UC1); // Default CV_8UC1
40 
41  // Open Camera
42  ROS_INFO("Opening Camera...");
43 
44  if (!Camera.open()) {
45  ROS_ERROR("Error opening the camera!");
46  }
47 
48  while (node_obj.ok()) {
49  // Grab a new image
50  Camera.grab();
51  Camera.retrieve(cv_image);
52 
53  // Convert OpenCV image to ROS message and Publish Message
54  msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", cv_image).toImageMsg();
55  pub.publish(msg);
56 
57  // Exit the loop if 'ESC' key is pressed for longer than delay time.
58  c = (char) cvWaitKey(delay);
59  if (c == 27)
60  break;
61 
62  // Spin and Sleep
63  ros::spinOnce();
64  loop_rate.sleep();
65  }
66 
67  // Release Pi Cam
68  Camera.release();
69 
70  return 0;
71 
72 
73 }
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
#define ROS_INFO(...)
bool sleep()
bool ok() const
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)


pheeno_ros
Author(s): Zahi Kakish, Sean Wilson
autogenerated on Mon Jun 10 2019 14:10:48