Rancha mark_II

Dependencies:   Motor PID Joystick_OrdoV5 mbed millis

Fork of Joystick_ManualBaseBaruV2_enc by KRAI 2017

Committer:
franshendri
Date:
Tue Nov 29 15:15:28 2016 +0000
Revision:
10:f0f0dc3904e0
Parent:
9:5a50782510fb
Child:
12:e07c59c28c29
motor bawah oke

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