Library with P, PI and PID controller
Dependents: buttoncontrol includeair includeair Oudverslag
controlandadjust.cpp@1:ece12a295ce3, 2015-10-06 (annotated)
- Committer:
- Gerth
- Date:
- Tue Oct 06 12:35:20 2015 +0000
- Revision:
- 1:ece12a295ce3
- Parent:
- 0:041a12a5b315
- Child:
- 5:5794dbc42c3d
Added a readout function for PWM signal;
Who changed what in which revision?
User | Revision | Line number | New 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 | #include <string> |
Gerth | 0:041a12a5b315 | 5 | |
Gerth | 0:041a12a5b315 | 6 | float CW=1; |
Gerth | 0:041a12a5b315 | 7 | float CCW=0; |
Gerth | 0:041a12a5b315 | 8 | float direction1; |
Gerth | 0:041a12a5b315 | 9 | float speed1; |
Gerth | 0:041a12a5b315 | 10 | float direction2; |
Gerth | 0:041a12a5b315 | 11 | float speed2; |
Gerth | 0:041a12a5b315 | 12 | |
Gerth | 0:041a12a5b315 | 13 | 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 | 14 | PwmOut motor1_speed(D6);//aanstuursnelheid motor 1 |
Gerth | 0:041a12a5b315 | 15 | PwmOut motor2_speed(D5); |
Gerth | 0:041a12a5b315 | 16 | DigitalOut motor2_dir(D4); |
Gerth | 0:041a12a5b315 | 17 | |
Gerth | 0:041a12a5b315 | 18 | |
Gerth | 0:041a12a5b315 | 19 | |
Gerth | 0:041a12a5b315 | 20 | controlandadjust::controlandadjust(void) |
Gerth | 0:041a12a5b315 | 21 | { |
Gerth | 1:ece12a295ce3 | 22 | //motor1_speed.write(0); |
Gerth | 1:ece12a295ce3 | 23 | // motor2_speed.write(0); |
Gerth | 1:ece12a295ce3 | 24 | |
Gerth | 1:ece12a295ce3 | 25 | |
Gerth | 0:041a12a5b315 | 26 | } |
Gerth | 0:041a12a5b315 | 27 | |
Gerth | 0:041a12a5b315 | 28 | void controlandadjust::verwerksignaal(float signaal1,float signaal2) |
Gerth | 0:041a12a5b315 | 29 | { |
Gerth | 0:041a12a5b315 | 30 | //motor1 |
Gerth | 0:041a12a5b315 | 31 | if (signaal1>=0) {//determine CW or CCW rotation |
Gerth | 0:041a12a5b315 | 32 | direction1=CW; |
Gerth | 0:041a12a5b315 | 33 | } else { |
Gerth | 0:041a12a5b315 | 34 | direction1=CCW; |
Gerth | 0:041a12a5b315 | 35 | } |
Gerth | 0:041a12a5b315 | 36 | |
Gerth | 0:041a12a5b315 | 37 | if (fabs(signaal1)>=1) { //check if signal is <1 |
Gerth | 0:041a12a5b315 | 38 | speed1=1;//if signal >1 make it 1 to not damage motor |
Gerth | 0:041a12a5b315 | 39 | } else { |
Gerth | 0:041a12a5b315 | 40 | speed1=fabs(signaal1);// if signal<1 use signal |
Gerth | 0:041a12a5b315 | 41 | } |
Gerth | 0:041a12a5b315 | 42 | |
Gerth | 0:041a12a5b315 | 43 | //motor2 |
Gerth | 0:041a12a5b315 | 44 | if (signaal2>=0) {//determine CW or CCW rotation |
Gerth | 0:041a12a5b315 | 45 | direction2=CW; |
Gerth | 0:041a12a5b315 | 46 | } else { |
Gerth | 0:041a12a5b315 | 47 | direction2=CCW; |
Gerth | 0:041a12a5b315 | 48 | } |
Gerth | 0:041a12a5b315 | 49 | |
Gerth | 0:041a12a5b315 | 50 | if (fabs(signaal2)>=1) { //check if signal is <1 |
Gerth | 0:041a12a5b315 | 51 | speed2=1;//if signal >1 make it 1 to not damage motor |
Gerth | 0:041a12a5b315 | 52 | } else { |
Gerth | 0:041a12a5b315 | 53 | speed2=fabs(signaal2);// if signal<1 use signal |
Gerth | 0:041a12a5b315 | 54 | } |
Gerth | 0:041a12a5b315 | 55 | //write to motor |
Gerth | 0:041a12a5b315 | 56 | motor1_dir.write(direction1); |
Gerth | 0:041a12a5b315 | 57 | motor1_speed.write(speed1); |
Gerth | 0:041a12a5b315 | 58 | motor2_dir.write(direction2); |
Gerth | 0:041a12a5b315 | 59 | motor2_speed.write(speed2); |
Gerth | 0:041a12a5b315 | 60 | |
Gerth | 0:041a12a5b315 | 61 | } |
Gerth | 1:ece12a295ce3 | 62 | float controlandadjust::motor1pwm() |
Gerth | 1:ece12a295ce3 | 63 | { |
Gerth | 1:ece12a295ce3 | 64 | return speed1; |
Gerth | 1:ece12a295ce3 | 65 | } |
Gerth | 1:ece12a295ce3 | 66 | |
Gerth | 1:ece12a295ce3 | 67 | float controlandadjust::motor2pwm() |
Gerth | 1:ece12a295ce3 | 68 | { |
Gerth | 1:ece12a295ce3 | 69 | return speed2; |
Gerth | 1:ece12a295ce3 | 70 | } |
Gerth | 1:ece12a295ce3 | 71 | |
Gerth | 0:041a12a5b315 | 72 | |
Gerth | 0:041a12a5b315 | 73 | void controlandadjust::P(float error1,float error2,float Kp) |
Gerth | 0:041a12a5b315 | 74 | { |
Gerth | 0:041a12a5b315 | 75 | float signaal1=error1*Kp; |
Gerth | 0:041a12a5b315 | 76 | float signaal2=error2*Kp; |
Gerth | 0:041a12a5b315 | 77 | verwerksignaal(signaal1,signaal2); |
Gerth | 0:041a12a5b315 | 78 | } |
Gerth | 0:041a12a5b315 | 79 | |
Gerth | 0:041a12a5b315 | 80 | void controlandadjust::PI(float error1, float error2, float Kp, float Ki,float Ts, float &error1_int, float &error2_int) |
Gerth | 0:041a12a5b315 | 81 | { |
Gerth | 0:041a12a5b315 | 82 | error1_int = error1_int + Ts * error1; |
Gerth | 0:041a12a5b315 | 83 | float signaal1=Kp*error1+Ki*error1_int; |
Gerth | 0:041a12a5b315 | 84 | |
Gerth | 0:041a12a5b315 | 85 | error2_int = error2_int + Ts * error2; |
Gerth | 0:041a12a5b315 | 86 | float signaal2=Kp*error2+Ki*error2_int; |
Gerth | 0:041a12a5b315 | 87 | |
Gerth | 0:041a12a5b315 | 88 | verwerksignaal (signaal1,signaal2); |
Gerth | 0:041a12a5b315 | 89 | } |
Gerth | 0:041a12a5b315 | 90 | |
Gerth | 0:041a12a5b315 | 91 | void controlandadjust::PID(float error1, float error2, float Kp, float Ki,float Kd,float Ts, |
Gerth | 1:ece12a295ce3 | 92 | float &error1_int, float &error2_int, float &error1_prev, float &error2_prev) |
Gerth | 0:041a12a5b315 | 93 | { |
Gerth | 0:041a12a5b315 | 94 | // Derivative |
Gerth | 0:041a12a5b315 | 95 | double error1_der = (error1 - error1_prev)/Ts; |
Gerth | 0:041a12a5b315 | 96 | error1_prev = error1; |
Gerth | 0:041a12a5b315 | 97 | |
Gerth | 0:041a12a5b315 | 98 | double error2_der = (error2 - error2_prev)/Ts; |
Gerth | 0:041a12a5b315 | 99 | error2_prev = error2; |
Gerth | 0:041a12a5b315 | 100 | |
Gerth | 0:041a12a5b315 | 101 | // Integral |
Gerth | 0:041a12a5b315 | 102 | error1_int = error1_int + Ts * error1; |
Gerth | 0:041a12a5b315 | 103 | error2_int = error2_int + Ts * error2; |
Gerth | 0:041a12a5b315 | 104 | // PID |
Gerth | 0:041a12a5b315 | 105 | float signaal1= Kp * error1 + Ki * error1_int + Kd * error1_der; |
Gerth | 0:041a12a5b315 | 106 | float signaal2= Kp * error2 + Ki * error2_int + Kd * error2_der; |
Gerth | 0:041a12a5b315 | 107 | |
Gerth | 0:041a12a5b315 | 108 | verwerksignaal(signaal1,signaal2); |
Gerth | 0:041a12a5b315 | 109 | } |