Awesome model_predictive_control created by AdityaNG
Project description
model_predictive_control
Python implementation of MPC solver
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
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for model_predictive_control-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92156b1804278a848f26f778dbd908bf75b9947d9483c3970d2239823c37df55 |
|
MD5 | 8ca9890016b6985c01f31d1f0d4269ef |
|
BLAKE2b-256 | eb99d417ff3f4b3ca67433bfcbde7d9646c5c68bca48e392bd65084dca741860 |
Close
Hashes for model_predictive_control-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d7e6a1e23660415d382c87c2759ae3e08cadd13dcc6e438e21bc2fc834d9c70 |
|
MD5 | 50bbc1fb91af5da3a14f51f096d0fe35 |
|
BLAKE2b-256 | 03b6c970ccd86a691bbb6a3e46630d67150661a36c2fb98afb580b59668c77cf |