|
| 1 | +import argparse |
| 2 | +import threading |
| 3 | +import time |
| 4 | + |
| 5 | +from teleop_demo_python.hardware import ( |
| 6 | + DualArmURController, |
| 7 | + DynamixelHeadController, |
| 8 | +) |
| 9 | +from teleop_demo_python.utils.pico_client import PicoClient |
| 10 | + |
| 11 | + |
| 12 | +def main(): |
| 13 | + parser = argparse.ArgumentParser(description="Run head and dual arm teleoperation control.") |
| 14 | + parser.add_argument( |
| 15 | + "--reset", |
| 16 | + action="store_true", |
| 17 | + help="Run the reset procedure for the dual arm controller.", |
| 18 | + ) |
| 19 | + parser.add_argument( |
| 20 | + "--visualize_placo", action="store_true", help="Visualize Placo in the arm controller." |
| 21 | + ) |
| 22 | + |
| 23 | + args = parser.parse_args() |
| 24 | + |
| 25 | + pico_client = PicoClient() |
| 26 | + head_controller = DynamixelHeadController(pico_client) |
| 27 | + arm_controller = DualArmURController(pico_client, visualize_placo=args.visualize_placo) |
| 28 | + |
| 29 | + if args.reset: |
| 30 | + print("Reset flag detected. Running arm controller reset procedure...") |
| 31 | + try: |
| 32 | + arm_controller.reset() |
| 33 | + print("Arm controller reset procedure completed.") |
| 34 | + except Exception as e: |
| 35 | + print(f"Error during arm_controller.reset(): {e}") |
| 36 | + else: |
| 37 | + print("No reset flag detected. Proceeding with normal operation.") |
| 38 | + arm_controller.calc_target_joint_position() |
| 39 | + |
| 40 | + stop_signal = threading.Event() |
| 41 | + head_thread = threading.Thread( |
| 42 | + target=head_controller.run_thread, |
| 43 | + args=(stop_signal,), |
| 44 | + ) |
| 45 | + left_arm_thread = threading.Thread( |
| 46 | + target=arm_controller.run_left_controller_thread, |
| 47 | + args=(stop_signal,), |
| 48 | + ) |
| 49 | + right_arm_thread = threading.Thread( |
| 50 | + target=arm_controller.run_right_controller_thread, |
| 51 | + args=(stop_signal,), |
| 52 | + ) |
| 53 | + ik_thread = threading.Thread( |
| 54 | + target=arm_controller.run_ik_thread, |
| 55 | + args=(stop_signal,), |
| 56 | + ) |
| 57 | + |
| 58 | + # Start the threads |
| 59 | + head_thread.start() |
| 60 | + left_arm_thread.start() |
| 61 | + right_arm_thread.start() |
| 62 | + ik_thread.start() |
| 63 | + |
| 64 | + while not stop_signal.is_set(): |
| 65 | + try: |
| 66 | + time.sleep(0.01) |
| 67 | + except KeyboardInterrupt: |
| 68 | + print("KeyboardInterrupt detected. Exiting...") |
| 69 | + stop_signal.set() # Trigger the stop signal for all threads |
| 70 | + |
| 71 | + head_thread.join() |
| 72 | + left_arm_thread.join() |
| 73 | + right_arm_thread.join() |
| 74 | + ik_thread.join() |
| 75 | + |
| 76 | + head_controller.close() |
| 77 | + arm_controller.close() |
| 78 | + print("All controllers have been stopped and disconnected.") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments