raspicam_ros_pub.cpp
Go to the documentation of this file.
00001 //
00002 // Created by acs-lab on 8/9/17.
00003 //
00004 
00005 #include <ctime>
00006 #include <iostream>
00007 #include <ros/ros.h>
00008 #include <raspicam/raspicam_cv.h>
00009 #include <image_transport/image_transport.h>
00010 #include <cv_bridge/cv_bridge.h>
00011 #include <sensor_msgs/image_encodings.h>
00012 #include <opencv2/highgui/highgui.hpp>
00013 
00014 int main(int argc, char **argv) {
00015   // Variables
00016   raspicam::RaspiCam_Cv Camera;
00017   cv::Mat cv_image;
00018   char c;
00019   int delay = 30;  // 30ms delay
00020   const char *WIN_NAME = "Test Window";
00021 
00022   // Create ROS Node and Publisher
00023   ros::init(argc, argv, "raspicam_publisher");
00024   ros::NodeHandle node_obj;
00025   image_transpor::ImageTransport it(node_obj);
00026   image_transport::Publisher pub = it.advertise("camera/image", 1);
00027   ros::Rate loop_rate(30);
00028 
00029   // Create Message Variable
00030   sensor_msgs::ImagePtr msg;
00031 
00032   // Set camera parameters
00033   Camera.set(CV_CAP_PROP_FRAME_WIDTH, 640);   // Default  1280
00034   Camera.set(CV_CAP_PROP_FRAME_HEIGHT, 480);  // Default   960
00035   // Camera.set(CV_CAP_PROP_BRIGHTNESS, 50);  // Default    50
00036   // Camera.set(CV_CAP_PROP_CONTRAST, 50);    // Default    50
00037   // Camera.set(CV_CAP_PROP_SATURATION, 50);  // Default    50
00038   // Camera.set(CV_CAP_PROP_GAIN, 50);        // Default    50
00039   // Camera.set(CV_CAP_PROP_FORMAT, CV_8UC1); // Default CV_8UC1
00040 
00041   // Open Camera
00042   ROS_INFO("Opening Camera...");
00043 
00044   if (!Camera.open()) {
00045     ROS_ERROR("Error opening the camera!");
00046   }
00047 
00048   while (node_obj.ok()) {
00049     // Grab a new image
00050     Camera.grab();
00051     Camera.retrieve(cv_image);
00052 
00053     // Convert OpenCV image to ROS message and Publish Message
00054     msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", cv_image).toImageMsg();
00055     pub.publish(msg);
00056 
00057     // Exit the loop if 'ESC' key is pressed for longer than delay time.
00058     c = (char) cvWaitKey(delay);
00059     if (c == 27)
00060       break;
00061 
00062     // Spin and Sleep
00063     ros::spinOnce();
00064     loop_rate.sleep();
00065   }
00066 
00067   // Release Pi Cam
00068   Camera.release();
00069 
00070   return 0;
00071 
00072 
00073 }


pheeno_ros
Author(s): Zahi Kakish, Sean Wilson
autogenerated on Thu Jun 6 2019 21:26:16