unitree_sdk2

repository·main·Indexed 22 days ago

https://github.com/unitreerobotics/unitree_sdk2

The second generation software development kit for Unitree robots, providing interfaces for robot control and communication. It includes the unitree::robot::Client class for API interaction and lease management, as well as specialized control modes like PR Mode for the H1_2 parallel ankle mechanism. The SDK supports Ubuntu 20.04 and requires CMake 3.10+, GCC 9.4.0, and libraries such as yaml-cpp, Eigen3, Boost, spdlog, and fmt.

Tokens
3.6K
Snippets
7
Records
19
Agent score
77%

What's inside unitree_sdk2

  1. Control the H1_2 parallel ankle mechanism using PR mode

    main

    The Unitree H1_2 robot uses a parallel mechanism for its left and right ankles. While the hardware consists of four physical joints (A and B parallel joints, and P and R serial joints), the P (Pitch) and R (Roll) joints cannot be driven directly by motors.

    To simplify control, the SDK provides a PR mode that allows you to command the serial P and R joints directly. The robot's internal controller automatically converts these commands into the necessary movements for the A and B physical joints using kinematic and dynamic relationships.

    When using PR mode, each serial joint accepts the following control parameters:

    ParameterVariableDescription
    Feedforward TorquetauDirect torque command
    Target AngleqDesired joint position
    Target Angular VelocitydqDesired joint velocity
    Joint StiffnesskpProportional gain
    Joint DampingkdDerivative gain

    The total executed torque for the serial joint is calculated as: T = kp * (q - q_m) + kd * (dq - dq_m) + tau, where q_m and dq_m are the measured position and velocity.

  2. How PR Mode works for H1_2 Parallel Mechanism Control

    main

    The Unitree H1_2 robot uses a parallel mechanism for its ankle joints (left and right legs). While the hardware consists of four joints (Parallel joints A and B, and Serial joints Pitch P and Roll R), only the A and B joints are directly motor-driven.

    To simplify control, the SDK provides PR Mode. This mode allows users to interact with the ankle as if it were a standard serial joint system. When PR Mode is enabled, the robot's internal controller automatically converts your commands for the P and R joints into the necessary motor commands for the A and B joints using kinematic and dynamic models.

    In PR Mode, each serial joint accepts the following command variables:

    Command NameVariableDescription
    Feedforward TorquetauDirect torque command
    Target AngleqDesired joint position
    Target Angular VelocitydqDesired joint velocity
    Joint StiffnesskpProportional gain
    Joint DampingkdDerivative gain

    The total applied torque is calculated as: T = kp * (q - q_m) + kd * (dq - dq_m) + tau, where q_m and dq_m are the measured position and velocity.

  3. Run the H1_2 ankle tracking test routine

    main

    To test the ankle PR mode control performance, use the provided h1_2_ankle_track example.

    Safety Warning: Ensure the robot is suspended (hanging) before running this test to prevent injury or damage.

    Execution:

    1. Install and build unitree_sdk2.
    2. Run the following command in your terminal, replacing network_interface with your actual network interface (e.g., eth0 or enp3s0):
    h1_2_ankle_track network_interface

    Upon startup, the robot will return to its zero position and then begin periodic ankle oscillations, printing the expected and measured values to the terminal.

  4. Install dependencies for unitree_sdk2

    main

    Before building or running the SDK on Ubuntu 20.04, ensure you have CMake (3.10+), GCC (9.4.0), and Make installed, along with the following development libraries:

    • libyaml-cpp-dev
    • libeigen3-dev
    • libboost-all-dev
    • libspdlog-dev
    • libfmt-dev
    apt-get update
    apt-get install -y cmake g++ build-essential libyaml-cpp-dev libeigen3-dev libboost-all-dev libspdlog-dev libfmt-dev
  5. Run the H1_2 Ankle Tracking experiment

    main

    To test the performance of the H1_2 ankle in PR Mode using the provided tracking example, follow these steps:

    1. Safety First: Ensure the robot is suspended before starting.
    2. Compile: Ensure unitree_sdk2 is installed and compiled.
    3. Execute: Run the tracking routine from the terminal, providing the network interface.

    Once started, the robot will reset to the zero position and then periodically swing its ankles while printing desired and measured values to the terminal.

    h1_2_ankle_track network_interface
  6. Integrate unitree_sdk2 into a CMake project

    main

    To use the SDK in your own application, refer to example/cmake_sample for implementation details.

    Important: If you install the library to a non-standard location (anything other than /opt/unitree_robotics), you must add that path to your ${CMAKE_PREFIX_PATH} environment variable so that find_package() can locate the SDK.

  7. Install unitree_sdk2 to a custom directory

    main

    You can specify a custom installation path using the DCMAKE_INSTALL_PREFIX flag during the CMake configuration step. This is useful for managing multiple versions or keeping the system directory clean.

    mkdir build
    cd build
    cmake .. -DCMAKE_INSTALL_PREFIX=/opt/unitree_robotics
    sudo make install
  8. Implement ankle joint tracking in PR mode

    main

    To perform tracking experiments on the H1_2 ankle, you must enable PR mode and then populate the dds_low_command motor commands for the specific ankle indices.

    Note on Motor Indices (from example):

    • 4: Left Ankle Pitch
    • 5: Left Ankle Roll
    • 10: Right Ankle Pitch
    • 11: Right Ankle Roll

    Example Workflow:

    1. Set mode_ = PR.
    2. Calculate desired trajectories (e.g., sine waves).
    3. Update dds_low_command.motor_cmd().at(index) with q, dq, kp, kd, and tau.
    4. Read actual values from low_state_.motor_state().at(index).q() to verify tracking.
    // [Stage 2]: swing ankle's PR
    mode_ = PR;  // Enable PR mode
    // generate sin/cos trajectory
    double max_P = 0.25;  // [rad]
    double max_R = 0.25;  // [rad]
    double t = time_ - duration_;
    double L_P_des = max_P * std::cos(2.0 * M_PI * t);
    double L_R_des = max_R * std::sin(2.0 * M_PI * t);
    double R_P_des = max_P * std::cos(2.0 * M_PI * t);
    double R_R_des = -max_R * std::sin(2.0 * M_PI * t);
    
    // update ankle joint position targets
    float Kp_Pitch = 80;
    float Kd_Pitch = 1;
    float Kp_Roll = 80;
    float Kd_Roll = 1;
    
    dds_low_command.motor_cmd().at(4).q() = L_P_des;  // 4: LeftAnklePitch
    dds_low_command.motor_cmd().at(4).dq() = 0;
    dds_low_command.motor_cmd().at(4).kp() = Kp_Pitch;
    dds_low_command.motor_cmd().at(4).kd() = Kd_Pitch;
    dds_low_command.motor_cmd().at(4).tau() = 0;
    
    dds_low_command.motor_cmd().at(5).q() = L_R_des;  // 5: LeftAnkleRoll
    dds_low_command.motor_cmd().at(5).dq() = 0;
    dds_low_command.motor_cmd().at(5).kp() = Kp_Roll;
    dds_low_command.motor_cmd().at(5).kd() = Kd_Roll;
    dds_low_command.motor_cmd().at(5).tau() = 0;
    
    dds_low_command.motor_cmd().at(10).q() = R_P_des;  // 10: RightAnklePitch
    dds_low_command.motor_cmd().at(10).dq() = 0;
    dds_low_command.motor_cmd().at(10).kp() = Kp_Pitch;
    dds_low_command.motor_cmd().at(10).kd() = Kd_Pitch;
    dds_low_command.motor_cmd().at(10).tau() = 0;
    
    dds_low_command.motor_cmd().at(11).q() = R_R_des;  // 11: RightAnkleRoll
    dds_low_command.motor_cmd().at(11).dq() = 0;
    dds_low_command.motor_cmd().at(11).kp() = Kp_Roll;
    dds_low_command.motor_cmd().at(11).kd() = Kd_Roll;
    dds_low_command.motor_cmd().at(11).tau() = 0;
  9. Control H1_2 ankle joints in PR Mode

    main

    To control the H1_2 ankle joints using the serial interface, you must first set the control mode to PR. You then populate the dds_low_command object with target values for position (q), velocity (dq), stiffness (kp), damping (kd), and feedforward torque (tau).

    Note the specific motor indices used in the example for the H1_2 ankle joints:

    • 4: Left Ankle Pitch
    • 5: Left Ankle Roll
    • 10: Right Ankle Pitch
    • 11: Right Ankle Roll
  10. Use the Client class to interact with the robot

    main

    The unitree::robot::Client class is the primary interface for communicating with the robot. It inherits from ClientBase and provides methods for calling robot APIs, managing API versions, and handling leases.

    To use it, instantiate a Client with a unique name. If your application requires exclusive access or specific resource management, you can enable the lease mechanism by setting enableLease to true in the constructor.