]> git.leonardobizzoni.com Git - pioneer-stm32/commitdiff
refactor pid
authorFederica Di Lauro <federicadilauro1998@gmail.com>
Wed, 22 Jan 2020 13:24:38 +0000 (14:24 +0100)
committerFederica Di Lauro <federicadilauro1998@gmail.com>
Wed, 22 Jan 2020 13:24:38 +0000 (14:24 +0100)
utils/pid_tuning/otto_pid_tuning/Core/Inc/odometry.h
utils/pid_tuning/otto_pid_tuning/Core/Inc/pid.h

index 5a0b5298fb44315ee06698d8d7c2592c6c82bebb..6ede504fa3a84e5e6564a224ba32c4997d4dc4c8 100644 (file)
@@ -8,7 +8,6 @@ class Odometry {
  public:
   Encoder left_encoder_;
   Encoder right_encoder_;
-  float kBaseline;
 
   float linear_velocity;
   float angular_velocity;
@@ -17,14 +16,12 @@ class Odometry {
   Odometry() {
     left_encoder_ = NULL;
     right_encoder_ = NULL;
-    kBaseline = 0.35;  //in meters
   }
 
   Odometry(Encoder left, Encoder right) {
 
     left_encoder_ = left;
     right_encoder_ = right;
-    kBaseline = 0.35;  //in meters
   }
 
   void UpdateValues() {
index 645cced539eaf2fe10d689de6135720f2937a41d..66d2b1e9dfc905ba93bff0948cd8e3b55fbf67bb 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef PID_H
 #define PID_H
 
-#define WINDOW_SIZE 500
+#include "constants.h"
 
 class Pid {
  public:
@@ -13,9 +13,8 @@ class Pid {
   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_;
@@ -32,14 +31,10 @@ class Pid {
     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;
 
   }
 
@@ -52,11 +47,8 @@ class Pid {
     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) {
@@ -70,28 +62,9 @@ class Pid {
     //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_;