Update 18 Februari 2017, PID laucnher dan Base sudah diperbarui

Dependencies:   Motor PID Joystick_OrdoV5 mbed millis

Fork of MainProgram_BaseBaru_otomatis-reloader by KRAI 2017

Committer:
franshendri
Date:
Sat Dec 03 05:43:10 2016 +0000
Revision:
12:e07c59c28c29
Parent:
10:f0f0dc3904e0
Child:
13:8ab42383a2ca
tes H

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rahmadirizki18 5:3aa203218306 1 /****************************************************************************/
rahmadirizki18 5:3aa203218306 2 /* PROGRAM UNTUK PID CLOSED LOOP */
rahmadirizki18 5:3aa203218306 3 /* */
rahmadirizki18 5:3aa203218306 4 /* - Digunakan encoder autonics */
rahmadirizki18 5:3aa203218306 5 /* - Konfigurasi Motor dan Encoder sbb : */
rahmadirizki18 5:3aa203218306 6 /* _________________ */
rahmadirizki18 5:3aa203218306 7 /* | DEPAN | */
rahmadirizki18 5:3aa203218306 8 /* | 1. e .2 | Angka ==> Motor */
rahmadirizki18 5:3aa203218306 9 /* | ` ` | e ==> Encoder */
rahmadirizki18 5:3aa203218306 10 /* | e e | */
rahmadirizki18 5:3aa203218306 11 /* | . . | */
rahmadirizki18 5:3aa203218306 12 /* | 4` e `3 | */
rahmadirizki18 5:3aa203218306 13 /* |________________| */
rahmadirizki18 5:3aa203218306 14 /* */
rahmadirizki18 5:3aa203218306 15 /* SETTINGS (WAJIB!) : */
rahmadirizki18 5:3aa203218306 16 /* 1. Settings Pin Encoder, Resolusi, dan Tipe encoding di omniPos.h */
rahmadirizki18 5:3aa203218306 17 /* 2. Deklarasi penggunaan library omniPos pada bagian deklarasi encoder */
rahmadirizki18 5:3aa203218306 18 /* */
rahmadirizki18 5:3aa203218306 19 /****************************************************************************/
rahmadirizki18 6:68293bed71ea 20 /* */
rahmadirizki18 6:68293bed71ea 21 /* Joystick */
rahmadirizki18 6:68293bed71ea 22 /* kanan => posisi target x ditambah 0.01 */
rahmadirizki18 6:68293bed71ea 23 /* kiri => posisi target x dikurang 0.01 */
rahmadirizki18 6:68293bed71ea 24 /* atas => posisi target y ditambah 0.01 */
rahmadirizki18 6:68293bed71ea 25 /* bawah => posisi target y dikurang 0.01 */
rahmadirizki18 6:68293bed71ea 26 /* */
rahmadirizki18 6:68293bed71ea 27 /* Tombol silang => Kembali keposisi Awal */
rahmadirizki18 6:68293bed71ea 28 /* Tombol segitiga => Aktif motor Launcher */
rahmadirizki18 6:68293bed71ea 29 /* Tombol kotak => Aktif servo Launcher */
rahmadirizki18 6:68293bed71ea 30 /* */
rahmadirizki18 6:68293bed71ea 31 /****************************************************************************/
rahmadirizki18 6:68293bed71ea 32
fanny868 0:9072e932503c 33
fanny868 0:9072e932503c 34 #include "mbed.h"
fanny868 0:9072e932503c 35 #include "JoystickPS3.h"
fanny868 0:9072e932503c 36 #include "Motor.h"
rahmadirizki18 6:68293bed71ea 37 #include "Servo.h"
fanny868 0:9072e932503c 38
fanny868 0:9072e932503c 39 //#define koefperlambatan 0.8
rahmadirizki18 5:3aa203218306 40 #include "encoderKRAI.h"
rahmadirizki18 5:3aa203218306 41
rahmadirizki18 5:3aa203218306 42 #define pi 22/7
rahmadirizki18 5:3aa203218306 43 #define diaEncoder 0.058
rahmadirizki18 5:3aa203218306 44 #define PPR 1000
rahmadirizki18 5:3aa203218306 45 #define diaRobot 0.64
Joshua23 9:5a50782510fb 46 #define pinservo1 PC_8
rahmadirizki18 7:d138c56dab20 47 //PC 9
Joshua23 9:5a50782510fb 48 #define pinservo2 PC_9
rahmadirizki18 5:3aa203218306 49
rahmadirizki18 5:3aa203218306 50 float K_enc = pi*diaEncoder;
rahmadirizki18 5:3aa203218306 51 float K_robot = pi*diaRobot;
rahmadirizki18 5:3aa203218306 52
rahmadirizki18 5:3aa203218306 53 float speed1=0.6;
rahmadirizki18 5:3aa203218306 54 float speed2=0.6;
rahmadirizki18 5:3aa203218306 55 float speed3=0.6;
rahmadirizki18 5:3aa203218306 56 float speed4=0.6;
franshendri 12:e07c59c28c29 57 float speedB=0.43 ;
franshendri 12:e07c59c28c29 58 float speedL=0.4;
rahmadirizki18 5:3aa203218306 59
rahmadirizki18 5:3aa203218306 60 float KpX=0.5, KpY=0.5, Kp_tetha=0.03;
rahmadirizki18 6:68293bed71ea 61
rahmadirizki18 6:68293bed71ea 62 Timer waktu;
rahmadirizki18 5:3aa203218306 63 //float jarakGlobalY;
rahmadirizki18 5:3aa203218306 64
rahmadirizki18 5:3aa203218306 65 // Deklarasi variabel PID
rahmadirizki18 5:3aa203218306 66 //PID PID(0.992,0.000,0.81,0.001); //(P,I,D, time sampling)
rahmadirizki18 5:3aa203218306 67
rahmadirizki18 5:3aa203218306 68 // Deklarasi variabel encoder
rahmadirizki18 6:68293bed71ea 69 encoderKRAI encoderDepan( PB_14, PB_13, 2000, encoderKRAI::X2_ENCODING); //inA, inB, pin Indeks (NC = tak ada), resolusi, pembacaan
rahmadirizki18 5:3aa203218306 70 encoderKRAI encoderBelakang( PC_4, PB_15, 2000, encoderKRAI::X2_ENCODING);
rahmadirizki18 5:3aa203218306 71 encoderKRAI encoderKanan( PD_2, PC_12, 720, encoderKRAI::X2_ENCODING);
rahmadirizki18 5:3aa203218306 72 encoderKRAI encoderKiri( PC_11, PC_10, 2000, encoderKRAI::X2_ENCODING);
fanny868 0:9072e932503c 73
fanny868 0:9072e932503c 74 // Deklarasi variabel motor
franshendri 10:f0f0dc3904e0 75 Motor motor1(PA_8, PB_0 , PC_15); // pwm, fwd, rev
franshendri 10:f0f0dc3904e0 76 Motor motor2(PA_11, PA_6 ,PA_5); // pwm, fwd, rev
franshendri 10:f0f0dc3904e0 77 Motor motor3(PA_9, PC_2 , PC_3); // pwm, fwd, rev
franshendri 10:f0f0dc3904e0 78 Motor motor4(PA_10, PB_5 ,PB_4); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 79
rahmadirizki18 6:68293bed71ea 80 //Motor Atas
franshendri 10:f0f0dc3904e0 81 Motor motorld(PB_8, PB_1 , PA_13); // pwm, fwd, rev
franshendri 10:f0f0dc3904e0 82 Motor motorlb(PB_9, PA_12, PC_5 ); // pwm, fwd, rev
rahmadirizki18 6:68293bed71ea 83
rahmadirizki18 6:68293bed71ea 84 //Servo Atas
franshendri 10:f0f0dc3904e0 85 Servo servoS(PC_9);
franshendri 10:f0f0dc3904e0 86 Servo servoB(PC_7);
rahmadirizki18 6:68293bed71ea 87
rahmadirizki18 5:3aa203218306 88 // Deklarasi variabel posisi robot
rahmadirizki18 5:3aa203218306 89 //robotPos posisi();
rahmadirizki18 5:3aa203218306 90
rahmadirizki18 5:3aa203218306 91 // Inisialisasi Pin TX-RX Joystik dan PC
rahmadirizki18 5:3aa203218306 92 //Serial pc(PA_0,PA_1);
rahmadirizki18 5:3aa203218306 93 //Serial pc(USBTX,USBRX);
rahmadirizki18 5:3aa203218306 94 // Deklarasi Variabel Global
rahmadirizki18 5:3aa203218306 95 /*
rahmadirizki18 5:3aa203218306 96 * posX dan posY berdasarkan arah robot
rahmadirizki18 5:3aa203218306 97 * encoder Depan & Belakang sejajar sumbu Y
rahmadirizki18 5:3aa203218306 98 * encoder Kanan & Kiri sejajar sumbu X
rahmadirizki18 5:3aa203218306 99 */
rahmadirizki18 5:3aa203218306 100 float jarak, posX, posY;
rahmadirizki18 5:3aa203218306 101 //float keliling = pi*diameterRoda;
rahmadirizki18 5:3aa203218306 102
rahmadirizki18 5:3aa203218306 103 void detect_encoder(float speed);
rahmadirizki18 5:3aa203218306 104 void setCenter();
rahmadirizki18 5:3aa203218306 105 float getY();
rahmadirizki18 5:3aa203218306 106 float getX();
rahmadirizki18 5:3aa203218306 107 float getTetha();
rahmadirizki18 5:3aa203218306 108
fanny868 0:9072e932503c 109 // Inisialisasi Pin TX-RX Joystik dan PC
rahmadirizki18 3:1287fccc11be 110 joysticknucleo joystick(PA_0,PA_1);
rahmadirizki18 3:1287fccc11be 111 Serial pc(USBTX,USBRX);
fanny868 0:9072e932503c 112
rahmadirizki18 5:3aa203218306 113 // Posisi target
rahmadirizki18 5:3aa203218306 114 float XT, YT, Tetha;
rahmadirizki18 5:3aa203218306 115
rahmadirizki18 5:3aa203218306 116 //encoder variable
rahmadirizki18 5:3aa203218306 117 float errX, errY, errT, Vt, Vx, Vy;
rahmadirizki18 5:3aa203218306 118 float V1, V2, V3, V4;
rahmadirizki18 5:3aa203218306 119
fanny868 0:9072e932503c 120 //bool perlambatan=0;
fanny868 0:9072e932503c 121 char case_ger;
rahmadirizki18 3:1287fccc11be 122 bool maju=false,mundur=false,pivka=false,pivki=false,kiri=false,kanan=false,saka=false,saki=false,sbka=false,sbki=false,cw1=false,ccw1=false,cw2=false,ccw2=false,cw3=false,ccw3=false,analog=false;
fanny868 0:9072e932503c 123 bool stop = true;
rahmadirizki18 6:68293bed71ea 124 bool Launcher = false,ServoGo = false;
rahmadirizki18 4:483c07cc22e1 125 float jLX,jLY;
rahmadirizki18 5:3aa203218306 126 double vcurr;
rahmadirizki18 4:483c07cc22e1 127 float x,y; // untuk analoghat kiri
rahmadirizki18 3:1287fccc11be 128 float errorx=0,errory=0;
rahmadirizki18 3:1287fccc11be 129
rahmadirizki18 4:483c07cc22e1 130 // Fungsi mapping 0-255 ke -128 sampai 127
rahmadirizki18 4:483c07cc22e1 131 float mapping(float a,float error)
rahmadirizki18 3:1287fccc11be 132 {
rahmadirizki18 4:483c07cc22e1 133 float hasil,b;
rahmadirizki18 4:483c07cc22e1 134 b = (float)((a-128)/128);
rahmadirizki18 4:483c07cc22e1 135 if (b>(error - 0.2) && b<(error + 0.2))
rahmadirizki18 4:483c07cc22e1 136 {
rahmadirizki18 4:483c07cc22e1 137 hasil = 0;
rahmadirizki18 4:483c07cc22e1 138 } else {
rahmadirizki18 4:483c07cc22e1 139 hasil = b;
rahmadirizki18 4:483c07cc22e1 140 }
rahmadirizki18 3:1287fccc11be 141 return (hasil);
rahmadirizki18 3:1287fccc11be 142 }
fanny868 0:9072e932503c 143
rahmadirizki18 4:483c07cc22e1 144 // Kalibrasi tombol analog kiri
rahmadirizki18 4:483c07cc22e1 145 void kalibrasi()
rahmadirizki18 4:483c07cc22e1 146 {
rahmadirizki18 4:483c07cc22e1 147 errorx = (jLX - 128)/128;
rahmadirizki18 4:483c07cc22e1 148 errory = (jLY - 128)/128;
rahmadirizki18 4:483c07cc22e1 149
rahmadirizki18 4:483c07cc22e1 150 }
rahmadirizki18 4:483c07cc22e1 151
rahmadirizki18 3:1287fccc11be 152 // simpan data analog
rahmadirizki18 3:1287fccc11be 153 void baca_analog()
rahmadirizki18 3:1287fccc11be 154 {
rahmadirizki18 3:1287fccc11be 155 jLX = joystick.LX;
rahmadirizki18 3:1287fccc11be 156 jLY = joystick.LY;
rahmadirizki18 3:1287fccc11be 157
rahmadirizki18 3:1287fccc11be 158 // Pembacaan nilai Y terbalik
rahmadirizki18 4:483c07cc22e1 159 x = mapping(jLX,errorx);
rahmadirizki18 4:483c07cc22e1 160 y = -mapping(jLY,errory);
rahmadirizki18 3:1287fccc11be 161 }
fanny868 0:9072e932503c 162
rahmadirizki18 3:1287fccc11be 163 // Gerak dari Motor base
fanny868 0:9072e932503c 164 int case_gerak()
fanny868 0:9072e932503c 165 {
fanny868 0:9072e932503c 166 int casegerak;
rahmadirizki18 3:1287fccc11be 167 baca_analog();
rahmadirizki18 3:1287fccc11be 168 if (!joystick.L1 && joystick.R1) {
fanny868 0:9072e932503c 169 // Pivot Kanan
fanny868 0:9072e932503c 170 casegerak = 1;
fanny868 0:9072e932503c 171 } else if (!joystick.R1 && joystick.L1) {
fanny868 0:9072e932503c 172 // Pivot Kiri
fanny868 0:9072e932503c 173 casegerak = 2;
rahmadirizki18 3:1287fccc11be 174 } else if ((!joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
fanny868 0:9072e932503c 175 // Maju
rahmadirizki18 3:1287fccc11be 176 casegerak = 3;
rahmadirizki18 3:1287fccc11be 177 } else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
fanny868 0:9072e932503c 178 // Mundur
fanny868 0:9072e932503c 179 casegerak = 4;
rahmadirizki18 3:1287fccc11be 180 } else if ((!joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(!joystick.kiri)&&(joystick.kanan)) {
fanny868 0:9072e932503c 181 // Serong Atas Kanan
fanny868 0:9072e932503c 182 casegerak = 5;
rahmadirizki18 3:1287fccc11be 183 } else if((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(!joystick.kiri)&&(joystick.kanan)) {
fanny868 0:9072e932503c 184 // Serong Bawah Kanan
fanny868 0:9072e932503c 185 casegerak = 6;
rahmadirizki18 3:1287fccc11be 186 } else if ((!joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(joystick.kiri)&&(!joystick.kanan)) {
fanny868 0:9072e932503c 187 // Serong Atas Kiri
fanny868 0:9072e932503c 188 casegerak = 7;
rahmadirizki18 3:1287fccc11be 189 } else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(joystick.kiri)&&(!joystick.kanan)) {
fanny868 0:9072e932503c 190 // Serong Bawah Kiri
fanny868 0:9072e932503c 191 casegerak = 8;
rahmadirizki18 3:1287fccc11be 192 } else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) {
fanny868 0:9072e932503c 193 // Kanan
fanny868 0:9072e932503c 194 casegerak = 9;
rahmadirizki18 3:1287fccc11be 195 } else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) {
fanny868 0:9072e932503c 196 // Kiri
rahmadirizki18 3:1287fccc11be 197 casegerak = 10;
rahmadirizki18 3:1287fccc11be 198 } else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
rahmadirizki18 3:1287fccc11be 199 // case gerak analog on off
rahmadirizki18 3:1287fccc11be 200 if (analog){
rahmadirizki18 3:1287fccc11be 201 casegerak = 11;
rahmadirizki18 3:1287fccc11be 202 } else {
rahmadirizki18 3:1287fccc11be 203 casegerak = 12;
rahmadirizki18 3:1287fccc11be 204 }
rahmadirizki18 3:1287fccc11be 205 }
fanny868 0:9072e932503c 206 return(casegerak);
fanny868 0:9072e932503c 207 }
fanny868 0:9072e932503c 208
fanny868 0:9072e932503c 209
fanny868 0:9072e932503c 210
fanny868 0:9072e932503c 211 /**
fanny868 0:9072e932503c 212
rahmadirizki18 3:1287fccc11be 213 ** Case 1 : Pivot kanan
fanny868 0:9072e932503c 214 ** Case 2 : Pivot Kiri
fanny868 0:9072e932503c 215 ** Case 3 : Maju
fanny868 0:9072e932503c 216 ** Case 4 : Mundur
fanny868 0:9072e932503c 217 ** Case 5 : Serong Atas Kanan
fanny868 0:9072e932503c 218 ** Case 6 : Serong Bawah Kanan
fanny868 0:9072e932503c 219 ** Case 7 : Serong Atas Kiri
fanny868 0:9072e932503c 220 ** Case 8 : Serong Bawah Kiri
fanny868 0:9072e932503c 221 ** Case 9 : Kanan
fanny868 0:9072e932503c 222 ** Case 10 : Kiri
rahmadirizki18 3:1287fccc11be 223 ** Case 11 : Analog
rahmadirizki18 3:1287fccc11be 224 ** Case 11 : break
fanny868 0:9072e932503c 225
fanny868 0:9072e932503c 226 **/
rahmadirizki18 3:1287fccc11be 227
rahmadirizki18 5:3aa203218306 228
rahmadirizki18 3:1287fccc11be 229 // Pergerakan dari base
fanny868 0:9072e932503c 230 void aktuator()
fanny868 0:9072e932503c 231 {
rahmadirizki18 6:68293bed71ea 232 //Servo
rahmadirizki18 7:d138c56dab20 233 if (ServoGo){
franshendri 12:e07c59c28c29 234 servoS.position(20);
franshendri 10:f0f0dc3904e0 235 wait_ms(500);
franshendri 12:e07c59c28c29 236 servoS.position(-28);
Joshua23 8:0711dea61312 237 wait_ms(500);
franshendri 12:e07c59c28c29 238 servoS.position(20);
Joshua23 8:0711dea61312 239 wait_ms(500);
franshendri 12:e07c59c28c29 240 for (int i = -0; i<=70; i++){
Joshua23 8:0711dea61312 241 servoB.position(i);
Joshua23 8:0711dea61312 242 wait_ms(10);
Joshua23 8:0711dea61312 243 }
Joshua23 8:0711dea61312 244 wait_ms(500);
Joshua23 8:0711dea61312 245 servoB.position(0);
rahmadirizki18 7:d138c56dab20 246 ServoGo = false;
rahmadirizki18 6:68293bed71ea 247
rahmadirizki18 6:68293bed71ea 248 }else{
franshendri 12:e07c59c28c29 249 servoS.position(20);
rahmadirizki18 6:68293bed71ea 250 servoB.position(0);
fanny868 0:9072e932503c 251
rahmadirizki18 7:d138c56dab20 252 }
rahmadirizki18 6:68293bed71ea 253
rahmadirizki18 6:68293bed71ea 254 // Motor Atas
rahmadirizki18 6:68293bed71ea 255 if (Launcher) {
rahmadirizki18 7:d138c56dab20 256 motorld.speed(speedL);
franshendri 12:e07c59c28c29 257 motorlb.speed(speedB);
rahmadirizki18 6:68293bed71ea 258 }else{
rahmadirizki18 6:68293bed71ea 259 motorld.speed(0);
rahmadirizki18 6:68293bed71ea 260 motorlb.speed(0);
rahmadirizki18 6:68293bed71ea 261 }
rahmadirizki18 6:68293bed71ea 262
rahmadirizki18 6:68293bed71ea 263 // MOTOR Bawah
fanny868 0:9072e932503c 264 switch (case_ger)
fanny868 0:9072e932503c 265 {
rahmadirizki18 3:1287fccc11be 266 case (1):
fanny868 0:9072e932503c 267 {
rahmadirizki18 5:3aa203218306 268 Tetha = Tetha - 0.5;
fanny868 0:9072e932503c 269 pivka=true;
rahmadirizki18 3:1287fccc11be 270 maju=mundur=analog=kiri=kanan=saka=saki=sbka=sbki=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 271
rahmadirizki18 5:3aa203218306 272 pc.printf("pivKa Xt =%.2f x=%.2f YT=%.2f y=%.2f errx=%.2f erry=%.2f \n",XT,x,YT,y,errX,errY);
fanny868 0:9072e932503c 273
rahmadirizki18 5:3aa203218306 274
fanny868 0:9072e932503c 275 break;
fanny868 0:9072e932503c 276 }
fanny868 0:9072e932503c 277 case (2):
fanny868 0:9072e932503c 278 {
rahmadirizki18 5:3aa203218306 279 Tetha = Tetha + 0.5;
fanny868 0:9072e932503c 280
fanny868 0:9072e932503c 281 pivki=true;
rahmadirizki18 3:1287fccc11be 282 maju=mundur=kiri=analog=kanan=saka=saki=sbka=sbki=pivka=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 283
rahmadirizki18 5:3aa203218306 284 pc.printf("pivKi Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 285
rahmadirizki18 5:3aa203218306 286
fanny868 0:9072e932503c 287 break;
fanny868 0:9072e932503c 288 }
fanny868 0:9072e932503c 289 case (3):
fanny868 0:9072e932503c 290 {
franshendri 10:f0f0dc3904e0 291 YT = YT + 0.1;
fanny868 0:9072e932503c 292
fanny868 0:9072e932503c 293 maju=true;
rahmadirizki18 3:1287fccc11be 294 mundur=kiri=kanan=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 295
rahmadirizki18 5:3aa203218306 296 pc.printf("maju Xt =%.2f x=%.2f YT=%.2f y=%.2f errx=%.2f erry=%.2f \n",XT,x,YT,y,errX,errY);
fanny868 0:9072e932503c 297
fanny868 0:9072e932503c 298 break;
fanny868 0:9072e932503c 299 }
fanny868 0:9072e932503c 300 case (4):
fanny868 0:9072e932503c 301 {
franshendri 10:f0f0dc3904e0 302 YT = YT - 0.1;
fanny868 0:9072e932503c 303
fanny868 0:9072e932503c 304 mundur=true;
rahmadirizki18 3:1287fccc11be 305 maju=kiri=kanan=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 306
rahmadirizki18 5:3aa203218306 307 pc.printf("mundur Xt =%.2f x=%.2f YT=%.2f y=%.2f errx=%.2f erry=%.2f \n",XT,x,YT,y,errX,errY);
fanny868 0:9072e932503c 308
fanny868 0:9072e932503c 309
fanny868 0:9072e932503c 310 break;
fanny868 0:9072e932503c 311 }
fanny868 0:9072e932503c 312 case (5) :
fanny868 0:9072e932503c 313 {
franshendri 10:f0f0dc3904e0 314 XT = XT + 0.1;
franshendri 10:f0f0dc3904e0 315 YT = YT + 0.1;
fanny868 0:9072e932503c 316
fanny868 0:9072e932503c 317 saka=true;
rahmadirizki18 3:1287fccc11be 318 maju=mundur=kiri=kanan=sbka=saki=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 319
rahmadirizki18 5:3aa203218306 320 pc.printf("saka Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 321
fanny868 0:9072e932503c 322
fanny868 0:9072e932503c 323 break;
fanny868 0:9072e932503c 324 }
fanny868 0:9072e932503c 325 case (6) :
fanny868 0:9072e932503c 326 {
franshendri 10:f0f0dc3904e0 327 XT = XT + 0.1;
franshendri 10:f0f0dc3904e0 328 YT = YT - 0.1;
rahmadirizki18 5:3aa203218306 329
fanny868 0:9072e932503c 330
fanny868 0:9072e932503c 331 sbka=true;
rahmadirizki18 3:1287fccc11be 332 maju=mundur=kiri=kanan=saka=saki=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 333
rahmadirizki18 5:3aa203218306 334 pc.printf("sbka Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 335
fanny868 0:9072e932503c 336
fanny868 0:9072e932503c 337 break;
fanny868 0:9072e932503c 338 }
fanny868 0:9072e932503c 339 case (7) :
fanny868 0:9072e932503c 340 {
franshendri 10:f0f0dc3904e0 341 XT = XT - 0.1;
franshendri 10:f0f0dc3904e0 342 YT = YT + 0.1;
rahmadirizki18 5:3aa203218306 343
fanny868 0:9072e932503c 344
fanny868 0:9072e932503c 345 saki=true;
rahmadirizki18 3:1287fccc11be 346 maju=kiri=kanan=saka=mundur=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 347
rahmadirizki18 5:3aa203218306 348 pc.printf("saki Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 349
fanny868 0:9072e932503c 350
fanny868 0:9072e932503c 351 break;
fanny868 0:9072e932503c 352 }
fanny868 0:9072e932503c 353 case (8) :
fanny868 0:9072e932503c 354 {
franshendri 10:f0f0dc3904e0 355 XT = XT - 0.1;
franshendri 10:f0f0dc3904e0 356 YT = YT - 0.1;
rahmadirizki18 5:3aa203218306 357
rahmadirizki18 5:3aa203218306 358 pc.printf("sbki Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 359
fanny868 0:9072e932503c 360
fanny868 0:9072e932503c 361 break;
fanny868 0:9072e932503c 362 }
fanny868 0:9072e932503c 363 case (9) :
fanny868 0:9072e932503c 364 {
franshendri 10:f0f0dc3904e0 365 XT = XT + 0.1;
fanny868 0:9072e932503c 366 kanan=true;
rahmadirizki18 3:1287fccc11be 367 maju=kiri=mundur=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 368
rahmadirizki18 5:3aa203218306 369 pc.printf("Kanan Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 370
fanny868 0:9072e932503c 371 break;
fanny868 0:9072e932503c 372 }
fanny868 0:9072e932503c 373 case (10) :
fanny868 0:9072e932503c 374 {
franshendri 10:f0f0dc3904e0 375 XT = XT - 0.1;
fanny868 0:9072e932503c 376
fanny868 0:9072e932503c 377 kiri=true;
rahmadirizki18 3:1287fccc11be 378 maju=kanan=mundur=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 379
rahmadirizki18 5:3aa203218306 380 pc.printf("Kiri Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 381
fanny868 0:9072e932503c 382
fanny868 0:9072e932503c 383 break;
fanny868 0:9072e932503c 384 }
rahmadirizki18 3:1287fccc11be 385 case (11):
rahmadirizki18 3:1287fccc11be 386 {
rahmadirizki18 4:483c07cc22e1 387
franshendri 10:f0f0dc3904e0 388 XT = XT + 0.1*x;
franshendri 10:f0f0dc3904e0 389 YT = YT + 0.1*y;
rahmadirizki18 3:1287fccc11be 390
rahmadirizki18 3:1287fccc11be 391 analog=true;
rahmadirizki18 3:1287fccc11be 392 maju=mundur=kiri=kanan=saka=saki=sbka=sbki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
rahmadirizki18 3:1287fccc11be 393
rahmadirizki18 5:3aa203218306 394 pc.printf("analog Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
rahmadirizki18 3:1287fccc11be 395
rahmadirizki18 3:1287fccc11be 396 break;
rahmadirizki18 3:1287fccc11be 397 }
rahmadirizki18 3:1287fccc11be 398 default :
fanny868 0:9072e932503c 399 {
rahmadirizki18 5:3aa203218306 400
rahmadirizki18 3:1287fccc11be 401 maju=mundur=kiri=kanan=saka=saki=sbka=sbki=analog=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 402 stop = true;
fanny868 0:9072e932503c 403
fanny868 0:9072e932503c 404 //s1 = 0;s2 =0; s3 =0; s4 =0;
fanny868 0:9072e932503c 405
rahmadirizki18 5:3aa203218306 406 // pc.printf("Stop V1=%.2f V2=%.2f V3=%.2f V4=%.2f\n",V1,V2,V3,V4);
rahmadirizki18 3:1287fccc11be 407 }
rahmadirizki18 5:3aa203218306 408 }
rahmadirizki18 5:3aa203218306 409 }
rahmadirizki18 5:3aa203218306 410
rahmadirizki18 5:3aa203218306 411 //Begin Encoder
rahmadirizki18 5:3aa203218306 412
rahmadirizki18 5:3aa203218306 413 void setCenter()
rahmadirizki18 5:3aa203218306 414 {
rahmadirizki18 5:3aa203218306 415 encoderDepan.reset();
rahmadirizki18 5:3aa203218306 416 encoderBelakang.reset();
rahmadirizki18 5:3aa203218306 417 encoderKanan.reset();
rahmadirizki18 5:3aa203218306 418 encoderKiri.reset();
rahmadirizki18 5:3aa203218306 419 }
rahmadirizki18 5:3aa203218306 420
rahmadirizki18 5:3aa203218306 421 float getX()
rahmadirizki18 5:3aa203218306 422 {
rahmadirizki18 5:3aa203218306 423 float jarakEncDpn, jarakEncBlk;
rahmadirizki18 5:3aa203218306 424 jarakEncDpn = (encoderDepan.getPulses())/(float)(2000.0)*K_enc;
rahmadirizki18 5:3aa203218306 425 jarakEncBlk = (encoderBelakang.getPulses())/(float)(2000.0)*K_enc;
rahmadirizki18 5:3aa203218306 426 return (jarakEncDpn-jarakEncBlk)/2;
rahmadirizki18 5:3aa203218306 427 }
rahmadirizki18 5:3aa203218306 428
rahmadirizki18 5:3aa203218306 429 float getY()
rahmadirizki18 5:3aa203218306 430 {
rahmadirizki18 5:3aa203218306 431 float jarakEncKir, jarakEncKan;
rahmadirizki18 5:3aa203218306 432 jarakEncKir = (encoderKiri.getPulses())/(float)(2000.0)*K_enc;
rahmadirizki18 5:3aa203218306 433 jarakEncKan = (encoderKanan.getPulses())/(float)(720.0)*K_enc;
rahmadirizki18 5:3aa203218306 434 return (jarakEncKir-jarakEncKan)/2;
rahmadirizki18 5:3aa203218306 435 }
rahmadirizki18 5:3aa203218306 436
rahmadirizki18 5:3aa203218306 437 float getTetha()
rahmadirizki18 5:3aa203218306 438 {
rahmadirizki18 5:3aa203218306 439 float busurDpn, busurBlk, busurKir, busurKan;
rahmadirizki18 5:3aa203218306 440 busurDpn = ((encoderDepan.getPulses())/(float)(2000.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 441 busurBlk = ((encoderBelakang.getPulses())/(float)(2000.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 442 busurKir = ((encoderKiri.getPulses())/(float)(2000.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 443 busurKan = ((encoderKanan.getPulses())/(float)(720.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 444
rahmadirizki18 5:3aa203218306 445 return -(busurDpn+busurBlk+busurKir+busurKan)/4;
fanny868 0:9072e932503c 446 }
fanny868 0:9072e932503c 447
rahmadirizki18 5:3aa203218306 448 void gotoXYT(float xa, float ya, float Ta)
rahmadirizki18 5:3aa203218306 449 {
rahmadirizki18 5:3aa203218306 450
rahmadirizki18 5:3aa203218306 451 errX = xa-getX();
rahmadirizki18 5:3aa203218306 452 Vx = KpX*errX;
rahmadirizki18 5:3aa203218306 453
rahmadirizki18 5:3aa203218306 454 errY = ya-getY();
rahmadirizki18 5:3aa203218306 455 Vy = KpY*errY;
rahmadirizki18 5:3aa203218306 456
rahmadirizki18 5:3aa203218306 457 errT = Ta-getTetha();
rahmadirizki18 5:3aa203218306 458 Vt = Kp_tetha*errT;
rahmadirizki18 5:3aa203218306 459
rahmadirizki18 5:3aa203218306 460 V1 = Vx+Vy-Vt;
rahmadirizki18 5:3aa203218306 461 V2 = Vx-Vy-Vt;
rahmadirizki18 5:3aa203218306 462 V3 = -Vx-Vy-Vt;
rahmadirizki18 5:3aa203218306 463 V4 = -Vx+Vy-Vt;
rahmadirizki18 5:3aa203218306 464
rahmadirizki18 5:3aa203218306 465 if (V1>speed1)
rahmadirizki18 5:3aa203218306 466 { V1 = speed1; }
rahmadirizki18 5:3aa203218306 467 else if (V1<-speed1)
rahmadirizki18 5:3aa203218306 468 { V1 = -speed1; }
rahmadirizki18 5:3aa203218306 469
rahmadirizki18 5:3aa203218306 470 if (V2>speed2)
rahmadirizki18 5:3aa203218306 471 { V2 = speed2; }
rahmadirizki18 5:3aa203218306 472 else if (V2<-speed2)
rahmadirizki18 5:3aa203218306 473 { V2 = -speed2; }
rahmadirizki18 5:3aa203218306 474
rahmadirizki18 5:3aa203218306 475 if (V3>speed3)
rahmadirizki18 5:3aa203218306 476 { V3 = speed3; }
rahmadirizki18 5:3aa203218306 477 else if (V3<-speed3)
rahmadirizki18 5:3aa203218306 478 { V3 = -speed3; }
rahmadirizki18 5:3aa203218306 479
rahmadirizki18 5:3aa203218306 480 if (V4>speed4)
rahmadirizki18 5:3aa203218306 481 { V4 = speed4; }
rahmadirizki18 5:3aa203218306 482 else if (V4<-speed4)
rahmadirizki18 5:3aa203218306 483 { V4 = -speed4; }
rahmadirizki18 5:3aa203218306 484
rahmadirizki18 5:3aa203218306 485 if (((errX > 0.05) || (errX<-0.05)) || ((errY > 0.05) || (errY<-0.05)) || ((errT > 0.05) || (errT<-0.05)))
rahmadirizki18 5:3aa203218306 486 {
rahmadirizki18 5:3aa203218306 487 motor1.speed(V1);
rahmadirizki18 5:3aa203218306 488 motor2.speed(V2);
rahmadirizki18 5:3aa203218306 489 motor3.speed(V3);
rahmadirizki18 5:3aa203218306 490 motor4.speed(V4);
rahmadirizki18 5:3aa203218306 491 // pc.printf("V1=%.2f \ ", V1);
rahmadirizki18 5:3aa203218306 492 }
rahmadirizki18 5:3aa203218306 493 else
rahmadirizki18 5:3aa203218306 494 {
rahmadirizki18 5:3aa203218306 495 motor1.brake(1);
rahmadirizki18 5:3aa203218306 496 motor2.brake(1);
rahmadirizki18 5:3aa203218306 497 motor3.brake(1);
rahmadirizki18 5:3aa203218306 498 motor4.brake(1);
rahmadirizki18 5:3aa203218306 499 //_ms(1000);
rahmadirizki18 5:3aa203218306 500 }
rahmadirizki18 5:3aa203218306 501 //pc.printf("%f\t%f\t%f ", errX*100,errY*100,errT);
rahmadirizki18 5:3aa203218306 502 // wait_ms(10);
rahmadirizki18 5:3aa203218306 503
rahmadirizki18 5:3aa203218306 504 }
rahmadirizki18 5:3aa203218306 505 //End Encoder
rahmadirizki18 5:3aa203218306 506
rahmadirizki18 7:d138c56dab20 507 void speedLauncher()
rahmadirizki18 7:d138c56dab20 508 {
franshendri 12:e07c59c28c29 509 if (joystick.R3_click and speedL < 0.8){
franshendri 12:e07c59c28c29 510 speedL = speedL + 0.01;}
rahmadirizki18 7:d138c56dab20 511 if (joystick.L3_click and speedL > 0.1){
franshendri 12:e07c59c28c29 512 speedL = speedL - 0.01;}
franshendri 12:e07c59c28c29 513 /* if (joystick.R2>0 and speedB < 0.8){
franshendri 12:e07c59c28c29 514 speedB = speedB + 0.01;}
franshendri 12:e07c59c28c29 515 if (joystick.L2>0 and speedB > 0.1){
franshendri 12:e07c59c28c29 516 speedB = speedB - 0.01;}*/
franshendri 12:e07c59c28c29 517 pc.printf("Pwm depan = %.3f\n ", speedL);
rahmadirizki18 7:d138c56dab20 518 }
franshendri 12:e07c59c28c29 519
franshendri 12:e07c59c28c29 520
franshendri 12:e07c59c28c29 521
fanny868 0:9072e932503c 522 int main (void)
fanny868 0:9072e932503c 523 {
fanny868 0:9072e932503c 524 // Set baud rate - 115200
fanny868 0:9072e932503c 525 joystick.setup();
rahmadirizki18 3:1287fccc11be 526 pc.baud(115200);
rahmadirizki18 6:68293bed71ea 527 wait_ms(1000);
rahmadirizki18 5:3aa203218306 528 setCenter();
rahmadirizki18 5:3aa203218306 529 wait_ms(500);
rahmadirizki18 5:3aa203218306 530
rahmadirizki18 5:3aa203218306 531 //Posisi Awal
rahmadirizki18 5:3aa203218306 532 XT = 0;
rahmadirizki18 5:3aa203218306 533 YT = 0;
rahmadirizki18 5:3aa203218306 534 Tetha = 0;
rahmadirizki18 3:1287fccc11be 535 pc.printf("Ready...\n");
rahmadirizki18 4:483c07cc22e1 536 kalibrasi();
rahmadirizki18 6:68293bed71ea 537 waktu.start();
fanny868 0:9072e932503c 538 while(1)
fanny868 0:9072e932503c 539 {
fanny868 0:9072e932503c 540 // Interrupt Serial
fanny868 0:9072e932503c 541 joystick.idle();
fanny868 0:9072e932503c 542 if(joystick.readable() ) {
fanny868 0:9072e932503c 543 // Panggil fungsi pembacaan joystik
fanny868 0:9072e932503c 544 joystick.baca_data();
fanny868 0:9072e932503c 545 // Panggil fungsi pengolahan data joystik
fanny868 0:9072e932503c 546 joystick.olah_data();
franshendri 12:e07c59c28c29 547 //pc.printf("%3d %3d\n\r",joystick.R2, joystick.L2);
fanny868 0:9072e932503c 548 //pc.printf("%2x %2x %2x %2x %3d %3d %3d %3d %3d %3d\n\r",joystick.button, joystick.RL, joystick.button_click, joystick.RL_click, joystick.R2, joystick.L2, joystick.RX, joystick.RY, joystick.LX, joystick.LY);
fanny868 0:9072e932503c 549 case_ger = case_gerak();
rahmadirizki18 3:1287fccc11be 550 aktuator();
rahmadirizki18 6:68293bed71ea 551
franshendri 12:e07c59c28c29 552 //pc.printf("bacaS = %.2f\tbacaB = %.2f\n",servoS.read(), servoB.read());
rahmadirizki18 6:68293bed71ea 553
rahmadirizki18 3:1287fccc11be 554 //kalibrasi
rahmadirizki18 4:483c07cc22e1 555 if (joystick.START){
rahmadirizki18 4:483c07cc22e1 556 kalibrasi();}
rahmadirizki18 4:483c07cc22e1 557 // analog switch mode
rahmadirizki18 6:68293bed71ea 558 if (joystick.SELECT_click) {analog = !analog;}
rahmadirizki18 6:68293bed71ea 559 if (joystick.segitiga_click) {Launcher = !Launcher;}
rahmadirizki18 6:68293bed71ea 560 if (joystick.lingkaran_click){
rahmadirizki18 6:68293bed71ea 561 ServoGo = true;
rahmadirizki18 6:68293bed71ea 562 waktu.reset();
rahmadirizki18 6:68293bed71ea 563 }
rahmadirizki18 5:3aa203218306 564 if (joystick.silang) {
rahmadirizki18 5:3aa203218306 565 XT = 0;
rahmadirizki18 5:3aa203218306 566 YT = 0;
rahmadirizki18 5:3aa203218306 567 Tetha = 0;
rahmadirizki18 5:3aa203218306 568 pc.printf("x..\n");
rahmadirizki18 5:3aa203218306 569 }
rahmadirizki18 7:d138c56dab20 570 speedLauncher();
franshendri 12:e07c59c28c29 571
fanny868 0:9072e932503c 572 } else {
fanny868 0:9072e932503c 573 joystick.idle();
fanny868 0:9072e932503c 574
fanny868 0:9072e932503c 575 }
rahmadirizki18 5:3aa203218306 576 gotoXYT(XT,YT,Tetha);
rahmadirizki18 5:3aa203218306 577
fanny868 0:9072e932503c 578 }
fanny868 0:9072e932503c 579 }