Giter Club home page Giter Club logo

racinglmpc's Introduction

Learning Model Predictive Control (LMPC) for autonomous racing

The Learning Model Predictive Control (LMPC) is a data-driven control framework developed at UCB in the MPC lab. In this example, we implemented the LMPC for the autonomous racing problem. The controller drives several laps on race track and it learns from experience how to drive faster.

In the above animation we see the vehicle's closed-loop trajectory (in black) for laps 5, 30, 31 and 32. At each time instant the LMPC leverages forecast to plan the vehicle trajectory (in red) few seconds into the future. This trajectory is planned to minimize the lap time, but it is constrained to land into the safe set (in green). This safe set is the domain of the approximation to the value function and it is updated after each lap using historical data.

Prerequisites

The packeges needed for running the code can be installed using pip

pip install cvxopt
pip install osqp
pip install pathos

Description

The Plant

The vehicle is modelled using the dynamics signle track bicycle model and the tire forces are modelled using the Pacejka formula.

The Path Following

  1. Lap 1: a PID path following controller is used to drive the vehicle around the track.
  2. Lap 2: the data from lap 1 are used to estimate a LTI model used to design a MPC for path following
  3. Lap 3: the data from lap 1 are used to estimate a LTV model used to design a MPC for path following

References

This code is based on the following:

  • Ugo Rosolia and Francesco Borrelli. "Learning Model Predictive Control for Iterative Tasks. A Data-Driven Control Framework." In IEEE Transactions on Automatic Control (2017). PDF
  • Ugo Rosolia and Francesco Borrelli. "Learning how to autonomously race a car: a predictive control approach." IEEE Transactions on Control Systems Technology (2019) PDF.
  • Ugo Rosolia and Francesco Borrelli. "Learning Model Predictive Control for Iterative Tasks: A Computationally Efficient Approach for Linear System." IFAC-PapersOnLine 50.1 (2017). PDF

racinglmpc's People

Contributors

junzengx14 avatar sarahxdean avatar urosolia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

racinglmpc's Issues

UnicodeDecodeError

Starting PID
Traceback (most recent call last):
File "/home_net/malici/Downloads/lmpcugo/RacingLMPC/src/main.py", line 107, in
ClosedLoopDataPID = pickle.load(file_data)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x9e in position 2: ordinal not in range(128)

Runtime error in LMPC

Hi Ugo,

I faced with the following error:

  File "main.py", line 165, in <module>
    LMPCSimulator.Sim(ClosedLoopLMPC, LMPController, LMPCOpenLoopData)
  File "fnc/SysModel.py", line 37, in Sim
    Controller.solve(x[i, :])
  File "fnc/LMPC.py", line 96, in solve
    Qfun_SelectedTot    =  np.append(Qfun_SelectedTot, Qfun_Selected, axis=1)
  File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 4575, in append
    return concatenate((arr, values), axis=axis)
IndexError: axis 1 out of bounds [0, 1)

It seems that you should use axis=0 instead of axis=1 in line 96 of LMPC.py

Best.
Alex

Error with pickle line 107

(base) C:\dev\Projects_Work\RacingLMPC\src>python main.py
Starting PID
Traceback (most recent call last):
File "main.py", line 107, in
ClosedLoopDataPID = pickle.load(file_data)
ModuleNotFoundError: No module named 'Classes\r'

Typo in comment?

In PathfollowingLTIMPC.py -> should be as below:

# Build matrices for optimization (Convention from http://www.mpc.berkeley.edu/mpc-course-material, Optimal Control Handout, page 4-43, construction of a QP without sub)
# We are going to build our optimization vector z \in \mathbb{R}^((N+1) \dot n + N \dot d), note that this vector
# stacks the predicted trajectory x_{k|t} \forall k = t, \ldots, t+N+1 over the horizon and
# the predicted input u_{k|t} \forall k = t, \ldots, t+N over the horizon

A possible error coding in LTVMPC.py

