GithubHelp home page GithubHelp logo

nsmoly / mpc Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 39 KB

Model Predictive Control (MPC) for kinematic bicycle model

License: MIT License

Python 100.00%
bicycle-model kinematics mpc mpc-control optimization pytorch linear-regression nsmoly

mpc's Introduction

MPC

Model Predictive Control (MPC) for kinematic bicycle model

This code example demonstrates the use of PyTorch as an optimizer for Model Predictive Control. The program takes a reference trajectory and computes controls for kinematic bicycle model that allow it to follow this trajectory. Optimization is done as a loss minimization via PyTorch by using gradient descent (via AdamW optimizer).

Bicycle model is defined as:

# Compute speed
friction = self.speed[i]*self.friction_road + self.friction_air*self.speed[i]*self.speed[i]
self.speed[i+1] = torch.clamp(self.speed[i] + self.dt*(self.accel[i] - friction), 0, self.max_speed)

# Clamp steering control and compute current angular velocity
steering_angle = torch.clamp(self.steering[i], -self.max_steer, self.max_steer)
angular_velocity = self.speed[i]*torch.tan(steering_angle)/self.wheelbase

self.x[i+1] = self.x[i] + self.speed[i]*torch.cos(self.yaw[i])*self.dt
self.y[i+1] = self.y[i] + self.speed[i]*torch.sin(self.yaw[i])*self.dt
self.yaw[i+1] = self.yaw[i] + angular_velocity*self.dt

The input to the optimizer is the reference trajectory: Reference trajectory to optimize control for

The optimizer computes model controls (acceleration and streering) such that the realized trajectory (via bicycle model) minimizes L2 loss. This figure shows the input reference trajectory in blue and computed/optimized trajectory (by optimizing controls) in red: Optimized trajectory (in red) vs reference trajectory (in blue)

This figure shows resulting L2/MSE loss: Loss

mpc's People

Contributors

nsmoly avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

lxjlu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.