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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
#!/usr/bin/env python3
"""
RM65 关节空间数据集录制(手柄 movep 遥操 + 双相机同步)。

控制:笛卡尔 movep_canfd(与 rm65_movep_teleop 相同)
落盘:LeRobot v2.1
  - observation.state = SDK 实测关节(度) + 夹爪(0~1)
  - action = 遥操积分位姿 IK 目标关节 + 夹爪指令
  - observation.images.front = 大白 USB(/dev/video0)
  - observation.images.astra = Astra Pro USB(/dev/video2)

USB 设备录制时独占,独立网页预览必须停掉。本脚本用已取帧在
8090/8093 继续出预览(大白/Astra),无需再占设备。

按键(G7 Pro):
  A  双路相机同步开始写入当前 episode
  B  同步结束并保存 episode
  X  丢弃当前 episode 缓冲(重录)
  LB 夹爪开 / RB 夹爪合
  Ctrl+C 退出且不保存当前未完成的 episode

用法:
  /home/sdznkj/zkx_ws/sendlerobot/scripts/rm65_run_record.sh
  /home/sdznkj/zkx_ws/sendlerobot/scripts/rm65_run_record.sh --resume
"""

from __future__ import annotations

import argparse
import json
import math
import os
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path

import cv2
import numpy as np

# 保证可 import 同目录遥操模块与 sendrobot / lerobot
ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(SCRIPTS))
VENDOR = ROOT / "vendor"
if VENDOR.is_dir():
    sys.path.insert(0, str(VENDOR))

from rm65_ready import movej_ready  # noqa: E402
from rm65_stick import _env_bool, _make_stick  # noqa: E402

MOTOR_KEYS = [f"left_joint_{i}.pos" for i in range(1, 7)] + ["left_gripper.pos"]
DEFAULT_FRONT_KEY = "front"
DEFAULT_ASTRA_KEY = "astra"
DEFAULT_ASTRA_DEVICE = "/dev/video2"
_PREVIEW_LOCK = threading.Lock()
_PREVIEW_FRAMES: dict[str, np.ndarray] = {}


def _load_info(root: Path) -> dict:
    return json.loads((root / "meta" / "info.json").read_text(encoding="utf-8"))


def _upgrade_empty_dataset_cameras(
    root: Path,
    expected_keys: list[str],
    cam_shape: tuple[int, int, int],
    use_video: bool,
) -> list[str]:
    """0 episode 空壳可补上缺失的图像 feature;已有数据则不改。"""
    info_path = root / "meta" / "info.json"
    info = _load_info(root)
    have = _camera_keys_from_info(root)
    if sorted(have) == sorted(expected_keys):
        return have
    if int(info.get("total_episodes", 0) or 0) > 0:
        return have
    extra = set(have) - set(expected_keys)
    changed = False
    if extra:
        # 0 episode 空壳:丢掉不再使用的图像 key(例如 astra → mono)
        for key in extra:
            info["features"].pop(f"observation.images.{key}", None)
        print(f"空数据集已移除旧相机 feature: {sorted(extra)}", flush=True)
        changed = True
    dtype = "video" if use_video else "image"
    added: list[str] = []
    for key in expected_keys:
        full = f"observation.images.{key}"
        if full in info["features"]:
            continue
        info["features"][full] = {
            "dtype": dtype,
            "shape": list(cam_shape),
            "names": ["height", "width", "channels"],
        }
        added.append(key)
    if added:
        print(f"空数据集已补相机 feature: {added}", flush=True)
        changed = True
    if changed:
        info_path.write_text(
            json.dumps(info, ensure_ascii=False, indent=4) + "\n",
            encoding="utf-8",
        )
    return _camera_keys_from_info(root)


