00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00085
00086
00087
00088 std::cout<<"Vehicle on: ("<<x<<", "<<y<<")\n";
00089 clickData.x = x;
00090 clickData.y = y;
00091 clickData.isNew = true;
00092
00093 break;
00094 }
00095 default:
00096 break;
00097 }
00098 }
00099
00100 void callback(labust::blueview::BVImageProcessor* processor, const aidnav_msgs::MBSonarConstPtr& image)
00101 {
00102
00103 static ros::Time last = ros::Time::now();
00104
00105
00106 last = ros::Time::now();
00107
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
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
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
00159
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
00174
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
00217
00218
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
00235 ros::init(argc,argv,"bv_monitor");
00236 ros::NodeHandle nhandle;
00237
00238
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
00249
00250
00251 cv::namedWindow("Sonar",0);
00252 cv::setMouseCallback("Sonar",&mouse_cb,0);
00253
00254
00255 ros::Subscriber imageTopic = nhandle.subscribe<aidnav_msgs::MBSonar>(topicName,bufferSize,boost::bind(&callback,&processor,_1));
00256
00257
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