TinyMPC: Model-Predictive Control on Resource-Constrained Microcontrollers
TinyMPC: Model-Predictive Control on Resource-Constrained Microcontrollers
Resource-Constrained Microcontrollers
Khai Nguyen*, Sam Schoedel*, Anoushka Alavilli*, Brian Plancher, Zachary Manchester
I. I NTRODUCTION
Model-predictive control (MPC) enables reactive and dy-
namic online control for robots while respecting complex
control and state constraints such as those encountered during
dynamic obstacle avoidance and contact events [1], [2], [3],
[4]. However, despite MPC’s many successes, its practical
application is often hindered by computational limitations,
which can necessitate algorithmic simplifications [5], [6].
Fig. 1. TinyMPC is a fast convex model-predictive control solver that
This challenge is amplified when dealing with systems that enables real-time optimal control on resource-constrained microcontrollers.
have fast or unstable open-loop dynamics, where high control We demonstrate its efficacy in dynamic obstacle avoidance (top) and
rates are needed for safe and effective operation. recovery from 90◦ attitude errors (bottom) on a 27 gram Crazyflie quadrotor.
At the same time, there has been an explosion of interest
in tiny, low-cost robots that can operate in confined spaces, ligent robot behaviors executed on these tiny platforms rely
making them a promising solution for applications ranging on off-board computers [14], [15], [16], [17], [18], [19], [20].
from emergency search and rescue [7] to routine monitoring Several efficient optimization solvers suitable for embed-
and maintenance of infrastructure and equipment [8], [9]. ded MPC [29], [30] have emerged in recent years, most
These robots are limited to low-power, resource-constrained notably OSQP [31] and CVXGEN [32]. Both of these
microcontrollers (MCUs) for their computation [10], [11]. solvers have code-generation tools that enable users to create
As shown in Figure 2, these microcontrollers feature orders dependency-free C code to solve quadratic programs (QPs)
of magnitude less RAM, flash memory, and processor speed on embedded computers. However, they do not take full
compared to the CPUs and GPUs available on larger robots advantage of the unique structure of the MPC problem,
and historically were not able to support the real-time execu- generally making them too memory intensive and too com-
tion of computationally or memory-intensive algorithms [12], putationally demanding to run within the resource constraints
[13]. Consequently, many examples in the literature of intel- of many microcontrollers.
On the other hand, the recent success of “TinyML”
∗These authors contributed equally. has enabled the deployment of neural networks on micro-
Khai Nguyen is with the Department of Mechanical Engineering,
Carnegie Mellon University, 5000 Forbes Ave., Pittsburgh, PA 15213. controllers [12]. Motivated by these results, we developed
[email protected] TinyMPC, an MCU-optimized implementation of convex
Samuel Schoedel and Zachary Manchester are with the Robotics Institute, MPC using the alternating direction method of multipliers
Carnegie Mellon University, 5000 Forbes Ave., Pittsburgh, PA 15213.
{sschoede, zmanches}@andrew.cmu.edu (ADMM) algorithm. At its core, our solver is designed to
Anoushka Alavilli is with the Department of Electrical & Computer accelerate and compress the ADMM algorithm by exploiting
Engineering, Carnegie Mellon University, 5000 Forbes Ave., Pittsburgh, PA the structure of the MPC problem.
15213. [email protected]
Brian Plancher is with Barnard College, Columbia University, 3009 In particular, we precompute and cache expensive matrix
Broadway New York, NY 10027. [email protected] factorizations, allowing TinyMPC to completely avoid online
Fig. 2. A comparison of micro, tiny, and full-scale robot platforms and their associated computational hardware. At the smallest scale, microrobots like
the Robobee [21] and HAMR-F [22] use highly constrained 8-bit microcontrollers to execute pre-planned open-loop gaits or wing motions. At large scales,
powerful embedded CPUs and GPUs, found onboard the Snapdragon Flight quadrotor [23] or Unitree Go1edu quadruped [24], enable high performance
at the cost of high power requirements. In this work we target tiny robots such as the Crazyflie2.1 [25], DeepPiCarMicro [26], PIXHAWK PX4 [27], and
Petoi Bittle [28] that leverage 32-bit microcontrollers for motion planning and control. These devices are capable of some onboard computation, but feature
orders of magnitude less processor speed, RAM, and flash memory than full-scale robots.
division and matrix inversion. This approach enables rapid dynamics, Q ⪰ 0, R ≻ 0, and Qf ⪰ 0 are symmetric cost
computation with a very small memory footprint, enabling matrices and q and r are linear cost vectors.
deployment on resource-constrained MCUs. To the best of Equation (1) has a closed-form solution in the form of an
the authors’ knowledge, TinyMPC is the first MPC solver affine feedback controller [33]:
tailored for execution on these MCUs that has been demon-
strated onboard a highly dynamic, compute-limited robotic u∗k = −Kk xk − dk . (2)
system. Our contributions include: The feedback gain Kk and feedforward dk are found by solv-
• A novel quadratic-programming algorithm that is opti- ing the discrete Riccati equation backward in time, starting
mized for MPC, is matrix-inversion free, and achieves from PN = Qf , pN = qf , where Pk , pk are the quadratic
high efficiency and a very low memory footprint. and linear terms of the cost-to-go (or value) function [33]:
This combination makes it suitable for deployment on Kk = (R + B ⊺ Pk+1 B)−1 (B ⊺ Pk+1 A)
resource-constrained microcontrollers.
• An open-source implementation of TinyMPC in C++ dk = (R + B ⊺ Pk+1 B)−1 (B ⊺ pk+1 + rk )
that delivers state-of-the-art real-time performance for Pk = Q + Kk⊺ RKk + (A − BKk )⊺ Pk+1 (A − BKk ) (3)
convex MPC problems on microcontrollers. pk = qk + (A − BKk )⊺ (pk+1 − Pk+1 Bdk )+
• Experimental demonstrations on a small, agile, Kk⊺ (Rdk − rk ).
resource-constrained quadrotor platform.
B. Convex Model-Predictive Control
This paper proceeds as follows: Section II reviews linear-
quadratic optimal control, convex optimization, and ADMM. Convex MPC extends the LQR formulation to admit
Section III then derives the core TinyMPC solver algo- additional convex constraints on the system states and control
rithm. Benchmarking results and hardware experiments on inputs, such as joint and torque limits, hyperplanes for
a Crazyflie quadrotor are presented in Section IV. Finally, obstacle avoidance, and contact constraints:
we summarize our results and conclusions in Section V. min J(x1:N , u1:N −1 )
x1:N ,u1:N −1
If we alternate minimization over x and z, rather than subject to xk+1 = Axk + Buk ,
simultaneously minimizing over both, we arrive at the three- where
step ADMM iteration, Q̃f = Qf + ρI, q̃f = qf + λN − ρzN ,
primal update : x+ = arg min LA (x, z, λ), (10) Q̃ = Q + ρI, q̃k = qk + λk − ρzk , (15)
x
+ +
slack update : z = arg min LA (x , z, λ), (11) R̃ = R + ρI, r̃k = rk + µk − ρwk .
z
We reformulate (15) and introduce the scaled dual vari-
dual update : λ+ = λ + ρ(x+ − z + ), (12)
ables y and g for convenience [41]:
the last step of which is a gradient-ascent update on the q̃f = qf + ρ(λN /ρ − zN ) = qf + ρ(yN − zN ),
Lagrange multiplier [41]. These steps can be iterated until a
desired convergence tolerance is achieved. q̃k = qk + ρ(λk /ρ − zk ) = qk + ρ(yk − zk ), (16)
In the special case of a QP, each step of the ADMM r̃k = rk + ρ(µk /ρ − wk ) = rk + ρ(gk − wk ).
algorithm becomes very simple to compute: the primal We observe that, because (14) exhibits the same LQR prob-
update is the solution to a linear system, and the slack lem structure as in (1), it can be solved efficiently with the
update is a linear projection. ADMM-based QP solvers, like Riccati recursion in (3). The slack update for (13) becomes
OSQP [31], have demonstrated state-of-the-art results. a simple linear projection onto the feasible set:
zk+ = projX (x+
k + yk ),
III. T HE T INY MPC S OLVER (17)
wk+ = projU (u+
k + gk ),
TinyMPC trades generality for speed by exploiting the spe-
where the superscript denotes the variable at the subsequent
cial structure of the MPC problem. Specifically, we leverage
ADMM iteration. The dual update for (13) becomes:
the closed-form Riccati solution to the LQR problem to com-
pute the primal update in (10). Pre-computing and caching yk+ = yk + x+ +
k − zk ,
(18)
this solution allows us to avoid online matrix factorizations gk+ = gk + u+ +
k − wk .
and enables very fast performance while maintaining a small
memory footprint. Finally, the algorithm terminates when the primal and dual
residuals are within a set tolerance.
Time per Iteration (us)
600 600 600 TinyMPC
OSQP
400 400 400
0 0 0
0 10 20 30 0 10 20 30 0 10 20 30 40 50
600 600 600
TinyMPC
Memory Usage (kB)
Fig. 3. Comparison of average iteration times (top) and memory usage (bottom) for OSQP and TinyMPC on randomly generated trajectory tracking
problems on a Teensy 4.1 development board (ARM Cortex-M7 running at 600 MHz with 32-bit floating point support, 7.75 MB of flash, and 512 kB of
tightly coupled RAM). Error bars show the maximum and minimum time per iteration over all MPC steps executed for a given problem. In (a), the input
dimension and time horizon are held constant at m = 4 and N = 10 while the state dimension n varies from 4 to 32. In (b), n = 10 and N = 10 while
the m varies from 4 to 32. In (c), n = 10, m = 4 and N varies from 4 to 50. The dotted black line indicates the memory limit of the Teensy 4.1.
0 0.2
0
0 0.1 0.2 0.3 0.4 0.5
(a) Time [s], Brescianini
−0.5
·105
1
−1 −0.5 0 0.5 1 0.8
Controls
(a) X [m], period = 5 s 0.6
0.4
0.2
0.5 0
0 0.1 0.2 0.3 0.4 0.5
(b) Time [s], PID
Y [m]
0 ·105
1
Reference
0.8
Mellinger 0.6
Brescianini 0.4
−0.5 TinyMPC 0.2
0
−1 −0.5 0 0.5 1 0 0.1 0.2 0.3 0.4 0.5
(b) X [m], period = 3 s (c) Time [s], TinyMPC
Fig. 4. Figure-eight tracking at low speed (top) and high speed (bottom) Fig. 5. Control trajectories during the Extreme Initial Poses experiment.
comparing TinyMPC with the two most performant controllers available Four pre-clipped PWM motor commands are shown for each controller.
on the Crazyflie. For slower trajectories, all three controllers resulted in The black dotted line denotes the thrust limit, from 0 to 65535 PWM value.
similar performance. For faster trajectories, only TinyMPC was capable of Among the three successful controllers, only TinyMPC could reason about
maintaining tracking without crashing. The maximum velocity and attitude control feasibility, exhibiting the maneuver shown in Fig. 1 (bottom).
deviation from hover with TinyMPC reached 1.5 m/s and 20◦ , respectively.