rl_sar/README.md

164 lines
5.9 KiB
Markdown
Raw Normal View History

2024-03-14 12:42:00 +08:00
# rl_sar
2024-03-06 17:32:15 +08:00
2024-03-18 14:54:46 +08:00
[中文文档](README_CN.md)
2024-03-14 12:42:00 +08:00
2024-05-25 08:54:13 +08:00
Simulation verification and physical deployment of robot reinforcement learning algorithms, suitable for quadruped robots, wheeled robots, and humanoid robots. "sar" stands for "simulation and real"
2024-03-06 17:32:15 +08:00
2024-08-09 18:50:04 +08:00
This framework supports legged_gym based on IaacGym and IsaacLab based on IsaacSim. Use `framework` to distinguish.
2024-08-09 17:55:01 +08:00
2024-06-01 23:01:53 +08:00
[Click to discuss on Discord](https://discord.gg/vmVjkhVugU)
2024-06-01 11:57:49 +08:00
2024-03-07 11:54:33 +08:00
## Preparation
Clone the code
2024-03-07 11:54:33 +08:00
```bash
git clone https://github.com/fan-ziqi/rl_sar.git
2024-03-07 11:54:33 +08:00
```
2024-04-05 22:03:00 +08:00
## Dependency
2024-05-25 08:54:13 +08:00
This project relies on ROS Noetic (Ubuntu 20.04)
After installing ROS, install the dependency library
```bash
sudo apt install ros-noetic-teleop-twist-keyboard ros-noetic-controller-interface ros-noetic-gazebo-ros-control ros-noetic-joint-state-controller ros-noetic-effort-controllers ros-noetic-joint-trajectory-controller
```
2024-03-07 11:54:33 +08:00
Download and deploy `libtorch` at any location
2024-03-06 17:32:15 +08:00
```bash
cd /path/to/your/torchlib
wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.0.1%2Bcpu.zip
unzip libtorch-cxx11-abi-shared-with-deps-2.0.1+cpu.zip -d ./
echo 'export Torch_DIR=/path/to/your/torchlib' >> ~/.bashrc
```
2024-04-05 22:03:00 +08:00
Install yaml-cpp
```bash
git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp && mkdir build && cd build
cmake -DYAML_BUILD_SHARED_LIBS=on .. && make
sudo make install
sudo ldconfig
```
2024-04-17 15:20:19 +08:00
Install lcm
```bash
git clone https://github.com/lcm-proj/lcm.git
cd lcm && mkdir build && cd build
cmake .. && make
sudo make install
sudo ldconfig
```
2024-03-07 11:54:33 +08:00
## Compilation
2024-03-06 17:32:15 +08:00
2024-03-14 12:42:00 +08:00
Customize the following two functions in your code to adapt to different models:
2024-03-06 17:32:15 +08:00
```cpp
torch::Tensor forward() override;
torch::Tensor compute_observation() override;
```
2024-03-07 11:54:33 +08:00
Then compile in the root directory
2024-03-06 17:32:15 +08:00
```bash
cd ..
catkin build
```
If catkin build report errors: `Unable to find either executable 'empy' or Python module 'em'`, run `catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3` before `catkin build`
2024-03-14 12:42:00 +08:00
## Running
2024-04-05 22:03:00 +08:00
Before running, copy the trained pt model file to `rl_sar/src/rl_sar/models/YOUR_ROBOT_NAME`, and configure the parameters in `config.yaml`.
2024-03-06 17:32:15 +08:00
2024-03-14 12:42:00 +08:00
### Simulation
2024-03-06 17:32:15 +08:00
2024-06-29 11:38:07 +08:00
Open a terminal, launch the gazebo simulation environment
2024-03-06 17:32:15 +08:00
```bash
source devel/setup.bash
2024-05-25 17:39:22 +08:00
roslaunch rl_sar gazebo_<ROBOT>.launch
2024-03-06 17:32:15 +08:00
```
2024-06-29 11:38:07 +08:00
Open a new terminal, launch the control program
```bash
source devel/setup.bash
(for cpp version) rosrun rl_sar rl_sim
(for python version) rosrun rl_sar rl_sim.py
```
2024-08-09 17:55:01 +08:00
Where \<ROBOT\> can be `a1` or `a1_isaaclab` or `gr1t1` or `gr1t2`.
2024-05-25 17:39:22 +08:00
2024-06-12 16:01:17 +08:00
Control:
* Press **\<Enter\>** to toggle simulation start/stop.
* **W** and **S** controls x-axis, **A** and **D** controls yaw, and **J** and **L** controls y-axis.
* Press **\<Space\>** to sets all control commands to zero.
* If robot falls down, press **R** to reset Gazebo environment.
2024-05-25 17:55:57 +08:00
### Real Robots
2024-03-14 12:42:00 +08:00
**Example: Unitree A1**
2024-03-18 14:53:31 +08:00
2024-04-08 23:25:05 +08:00
Unitree A1 can be connected using both wireless and wired methods:
2024-03-18 14:53:31 +08:00
2024-04-08 23:25:05 +08:00
* Wireless: Connect to the Unitree starting with WIFI broadcasted by the robot **(Note: Wireless connection may lead to packet loss, disconnection, or even loss of control, please ensure safety)**
* Wired: Use an Ethernet cable to connect any port on the computer and the robot, configure the computer IP as 192.168.123.162, and the gateway as 255.255.255.0
Open a new terminal and start the control program
2024-03-06 17:32:15 +08:00
2024-03-14 12:42:00 +08:00
```bash
source devel/setup.bash
2024-04-08 23:25:05 +08:00
rosrun rl_sar rl_real_a1
2024-03-14 12:42:00 +08:00
```
2024-03-06 17:32:15 +08:00
2024-04-08 23:25:05 +08:00
Press the **R2** button on the controller to switch the robot to the default standing position, press **R1** to switch to RL control mode, and press **L2** in any state to switch to the initial lying position. The left stick controls x-axis up and down, controls yaw left and right, and the right stick controls y-axis left and right.
2024-07-28 22:03:10 +08:00
Or press **0** on the keyboard to switch the robot to the default standing position, press **P** to switch to RL control mode, and press **1** in any state to switch to the initial lying position. WS controls x-axis, AD controls yaw, and JL controls y-axis.
### Train the actuator network
1. Uncomment `#define CSV_LOGGER` in the top of `rl_real.cpp`. You can also modify the corresponding part in the simulation program to collect simulation data for testing the training process.
2. Run the control program, and the program will log all data after execution.
3. Stop the control program and start training the actuator network. Note that `rl_sar/src/rl_sar/models/` is omitted before the following paths.
```bash
rosrun rl_sar actuator_net.py --mode train --data a1/motor.csv --output a1/motor.pt
```
4. Verify the trained actuator network.
```bash
rosrun rl_sar actuator_net.py --mode play --data a1/motor.csv --output a1/motor.pt
```
2024-03-22 00:16:13 +08:00
## Add Your Robot
In the following text, `<ROBOT>` represents the name of the robot
1. Create a model package named `<ROBOT>_description` in the `rl_sar/src/robots` directory. Place the robot's URDF file in the `rl_sar/src/robots/<ROBOT>_description/urdf` directory and name it `<ROBOT>.urdf`. Additionally, create a joint configuration file with the namespace `<ROBOT>_gazebo` in the `rl_sar/src/robots/<ROBOT>_description/config` directory.
2. Place the trained RL model files in the `rl_sar/src/rl_sar/models/<ROBOT>` directory.
3. In the `rl_sar/src/rl_sar/models/<ROBOT>` directory, create a `config.yaml` file, and modify its parameters based on the `rl_sar/src/rl_sar/models/a1_isaacgym/config.yaml` file.
4. If you need to run simulations, modify the launch files as needed by referring to those in the `rl_sar/src/rl_sar/launch` directory.
5. If you need to run on the physical robot, modify the file `rl_sar/src/rl_sar/src/rl_real_a1.cpp` as needed.
## Reference
[unitree_ros](https://github.com/unitreerobotics/unitree_ros)
2024-03-22 00:16:13 +08:00
## Citation
Please cite the following if you use this code or parts of it:
```
@software{fan-ziqi2024rl_sar,
author = {fan-ziqi},
2024-05-25 08:54:13 +08:00
title = {{rl_sar: Simulation Verification and Physical Deployment of Robot Reinforcement Learning Algorithm.}},
2024-03-22 00:16:13 +08:00
url = {https://github.com/fan-ziqi/rl_sar},
year = {2024}
}
2024-06-01 23:01:53 +08:00
```