Hi Rosolia, I am Hao, now studying autonomous racing vehicle in UCB. Your LMPC is amazing while I found a possible error in the fnc/PathFollowingLTVMPC.py, line 279, where you linearize the epsi/ey.
depsi_ey = - dt * (vx * np.cos(epsi) - vy * np.sin(epsi)) / (den**2) * cur * (-cur)
However, according to the kinetic model, which you write in the "Learning how to autonomously race a car: a predictive control approach"
f1
I think this equation should be minus rather than positive based on fractional derivative. So I try to change this equation and finally found the simulator runs faster.(6.0s before, 5.8s after).
I am not quite sure if there is a minor error, but I hope this comment can help you. In all, thank you for sharing the LMPC. If you don't mind, I want to implement the LMPC in my project in UCB.

AttributeError: 'OSQP' object has no attribute 'constant'

Starting PID
===== PID terminated
Starting MPC
===== MPC terminated
Starting TV-MPC
===== TV-MPC terminated
Starting LMPC
Traceback (most recent call last):
File "main.py", line 173, in
LMPCSimulator.Sim(ClosedLoopLMPC, LMPController, LMPCOpenLoopData)
File "fnc/SysModel.py", line 37, in Sim
Controller.solve(x[i, :])
File "fnc/LMPC.py", line 117, in solve
res_cons, feasible = osqp_solve_qp(sparse.csr_matrix(M), q, sparse.csr_matrix(F), b, sparse.csr_matrix(G), np.add(np.dot(E,x0),L[:,0]))
File "fnc/LMPC.py", line 426, in osqp_solve_qp
if res.info.status_val != osqp.constant('OSQP_SOLVED'):
AttributeError: 'OSQP' object has no attribute 'constant'

QP formulation and LMPC Relaxation in the paper

Hi Ugo,

I was trying to implement your LMPC on the F1/10 autonomous car but had some trouble understanding the LMPC Relaxation part in your paper.
image
I'm a bit confused about why the time index is up to 4N. By doing this relaxation are you effectively trying to convert the problem into a QP?
I would highly appreciate if you could give any more insights regarding the idea behind the relaxation. Thank you!

Yuwei

New Track

Hi,
i tried the following new map:

    self.slack = 0.45
    self.halfWidth = 0.8
    self.lengthcurve = 4
    spec = np.array([[3/2, 0],
                     [self.lengthcurve/ 2 , self.lengthcurve /np.pi],
                     [self.lengthcurve / np.pi * (3.5), 0],
                     [self.lengthcurve /2,self.lengthcurve / np.pi],
                     [4/2, 0],
                     [self.lengthcurve  , self.lengthcurve / np.pi],
                     #[1/2 + 0.1, 0],
                     [self.lengthcurve, - self.lengthcurve / np.pi],
                     #[1/2, 0],
                     [self.lengthcurve / 2 *3, - 3* self.lengthcurve / np.pi],
                     [self.lengthcurve, self.lengthcurve / np.pi],
                     #[1-0.04647909, 0],
                     [self.lengthcurve/ 2 * 4.5, 4.5 * self.lengthcurve / np.pi]])

Figure_23

