1 开发环境
Ubuntu 12.04
ROS Hydro
2 前提
已安装adrone_autonomy package 而且能够执行
https://github.com/AutonomyLab/ardrone_autonomy
文档:http://ardrone-autonomy.readthedocs.org
已通过catkin创建一个package (方法见上一篇文章)这里我创建的名称为droneTest
3 欲实现效果
获取ar drone的摄像头实时图像而且能够进行处理
4 參考网页
http://answers.ros.org/question/79306/help-with-streaming-ardrone-camera-images-to-opencv/
http://wiki.ros.org/cv_bridge/Tutorials/UsingCvBridgeToConvertBetweenROSImagesAndOpenCVImages
http://wiki.ros.org/vision_opencv
5 详细实现Step-by-Step
Step 1:在~/catkin_ws/src/droneTest/src/ 中创建一个新的文件这里命名为droneTest.cpp
Step 2: 编辑droneTest.cpp文件,代码例如以下:
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <sensor_msgs/image_encodings.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
static const char WINDOW[]="RGB Image";
static const char WINDOW2[]="Gray Image";
void process(const sensor_msgs::ImageConstPtr& cam_image){
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(cam_image,sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception:%s",e.what());
return;
}
Mat img_rgb = cv_ptr->image;
Mat img_gray;
cvtColor(img_rgb,img_gray,CV_RGB2GRAY);
imshow(WINDOW,img_rgb);
imshow(WINDOW2,img_gray);
cvWaitKey(1);
}
int main(int argc, char **argv){
ros::init(argc,argv,"droneTest");
ros::NodeHandle n;
image_transport::ImageTransport it(n);
image_transport::Subscriber image_sub = it.subscribe("/ardrone/image_raw",1,process);
cv::namedWindow(WINDOW);
cv::namedWindow(WINDOW2);
ros::spin();
return 0;
}
这里使用cv_bridge的toCvCopy来实现格式转换。很easy
Step 3:编辑CMakeLists.txt
主要目的是加入依赖和加入opencv库
cmake_minimum_required(VERSION 2.8.3)
project(droneTest)
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
sensor_msgs
cv_bridge
image_transport
)
catkin_package()
find_package(OpenCV)
include_directories(
${OpenCV_INCLUDE_DIRS}
)
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(droneTest src/droneTest.cpp)
target_link_libraries(droneTest ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
add_dependencies(droneTest droneTest_generate_messages_cpp)
Step 4:编译
编译catkin。在terminal中输入:
cd ~/catkin_ws
catkin_make
这里说明一下就是package.xml这个文件改不改不影响,我发现甚至把里面的dependency都删掉也能够make。
接下来是执行
这里我为了执行方便一般把package拷贝到~/workshop下
然后把~/catkin_ws/devel/lib/droneTest 拷贝到~/workshop/droneTest下。这里我的ROS_PACKAGE_PATH 包括~/workshop
我在bashrc中有加入例如以下代码:
source /opt/ros/hydro/setup.bash
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/workshop
Step 6:执行
1打开一个terminal执行roscore
2 连接ar drone
3 再打开一个terminal执行rosrun ardrone_autonomy ardrone_driver
4 再打开一个terminal执行rosrun droneTest droneTest
ok了