generated from Avi-73/Avi_73J_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00771c4
commit 17624e8
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef PID_H | ||
#define PID_H | ||
|
||
#include <Arduino.h> | ||
|
||
class PID{ | ||
private: | ||
double Kp, Ki, Kd; | ||
double previousRadian = 0; | ||
double error = 0; | ||
double integral = 0; | ||
double targetRadian = 0; | ||
const double dt = 0.001; | ||
const int volt = 5; | ||
double inputVoltage = 0; | ||
|
||
public: | ||
PID(double p, double i, double d); | ||
double calculateIPD(double radian); | ||
void setTarget(double target) {targetRadian = target;} | ||
double getVoltage() {return inputVoltage;} | ||
void printParameter(); | ||
|
||
}; | ||
|
||
PID::PID(double proportionalGain, double integralGain, double derivativeGain){ | ||
Kp = proportionalGain; | ||
Ki = integralGain; | ||
Kd = derivativeGain; | ||
} | ||
double PID::calculateIPD(double radian){ | ||
error = targetRadian - radian; | ||
integral += error*dt; | ||
inputVoltage = -Kp*radian + Ki*integral - Kd*(radian-previousRadian)/dt; | ||
previousRadian = radian; | ||
if(inputVoltage > volt) {inputVoltage = volt; integral -= error*dt;} | ||
else if(inputVoltage < -volt) {inputVoltage = -volt; integral -= error*dt;} | ||
return inputVoltage; | ||
} | ||
void PID::printParameter(){ | ||
Serial.printf("Kp: %f, Ki: %f, Kd: %f, previousRadian: %f, error: %f, integral %f\n", Kp, Ki, Kd, previousRadian, error, integral); | ||
} | ||
#endif |