SDK Development
===============

This section summarises how to set up and develop applications for the Unitree H2 using the official SDKs. It
is based on Unitree's
`H2 Developer Guide <https://support.unitree.com/home/en/H2_developer>`_ and the public Unitree repositories.
Always treat the official guide as the authoritative reference for your firmware version.

Overview
--------

The H2 is controlled through Unitree's CycloneDDS-based communication stack, which operates with or without
ROS 2 while remaining ROS 2 compatible. Three primary SDKs are available:

- **unitree_sdk2** — the C++ SDK for high- and low-level control, state subscription, and sensor access.
  Repository: `unitree_sdk2 <https://github.com/unitreerobotics/unitree_sdk2>`_.

- **unitree_sdk2_python** — the Python interface (pybind11 wrapper) for rapid prototyping.
  Repository: `unitree_sdk2_python <https://github.com/unitreerobotics/unitree_sdk2_python>`_.

- **unitree_ros2** — ROS 2 packages that bridge the same DDS interface into ROS 2 topics and messages.
  Repository: `unitree_ros2 <https://github.com/unitreerobotics/unitree_ros2>`_.

All Unitree open-source SDKs are published under the
`Unitree Robotics GitHub organisation <https://github.com/unitreerobotics>`_.

Requirements
------------

+-------------------------------+----------------------------------------------+
| **Item**                      | **Requirement**                              |
+===============================+==============================================+
| Operating System              | Ubuntu 20.04 LTS                             |
+-------------------------------+----------------------------------------------+
| Compiler                      | GCC 9.4.0 (C++17)                            |
+-------------------------------+----------------------------------------------+
| Build System                  | CMake 3.10+                                  |
+-------------------------------+----------------------------------------------+
| Architecture                  | x86_64 or aarch64                            |
+-------------------------------+----------------------------------------------+
| Transport                     | DDS (CycloneDDS)                             |
+-------------------------------+----------------------------------------------+
| Python (optional)             | Python 3.8+                                  |
+-------------------------------+----------------------------------------------+

Installing Dependencies
-----------------------

Install the build toolchain and required libraries:

.. code-block:: bash

   sudo apt update
   sudo apt install -y cmake g++ build-essential \
       libyaml-cpp-dev libeigen3-dev libboost-all-dev \
       libspdlog-dev libfmt-dev

Building and Installing unitree_sdk2
------------------------------------

Clone and build the C++ SDK:

.. code-block:: bash

   git clone https://github.com/unitreerobotics/unitree_sdk2.git
   cd unitree_sdk2
   mkdir build && cd build
   cmake ..
   make

To install the SDK system-wide (recommended for use in your own CMake projects):

.. code-block:: bash

   cd unitree_sdk2/build
   cmake .. -DCMAKE_INSTALL_PREFIX=/opt/unitree_robotics
   sudo make install

Installing the Python Interface
-------------------------------

For Python development, install ``unitree_sdk2_python``:

.. code-block:: bash

   git clone https://github.com/unitreerobotics/unitree_sdk2_python.git
   cd unitree_sdk2_python
   pip3 install -e .

Network Configuration
---------------------

The H2 communicates with a development PC over Gigabit Ethernet using CycloneDDS. Connect the robot and your
computer with an Ethernet cable, then identify the interface that links to the robot:

.. code-block:: bash

   # List network interfaces and find the one connected to the H2
   ifconfig

Unitree robots use the ``192.168.123.x`` subnet by convention. Ensure your development machine is on the **same
subnet** as the robot's onboard computer. Configure your PC's wired interface to a static address on that subnet:

.. code-block:: bash

   # Example: set a static IP on the wired interface (replace eth0 with your NIC)
   sudo ip addr add 192.168.123.222/24 dev eth0
   sudo ip link set eth0 up

   # Confirm the robot is reachable (use the robot's documented address)
   ping 192.168.123.<robot>

.. important::

   The exact network setup and any robot-specific addresses are documented in the official
   `H2 Developer Guide <https://support.unitree.com/home/en/H2_developer>`_. Confirm the values for your unit
   before configuring your PC.

Specifying the Network Interface
--------------------------------

``unitree_sdk2`` examples and applications must be told which network interface is connected to the robot. The
interface name (for example ``eth0``) is passed as a command-line argument when launching an example:

.. code-block:: bash

   # Run a built example, passing the network interface name
   ./example_program eth0

In C++, the interface is supplied when initialising the DDS channel factory:

.. code-block:: cpp

   #include <unitree/robot/channel/channel_factory.hpp>

   int main(int argc, char** argv)
   {
       // argv[1] is the network interface connected to the H2 (e.g. "eth0")
       unitree::robot::ChannelFactory::Instance()->Init(0, argv[1]);
       // ... create publishers / subscribers and control the robot ...
       return 0;
   }

The equivalent in Python:

.. code-block:: python

   import sys
   from unitree_sdk2py.core.channel import ChannelFactoryInitialize

   # sys.argv[1] is the network interface connected to the H2 (e.g. "eth0")
   ChannelFactoryInitialize(0, sys.argv[1])
   # ... create publishers / subscribers and control the robot ...

Control Interfaces
------------------

The SDK exposes both **high-level** and **low-level** control:

- **High-level** control issues task-space commands (locomotion modes, posture, velocity) and is the safe
  default for most applications.

- **Low-level** control commands individual joint targets (position, velocity, torque, stiffness, and damping).
  Low-level commands bypass built-in balancing safeguards and must remain within the documented joint limits.

The H2 joint indices, names, and motion limits used by the low-level interface are defined in the SDK headers
and the official `H2 Developer Guide <https://support.unitree.com/home/en/H2_developer>`_. Always validate
commanded joint motions against the documented limits before sending them.

.. warning::

   Custom low-level control code can command the robot into unsafe states. Test new control software with the
   H2 safely supported or in a clear, open area, keep the emergency stop within reach, and validate joint
   commands against the documented limits before deployment.

Further Resources
-----------------

- `H2 Developer Guide (official) <https://support.unitree.com/home/en/H2_developer>`_
- `unitree_sdk2 (C++ SDK) <https://github.com/unitreerobotics/unitree_sdk2>`_
- `unitree_sdk2_python (Python interface) <https://github.com/unitreerobotics/unitree_sdk2_python>`_
- `unitree_ros2 (ROS 2 packages) <https://github.com/unitreerobotics/unitree_ros2>`_
- `Unitree Robotics on GitHub <https://github.com/unitreerobotics>`_
- `Unitree Document Center <https://support.unitree.com/home/en/developer>`_
