#ifndef PID_H
#define PID_H
-#define WINDOW_SIZE 500
+#include "constants.h"
class Pid {
public:
float error_;
float setpoint_;
- //needed for integral term
- float error_sum_array_[WINDOW_SIZE];
- int error_sum_index_;
+ //needed for integrative term
+ int error_sum_;
//needed for derivative term
float previous_error_;
this->setpoint_ = 0;
this->previous_error_ = 0;
- this->error_sum_index_ = 0;
+ this->error_sum_ = 0;
- for (int i = 0; i < WINDOW_SIZE; i++) {
- this->error_sum_array_[i] = 0;
- }
-
- this->min_ = -790;
- this->max_ = 790;
+ this->min_ = -MAX_DUTY_CYCLE;
+ this->max_ = MAX_DUTY_CYCLE;
}
this->setpoint_ = 0;
this->previous_error_ = 0;
- this->error_sum_index_ = 0;
+ this->error_sum_ = 0;
- for (int i = 0; i < WINDOW_SIZE; i++) {
- this->error_sum_array_[i] = 0;
- }
}
void set(float setpoint) {
//proportional term
float output = this->error_ * this->kp_;
- //integral term with antiwindup
-// if (this->error_sum_index_ == WINDOW_SIZE) {
-// this->error_sum_array_[0] = this->error_;
-// this->error_sum_index_ = 1;
-// } else {
-// this->error_sum_array_[this->error_sum_index_] = this->error_;
-// this->error_sum_index_++;
-// }
-//
-// float error_sum = 0;
-// for (int i = 0; i < WINDOW_SIZE; i++) {
-// error_sum += this->error_sum_array_[i];
-// }
-//
-// output += error_sum * this->ki_;
-
//integral term without windup
-
- error_sum_array_[0] += this->error_;
- output += error_sum_array_[0] * this->ki_;
-
-
+ error_sum_ += this->error_;
+ output += error_sum_ * this->ki_;
//derivative term
output += (this->error_ - this->previous_error_) * kd_;