1.生成发布和订阅程序框架
./moos-ivp/scripts/GenMOOSApp PublishImage p "leone"//发布image
./moos-ivp/scripts/GenMOOSApp ReceiveImage p "leone"//订阅image
2.代码编写
(1)发布图像
PublishImage.cpp
先初始化
bool PublishImage::OnStartUp()//完成初始化
{
image_ = cv::Mat(480,640,CV_8UC1);
bw_image_ = cv::Mat(480,640,CV_8UC1);
if(!vc_.open(0))//打开摄像头
{
std::cerr<<"camera open FAIL\n";
return false;
}
else
std::cerr<<"camera opened OK\n";
m_timewarp = GetMOOSTimeWarp();//获取时间戳
RegisterVariables();
return(true);
}
Notify发布topic
bool PublishImage::Iterate()
{
m_iterations++;
vc_>>capture_frame_;
if(capture_frame_.data!=NULL)
{
cv::cvtColor(capture_frame_, bw_image_, COLOR_BGR2GRAY);
cv::resize(bw_image_, image_, image_.size(), 0, 0, cv::INTER_NEAREST);
Notify("Image",(void*)image_.data,image_.size().area(),MOOSLocalTime());//发布topic
}
else
{
std::cerr<<"no image read from camera..\n";
}
return(true);
}
(2)接收图像
先注册
void ReceiveImage::RegisterVariables()
{
m_Comms.Register("Image", 0);//注册图像topic
}
订阅图像topic
bool ReceiveImage::OnNewMail(MOOSMSG_LIST &NewMail)
{
MOOSMSG_LIST::iterator p;
//std::cout<<"On New Mail "<<std::endl;
for(p=NewMail.begin(); p!=NewMail.end(); p++) {
//CMOOSMsg &msg = *p;
if(p->IsName("Image")){
std::cout<<"time stamp:"<<p->GetTime()<<std::endl;
memcpy(image_.data, p->GetBinaryData(), p->GetBinaryDataSize());
}
#if 0 // Keep these around just for template
string key = msg.GetKey();
string comm = msg.GetCommunity();
double dval = msg.GetDouble();
string sval = msg.GetString();
string msrc = msg.GetSource();
double mtime = msg.GetTime();
bool mdbl = msg.IsDouble();
bool mstr = msg.IsString();
#endif
}
return(true);
}
这里做一些显示图像的操作
bool ReceiveImage::Iterate()//
{
m_iterations++;
std::cout<<"receive image"<<std::endl;
cv::imshow("display", image_);
cv::waitKey(10);
return(true);
}
3.运行
test.moos配置文件
// MOOS file
ServerHost = localhost
ServerPort = 9000
Community = enstaboat
//------------------------------------------
// Antler configuration block
ProcessConfig = ANTLER
{
MSBetweenLaunches = 200
Run = MOOSDB @ NewConsole = false//类似于ros的roscore
Run = pPublishImage @ NewConsole = true //发布程序
Run = pReceiveImage @ NewConsole = true //接收程序
}
//------------------------------------------------
// iCamera config block
ProcessConfig = pPublishImage//以10hz发布
{
AppTick = 10
CommsTick = 10
}
ProcessConfig = pReceiveImage//以10hz接收
{
AppTick = 10
CommsTick = 10
}
运行
pAnter test.moos
代码链接地址源码