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; } 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 + diff --git a/include/PID.h b/include/PID.h index 47a9bc14..cb48972d 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,39 @@ 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; -} + /** + * @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: - 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; }; 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 +``` 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..a3592749 --- /dev/null +++ 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 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 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; +}