File size: 4,469 Bytes
ef6a683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Standalone MuJoCo Simulator for Unitree G1

This is a standalone MuJoCo physics simulator for the Unitree G1 robot, extracted from the GR00T-WholeBodyControl repository.

## Features

- **Physics Simulation**: Runs G1 robot in MuJoCo at 500Hz (2ms timestep)
- **DDS Communication**: Uses Unitree SDK2 DDS for robot communication
- **Compatible**: Works with existing `unitree_g1.py` control code via DDS
- **Visualization**: Real-time 3D visualization of robot motion

## Directory Structure

```
mujoco_sim_g1/
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ run_sim.py               # Main launcher script
β”œβ”€β”€ config.yaml              # Simulation configuration
β”œβ”€β”€ sim/                     # Simulation modules
β”‚   β”œβ”€β”€ base_sim.py
β”‚   β”œβ”€β”€ simulator_factory.py
β”‚   β”œβ”€β”€ unitree_sdk2py_bridge.py
β”‚   └── ...
└── assets/                  # Robot models
    β”œβ”€β”€ g1_29dof_with_hand.xml
    └── meshes/*.STL
```

## Installation

### 1. Create Virtual Environment (Recommended)

```bash
cd mujoco_sim_g1
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

### 2. Install Dependencies

```bash
pip install -r requirements.txt
```

**Note**: If you encounter issues with `rclpy`, you can comment out the ROS2 imports in `sim/base_sim.py` (lines 11, 609-617) if you don't need camera publishing.

## Usage

### Basic Usage

```bash
# Activate environment
source venv/bin/activate

# Run the simulator
python run_sim.py
```

The simulator will:
1. Load the G1 robot model
2. Initialize DDS communication on domain 0
3. Open a MuJoCo visualization window
4. Start listening for motor commands via DDS

### Running with Your Robot Control Code

Once the simulator is running, you can control it from another terminal:

```bash
# In another terminal
cd /home/yope/Documents/lerobot
conda activate unitree_lerobot

# Run your existing control code
python test_locomotion_minimal.py
```

Your `unitree_g1.py` code will automatically connect to the simulator via DDS!

## Configuration

Edit `config.yaml` to customize:

- **SIMULATE_DT**: Simulation timestep (default: 0.002s = 500Hz)
- **DOMAIN_ID**: DDS domain ID (default: 0)
- **ENABLE_ONSCREEN**: Show visualization (default: true)
- **USE_JOYSTICK**: Enable gamepad control (default: false)
- **ROBOT_SCENE**: Path to MuJoCo XML scene

### PD Gains

The simulator uses the following PD gains (matching NVIDIA GR00T):

**Legs (indices 0-11):**
- Hip joints: KP=150, KD=2
- Knee joints: KP=300, KD=4
- Ankle joints: KP=40, KD=2

**Waist (indices 12-14):**
- All waist joints: KP=250, KD=5

**Arms (indices 15-28):**
- Shoulders: KP=100, KD=2-5
- Elbows/Wrists: KP=20-40, KD=1-2

## Troubleshooting

### ImportError: cannot import name 'ChannelFactoryInitialize'

```bash
pip install --upgrade unitree-sdk2py
```

### ROS2/rclpy errors

If you don't need camera publishing, edit `sim/base_sim.py`:
- Comment out line 11: `import rclpy`
- Comment out lines 609-617 (ROS2 initialization)

### Meshes not found

Make sure mesh paths in `assets/g1_29dof_with_hand.xml` are relative:
```xml
<mesh file="meshes/pelvis.STL"/>  <!-- Not absolute path -->
```

### Robot falls immediately

Check that:
1. PD gains match NVIDIA's values (see config.yaml)
2. Velocity command scaling is correct (ang_vel_scale=0.25, cmd_scale=[2.0, 2.0, 0.25])
3. Observations for missing joints are zeroed out (indices 12, 14, 20, 21, 27, 28)

## Technical Details

### Communication

The simulator publishes:
- **`rt/lowstate`**: Robot state (joint positions, velocities, IMU, etc.)
- **`rt/wirelesscontroller`**: Wireless remote controller state (if joystick enabled)

The simulator subscribes to:
- **`rt/lowcmd`**: Motor commands (position, velocity, torque, KP, KD)

### Coordinate Frames

- **World frame**: Z-up
- **Joint ordering**: 29 DOF (12 legs + 3 waist + 14 arms)
- **IMU**: Quaternion in [w, x, y, z] format

### Performance

- Simulation runs at ~500Hz (2ms timestep)
- Viewer updates at ~50Hz (20ms)
- Typical CPU usage: 20-40% on single core

## Files from GR00T-WholeBodyControl

This standalone simulator was extracted from:
- `gr00t_wbc/control/envs/g1/sim/` (simulation modules)
- `gr00t_wbc/control/robot_model/model_data/g1/` (robot model files)
- `gr00t_wbc/control/main/teleop/configs/g1_29dof_gear_wbc.yaml` (configuration)

## License

Follows the license of the original GR00T-WholeBodyControl repository.