2024-03-14 11:36:49 +08:00
|
|
|
|
# rl_sar
|
2024-03-07 11:54:33 +08:00
|
|
|
|
|
|
|
|
|
[English document](README.md)
|
|
|
|
|
|
2024-05-24 19:24:04 +08:00
|
|
|
|
机器人强化学习算法的仿真验证与实物部署,适配四足机器人、轮足机器人、人形机器人。"sar"代表"simulation and real"
|
2024-03-14 11:36:49 +08:00
|
|
|
|
|
2024-08-09 18:50:04 +08:00
|
|
|
|
本框架支持基于IaacGym的legged_gym,也支持基于IsaacSim的IsaacLab,用`framework`加以区分。
|
2024-08-09 17:55:01 +08:00
|
|
|
|
|
2024-06-01 11:57:49 +08:00
|
|
|
|
[点击在Discord上讨论](https://discord.gg/MC9KguQHtt)
|
|
|
|
|
|
2024-03-07 11:54:33 +08:00
|
|
|
|
## 准备
|
|
|
|
|
|
2024-05-23 20:55:35 +08:00
|
|
|
|
拉取代码
|
2024-03-07 11:54:33 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2024-05-23 20:55:35 +08:00
|
|
|
|
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
|
|
|
|
## 依赖
|
|
|
|
|
|
2024-05-25 08:54:13 +08:00
|
|
|
|
本项目依赖ROS-Noetic(Ubuntu20.04)
|
|
|
|
|
|
|
|
|
|
安装好ros之后安装依赖库
|
|
|
|
|
|
|
|
|
|
```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
|
|
|
|
在任意位置下载并部署`libtorch`
|
|
|
|
|
|
|
|
|
|
```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 21:37:25 +08:00
|
|
|
|
安装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
|
|
|
|
安装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
|
|
|
|
## 编译
|
|
|
|
|
|
|
|
|
|
自定义代码中的以下两个函数,以适配不同的模型:
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
torch::Tensor forward() override;
|
|
|
|
|
torch::Tensor compute_observation() override;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
然后到根目录编译
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
cd ..
|
|
|
|
|
catkin build
|
|
|
|
|
```
|
|
|
|
|
|
2024-08-15 23:14:48 +08:00
|
|
|
|
如果 catkin build 报错: `Unable to find either executable 'empy' or Python module 'em'`, 在`catkin build` 之前执行 `catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3`
|
|
|
|
|
|
2024-03-07 11:54:33 +08:00
|
|
|
|
## 运行
|
|
|
|
|
|
2024-04-05 22:03:00 +08:00
|
|
|
|
运行前请将训练好的pt模型文件拷贝到`rl_sar/src/rl_sar/models/YOUR_ROBOT_NAME`中,并配置`config.yaml`中的参数。
|
2024-03-14 11:36:49 +08:00
|
|
|
|
|
|
|
|
|
### 仿真
|
2024-03-07 11:54:33 +08:00
|
|
|
|
|
2024-06-29 11:38:07 +08:00
|
|
|
|
打开一个终端,启动gazebo仿真环境
|
2024-03-07 11:54:33 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
source devel/setup.bash
|
2024-05-25 17:39:22 +08:00
|
|
|
|
roslaunch rl_sar gazebo_<ROBOT>.launch
|
2024-03-07 11:54:33 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-06-29 11:38:07 +08:00
|
|
|
|
打开一个新终端,启动控制程序
|
|
|
|
|
|
|
|
|
|
```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
|
|
|
|
其中 \<ROBOT\> 可以是 `a1` 或 `a1_isaaclab` 或 `gr1t1` 或 `gr1t2`.
|
2024-05-25 17:39:22 +08:00
|
|
|
|
|
2024-06-12 16:01:17 +08:00
|
|
|
|
控制:
|
2024-03-07 11:54:33 +08:00
|
|
|
|
|
2024-06-12 16:01:17 +08:00
|
|
|
|
* 按 **\<Enter\>** 切换仿真器运行/停止。
|
|
|
|
|
* **W** 和 **S** 控制x轴,**A** 和 **D** 控制yaw轴,**J** 和 **L** 控制y轴,按下空格重置控制指令。
|
|
|
|
|
* 按 **\<Space\>** 将所有控制指令设置为零。
|
|
|
|
|
* 如果机器人摔倒,按 **R** 重置Gazebo环境。
|
2024-05-25 17:55:57 +08:00
|
|
|
|
|
2024-08-15 23:14:48 +08:00
|
|
|
|
### 真实机器人
|
2024-03-07 11:54:33 +08:00
|
|
|
|
|
2024-08-15 23:14:48 +08:00
|
|
|
|
**示例:Unitree A1**
|
2024-03-29 19:36:58 +08:00
|
|
|
|
|
2024-04-08 23:25:05 +08:00
|
|
|
|
与Unitree A1连接可以使用无线与有线两种方式
|
2024-03-18 14:53:31 +08:00
|
|
|
|
|
|
|
|
|
* 无线:连接机器人发出的Unitree开头的WIFI **(注意:无线连接可能会出现丢包断联甚至失控,请注意安全)**
|
|
|
|
|
* 有线:用网线连接计算机和机器人的任意网口,配置计算机ip为192.168.123.162,网关255.255.255.0
|
|
|
|
|
|
2024-03-14 11:36:49 +08:00
|
|
|
|
新建终端,启动控制程序
|
2024-03-07 11:54:33 +08:00
|
|
|
|
|
2024-03-14 11:36:49 +08:00
|
|
|
|
```bash
|
|
|
|
|
source devel/setup.bash
|
2024-04-08 23:25:05 +08:00
|
|
|
|
rosrun rl_sar rl_real_a1
|
2024-03-14 11:36:49 +08:00
|
|
|
|
```
|
2024-03-14 00:18:32 +08:00
|
|
|
|
|
2024-03-18 14:53:31 +08:00
|
|
|
|
按下遥控器的**R2**键让机器人切换到默认站起姿态,按下**R1**键切换到RL控制模式,任意状态按下**L2**切换到最初的趴下姿态。左摇杆上下控制x左右控制yaw,右摇杆左右控制y。
|
2024-03-22 00:16:13 +08:00
|
|
|
|
|
2024-07-28 22:03:10 +08:00
|
|
|
|
或者按下键盘上的**0**键让机器人切换到默认站起姿态,按下**P**键切换到RL控制模式,任意状态按下**1**键切换到最初的趴下姿态。WS控制x,AD控制yaw,JL控制y。
|
|
|
|
|
|
|
|
|
|
### 训练执行器网络
|
|
|
|
|
|
|
|
|
|
1. 取消注释`rl_real.cpp`中最上面的`#define CSV_LOGGER`,你也可以在仿真程序中修改对应部分采集仿真数据用来测试训练过程。
|
|
|
|
|
2. 运行控制程序,程序会在执行后记录所有数据。
|
|
|
|
|
3. 停止控制程序,开始训练执行器网络。注意,下面的路径前均省略了`rl_sar/src/rl_sar/models/`。
|
|
|
|
|
```bash
|
|
|
|
|
rosrun rl_sar actuator_net.py --mode train --data a1/motor.csv --output a1/motor.pt
|
|
|
|
|
```
|
|
|
|
|
4. 验证已经训练好的训练执行器网络。
|
|
|
|
|
```bash
|
|
|
|
|
rosrun rl_sar actuator_net.py --mode play --data a1/motor.csv --output a1/motor.pt
|
|
|
|
|
```
|
2024-03-29 19:36:58 +08:00
|
|
|
|
|
2024-04-28 16:51:46 +08:00
|
|
|
|
## 添加你的机器人
|
|
|
|
|
|
2024-08-15 23:14:48 +08:00
|
|
|
|
下文中将`<ROBOT>`代表机器人名称
|
2024-04-28 16:51:46 +08:00
|
|
|
|
|
2024-08-15 23:14:48 +08:00
|
|
|
|
1. 在`rl_sar/src/robots`路径下创建名为`<ROBOT>_description`的模型包,将模型的urdf放到`rl_sar/src/robots/<ROBOT>_description/urdf`路径下并命名为`<ROBOT>.urdf`,并在`rl_sar/src/robots/<ROBOT>_description/config`路径下创建命名空间为`<ROBOT>_gazebo`的关节配置文件
|
|
|
|
|
2. 将训练好的RL模型文件放到`rl_sar/src/rl_sar/models/<ROBOT>`路径下
|
|
|
|
|
3. 在`rl_sar/src/rl_sar/models/<ROBOT>`中新建config.yaml文件,参考`rl_sar/src/rl_sar/models/a1_isaacgym/config.yaml`文件修改其中参数
|
|
|
|
|
4. 若需要运行仿真,则参考`rl_sar/src/rl_sar/launch`路径下的launch文件自行修改
|
|
|
|
|
5. 若需要运行实物,则参考`rl_sar/src/rl_sar/src/rl_real_a1.cpp`文件自行修改
|
2024-05-23 20:55:35 +08:00
|
|
|
|
|
|
|
|
|
## 参考
|
|
|
|
|
|
|
|
|
|
[unitree_ros](https://github.com/unitreerobotics/unitree_ros)
|
2024-04-28 16:51:46 +08:00
|
|
|
|
|
2024-03-22 00:16:13 +08:00
|
|
|
|
## 引用
|
|
|
|
|
|
|
|
|
|
如果您使用此代码或其部分内容,请引用以下内容:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
@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}
|
|
|
|
|
}
|
|
|
|
|
```
|