Files
RC2027_BR_code/sru_ws/tools/make_synthetic_bag.py
T
walunt5 c04c671a20 feat(sru_ws): 方案 C 零样本评估工具链
tools/ 新增:export_bag_to_npz.py(rosbag→npz)、make_synthetic_bag.py
(无真实数据时构造可复现输入)、sru_policy_math.py(策略数学,纯函数)、
sru_zeroshot_replay.py(回放评估),以及 tests/ 单测。
docs/SOLUTION_C_ASSESSMENT.md 记录判据与结论。

文档结论刻意写成「工具与判据已就绪,真实数据的零样本结论尚未获得」:
仓库里没有任何录过深度的包,所以现有数字全部来自合成输入,不构成
对策略能力的判断。文中每个数字都标了复现命令和待确认项。

运行产物 sru_ws/tools/_zs_out/ 不入库(见 .gitignore 第 10 节)。
2026-11-05 11:59:28 +08:00

114 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""Write a small synthetic rosbag2 with depth + odometry.
Purpose: exercise the real recording -> export -> replay chain WITHOUT the
robot. The depth frames come from the same analytic corridor renderer the
replay script uses, so a round-trip through a bag must reproduce the same
command sequence (up to LSTM warm-up noise). If it does not, the export or the
message decoding is wrong -- which is exactly the bug you do not want to
discover for the first time on the car.
Needs ROS 2 (``source /opt/ros/humble/setup.bash``).
python3 make_synthetic_bag.py /tmp/syn_bag --frames 40
python3 export_bag_to_npz.py /tmp/syn_bag --out /tmp/syn.npz --goal 6,0,0
python3 sru_zeroshot_replay.py --npz /tmp/syn.npz
"""
from __future__ import annotations
import argparse
from pathlib import Path
import numpy as np
import sru_zeroshot_replay as replay
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("bag", type=Path)
ap.add_argument("--frames", type=int, default=40)
ap.add_argument("--dt", type=float, default=0.2)
ap.add_argument("--depth-topic", default="/odin1/depth_img_competetion")
ap.add_argument("--odom-topic", default="/odin1/odometry_highfreq")
ap.add_argument("--goal-topic", default="/goal_pose")
ap.add_argument("--cmd-topic", default="/cmd_vel")
ap.add_argument("--goal", default="6,0,0")
ap.add_argument("--cam-height", type=float, default=0.5)
args = ap.parse_args(argv)
from rclpy.serialization import serialize_message
from rosbag2_py import (ConverterOptions, SequentialWriter, StorageOptions,
TopicMetadata)
from sensor_msgs.msg import Image
from nav_msgs.msg import Odometry
from geometry_msgs.msg import PoseStamped, Twist
if args.bag.exists():
raise SystemExit(f"{args.bag} exists; remove it first")
writer = SequentialWriter()
writer.open(
StorageOptions(uri=str(args.bag), storage_id="sqlite3"),
ConverterOptions(input_serialization_format="cdr",
output_serialization_format="cdr"),
)
for name, typ in ((args.depth_topic, "sensor_msgs/msg/Image"),
(args.odom_topic, "nav_msgs/msg/Odometry"),
(args.goal_topic, "geometry_msgs/msg/PoseStamped"),
(args.cmd_topic, "geometry_msgs/msg/Twist")):
writer.create_topic(TopicMetadata(name=name, type=typ,
serialization_format="cdr"))
depth = replay.render_corridor(cam_height=args.cam_height)
h, w = depth.shape
goal = [float(v) for v in args.goal.split(",")]
t0 = 1_700_000_000.0
n_odom = int(args.frames * args.dt * 50) # 50 Hz odom
for k in range(n_odom):
t = t0 + k / 50.0
msg = Odometry()
msg.header.stamp = rclpy_time(t)
msg.header.frame_id = "odom"
msg.child_frame_id = "base_link"
msg.pose.pose.orientation.w = 1.0
writer.write(args.odom_topic, serialize_message(msg), int(t * 1e9))
for k in range(args.frames):
t = t0 + k * args.dt
img = Image()
img.header.stamp = rclpy_time(t)
img.header.frame_id = "odin1_base_link"
img.height, img.width = h, w
img.encoding = "32FC1"
img.is_bigendian = 0
img.step = 4 * w
img.data = depth.astype(np.float32).tobytes()
writer.write(args.depth_topic, serialize_message(img), int(t * 1e9))
cmd = Twist() # a parked robot: the node would publish zeros
writer.write(args.cmd_topic, serialize_message(cmd), int(t * 1e9))
if k == 0:
pose = PoseStamped()
pose.header.stamp = rclpy_time(t)
pose.header.frame_id = "odom"
pose.pose.position.x, pose.pose.position.y, pose.pose.position.z = goal
pose.pose.orientation.w = 1.0
writer.write(args.goal_topic, serialize_message(pose), int(t * 1e9))
del writer
print(f"[synth-bag] {args.frames} depth frames ({h}x{w}) + {n_odom} odom -> {args.bag}")
def rclpy_time(seconds):
from builtin_interfaces.msg import Time
sec = int(seconds)
return Time(sec=sec, nanosec=int(round((seconds - sec) * 1e9)))
if __name__ == "__main__":
main()