Environment Setup for Go1 with ROS



# Overview
This page describes how to setup environment to simulate and control Go1 with ROS. The following contents are included.

- Launch Docker
- Prepare catkin workspace
- Install ROS packages

# Environment
- unitree_legged_sdk v3.5.1 (v3.8.0)
- unitree_ros_to_real v3.5.0
- unitree_ros (version is not specified. installed on June 29, 2022)
Since unitree_* repositories seem not to be well synchronized, you may need to add some modifications if you use newer versions.

# Procedure
### Run docker 

```console
docker run -p 6080:80 --shm-size=512m --name ros_go1 -d tiryoh/ros-desktop-vnc:melodic 
```

Then, access to
```
http://127.0.0.1:6080
```
with your browser.

-------------------
### Preparation of catkin workspace
Launch terminal in the browser, and make catkin workspace directory.
```console
mkdir -p catkin_ws/src
cd catkin_ws
```

You can use `catkin build` instead of `catkin_make` by installing the following package.
```console
sudo apt install python-catkin-tools
```
or 
```console
sudo apt install python3-catkin-tools
```
for ROS noetic.

Then, initialize it with
```
cd ~/catkin_ws
catkin init
```
Make sure that run this command in catkin_ws directory.

-------------------
### Installation of packages
You need to install several packages and libraries to run the ROS simulator.
- unitree_legged_sdk
  - Boost (version 1.5.4 or higher)
  - CMake (version 2.8.3 or higher)
  - LCM (version 1.4.0 or higher)
  - g++ (version 8.3.0 or higher)
- unitree_ros_to_real
- unitree_ros

-------------------
#### unitree_legged_sdk

##### Boost
https://kaworu.jpn.org/cpp/boostのインストール

```console
sudo apt update
sudo apt install libboost-dev
```

##### CMake
https://qiita.com/comachi/items/d0c1ce5d7b90fe30fced

##### LCM
https://lcm-proj.github.io/build_instructions.html

```console
cd ~/catkin_ws/src
git clone https://github.com/lcm-proj/lcm.git
cd lcm
mkdir build
cd build
cmake ..
make
sudo make install
```

##### g++
https://0h-n0.github.io/blog/2020/05/07/ubuntu-gcc.html

You can check the version of g++ with
```console
g++ --version
```

If it is lower than the requirement, install higher versions.
(You don't need all of them.)
```console
sudo apt install build-essential
sudo apt -y install g++-7 g++-8 g++-9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 7
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
g++ --version
```

-------
##### unitree_legged_sdk
https://github.com/unitreerobotics/unitree_legged_sdk

```console
cd ~/catkin_ws/src
git clone https://github.com/unitreerobotics/unitree_legged_sdk.git
cd unitree_legged_sdk
mkdir build
cd build
cmake ..
make
```

#### unitree_ros_to_real
https://github.com/unitreerobotics/unitree_ros_to_real


```console
cd ~/catkin_ws/src/
git clone https://github.com/unitreerobotics/unitree_ros_to_real.git
catkin build unitree_legged_msgs unitree_legged_real
cd ../
source devel/setup.bash
```

Since the original path for `unitree_legged_sdk` is not set correctly, you need to modify `unitree_ros_to_real/unitree_legged_real/CMakeLists.txt` as follows:
```diff_xml:unitree_ros_to_real/unitree_legged_real/CMakeLists.txt
+ set(LEGGED_SDK_DIR "${PROJECT_SOURCE_DIR}/../../unitree_legged_sdk")

include_directories(
    include
    ${catkin_INCLUDE_DIRS}
-   ${CMAKE_SOURCE_DIR}/unitree_legged_sdk-master/include
+   ${LEGGED_SDK_DIR}/include
)

- link_directories(${CMAKE_SOURCE_DIR}/unitree_legged_sdk-master/lib)
+ link_directories(${LEGGED_SDK_DIR}/lib)
```

If you are working on v3.8.0 of unitree_legged_sdk, change the file as follows:
```diff_xml:unitree_ros_to_real/unitree_legged_real/CMakeLists.txt
- set(LEGGED_SDK_NAME -pthread libunitree_legged_sdk_${ARCH}.so lcm)
+ set(LEGGED_SDK_NAME -pthread libunitree_legged_sdk.so lcm)

set(EXTRA_LIBS ${LEGGED_SDK_NAME} lcm)

set(CMAKE_CXX_FLAGS "-03 0fPIC")

+ set(LEGGED_SDK_DIR "${PROJECT_SOURCE_DIR}/../../unitree_legged_sdk")

include_directories(
    include
    ${catkin_INCLUDE_DIRS}
-   ${CMAKE_SOURCE_DIR}/unitree_legged_sdk-master/include
+   ${LEGGED_SDK_DIR}/include
)

- link_directories(${CMAKE_SOURCE_DIR}/unitree_legged_sdk-master/lib)
+ link_directories(${LEGGED_SDK_DIR}/lib/cpp/${ARCH})
```
If you use python, replace `cpp` with `python`.

Note that unitree_legged_real is not required for the simulation, and so you can skip its compiling if not necessary.

-------
#### unitree_ros
```console
cd ~/catkin_ws/src
git clone https://github.com/unitreerobotics/unitree_ros.git
```

```console
sudo apt update
sudo apt upgrade
sudo apt install ros-melodic-controller-interface  ros-melodic-gazebo-ros-control ros-melodic-joint-state-controller ros-melodic-effort-controllers ros-melodic-joint-trajectory-controller
```

Then, modify the last line in `unitree_ros/unitree_gazebo/worlds/stairs.world`
```diff_xml:unitree_ros/unitree_gazebo/worlds/stairs.world
<include>
- <uri>model:///home/unitree/catkin_ws/src/unitree_ros/unitree_gazebo/worlds/building_editor_models/stairs</uri>
+ <uri>model:///home/ubuntu/catkin_ws/src/unitree_ros/unitree_gazebo/worlds/building_editor_models/stairs</uri>
</include>
```

Finally, build packages.
```console
catkin build
source devel/setup.bash
```


:::note warn
You may get an error message indicating `unitree_joint_control_tool` cannot be included. 
In that case, try to modify the last line in `unitree_ros/unitree_legged_control/CMakeLists.txt` to specify another one formatted as `elf64-littleaarch64` instead of the default one, `lib/libunitree_joint_control_tool.so`, whose file format is `elf64-little` as:
:::
```diff_xml:unitree_ros/unitree_legged_control/CMakeLists.txt
- target_link_libraries(unitree_legged_control ${catkin_LIBRARIES} unitree_joint_control_tool)
+ target_link_libraries(unitree_legged_control ${catkin_LIBRARIES} unitree_joint_control_tool_arm64)
```


:::note warn
You may get errors saying `eeForce` does not exist when compiling `unitree_ros/unitree_controller/src/servo.cpp`.
In that case, comment out the lines including "eeForce" since it is obsolete for `unitree_ros_msgs`. (02.06.2022)
:::