<add>1.Add dense depth map and dense depth map mode point cloud;

2.Add data recording compatible with MindCloud;
 3. fix compatibility issue on orin nx 22.04
This commit is contained in:
mt-lifan
2025-09-09 21:11:04 +08:00
parent 43c003f3a0
commit 97686cc034
20 changed files with 2005 additions and 54 deletions
+179
View File
@@ -0,0 +1,179 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <deque>
#include <filesystem>
#include <fstream>
#include <chrono>
#include <memory>
#include <cstdlib>
#include <ctime>
class BinaryDataLogger {
public:
struct Options {
public:
// Number of frames per batch before flushing to disk
size_t batch_size = 20;
// Base directory to place time-stamped run directory under
// If empty, will use $ODIN_DATA_DIR or default to $HOME/OdinData
std::filesystem::path base_dir;
};
explicit BinaryDataLogger(const Options& opts) {
namespace fs = std::filesystem;
// Determine base dir
fs::path base = opts.base_dir;
if (base.empty()) {
const char* env_dir = std::getenv("ODIN_DATA_DIR");
if (env_dir && *env_dir) {
base = fs::path(env_dir);
} else {
const char* home = std::getenv("HOME");
base = home && *home ? fs::path(home) / "OdinData" : fs::current_path() / "OdinData";
}
}
// Make timestamp folder name: YYYYMMDD_HHMMSS
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm{};
#ifdef _WIN32
localtime_s(&tm, &t);
#else
localtime_r(&t, &tm);
#endif
char buf[32];
std::strftime(buf, sizeof(buf), "%Y%m%d_%H%M%S", &tm);
root_dir_ = base / buf;
fs::create_directories(root_dir_);
fs::create_directories(root_dir_ / "image");
// 转成 std::string
std::string timestamp(buf);
// 拼接文件名
std::filesystem::path pcFile = root_dir_ / ("MT" + timestamp + ".olx");
// Create placeholder files
write_text_file(root_dir_ / "image" / "info.txt", "device=OdinOne\ncreated_at=" + std::string(buf) + "\n");
write_text_file(root_dir_ / "image" / "cam_in_ex.txt", "# camera intrinsics/extrinsics TBD\n");
// Init writers
pose_writer_ = std::make_unique<Writer>(root_dir_ / "OdinPose.bin", opts.batch_size);
cloud_writer_ = std::make_unique<Writer>(pcFile, opts.batch_size);
//cloud_writer_ = std::make_unique<Writer>(root_dir_ / "OdinPointCloud.olx", opts.batch_size);
image_writer_ = std::make_unique<Writer>(root_dir_ / "OdinImage.bin", opts.batch_size);
}
~BinaryDataLogger() {
// Ensure all writers flush on destruction
if (pose_writer_) pose_writer_->shutdown();
if (cloud_writer_) cloud_writer_->shutdown();
if (image_writer_) image_writer_->shutdown();
}
const std::filesystem::path& root_dir() const { return root_dir_; }
// Enqueue ready-to-write frame blobs (already formatted as per spec)
void enqueuePoseFrame(std::vector<uint8_t>&& blob) {
if (pose_writer_) pose_writer_->enqueue(std::move(blob));
}
void enqueuePointCloudFrame(std::vector<uint8_t>&& blob) {
if (cloud_writer_) cloud_writer_->enqueue(std::move(blob));
}
void enqueueImageFrame(std::vector<uint8_t>&& blob) {
if (image_writer_) image_writer_->enqueue(std::move(blob));
}
private:
struct Writer {
explicit Writer(const std::filesystem::path& filepath, size_t batch)
: file_path(filepath), batch_size(batch), stop(false) {
worker = std::thread([this]() { this->run(); });
}
~Writer() {
shutdown();
}
void enqueue(std::vector<uint8_t>&& frame) {
{
std::lock_guard<std::mutex> lk(mtx);
pending.emplace_back(std::move(frame));
if (pending.size() >= batch_size) {
swap_pending_unlocked();
}
}
cv.notify_one();
}
void shutdown() {
{
std::lock_guard<std::mutex> lk(mtx);
if (stop) return;
// Move leftovers to write buffer
if (!pending.empty()) {
swap_pending_unlocked();
}
stop = true;
}
cv.notify_one();
if (worker.joinable()) worker.join();
}
private:
void swap_pending_unlocked() {
if (!pending.empty()) {
write_queue.emplace_back(std::move(pending));
pending.clear();
}
}
void run() {
std::ofstream out(file_path, std::ios::binary | std::ios::app);
if (!out.is_open()) {
// If file can't be opened, silently drop (or could add logging hook)
return;
}
for (;;) {
std::vector<std::vector<uint8_t>> batch;
{
std::unique_lock<std::mutex> lk(mtx);
cv.wait(lk, [&]{ return stop || !write_queue.empty(); });
if (!write_queue.empty()) {
batch = std::move(write_queue.front());
write_queue.pop_front();
} else if (stop) {
break;
}
}
if (!batch.empty()) {
for (auto& frame : batch) {
if (!frame.empty()) {
out.write(reinterpret_cast<const char*>(frame.data()), static_cast<std::streamsize>(frame.size()));
}
}
out.flush();
}
}
}
std::filesystem::path file_path;
size_t batch_size;
std::mutex mtx;
std::condition_variable cv;
std::vector<std::vector<uint8_t>> pending; // accumulate frames
std::deque<std::vector<std::vector<uint8_t>>> write_queue; // queued batches
std::thread worker;
bool stop;
};
static void write_text_file(const std::filesystem::path& p, const std::string& content) {
std::ofstream f(p, std::ios::out | std::ios::trunc);
if (f.is_open()) {
f << content;
}
}
std::filesystem::path root_dir_;
std::unique_ptr<Writer> pose_writer_;
std::unique_ptr<Writer> cloud_writer_;
std::unique_ptr<Writer> image_writer_;
};
+81
View File
@@ -0,0 +1,81 @@
/*
Copyright 2025 Manifold Tech Ltd.(www.manifoldtech.com.co)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <sensor_msgs/msg/compressed_image.hpp>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.hpp>
#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <Eigen/Dense>
#include "pointcloud_depth_converter.hpp"
#include <string>
#include <memory>
class DepthImageRos2Node : public rclcpp::Node
{
public:
explicit DepthImageRos2Node(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
void initialize();
private:
std::string cloud_raw_topic_;
std::string color_compressed_topic_;
std::string depth_image_topic_;
std::string depth_cloud_topic_;
message_filters::Subscriber<sensor_msgs::msg::PointCloud2> cloud_sub_;
message_filters::Subscriber<sensor_msgs::msg::CompressedImage> color_compressed_sub_;
typedef message_filters::sync_policies::ApproximateTime<
sensor_msgs::msg::PointCloud2,
sensor_msgs::msg::CompressedImage> MySyncPolicy;
typedef message_filters::Synchronizer<MySyncPolicy> Sync;
std::shared_ptr<Sync> sync_;
std::shared_ptr<image_transport::ImageTransport> it_;
image_transport::Publisher depth_image_pub_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr depth_cloud_pub_;
std::unique_ptr<PointCloudToDepthConverter> depth_converter_;
PointCloudToDepthConverter::CameraParams loadCameraParams();
void syncCallback(const sensor_msgs::msg::PointCloud2::ConstSharedPtr cloud_msg,
const sensor_msgs::msg::CompressedImage::ConstSharedPtr image_msg);
void publishDepthImage(const cv::Mat &img,
const std_msgs::msg::Header &header,
const std::string &encoding = "32FC1");
void publishDepthCloud(const pcl::PointCloud<pcl::PointXYZRGB> &colored_cloud,
const std_msgs::msg::Header &header);
};
+81
View File
@@ -0,0 +1,81 @@
/*
Copyright 2025 Manifold Tech Ltd.(www.manifoldtech.com.co)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/CompressedImage.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <Eigen/Dense>
#include "pointcloud_depth_converter.hpp"
#include <string>
#include <memory>
class DepthImageRosNode
{
public:
DepthImageRosNode(ros::NodeHandle &nh, ros::NodeHandle &pnh);
private:
ros::NodeHandle nh_, pnh_;
image_transport::ImageTransport it_;
std::string cloud_raw_topic_;
std::string color_compressed_topic_;
std::string depth_image_topic_;
std::string depth_cloud_topic_;
message_filters::Subscriber<sensor_msgs::PointCloud2> cloud_sub_;
message_filters::Subscriber<sensor_msgs::CompressedImage> color_compressed_sub_;
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::CompressedImage> MySyncPolicy;
typedef message_filters::Synchronizer<MySyncPolicy> Sync;
std::shared_ptr<Sync> sync_;
image_transport::Publisher depth_image_pub_;
ros::Publisher depth_cloud_pub_;
std::unique_ptr<PointCloudToDepthConverter> depth_converter_;
PointCloudToDepthConverter::CameraParams loadCameraParams();
void syncCallback(const sensor_msgs::PointCloud2ConstPtr &cloud_msg,
const sensor_msgs::CompressedImageConstPtr &image_msg);
void publishDepthImage(const cv::Mat &img,
const std_msgs::Header &header,
const std::string &encoding = "32FC1");
void publishDepthCloud(const pcl::PointCloud<pcl::PointXYZRGB> &colored_cloud,
const std_msgs::Header &header);
};
+190 -21
View File
@@ -24,10 +24,14 @@ limitations under the License.
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <thread>
#include <Eigen/Dense>
#include <atomic>
#include <unordered_map>
#include "data_logger.h"
#include "lidar_api.h"
#include "lidar_api_type.h"
#include "rawCloudRender.h"
@@ -53,17 +57,15 @@ extern int g_sendcloudrender;
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include <std_msgs/msg/header.hpp>
#include "sensor_msgs/msg/image.hpp"
#include "sensor_msgs/msg/imu.hpp"
#include "sensor_msgs/msg/point_cloud2.hpp"
#include "sensor_msgs/point_cloud2_iterator.hpp"
#include <sensor_msgs/msg/imu.hpp>
#include <sensor_msgs/msg/compressed_image.hpp>
#include <builtin_interfaces/msg/time.hpp>
#include <nav_msgs/msg/odometry.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <sensor_msgs/msg/point_field.hpp>
#include <sensor_msgs/msg/image.hpp>
using ImageConstPtr = sensor_msgs::msg::Image::ConstSharedPtr;
using PointCloud2ConstPtr = sensor_msgs::msg::PointCloud2::ConstSharedPtr;
namespace ros {
using namespace rclcpp;
using namespace std_msgs::msg;
@@ -82,13 +84,13 @@ extern int g_sendcloudrender;
#include <ros/ros.h>
#include <ros/package.h>
#include <sensor_msgs/Image.h>
#include <std_msgs/Header.h>
#include <sensor_msgs/Imu.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/point_cloud2_iterator.h>
#include <sensor_msgs/CompressedImage.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/Image.h>
using ImageConstPtr = sensor_msgs::ImageConstPtr;
using PointCloud2ConstPtr = sensor_msgs::PointCloud2ConstPtr;
namespace ros {
using namespace ::ros;
using namespace sensor_msgs;
@@ -177,16 +179,36 @@ public:
MultiSensorPublisher(rclcpp::Node::SharedPtr node)
: node_(node) {
initialize_publishers();
// initialize_data_logger();
}
#else
MultiSensorPublisher(ros::NodeHandle& nh) {
initialize_publishers(nh);
// initialize_data_logger();
}
#endif
std::filesystem::path get_root_dir() const { return root_dir_; }
void set_log_level(int level) {
g_log_level = level;
}
// Optional external logger setter
void set_data_logger(std::shared_ptr<BinaryDataLogger> logger) {
data_logger_ = std::move(logger);
}
int get_pose_index() {
return pose_index_.load();
}
int get_cloud_index() {
return cloud_index_.load();
}
int get_image_index() {
return image_index_.load();
}
rawCloudRender render_;
void publishImu(icm_6aixs_data_t *stream) {
@@ -636,6 +658,8 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
#ifdef ROS2
auto header = std::make_shared<std_msgs::msg::Header>();
header->stamp = ns_to_ros_time(image.timestamp + 719060); // Offset compensation
//RCLCPP_INFO(rclcpp::get_logger("device_cb"), "image rgb %ld",image.timestamp + 719060);
header->frame_id = "camera_rgb_frame";
auto cv_image = std::make_shared<cv_bridge::CvImage>(*header, "bgr8", bgr);
@@ -664,7 +688,25 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
compression_params.push_back(80);
// Compress image
cv::imencode(".jpg", bgr, compressed_msg->data, compression_params);
cv::imencode(".JPEG", bgr, compressed_msg->data, compression_params);
// Enqueue binary logging for image
if (data_logger_) {
const uint32_t idx_now = image_index_.fetch_add(1, std::memory_order_relaxed);
const double ts_sec = static_cast<double>(image.timestamp + 719060) / 1e9;
const uint32_t jpeg_size = static_cast<uint32_t>(compressed_msg->data.size());
std::vector<uint8_t> blob;
blob.reserve(sizeof(uint32_t) + sizeof(double) + sizeof(uint32_t) + jpeg_size);
auto append_pod = [&](const auto& v) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
blob.insert(blob.end(), p, p + sizeof(v));
};
append_pod(idx_now);
append_pod(ts_sec);
append_pod(jpeg_size);
blob.insert(blob.end(), compressed_msg->data.begin(), compressed_msg->data.end());
data_logger_->enqueueImageFrame(std::move(blob));
}
compressed_rgb_pub_->publish(*compressed_msg);
@@ -702,7 +744,25 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
// Compress image
cv::imencode(".jpg", bgr, compressed_msg->data, compression_params);
// Enqueue binary logging for image
if (data_logger_) {
const uint32_t idx_now = image_index_.fetch_add(1, std::memory_order_relaxed);
// Convert ROS1 header.stamp to seconds
const double ts_sec = static_cast<double>(header.stamp.sec) + static_cast<double>(header.stamp.nsec) / 1e9;
const uint32_t jpeg_size = static_cast<uint32_t>(compressed_msg->data.size());
std::vector<uint8_t> blob;
blob.reserve(sizeof(uint32_t) + sizeof(double) + sizeof(uint32_t) + jpeg_size);
auto append_pod = [&](const auto& v) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
blob.insert(blob.end(), p, p + sizeof(v));
};
append_pod(idx_now);
append_pod(ts_sec);
append_pod(jpeg_size);
blob.insert(blob.end(), compressed_msg->data.begin(), compressed_msg->data.end());
data_logger_->enqueueImageFrame(std::move(blob));
}
compressed_rgb_pub_.publish(compressed_msg);
#endif
@@ -737,6 +797,24 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
rgb_image_queue_.push_back(cv_image.toImageMsg());
}
// Enqueue binary logging for image
if (data_logger_) {
const uint32_t idx_now = image_index_.fetch_add(1, std::memory_order_relaxed);
const double ts_sec = static_cast<double>(stream->imageList[0].timestamp + 719060) / 1e9;
const uint32_t jpeg_size = static_cast<uint32_t>(jpeg_data.size());
std::vector<uint8_t> blob;
blob.reserve(sizeof(uint32_t) + sizeof(double) + sizeof(uint32_t) + jpeg_size);
auto append_pod = [&](const auto& v) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
blob.insert(blob.end(), p, p + sizeof(v));
};
append_pod(idx_now);
append_pod(ts_sec);
append_pod(jpeg_size);
blob.insert(blob.end(), jpeg_data.begin(), jpeg_data.end());
data_logger_->enqueueImageFrame(std::move(blob));
}
#ifdef ROS2
{
rgb_pub_->publish(*cv_image.toImageMsg());
@@ -775,6 +853,9 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
sensor_msgs::msg::PointCloud2 msg;
msg.header.frame_id = "map";
msg.header.stamp = ns_to_ros_time(stream->imageList[0].timestamp);
//RCLCPP_INFO(rclcpp::get_logger("device_cb"), "Point cloudrgba %ld",stream->imageList[0].timestamp);
size_t pt_size = sizeof(int32_t) * 3 + sizeof(int32_t) * 4;
uint32_t points = stream->imageList[idx].length / pt_size;
@@ -830,15 +911,15 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
for(uint32_t i = 0; i < points; i++) {
int32_t* ptr = xyz_data + 7*i;
#ifdef ROS2
#ifdef ROS2
*iter_x = static_cast<float>(ptr[0]) / 10000.0f; ++iter_x;
*iter_y = static_cast<float>(ptr[1]) / 10000.0f; ++iter_y;
*iter_z = static_cast<float>(ptr[2]) / 10000.0f; ++iter_z;
#else
#else
*iter_x = (1.0 * ptr[0]) / 1e4; ++iter_x;
*iter_y = (1.0 * ptr[1]) / 1e4; ++iter_y;
*iter_z = (1.0 * ptr[2]) / 1e4; ++iter_z;
#endif
#endif
uint8_t r = ptr[3] & 0xff;
uint8_t g = ptr[4] & 0xff;
@@ -853,25 +934,62 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
*iter_rgb = rgb_float; ++iter_rgb;
}
// Enqueue binary logging for point cloud (XYZRGB per point)
if (data_logger_ && points > 0) {
const double ts_sec = static_cast<double>(stream->imageList[0].timestamp) / 1e9;
const uint32_t idx_now = cloud_index_.fetch_add(1, std::memory_order_relaxed);
// Compute total blob size: header + per-point payload
const size_t header_size = sizeof(uint32_t) + sizeof(double) + sizeof(uint32_t);
const size_t point_size = sizeof(float) * 3 + sizeof(uint8_t) * 3;
std::vector<uint8_t> blob;
blob.reserve(header_size + static_cast<size_t>(points) * point_size);
auto append_pod = [&](const auto& v) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
blob.insert(blob.end(), p, p + sizeof(v));
};
append_pod(idx_now);
append_pod(ts_sec);
append_pod(points);
for (uint32_t i = 0; i < points; ++i) {
int32_t* ptr = xyz_data + 7 * i;
float fx = static_cast<float>(ptr[0]) / 10000.0f;
float fy = static_cast<float>(ptr[1]) / 10000.0f;
float fz = static_cast<float>(ptr[2]) / 10000.0f;
uint8_t r = static_cast<uint8_t>(ptr[3] & 0xff);
uint8_t g = static_cast<uint8_t>(ptr[4] & 0xff);
uint8_t b = static_cast<uint8_t>(ptr[5] & 0xff);
append_pod(fx);
append_pod(fy);
append_pod(fz);
blob.push_back(r);
blob.push_back(g);
blob.push_back(b);
}
data_logger_->enqueuePointCloudFrame(std::move(blob));
}
#ifdef ROS2
#ifdef ROS2
xyzrgbacloud_pub_->publish(std::move(msg));
#else
#else
xyzrgbacloud_pub_.publish(msg);
#endif
#endif
}
void publishOdometry(capture_Image_List_t* stream) {
#ifdef ROS2
#ifdef ROS2
auto msg = nav_msgs::msg::Odometry();
#else
#else
ros::Odometry msg;
#endif
#endif
msg.header.frame_id = "map";
msg.child_frame_id = "base_link";
//RCLCPP_INFO(rclcpp::get_logger("device_cb"), "odom %ld",odom_data->timestamp_ns);
uint32_t data_len = stream->imageList[0].length;
if (data_len == sizeof(ros_odom_convert_complete_t)) {
@@ -887,6 +1005,30 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
msg.pose.pose.orientation.z = static_cast<double>(odom_data->orient[2]) / 1e6;
msg.pose.pose.orientation.w = static_cast<double>(odom_data->orient[3]) / 1e6;
// Enqueue binary logging for pose
if (data_logger_) {
const uint32_t idx_now = pose_index_.fetch_add(1, std::memory_order_relaxed);
const double ts_sec = static_cast<double>(odom_data->timestamp_ns) / 1e9;
float pose_arr[7];
pose_arr[0] = static_cast<float>(msg.pose.pose.position.x);
pose_arr[1] = static_cast<float>(msg.pose.pose.position.y);
pose_arr[2] = static_cast<float>(msg.pose.pose.position.z);
pose_arr[3] = static_cast<float>(msg.pose.pose.orientation.x);
pose_arr[4] = static_cast<float>(msg.pose.pose.orientation.y);
pose_arr[5] = static_cast<float>(msg.pose.pose.orientation.z);
pose_arr[6] = static_cast<float>(msg.pose.pose.orientation.w);
std::vector<uint8_t> blob;
blob.reserve(sizeof(uint32_t) + sizeof(double) + sizeof(float) * 7);
auto append_pod = [&](const auto& v) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(&v);
blob.insert(blob.end(), p, p + sizeof(v));
};
append_pod(idx_now);
append_pod(ts_sec);
for (int i = 0; i < 7; ++i) append_pod(pose_arr[i]);
data_logger_->enqueuePoseFrame(std::move(blob));
}
msg.twist.twist.linear.x = static_cast<double>(odom_data->linear_velocity[0]) / 1e6;
msg.twist.twist.linear.y = static_cast<double>(odom_data->linear_velocity[1]) / 1e6;
msg.twist.twist.linear.z = static_cast<double>(odom_data->linear_velocity[2]) / 1e6;
@@ -919,11 +1061,31 @@ void process_pair(const ImageConstPtr &rgb_msg, const PointCloud2ConstPtr &pcd_m
msg.pose.pose.orientation.w = static_cast<double>(odom_data->orient[3]) / 1e6;
}
#ifdef ROS2
#ifdef ROS2
odom_publisher_->publish(std::move(msg));
#else
#else
odom_publisher_.publish(msg);
#endif
#endif
}
void initialize_data_logger(std::string data_dir = "") {
try {
BinaryDataLogger::Options opts;
opts.batch_size = 100;
opts.base_dir = data_dir;
data_logger_ = std::make_shared<BinaryDataLogger>(opts);
root_dir_ = data_logger_->root_dir();
#ifdef ROS2
RCLCPP_INFO(node_->get_logger(), "Data logger initialized at %s", root_dir_.c_str());
#endif
} catch (...) {
// Swallow logger initialization failures to avoid affecting runtime
#ifdef ROS2
RCLCPP_INFO(node_->get_logger(), "Failed to initialize data logger");
#endif
data_logger_.reset();
}
}
private:
@@ -936,6 +1098,13 @@ private:
std::deque<PointCloud2ConstPtr> pcd_queue_;
const size_t max_pcd_queue_size_ = 10; // Maximum cache frames
// Binary logger and frame indices
std::shared_ptr<BinaryDataLogger> data_logger_;
std::atomic<uint32_t> pose_index_{0};
std::atomic<uint32_t> cloud_index_{0};
std::atomic<uint32_t> image_index_{0};
std::filesystem::path root_dir_;
// Updated helper functions
ImageConstPtr getLatestRgbImage() {
+94
View File
@@ -0,0 +1,94 @@
/*
Copyright 2025 Manifold Tech Ltd.(www.manifoldtech.com.co)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/common/transforms.h>
#include <opencv2/opencv.hpp>
#include <Eigen/Dense>
#include <vector>
class PointCloudToDepthConverter
{
public:
struct CameraParams
{
int image_width;
int image_height;
double A11, A12, A22;
double u0, v0;
double k2, k3, k4, k5, k6, k7;
double scale;
int point_sampling_rate;
Eigen::Matrix4d Tcl;
};
struct ProcessResult
{
cv::Mat depth_image;
pcl::PointCloud<pcl::PointXYZRGB> colored_cloud;
bool success;
std::string error_message;
};
explicit PointCloudToDepthConverter(const CameraParams &params);
ProcessResult processCloudAndImage(const pcl::PointCloud<pcl::PointXYZ> &cloud,
const cv::Mat &image);
cv::Mat customResize(const cv::Mat& src, const cv::Size& size);
const CameraParams &getCameraParams() const { return params_; }
void updateCameraParams(const CameraParams &params);
private:
CameraParams params_;
Eigen::Matrix3d K_;
Eigen::Matrix3d Kl_;
Eigen::Matrix4d K_4x4_;
Eigen::Matrix4d Kcl_;
cv::Mat map_x_, map_y_;
cv::Mat inv_map_x_, inv_map_y_;
cv::Mat depth_undistorted_;
int scaled_width_, scaled_height_;
void initializeInternalParams();
void createDistortionMaps();
cv::Mat projectCloudToDepth(const pcl::PointCloud<pcl::PointXYZ> &cloud_in_cam);
cv::Mat postProcessDepthImage(const cv::Mat &depth_img);
pcl::PointCloud<pcl::PointXYZRGB> generateColoredCloud(const cv::Mat &depth_img,
const cv::Mat &color_img);
std::pair<bool, std::string> validateInputs(const pcl::PointCloud<pcl::PointXYZ> &cloud,
const cv::Mat &image);
};