def _start_mjpeg_preview(port: int, key: str, title: str) -> bool:
    """用录制已取 RGB 帧出预览;不另开 V4L2。JPEG 较小以免网页积压。"""

    class Handler(BaseHTTPRequestHandler):
        def log_message(self, fmt: str, *args) -> None:
            return

        def do_GET(self) -> None:  # noqa: N802
            if self.path in ("/", "/index.html"):
                body = (
                    "<html><body style='background:#111;color:#eee;font-family:sans-serif'>"
                    f"<h3>{title}(录制预览)</h3>"
                    "<img src='/stream/rgb' style='max-width:100%'/>"
                    "</body></html>"
                ).encode("utf-8")
                self.send_response(200)
                self.send_header("Content-Type", "text/html; charset=utf-8")
                self.send_header("Content-Length", str(len(body)))
                self.end_headers()
                self.wfile.write(body)
                return
            if not self.path.startswith("/stream"):
                self.send_error(404)
                return
            self.send_response(200)
            self.send_header(
                "Content-Type", "multipart/x-mixed-replace; boundary=frame"
            )
            self.end_headers()
            try:
                while True:
                    with _PREVIEW_LOCK:
                        fr = _PREVIEW_FRAMES.get(key)
                        img = None if fr is None else fr.copy()
                    if img is None:
                        time.sleep(0.05)
                        continue
                    bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
                    ok, buf = cv2.imencode(
                        ".jpg", bgr, [int(cv2.IMWRITE_JPEG_QUALITY), 55]
                    )
                    if not ok:
                        continue
                    payload = (
                        b"--frame\r\nContent-Type: image/jpeg\r\n\r\n"
                        + buf.tobytes()
                        + b"\r\n"
                    )
                    self.wfile.write(payload)
                    time.sleep(0.07)
            except (BrokenPipeError, ConnectionResetError, ConnectionAbortedError):
                return

    try:
        server = ThreadingHTTPServer(("0.0.0.0", port), Handler)
    except OSError as exc:
        print(f"预览端口 {port} 无法绑定({title}): {exc}", file=sys.stderr)
        return False
    threading.Thread(target=server.serve_forever, daemon=True).start()
    print(f"预览: http://0.0.0.0:{port}/  ({title})")
    return True


def _camera_keys_from_info(root: Path) -> list[str]:
    info_path = root / "meta" / "info.json"
    if not info_path.is_file():
        return [DEFAULT_FRONT_KEY, DEFAULT_ASTRA_KEY]
    info = _load_info(root)
    keys = [
        key.removeprefix("observation.images.")
        for key in info.get("features", {})
        if key.startswith("observation.images.")
    ]
    return keys or [DEFAULT_FRONT_KEY, DEFAULT_ASTRA_KEY]


def _ensure_local_meta_stubs(root: Path) -> None:
    """空壳数据集常只有 info.json;补齐空 jsonl,避免 LeRobot 去拉 HuggingFace。"""
    meta = root / "meta"
    meta.mkdir(parents=True, exist_ok=True)
    for name in ("tasks.jsonl", "episodes.jsonl", "episodes_stats.jsonl"):
        path = meta / name
        if not path.exists():
            path.write_text("", encoding="utf-8")


def _open_local_recording_dataset(
    repo_id: str,
    root: Path,
    *,
    image_writer_threads: int = 4,
):
    """纯本地打开录制数据集,禁止访问 HuggingFace Hub(否则无网/假 repo 会卡住)。"""
    from lerobot.common.datasets.lerobot_dataset import (
        CODEBASE_VERSION,
        LeRobotDataset,
        LeRobotDatasetMetadata,
        get_safe_default_codec,
    )

    # 强制离线:续录路径绝不访问 Hub
    os.environ["HF_HUB_OFFLINE"] = "1"
    os.environ["HF_DATASETS_OFFLINE"] = "1"

    _ensure_local_meta_stubs(root)
    info = _load_info(root)
    total_episodes = int(info.get("total_episodes", 0) or 0)

    if total_episodes == 0:
        # 0 episode 时没有 data/*.parquet,LeRobotDataset() 会误走 Hub 下载
        print("本地打开空数据集(跳过 HuggingFace)…", flush=True)
        meta = LeRobotDatasetMetadata.__new__(LeRobotDatasetMetadata)
        meta.repo_id = repo_id
        meta.root = root
        meta.revision = CODEBASE_VERSION
        meta.load_metadata()

        obj = LeRobotDataset.__new__(LeRobotDataset)
        obj.meta = meta
        obj.repo_id = repo_id
        obj.root = root
        obj.revision = None
        obj.tolerance_s = 1e-4
        obj.image_writer = None
        obj.episode_buffer = None
        obj.episodes = None
        obj.hf_dataset = obj.create_hf_dataset()
        obj.image_transforms = None
        obj.delta_timestamps = None
        obj.delta_indices = None
        obj.episode_data_index = None
        obj.video_backend = get_safe_default_codec()
        obj.start_image_writer(num_processes=0, num_threads=image_writer_threads)
        obj.episode_buffer = obj.create_episode_buffer()
        return obj

    print(f"本地续录已有 {total_episodes} episodes(跳过 HuggingFace)…", flush=True)
    dataset = LeRobotDataset(repo_id, root=root)
    dataset.start_image_writer(num_processes=0, num_threads=image_writer_threads)
    if dataset.episode_buffer is None:
        dataset.episode_buffer = dataset.create_episode_buffer()
    return dataset


def _dataset_features(
    cameras: dict[str, tuple[int, int, int]],
    use_video: bool,
) -> dict:
    from lerobot.common.datasets.utils import hw_to_dataset_features

    obs_hw: dict = {k: float for k in MOTOR_KEYS}
    obs_hw.update(cameras)
    act_hw = {k: float for k in MOTOR_KEYS}
    return {
        **hw_to_dataset_features(act_hw, "action", use_video),
        **hw_to_dataset_features(obs_hw, "observation", use_video),
    }


