How declare ROS subscriber inside a class?
I am publishing images to another node and while publishing I am also subscribing a string. When that string is true I will save the image of the published data at that moment. I am having some trouble because of the following error: no match operator '='
for the subscriber which I don't understand why.
/home/rrcam/catkin_ws/src/image_pub/src/im_pub_sub.cpp: In constructor ‘Node::Node()’:
/home/rrcam/catkin_ws/src/image_pub/src/im_pub_sub.cpp:47:73: error: no match for ‘operator=’ (operand types are ‘image_transport::Subscriber’ and ‘ros::Subscriber’)
sub = nh_.subscribe("/chatter",1, &Node::imageCallback, this);
^
In file included from /opt/ros/melodic/include/image_transport/image_transport.h:39:0,
from /home/rrcam/catkin_ws/src/image_pub/src/im_pub_sub.cpp:2:
/opt/ros/melodic/include/image_transport/subscriber.h:61:7: note: candidate: image_transport::Subscriber& image_transport::Subscriber::operator=(const image_transport::Subscriber&)
class Subscriber
^~~~~~~~~~
/opt/ros/melodic/include/image_transport/subscriber.h:61:7: note: no known conversion for argument 1 from ‘ros::Subscriber’ to ‘const image_transport::Subscriber&’
/opt/ros/melodic/include/image_transport/subscriber.h:61:7: note: candidate: image_transport::Subscriber& image_transport::Subscriber::operator=(image_transport::Subscriber&&)
/opt/ros/melodic/include/image_transport/subscriber.h:61:7: note: no known conversion for argument 1 from ‘ros::Subscriber’ to ‘image_transport::Subscriber&&’
image_pub/CMakeFiles/i_pub_sub.dir/build.make:62: recipe for target 'image_pub/CMakeFiles/i_pub_sub.dir/src/im_pub_sub.cpp.o' failed
make[2]: *** [image_pub/CMakeFiles/i_pub_sub.dir/src/im_pub_sub.cpp.o] Error 1
CMakeFiles/Makefile2:1521: recipe for target 'image_pub/CMakeFiles/i_pub_sub.dir/all' failed
make[1]: *** [image_pub/CMakeFiles/i_pub_sub.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed
The Script is as follows: I don't get it what's wrong. I have also tried using boost::bind
for binding this
pointer and msg
in subscriber callback function:
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <ros/ros.h>
#include <sensor_msgs/image_encodings.h>
#include <exception>
#include <iostream>
#include <opencv2/imgcodecs.hpp>
#include "std_msgs/String.h"
class Node{
public:
cv::Mat frame1;
std::string GSpipeline1 = "nvarguscamerasrc sensor-id=0 ee-mode=0 ! video/x-raw(memory:NVMM), format=(string)NV12 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
int count =0;
ros::NodeHandle nh_;
ros::Rate loop_rate =10;
cv::VideoCapture cap1;
sensor_msgs::ImagePtr msg1;
image_transport::ImageTransport it;
image_transport::Publisher pub_frame1;
image_transport::Subscriber sub;
public:
Node():it(nh_),cap1(GSpipeline1, cv::CAP_GSTREAMER){
if (!cap1.isOpened())
{
std::cout << "Failed to open camera1." << std::endl;
return;
}
pub_frame1 = it.advertise("/cam1",1);
// sub = it.subscribe("/chatter",1, boost::bind(&Node::imageCallback, this));
sub = nh_.subscribe("/chatter",1, &Node::imageCallback, this);
}
~Node(){
}
void imageCallback(const std_msgs::String::ConstPtr& msg)
{
std::cout<<"I am here"<<std::endl;
if(msg->data.getData() == 'true'){
this->count++;
cv::imwrite("/home/rrcam/sensor0/img_"+ std::to_string(this->count)+".png",this->frame1);
}
}
void start(){
while (this->nh_.ok()) {
this->cap1 >> this->frame1;
// this->cap1.read(this->frame1);
if (this->frame1.empty()) {
ROS_ERROR_STREAM("Failed to capture1 image!");
ros::shutdown();
break;
}
int k = cv::waitKey(20) & 0xff ...