|
| 1 | +import subprocess |
| 2 | +import re |
| 3 | +import math |
| 4 | +import time |
| 5 | +import threading |
| 6 | + |
| 7 | +from typing import Dict, Optional |
| 8 | +from xml.etree import ElementTree as ET |
| 9 | +import sys |
| 10 | +sys.path = sys.path + ['/usr/lib/python3/dist-packages'] |
| 11 | +from mcap_protobuf.writer import Writer |
| 12 | +from gz.msgs10.pose_v_pb2 import Pose_V |
| 13 | +from gz.transport13 import Node |
| 14 | + |
| 15 | + |
| 16 | +class Pose: |
| 17 | + def __init__(self, name: str, position: Dict[str, float], orientation: Dict[str, float]): |
| 18 | + self.name = name |
| 19 | + self.position = position |
| 20 | + self.orientation = orientation |
| 21 | + |
| 22 | + def distance_to(self, other: "Pose") -> float: |
| 23 | + dx = self.position.get("x", 0) - other.position.get("x", 0) |
| 24 | + dy = self.position.get("y", 0) - other.position.get("y", 0) |
| 25 | + dz = self.position.get("z", 0) - other.position.get("z", 0) |
| 26 | + return math.sqrt(dx*dx + dy*dy + dz*dz) |
| 27 | + |
| 28 | + def is_near(self, other: "Pose", threshold: float = 0.5) -> bool: |
| 29 | + return self.distance_to(other) < threshold |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +class Gazebo: |
| 34 | + def __init__(self, recording_path: str = None): |
| 35 | + self.poses: Dict[str, Pose] = {} |
| 36 | + self.node = Node() |
| 37 | + world_name = "collision_test" |
| 38 | + self.topic_dynamicposes = f"/world/{world_name}/dynamic_pose/info" |
| 39 | + self._recording = False |
| 40 | + self._mcap_writer = None |
| 41 | + self._mcap_file = None |
| 42 | + if recording_path is not None: |
| 43 | + self._mcap_file = open(recording_path, "wb") |
| 44 | + self._mcap_writer = Writer(self._mcap_file) |
| 45 | + self._recording = True |
| 46 | + self.node.subscribe(Pose_V, self.topic_dynamicposes, self.posev_cb) |
| 47 | + |
| 48 | + def update_pose(self, name: str, position: Dict[str, float], orientation: Dict[str, float]): |
| 49 | + self.poses[name] = Pose(name, position, orientation) |
| 50 | + |
| 51 | + def get(self, name: str) -> Optional[Pose]: |
| 52 | + return self.poses.get(name) |
| 53 | + |
| 54 | + def posev_cb(self, msg: Pose_V): |
| 55 | + if self._recording and self._mcap_writer: |
| 56 | + now = int(time.time() * 1e9) |
| 57 | + self._mcap_writer.write_message( |
| 58 | + topic=self.topic_dynamicposes, |
| 59 | + message=msg, |
| 60 | + log_time=now, |
| 61 | + publish_time=now, # do we compute from msg.header.stamp instead?, or even a sim time topic? |
| 62 | + ) |
| 63 | + print("wrote 1 message") |
| 64 | + for pose_msg in msg.pose: |
| 65 | + name = pose_msg.name |
| 66 | + position = { |
| 67 | + "x": getattr(pose_msg.position, "x", 0.0), |
| 68 | + "y": getattr(pose_msg.position, "y", 0.0), |
| 69 | + "z": getattr(pose_msg.position, "z", 0.0), |
| 70 | + } |
| 71 | + orientation = { |
| 72 | + "x": getattr(pose_msg.orientation, "x", 0.0), |
| 73 | + "y": getattr(pose_msg.orientation, "y", 0.0), |
| 74 | + "z": getattr(pose_msg.orientation, "z", 0.0), |
| 75 | + "w": getattr(pose_msg.orientation, "w", 1.0), |
| 76 | + } |
| 77 | + self.update_pose(name, position, orientation) |
| 78 | + |
| 79 | + def start(self): |
| 80 | + self._running = True |
| 81 | + self._thread = threading.Thread(target=self._spin_loop, daemon=True) |
| 82 | + self._thread.start() |
| 83 | + |
| 84 | + def stop(self): |
| 85 | + self._running = False |
| 86 | + if hasattr(self, "_thread"): |
| 87 | + self._thread.join() |
| 88 | + |
| 89 | + if self._recording: |
| 90 | + if self._mcap_file: |
| 91 | + self._mcap_writer.finish() |
| 92 | + self._mcap_file.close() |
| 93 | + self._recording = False |
| 94 | + |
| 95 | + def _spin_loop(self): |
| 96 | + while self._running: |
| 97 | + time.sleep(0.001) |
| 98 | + |
| 99 | +def main(): |
| 100 | + if len(sys.argv) != 2: |
| 101 | + print("Usage: python recorder.py <output.mcap>") |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | + output_path = sys.argv[1] |
| 105 | + gz = Gazebo(recording_path=output_path) |
| 106 | + gz.start() |
| 107 | + print(f"Recording simulation to {output_path}. Press Ctrl+C to stop.") |
| 108 | + |
| 109 | + try: |
| 110 | + while True: |
| 111 | + time.sleep(1) |
| 112 | + except KeyboardInterrupt: |
| 113 | + print("Stopping recording...") |
| 114 | + gz.stop() |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments