livox/CustomMsg消息从ROS1 bag转换成ROS2

首先把ROSbag转化成ros2的db3格式

rosbags-convert --src Shield_tunnel1_gamma.bag --dst shield1

由于消息格式livox_ros_driver/msg/CustomMsg不会自动转化成livox_ros_driver2/msg/CustomMsg,在ros2 bag play时是不识别的,因此需要用脚本转换:

py 复制代码
from pathlib import Path
from rosbags.rosbag2 import Reader, Writer
from rosbags.typesys import Stores, get_typestore
from rosbags.typesys.msg import get_types_from_msg  # 确保从这里导入
import shutil
import numpy as np

# --- 请准确复制你的 CustomPoint.msg 和 CustomMsg.msg 内容 ---
CUSTOM_POINT_MSG = """
uint32 offset_time
float32 x
float32 y
float32 z
uint8 reflectivity
uint8 tag
uint8 line
"""

CUSTOM_MSG = """
std_msgs/Header header
uint64 time_base
uint32 point_num
uint8 lidar_id
uint8[3] rsvd
CustomPoint[] points
"""
# --- 结束 ---

def convert_bag(input_path: str, output_path: str):
    # 使用你的 ROS2 版本(Humble 用 ROS2_HUMBLE)
    typestore = get_typestore(Stores.ROS2_HUMBLE)  # Iron 用 ROS2_IRON 等

    # 注册类型
    typestore.register({
        **get_types_from_msg(CUSTOM_POINT_MSG, 'livox_ros_driver/msg/CustomPoint'),
        **get_types_from_msg(CUSTOM_MSG, 'livox_ros_driver/msg/CustomMsg'),
        **get_types_from_msg(CUSTOM_POINT_MSG, 'livox_ros_driver2/msg/CustomPoint'),
        **get_types_from_msg(CUSTOM_MSG, 'livox_ros_driver2/msg/CustomMsg'),
    })

    # 先创建输出目录(Writer 需要它存在)
    if Path(output_path).exists():
        shutil.rmtree(output_path)
    # Path(output_path).mkdir(parents=True, exist_ok=True)

    with Reader(input_path) as reader:
        with Writer(path=output_path,version=9) as writer:  # 这里修正:用 keyword path=
            conn_map = {}
            for conn in reader.connections:
                if conn.topic == '/livox/lidar':
                    new_conn = writer.add_connection(
                        conn.topic,
                        'livox_ros_driver2/msg/CustomMsg',
                        serialization_format='cdr',
                        typestore=typestore,
                        # offered_qos_profiles=conn.offered_qos_profiles,
                    )
                    conn_map[conn.id] = new_conn
                else:
                    new_conn = writer.add_connection(
                        conn.topic,
                        conn.msgtype,
                        serialization_format='cdr',
                        typestore=typestore,
                        # offered_qos_profiles=conn.offered_qos_profiles,
                    )
                    conn_map[conn.id] = new_conn

            # 获取类型类
            OldMsg = typestore.types['livox_ros_driver/msg/CustomMsg']
            NewMsg = typestore.types['livox_ros_driver2/msg/CustomMsg']
            NewPoint = typestore.types['livox_ros_driver2/msg/CustomPoint']

            for conn, timestamp, rawdata in reader.messages():
                if conn.topic == '/livox/lidar':
                    old_msg = typestore.deserialize_cdr(rawdata, 'livox_ros_driver/msg/CustomMsg')

                    new_msg = NewMsg(
                        header=old_msg.header,
                        time_base=old_msg.time_base,
                        point_num=old_msg.point_num,
                        lidar_id=old_msg.lidar_id,
                        rsvd=np.array(old_msg.rsvd, dtype=np.uint8),  # 固定长度数组转 list
                        points=[NewPoint(
                            offset_time=p.offset_time,
                            x=p.x, y=p.y, z=p.z,
                            reflectivity=p.reflectivity,
                            tag=p.tag,
                            line=p.line
                        ) for p in old_msg.points]
                    )

                    new_data = typestore.serialize_cdr(new_msg, 'livox_ros_driver2/msg/CustomMsg')
                    writer.write(conn_map[conn.id], timestamp, new_data)
                else:
                    writer.write(conn_map[conn.id], timestamp, rawdata)

    print(f"转换完成!新 bag 保存到: {output_path}")
    print("现在可以用 ros2 bag play {output_path} --clock 直接播放,你的 lightning 程序能正常订阅 /livox/lidar。")

if __name__ == '__main__':
    input_bag = 'offroad4'                # 输入 bag 目录
    output_bag = 'offroad4_'     # 输出 bag 目录

    convert_bag(input_bag, output_bag)

运行python3 convertlivox_ros12.py

相关推荐
默默开发35 分钟前
完整版:本地电脑 + WiFi 搭建 AI 自动炒股 + 自我学习系统
人工智能·学习·电脑
zzh9407739 分钟前
2026年AI文件上传功能实战:聚合站处理图片、PDF、PPT全指南
人工智能·pdf·powerpoint
新缸中之脑5 小时前
Paperless-NGX实战文档管理
人工智能
无极低码7 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
grant-ADAS7 小时前
记录paddlepaddleOCR从环境到使用默认模型,再训练自己的数据微调模型再推理
人工智能·深度学习
炎爆的土豆翔7 小时前
OpenCV 阈值二值化优化实战:LUT 并行、手写 AVX2 与 cv::threshold 性能对比
人工智能·opencv·计算机视觉
智能相对论7 小时前
从AWE看到海尔智慧家庭步步引领
人工智能
云和数据.ChenGuang7 小时前
魔搭社区 测试AI案例故障
人工智能·深度学习·机器学习·ai·mindstudio
小锋学长生活大爆炸7 小时前
【工具】无需Token!WebAI2API将网页AI转为API使用
人工智能·深度学习·chatgpt·openclaw
昨夜见军贴06168 小时前
AI审核赋能司法鉴定:IACheck如何保障刑事证据检测报告精准无误、经得起推敲?
人工智能