ROS订阅者只订阅一次消息


#include <ros/ros.h>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iomanip>
#include <opencv2/objdetect.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>

#include "std_msgs/Int32.h"
#include <vector>
#include <std_msgs/String.h>
#include <stdio.h>

#include <sensor_msgs/Image.h>
#include<cv_bridge/cv_bridge.h>
#include<sensor_msgs/image_encodings.h>
#include<image_transport/image_transport.h>

using namespace cv;
using namespace std;


int main(int argc, char **argv)
{
    ros::init(argc, argv, "test_carema");
    ros::NodeHandle nh;
    ros::Rate loop_rate(1.0);
    ROS_INFO_STREAM("Hello, ROS!");

    boost::shared_ptr<sensor_msgs::Image const> image_sub;  
    image_sub = ros::topic::waitForMessage<sensor_msgs::Image>("/usb_cam/image_raw",ros::Duration(3));
    //image_sub = ros::topic::waitForMessage<sensor_msgs::Image>("/usb_cam/image_raw",(ros::NodeHandle *)&nh, ros::Duration(3));
    if(image_sub != NULL)
    {
        sensor_msgs::Image  image_raw = *image_sub;
        //cout<<"battery:"<<  bat  <<endl;
        //cout<<"battery:"<<  image_sub  <<endl;
        cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(image_raw, sensor_msgs::image_encodings::BGR8);
	    Mat img = cv_ptr -> image;
        std::string image_name = "image.jpg";
        std::string path = "/home/muyuan/" + image_name;
        cv::imwrite(path.c_str(), img);
	    //cv::imshow("image", img);
        ROS_INFO("image ok!");
	    //cv::waitKey(1);
    }
    else
    {
        cout<<"no topic image_sub"<<endl;
    }

    ROS_INFO("ok!");


    ros::MultiThreadedSpinner spinner(0);
    spinner.spin();

    return 0;
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值