def _resize_rgb(image: np.ndarray, height: int, width: int) -> np.ndarray:
    if image.shape[0] == height and image.shape[1] == width:
        return image
    # cv2.resize 期望 HxW;输入为 RGB
    return cv2.resize(image, (width, height), interpolation=cv2.INTER_AREA)


def _open_usb_camera(
    device: str,
    fps: int | None,
    width: int,
    height: int,
):
    """USB 相机。fps=None 时不强制帧率(Astra Pro RGB 常为 15)。"""
    from lerobot.common.cameras.configs import ColorMode
    from lerobot.common.cameras.opencv.camera_opencv import OpenCVCamera
    from lerobot.common.cameras.opencv.configuration_opencv import OpenCVCameraConfig

    cfg = OpenCVCameraConfig(
        index_or_path=device,
        fps=fps,
        width=width,
        height=height,
        color_mode=ColorMode.RGB,
        warmup_s=1,
    )
    cam = OpenCVCamera(cfg)
    cam.connect()
    return cam


class RtspCamera:
    """后台线程拉 RTSP,接口对齐 OpenCVCamera.async_read(返回 RGB)。"""

    def __init__(self, url: str):
        self.url = url
        self.width = 0
        self.height = 0
        self.fps = 0.0
        self._lock = threading.Lock()
        self._frame: np.ndarray | None = None
        self._stop = threading.Event()
        self._err: str | None = None
        self._thread: threading.Thread | None = None

    def connect(self) -> None:
        os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = (
            "rtsp_transport;tcp|fflags;nobuffer|flags;low_delay|max_delay;500000"
        )
        self._thread = threading.Thread(target=self._loop, daemon=True)
        self._thread.start()
        t0 = time.time()
        while time.time() - t0 < 8.0:
            with self._lock:
                fr = None if self._frame is None else self._frame
                err = self._err
            if fr is not None:
                self.height, self.width = int(fr.shape[0]), int(fr.shape[1])
                return
            if err:
                raise RuntimeError(err)
            time.sleep(0.05)
        raise RuntimeError(f"RTSP 无画面: {self.url.split('@')[-1]}")

    def _loop(self) -> None:
        while not self._stop.is_set():
            cap = cv2.VideoCapture(self.url, cv2.CAP_FFMPEG)
            cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
            if not cap.isOpened():
                self._err = f"RTSP 打开失败: {self.url.split('@')[-1]}"
                time.sleep(1.0)
                continue
            n = 0
            t0 = time.time()
            while not self._stop.is_set():
                ok, fr = cap.read()
                if not ok or fr is None:
                    break
                rgb = cv2.cvtColor(fr, cv2.COLOR_BGR2RGB)
                with self._lock:
                    self._frame = rgb
                n += 1
                if n == 40:
                    dt = time.time() - t0
                    if dt > 0:
                        self.fps = n / dt
            cap.release()
            if not self._stop.is_set():
                time.sleep(0.3)

    def latest_rgb(self) -> np.ndarray | None:
        with self._lock:
            if self._frame is None:
                return None
            return self._frame.copy()

    def async_read(self, timeout_ms: float = 500.0) -> np.ndarray:
        # 有帧立刻返回,避免遥操主循环被 RTSP 卡住。
        fr = self.latest_rgb()
        if fr is not None:
            return fr
        deadline = time.time() + max(0.0, timeout_ms) / 1000.0
        while time.time() < deadline:
            fr = self.latest_rgb()
            if fr is not None:
                return fr
            time.sleep(0.01)
        raise TimeoutError("RTSP async_read timeout")

    def disconnect(self) -> None:
        self._stop.set()
        if self._thread is not None:
            self._thread.join(timeout=2.0)


def _latest_rgb(cam) -> np.ndarray | None:
    """取后台线程已有最新帧,不阻塞等待下一帧(否则 USB 15fps 会把 50Hz 遥操卡成一顿一顿)。"""
    if isinstance(cam, RtspCamera):
        return cam.latest_rgb()
    lock = getattr(cam, "frame_lock", None)
    if lock is not None:
        with lock:
            fr = getattr(cam, "latest_frame", None)
            event = getattr(cam, "new_frame_event", None)
            if event is not None:
                event.clear()
            return None if fr is None else np.ascontiguousarray(fr)
    try:
        return cam.async_read(timeout_ms=0.0)
    except Exception:
        return None


def _kick_async(cam, name: str, timeout_ms: float = 3000.0) -> None:
    try:
        cam.async_read(timeout_ms=timeout_ms)
    except Exception as exc:
        raise RuntimeError(f"相机 {name} 预热读帧失败: {exc}") from exc


