| |
| |
|
|
| import os |
| import sys |
| import time |
| import math |
| import csv |
| import datetime |
|
|
| import pygame |
| import cv2 |
| import numpy as np |
| import pyrealsense2 as rs |
|
|
| from piper_sdk import C_PiperInterface_V2 |
|
|
| |
| X_MIN, X_MAX = 200000, 500000 |
| Y_MIN, Y_MAX = -100000, 200000 |
| Z_MIN, Z_MAX = 140000, 250000 |
| FIXED_RX = 177756 |
| FIXED_RY = 6035 |
| FIXED_RZ = -158440 |
| ROTATION_RANGE = 50000 |
| GRIP_OPEN, GRIP_CLOSE = 80000, 0 |
| HEARTBEAT_CMD_1, HEARTBEAT_CMD_2 = 0x01, 0x02 |
| HEARTBEAT_SPEED = 70 |
| HEARTBEAT_FLAG = 0x00 |
| LOOP_HZ = 30 |
| DELAY_S = 0.01 |
|
|
| |
| END_EPISODE_KEY = pygame.K_p |
|
|
|
|
| def enable_fun(piper: C_PiperInterface_V2, timeout: float = 5.0): |
| """ |
| Enable all six motors and the gripper. |
| Exits on timeout if not all drivers report enabled. |
| """ |
| start_time = time.time() |
| while True: |
| piper.EnableArm(7) |
| piper.GripperCtrl(GRIP_OPEN, 1000, 0x01, 0) |
| info = piper.GetArmLowSpdInfoMsgs() |
| flags = [ |
| info.motor_1.foc_status.driver_enable_status, |
| info.motor_2.foc_status.driver_enable_status, |
| info.motor_3.foc_status.driver_enable_status, |
| info.motor_4.foc_status.driver_enable_status, |
| info.motor_5.foc_status.driver_enable_status, |
| info.motor_6.foc_status.driver_enable_status, |
| ] |
| print("Motor enabled states:", all(flags)) |
| if all(flags): |
| print("Piper online ✅") |
| return |
| if time.time() - start_time > timeout: |
| print(f"Enable timeout after {timeout} seconds. Exiting.") |
| sys.exit(1) |
| time.sleep(1) |
|
|
|
|
| if __name__ == "__main__": |
| |
| piper = C_PiperInterface_V2("can0") |
| piper.ConnectPort() |
| enable_fun(piper) |
|
|
| |
| piper.GripperCtrl(GRIP_OPEN, 1000, 0x01, 0) |
| piper.MotionCtrl_2(HEARTBEAT_CMD_1, HEARTBEAT_CMD_2, HEARTBEAT_SPEED, HEARTBEAT_FLAG) |
| init_x = (X_MIN + X_MAX) // 2 |
| init_y = (Y_MIN + Y_MAX) // 2 |
| init_z = (Z_MIN + Z_MAX) // 2 |
| piper.EndPoseCtrl(init_x, init_y, init_z, FIXED_RX, FIXED_RY, FIXED_RZ) |
| time.sleep(DELAY_S) |
|
|
| |
| episode_id = datetime.datetime.now().strftime('%Y%m%d_%H%M%S') |
| base_dir = os.path.join('data', episode_id) |
| color_dir = os.path.join(base_dir, 'color') |
| depth_dir = os.path.join(base_dir, 'depth') |
| cam_dir = os.path.join(base_dir, 'webcam') |
| os.makedirs(color_dir, exist_ok=True) |
| os.makedirs(depth_dir, exist_ok=True) |
| os.makedirs(cam_dir, exist_ok=True) |
|
|
| |
| csv_path = os.path.join(base_dir, 'data.csv') |
| csv_file = open(csv_path, mode='w', newline='') |
| writer = csv.writer(csv_file) |
| headers = [ |
| 'timestamp', |
| 'joint1', 'joint2', 'joint3', 'joint4', 'joint5', 'joint6', |
| 'x', 'y', 'z', 'rx', 'ry', 'rz', 'grip', |
| 'color_image', 'depth_image', 'webcam_image' |
| ] |
| writer.writerow(headers) |
|
|
| |
| rs_pipeline = rs.pipeline() |
| rs_config = rs.config() |
| rs_config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) |
| rs_config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) |
| rs_pipeline.start(rs_config) |
|
|
| cap = cv2.VideoCapture(10) |
| if not cap.isOpened(): |
| print("Camera 8 failed to open.") |
| else: |
| print("Camera 8 opened successfully.") |
|
|
| |
| pygame.init() |
| pygame.joystick.init() |
| screen = pygame.display.set_mode((100, 100)) |
| if pygame.joystick.get_count() == 0: |
| print("No joystick detected.") |
| sys.exit(1) |
| joystick = pygame.joystick.Joystick(0) |
| joystick.init() |
| print(f"Detected controller: {joystick.get_name()}") |
| clock = pygame.time.Clock() |
|
|
| try: |
| while True: |
| for event in pygame.event.get(): |
| if event.type == pygame.QUIT: |
| raise KeyboardInterrupt |
| if event.type == pygame.KEYDOWN and event.key == END_EPISODE_KEY: |
| print("Ending episode and exiting script.") |
| sys.exit(0) |
|
|
| |
| raw_ly = joystick.get_axis(0) |
| raw_lx = joystick.get_axis(1) |
| raw_lt = joystick.get_axis(2) |
| raw_rx = joystick.get_axis(3) |
| raw_rt = joystick.get_axis(5) |
|
|
| |
| mapped_x = int(X_MIN + (raw_lx + 1) * 0.5 * (X_MAX - X_MIN)) |
| mapped_y = int(Y_MIN + (raw_ly + 1) * 0.5 * (Y_MAX - Y_MIN)) |
| mapped_z = int(Z_MIN + (1 - (raw_lt + 1) * 0.5) * (Z_MAX - Z_MIN)) |
| mapped_rz = int(FIXED_RZ + raw_rx * ROTATION_RANGE) |
| grip_val = int(GRIP_OPEN + (raw_rt + 1) * 0.5 * (GRIP_CLOSE - GRIP_OPEN)) |
|
|
| |
| piper.MotionCtrl_2(HEARTBEAT_CMD_1, HEARTBEAT_CMD_2, HEARTBEAT_SPEED, HEARTBEAT_FLAG) |
| piper.EndPoseCtrl(mapped_x, mapped_y, mapped_z, FIXED_RX, FIXED_RY, mapped_rz) |
| piper.GripperCtrl(grip_val, 1000, 0x01, 0) |
|
|
| |
| ts = datetime.datetime.now().strftime('%Y%m%d_%H%M%S_%f') |
|
|
| |
| high_info = piper.GetArmHighSpdInfoMsgs() |
| try: |
| joints = [ |
| getattr(high_info, f"motor_{i}").mechanical_angle |
| for i in range(1, 7) |
| ] |
| except Exception: |
| joints = [None] * 6 |
|
|
| |
| frames = rs_pipeline.wait_for_frames() |
| color_frame = frames.get_color_frame() |
| depth_frame = frames.get_depth_frame() |
| color_image = np.asanyarray(color_frame.get_data()) |
| depth_image = np.asanyarray(depth_frame.get_data()) |
|
|
| ret_cam, cam_image = cap.read() |
| print("ret_cam:", ret_cam) |
| if cam_image is not None: |
| print("cam_image shape:", cam_image.shape) |
| cv2.imwrite("test_cam_output.png", cam_image) |
| else: |
| print("Camera frame is None") |
|
|
| |
| color_path = os.path.join(color_dir, f"color_{ts}.png") |
| depth_path = os.path.join(depth_dir, f"depth_{ts}.png") |
| cam_path = os.path.join(cam_dir, f"cam_{ts}.png") |
| cv2.imwrite(color_path, color_image) |
| cv2.imwrite(depth_path, depth_image) |
| if ret_cam: |
| cv2.imwrite(cam_path, cam_image) |
| else: |
| cam_path = '' |
|
|
| |
| row = [ts] + joints + [mapped_x, mapped_y, mapped_z, |
| FIXED_RX, FIXED_RY, mapped_rz, |
| grip_val, |
| color_path, depth_path, cam_path] |
| writer.writerow(row) |
| csv_file.flush() |
|
|
| |
| print(f"{ts} | X:{mapped_x} Y:{mapped_y} Z:{mapped_z} RZ:{mapped_rz} Grip:{grip_val}") |
| clock.tick(LOOP_HZ) |
| time.sleep(DELAY_S) |
|
|
| except KeyboardInterrupt: |
| print("Interrupted by user, exiting.") |
|
|
| finally: |
| |
| csv_file.close() |
| rs_pipeline.stop() |
| cap.release() |
| pygame.quit() |
|
|