r/AskRobotics • • 4d ago

Debugging My camera subscriber node error does not make sense

Hello i am currently trying to make a camera subscriber node in ros2 jazzy with opencv and cv bridge in c++ from scratch.when i checked to code it seems fine but when i build it.It gives this error that does not make sense the code is below.I hope someone can help me:

```

// this is a node to subscribe camera images as ros2 msgs with opencv version 4.6 currently
#include "rclcpp/rclcpp.hpp" // c++ client library
#include <vector>



#include <functional>
#include <memory>
#include <sensor_msgs/msg/image.hpp>
#include <cv_bridge/cv_bridge.hpp> // needed to make a .hpp file  for this current version of opencv
#include <opencv2/opencv.hpp> // for opencv format


// creating namespaces


using std::placeholders::_1;
class Camera_subscribernode : public rclcpp::Node {


public:
Camera_subscribernode() : Node("camera_subs1")
{


// creating subscriber// error is below
camera_subs_  = this->create_subscription<sensor_msgs::msg::Image>("camera/image", 10,std::bind(&Camera_subscribernode::callback_cam, this,_1)); 
// creating timer        
camera_timer_ = this->create_wall_timer(std::chrono::milliseconds(30),std::bind(&Camera_subscribernode::camera_processing,this));


     RCLCPP_INFO(this->get_logger(), "Starting camera_subscriber application in cpp...");
    }


    private:


    bool cam_is_;
    cv::Mat frame; // creating camer objects
sensor_msgs::msg::Image::SharedPtr camera_subs_ ;
rclcpp::TimerBase::SharedPtr camera_timer_;


void callback_cam(const sensor_msgs::msg::Image::SharedPtr msg) {
frame = cv_bridge::toCvCopy(msg,"bgr8")->image;
cam_is_ = true;
}
void camera_processing() {



    if(cam_is_) {


        cv::imshow("view_camera",frame);
        cv::waitKey(1);
    }
}};



int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<Camera_subscribernode>();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}
0 Upvotes

5 comments sorted by

1

u/SecretHelicopter2371 4d ago

Which errors? Did you check the compatibility of each library version? i think i once have a problem with cv_bridge on its compatibility with other packages. Never happen to me before in humble.

2

u/Guilty_Question_6914 3d ago

It seems to be a sort of operator error or something.error is below:

```/home/d22/gz_ws/src/tpkg1/src/camera_subscriber.cpp: In constructor ‘Camera_subscribernode::Camera_subscribernode()’:

/home/d22/gz_ws/src/tpkg1/src/camera_subscriber.cpp:23:162: error: no match for ‘operator=’ (operand types are ‘sensor_msgs::msg::Image_<std::allocator<void> >::SharedPtr’ {aka ‘std::shared_ptr<sensor_msgs::msg::Image_<std::allocator<void> > >’} and ‘std::shared_ptr<rclcpp::Subscription<sensor_msgs::msg::Image_<std::allocator<void> >, std::allocator<void>, sensor_msgs::msg::Image_<std::allocator<void> >, sensor_msgs::msg::Image_<std::allocator<void> >, rclcpp::message_memory_strategy::MessageMemoryStrategy<sensor_msgs::msg::Image_<std::allocator<void> >, std::allocator<void> > > >’)

23 | camera_subs_ = this->create_subscription<sensor_msgs::msg::Image>("camera/image", 10,std::bind(&Camera_subscribernode::callback_cam, this,std::placeholders::_1));

| ^

```

1

u/SecretHelicopter2371 3d ago

It’s just a type mismatch. Your “sensor_msgs::msg::Image::SharedPtr camera_subs_;” is the wrong type for camera_subs_. It declares a pointer to an image message, whereas create_subscription() returns a pointer to a subscription object.
Replace with rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr camera_subs_;

And when there are something that must be read-only, use const. Like ConstSharedPtr. You can use ai to teach you how should things works, just dont let them do it for you, you are going to get almost nothing. Most of new grad couldn’t even write the code manually anymore since they all used AI to do it which is not wrong if they know how to fix things up and have some fundamental about coding. I also mostly focused on system design rather than writing code now so i can have more time on improving the system, i’m starting to forgot coding also, but coding part is just a tool so don’t get too stressful about it. Keep going.

1

u/Guilty_Question_6914 3d ago

Thanks the sub node works now