Skip to content

henrygerardmoore/simple_model_predictive_control

Repository files navigation

simple_model_predictive_control

This is a simple rust implementation of Model Predictive Control (MPC).

It intends to provide a simple framework to define MPC as an argmin problem and also provides a solver to leverage the sequential nature of dynamics problems with a tree structure, in a similar fashion to RRT.

I created this library for a simple, pure-rust MPC implementation with no code generation.

I originally intended to use std::autodiff (check out its tracking issue if you're interested in this) with optimization-engine, but found the necessity of using an experimental compiler to be prohibitive to development.

Demonstrations

acrobot demo Acrobot

cartpole demo Cartpole

MPC tree A slice of the 4D tree showing the MPC optimization problem sampling the state space. Blue dots are states; faint red lines are the state connections; the green dot is the goal.

Examples

Try cargo run --profile heavy_optimization --example acrobot, cargo run --profile heavy_optimization --example cartpole, or cargo run --example simple_continuous to see the examples. They each produce .gifs of their outputs. Cartpole and acrobot are run in a 'realistic' manner, only using the first optimized output and having a relatively small dt (0.01 and 0.1 for cartpole and acrobot, respectively). They all achieve faster-than-realtime performance (at least when run with the heavy_optimization profile on both my laptop and desktop computers).

Benefits

simple_model_predictive_control is lightweight, quick to set up, requires no hand calculation of gradients or automatic differentiation, and is simple enough to read and understand.

It supports both simple continuous dynamics (Euler integration of an ODE) and custom discrete dynamics. Discrete dynamics functions can be used for dynamics that cannot be expressed as ODEs, using external physics simulation as the dynamics, or enforcing constraints. See the acrobot and cartpole examples for examples of how to use discrete dynamics functions for both non-ODE dynamics. You could also replace their integration functions with RK4 or an integration library of your choice.

Please feel encouraged to use this project as inspiration or a starting point for your own MPC implementations!

I plan to implement a wrapper for this as a controller in Copper along with using it for their existing cartpole example, so stay tuned for a more realistic simulated example! I will provide a link to that example from this README when it is complete.

I want to try using Numerica and optimization-engine to still have a pure rust MPC implementation but with autodiff, but that will be in a different repo.

Areas for Improvement

Cost function

Cost and dynamics functions are difficult to create and tune. The acrobot example's cost function, for instance, took quite a long time to formulate in a way that would produce the desired outcome. This is partially a drawback to MPC in general, but could be improved with tooling to support visualization of costs. Such visualization would be relatively easy to make in plotters, for example, but having it built-in would be a bonus.

Constraints

This library does not support hard constraints. Despite this, there are some workarounds. For example, for input constraints, clamping the input to the valid range works well (and is done in the cartpole and acrobot examples). Formulating constraints as high-penalty soft constraints is another way to work around this, but it obviously isn't a complete substitute for handling hard constraints.

Performance

I have not tested this library on any 'harder' problems than cartpole and acrobot, and expect that its performance would degrade greatly on sufficiently hard problems. Check out the below options for production-ready MPC implementations.

Alternatives

Rust

Check out optimization-engine for a more powerful, more complete, and more production-ready MPC implementation! It has Rust and python bindings and works with ROS.

Other

I've used MuJoCo MPC at my work, and it is great to use! The MuJoCo simulator as a whole is quite powerful and user-friendly.

License

You can use this project under either the Apache License (Version 2.0) or the MIT license.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Please feel free to open an issue or pull request! Any problems you run into are likely something I encountered while building this library. I'm always happy to provide help, and anything that's unclear likely needs better documentation.

About

A simple model predictive control library

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Languages