|
@@ -6,25 +6,23 @@
|
|
|
*/
|
|
|
#include "color_detection.h"
|
|
|
|
|
|
-Color_Detection::Color_Detection(){
|
|
|
-
|
|
|
+Color_Detection::Color_Detection()
|
|
|
+{
|
|
|
//订阅者订阅压缩图像
|
|
|
_img_sub = nh.subscribe<sensor_msgs::CompressedImage>("/raspicam_node/image/compressed", 1,
|
|
|
&Color_Detection::img_callback, this);
|
|
|
//发布者发布速度消息
|
|
|
_cmd_vel_pub = nh.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
Color_Detection::~Color_Detection(){
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @description: 订阅图像数据回调函数
|
|
|
*/
|
|
|
-void Color_Detection::img_callback(const sensor_msgs::CompressedImage::ConstPtr & data){
|
|
|
-
|
|
|
+void Color_Detection::img_callback(const sensor_msgs::CompressedImage::ConstPtr & data)
|
|
|
+{
|
|
|
cv_bridge::CvImagePtr cv_ptr;
|
|
|
//将ROS中图像数据转化成OpenCV-BRG8数据
|
|
|
cv_ptr = cv_bridge::toCvCopy(data, sensor_msgs::image_encodings::BGR8);
|
|
@@ -32,13 +30,14 @@ void Color_Detection::img_callback(const sensor_msgs::CompressedImage::ConstPtr
|
|
|
cv::Mat img, img_copy;
|
|
|
cv_ptr->image.copyTo(img);
|
|
|
cv_ptr->image.copyTo(img_copy);
|
|
|
-
|
|
|
+
|
|
|
double prec_tick = ticks;
|
|
|
ticks = (double)cv::getTickCount();
|
|
|
|
|
|
double dT = (ticks - prec_tick) / cv::getTickFrequency();
|
|
|
-
|
|
|
- if(found){
|
|
|
+
|
|
|
+ if(found)
|
|
|
+ {
|
|
|
//卡尔曼滤波预测跟踪小球
|
|
|
img_kalman_filter.found_draw(dT, img_copy, x, area);
|
|
|
//根据小球与小车的位置关系发布小车的速度
|
|
@@ -81,13 +80,16 @@ void Color_Detection::img_callback(const sensor_msgs::CompressedImage::ConstPtr
|
|
|
cout << "Balls found:" << balls_box.size() << endl;
|
|
|
// <<<<< OpenCV处理图像找出画面中蓝色小球
|
|
|
|
|
|
- if(balls.size() == 0){
|
|
|
+ if(balls.size() == 0)
|
|
|
+ {
|
|
|
not_found_count++;
|
|
|
cout << "notFoundCount:" << not_found_count << endl;
|
|
|
if(not_found_count >= 100 ){
|
|
|
found = false;
|
|
|
}
|
|
|
- }else{
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
not_found_count = 0;
|
|
|
//卡尔曼滤波更新
|
|
|
img_kalman_filter.kalman_update(found, balls_box);
|
|
@@ -96,14 +98,14 @@ void Color_Detection::img_callback(const sensor_msgs::CompressedImage::ConstPtr
|
|
|
//显示图像
|
|
|
cv::imshow("color_detection", img_copy);
|
|
|
cv::waitKey(1);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @description: 根据小球与小车位置控制小车移动,当小球不在小车中心时控制小车旋转,
|
|
|
* 当小球距小车较远时小车前进,当小球距小车较近时小扯后退
|
|
|
*/
|
|
|
-void Color_Detection::cmd_vel_pub(float x, float area){
|
|
|
+void Color_Detection::cmd_vel_pub(float x, float area)
|
|
|
+{
|
|
|
_twist.linear.x = 0;
|
|
|
_twist.angular.z = 0;
|
|
|
|
|
@@ -127,7 +129,6 @@ void Color_Detection::cmd_vel_pub(float x, float area){
|
|
|
if(abs(_twist.linear.x) < 0.015){
|
|
|
_twist.linear.x = 0;
|
|
|
}
|
|
|
-
|
|
|
- _cmd_vel_pub.publish(_twist);
|
|
|
|
|
|
-}
|
|
|
+ _cmd_vel_pub.publish(_twist);
|
|
|
+}
|