<add>
1.Added camera point cloud extrinsic parameter rendering function 2.add image_compressed 3.fix imshow error about slam pointcloud
This commit is contained in:
+265
-98
@@ -1,22 +1,67 @@
|
||||
#include "host_sdk_sample.h"
|
||||
#include "yaml_parser.h"
|
||||
#include "rawCloudRender.h"
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <deque>
|
||||
#include <unistd.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
#include <chrono>
|
||||
|
||||
#ifdef ROS2
|
||||
#include <ament_index_cpp/get_package_share_directory.hpp>
|
||||
#include <rclcpp/rclcpp.hpp>
|
||||
#else
|
||||
#include <ros/package.h>
|
||||
#include <ros/ros.h>
|
||||
#endif
|
||||
|
||||
// Global variable declarations
|
||||
static device_handle odinDevice = nullptr;
|
||||
static std::shared_ptr<MultiSensorPublisher> g_ros_object;
|
||||
static std::atomic<bool> deviceConnected(false);
|
||||
static std::atomic<bool> deviceDisconnected(false); // Device disconnection flag
|
||||
static std::mutex device_mutex; // Device operation mutex lock
|
||||
|
||||
// Function to get package share path
|
||||
#ifdef ROS2
|
||||
std::shared_ptr<MultiSensorPublisher> g_ros_object = nullptr;
|
||||
#else
|
||||
MultiSensorPublisher* g_ros_object = nullptr;
|
||||
#endif
|
||||
|
||||
int g_log_level = LOG_LEVEL_INFO;
|
||||
int g_show_fps = 0; // FPS display toggle control
|
||||
|
||||
static std::mutex g_rgb_mutex;
|
||||
static std::shared_ptr<cv::Mat> g_latest_bgr;
|
||||
static uint64_t g_latest_rgb_timestamp = 0;
|
||||
static bool g_has_rgb = false;
|
||||
static capture_Image_List_t g_latest_rgb;
|
||||
static bool g_renderer_initialized = false;
|
||||
static std::shared_ptr<rawCloudRender> g_renderer = nullptr;
|
||||
|
||||
// Global configuration variables
|
||||
int g_sendrgb = 1;
|
||||
int g_sendimu = 1;
|
||||
int g_senddtof = 1;
|
||||
int g_sendodom = 1;
|
||||
int g_sendcloudslam = 0;
|
||||
int g_sendcloudrender = 0;
|
||||
int g_sendrgb_compressed = 0;
|
||||
|
||||
// Function declarations
|
||||
void clear_all_queues();
|
||||
|
||||
// Get package share path
|
||||
std::string get_package_share_path(const std::string& package_name) {
|
||||
#ifdef ROS2
|
||||
try {
|
||||
@@ -33,15 +78,31 @@ std::string get_package_share_path(const std::string& package_name) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Global configuration variables
|
||||
static int g_sendrgb = 1;
|
||||
static int g_sendimu = 1;
|
||||
static int g_senddtof = 1;
|
||||
static int g_sendodom = 1;
|
||||
static int g_sendcloudslam = 0;
|
||||
// Get package path
|
||||
std::string get_package_path(const std::string& package_name) {
|
||||
#ifdef ROS2
|
||||
return ament_index_cpp::get_package_share_directory(package_name);
|
||||
#else
|
||||
return ros::package::getPath(package_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Clear all queues
|
||||
void clear_all_queues() {
|
||||
// Reset state variables
|
||||
g_latest_bgr.reset();
|
||||
g_latest_rgb_timestamp = 0;
|
||||
g_has_rgb = false;
|
||||
}
|
||||
|
||||
// Lidar data callback
|
||||
static void lidar_data_callback(const lidar_data_t *data, void *user_data)
|
||||
{
|
||||
// If device is not connected, ignore all data
|
||||
if (!deviceConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
device_handle *dev_handle = static_cast<device_handle *>(user_data);
|
||||
if(!dev_handle || !data) {
|
||||
printf("Invalid device handle or data.\n");
|
||||
@@ -63,10 +124,10 @@ static void lidar_data_callback(const lidar_data_t *data, void *user_data)
|
||||
}
|
||||
break;
|
||||
case LIDAR_DT_RAW_DTOF:
|
||||
if (g_senddtof) {
|
||||
g_ros_object->publishIntensityCloud((capture_Image_List_t *)&data->stream, 1);
|
||||
if (g_senddtof ) {
|
||||
g_ros_object->publishIntensityCloud((capture_Image_List_t *)&data->stream, 1);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case LIDAR_DT_SLAM_CLOUD:
|
||||
if (g_sendcloudslam) {
|
||||
g_ros_object->publishPC2XYZRGBA((capture_Image_List_t *)&data->stream, 0);
|
||||
@@ -83,6 +144,7 @@ static void lidar_data_callback(const lidar_data_t *data, void *user_data)
|
||||
}
|
||||
}
|
||||
|
||||
// Lidar device callback
|
||||
static void lidar_device_callback(const lidar_device_info_t* device, bool attach)
|
||||
{
|
||||
int type = LIDAR_MODE_SLAM;
|
||||
@@ -93,16 +155,13 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
|
||||
ROS_INFO("Device attaching...");
|
||||
#endif
|
||||
|
||||
// Clean up any existing device
|
||||
// Clean up existing device resources
|
||||
if (odinDevice) {
|
||||
lidar_stop_stream(odinDevice, type);
|
||||
lidar_unregister_stream_callback(odinDevice);
|
||||
lidar_close_device(odinDevice);
|
||||
lidar_destory_device(odinDevice);
|
||||
// Skip stopping data stream, unregistering callbacks, closing device, destroying device
|
||||
odinDevice = nullptr;
|
||||
}
|
||||
|
||||
// Use const_cast to remove const qualifier
|
||||
// Create new device
|
||||
if (lidar_create_device(const_cast<lidar_device_info_t*>(device), &odinDevice)) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Create device failed");
|
||||
@@ -124,7 +183,82 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
|
||||
return;
|
||||
}
|
||||
|
||||
// Set mode
|
||||
// Get package path
|
||||
const std::string package_name = "odin_ros_driver";
|
||||
std::string config_dir = "";
|
||||
#ifdef ROS2
|
||||
// Get source code directory (not install directory)
|
||||
char* ros_workspace = std::getenv("COLCON_PREFIX_PATH");
|
||||
if (ros_workspace) {
|
||||
// Infer source directory from COLCON_PREFIX_PATH
|
||||
std::string workspace_path(ros_workspace);
|
||||
// Remove "/install" part
|
||||
size_t pos = workspace_path.find("/install");
|
||||
if (pos != std::string::npos) {
|
||||
config_dir = workspace_path.substr(0, pos) + "/src/odin_ros_driver/config";
|
||||
} else {
|
||||
// Fallback to install directory
|
||||
config_dir = ament_index_cpp::get_package_share_directory(package_name) + "/config";
|
||||
}
|
||||
} else {
|
||||
// Fallback to install directory
|
||||
config_dir = ament_index_cpp::get_package_share_directory(package_name) + "/config";
|
||||
}
|
||||
#else
|
||||
config_dir = ros::package::getPath(package_name) + "/config";
|
||||
#endif
|
||||
|
||||
// Print path information
|
||||
#ifdef ROS2
|
||||
RCLCPP_INFO(rclcpp::get_logger("device_cb"), "Calibration files will be saved to: %s", config_dir.c_str());
|
||||
#else
|
||||
ROS_INFO("Calibration files will be saved to: %s", config_dir.c_str());
|
||||
#endif
|
||||
|
||||
// Get calibration files - using modified function
|
||||
if (lidar_get_calib_file(odinDevice, config_dir.c_str())) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Failed to get calibration file");
|
||||
#else
|
||||
ROS_ERROR("Failed to get calibration file");
|
||||
#endif
|
||||
lidar_close_device(odinDevice);
|
||||
lidar_destory_device(odinDevice);
|
||||
odinDevice = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef ROS2
|
||||
RCLCPP_INFO(rclcpp::get_logger("device_cb"), "Successfully retrieved calibration files");
|
||||
#else
|
||||
ROS_INFO("Successfully retrieved calibration files");
|
||||
#endif
|
||||
|
||||
// Move point cloud renderer initialization here
|
||||
std::string calib_config = config_dir + "/calib.yaml";
|
||||
if (std::filesystem::exists(calib_config)) {
|
||||
g_renderer = std::make_shared<rawCloudRender>();
|
||||
if (g_renderer->init(calib_config)) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_INFO(rclcpp::get_logger("device_cb"), "Point cloud renderer initialized");
|
||||
#else
|
||||
ROS_INFO("Point cloud renderer initialized");
|
||||
#endif
|
||||
} else {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Failed to initialize point cloud renderer");
|
||||
#else
|
||||
ROS_ERROR("Failed to initialize point cloud renderer");
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
#ifdef ROS2
|
||||
RCLCPP_WARN(rclcpp::get_logger("device_cb"), "Renderer config file not found: %s", calib_config.c_str());
|
||||
#else
|
||||
ROS_WARN("Renderer config file not found: %s", calib_config.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
if (lidar_set_mode(odinDevice, type)) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Set mode failed");
|
||||
@@ -136,7 +270,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
|
||||
odinDevice = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Register callback
|
||||
lidar_data_callback_info_t data_callback_info;
|
||||
data_callback_info.data_callback = lidar_data_callback;
|
||||
@@ -144,7 +278,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
|
||||
|
||||
if (lidar_register_stream_callback(odinDevice, data_callback_info)) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Register callback failed");
|
||||
RCLCPP_ERROR(rclcpp::get_logger("device"), "Register callback failed");
|
||||
#else
|
||||
ROS_ERROR("Register callback failed");
|
||||
#endif
|
||||
@@ -185,6 +319,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
|
||||
}
|
||||
|
||||
deviceConnected = true;
|
||||
deviceDisconnected = false; // Reset disconnection flag
|
||||
#ifdef ROS2
|
||||
RCLCPP_INFO(rclcpp::get_logger("device_cb"), "Device ready and streams activated");
|
||||
#else
|
||||
@@ -196,134 +331,166 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
|
||||
#else
|
||||
ROS_INFO("Device detaching...");
|
||||
#endif
|
||||
|
||||
|
||||
// Set device disconnection flag
|
||||
deviceConnected = false;
|
||||
|
||||
if (odinDevice) {
|
||||
lidar_stop_stream(odinDevice, LIDAR_MODE_SLAM);
|
||||
lidar_unregister_stream_callback(odinDevice);
|
||||
lidar_close_device(odinDevice);
|
||||
lidar_destory_device(odinDevice);
|
||||
odinDevice = nullptr;
|
||||
}
|
||||
deviceDisconnected = true;
|
||||
|
||||
// Clear all message queues
|
||||
clear_all_queues();
|
||||
|
||||
#ifdef ROS2
|
||||
RCLCPP_INFO(rclcpp::get_logger("device_cb"), "Waiting for device reconnection...");
|
||||
#else
|
||||
ROS_INFO("Waiting for device reconnection...");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// ROS initialization
|
||||
#ifdef ROS2
|
||||
rclcpp::init(argc, argv);
|
||||
auto node = std::make_shared<rclcpp::Node>("lydros_node");
|
||||
g_ros_object = std::make_shared<MultiSensorPublisher>(node);
|
||||
#else
|
||||
ros::init(argc, argv, "lydros_node");
|
||||
ros::NodeHandle nh;
|
||||
g_ros_object = std::make_shared<MultiSensorPublisher>(nh);
|
||||
#endif
|
||||
#ifdef ROS2
|
||||
rclcpp::init(argc, argv);
|
||||
auto node = std::make_shared<rclcpp::Node>("lydros_node");
|
||||
g_ros_object = std::make_shared<MultiSensorPublisher>(node);
|
||||
#else
|
||||
ros::init(argc, argv, "lydros_node");
|
||||
ros::NodeHandle nh;
|
||||
g_ros_object = new MultiSensorPublisher(nh);
|
||||
#endif
|
||||
|
||||
try {
|
||||
std::string package_path = get_package_share_path("odin_ros_driver");
|
||||
std::string config_file = package_path + "/config/control_command.yaml";
|
||||
|
||||
// Create YAML parser
|
||||
odin_ros_driver::YamlParser parser(config_file);
|
||||
|
||||
// Load configuration
|
||||
|
||||
odin_ros_driver::YamlParser parser(config_file);
|
||||
if (!parser.loadConfig()) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(node->get_logger(), "Failed to load config file: %s", config_file.c_str());
|
||||
#else
|
||||
ROS_ERROR("Failed to load config file: %s", config_file.c_str());
|
||||
#endif
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(node->get_logger(), "Failed to load config file: %s", config_file.c_str());
|
||||
#else
|
||||
ROS_ERROR("Failed to load config file: %s", config_file.c_str());
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get key-value
|
||||
|
||||
auto keys = parser.getRegisterKeys();
|
||||
|
||||
// Print configuration
|
||||
parser.printConfig();
|
||||
|
||||
auto get_key_value = [&](const std::string& key_name, int default_value) -> int {
|
||||
auto it = keys.find(key_name);
|
||||
if (it != keys.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return default_value;
|
||||
|
||||
auto get_key_value = [&](const std::string& key, int default_value) -> int {
|
||||
auto it = keys.find(key);
|
||||
return it != keys.end() ? it->second : default_value;
|
||||
};
|
||||
|
||||
// Read configuration values into global variables
|
||||
g_sendrgb = get_key_value("sendrgb", 1);
|
||||
g_sendimu = get_key_value("sendimu", 1);
|
||||
g_senddtof = get_key_value("senddtof", 1);
|
||||
g_sendodom = get_key_value("sendodom", 1);
|
||||
|
||||
g_sendrgb = get_key_value("sendrgb", 1);
|
||||
g_sendimu = get_key_value("sendimu", 1);
|
||||
g_senddtof = get_key_value("senddtof", 1);
|
||||
g_sendodom = get_key_value("sendodom", 1);
|
||||
g_sendcloudslam = get_key_value("sendcloudslam", 0);
|
||||
|
||||
// Set log level
|
||||
g_sendcloudrender = get_key_value("sendcloudrender", 1);
|
||||
g_sendrgb_compressed = get_key_value("sendrgbcompressed", 1);
|
||||
g_log_level = get_key_value("log_devel", LOG_LEVEL_INFO);
|
||||
lidar_log_set_level(LIDAR_LOG_INFO);
|
||||
|
||||
// Initialize system and start USB monitoring
|
||||
if(lidar_system_init(lidar_device_callback)) {
|
||||
if (lidar_system_init(lidar_device_callback)) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(node->get_logger(), "Lidar system init failed");
|
||||
RCLCPP_ERROR(node->get_logger(), "Lidar system init failed");
|
||||
#else
|
||||
ROS_ERROR("Lidar system init failed");
|
||||
ROS_ERROR("Lidar system init failed");
|
||||
#endif
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// wait device connect
|
||||
|
||||
#ifdef ROS2
|
||||
RCLCPP_INFO(node->get_logger(), "Waiting for device connection...");
|
||||
RCLCPP_INFO(node->get_logger(), "Waiting for device connection...");
|
||||
#else
|
||||
ROS_INFO("Waiting for device connection...");
|
||||
ROS_INFO("Waiting for device connection...");
|
||||
#endif
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
|
||||
// Wait indefinitely for device connection
|
||||
while (!deviceConnected) {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - start);
|
||||
|
||||
if (elapsed.count() >= 30) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(node->get_logger(), "No device connected after 30 seconds");
|
||||
RCLCPP_INFO(node->get_logger(), "Waiting for device connection...");
|
||||
#else
|
||||
ROS_ERROR("No device connected after 30 seconds");
|
||||
ROS_INFO("Waiting for device connection...");
|
||||
#endif
|
||||
lidar_system_deinit();
|
||||
return -1;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1)); // Check every second
|
||||
}
|
||||
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_ERROR(node->get_logger(), "Exception: %s", e.what());
|
||||
RCLCPP_ERROR(node->get_logger(), "Exception: %s", e.what());
|
||||
#else
|
||||
ROS_ERROR("Exception: %s", e.what());
|
||||
ROS_ERROR("Exception: %s", e.what());
|
||||
#endif
|
||||
lidar_system_deinit();
|
||||
return -1;
|
||||
lidar_system_deinit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// ROS loop
|
||||
#ifdef ROS2
|
||||
rclcpp::spin(node);
|
||||
// Create 10Hz Rate object
|
||||
rclcpp::Rate rate(10);
|
||||
while (rclcpp::ok()) {
|
||||
rclcpp::spin_some(node);
|
||||
|
||||
// Check device disconnection status
|
||||
if (deviceDisconnected.load()) {
|
||||
#ifdef ROS2
|
||||
RCLCPP_INFO(node->get_logger(), "Device disconnected, waiting for reconnection...");
|
||||
#else
|
||||
ROS_INFO("Device disconnected, waiting for reconnection...");
|
||||
#endif
|
||||
|
||||
// Wait 0.1 seconds
|
||||
rate.sleep();
|
||||
continue; // Skip rest of this loop iteration
|
||||
}
|
||||
|
||||
// Data processing when device is connected
|
||||
if (g_sendcloudrender) {
|
||||
g_ros_object->try_process_pair();
|
||||
}
|
||||
|
||||
// Wait 0.1 seconds
|
||||
rate.sleep();
|
||||
}
|
||||
rclcpp::shutdown();
|
||||
#else
|
||||
ros::spin();
|
||||
// Create 10Hz Rate object
|
||||
ros::Rate rate(10);
|
||||
while (ros::ok()) {
|
||||
ros::spinOnce();
|
||||
|
||||
// Check device disconnection status
|
||||
if (deviceDisconnected.load()) {
|
||||
ROS_INFO("Device disconnected, waiting for reconnection...");
|
||||
|
||||
// Wait 0.1 seconds
|
||||
rate.sleep();
|
||||
continue; // Skip rest of this loop iteration
|
||||
}
|
||||
|
||||
// Data processing when device is connected
|
||||
if (g_sendcloudrender) {
|
||||
g_ros_object->try_process_pair();
|
||||
}
|
||||
|
||||
// Wait 0.1 seconds
|
||||
rate.sleep();
|
||||
}
|
||||
ros::shutdown();
|
||||
#endif
|
||||
|
||||
// Cleanup
|
||||
// Cleanup on normal program exit
|
||||
if (odinDevice) {
|
||||
// Perform cleanup on normal exit
|
||||
lidar_stop_stream(odinDevice, LIDAR_MODE_SLAM);
|
||||
lidar_unregister_stream_callback(odinDevice);
|
||||
lidar_close_device(odinDevice);
|
||||
lidar_destory_device(odinDevice);
|
||||
}
|
||||
lidar_system_deinit();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
#include "rawCloudRender.h"
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <Eigen/Dense>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
|
||||
struct ValidPointInfo {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
int u;
|
||||
int v;
|
||||
};
|
||||
|
||||
|
||||
namespace GlobalCameraParams {
|
||||
float g_fx = 0.0f;
|
||||
float g_fy = 0.0f;
|
||||
float g_cx = 0.0f;
|
||||
float g_cy = 0.0f;
|
||||
float g_skew = 0.0f;
|
||||
float g_k2 = 0.0f;
|
||||
float g_k3 = 0.0f;
|
||||
float g_k4 = 0.0f;
|
||||
float g_k5 = 0.0f;
|
||||
float g_k6 = 0.0f;
|
||||
float g_k7 = 0.0f;
|
||||
Eigen::Matrix4f g_T_camera_lidar = Eigen::Matrix4f::Identity();
|
||||
}
|
||||
bool raw_debug=0;
|
||||
bool rawCloudRender::init(const std::string& yamlFilePath) {
|
||||
YAML::Node config;
|
||||
try {
|
||||
config = YAML::LoadFile(yamlFilePath);
|
||||
} catch (const YAML::BadFile& e) {
|
||||
std::cerr << "Error: Could not open file '" << yamlFilePath << "' - " << e.what() << std::endl;
|
||||
return false;
|
||||
} catch (const YAML::ParserException& e) {
|
||||
std::cerr << "Error: YAML parsing failed - " << e.what() << std::endl;
|
||||
return false;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Unexpected error: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Intrinsic parameters node (cam_0)
|
||||
const std::string cam_node_name = "cam_0";
|
||||
if (!config[cam_node_name]) {
|
||||
std::cerr << "Error: Missing camera node '" << cam_node_name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
YAML::Node cam_node = config[cam_node_name];
|
||||
|
||||
// Directly access Tcl_0 node
|
||||
const std::string tcl_node_name = "Tcl_0";
|
||||
if (!config[tcl_node_name]) {
|
||||
std::cerr << "Error: Missing transformation matrix node '" << tcl_node_name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
YAML::Node tclNode = config[tcl_node_name];
|
||||
if (tclNode.size() != 16) {
|
||||
std::cerr << "Error: Transformation matrix must be 4x4 (16 elements)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// === Read intrinsic parameters ===
|
||||
GlobalCameraParams::g_k2 = cam_node["k2"].as<float>();
|
||||
GlobalCameraParams::g_k3 = cam_node["k3"].as<float>();
|
||||
GlobalCameraParams::g_k4 = cam_node["k4"].as<float>();
|
||||
GlobalCameraParams::g_k5 = cam_node["k5"].as<float>();
|
||||
GlobalCameraParams::g_k6 = cam_node["k6"].as<float>();
|
||||
GlobalCameraParams::g_k7 = cam_node["k7"].as<float>();
|
||||
|
||||
GlobalCameraParams::g_fx = cam_node["A11"].as<float>();
|
||||
GlobalCameraParams::g_skew = cam_node["A12"].as<float>();
|
||||
GlobalCameraParams::g_fy = cam_node["A22"].as<float>();
|
||||
|
||||
GlobalCameraParams::g_cx = cam_node["u0"].as<float>();
|
||||
GlobalCameraParams::g_cy = cam_node["v0"].as<float>();
|
||||
|
||||
// === Read extrinsic transformation matrix ===
|
||||
GlobalCameraParams::g_T_camera_lidar <<
|
||||
tclNode[0].as<float>(), tclNode[1].as<float>(), tclNode[2].as<float>(), tclNode[3].as<float>(),
|
||||
tclNode[4].as<float>(), tclNode[5].as<float>(), tclNode[6].as<float>(), tclNode[7].as<float>(),
|
||||
tclNode[8].as<float>(), tclNode[9].as<float>(), tclNode[10].as<float>(), tclNode[11].as<float>(),
|
||||
tclNode[12].as<float>(), tclNode[13].as<float>(), tclNode[14].as<float>(), tclNode[15].as<float>();
|
||||
|
||||
// === Display key parameters concisely ===
|
||||
std::cout << "=== Camera Calibration Parameters ===" << std::endl;
|
||||
std::cout << "Intrinsics:" << std::endl;
|
||||
std::cout << " fx: " << GlobalCameraParams::g_fx
|
||||
<< ", fy: " << GlobalCameraParams::g_fy
|
||||
<< ", cx: " << GlobalCameraParams::g_cx
|
||||
<< ", cy: " << GlobalCameraParams::g_cy << std::endl;
|
||||
std::cout << "Distortion: k2=" << GlobalCameraParams::g_k2
|
||||
<< ", k3=" << GlobalCameraParams::g_k3 << std::endl;
|
||||
|
||||
Eigen::Vector3f translation = GlobalCameraParams::g_T_camera_lidar.block<3,1>(0,3);
|
||||
Eigen::Matrix3f rotation = GlobalCameraParams::g_T_camera_lidar.block<3,3>(0,0);
|
||||
std::cout << "Extrinsics:" << std::endl;
|
||||
std::cout << " Translation: [" << translation.x() << ", "
|
||||
<< translation.y() << ", " << translation.z() << "]" << std::endl;
|
||||
std::cout << " Rotation (euler angles): "
|
||||
<< rotation.eulerAngles(0,1,2).transpose() * 180/M_PI << "°" << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
void rawCloudRender::render(std::vector<std::vector<float>>& rgb_image,
|
||||
capture_Image_List_t* pcd_stream,
|
||||
int pcdIdx,
|
||||
std::vector<float>& rgbCloud_flat)
|
||||
{
|
||||
// Initialize constants
|
||||
constexpr float inv_1000 = 0.001f;
|
||||
const float fx = GlobalCameraParams::g_fx;
|
||||
const float fy = GlobalCameraParams::g_fy;
|
||||
const float cx = GlobalCameraParams::g_cx;
|
||||
const float cy = GlobalCameraParams::g_cy;
|
||||
const float skew = GlobalCameraParams::g_skew;
|
||||
const float k2 = GlobalCameraParams::g_k2;
|
||||
const float k3 = GlobalCameraParams::g_k3;
|
||||
const float k4 = GlobalCameraParams::g_k4;
|
||||
const float k5 = GlobalCameraParams::g_k5;
|
||||
const float k6 = GlobalCameraParams::g_k6;
|
||||
const float k7 = GlobalCameraParams::g_k7;
|
||||
const Eigen::Matrix4f& T = GlobalCameraParams::g_T_camera_lidar;
|
||||
|
||||
// Precompute matrix elements
|
||||
const float T00 = T(0,0), T01 = T(0,1), T02 = T(0,2), T03 = T(0,3);
|
||||
const float T10 = T(1,0), T11 = T(1,1), T12 = T(1,2), T13 = T(1,3);
|
||||
const float T20 = T(2,0), T21 = T(2,1), T22 = T(2,2), T23 = T(2,3);
|
||||
|
||||
// Initialize lookup table
|
||||
static std::array<float, 10000> dist_table;
|
||||
static bool table_init = [&](){
|
||||
for (size_t i=0; i<dist_table.size(); ++i) {
|
||||
float theta = i * (M_PI/2) / dist_table.size();
|
||||
dist_table[i] = theta*(1 + theta*(k2 + theta*(k3 + theta*(k4 + theta*(k5 + theta*(k6 + theta*k7))))));
|
||||
}
|
||||
return true;
|
||||
}();
|
||||
|
||||
// Get point cloud data (direct access)
|
||||
if (!pcd_stream || pcdIdx < 0 || pcdIdx >= 10) {
|
||||
std::cerr << "ERROR: Invalid pcd_stream or index in render function" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
buffer_List_t& pcd_buffer = pcd_stream->imageList[pcdIdx];
|
||||
const int total_points = pcd_buffer.height * pcd_buffer.width;
|
||||
|
||||
if (!pcd_buffer.pAddr) {
|
||||
std::cerr << "ERROR: Null point cloud data pointer in render function" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
float* data = static_cast<float*>(pcd_buffer.pAddr);
|
||||
|
||||
// Prepare output
|
||||
rgbCloud_flat.clear();
|
||||
rgbCloud_flat.resize(total_points * 4); // Preallocate maximum space
|
||||
float* output_ptr = rgbCloud_flat.data();
|
||||
|
||||
// Get image dimensions
|
||||
const int img_height = 1296;
|
||||
const int img_width = 1600;
|
||||
|
||||
// Process point cloud
|
||||
int valid_count = 0;
|
||||
for (int idx = 0; idx < total_points; ++idx)
|
||||
{
|
||||
float* pf = data + idx*4;
|
||||
|
||||
// Quick check for invalid points
|
||||
if (std::abs(pf[0]) < 1e-5f && std::abs(pf[1]) < 1e-5f && std::abs(pf[2]) < 1e-5f) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Coordinate transformation
|
||||
const float x = pf[2] * inv_1000;
|
||||
const float y = -pf[0] * inv_1000;
|
||||
const float z = pf[1] * inv_1000;
|
||||
|
||||
// Manual matrix transformation
|
||||
const float x1 = T00*x + T01*y + T02*z + T03;
|
||||
const float y1 = T10*x + T11*y + T12*z + T13;
|
||||
const float z1 = T20*x + T21*y + T22*z + T23;
|
||||
|
||||
// Check for points behind camera
|
||||
if (z1 <= 0.0f) continue;
|
||||
|
||||
// Calculate projection
|
||||
const float x1_sq = x1*x1;
|
||||
const float y1_sq = y1*y1;
|
||||
const float z1_sq = z1*z1;
|
||||
|
||||
const float norm = std::sqrt(x1_sq + y1_sq + z1_sq);
|
||||
if (norm < 1e-7f) continue;
|
||||
|
||||
const float r = std::sqrt(x1_sq + y1_sq);
|
||||
if (r < 1e-7f) continue;
|
||||
|
||||
const float cost = z1 / norm;
|
||||
const float theta = std::acos(cost);
|
||||
|
||||
// Safe table lookup
|
||||
const size_t table_idx = static_cast<size_t>(theta * (2.0f/M_PI) * dist_table.size());
|
||||
const size_t safe_idx = std::min(table_idx, dist_table.size()-1);
|
||||
const float thetad = dist_table[safe_idx];
|
||||
|
||||
const float scaling = thetad / r;
|
||||
const float xd = x1 * scaling;
|
||||
const float yd = y1 * scaling;
|
||||
const float pd_2d_x = xd * fx + yd * skew + cx;
|
||||
const float pd_2d_y = yd * fy + cy;
|
||||
|
||||
// Quick boundary check
|
||||
const int u = static_cast<int>(pd_2d_x);
|
||||
const int v = static_cast<int>(pd_2d_y);
|
||||
|
||||
// Strict boundary check
|
||||
if (u < 0 || u >= img_width || v < 0 || v >= img_height) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Safe image data access
|
||||
if (v < static_cast<int>(rgb_image.size()) && u < static_cast<int>(rgb_image[v].size())) {
|
||||
*output_ptr++ = x;
|
||||
*output_ptr++ = y;
|
||||
*output_ptr++ = z;
|
||||
*output_ptr++ = rgb_image[v][u];
|
||||
valid_count++;
|
||||
} else {
|
||||
// Handle invalid coordinates
|
||||
static bool warned = false;
|
||||
if (!warned) {
|
||||
if(raw_debug)
|
||||
{
|
||||
std::cerr << "WARNING: Invalid image coordinates: u=" << u << ", v=" << v
|
||||
<< " (image size: " << rgb_image.size() << "x"
|
||||
<< (rgb_image.empty() ? 0 : rgb_image[0].size()) << ")" << std::endl;
|
||||
}
|
||||
|
||||
warned = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Resize output
|
||||
rgbCloud_flat.resize(output_ptr - rgbCloud_flat.data());
|
||||
if(raw_debug)
|
||||
{
|
||||
std::cout << "Render completed: " << total_points << " points processed, "
|
||||
<< valid_count << " valid points ("
|
||||
<< (100.0 * valid_count / total_points) << "%)" << std::endl;
|
||||
}
|
||||
}
|
||||
void rawCloudRender::print_camera_calib() {
|
||||
std::cout << model_type_ << std::endl;
|
||||
std::cout << image_width_ << std::endl;
|
||||
std::cout << image_height_ << std::endl;
|
||||
|
||||
std::cout << "T_camera_lidar" << std::endl;
|
||||
std::cout << T_camera_lidar_(0,0) << " " << T_camera_lidar_(0,1) << " " << T_camera_lidar_(0,2) << " " << T_camera_lidar_(0,3) << std::endl;
|
||||
std::cout << T_camera_lidar_(1,0) << " " << T_camera_lidar_(1,1) << " " << T_camera_lidar_(1,2) << " " << T_camera_lidar_(1,3) << std::endl;
|
||||
std::cout << T_camera_lidar_(2,0) << " " << T_camera_lidar_(2,1) << " " << T_camera_lidar_(2,2) << " " << T_camera_lidar_(2,3) << std::endl;
|
||||
std::cout << T_camera_lidar_(3,0) << " " << T_camera_lidar_(3,1) << " " << T_camera_lidar_(3,2) << " " << T_camera_lidar_(3,3) << std::endl;
|
||||
|
||||
std::cout << "cam" << std::endl;
|
||||
std::cout << k2_ << std::endl;
|
||||
std::cout << k3_ << std::endl;
|
||||
std::cout << k4_ << std::endl;
|
||||
std::cout << k5_ << std::endl;
|
||||
std::cout << k6_ << std::endl;
|
||||
std::cout << k7_ << std::endl;
|
||||
|
||||
std::cout << A11_fx_ << std::endl;
|
||||
std::cout << A12_skew_ << std::endl;
|
||||
std::cout << A22_fy_ << std::endl;
|
||||
std::cout << u0_cx_ << std::endl;
|
||||
std::cout << v0_cy_ << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user