<fix>​ Fix format errors in README, Add rviz configuration loading.​​

This commit is contained in:
manifoldsdk
2025-07-12 21:28:38 +08:00
parent 31bff6868b
commit 876cc931b4
14 changed files with 838 additions and 344 deletions
+20 -21
View File
@@ -16,7 +16,7 @@ static device_handle odinDevice = nullptr;
static std::shared_ptr<MultiSensorPublisher> g_ros_object;
static std::atomic<bool> deviceConnected(false);
// 定义获取包路径的函数
// Function to get package share path
std::string get_package_share_path(const std::string& package_name) {
#ifdef ROS2
try {
@@ -33,7 +33,7 @@ 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;
@@ -93,7 +93,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
ROS_INFO("Device attaching...");
#endif
// 清理任何已存在的设备
// Clean up any existing device
if (odinDevice) {
lidar_stop_stream(odinDevice, type);
lidar_unregister_stream_callback(odinDevice);
@@ -102,7 +102,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
odinDevice = nullptr;
}
// 修复:使用const_cast移除const限定符
// Use const_cast to remove const qualifier
if (lidar_create_device(const_cast<lidar_device_info_t*>(device), &odinDevice)) {
#ifdef ROS2
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Create device failed");
@@ -112,7 +112,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
return;
}
// 打开设备
// Open device
if (lidar_open_device(odinDevice)) {
#ifdef ROS2
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Open device failed");
@@ -124,7 +124,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
return;
}
// 设置模式
// Set mode
if (lidar_set_mode(odinDevice, type)) {
#ifdef ROS2
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Set mode failed");
@@ -137,7 +137,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
return;
}
// 注册回调
// Register callback
lidar_data_callback_info_t data_callback_info;
data_callback_info.data_callback = lidar_data_callback;
data_callback_info.user_data = &odinDevice;
@@ -154,7 +154,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
return;
}
// 启动数据流
// Start data stream
if (lidar_start_stream(odinDevice, type)) {
#ifdef ROS2
RCLCPP_ERROR(rclcpp::get_logger("device_cb"), "Start stream failed");
@@ -167,7 +167,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
return;
}
// 根据配置激活数据流类型
// Activate stream types based on configuration
if (g_sendrgb) {
lidar_activate_stream_type(odinDevice, LIDAR_DT_RAW_RGB);
}
@@ -211,7 +211,7 @@ static void lidar_device_callback(const lidar_device_info_t* device, bool attach
int main(int argc, char *argv[])
{
// ROS初始化
// ROS initialization
#ifdef ROS2
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>("lydros_node");
@@ -226,10 +226,10 @@ int main(int argc, char *argv[])
std::string package_path = get_package_share_path("odin_ros_driver");
std::string config_file = package_path + "/config/control_command.yaml";
// 创建 YAML 解析器
// Create YAML parser
odin_ros_driver::YamlParser parser(config_file);
// 加载配置
// Load configuration
if (!parser.loadConfig()) {
#ifdef ROS2
RCLCPP_ERROR(node->get_logger(), "Failed to load config file: %s", config_file.c_str());
@@ -239,13 +239,12 @@ int main(int argc, char *argv[])
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()) {
@@ -254,17 +253,17 @@ int main(int argc, char *argv[])
return 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_sendcloudslam = get_key_value("sendcloudslam", 0);
// 设置日志级别
// Set log level
lidar_log_set_level(LIDAR_LOG_INFO);
// 初始化系统,启动USB监控
// Initialize system and start USB monitoring
if(lidar_system_init(lidar_device_callback)) {
#ifdef ROS2
RCLCPP_ERROR(node->get_logger(), "Lidar system init failed");
@@ -274,7 +273,7 @@ int main(int argc, char *argv[])
return -1;
}
// 等待设备连接(最长30秒)
// wait device connect
#ifdef ROS2
RCLCPP_INFO(node->get_logger(), "Waiting for device connection...");
#else
@@ -308,7 +307,7 @@ int main(int argc, char *argv[])
return -1;
}
// ROS主循环
// ROS loop
#ifdef ROS2
rclcpp::spin(node);
rclcpp::shutdown();
@@ -317,7 +316,7 @@ int main(int argc, char *argv[])
ros::shutdown();
#endif
// 清理
// Cleanup
if (odinDevice) {
lidar_stop_stream(odinDevice, LIDAR_MODE_SLAM);
lidar_unregister_stream_callback(odinDevice);
+10 -11
View File
@@ -2,35 +2,34 @@
#include <fstream>
#include <filesystem>
#include <iostream>
#include <algorithm> // 用于 std::transform
#include <algorithm>
namespace odin_ros_driver {
// 使用一致的成员变量名
YamlParser::YamlParser(const std::string& config_file)
: config_file_(config_file) {} // 这里使用 config_file_
: config_file_(config_file) {} // // Using config_file_ member variable
bool YamlParser::loadConfig() {
try {
// 使用成员变量 config_file_
// Using member variable config_file_
std::cerr << "Loading config file: " << config_file_ << std::endl;
// 检查文件是否存在
// Check if file exists
if (!std::filesystem::exists(config_file_)) {
std::cerr << "Config file not found: " << config_file_ << std::endl;
return false;
}
// 打印文件内容
// Print file contents
std::ifstream file(config_file_);
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
std::cerr << "Config file content:\n" << content << "\n--- End of file ---" << std::endl;
// 加载 YAML - 使用成员变量 config_file_
// Load YAML
YAML::Node config = YAML::LoadFile(config_file_);
// 检查是否存在 register_keys 节点
// Check if 'register_keys' node exists
if (!config["register_keys"]) {
std::cerr << "Missing 'register_keys' section in config file" << std::endl;
return false;
@@ -39,14 +38,14 @@ bool YamlParser::loadConfig() {
YAML::Node register_keys = config["register_keys"];
register_keys_.clear();
// 打印键值对数量
// Print number of key-value pairs found
std::cerr << "Found " << register_keys.size() << " keys in config" << std::endl;
for (YAML::const_iterator it = register_keys.begin(); it != register_keys.end(); ++it) {
std::string key = it->first.as<std::string>();
int value = it->second.as<int>();
// 转换为小写
// Convert key to lowercase
std::transform(key.begin(), key.end(), key.begin(),
[](unsigned char c){ return std::tolower(c); });
@@ -80,4 +79,4 @@ void YamlParser::printConfig() const {
}
}
} // namespace odin_ros_driver
}