bv_processor.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, LABUST, UNIZG-FER
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the LABUST nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 #include <labust/blueview/TrackerROI.hpp>
00035 #include <labust/blueview/ImageProcessing.hpp>
00036 #include <labust/blueview/ProcessingChain.hpp>
00037 #include <aidnav_msgs/MBSonar.h>
00038 
00039 #include <labust/navigation/KFCore.hpp>
00040 #include <labust/navigation/KinematicModel.hpp>
00041 
00042 #include <sensor_msgs/image_encodings.h>
00043 #include <cv_bridge/cv_bridge.h>
00044 #include <ros/ros.h>
00045 
00046 #include <opencv2/highgui/highgui.hpp>
00047 
00048 #include <boost/bind.hpp>
00049 #include <boost/thread.hpp>
00050 
00051 #include <exception>
00052 #include <cmath>
00053 
00054 struct click
00055 {
00056         click():x(0),y(0),isNew(false){};
00057         int x,y;
00058         bool isNew;
00059 };
00060 
00061 boost::mutex navP;
00062 
00063 click clickData;
00064 click ROIPosition;
00065 click EstimatePosition;
00066 click Measurement;
00067 double traceP = 10000;
00068 
00069 bool simulateLoss = false;
00070 
00071 void mouse_cb(int event, int x, int y, int flags, void* param)
00072 {
00073   switch( event )
00074   {
00075   case CV_EVENT_RBUTTONDOWN:
00076   {
00077     std::cout<<"Target: ("<<x<<", "<<y<<")\n";
00078     simulateLoss = !simulateLoss;
00079     break;
00080   }
00081   case CV_EVENT_LBUTTONDOWN:
00082   {
00083         /*
00084     BVSonar& son = *((BVSonar*)param);
00085     head.range = BVTMagImage_GetPixelRange(son.magImage(headNum),y,x);
00086     head.bearing = BVTMagImage_GetPixelRelativeBearing(son.magImage(headNum),y,x);
00087     processor.setPosition(head);*/
00088     std::cout<<"Vehicle on: ("<<x<<", "<<y<<")\n";
00089     clickData.x = x;
00090     clickData.y = y;
00091     clickData.isNew = true;
00092     //init = false;
00093     break;
00094   }
00095   default:
00096     break;
00097   }
00098 }
00099 
00100 void callback(labust::blueview::BVImageProcessor* /*labust::blueview::ProcessingChain<>*/ processor, const aidnav_msgs::MBSonarConstPtr& image)
00101 {
00102         //Do some time calculation for performance.
00103         static ros::Time last = ros::Time::now();
00104         //ROS_INFO("Received image: %d x %d x 2 = %d",image->image.width, image->image.height,image->image.data.size());
00105         //ROS_INFO("Elapsed time: %f",(ros::Time::now() - last).toSec());
00106         last = ros::Time::now();
00107         //Get the magnitude image
00108         labust::blueview::TrackerROI roi;
00109         roi.origin.x = roi.origin.y = 0;
00110         roi.headData.bearing = roi.headData.heading = roi.headData.range = 0;
00111         roi.headData.tiltAngle = roi.headData.panAngle = 0;
00112         roi.headData.resolution = image->range_res;
00113 
00114         if (clickData.isNew)
00115         {
00116                 clickData.isNew = false;
00117                 float x = -(clickData.x - image->origin.x)*image->range_res;
00118                 float y = -(clickData.y - image->origin.y)*image->range_res;
00119     roi.headData.range = std::sqrt(std::pow(x,2) + std::pow(y,2));
00120     roi.headData.bearing =std::atan2(x,y);
00121     //processor.setPosition(head);
00122     std::cout<<"Origin ("<<image->origin.x<<","<<image->origin.y<<")"<<std::endl;
00123     std::cout<<"Range:"<<roi.headData.range<<","<<"bearing:"<<roi.headData.bearing<<std::endl;
00124 
00125     processor->setPosition(roi.headData);
00126     ROIPosition = clickData;
00127         }
00128 
00129         cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(image->image, sensor_msgs::image_encodings::MONO16);
00130 
00131   cv::Mat img2 = cv_ptr->image.clone();
00132 
00133         if (processor->isTracking())
00134         {
00135                 boost::mutex::scoped_lock l(navP);
00136                 int wishSize = ((0.25*traceP>4)?4:0.25*traceP);
00137                 int roi_len = (wishSize>0.75?wishSize:0.75)/image->range_res;
00138     l.unlock();
00139 
00140                 //std::cout<<"Feature:("<<ROIPosition.x<<","<<ROIPosition.y<<")"<<std::endl;
00141                 int xup = ROIPosition.x - roi_len/2;
00142                 int yup = ROIPosition.y - roi_len/2;
00143                 cv::Point roiup(((xup>0)?xup:0), ((yup>0)?yup:0));
00144 
00145                 int xrest = cv_ptr->image.size().width - (roiup.x + roi_len);
00146                 int yrest = cv_ptr->image.size().height - (roiup.y + roi_len);
00147 
00148                 if ((xrest<0) || (yrest<0)) std::cout<<"Out of bounds."<<std::endl;
00149 
00150                 std::cout<<"ROIup:"<<roiup.x<<","<<roiup.y<<std::endl;
00151 
00152                 cv::Rect rect(roiup.x, roiup.y, (xrest<0)?(roi_len+xrest):roi_len, (yrest<0)?(roi_len+yrest):roi_len);
00153 
00154                 roi.roi = cv::Mat(cv_ptr->image, rect);
00155                 roi.size = roi.roi.size();
00156     roi.origin.y = -(roiup.y - image->origin.y);
00157     roi.origin.x = -(roiup.x - image->origin.x);
00158                 //labust::blueview::TrackedFeaturePtr feature = processor->processROI(roi);
00159     //Draw ROI
00160     cv::rectangle(img2, rect, cv::Scalar(255,255,255), 3);
00161 
00162     boost::mutex::scoped_lock lock(navP);
00163     cv::circle(img2,cv::Point(EstimatePosition.x, EstimatePosition.y),10,cv::Scalar(65536));
00164     std::cout<<"Estimate:"<<EstimatePosition.x<<","<<EstimatePosition.y<<std::endl;
00165         ROIPosition.y = EstimatePosition.y;
00166         ROIPosition.x = EstimatePosition.x;
00167     lock.unlock();
00168 
00169     if (processor->processROI(roi))
00170     {
00171         std::cout<<"Found contact."<<std::endl;
00172         labust::blueview::TrackedFeature feature = processor->getTracklet();
00173         //ROIPosition.y = roiup.y + feature->pposition.y;
00174         //ROIPosition.x = roiup.x + feature->pposition.x;
00175         ROIPosition.y = roiup.y + feature.pposition.y;
00176         ROIPosition.x = roiup.x + feature.pposition.x;
00177 
00178         if (!simulateLoss)
00179         {
00180                 boost::mutex::scoped_lock lock(navP);
00181                 Measurement.isNew = true;
00182                 Measurement.x = roiup.x + feature.pposition.x;
00183                 Measurement.y = roiup.y + feature.pposition.y;
00184                 lock.unlock();
00185         };
00186     }
00187     else
00188     {
00189         std::cout<<"Unable to find contact."<<std::endl;
00190     }
00191         }
00192 
00193         cv::imshow("Sonar",img2*200);
00194         cv::waitKey(10);
00195 }
00196 
00197 typedef labust::navigation::KFCore<labust::navigation::KinematicModel> KinNav;
00198 
00199 
00200 bool runTest = true;
00201 
00202 void do_estimation(KinNav* kinnav)
00203 {
00204         ros::Rate rate(10);
00205         while (runTest)
00206         {
00207                 kinnav->predict();
00208                 boost::mutex::scoped_lock l(navP);
00209                 if (Measurement.isNew)
00210                 {
00211                         Measurement.isNew = false;
00212                         KinNav::output_type vec(2);
00213                         vec(0) = Measurement.x;
00214                         vec(1) = Measurement.y;
00215                         kinnav->correct(vec);
00216                         //KinNav::vector state = kinnav->getState();
00217                         //state(KinNav::Vv) = 10;
00218                         //kinnav->setState(state);
00219                 }
00220                 EstimatePosition.x = kinnav->getState()(KinNav::xp);
00221                 EstimatePosition.y = kinnav->getState()(KinNav::yp);
00222 
00223                 std::cout<<"KINNAV: "<<kinnav->getState()(KinNav::Vv)<<","<<kinnav->getState()(KinNav::xp)<<","<<kinnav->getState()(KinNav::yp)<<","<<kinnav->traceP()<<std::endl;
00224                 traceP = kinnav->traceP();
00225                 l.unlock();
00226 
00227                 rate.sleep();
00228         }
00229 }
00230 
00231 int main(int argc, char* argv[])
00232 try
00233 {
00234         //Setup ROS
00235         ros::init(argc,argv,"bv_monitor");
00236         ros::NodeHandle nhandle;
00237 
00238         //Configure this node
00239         std::string topicName("bvsonar_img");
00240         std::string topicName2("bvsonar_cimg");
00241         int bufferSize(1),rate(10);
00242         ros::NodeHandle phandle("~");
00243         phandle.param("TopicName",topicName,topicName);
00244         phandle.param("BufferSize",bufferSize,bufferSize);
00245         phandle.param("Rate",rate,rate);
00246 
00247         labust::blueview::BVImageProcessor processor;
00248         //labust::blueview::ProcessingChain<> processor;
00249 
00250         //Create a window
00251         cv::namedWindow("Sonar",0);
00252         cv::setMouseCallback("Sonar",&mouse_cb,0);
00253 
00254         //Create topic subscription
00255         ros::Subscriber imageTopic = nhandle.subscribe<aidnav_msgs::MBSonar>(topicName,bufferSize,boost::bind(&callback,&processor,_1));
00256 
00257         //setup estimation
00258         KinNav estimator;
00259         estimator.setTs(0.1);
00260         Measurement.isNew = false;
00261         boost::thread test(boost::bind(&do_estimation,&estimator));
00262 
00263         ros::spin();
00264 
00265         runTest = false;
00266         test.join();
00267         return 0;
00268 }
00269 catch (std::exception& e)
00270 {
00271         std::cerr<<e.what()<<std::endl;
00272 }
00273 
00274 
00275 


target_detector
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:36:05