{"id":21204537,"url":"https://github.com/maudzung/self-driving-car-08-pid-control-cpp","last_synced_at":"2026-08-03T00:02:57.356Z","repository":{"id":108978051,"uuid":"264690309","full_name":"maudzung/Self-Driving-Car-08-PID-Control-CPP","owner":"maudzung","description":"An implementation of a PID controller that determines the steering angle in order to keep a car in the center of the lane track during driving.","archived":false,"fork":false,"pushed_at":"2020-05-19T06:49:16.000Z","size":51668,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T22:46:29.079Z","etag":null,"topics":["pid-control","pid-controller","self-driving-car"],"latest_commit_sha":null,"homepage":"","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/maudzung.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":"2020-05-17T14:52:08.000Z","updated_at":"2024-09-23T14:29:59.000Z","dependencies_parsed_at":"2023-06-13T02:30:59.648Z","dependency_job_id":null,"html_url":"https://github.com/maudzung/Self-Driving-Car-08-PID-Control-CPP","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/maudzung/Self-Driving-Car-08-PID-Control-CPP","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maudzung%2FSelf-Driving-Car-08-PID-Control-CPP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maudzung%2FSelf-Driving-Car-08-PID-Control-CPP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maudzung%2FSelf-Driving-Car-08-PID-Control-CPP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maudzung%2FSelf-Driving-Car-08-PID-Control-CPP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maudzung","download_url":"https://codeload.github.com/maudzung/Self-Driving-Car-08-PID-Control-CPP/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maudzung%2FSelf-Driving-Car-08-PID-Control-CPP/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279006756,"owners_count":26084176,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["pid-control","pid-controller","self-driving-car"],"created_at":"2024-11-20T20:36:26.322Z","updated_at":"2025-10-11T09:05:50.502Z","avatar_url":"https://github.com/maudzung.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PID Controls\n\n[![CarND](https://s3.amazonaws.com/udacity-sdc/github/shield-carnd.svg)](https://www.udacity.com/course/self-driving-car-engineer-nanodegree--nd013)\n\nThe goal of this project is to implement a PID controller that determines the steering angle in order to \nkeep a car in the center of the lane track during driving.\n\n---\n## Demostration\n![demo](./demo.gif)\n\nThe full demostration is at [https://youtu.be/jeNe3Oa1ZVo](https://youtu.be/jeNe3Oa1ZVo)\n\n## Terminologies\n- **Cross Track Error (CTE)**: The distance from the car to the trajectory\n- **PID**: Proportional-Integral-Derivative\n- **P** component: The steering angle is set to proportion of CTE with a proportional factor tau_p.\n    ```\n    steer_angle = - tau_p * cte\n    ```\n- **D** component: The differential component of the controller which helps to take temporal derivative of error. \nWhen the car turned enough to reduce the error, it will help not to overshoot the road.\n    ```\n    diff_cte = cte - prev_cte\n    prev_cte = cte\n    steer_angle += - tau_d * diff_cte\n    ```\n- **I** component: The sum of cte overtime to minimize the average CTE.\n    ```\n    sum_cte += cte\n    steer_angle += tau_i * sum_cte\n    ```\n- The **PID** controller controls the steering value.\n    ```\n    cte = robot.y\n    diff_cte = cte - prev_cte\n    prev_cte = cte\n    sum_cte += cte\n    steer = -tau_p * cte - tau_d * diff_cte - tau_i * sum_cte\n    ```\n\n## PID Fine-Tune\n### 1. Manually fine-tune parameters for Kp, Ki, Kd \n- **Step 1**: I set parameters to zeros. Obviously, the car drives straight. \n- **Step 2**: I increased the Kp of the P component with an increment of 0.01 until the car start going on following the road \nand drives with constant oscillations.\n- **Step 3**: I increased the Kd of the D component with an increment of 0.1 to try to reduce oscillations. \u003cbr\u003e\n *Loop the 2nd step and the 3rd step.*\n- **Step 4**: When the car drives the track with really small oscillations and without going out of the road, \nI increased the Ki of the I component to minimize the average CTE.\n\nFinally, I ended up with the parameters: ```Kp = 0.15, Ki = 0.0001, Kd = 1.0```\n\n### 2. Applying **Twiddle** algorithm to optimize the parameters\nI also implemented the Twiddle algorithm to optimize the parameters. \nI'll update the results of the algorithm later.\n\n## Code Style\n\n[Google's C++ style guide](https://google.github.io/styleguide/cppguide.html).\n* indent using spaces\n* set tab width to 2 spaces (keeps the matrices in source code aligned)\n\n## Source code structure\nThe directory structure of this repository is as follows:\n\n```shell script\n${ROOT}\n├──build.sh\n├──clean.sh\n├──CMakeLists.txt\n├──README.md\n├──run.sh\n├──src/\n    ├──json.hpp\n    ├──main.cpp\n    ├──PID.cpp\n    ├──PID.h\n```\n\n## Dependencies\n* cmake \u003e= 3.5\n* make \u003e= 4.1\n* gcc/g++ \u003e= 5.4\n* [uWebSockets](https://github.com/uWebSockets/uWebSockets)\n\n## How to compile and run\n1. Download the Term 2 Simulator [here](https://github.com/udacity/self-driving-car-sim/releases).\n2. Install `uWebSocketIO`: \u003cbr\u003e\nThis repository includes two files that can be used to set up and install [uWebSocketIO](https://github.com/uWebSockets/uWebSockets) \nfor either Linux or Mac systems. For windows you can use either Docker, VMware, \nor even [Windows 10 Bash on Ubuntu](https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/)\u003cbr\u003e\nYou can execute the `install-ubuntu.sh` to install uWebSocketIO.\n3. Once the install for uWebSocketIO is complete, the main program can be built and ran by doing the following from the project top directory.\n\n```shell script\nmkdir build\ncd build\ncmake ..\nmake\n./pid\n```\n\n## References\n1. [PID controller](https://www.wikiwand.com/en/PID_controller#/overview)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaudzung%2Fself-driving-car-08-pid-control-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaudzung%2Fself-driving-car-08-pid-control-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaudzung%2Fself-driving-car-08-pid-control-cpp/lists"}