class JointReader:
    """关节角缓存。SDK 查询放后台线程,禁止在 50Hz movep 循环里同步打 TCP。"""

    def __init__(self, ip: str, port: int):
        self.ip = ip
        self.port = int(port)
        self._kin = None
        self._last: list[float] | None = None
        self._ros_last: list[float] | None = None
        self._lock = threading.Lock()
        self._stop = threading.Event()
        self._thread: threading.Thread | None = None

    def on_ros_joint_state(self, names, positions) -> None:
        name_to_i = {f"joint{i}": i - 1 for i in range(1, 7)}
        joints = [0.0] * 6
        ok = False
        for name, pos in zip(names, positions):
            if name in name_to_i:
                joints[name_to_i[name]] = math.degrees(float(pos))
                ok = True
        if ok and max(abs(v) for v in joints) > 0.01:
            with self._lock:
                self._ros_last = joints

    def start_sdk_poll(self, hz: float = 10.0) -> None:
        if self._thread is not None:
            return
        period = 1.0 / max(1.0, float(hz))

        def _loop() -> None:
            while not self._stop.wait(period):
                kin = self._kin
                if kin is None:
                    continue
                try:
                    q = [float(v) for v in kin.get_current_joints_deg()[:6]]
                except Exception:
                    continue
                with self._lock:
                    self._last = q

        self._thread = threading.Thread(target=_loop, daemon=True, name="joint_sdk_poll")
        self._thread.start()

    def stop(self) -> None:
        self._stop.set()
        if self._thread is not None:
            self._thread.join(timeout=1.5)
            self._thread = None

    def read_deg(self) -> list[float]:
        with self._lock:
            if self._ros_last is not None:
                return list(self._ros_last)
            if self._last is not None:
                return list(self._last)
        raise RuntimeError("无法读取关节角")


def _motor_dict(joints_deg: list[float], gripper_norm: float) -> dict[str, float]:
    out = {f"left_joint_{i}.pos": float(joints_deg[i - 1]) for i in range(1, 7)}
    out["left_gripper.pos"] = float(gripper_norm)
    return out