But then it fails in the first iteration of LMPC with the raise:
Starting PID
===== PID terminated
Starting MPC
===== MPC terminated
Starting TV-MPC
===== TV-MPC terminated
Starting LMPC
Traceback (most recent call last):
File "C:\Users\jonas\AppData\Local\Programs\Python\Python39\lib\site-packages\cvxopt\misc.py", line 1429, in factor
lapack.potrf(F['S'])
ArithmeticError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\jonas\AppData\Local\Programs\Python\Python39\lib\site-packages\cvxopt\coneprog.py", line 2010, in coneqp
try: f3 = kktsolver({'d': matrix(0.0, (0,1)), 'di':
File "C:\Users\jonas\AppData\Local\Programs\Python\Python39\lib\site-packages\cvxopt\coneprog.py", line 1981, in kktsolver
return factor(W, P)
File "C:\Users\jonas\AppData\Local\Programs\Python\Python39\lib\site-packages\cvxopt\misc.py", line 1444, in factor
lapack.potrf(F['S'])
ArithmeticError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\jonas\PycharmProjects\RacingLMPC-master(alt)\src\main.py", line 179, in
main()
File "C:\Users\jonas\PycharmProjects\RacingLMPC-master(alt)\src\main.py", line 116, in main
xTVMPC_cl, uTVMPC_cl, xTVMPC_cl_glob, _ = simulator.sim(xS, mpc)
File "C:\Users\jonas\PycharmProjects\RacingLMPC-master(alt)\src\fnc/simulator\SysModel.py", line 34, in sim
Controller.solve(x_cl[-1])
File "C:\Users\jonas\PycharmProjects\RacingLMPC-master(alt)\src\fnc/controller\PredictiveControllers.py", line 117, in solve
self.computeLTVdynamics()
File "C:\Users\jonas\PycharmProjects\RacingLMPC-master(alt)\src\fnc/controller\PredictiveControllers.py", line 144, in computeLTVdynamics
Ai, Bi, Ci = self.predictiveModel.regressionAndLinearization(self.xLin[i], self.uLin[i])
File "C:\Users\jonas\PycharmProjects\RacingLMPC-master(alt)\src\fnc/controller\PredictiveModel.py", line 70, in regressionAndLinearization
Ai[yIndex, self.stateFeatures], Bi[yIndex, self.inputFeaturesVx], Ci[yIndex] = self.LMPC_LocLinReg(Q_vx, b_vx, self.inputFeaturesVx)
File "C:\Users\jonas\PycharmProjects\RacingLMPC-master(alt)\src\fnc/controller\PredictiveModel.py", line 172, in LMPC_LocLinReg
res_cons = qp(Q, b) # This is ordered as [A B C]
File "C:\Users\jonas\AppData\Local\Programs\Python\Python39\lib\site-packages\cvxopt\coneprog.py", line 4485, in qp
return coneqp(P, q, G, h, None, A, b, initvals, kktsolver = kktsolver, options = options)
File "C:\Users\jonas\AppData\Local\Programs\Python\Python39\lib\site-packages\cvxopt\coneprog.py", line 2013, in coneqp
raise ValueError("Rank(A) < p or Rank([P; A; G]) < n")
ValueError: Rank(A) < p or Rank([P; A; G]) < n

Does someone have an idea why this map is not working correctly?

Regarding the problem of the car exceeding the road boundary

Hello,I am studying your code at [https://github.com/urosolia/RacingLMPC].

But I had some questions when I was running your program.

I use a judgment condition to judge whether the state variable exceeds the boundary constraint in 'SysModels.py' when a new state variable is generated :
x[i + 1, :], x_glob[i + 1, :] = _DynModel(x[i, :], x_glob[i, :], u[i, :], np, ClosedLoopData.dt, self.map.PointAndTangent) if x[i+1,5]>=self.map.halfWidth: print 'Lateral distance',x[i+1,5], 'Road boundary',self.map.halfWidth

But when I ran the program, I found that your program goes beyond the road boundary sometimes :

Lateral distance 0.4191460527916872 Road boundary 0.4 Lateral distance 0.4324614096754442 Road boundary 0.4 Lateral distance 0.4138255604138284 Road boundary 0.4

Therefore, I am more curious about whether there is an error margin when the road width constraint is exceeded.

Or is there any way to keep it strictly within the road width constraint?

In addition, I noticed that the position of the car is expressed in the form of mass points. Therefore, when the particle of the car is located at the edge of the road width constraint, what about the body part of the car that exceeds the road width constraint?

I will be very grateful if you can answer my questions.

Thanks!

Input state from camera or map with GPS ?

Hi Ugo,

Thank for sharing this code. It really helps to understand the paper. I saw your video that demonstrates the use of this algorithm with a BARC car. I'm wondering wethere you're using the camera as input state to control the car or do you have the map of the track as with the simulator here. If the demo uses a map, how the car is get localized during the race ?

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.