foscam_8918_driver.cpp
Go to the documentation of this file.
00001 #include "foscam_8918_driver/foscam_8918_driver.h"
00002 
00003 namespace Foscam8918Driver
00004 {
00005 
00006 Foscam8918::Foscam8918(ros::NodeHandle nh_) :
00007     it_(new image_transport::ImageTransport(nh_)),
00008     image_pub_(it_->advertiseCamera("image_raw", 1)),
00009     camera_info_manager_(new camera_info_manager::CameraInfoManager(nh_))
00010 {
00011   // Set up a dynamic reconfigure server.
00012   reconfig_cb_ = boost::bind(&Foscam8918Driver::Foscam8918::configCallback, this, _1, _2);
00013   reconfig_srv_.setCallback(reconfig_cb_);
00014 
00015   // Initialize node parameters.
00016   ros::NodeHandle pnh("~");
00017   pnh.param("username",   username_,   std::string("change_me"));
00018   pnh.param("password",   password_,   std::string("change_me"));
00019   pnh.param("ip_address", ip_address_, std::string("192.168.1.1"));
00020   pnh.param("port",       port_,       std::string("80"));
00021   pnh.param("url_suffix", url_suffix_, std::string("video.cgi?.mjpg"));
00022   pnh.param("rate",       rate_,       int(10));
00023   if (rate_ <= 0)
00024   {
00025     rate_ = 1;
00026   }
00027 
00028   // Create a timer callback.
00029   ros::Timer timer = nh_.createTimer(ros::Duration(1.0/rate_), &Foscam8918Driver::Foscam8918::timerCallback, this);
00030 
00031   // Connect to the camera.
00032   connectToCamera();
00033 
00034   // Let callbacks take over.
00035   ros::spin();
00036 }
00037 
00038 Foscam8918::~Foscam8918()
00039 {
00040 }
00041 
00042 bool Foscam8918::connectToCamera()
00043 {
00044   // URL of camera video stream.
00045   const std::string video_stream_address = "http://" + username_ + ":" + password_ + "@" + ip_address_ + ":" + port_ + "/" + url_suffix_;
00046 
00047   // Open the video stream and make sure it's opened.
00048   have_connection_ = true;
00049   if (!vcap_.open(video_stream_address))
00050   {
00051     have_connection_ = false;
00052     ROS_ERROR("Error opening video stream or file");
00053     return false;
00054   }
00055 
00056   return true;
00057 }
00058 
00059 void Foscam8918::timerCallback(const ros::TimerEvent& event)
00060 {
00061   cv::Mat image;
00062 
00063   if (have_connection_)
00064   {
00065     if (!vcap_.read(image))
00066     {
00067       ROS_WARN("No frame");
00068       cv::waitKey();
00069     }
00071     cv_img_.header.stamp = ros::Time::now();
00072     cv_img_.header.frame_id = "foscam";
00073     cv_img_.encoding = "bgr8";
00074     cv_img_.image = image;
00075 
00076     sensor_msgs::CameraInfoPtr ci(new sensor_msgs::CameraInfo(camera_info_manager_->getCameraInfo()));
00077     ci->height = image.rows;
00078     ci->width = image.cols;
00079     ci->header = cv_img_.header;
00080 
00081     image_pub_.publish(cv_img_.toImageMsg(), ci);
00082   }
00083 }
00084 
00085 void Foscam8918::configCallback(foscam_8918_driver::foscam_8918_driverConfig &config, uint32_t level)
00086 {
00087   username_ = config.username;
00088   password_ = config.password;
00089   ip_address_ = config.ip_address;
00090   port_ = config.port;
00091   url_suffix_ = config.url_suffix;
00092 
00093   if (config.reset_connection)
00094   {
00095     config.reset_connection = false;
00096     connectToCamera();
00097     ROS_INFO("Resetting connection to camera with current parameters.");
00098   }
00099 }
00100 
00101 }


foscam_8918_driver
Author(s): Thomas Denewiler
autogenerated on Fri Aug 28 2015 10:41:49