PID sudah ternormalisasi

Dependencies:   Motor PS_PAD TextLCD mbed-os

Fork of cobaLCDJoyMotor_Thread by EL4121 Embedded System

Committer:
rizqicahyo
Date:
Sat Dec 16 13:40:56 2017 +0000
Revision:
5:d08502bd4abe
Parent:
4:cd5de3b14797
perbaikan pada algoritma PID dengan dilakukan normalisasi nilai output PID

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rizqicahyo 1:2bf3dac65b08 1
rizqicahyo 0:837acb06c892 2 #include "mbed.h"
rizqicahyo 0:837acb06c892 3 #include "TextLCD.h"
rizqicahyo 3:a03ce2084ceb 4 #include "PS_PAD.h"
be_bryan 4:cd5de3b14797 5 #include "Motor.h"
be_bryan 4:cd5de3b14797 6 #include "encoderKRAI.h"
rizqicahyo 0:837acb06c892 7
rizqicahyo 3:a03ce2084ceb 8 #include <string>
rizqicahyo 3:a03ce2084ceb 9 using namespace std;
rizqicahyo 3:a03ce2084ceb 10
rizqicahyo 5:d08502bd4abe 11 #define PI 3.14159265359
rizqicahyo 5:d08502bd4abe 12 #define RAD_TO_DEG 57.2957795131
rizqicahyo 5:d08502bd4abe 13
rizqicahyo 5:d08502bd4abe 14 #define PULSE_TO_MM 0.1177 //rev/pulse * K_lingkaran_roda
rizqicahyo 5:d08502bd4abe 15 #define L 144.0 // lengan roda dari pusat robot (mm)
rizqicahyo 5:d08502bd4abe 16 #define Ts 2.0 // Time Sampling sistem 2ms
rizqicahyo 5:d08502bd4abe 17
rizqicahyo 5:d08502bd4abe 18 #define MAX_SPEED 912.175 //Vresultan max (mm/s)
rizqicahyo 5:d08502bd4abe 19 #define MAX_W_SPEED 1314.72 //Vw max (mm/s)
rizqicahyo 5:d08502bd4abe 20 #define SPEED 1
rizqicahyo 5:d08502bd4abe 21
be_bryan 4:cd5de3b14797 22 Thread thread1(osPriorityNormal, OS_STACK_SIZE, NULL);
rizqicahyo 5:d08502bd4abe 23 Thread thread2(osPriorityNormal, OS_STACK_SIZE, NULL);
be_bryan 4:cd5de3b14797 24 Thread thread3(osPriorityNormal, OS_STACK_SIZE, NULL);
rizqicahyo 5:d08502bd4abe 25 Thread thread4(osPriorityNormal, OS_STACK_SIZE, NULL);
rizqicahyo 5:d08502bd4abe 26 Thread thread5(osPriorityNormal, OS_STACK_SIZE, NULL);
rizqicahyo 3:a03ce2084ceb 27
be_bryan 4:cd5de3b14797 28 TextLCD lcd(PA_9, PC_3, PC_2, PA_8, PB_0, PC_15, TextLCD::LCD20x4); //rs,e,d4-d7
be_bryan 4:cd5de3b14797 29 encoderKRAI enc1 (PC_14, PC_13, 11, encoderKRAI::X4_ENCODING);
be_bryan 4:cd5de3b14797 30 encoderKRAI enc2 (PC_0, PC_1, 11, encoderKRAI::X4_ENCODING);
be_bryan 4:cd5de3b14797 31 encoderKRAI enc3 (PB_10, PB_3, 11, encoderKRAI::X4_ENCODING);
rizqicahyo 3:a03ce2084ceb 32 PS_PAD ps2(PB_15,PB_14,PB_13, PC_4); //(mosi, miso, sck, ss)
rizqicahyo 3:a03ce2084ceb 33
rizqicahyo 5:d08502bd4abe 34 Motor motor3(PB_7, PA_14, PA_15); //motor4
rizqicahyo 5:d08502bd4abe 35 Motor motor2(PA_11, PA_6, PA_5); //motor2
rizqicahyo 5:d08502bd4abe 36 Motor motor1(PB_6, PA_7, PB_12); //motor 3
rizqicahyo 5:d08502bd4abe 37 //Motor motor1(PA_10, PB_5, PB_4); //motor_griper
rizqicahyo 5:d08502bd4abe 38
rizqicahyo 5:d08502bd4abe 39 Serial pc(USBTX,USBRX);
rizqicahyo 5:d08502bd4abe 40
be_bryan 4:cd5de3b14797 41 void dataJoystick();
be_bryan 4:cd5de3b14797 42 void lcdPrint();
rizqicahyo 5:d08502bd4abe 43 void self_localization();
be_bryan 4:cd5de3b14797 44 void motorP();
rizqicahyo 5:d08502bd4abe 45 void calculate_PID();
rizqicahyo 5:d08502bd4abe 46
rizqicahyo 5:d08502bd4abe 47 float moving_direction( float xs, float ys, float x, float y,float theta);
rizqicahyo 5:d08502bd4abe 48
rizqicahyo 5:d08502bd4abe 49
rizqicahyo 5:d08502bd4abe 50
rizqicahyo 5:d08502bd4abe 51 /*------------buruk----------------*/
rizqicahyo 5:d08502bd4abe 52 float x = 0;
rizqicahyo 5:d08502bd4abe 53 float y = 0;
rizqicahyo 5:d08502bd4abe 54 float theta = 0;
rizqicahyo 0:837acb06c892 55
rizqicahyo 5:d08502bd4abe 56 float x_prev = 0;
rizqicahyo 5:d08502bd4abe 57 float y_prev = 0;
rizqicahyo 5:d08502bd4abe 58 float theta_prev = 0;
rizqicahyo 5:d08502bd4abe 59
rizqicahyo 5:d08502bd4abe 60 float vr = 0;
rizqicahyo 5:d08502bd4abe 61 float vw = 0;
rizqicahyo 5:d08502bd4abe 62 float a = 0;
rizqicahyo 5:d08502bd4abe 63
rizqicahyo 5:d08502bd4abe 64 float Vx = 0;
rizqicahyo 5:d08502bd4abe 65 float Vy = 0;
rizqicahyo 5:d08502bd4abe 66 float W = 0;
rizqicahyo 5:d08502bd4abe 67
rizqicahyo 5:d08502bd4abe 68 string str;
rizqicahyo 5:d08502bd4abe 69 /*---------------------------------------*/
rizqicahyo 0:837acb06c892 70
rizqicahyo 0:837acb06c892 71 int main()
rizqicahyo 0:837acb06c892 72 {
rizqicahyo 0:837acb06c892 73 pc.baud(115200);
rizqicahyo 3:a03ce2084ceb 74 ps2.init();
be_bryan 4:cd5de3b14797 75 thread1.start(dataJoystick);
be_bryan 4:cd5de3b14797 76 thread2.start(lcdPrint);
rizqicahyo 5:d08502bd4abe 77 thread3.start(self_localization);
rizqicahyo 5:d08502bd4abe 78 thread4.start(motorP);
rizqicahyo 5:d08502bd4abe 79 thread5.start(calculate_PID);
rizqicahyo 5:d08502bd4abe 80
rizqicahyo 0:837acb06c892 81 while (1)
rizqicahyo 0:837acb06c892 82 {
rizqicahyo 3:a03ce2084ceb 83 // baca input
be_bryan 4:cd5de3b14797 84 /*
rizqicahyo 3:a03ce2084ceb 85 ps2.poll();
rizqicahyo 3:a03ce2084ceb 86 if(ps2.read(PS_PAD::PAD_X)==1) a = "silang";
rizqicahyo 3:a03ce2084ceb 87 else if(ps2.read(PS_PAD::PAD_CIRCLE)==1) a = "lingkaran";
rizqicahyo 3:a03ce2084ceb 88 else if(ps2.read(PS_PAD::PAD_TRIANGLE)==1) a = "segitiga";
rizqicahyo 3:a03ce2084ceb 89 else if(ps2.read(PS_PAD::PAD_SQUARE)==1) a = "kotak";
rizqicahyo 3:a03ce2084ceb 90 else a = "NULL";
be_bryan 4:cd5de3b14797 91 */
be_bryan 4:cd5de3b14797 92 /*
rizqicahyo 3:a03ce2084ceb 93 //tampilkan LCD
rizqicahyo 3:a03ce2084ceb 94 lcd.locate(0,0);
rizqicahyo 3:a03ce2084ceb 95 lcd.printf("input joystik :");
rizqicahyo 3:a03ce2084ceb 96 lcd.locate(0,1);
rizqicahyo 3:a03ce2084ceb 97 lcd.printf("%s",a);
rizqicahyo 0:837acb06c892 98
rizqicahyo 0:837acb06c892 99 wait_ms(10);
rizqicahyo 0:837acb06c892 100 lcd.cls();
be_bryan 4:cd5de3b14797 101 */
be_bryan 4:cd5de3b14797 102 }
be_bryan 4:cd5de3b14797 103 }
be_bryan 4:cd5de3b14797 104
be_bryan 4:cd5de3b14797 105 void dataJoystick(){
be_bryan 4:cd5de3b14797 106 while(true){
be_bryan 4:cd5de3b14797 107 ps2.poll();
rizqicahyo 5:d08502bd4abe 108 if(ps2.read(PS_PAD::PAD_X)==1) str = "silang";
rizqicahyo 5:d08502bd4abe 109 else if(ps2.read(PS_PAD::PAD_CIRCLE)==1) str = "lingkaran";
rizqicahyo 5:d08502bd4abe 110 else if(ps2.read(PS_PAD::PAD_TRIANGLE)==1) str = "segitiga";
rizqicahyo 5:d08502bd4abe 111 else if(ps2.read(PS_PAD::PAD_SQUARE)==1) str = "kotak";
rizqicahyo 5:d08502bd4abe 112 else str = "NULL";
rizqicahyo 0:837acb06c892 113 }
rizqicahyo 0:837acb06c892 114 }
be_bryan 4:cd5de3b14797 115
be_bryan 4:cd5de3b14797 116 void lcdPrint(){
be_bryan 4:cd5de3b14797 117 while (true){
rizqicahyo 5:d08502bd4abe 118 lcd.cls();
rizqicahyo 5:d08502bd4abe 119 lcd.locate(0,0);
rizqicahyo 5:d08502bd4abe 120 lcd.printf("input : %s", str);
rizqicahyo 5:d08502bd4abe 121 lcd.locate(0,1);
rizqicahyo 5:d08502bd4abe 122 //lcd.printf("Vr = %.2f", sqrt(Vx*Vx + Vy*Vy));
rizqicahyo 5:d08502bd4abe 123 lcd.printf("x = %.2f", x);
rizqicahyo 5:d08502bd4abe 124 lcd.locate(0,2);
rizqicahyo 5:d08502bd4abe 125 lcd.printf("y = %.2f", y);
rizqicahyo 5:d08502bd4abe 126 lcd.locate(0,3);
rizqicahyo 5:d08502bd4abe 127 lcd.printf("theta = %.2f", theta*RAD_TO_DEG);
rizqicahyo 5:d08502bd4abe 128 //lcd.printf("a = %.2f", a*RAD_TO_DEG);
rizqicahyo 5:d08502bd4abe 129
rizqicahyo 5:d08502bd4abe 130 Thread::wait(100);
rizqicahyo 5:d08502bd4abe 131 }
rizqicahyo 5:d08502bd4abe 132 }
rizqicahyo 5:d08502bd4abe 133
rizqicahyo 5:d08502bd4abe 134
rizqicahyo 5:d08502bd4abe 135 void self_localization(){
rizqicahyo 5:d08502bd4abe 136 while(true){
rizqicahyo 5:d08502bd4abe 137 float d1 = enc1.getPulses()*PULSE_TO_MM;
rizqicahyo 5:d08502bd4abe 138 float d2 = enc2.getPulses()*PULSE_TO_MM;
rizqicahyo 5:d08502bd4abe 139 float d3 = enc3.getPulses()*PULSE_TO_MM;
rizqicahyo 5:d08502bd4abe 140
rizqicahyo 5:d08502bd4abe 141 x_prev = x;
rizqicahyo 5:d08502bd4abe 142 y_prev = y;
rizqicahyo 5:d08502bd4abe 143 theta_prev = theta;
rizqicahyo 5:d08502bd4abe 144
rizqicahyo 5:d08502bd4abe 145 x = x_prev + (2*d1 - d2 - d3)/3*cos(theta_prev) - (-d2+d3)*0.5773*sin(theta_prev);
rizqicahyo 5:d08502bd4abe 146 y = y_prev + (2*d1 - d2 - d3)/3*sin(theta_prev) + (-d2+d3)*0.5773*cos(theta_prev);
rizqicahyo 5:d08502bd4abe 147 theta = theta_prev + (d1 + d2 + d3)/(3*L); // // 0.132629 => 180 / (3. L. pi)
rizqicahyo 5:d08502bd4abe 148
rizqicahyo 5:d08502bd4abe 149 Vx = (x - x_prev)/0.002;
rizqicahyo 5:d08502bd4abe 150 Vy = (y - y_prev)/0.002;
rizqicahyo 5:d08502bd4abe 151 W = (theta - theta_prev)/0.002;
rizqicahyo 5:d08502bd4abe 152
rizqicahyo 5:d08502bd4abe 153 enc1.reset();
rizqicahyo 5:d08502bd4abe 154 enc2.reset();
rizqicahyo 5:d08502bd4abe 155 enc3.reset();
rizqicahyo 5:d08502bd4abe 156
rizqicahyo 5:d08502bd4abe 157 Thread::wait(Ts); //frekuensi sampling = 500 Hz
be_bryan 4:cd5de3b14797 158 }
be_bryan 4:cd5de3b14797 159 }
be_bryan 4:cd5de3b14797 160
rizqicahyo 5:d08502bd4abe 161
rizqicahyo 5:d08502bd4abe 162 void calculate_PID(){
rizqicahyo 5:d08502bd4abe 163 // konstanta PID untuk kendali Posisi (x y)
rizqicahyo 5:d08502bd4abe 164 float Kp_s = 10.0;
rizqicahyo 5:d08502bd4abe 165 float Ki_s = 0.0;
rizqicahyo 5:d08502bd4abe 166 float Kd_s = 1.6;
rizqicahyo 5:d08502bd4abe 167
rizqicahyo 5:d08502bd4abe 168 // konstanta PID untuk kendali arah (theta)
rizqicahyo 5:d08502bd4abe 169 float Kp_w = 0.2;
rizqicahyo 5:d08502bd4abe 170 float Ki_w = 0.0;
rizqicahyo 5:d08502bd4abe 171 float Kd_w = 0.01;
rizqicahyo 5:d08502bd4abe 172
rizqicahyo 5:d08502bd4abe 173 // setpoint sistem
rizqicahyo 5:d08502bd4abe 174 float x_set = 400;
rizqicahyo 5:d08502bd4abe 175 float y_set = 700;
rizqicahyo 5:d08502bd4abe 176 float theta_set = 180;
rizqicahyo 5:d08502bd4abe 177 float S_set = sqrt(x_set*x_set + y_set*y_set);
rizqicahyo 5:d08502bd4abe 178
rizqicahyo 5:d08502bd4abe 179 // variabel tambahan
rizqicahyo 5:d08502bd4abe 180 float S_error_prev = 0;
rizqicahyo 5:d08502bd4abe 181 float theta_error_prev = 0;
rizqicahyo 5:d08502bd4abe 182
rizqicahyo 5:d08502bd4abe 183 float sum_S_error = 0;
rizqicahyo 5:d08502bd4abe 184 float sum_theta_error = 0;
rizqicahyo 5:d08502bd4abe 185
rizqicahyo 5:d08502bd4abe 186 while(true){
rizqicahyo 5:d08502bd4abe 187 //menghitung error posisi
rizqicahyo 5:d08502bd4abe 188 //float S = sqrt(x*x + y*y);
rizqicahyo 5:d08502bd4abe 189 //float S_error = S_set - S;
rizqicahyo 5:d08502bd4abe 190 float S_error = sqrt((x_set-x)*(x_set-x) + (y_set-y)*(y_set-y));
rizqicahyo 5:d08502bd4abe 191 //menghitung error arah
rizqicahyo 5:d08502bd4abe 192 float theta_error = theta_set - theta*RAD_TO_DEG;
rizqicahyo 5:d08502bd4abe 193
rizqicahyo 5:d08502bd4abe 194 sum_S_error += S_error;
rizqicahyo 5:d08502bd4abe 195 sum_theta_error += theta_error;
rizqicahyo 5:d08502bd4abe 196
rizqicahyo 5:d08502bd4abe 197 float vs = Kp_s*S_error + Ki_s*Ts*sum_S_error + Kd_s*(S_error - S_error_prev)/Ts;
rizqicahyo 5:d08502bd4abe 198 float w = Kp_w*theta_error + Ki_w*Ts*sum_theta_error + Kd_w*(theta_error - theta_error_prev)/Ts;
rizqicahyo 5:d08502bd4abe 199
rizqicahyo 5:d08502bd4abe 200 vr = vs/MAX_SPEED*0.5;
rizqicahyo 5:d08502bd4abe 201 vw = w*L/MAX_W_SPEED*0.5;
rizqicahyo 5:d08502bd4abe 202 a = moving_direction(x_set,y_set,x,y,theta);
rizqicahyo 5:d08502bd4abe 203
rizqicahyo 5:d08502bd4abe 204 S_error_prev = S_error;
rizqicahyo 5:d08502bd4abe 205 theta_error_prev = theta_error;
rizqicahyo 5:d08502bd4abe 206
rizqicahyo 5:d08502bd4abe 207 Thread::wait(Ts);
rizqicahyo 5:d08502bd4abe 208 }
rizqicahyo 5:d08502bd4abe 209 }
rizqicahyo 5:d08502bd4abe 210
rizqicahyo 5:d08502bd4abe 211
rizqicahyo 5:d08502bd4abe 212 float moving_direction( float xs, float ys, float x, float y,float theta){
rizqicahyo 5:d08502bd4abe 213 float temp = atan((ys - y)/(xs - x)) - theta;
rizqicahyo 5:d08502bd4abe 214
rizqicahyo 5:d08502bd4abe 215 if (xs < x) return temp + PI;
rizqicahyo 5:d08502bd4abe 216 else return temp;
rizqicahyo 5:d08502bd4abe 217 }
rizqicahyo 5:d08502bd4abe 218
rizqicahyo 5:d08502bd4abe 219
rizqicahyo 5:d08502bd4abe 220
rizqicahyo 5:d08502bd4abe 221 void motorP() {
rizqicahyo 5:d08502bd4abe 222 Thread::wait(1500);
rizqicahyo 5:d08502bd4abe 223
rizqicahyo 5:d08502bd4abe 224 while(1){
rizqicahyo 5:d08502bd4abe 225 motor1.speed(SPEED*(vr*cos(a) + vw));
rizqicahyo 5:d08502bd4abe 226 motor2.speed(SPEED*(vr*(-0.5*cos(a) - 0.866*sin(a)) + vw));
rizqicahyo 5:d08502bd4abe 227 motor3.speed(SPEED*(vr*(-0.5*cos(a) + 0.866*sin(a)) + vw));
rizqicahyo 5:d08502bd4abe 228 }
rizqicahyo 5:d08502bd4abe 229 }
rizqicahyo 5:d08502bd4abe 230