PID bijdrage

Dependencies:   mbed

Committer:
s1574396
Date:
Fri Oct 26 09:42:50 2018 +0000
Revision:
0:8c5ca301505f
PID controler;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s1574396 0:8c5ca301505f 1 #include "mbed.h"
s1574396 0:8c5ca301505f 2 const double Ts = // sample frequency
s1574396 0:8c5ca301505f 3
s1574396 0:8c5ca301505f 4 //PID rotation - aanmaken van PID constanten en variabelen voor rotatie
s1574396 0:8c5ca301505f 5 const double = Rot_Kp = , Rot_Ki = , Rot_Kd = ;
s1574396 0:8c5ca301505f 6 double Rot_error = 0;
s1574396 0:8c5ca301505f 7 double Rot_prev_error = 0;
s1574396 0:8c5ca301505f 8
s1574396 0:8c5ca301505f 9 //PID translation - aanmaken van PID constanten en variabelen voor translatie
s1574396 0:8c5ca301505f 10 const double = Trans_Kp = , Trans_Ki = , Trans_Kd = ;
s1574396 0:8c5ca301505f 11 double Trans_error = 0;
s1574396 0:8c5ca301505f 12 double Trans_prev_error = 0;
s1574396 0:8c5ca301505f 13
s1574396 0:8c5ca301505f 14 // PID execution
s1574396 0:8c5ca301505f 15 double PID_control(double error, const double kp, const double ki, const double kd,
s1574396 0:8c5ca301505f 16 {
s1574396 0:8c5ca301505f 17 // P control
s1574396 0:8c5ca301505f 18 double u_k = pk * error;
s1574396 0:8c5ca301505f 19
s1574396 0:8c5ca301505f 20 // I control
s1574396 0:8c5ca301505f 21 error_int = error_int + (Ts * error);
s1574396 0:8c5ca301505f 22 double u_i = ki * error_int;
s1574396 0:8c5ca301505f 23
s1574396 0:8c5ca301505f 24 // D control
s1574396 0:8c5ca301505f 25 double error_deriv = (error - error_prev);
s1574396 0:8c5ca301505f 26 double u_d = kd * error_deriv;
s1574396 0:8c5ca301505f 27 error_prev = error;
s1574396 0:8c5ca301505f 28
s1574396 0:8c5ca301505f 29 return u_k + u_i + u_d;
s1574396 0:8c5ca301505f 30
s1574396 0:8c5ca301505f 31 int main()
s1574396 0:8c5ca301505f 32 {
s1574396 0:8c5ca301505f 33
s1574396 0:8c5ca301505f 34 }