本文介绍如何将FLIR品牌的相机应用于ROS系统,将相机实时的数据以rostopic实时广播:
系统平台:Ubuntu16.04 LTS ROS Kinetic
依赖:OpenCV(本例中所用版本为2.9), cv_bridge
硬件:FLIR Grasshopper GS3-PGE-23S6C-C
1.首先安装相机在Ubuntu16.04下的驱动:
本例直接安装于买设备时自带的安装盘,于官网也可以下载到相关驱动:
网址:www.flir.com/mv-techsupport/downloads
找到相同设备的下载列表,选择对应的系统版本即可
安装完成后,控制台输入flycap有软件弹出代表安装成功
点击Auto Force IP,可以为设备自动分配IP,可以通过flycap看到设备
点击确定,可以进入照相模式
2.ROS代码编写:
参考地址:https://blog.****.net/qq_27050183/article/details/51141998
https://blog.****.net/lixujie666/article/details/83303836
项目中用QT编写代码,用QMake的pro文件配置依赖目录:
TEMPLATE = app
CONFIG += console c++ QMAKE_CXXFLAGS += -std=c++
CONFIG -= app_bundle
CONFIG -= qt SOURCES += \
main.cpp INCLUDEPATH += /usr/include/flycapture INCLUDEPATH += /usr/local/include/opencv2 LIBS += /usr/local/lib/libopencv_*.so \ LIBS += /usr/lib/libflycapture*.so LIBS += /usr/lib/libcv_bridge.so #---------------------ROS DEPENDS---------------------#
INCLUDEPATH += /opt/ros/kinetic/include DEPENDPATH += /opt/ros/kinetic/include LIBS += -L/opt/ros/kinetic/lib \
-L/usr/local/lib \
-lroscpp \
-lrospack \
-lpthread \
-lrosconsole \
-lrosconsole_log4cxx \
-lrosconsole_backend_interface \
-lxmlrpcpp \
-lroscpp_serialization \
-lrostime \
-lcpp_common \
-lroslib \
-ltf \ LIBS += -L /usr/lib \
-l Ice LIBS += /opt/ros/kinetic/lib/libroslib.so \
/usr/local/lib/liblog4cplus.so #---------------------ROS DEPENDS---------------------#
编写C++通信代码:
#include <iostream>
#include <stdlib.h>
#include <sensor_msgs/Image.h>
#include <cv_bridge/cv_bridge.h>
#include <ros/ros.h>
using namespace std; #include "FLIR.h" using namespace FLIR;
unsigned int width=;
unsigned int height=; int main(int argc, char ** argv)
{
if(argc==){
width=atoi(argv[]);
height=atoi(argv[]);
}
GigECamera cam;
CameraInfo camInfo[];
unsigned int numCameras;
FlyCapture2::Error error;
error = BusManager::DiscoverGigECameras(camInfo, &numCameras);
if (error != PGRERROR_OK)
{
PrintError(error);
return false;
}
IPAddress ipAddress=camInfo[camIndex-].ipAddress;
sprintf(ipStr,"%d.%d.%d.%d",ipAddress.octets[],ipAddress.octets[],ipAddress.octets[],ipAddress.octets[]);
BusManager busMgr;
error=busMgr.GetCameraFromIPAddress(ipAddress,&guid);
if (error != PGRERROR_OK)
{
PrintError(error);
return false;
}
error = cam.Connect(&guid);
if (error != PGRERROR_OK)
{
PrintError(error);
return false;
} GigEImageSettingsInfo imageSettingsInfo;
error = cam.GetGigEImageSettingsInfo(&imageSettingsInfo);
if (error != PGRERROR_OK)
{
PrintError(error);
return false;
}
GigEImageSettings imageSettings;
imageSettings.offsetX = (unsigned int)(imageSettingsInfo.maxWidth-width)/;
imageSettings.offsetY = (unsigned int)(imageSettingsInfo.maxHeight-height)/;
imageSettings.height = height;
imageSettings.width = width;
imageSettings.pixelFormat = PIXEL_FORMAT_RGB;
error = cam.SetGigEImageSettings(&imageSettings);
if (error != PGRERROR_OK)
{
PrintError(error);
return false;
}
error = cam.StartCapture();
if (error != PGRERROR_OK)
{
PrintError(error);
return false;
}
cv::Mat img;
cam.RetrieveBGR(img);
ros::init(argc, argv, "camera_node");
ros::NodeHandle nh;
ros::Publisher pub_camera = nh.advertise<sensor_msgs::Image>("flir_camera", );
ros::Rate r = ros::Rate();
cv_bridge::CvImage cvi;
cvi.header.frame_id = "image";
cvi.encoding = "bgr8";
while (ros::ok()){
cam.RetrieveBGR(img);
sensor_msgs::Image image;
cvi.image = img;
cvi.toImageMsg(image);
pub_camera.publish(image);
r.sleep();
}
return ;
}
编译,运行
3.完成效果:
发布消息类型为sensor_msgs::Image的话题,话题名为"flir_camera",可以用rqt_image_view查看效果: