detect.cpp
Go to the documentation of this file.
00001 /*************************************************************************
00002         > File Name: robot_detect.cpp
00003         > Author: Minglong Li
00004         > Mail: minglong_l@163.com
00005         > Created on: Aug. 12th, 2016
00006  ************************************************************************/
00007 
00008 #include <iostream>
00009 #include <ros/ros.h>
00010 #include <nodelet/nodelet.h>
00011 
00012 #include <sensor_msgs/Image.h>
00013 #include <sensor_msgs/image_encodings.h>
00014 #include <std_msgs/Bool.h>
00015 #include <vector>
00016 #include <cv_bridge/cv_bridge.h>
00017 
00018 #include <opencv2/highgui/highgui.hpp>
00019 #include <opencv2/core/core.hpp>
00020 #include <opencv2/imgproc/imgproc.hpp>
00021 
00022 #include <pluginlib/class_list_macros.h>
00023 
00024 namespace micros_mars_task_alloc{
00025     using namespace std;
00026     class RobotDetect : public nodelet::Nodelet
00027     {
00028     public:
00029         RobotDetect(){}
00030         virtual ~RobotDetect(){}
00031 
00032         virtual void onInit();
00033         
00034         void callback(const sensor_msgs::ImageConstPtr & msg);
00035 
00036     private:    
00037         ros::NodeHandle nh_;
00038         ros::Subscriber sub_; 
00039         ros::Publisher pub_;
00040         std::string output_topic_;
00041         //std::bool detected_flag;
00042     };
00043     void RobotDetect::onInit()
00044     {
00045         nh_ = getMTPrivateNodeHandle();
00046         if (!(nh_.getParam("output_topic", output_topic_)))
00047         {
00048             std::cout << "Fail to get the parameter output_topic." << std::endl;
00049             return;
00050         } 
00051         pub_ = nh_.advertise<std_msgs::Bool>(output_topic_, 10);                  
00052         sub_ = nh_.subscribe("/robot_0/image", 10, &RobotDetect::callback, this);//the topic here does not change
00053     }
00054     void RobotDetect::callback(const sensor_msgs::ImageConstPtr & msg)
00055     {
00056         cv_bridge::CvImagePtr cv_ptr;
00057         try
00058         {
00059             cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00060         }
00061         catch (cv_bridge::Exception& e)
00062         {
00063             ROS_ERROR("cv_bridge exception: %s", e.what());
00064             return;
00065         }
00066         cv::Mat_<cv::Vec3b>::iterator it = cv_ptr->image.begin<cv::Vec3b>();
00067         cv::Mat_<cv::Vec3b>::iterator itend = cv_ptr->image.end<cv::Vec3b>();
00068         bool flag = false;
00069         for(;it != itend; ++it)
00070         {
00071             if((*it)[0]==0 && (*it)[1]==0 && (*it)[2]==255)
00072             {
00073                  flag = true;
00074             }
00075         }
00076         if(flag)
00077         {
00078             std_msgs::BoolPtr bool_ptr(new std_msgs::Bool);
00079             bool_ptr -> data = true;
00080             pub_.publish(bool_ptr);
00081         }
00082     }
00083 }//namespace micros_mars_task_alloc
00084 PLUGINLIB_EXPORT_CLASS(micros_mars_task_alloc::RobotDetect, nodelet::Nodelet)


micros_mars_task_alloc
Author(s): Minglong Li , Xiaodong Yi , Yanzhen Wang , Zhongxuan Cai
autogenerated on Mon Jul 1 2019 19:55:03