def run_record(args: argparse.Namespace) -> int:
    import rclpy
    from geometry_msgs.msg import Pose
    from rclpy.qos import HistoryPolicy, QoSProfile, ReliabilityPolicy
    from rm_ros_interfaces.msg import Cartepos
    from sensor_msgs.msg import JointState

    from lerobot.common.datasets.lerobot_dataset import LeRobotDataset
    from lerobot.common.datasets.utils import build_dataset_frame
    from rm65_dataset_finalize import finalize_dataset

    root = Path(args.root).expanduser().resolve()
    repo_id = args.repo_id
    task = args.task
    fps = int(args.fps)
    control_hz = float(args.rate)
    episode_time_s = float(args.episode_time_s)
    num_episodes = int(args.num_episodes)
    front_key = args.front_key or DEFAULT_FRONT_KEY
    astra_key = args.astra_key or DEFAULT_ASTRA_KEY
    target_h = int(args.cam_height)
    target_w = int(args.cam_width)

    cam_shape = (target_h, target_w, 3)
    expected_keys = [front_key, astra_key]

    # --- 数据集(先于相机:避免 Hub 卡住后相机线程无法干净退出)---
    front_cam = None
    astra_cam = None
    if args.resume and (root / "meta" / "info.json").is_file():
        info_keys = _upgrade_empty_dataset_cameras(
            root, expected_keys, cam_shape, args.video
        )
        if sorted(info_keys) != sorted(expected_keys):
            raise SystemExit(
                f"续录相机 key 不匹配。\n"
                f"  数据集已有: {info_keys}\n"
                f"  当前脚本:   {expected_keys}\n"
                f"旧单相机数据集请换新 --root 新建,或改 key 与 features 对齐。"
            )
        dataset = _open_local_recording_dataset(repo_id, root)
        print(f"续录数据集: {root} 已有 episodes={dataset.num_episodes} cams={info_keys}")
    else:
        features = _dataset_features(
            {
                front_key: cam_shape,
                astra_key: cam_shape,
            },
            use_video=args.video,
        )
        if root.exists() and any(root.iterdir()):
            # 空壳(仅 meta)自动按续录打开,避免反复“目录非空”
            if (root / "meta" / "info.json").is_file():
                info_keys = _upgrade_empty_dataset_cameras(
                    root, expected_keys, cam_shape, args.video
                )
                if sorted(info_keys) == sorted(expected_keys):
                    print("检测到已有本地空/半成品数据集,自动续录(不连 Hub)…", flush=True)
                    dataset = _open_local_recording_dataset(repo_id, root)
                    print(
                        f"续录数据集: {root} 已有 episodes={dataset.num_episodes} cams={info_keys}"
                    )
                else:
                    raise SystemExit(
                        f"数据集目录非空且相机 key 不匹配: {root}\n"
                        f"  已有: {info_keys}\n  需要: {expected_keys}"
                    )
            else:
                raise SystemExit(
                    f"数据集目录非空: {root}\n"
                    f"请换 --root,或加 --resume 续录,或先清空该目录。"
                )
        else:
            print("新建本地数据集…", flush=True)
            dataset = LeRobotDataset.create(
                repo_id=repo_id,
                fps=fps,
                root=root,
                robot_type="rm65_movep",
                features=features,
                use_videos=args.video,
                image_writer_processes=0,
                image_writer_threads=4,
            )
            print(f"新建数据集: {root} cams={expected_keys}")

    # --- 双相机:启动时同时连接并预热后台取流线程,保证 A/B 时同步启停写盘 ---
    try:
        front_cam = _open_usb_camera(args.camera, fps, target_w, target_h)
        print(
            f"相机 front: {args.camera} key={front_key} "
            f"{front_cam.width}x{front_cam.height}@{front_cam.fps}"
        )
        astra_rtsp = (args.astra_rtsp or "").strip()
        if astra_rtsp:
            astra_cam = RtspCamera(astra_rtsp)
            astra_cam.connect()
            print(
                f"相机 cam2/{astra_key}: RTSP {astra_rtsp.split('@')[-1]} "
                f"{astra_cam.width}x{astra_cam.height}@{astra_cam.fps:.1f}"
            )
        else:
            astra_cam = _open_usb_camera(args.astra_camera, None, target_w, target_h)
            print(
                f"相机 astra: {args.astra_camera} key={astra_key} "
                f"{astra_cam.width}x{astra_cam.height}@{astra_cam.fps}"
            )
        _kick_async(front_cam, front_key)
        _kick_async(astra_cam, astra_key)
    except Exception:
        for cam in (front_cam, astra_cam):
            if cam is not None:
                try:
                    cam.disconnect()
                except Exception:
                    pass
        raise

    if args.preview:
        front_preview_port = int(args.front_preview_port)
        astra_preview_port = int(args.astra_preview_port)
        _start_mjpeg_preview(front_preview_port, front_key, "DaBai / front")
        _start_mjpeg_preview(astra_preview_port, astra_key, args.astra_preview_title)

    # --- ROS / 就绪 ---
    if args.go_ready:
        if not rclpy.ok():
            rclpy.init()
        try:
            movej_ready(speed=args.ready_speed)
        finally:
            pass
        time.sleep(0.5)

    if not rclpy.ok():
        rclpy.init()

    joint_reader = JointReader(args.ip, args.port)
    from sendrobot.send_robot.dahuan_gripper import DahuanGripperModbus
    from sendrobot.send_robot.rm_ik import RM65Kinematics

    kin = RM65Kinematics(ip=args.ip, port=args.port)
    kin.connect()
    joint_reader._kin = kin
    joint_reader._last = [float(v) for v in kin.get_current_joints_deg()[:6]]
    print(f"关节反馈(SDK): {[round(v, 2) for v in joint_reader._last]}")

    gripper_hw: DahuanGripperModbus | None = None
    gripper_norm = 1.0  # 1=开, 0=合
    if not args.no_gripper:
        try:

            class _Log:
                def info(self, m):
                    print(m)

                def warning(self, m):
                    print(f"警告: {m}", file=sys.stderr)

            assert kin.robot is not None
            gripper_hw = DahuanGripperModbus(kin.robot)
            gripper_hw.setup(_Log())
            print("夹爪: 已挂到录制 SDK 连接")
        except Exception as exc:
            print(f"夹爪初始化失败(继续录制): {exc}", file=sys.stderr)
            gripper_hw = None

    seed: list[float]
    if args.pose:
        seed = [float(v) for v in args.pose.split(",")]
        if len(seed) != 6:
            raise ValueError("--pose 需要 x,y,z,rx,ry,rz")
        print(f"初始位姿(手动): {[round(v, 4) for v in seed]}")
    else:
        seed = [float(v) for v in kin.get_current_pose()[:6]]
        print(
            f"初始位姿(SDK): xyz=({seed[0]:.4f},{seed[1]:.4f},{seed[2]:.4f}) "
            f"rpy=({seed[3]:.4f},{seed[4]:.4f},{seed[5]:.4f})"
        )

    stick = _make_stick(seed, args)
    stick.start()

    node = rclpy.create_node("rm65_record_movep")
    qos = QoSProfile(
        depth=1,
        reliability=ReliabilityPolicy.RELIABLE,
        history=HistoryPolicy.KEEP_LAST,
    )
    pub = node.create_publisher(Cartepos, "/rm_driver/movep_canfd_cmd", qos)
    node.create_subscription(
        JointState,
        "/joint_states",
        lambda msg: joint_reader.on_ros_joint_state(msg.name, msg.position),
        QoSProfile(depth=10, reliability=ReliabilityPolicy.RELIABLE),
    )

    for _ in range(50):
        if pub.get_subscription_count() > 0:
            break
        time.sleep(0.1)
        rclpy.spin_once(node, timeout_sec=0.05)
    if pub.get_subscription_count() == 0:
        raise RuntimeError("rm_driver 未订阅 movep_canfd_cmd")

    dt = 1.0 / control_hz
    record_interval = 1.0 / fps
    print(
        f"控制 {control_hz:g}Hz | 录制 {fps}Hz | 单集最长 {episode_time_s:.0f}s | 任务: {task}\n"
        f"按键: A=双相机同步开始  B=同步结束保存  X=丢弃重录  LB/RB=夹爪  Ctrl+C=退出(不保存当前集)\n"
        f"透传: movep 高跟随 follow={int(bool(args.follow))}\n"
        f"目标 episode 数: {num_episodes}(当前已有 {dataset.num_episodes})"
    )

    recording = False
    ep_start_t = 0.0
    last_record_t = 0.0
    frames_in_ep = 0
    saved_eps = 0
    last_log = 0.0
    saving_episode = False
    ik_fallback_warned = False

    def _buffer_has_frames() -> bool:
        buf = dataset.episode_buffer
        return buf is not None and int(buf.get("size", 0)) >= 1

    def _discard_unsaved() -> None:
        nonlocal recording, frames_in_ep
        try:
            dataset._wait_image_writer()
        except Exception:
            pass
        try:
            dataset.clear_episode_buffer()
        except Exception:
            pass
        recording = False
        frames_in_ep = 0

    def _save_episode_safe(label: str = "") -> bool:
        nonlocal recording, frames_in_ep, saved_eps, saving_episode
        if saving_episode:
            return False
        if frames_in_ep < 1 and not _buffer_has_frames():
            return False
        saving_episode = True
        try:
            dataset.save_episode()
            saved_eps += 1
            tag = f" ({label})" if label else ""
            print(f"<<< 已保存 episode{tag},累计 episodes={dataset.num_episodes}")
            return True
        except Exception as exc:
            print(f"保存失败: {exc}", file=sys.stderr)
            return False
        finally:
            saving_episode = False
            recording = False
            frames_in_ep = 0

    last_images: dict[str, np.ndarray] = {}

    def _read_all_images() -> dict[str, np.ndarray] | None:
        front_img = _latest_rgb(front_cam)
        astra_img = _latest_rgb(astra_cam)
        if front_img is None:
            front_img = last_images.get(front_key)
        if astra_img is None:
            astra_img = last_images.get(astra_key)
        if front_img is None or astra_img is None:
            return None
        out = {
            front_key: _resize_rgb(front_img, target_h, target_w),
            astra_key: _resize_rgb(astra_img, target_h, target_w),
        }
        last_images.update(out)
        return out

    try:
        while saved_eps < num_episodes:
            t0 = time.time()
            x, y, z, rx, ry, rz = stick.update()
            rclpy.spin_once(node, timeout_sec=0.0)

            # 夹爪(SDK 写寄存器放到后台,避免堵住 50Hz 透传)
            if stick.gripper_event == "open":
                gripper_norm = 1.0
                if gripper_hw is not None:
                    def _go():
                        try:
                            gripper_hw.set_position_normalized(1.0, min_delta=0.0)
                            print("夹爪开")
                        except Exception as exc:
                            print(f"夹爪开失败: {exc}", file=sys.stderr)

                    threading.Thread(target=_go, daemon=True).start()
            elif stick.gripper_event == "close":
                gripper_norm = 0.0
                if gripper_hw is not None:
                    def _gc():
                        try:
                            gripper_hw.set_position_normalized(0.0, min_delta=0.0)
                            print("夹爪合")
                        except Exception as exc:
                            print(f"夹爪合失败: {exc}", file=sys.stderr)

                    threading.Thread(target=_gc, daemon=True).start()

            # 录制状态机:A/B 同时控制双路写盘启停
            ev = stick.record_event
            if ev == "start" and not recording:
                recording = True
                ep_start_t = time.time()
                last_record_t = 0.0
                frames_in_ep = 0
                print(
                    f">>> 开始同步录制 episode #{dataset.num_episodes} "
                    f"(front+{astra_key},最多 {episode_time_s:.0f}s)"
                )
            elif ev == "stop" and recording:
                if not _save_episode_safe("B"):
                    print("本集无帧,取消保存")
                    _discard_unsaved()
            elif ev == "rerecord":
                if recording or frames_in_ep > 0:
                    _discard_unsaved()
                    print("xxx 已丢弃当前 episode,可按 A 重新开始")
                else:
                    print("当前无缓冲可丢弃")

            if recording and (time.time() - ep_start_t) >= episode_time_s:
                print(f"达到单集时限 {episode_time_s:.0f}s,自动保存")
                if not _save_episode_safe("timeout"):
                    _discard_unsaved()

            # 发 movep(与 rm65_movep_teleop 相同,控制器自己逆解)
            quat = stick.as_quat()
            pose = Pose()
            pose.position.x = x
            pose.position.y = y
            pose.position.z = z
            pose.orientation.x = float(quat[0])
            pose.orientation.y = float(quat[1])
            pose.orientation.z = float(quat[2])
            pose.orientation.w = float(quat[3])
            msg = Cartepos()
            msg.pose = pose
            msg.follow = args.follow
            pub.publish(msg)

            # 取双路帧(预览一直更新;录制中同时落盘)
            now = time.time()
            if (now - last_record_t) >= record_interval:
                last_record_t = now
                images = _read_all_images()
                if images is not None:
                    with _PREVIEW_LOCK:
                        _PREVIEW_FRAMES.update(images)
                if recording and images is not None:
                    joints = joint_reader.read_deg()
                    action_joints = kin.try_inverse_kinematics(
                        [x, y, z, rx, ry, rz], joints
                    )
                    if action_joints is None:
                        action_joints = list(joints)
                        if not ik_fallback_warned:
                            ik_fallback_warned = True
                            print("警告: IK 失败时将 action 回退为实测关节", file=sys.stderr)
                    obs_motors = _motor_dict(joints, gripper_norm)
                    act_motors = _motor_dict(action_joints, gripper_norm)
                    observation = {**obs_motors, **images}
                    action = act_motors
                    obs_frame = build_dataset_frame(
                        dataset.features, observation, prefix="observation"
                    )
                    act_frame = build_dataset_frame(
                        dataset.features, action, prefix="action"
                    )
                    dataset.add_frame({**obs_frame, **act_frame}, task=task)
                    frames_in_ep += 1

            if now - last_log >= 1.0:
                last_log = now
                st = "REC" if recording else "idle"
                loop_ms = (time.time() - t0) * 1000.0
                print(
                    f"[{st}] xyz=({x:.3f},{y:.3f},{z:.3f}) "
                    f"rpy=({rx:.3f},{ry:.3f},{rz:.3f}) "
                    f"frames={frames_in_ep} saved={dataset.num_episodes}/{num_episodes} "
                    f"grip={gripper_norm:.0f} loop={loop_ms:.0f}ms"
                )

            elapsed = time.time() - t0
            if elapsed > dt * 2.5:
                print(f"控制环超时 {elapsed*1000:.0f}ms(透传会断流,容易发响)", flush=True)
            time.sleep(max(0.0, dt - elapsed))
    except KeyboardInterrupt:
        print("\n用户中断")
        if recording or _buffer_has_frames():
            _discard_unsaved()
            print("已丢弃未完成的 episode(Ctrl+C 不保存)")
    finally:
        stick.cleanup()
        try:
            joint_reader.stop()
        except Exception:
            pass
        for cam in (front_cam, astra_cam):
            if cam is None:
                continue
            try:
                cam.disconnect()
            except Exception:
                pass
        try:
            kin.disconnect()
        except Exception:
            pass
        node.destroy_node()
        if rclpy.ok():
            rclpy.shutdown()
        if args.finalize_on_exit and dataset.num_episodes > 0:
            try:
                finalize_dataset(
                    root,
                    embodiment_id=args.embodiment_id,
                    camera_device=str(args.camera),
                    camera_devices={
                        f"observation.images.{front_key}": str(args.camera),
                        f"observation.images.{astra_key}": (
                            (args.astra_rtsp or "").strip() or str(args.astra_camera)
                        ),
                    },
                )
            except Exception as exc:
                print(f"finalize 失败: {exc}", file=sys.stderr)
        print(f"录制结束。数据目录: {dataset.root}")
    return 0


