From 8a975a02f3ee1ace90eb598e4062982dc89fd679 Mon Sep 17 00:00:00 2001
From: "timo.altan" <timo.altan@th-koeln.de>
Date: Wed, 11 May 2022 15:47:16 +0200
Subject: [PATCH] init

---
 .gitignore           |  2 ++
 examples/example.ino | 44 ++++++++++++++++++++++++++++++++++++++++++++
 pid_controller.cpp   | 25 +++++++++++++++++++++++++
 pid_controller.h     | 21 +++++++++++++++++++++
 4 files changed, 92 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 examples/example.ino

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6a8bc10
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.vscode
+build
\ No newline at end of file
diff --git a/examples/example.ino b/examples/example.ino
new file mode 100644
index 0000000..575199f
--- /dev/null
+++ b/examples/example.ino
@@ -0,0 +1,44 @@
+#include <pid_controller.h>
+
+double prop_gain = 0.1;
+double int_gain = 0.1;
+double diff_gain = 0.01;
+
+double input_value = 0;
+uint8_t step = 0;
+uint8_t setpoint = 50;
+
+PIDController pid(prop_gain, int_gain, diff_gain, 0);
+
+void setup(){
+    Serial.begin(115200);
+    pid.set_setpoint(setpoint);
+}
+
+void loop(){
+    step++;
+    // if (input_value < 70)
+    // {   
+        
+    //     pid.set_setpoint(100);
+    // }
+    input_value = pid.compute(input_value);
+
+    if(setpoint - input_value < 0.01)
+    {
+        Serial.print("Reached setpoint after ");
+        Serial.print(step);
+        Serial.print(" steps");
+        exit(0);
+    }
+    else
+    {
+        Serial.print(step);
+        Serial.print(": ");
+        Serial.print(input_value);
+        Serial.print("\n");
+    }
+
+    delay(100);
+
+}
\ No newline at end of file
diff --git a/pid_controller.cpp b/pid_controller.cpp
index e69de29..5334491 100644
--- a/pid_controller.cpp
+++ b/pid_controller.cpp
@@ -0,0 +1,25 @@
+#include <pid_controller.h>
+
+PIDController::PIDController(double Kp, double Ki, double Kd, double setpoint) : Kp(Kp), Ki(Ki), Kd(Kd), setpoint(setpoint) {}
+
+void PIDController::set_setpoint(double setpoint)
+{
+    this->setpoint = setpoint;
+}
+
+double PIDController::compute(double input_value)
+{
+    double error_value = setpoint - input_value;
+
+    out_p = Kp * error_value;  // P-Anteil
+    out_i += Ki * error_value; // I-Anteil
+    out_d -= Kd * error_value; // D-Anteil
+
+    return out_p + out_i + out_d;
+}
+
+void PIDController::reset()
+{
+    out_i = 0;
+    out_d = 0;
+}
\ No newline at end of file
diff --git a/pid_controller.h b/pid_controller.h
index e69de29..1faf019 100644
--- a/pid_controller.h
+++ b/pid_controller.h
@@ -0,0 +1,21 @@
+#ifndef PID_CONTROLLER_H
+#define PID_CONTROLLER_H
+
+#include "Arduino.h"
+
+class PIDController
+{
+    public:
+        PIDController(double Kp, double Ki, double Kd, double setpoint);
+        double compute(double input_value);
+        void set_setpoint(double setpoint);
+        void reset();
+
+    private:
+        double Kp, Ki, Kd;
+        double setpoint;
+        double out_p, out_i, out_d;
+
+};
+
+#endif
\ No newline at end of file
-- 
GitLab