unitree_sdk2_python

repository·master·Indexed 20 days ago

https://github.com/unitreerobotics/unitree_sdk2_python

A Python interface for the Unitree SDK2 that enables interaction with Unitree robots via DDS communication. It provides high-level and low-level control interfaces for status acquisition and robot movement, supporting models such as Go2, B2, H1, G1, and their variants. The SDK includes tools for RPC communication via Client and Server classes, as well as examples for wireless controller status, OpenCV camera access, obstacle avoidance, and VUI control.

Tokens
3.7K
Snippets
15
Records
22
Agent score
73%

What's inside unitree_sdk2_python

  1. Use Wireless Controller, Camera, and VUI features

    master

    Additional robot features can be accessed via the following commands (replace enp2s0 with your network interface):

    • Wireless Controller Status: Outputs the status of each key on the remote.
      python3 ./example/wireless_controller/wireless_controller.py enp2s0
    • Front Camera (OpenCV): Opens a graphical window showing the camera feed. Press ESC to exit.
      python3 ./example/front_camera/camera_opencv.py enp2s0
    • Obstacle Avoidance Switch: Cycles the obstacle avoidance service on and off.
      python3 ./example/obstacles_avoid_switch/obstacles_avoid_switch.py enp2s0
    • Light and Volume Control (VUI): Cycles the robot's volume and light brightness.
      python3 ./example/vui_client/vui_client_example.py enp2s0
  2. Install unitree_sdk2_python from source

    master

    To install the SDK, ensure you have python3-pip installed, then clone the repository and install it in editable mode using pip3 install -e ..

    Dependencies:

    • Python >= 3.8
    • cyclonedds == 0.10.2
    • numpy
    • opencv-python
    cd ~
    sudo apt install python3-pip
    git clone https://github.com/unitreerobotics/unitree_sdk2_python.git
    cd unitree_sdk2_python
    pip3 install -e .
  3. Install unitree_sdk2_python

    master

    To install the SDK, ensure you have the necessary dependencies and follow the installation steps using pip3.

    Dependencies:

    • python>=3.8
    • cyclonedds==0.10.2
    • numpy
    • opencv-python

    Installation Steps:

    1. Install python3-pip if not already present.
    2. Clone the repository.
    3. Install the package in editable mode using pip3 install -e ..
    cd ~
    sudo apt install python3-pip
    git clone https://github.com/unitreerobotics/unitree_sdk2_python.git
    cd unitree_sdk2_python
    pip3 install -e .
  4. Access High-Level Robot Status and Control

    master

    High-level interfaces allow for robot status acquisition and control (e.g., standing, moving, following trajectories). Always replace enp2s0 with your actual network interface name.

    Read High-Level Status:

    python3 ./example/high_level/read_highstate.py enp2s0

    High-Level Control (Sport Mode):

    python3 ./example/high_level/sportmode_test.py enp2s0

    Available test methods within sportmode_test.py include:

    • test.StandUpDown(): Stand up and lie down
    • test.VelocityMove(): Velocity control
    • test.BalanceAttitude(): Attitude control
    • test.TrajectoryFollow(): Trajectory tracking
    • test.SpecialMotions(): Special motions
    # Read status
    python3 ./example/high_level/read_highstate.py enp2s0
    
    # Control robot
    python3 ./example/high_level/sportmode_test.py enp2s0
  5. Use High-Level Status and Control

    master

    High-level interfaces allow for robot state monitoring and motion control. Most commands require specifying the network interface name (e.g., enp2s0).

    High-Level Status: Read robot state using read_highstate.py.

    High-Level Control: Control robot motions using sportmode_test.py. The script provides several test methods:

    • test.StandUpDown(): Stand up or lie down.
    • test.VelocityMove(): Velocity control.
    • test.BalanceAttitude(): Attitude control.
    • test.TrajectoryFollow(): Trajectory following.
    • test.SpecialMotions(): Special motions.
    # Read high-level status
    python3 ./example/high_level/read_highstate.py enp2s0
    
    # High-level control test
    python3 ./example/high_level/sportmode_test.py enp2s0
  6. Control Obstacle Avoidance and VUI (Lights/Volume)

    master

    The SDK provides interfaces for auxiliary robot services:

    Obstacle Avoidance: Toggle the obstacle avoidance service using obstacles_avoid_switch.py.

    VUI (Voice/User Interface) Client: Control light brightness and volume levels using vui_client_example.py.

    # Toggle obstacle avoidance
    python3 ./example/obstacles_avoid_switch/obstacles_avoid_switch.py enp2s0
    
    # Control lights and volume
    python3 ./example/vui_client/vui_client_example.py enp2s0
  7. Access Low-Level Status and Motor Control

    master

    Low-level interfaces provide direct access to joints and sensors.

    Warning: Before using low-level motor control, you must use the mobile app to turn off the high-level motion service (sport_mode) to prevent conflicting instructions.

    Read Low-Level Status: This command outputs the state of the right front leg hip joint, IMU, and battery voltage.

    python3 ./example/low_level/lowlevel_control.py enp2s0

    Low-Level Motor Control: Running the same command (lowlevel_control.py) while in low-level mode will cause the left hind leg hip joint to maintain a 0-degree position (with kp=10, kd=1) and the left hind leg calf joint to output 1Nm of torque.

  8. Use Low-Level Status and Motor Control

    master

    Low-level interfaces provide direct access to motor states and control.

    Important: Before using low-level motor control, you must use the mobile app to disable the high-level motion service (sport_mode) to prevent command conflicts.

    Low-Level Status: Run lowlevel_control.py to output hip joint states, IMU data, and battery voltage.

    Low-Level Motor Control: Running lowlevel_control.py also demonstrates basic motor control (e.g., setting specific torque or position on a joint).

    # Low-level status and control
    python3 ./example/low_level/lowlevel_control.py enp2s0
  9. Manage robot leases with Client

    master

    When enabaleLease is set to True during initialization, you must manage the lease lifecycle to ensure commands are authorized.

    Methods:

    • WaitLeaseApplied(): Blocks until the lease has been successfully applied.
    • GetLeaseId(): Returns the current active lease ID if enabled, otherwise returns None.
    from unitree_sdk2py.rpc.client import Client
    
    client = Client("your_service_name", enabaleLease=True)
    # Wait for the lease to be applied before sending commands
    client.WaitLeaseApplied()
    
    lease_id = client.GetLeaseId()
    print(f"Active Lease ID: {lease_id}")
  10. Troubleshoot cyclonedds installation error

    master

    If you encounter the error Could not locate cyclonedds. Try to set CYCLONEDDS_HOME or CMAKE_PREFIX_PATH, you must manually compile and install cyclonedds from source before installing the SDK.

    1. Clone and build cyclonedds (use branch releases/0.10.x):
    cd ~
    git clone https://github.com/eclipse-cyclonedds/cyclonedds -b releases/0.10.x 
    cd cyclonedds && mkdir build install && cd build
    cmake .. -DCMAKE_INSTALL_PREFIX=../install
    cmake --build . --target install
    1. Set the CYCLONEDDS_HOME environment variable to the installation path and install the SDK:
    cd ~/unitree_sdk2_python
    export CYCLONEDDS_HOME=~/cyclonedds/install
    pip3 install -e .