{"id":18574745,"url":"https://github.com/maxritter/sdc-model-predictive-control","last_synced_at":"2025-05-16T00:11:37.621Z","repository":{"id":149103103,"uuid":"94639484","full_name":"maxritter/SDC-Model-Predictive-Control","owner":"maxritter","description":"Implementation of a MPC algorithm to drive a car around a track in a simulated environment","archived":false,"fork":false,"pushed_at":"2017-06-17T17:55:56.000Z","size":1459,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-17T14:49:37.304Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxritter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-17T17:38:43.000Z","updated_at":"2017-06-17T17:51:42.000Z","dependencies_parsed_at":"2024-03-27T22:00:45.846Z","dependency_job_id":null,"html_url":"https://github.com/maxritter/SDC-Model-Predictive-Control","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxritter%2FSDC-Model-Predictive-Control","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxritter%2FSDC-Model-Predictive-Control/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxritter%2FSDC-Model-Predictive-Control/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxritter%2FSDC-Model-Predictive-Control/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxritter","download_url":"https://codeload.github.com/maxritter/SDC-Model-Predictive-Control/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254442856,"owners_count":22071878,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T23:16:14.724Z","updated_at":"2025-05-16T00:11:37.537Z","avatar_url":"https://github.com/maxritter.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"**Model Predictive Control** \n\nIn this project, a Model Predictive Control (MPC) algorithm is implemented to control the actuators of a car and drive it around a simulated track. \n\nThis control algorithm is far more advanced than a classic PID controller, because it can look N timesteps in the future by using the physical model and also use a lot of constraints for accurate steering and acceleration control. \n\nAdditionally, there's a 100 millisecond latency between actuations commands on top of the connection latency to account for real-time delay in actuations.\n\n\u003cimg src=\"./images/screen.PNG\" width=\"350\"\u003e\n\r\n----------\n\n**The model**\n\nThis project uses a kinematic bycicle model originally described in [this paper](http://www.me.berkeley.edu/~frborrel/pdfpub/IV_KinematicMPC_jason.pdf). Kinematic models are simplifications of dynamic models that ignore tire forces, gravity, and mass. At low and moderate speeds, kinematic models often approximate the actual vehicle dynamics.\n\nThe state of the vehicle can be represented as `x-position`, `y-position`, orientation `psi` and the velocity `v`. In addition to that, we also include the two errors `CTE` (cross-track-eror) and `epsi` (orientation error) into the  state vector:\n\n\u003cimg src=\"./images/state.PNG\" width=\"150\"\u003e\n\nWe can control the state of the vehicle over two actuators. `Delta` controls the steering angle, whereas `alpha` is used for acceleration (throttle \u0026 brake):\n\n\u003cimg src=\"./images/actuators.PNG\" width=\"44\"\u003e\n\nConcerning the actuators, constraints have to be made. First, the vehicle can't move in arbitrary directions. Therefore, the steering angle is limited to +- 25 degrees. Second, the gas and braking should be in defined boundaries. In our case, we use -1 for maximum brake, and +1 for maximum gas.\n\nOur kinematic model can be described by the following equations. They take into account the state \u0026 actuator variables, the timestep dt, and the distance between the front of the vehicle and its center of gravity Lf:\n\n\u003cimg src=\"./images/equations.png\" width=\"300\"\u003e\n\n**Timestep Length and Elapsed Duration**\n\nA value of 10 for the number for the timestep length N seems to be an optimal tradeoff between computational complexity and the ability to look ahead far enough. Slightly smaller (8) and larger (up to 15) values should also work well.\n\nFor the elapsed duration dt, I chose a dt of 0.1s (100ms). Higher values might not work well, because the environment might change too fast for the controller to give accurate results. A lower value drives computational costs unnecessarily high and therefore makes no sense for this simulation. I also changed the maximium CPU time for the IpOpt optimizer to 100ms, so it will deliver a result in this maximum time barrier.\n\nThe total calculated time can be calculated by N multiplied by dt, which gives a value of one second. This means that MPC predicts the set of states for the next second. When the car is driving at high speed, this seems like a good value. Of course, in real-environments, it could make sense to increase N to get a higher timeframe for prediction.\n\n**Polynomial Fitting and MPC Preprocessing**\n\nThe waypoints for the reference trajectory line are transformed to the car's perspective first. This makes all the following computations much easier. The waypoints are now centered around the car's x- and y-position and the orientation angle is zero. \n\nAfterwards, the method `polyfit` is used to fit those transformed waypoints to a 3rd order polynomial function. Given the coefficients and the x values, `polyeval` calculates the y positions. With several of those points, the yellow reference trajectory line is drawn inside the simulator.\n\n**Model Predictive Control with Latency**\n\nWhen applying the inputs to the actuators, there is a so called latency, which results from the time the actuators need to apply the commands. In this project, a time delay of 100ms (implemented as sleep in the main function) should take care of this. \n\nAfter trying several methods, I found a simple solution to account for the latency: My MPC returns a sum of the actuations for the next timestep and the following timestep afterwards. This way, the model applies also the next actuations and therefore handles the latency of 100ms. The described method is the case for both the steering angle `delta` and the acceleration `alpha`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxritter%2Fsdc-model-predictive-control","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxritter%2Fsdc-model-predictive-control","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxritter%2Fsdc-model-predictive-control/lists"}