Skip to main content

model_predictive_control

codecov CI GitHub License PyPI - Version PyPI - Downloads

Python implementation of MPC solver

demo

Install it from PyPI

pip install model_predictive_control

Usage

import numpy as np

from model_predictive_control.cost.trajectory2d_steering_penalty import (
    Traj2DSteeringPenalty,
)
from model_predictive_control.models.bicycle import (
    BicycleModel,
    BicycleModelParams,
)
from model_predictive_control.mpc import MPC

# Initialize the Bicycle Model
params = BicycleModelParams(
    time_step=time_step,
    steering_ratio=13.27,
    wheel_base=2.83972,
    speed_kp=1.0,
    speed_ki=0.1,
    speed_kd=0.05,
    throttle_min=-1.0,
    throttle_max=1.0,
    throttle_gain=5.0,  # Max throttle corresponds to 5m/s^2
)
bicycle_model = BicycleModel(params)

# Define the cost function
cost = Traj2DSteeringPenalty(model=bicycle_model)

# Initialize MPC Controller
horizon = 20
state_dim = 4  # (x, y, theta, velocity)
controls_dim = 2  # (steering_angle, velocity)

mpc = MPC(
    model=bicycle_model,
    cost=cost,
    horizon=horizon,
    state_dim=state_dim,
    controls_dim=controls_dim,
)

# Define initial state (x, y, theta, velocity)
start_state = [0.0, 0.0, 0.0, 1.0]

# Define desired trajectory: moving in a straight line
desired_state_sequence = [[i * 1.0, i * 0.5, 0.0, 1.0] for i in range(horizon)]

# Initial control sequence: assuming zero steering and constant speed
initial_control_sequence = [[0.0, 1.0] for _ in range(horizon)]

# Define control bounds: steering_angle between -0.5 and 0.5 radians,
# velocity between 0.0 and 2.0 m/s
bounds = [[(-np.deg2rad(400), np.deg2rad(400)), (-1.0, 1.0)] for _ in range(horizon)]

# Optimize control inputs using MPC
optimized_control_sequence = mpc.step(
    start_state_tuple=start_state,
    desired_state_sequence=desired_state_sequence,
    initial_control_sequence=initial_control_sequence,
    bounds=bounds,
    max_iters=50,
)

Run the demo with the following

$ python -m model_predictive_control
#or
$ model_predictive_control

Cite

This work was a part of the D³Nav paper. Cite our work if you find it useful

@article{NG2024D3Nav,
  title={D³Nav: Data-Driven Driving Agents for Autonomous Vehicles in Unstructured Traffic},
  author={Aditya NG and Gowri Srinivas},
  journal={The 35th British Machine Vision Conference (BMVC)},
  year={2024},
  url={https://bmvc2024.org/}
}

Development

Read the CONTRIBUTING.md file.

TODO

  • Bicycle Model
  • Drone Model
  • MPC
  • Visualizer Demo
  • MPC Auto-Optimizer: Takes a set of expected vehicle trajectories and the search space of hyperparamters and returns the list of optimal hyperparameters
  • MPC Compiler: Takes the MPC model with a set of expected vehicle trajectories and produces numpy array a mapping from trajectory to control signals. This can be used with a cosine similarity logic to decide on control logic in real time.

Metadata

Release files for model-predictive-control 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for model-predictive-control 0.4.0
File Size Uploaded Provenance Actions
model_predictive_control-0.4.0.tar.gz 18.9 kB Without provenance Details for model_predictive_control-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for model-predictive-control 0.4.0
File Interpreter ABI Platform Actions
model_predictive_control-0.4.0-py3-none-any.whl Python 3 none any Details for model_predictive_control-0.4.0-py3-none-any.whl

Total release size: 37.7 kB

Release files / model_predictive_control-0.4.0.tar.gz

Download URL model_predictive_control-0.4.0.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5d812faf790f7df54c4308914f30d4757809d6de0a131712fc817942508a8236
BLAKE2b-256 checksum
How to use checksums
2dd7bc2ff7874919a90b709a09d4e15d6edbab6582878d8c8517e79dda37efc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / model_predictive_control-0.4.0-py3-none-any.whl

Download URL model_predictive_control-0.4.0-py3-none-any.whl
Size 18.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e4b5514684a2ea2a1270e7617b0f2cfa12aa63b3041ede965110c6bd968259b0
BLAKE2b-256 checksum
How to use checksums
caf50057d3708f50299551bff0e211100e3ebf3448f4f9d766cf0c70b196d26f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Security for model-predictive-control 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page