Commit 680c218e authored by CANNERE Xavier's avatar CANNERE Xavier
Browse files

6.1.7

parent 50a69310
......@@ -126,8 +126,76 @@ And in the constructor::
pub_ = this->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 1);
Now create the method *getImage* where the algorithm will turn. The first instructions are a necessary instructions to convert ros image type into opencv image type. You can try a simulation on Gazebo with a camera after compiling node, then start the node, if you receive the first message of RCLCPP_INFO it works::
private:
void getImage(const sensor_msgs::msg::Image::SharedPtr _msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(_msg, sensor_msgs::image_encodings::BGR8);
RCLCPP_INFO("I have received image! ;-)");
}
catch (cv_bridge::Exception& e)
{
RCLCPP_ERROR("cv_bridge exception: %s", e.what());
return;
}
}
After that you can put on comment the *RCLCPP* lines, there were here to test the script. Check if your code is like that::
#include <cstdio>
#include "opencv2/opencv.hpp"
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "geometry_msgs/msg/twist.hpp"
#include "cv_bridge/cv_bridge.h"
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr img_;
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr pub_;
class Visual : public rclcpp::Node
{
public:
Visual()
: Node("visu")
{
auto sensor_qos = rclcpp::QoS(rclcpp::SensorDataQoS());
img_ = this->create_subscription<sensor_msgs::msg::Image>(
"/camera/rgb/image_raw", sensor_qos,
std::bind(&Visual::getImage, this, std::placeholders::_1));
pub_ = this->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 1);
}
private:
void getImage(const sensor_msgs::msg::Image::SharedPtr _msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(_msg, sensor_msgs::image_encodings::BGR8);
//RCLCPP_INFO("I have received image! ;-)");
}
catch (cv_bridge::Exception& e)
{
//RCLCPP_ERROR("cv_bridge exception: %s", e.what());
return;
}
}
};
int main(int argc, char ** argv)
{
(void) argc;
(void) argv;
rclcpp::init(argc, argv);
auto node = std::make_shared<Visual>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment