Library with P, PI and PID controller

Dependents:   buttoncontrol includeair includeair Oudverslag

Committer:
Gerth
Date:
Thu Oct 08 13:14:29 2015 +0000
Revision:
8:3b077fc3d1ec
Parent:
7:6e2cd9b0403b
Child:
9:2d57152425d1
added stop function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gerth 0:041a12a5b315 1 #include "controlandadjust.h"
Gerth 0:041a12a5b315 2 #include "mbed.h"
Gerth 0:041a12a5b315 3 #include "QEI.h"
Gerth 0:041a12a5b315 4
Gerth 7:6e2cd9b0403b 5 float CW=1;
Gerth 7:6e2cd9b0403b 6 float CCW=0;
Gerth 0:041a12a5b315 7 float direction1;
Gerth 0:041a12a5b315 8 float speed1;
Gerth 0:041a12a5b315 9 float direction2;
Gerth 0:041a12a5b315 10 float speed2;
Gerth 0:041a12a5b315 11
Gerth 0:041a12a5b315 12 DigitalOut motor1_dir(D7);// draairichting motor 1 (1 is CW encoder als je daar op kijkt en CW shaft als je daar op kijkt)
Gerth 0:041a12a5b315 13 PwmOut motor1_speed(D6);//aanstuursnelheid motor 1
Gerth 0:041a12a5b315 14 PwmOut motor2_speed(D5);
Gerth 0:041a12a5b315 15 DigitalOut motor2_dir(D4);
Gerth 0:041a12a5b315 16
Gerth 0:041a12a5b315 17
Gerth 0:041a12a5b315 18
Gerth 0:041a12a5b315 19 controlandadjust::controlandadjust(void)
Gerth 0:041a12a5b315 20 {
Gerth 1:ece12a295ce3 21 //motor1_speed.write(0);
Gerth 1:ece12a295ce3 22 // motor2_speed.write(0);
Gerth 1:ece12a295ce3 23
Gerth 1:ece12a295ce3 24
Gerth 0:041a12a5b315 25 }
Gerth 0:041a12a5b315 26
Gerth 0:041a12a5b315 27 void controlandadjust::verwerksignaal(float signaal1,float signaal2)
Gerth 0:041a12a5b315 28 {
Gerth 0:041a12a5b315 29 //motor1
Gerth 0:041a12a5b315 30 if (signaal1>=0) {//determine CW or CCW rotation
Gerth 0:041a12a5b315 31 direction1=CW;
Gerth 0:041a12a5b315 32 } else {
Gerth 0:041a12a5b315 33 direction1=CCW;
Gerth 0:041a12a5b315 34 }
Gerth 0:041a12a5b315 35
Gerth 0:041a12a5b315 36 if (fabs(signaal1)>=1) { //check if signal is <1
Gerth 0:041a12a5b315 37 speed1=1;//if signal >1 make it 1 to not damage motor
Gerth 0:041a12a5b315 38 } else {
Gerth 0:041a12a5b315 39 speed1=fabs(signaal1);// if signal<1 use signal
Gerth 0:041a12a5b315 40 }
Gerth 0:041a12a5b315 41
Gerth 0:041a12a5b315 42 //motor2
Gerth 0:041a12a5b315 43 if (signaal2>=0) {//determine CW or CCW rotation
Gerth 0:041a12a5b315 44 direction2=CW;
Gerth 0:041a12a5b315 45 } else {
Gerth 0:041a12a5b315 46 direction2=CCW;
Gerth 0:041a12a5b315 47 }
Gerth 0:041a12a5b315 48
Gerth 0:041a12a5b315 49 if (fabs(signaal2)>=1) { //check if signal is <1
Gerth 0:041a12a5b315 50 speed2=1;//if signal >1 make it 1 to not damage motor
Gerth 0:041a12a5b315 51 } else {
Gerth 0:041a12a5b315 52 speed2=fabs(signaal2);// if signal<1 use signal
Gerth 0:041a12a5b315 53 }
Gerth 0:041a12a5b315 54 //write to motor
Gerth 0:041a12a5b315 55 motor1_dir.write(direction1);
Gerth 0:041a12a5b315 56 motor1_speed.write(speed1);
Gerth 0:041a12a5b315 57 motor2_dir.write(direction2);
Gerth 0:041a12a5b315 58 motor2_speed.write(speed2);
Gerth 0:041a12a5b315 59
Gerth 0:041a12a5b315 60 }
Gerth 1:ece12a295ce3 61 float controlandadjust::motor1pwm()
Gerth 1:ece12a295ce3 62 {
Gerth 1:ece12a295ce3 63 return speed1;
Gerth 1:ece12a295ce3 64 }
Gerth 1:ece12a295ce3 65
Gerth 1:ece12a295ce3 66 float controlandadjust::motor2pwm()
Gerth 1:ece12a295ce3 67 {
Gerth 1:ece12a295ce3 68 return speed2;
Gerth 1:ece12a295ce3 69 }
Gerth 1:ece12a295ce3 70
Gerth 0:041a12a5b315 71
Gerth 0:041a12a5b315 72 void controlandadjust::P(float error1,float error2,float Kp)
Gerth 0:041a12a5b315 73 {
Gerth 0:041a12a5b315 74 float signaal1=error1*Kp;
Gerth 0:041a12a5b315 75 float signaal2=error2*Kp;
Gerth 0:041a12a5b315 76 verwerksignaal(signaal1,signaal2);
Gerth 0:041a12a5b315 77 }
Gerth 0:041a12a5b315 78
Gerth 0:041a12a5b315 79 void controlandadjust::PI(float error1, float error2, float Kp, float Ki,float Ts, float &error1_int, float &error2_int)
Gerth 0:041a12a5b315 80 {
Gerth 0:041a12a5b315 81 error1_int = error1_int + Ts * error1;
Gerth 0:041a12a5b315 82 float signaal1=Kp*error1+Ki*error1_int;
Gerth 0:041a12a5b315 83
Gerth 0:041a12a5b315 84 error2_int = error2_int + Ts * error2;
Gerth 0:041a12a5b315 85 float signaal2=Kp*error2+Ki*error2_int;
Gerth 0:041a12a5b315 86
Gerth 0:041a12a5b315 87 verwerksignaal (signaal1,signaal2);
Gerth 0:041a12a5b315 88 }
Gerth 0:041a12a5b315 89
Gerth 0:041a12a5b315 90 void controlandadjust::PID(float error1, float error2, float Kp, float Ki,float Kd,float Ts,
Gerth 1:ece12a295ce3 91 float &error1_int, float &error2_int, float &error1_prev, float &error2_prev)
Gerth 0:041a12a5b315 92 {
Gerth 0:041a12a5b315 93 // Derivative
Gerth 0:041a12a5b315 94 double error1_der = (error1 - error1_prev)/Ts;
Gerth 0:041a12a5b315 95 error1_prev = error1;
Gerth 0:041a12a5b315 96
Gerth 0:041a12a5b315 97 double error2_der = (error2 - error2_prev)/Ts;
Gerth 0:041a12a5b315 98 error2_prev = error2;
Gerth 0:041a12a5b315 99
Gerth 0:041a12a5b315 100 // Integral
Gerth 0:041a12a5b315 101 error1_int = error1_int + Ts * error1;
Gerth 0:041a12a5b315 102 error2_int = error2_int + Ts * error2;
Gerth 0:041a12a5b315 103 // PID
Gerth 0:041a12a5b315 104 float signaal1= Kp * error1 + Ki * error1_int + Kd * error1_der;
Gerth 0:041a12a5b315 105 float signaal2= Kp * error2 + Ki * error2_int + Kd * error2_der;
Gerth 0:041a12a5b315 106
Gerth 0:041a12a5b315 107 verwerksignaal(signaal1,signaal2);
Gerth 8:3b077fc3d1ec 108 }
Gerth 8:3b077fc3d1ec 109
Gerth 8:3b077fc3d1ec 110 void controlandadjust::STOP()
Gerth 8:3b077fc3d1ec 111 {
Gerth 8:3b077fc3d1ec 112 motor1_speed.write(speed1);
Gerth 8:3b077fc3d1ec 113 motor2_speed.write(speed2);
Gerth 0:041a12a5b315 114 }