PID

Dependents:   balance_all

Files at this revision

API Documentation at this revision

Comitter:
ckalintra
Date:
Wed May 16 10:27:33 2018 +0000
Commit message:
PID

Changed in this revision

PID.cpp Show annotated file Show diff for this revision Revisions of this file
PID.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 3d03a93d9671 PID.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PID.cpp	Wed May 16 10:27:33 2018 +0000
@@ -0,0 +1,18 @@
+#include "PID.h"
+#define Kp 0.1
+#define Ki 0.00001
+
+void PID::control(float x, float *output, float time, float integral)
+{
+    float error = - x;
+    float Pout = Kp * error;//multiply the error with kp to get P output
+
+    integral += error * time;//integral adds the previous error + the error now * time the error presist
+    
+    float Iout = Ki * integral;//multiply by Ki
+    output[0] = Pout + Iout;
+    if( output[0] > 1 )//limit the output
+        {output[0] = 1;}
+    else if( output[0] < -1 )
+        {output[0] = -1;}
+}
\ No newline at end of file
diff -r 000000000000 -r 3d03a93d9671 PID.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PID.h	Wed May 16 10:27:33 2018 +0000
@@ -0,0 +1,20 @@
+#ifndef PID_H
+#define PID_H
+
+#include "mbed.h"
+
+extern Serial pc;
+
+class PID
+{
+    protected:
+    public: 
+    /*x = error
+    output = PID output
+    time = time since last pid is run
+    //integral = sum of all previous error
+    */
+   void control(float x, float *output, float time, float integral);
+};
+
+#endif
\ No newline at end of file