def main() -> int:
    p = argparse.ArgumentParser(description="RM65 movep 遥操 + 双相机同步 LeRobot 录制")
    p.add_argument("--ip", default=os.environ.get("RM_IP", "192.168.73.18"))
    p.add_argument("--port", type=int, default=8080)
    p.add_argument("--rate", type=float, default=50.0, help="遥操控制频率 Hz")
    p.add_argument("--fps", type=int, default=20, help="数据集写入频率")
    p.add_argument(
        "--sensitivity",
        type=float,
        default=float(os.environ.get("TELEOP_SENSITIVITY", "0.001")),
    )
    p.add_argument(
        "--orient-sensitivity",
        type=float,
        default=float(os.environ.get("TELEOP_ORIENT_SENSITIVITY", "0.003")),
    )
    p.add_argument(
        "--deadzone",
        type=float,
        default=float(os.environ.get("GAMEPAD_DEADZONE", "0.15")),
    )
    p.add_argument(
        "--follow",
        action=argparse.BooleanOptionalAction,
        default=_env_bool("TELEOP_FOLLOW", True),
        help="movep CANFD 高跟随(默认开)",
    )
    p.add_argument("--go-ready", action="store_true")
    p.add_argument("--ready-speed", type=int, default=5)
    p.add_argument("--pose", default="")
    p.add_argument("--no-gripper", action="store_true")
    p.add_argument("--x-min", type=float, default=-0.8)
    p.add_argument("--x-max", type=float, default=0.0)
    p.add_argument("--y-min", type=float, default=-0.5)
    p.add_argument("--y-max", type=float, default=0.5)
    p.add_argument("--z-min", type=float, default=0.05)
    p.add_argument("--z-max", type=float, default=0.85)
    p.add_argument(
        "--camera",
        default=os.environ.get("RECORD_CAMERA", "/dev/video0"),
        help="大白 USB 设备(front)",
    )
    p.add_argument(
        "--rtsp",
        default="",
        help=argparse.SUPPRESS,  # 已停用海康,保留以免旧命令行报错
    )
    p.add_argument(
        "--astra-camera",
        default=os.environ.get("RECORD_ASTRA_CAMERA", DEFAULT_ASTRA_DEVICE),
        help="Astra Pro USB 设备(astra);若设置 --astra-rtsp 则忽略",
    )
    p.add_argument(
        "--astra-rtsp",
        default=os.environ.get("RECORD_ASTRA_RTSP", ""),
        help="第二路改为 RTSP(USB insert 单目子码流);设置后不再打开 USB Astra",
    )
    p.add_argument(
        "--front-key",
        default=os.environ.get("RECORD_FRONT_KEY", DEFAULT_FRONT_KEY),
        help="大白图像 feature 短名,默认 front",
    )
    p.add_argument(
        "--astra-key",
        default=os.environ.get("RECORD_ASTRA_KEY", DEFAULT_ASTRA_KEY),
        help="Astra 图像 feature 短名,默认 astra",
    )
    p.add_argument(
        "--exterior-key",
        default="",
        help=argparse.SUPPRESS,  # 已停用海康
    )
    p.add_argument(
        "--camera-key",
        default=os.environ.get("RECORD_CAMERA_KEY", ""),
        help="兼容旧参数;若设置则覆盖 --front-key",
    )
    p.add_argument("--cam-width", type=int, default=640)
    p.add_argument("--cam-height", type=int, default=480)
    p.add_argument(
        "--repo-id",
        default=os.environ.get("RECORD_REPO_ID", "local/rm65_usb_insert_dual"),
    )
    p.add_argument(
        "--root",
        default=os.environ.get(
            "RECORD_ROOT",
            str(ROOT / "datasets" / "rm65_usb_insert_dual"),
        ),
    )
    p.add_argument(
        "--task",
        default="Pick up the USB plug and insert it into the port",
    )
    p.add_argument("--num-episodes", type=int, default=20)
    p.add_argument(
        "--episode-time-s",
        type=float,
        default=float(os.environ.get("RECORD_EPISODE_TIME_S", "120")),
        help="单条 episode 最长秒数,超时自动保存(默认 120)",
    )
    p.add_argument("--video", action="store_true", default=True)
    p.add_argument("--no-video", action="store_false", dest="video")
    p.add_argument("--resume", action="store_true")
    p.add_argument(
        "--embodiment-id",
        default=os.environ.get("RECORD_EMBODIMENT_ID", "rm65_dahuan"),
    )
    p.add_argument(
        "--finalize-on-exit",
        action="store_true",
        default=True,
        help="退出时写 stats.json + clean_report.json(默认开)",
    )
    p.add_argument("--no-finalize-on-exit", action="store_false", dest="finalize_on_exit")
    p.add_argument(
        "--preview",
        action=argparse.BooleanOptionalAction,
        default=True,
        help="录制进程用已取帧出预览(默认开)",
    )
    p.add_argument(
        "--front-preview-port",
        type=int,
        default=int(os.environ.get("RECORD_FRONT_PREVIEW_PORT", "8090")),
        help="大白预览端口(默认 8090)",
    )
    p.add_argument(
        "--astra-preview-port",
        type=int,
        default=int(os.environ.get("RECORD_ASTRA_PREVIEW_PORT", "8093")),
        help="Astra 预览端口(默认 8093;USB insert 新 Astra 用 8094)",
    )
    p.add_argument(
        "--astra-preview-title",
        default=os.environ.get("RECORD_ASTRA_PREVIEW_TITLE", "Astra Pro"),
        help="Astra 预览页标题",
    )
    args = p.parse_args()
    if args.camera_key:
        args.front_key = args.camera_key
    try:
        return run_record(args)
    except Exception as exc:
        print(f"错误: {exc}", file=sys.stderr)
        return 1


if __name__ == "__main__":
    raise SystemExit(main())