Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*Copyright 2021 Arunava Basu, Shon Byron Cortes,
Anubhav Paras & Charu Sharma */
Anubhav Paras & Charu Sharma */

#include <iostream>
#include <memory>
#include <PID.h>

using std::cout;
using std::cin;
Expand All @@ -14,5 +16,23 @@ using std::endl;
* @return int
*/
int main() {
// To do pair 2
// To do pair 2 - Done
std::unique_ptr<PID> 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;
}
6 changes: 6 additions & 0 deletions build_coverage_off.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rm -rf build/
mkdir build
cd build
cmake ..
make
test/cpp-test
8 changes: 8 additions & 0 deletions build_with_coverage.sh
Original file line number Diff line number Diff line change
@@ -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

61 changes: 33 additions & 28 deletions include/PID.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*Copyright 2021 Arunava Basu, Shon Byron Cortes,
Anubhav Paras & Charu Sharma */
Anubhav Paras & Charu Sharma */
#pragma once
#include <iostream>

Expand All @@ -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;
};
25 changes: 21 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -34,13 +39,25 @@ The activity diagram is as follows :
*Fig 2 : Activity diagram*

### Steps to build

```
cd ...<path_to_directory>/
mkdir build
cd build
cmake ..
make

```
### Steps to run
```
...<path_to_directory>/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
```
Empty file.
8 changes: 8 additions & 0 deletions results/cppcheck_result_part2.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions results/cpplint_result_part2.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions run_cppcheck.sh
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions run_cpplint.sh
Original file line number Diff line number Diff line change
@@ -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"
41 changes: 36 additions & 5 deletions src/PID.cpp
Original file line number Diff line number Diff line change
@@ -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 <algorithm>

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
Expand All @@ -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;
}