Hello, I’m learning how to convert RGB stereo stream into ROS 2 format and publish it.
I’ve written a snippet of function to get the image data from “map.data” convert it using cv_bridge, and publish it:
cv::Mat image(height, width, CV_8UC3, map.data);
// Convert the cv::Mat to a ROS 2 Image message
auto image_msg = cv_bridge::CvImage(std_msgs::msg::Header(), sensor_msgs::image_encodings::RGB8, image).toImageMsg();
image_msg->header.stamp = rclcpp::Clock(RCL_ROS_TIME).now();
image_msg->header.frame_id = "camera_frame_" + std::to_string(cfg->camid);
std::thread([image_msg, cfg]() {
auto publish_start = std::chrono::high_resolution_clock::now();
if (cfg->camid == 0) {
camera_publisher_node->publish_cv_image_data_cam0(image_msg);
} else if (cfg->camid == 1) {
camera_publisher_node->publish_cv_image_data_cam1(image_msg);
}
auto publish_end = std::chrono::high_resolution_clock::now();
auto publish_duration = std::chrono::duration_cast<std::chrono::microseconds>(publish_end - publish_start).count();
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Publishing took %ld microseconds", publish_duration);
}).detach();
Here is the CamerClass I wrote:
class CameraPublisher : public rclcpp::Node {
public:
CameraPublisher()
: Node("camera_publisher") {
// Define QoS settings
rclcpp::QoS qos_settings = rclcpp::QoS(rclcpp::KeepLast(10))
.reliability(RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT)
.durability(RMW_QOS_POLICY_DURABILITY_VOLATILE);
img_publisher_cam0_ = this->create_publisher<sensor_msgs::msg::Image>("camera0/image", qos_settings);
img_publisher_cam1_ = this->create_publisher<sensor_msgs::msg::Image>("camera1/image", qos_settings);
void publish_cv_image_data_cam0(const std::shared_ptr<sensor_msgs::msg::Image>& image_msg) {
img_publisher_cam0_->publish(*image_msg);
}
void publish_cv_image_data_cam1(const std::shared_ptr<sensor_msgs::msg::Image>& image_msg) {
img_publisher_cam1_->publish(*image_msg);
}
private:
rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr img_publisher_cam0_;
rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr img_publisher_cam1_;
};
#endif
Although I’m getting camera stream from cam0 and cam1, it is very jittery:
You can also see the publishing time spikes sometimes…
- Is this the proper way design the ROS 2 image stream for stereo?
- What would be the reason why the image is jittery?