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

相关推荐
IT知识分享2 小时前
中科天玑全要素AI舆情系统功能、架构解析
人工智能·语言模型·架构
AI营销资讯站2 小时前
AI营销内容生产新趋势:原圈科技引领企业全球化内容智能升级
人工智能·科技
dagouaofei2 小时前
AI自动生成PPT工具横评,真实使用感受分享
人工智能·python·powerpoint
JoannaJuanCV2 小时前
自动驾驶—CARLA仿真(19)automatic_control demo
人工智能·机器学习·自动驾驶
热爱生活的五柒2 小时前
PolSAR Image Registration——极化合成孔径雷达(PolSAR)图像配准
人工智能·计算机视觉·sar
qq_233772712 小时前
**给复杂机器“装上行车记录仪”:一篇量子论文如何照亮AI时代的信任之路**
人工智能
美林数据Tempodata2 小时前
案例分享|西安财经大学打造全覆盖、全链条人工智能通识教育培养体系
人工智能
O561 6O623O7 安徽正华露2 小时前
露,生物信号采集处理系统一体机 生物机能实验系统 生物信号采集处理系统 生理机能实验
人工智能
AI营销快线2 小时前
原圈科技如何引领AI营销内容生产升级:行业进化路线与闭环创新洞察
人工智能