From 0f9cbbd745e9436cfec7b17b245748553eee89c6 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 15:45:33 -0400 Subject: [PATCH 1/9] Added scripts for building the project --- build_coverage_off.sh | 6 ++++++ build_with_coverage.sh | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 build_coverage_off.sh create mode 100644 build_with_coverage.sh diff --git a/build_coverage_off.sh b/build_coverage_off.sh new file mode 100644 index 00000000..60c25c55 --- /dev/null +++ b/build_coverage_off.sh @@ -0,0 +1,6 @@ +rm -rf build/ +mkdir build +cd build +cmake .. +make +test/cpp-test \ No newline at end of file diff --git a/build_with_coverage.sh b/build_with_coverage.sh new file mode 100644 index 00000000..b9d6bb2f --- /dev/null +++ b/build_with_coverage.sh @@ -0,0 +1,8 @@ +rm -rf build/ +mkdir build +cd build +cmake -DCOVERAGE=ON -DCMAKE_BUILD_TYPE=Debug ../ +make +make code_coverage +test/cpp-test + From 8a1d3ef8b8ca644561775e53619eafc7a8e5b181 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 15:46:24 -0400 Subject: [PATCH 2/9] Moved the implementation to PID.cpp --- include/PID.h | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/include/PID.h b/include/PID.h index 47a9bc14..feb4e74e 100644 --- a/include/PID.h +++ b/include/PID.h @@ -1,5 +1,5 @@ /*Copyright 2021 Arunava Basu, Shon Byron Cortes, -Anubhav Paras & Charu Sharma */ + Anubhav Paras & Charu Sharma */ #pragma once #include @@ -8,34 +8,24 @@ using std::cin; class PID { public: - double calculate(double target_velocity, double present_velocity); + /** + * @brief Construct a new PID object + * + * @param Kp Proportional gain + * @param Ki Integral gain + * @param Kd Derivitive gain + */ + PID(double Kp, double Ki, double Kd); - /** - * @brief Construct a new PID object - * - * @param Kp Proportional gain - * @param Ki Integral gain - * @param Kd Derivitive gain - */ - PID(double Kp, double Ki, double Kd) { - /** Initialize kp, ki, kd to their respective values. Time step set to 0.1. - previous error, min velocity and integral are 0, max velocity is 100. - - */ - - // To do pair 2 - _Kp; - _Ki; - _Kd; -} + double calculate(double target_velocity, double present_velocity); private: - double _Kp; - double _Ki; - double _Kd; - double _dt; - double _previous_error; - double _integral; - double _max_velocity; - double _min_velocity; + double _Kp; + double _Ki; + double _Kd; + double _dt; + double _previous_error; + double _integral; + double _max_velocity; + double _min_velocity; }; From 74e071696d0d94a7f684e5bd3a60f8f182643437 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 15:47:24 -0400 Subject: [PATCH 3/9] Added the implementations for calculate and initialization in constructor --- src/PID.cpp | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/PID.cpp b/src/PID.cpp index f713b02b..3326ff14 100644 --- a/src/PID.cpp +++ b/src/PID.cpp @@ -1,9 +1,27 @@ /*Copyright 2021 Arunava Basu, Shon Byron Cortes, -Anubhav Paras & Charu Sharma */ + Anubhav Paras & Charu Sharma */ // #pragma once #include "PID.h" +#include +PID::PID(double Kp, double Ki, double Kd) { + /** Initialize kp, ki, kd to their respective values. Time step set to 0.1. + previous error, min velocity and integral are 0, max velocity is 100. + + */ + + // To do pair 2 - Done + _dt = 0.1; + _previous_error = 0; + _integral = 0; + _min_velocity = 0; + _max_velocity = 100; + + _Kp = Kp; + _Ki = Ki; + _Kd = Kd; +} /** * @brief A function which computes the PID controller output value. target_voltage is used to store the setpoint * present_voltage is used to store the present voltage value @@ -18,10 +36,23 @@ Anubhav Paras & Charu Sharma */ * @param target_velocity Desired final velocity * @param present_velocity Current velocity * @return double Final velocity calculated by PID controller - */ + */ double PID::calculate(double target_velocity, double present_velocity) { -// To do pair 2 -return 999; -} + // To do pair 2 - Done + + double error = target_velocity - present_velocity; + double p_term = _Kp * error; + _integral = _integral + (error * _dt); + double i_term = _Ki * _integral; + + double d_term = _Kd * (error - _previous_error) / _dt; + + double output = p_term + i_term + d_term; + + _previous_error = error; + + output = std::max(_min_velocity, std::min(_max_velocity, output)); + return output; +} From 77fa68017814533c09c49cd7951906fafd0fbc2c Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 15:47:49 -0400 Subject: [PATCH 4/9] Added implentation in main.cpp --- app/main.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index d7dfee73..3f11055b 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -1,7 +1,9 @@ /*Copyright 2021 Arunava Basu, Shon Byron Cortes, -Anubhav Paras & Charu Sharma */ + Anubhav Paras & Charu Sharma */ #include +#include +#include using std::cout; using std::cin; @@ -14,5 +16,23 @@ using std::endl; * @return int */ int main() { -// To do pair 2 + // To do pair 2 - Done + std::unique_ptr pid_controller(new PID(0.1, 0.01, 0.5)); + double velocity = 10; + double final_vel = 20; + + std::cout << "Initial velocity= " << velocity << std::endl; + std::cout << "Final velocity= " << final_vel << std::endl; + + std::cout << "PID Gains- " << std::endl; + std::cout << "Kp = 0.1" << std::endl; + std::cout << "Ki = 0.01" << std::endl; + std::cout << "Kd = 0.5" << std::endl; + + for (int i = 0; i < 100; i++) { + double output = pid_controller->calculate(final_vel, velocity); + velocity = velocity + output; + } + + std::cout << "Velocity after 100 iterations= " << velocity << std::endl; } From 39855a42e7fc861d1c0e20ad51639c3243e72140 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 16:11:09 -0400 Subject: [PATCH 5/9] Added files to run cppcheck and cpplint --- run_cppcheck.sh | 2 ++ run_cpplint.sh | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 run_cppcheck.sh create mode 100644 run_cpplint.sh diff --git a/run_cppcheck.sh b/run_cppcheck.sh new file mode 100644 index 00000000..6668d4d0 --- /dev/null +++ b/run_cppcheck.sh @@ -0,0 +1,2 @@ +cppcheck --enable=all --std=c++11 -I include/ --suppress=missingIncludeSystem $( find . -name *.cpp | grep -vE -e "^./build/" -e "^./vendor/") --output-file=results/cppcheck_process_part2.txt > results/cppcheck_result_part2.txt +echo "Done Processing. Results are stored in results/cppcheck_process.txt, results/cppcheck_result.txt" \ No newline at end of file diff --git a/run_cpplint.sh b/run_cpplint.sh new file mode 100644 index 00000000..689e902e --- /dev/null +++ b/run_cpplint.sh @@ -0,0 +1,2 @@ +cpplint $( find . -name *.cpp | grep -vE -e "^./build/" -e "^./vendor/") $( find . -name *.hpp | grep -vE -e "^./build/" -e "^./vendor/") > results/cpplint_result_part2.txt +echo "Done Processing. Results are stored in results/cpplint_result.txt" \ No newline at end of file From bc734d6d36d243312122a85c16702091efe2f587 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 16:11:33 -0400 Subject: [PATCH 6/9] Results of cppcheck and cpplint --- results/cppcheck_process_part2.txt | 0 results/cppcheck_result_part2.txt | 8 ++++++++ results/cpplint_result_part2.txt | 0 3 files changed, 8 insertions(+) create mode 100644 results/cppcheck_process_part2.txt create mode 100644 results/cppcheck_result_part2.txt create mode 100644 results/cpplint_result_part2.txt diff --git a/results/cppcheck_process_part2.txt b/results/cppcheck_process_part2.txt new file mode 100644 index 00000000..e69de29b diff --git a/results/cppcheck_result_part2.txt b/results/cppcheck_result_part2.txt new file mode 100644 index 00000000..9ebe931e --- /dev/null +++ b/results/cppcheck_result_part2.txt @@ -0,0 +1,8 @@ +Checking app/main.cpp ... +1/4 files checked 28% done +Checking src/PID.cpp ... +2/4 files checked 79% done +Checking test/main.cpp ... +3/4 files checked 85% done +Checking test/test_PID.cpp ... +4/4 files checked 100% done diff --git a/results/cpplint_result_part2.txt b/results/cpplint_result_part2.txt new file mode 100644 index 00000000..e69de29b From 44b278f46379f8821e43df92e1894c880ce85cb8 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 16:11:50 -0400 Subject: [PATCH 7/9] Updated readme --- readme.md | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 1f5d7ae3..f21098aa 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,12 @@ This repository pertains to the test driven development exercise. The group memb ## Part 1 The members of the first pair for Part 1 of this repository are : - Arunava Basu (Driver) - - Shon Cortes (Navigator + - Shon Cortes (Navigator) + + ## Part 2 +The members of the first pair for Part 1 of this repository are : + - Charu Sharma (Driver) + - Anubhav Paras (Navigator) ## Description The design contains one class, PID. The main function takes the target velocity and present velocity as input and calls the calculate function of the PID class for 100 iterations. @@ -34,13 +39,25 @@ The activity diagram is as follows : *Fig 2 : Activity diagram* ### Steps to build - +``` cd .../ mkdir build cd build cmake .. make - +``` ### Steps to run +``` .../app/shell-app - +``` + +### Steps to run cppcheck and cpplint +Run cppcheck: Results are stored in `./results/cppcheck_process_part2.txt`, `./results/cppcheck_result_part2.txt` +``` +sh run_cppcheck.sh +``` + +Run cpplint: Results are stored in `./results/cpplint_result_part2.txt` +``` +sh run_cpplint.sh +``` From cc5b751bfd1489cc7400ef8bf342dfa4ae6843f3 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 16:24:49 -0400 Subject: [PATCH 8/9] Added results for cpplint --- results/cpplint_result_part2.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/results/cpplint_result_part2.txt b/results/cpplint_result_part2.txt index e69de29b..a3592749 100644 --- a/results/cpplint_result_part2.txt +++ b/results/cpplint_result_part2.txt @@ -0,0 +1,4 @@ +Done processing ./app/main.cpp +Done processing ./src/PID.cpp +Done processing ./test/main.cpp +Done processing ./test/test_PID.cpp From 168143abd3464e929900024219813d1cfd6c4693 Mon Sep 17 00:00:00 2001 From: Charu Date: Sun, 3 Oct 2021 16:25:15 -0400 Subject: [PATCH 9/9] Added Doxygen for calculate --- include/PID.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/PID.h b/include/PID.h index feb4e74e..cb48972d 100644 --- a/include/PID.h +++ b/include/PID.h @@ -17,6 +17,21 @@ class PID { */ PID(double Kp, double Ki, double Kd); + /** + * @brief A function which computes the PID controller output value. target_voltage is used to store the setpoint + * present_voltage is used to store the present voltage value + * Steps to calculate output : + * 1) error is the difference between the target and the present voltage + * 2) The proportional term is Kp times the error + * 3) The error is multiplied with the time step dt and added to the integral variable + * 4) The integral term is Ki times the integral variable + * 5) The derivate term is Kd times the difference in present error and previous error divided by the time step + * 6) Total output is the bounded (withing min and max) sum of the proportional, integral, and derivate term + * + * @param target_velocity Desired final velocity + * @param present_velocity Current velocity + * @return double Final velocity calculated by PID controller + */ double calculate(double target_velocity, double present_velocity); private: