RoboMaster Hero: Embedded Control for a Swerve-Drive Robot
Overview
In RoboMaster, the hero is the heavy hitter of a team: the only robot that fires 42 mm projectiles, the ones that do the most damage to outposts and bases. Our hero drives on a swerve (steering-wheel) chassis and carries a two-axis gimbal with a friction-wheel launcher. I developed its electrical control firmware: everything between the remote controller and the motors, on a single STM32H723.
The firmware reads the referee system and the image-link remote, estimates the gimbal attitude from the IMU, turns stick and mouse commands into motor commands for the chassis, the gimbal and the launcher, and talks to an onboard Orin Nano for auto-aiming. Every control loop runs from its own hardware-timer interrupt, so its rate stays fixed.
Firmware Architecture
Each layer only calls the one below it, so a new motor or sensor only touches the device layer.
| Task | Rate | Timer | What it does |
|---|---|---|---|
gimbal_task() | 1000 Hz | TIM5 | IMU update, yaw and pitch control |
chore_task() | 200 Hz | TIM2 | referee UI, super capacitor |
chassis_task() | 125 Hz | TIM15 | swerve chassis control |
ammo_task() | 125 Hz | TIM12 | friction wheels and trigger |
How It Works
Swerve chassis
The driver steers in the gimbal frame, so the command \((v_x, v_y)\) is first rotated by the yaw difference \(\Delta\psi\) between gimbal and chassis. Each of the four modules sits on a diagonal, so the spin command \(v_r\) adds a velocity perpendicular to it:
\[ \mathbf{v}_c = R(\Delta\psi)\begin{bmatrix} v_x \\ v_y \end{bmatrix}, \qquad \mathbf{v}_i = \mathbf{v}_c + \tfrac{v_r}{\sqrt{2}} \begin{bmatrix} \pm 1 \\ \pm 1 \end{bmatrix}, \qquad \theta_i = \operatorname{atan2}(v_{i,y}, v_{i,x}), \quad s_i = \|\mathbf{v}_i\|. \]A module never turns by more than 90°: if the target angle is further away, it steers to the opposite angle and reverses the wheel. While it is still turning, the wheel speed is scaled by the cosine of the remaining angle error, so the wheels do not fight each other and skid.
Attitude estimation: quaternions and a Mahony filter
The gimbal holds its aim in the world frame, so it needs its own attitude at 1 kHz. I derived the quaternion kinematics and wrote a Mahony filter from scratch for the BMI088 IMU. The accelerometer gives the direction of gravity; the cross product with the gravity direction predicted by the current quaternion is the attitude error, which corrects the gyro rate through a PI term:
\[ \mathbf{e} = \hat{\mathbf{a}} \times \hat{\mathbf{g}}(q), \qquad \tilde{\boldsymbol\omega} = \boldsymbol\omega + k_p\,\mathbf{e} + k_i \!\int\! \mathbf{e}\,dt, \qquad \dot q = \tfrac12\, q \otimes \begin{bmatrix} 0 \\ \tilde{\boldsymbol\omega} \end{bmatrix}. \]The quaternion is integrated with an Euler step and renormalized every millisecond; the integral term is clamped against wind-up.
Gimbal and launcher
- Yaw: a cascade loop on a DM6006 motor. An outer PID turns the yaw error into a rate command and an inner loop tracks it, both fed by the IMU, so the gimbal keeps pointing the same way while the chassis spins underneath.
- Pitch: a Xiaomi motor in position mode, limited to the mechanical elevation and depression range.
- Launcher: two stages of three friction wheels ramp up and down gradually. While the trigger is held, the feeder advances five projectiles per second, and it stops as soon as the referee system reports that the barrel heat limit is reached.
- Safety: in safe mode every motor gets a zero command and the setpoints follow the measured state, so the robot does not jump when control is switched back on.
Tooling
The project builds with a Makefile and the Arm GNU toolchain, and flashes through CMSIS-DAP. For tuning, SEGGER RTT streams variables to VOFA+ for live plots without a spare UART.
Code
The full firmware (application, algorithm, device and BSP layers) is on GitHub.