Rancha mark_II

Dependencies:   Motor PID Joystick_OrdoV5 mbed millis

Fork of Joystick_ManualBaseBaruV2_enc by KRAI 2017

Committer:
rahmadirizki18
Date:
Sun Nov 27 09:38:37 2016 +0000
Revision:
6:68293bed71ea
Parent:
5:3aa203218306
Child:
7:d138c56dab20
blm di optimisasi servonya

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
rahmadirizki18 6:68293bed71ea 46 #define pinservo1 PC_9
rahmadirizki18 6:68293bed71ea 47 #define pinservo2 PC_8
rahmadirizki18 5:3aa203218306 48
rahmadirizki18 5:3aa203218306 49 float K_enc = pi*diaEncoder;
rahmadirizki18 5:3aa203218306 50 float K_robot = pi*diaRobot;
rahmadirizki18 5:3aa203218306 51
rahmadirizki18 5:3aa203218306 52 float speed1=0.6;
rahmadirizki18 5:3aa203218306 53 float speed2=0.6;
rahmadirizki18 5:3aa203218306 54 float speed3=0.6;
rahmadirizki18 5:3aa203218306 55 float speed4=0.6;
rahmadirizki18 5:3aa203218306 56
rahmadirizki18 5:3aa203218306 57 float KpX=0.5, KpY=0.5, Kp_tetha=0.03;
rahmadirizki18 6:68293bed71ea 58
rahmadirizki18 6:68293bed71ea 59 Timer waktu;
rahmadirizki18 5:3aa203218306 60 //float jarakGlobalY;
rahmadirizki18 5:3aa203218306 61
rahmadirizki18 5:3aa203218306 62 // Deklarasi variabel PID
rahmadirizki18 5:3aa203218306 63 //PID PID(0.992,0.000,0.81,0.001); //(P,I,D, time sampling)
rahmadirizki18 5:3aa203218306 64
rahmadirizki18 5:3aa203218306 65 // Deklarasi variabel encoder
rahmadirizki18 6:68293bed71ea 66 encoderKRAI encoderDepan( PB_14, PB_13, 2000, encoderKRAI::X2_ENCODING); //inA, inB, pin Indeks (NC = tak ada), resolusi, pembacaan
rahmadirizki18 5:3aa203218306 67 encoderKRAI encoderBelakang( PC_4, PB_15, 2000, encoderKRAI::X2_ENCODING);
rahmadirizki18 5:3aa203218306 68 encoderKRAI encoderKanan( PD_2, PC_12, 720, encoderKRAI::X2_ENCODING);
rahmadirizki18 5:3aa203218306 69 encoderKRAI encoderKiri( PC_11, PC_10, 2000, encoderKRAI::X2_ENCODING);
fanny868 0:9072e932503c 70
fanny868 0:9072e932503c 71 // Deklarasi variabel motor
rahmadirizki18 5:3aa203218306 72 Motor motor1(PB_8, PB_1 , PA_13); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 73 Motor motor2(PB_9, PA_12, PC_5); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 74 Motor motor3(PB_6, PA_7 , PB_12); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 75 Motor motor4(PB_7, PA_14 ,PA_15); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 76
rahmadirizki18 6:68293bed71ea 77 //Motor Atas
rahmadirizki18 6:68293bed71ea 78 Motor motorld(PA_8, PB_0 ,PC_15); // pwm, fwd, rev
rahmadirizki18 6:68293bed71ea 79 Motor motorlb(PA_11, PA_6 ,PA_5); // pwm, fwd, rev
rahmadirizki18 6:68293bed71ea 80
rahmadirizki18 6:68293bed71ea 81 //Servo Atas
rahmadirizki18 6:68293bed71ea 82 Servo servoS(pinservo1);
rahmadirizki18 6:68293bed71ea 83 Servo servoB(pinservo2);
rahmadirizki18 6:68293bed71ea 84
rahmadirizki18 5:3aa203218306 85 // Deklarasi variabel posisi robot
rahmadirizki18 5:3aa203218306 86 //robotPos posisi();
rahmadirizki18 5:3aa203218306 87
rahmadirizki18 5:3aa203218306 88 // Inisialisasi Pin TX-RX Joystik dan PC
rahmadirizki18 5:3aa203218306 89 //Serial pc(PA_0,PA_1);
rahmadirizki18 5:3aa203218306 90 //Serial pc(USBTX,USBRX);
rahmadirizki18 5:3aa203218306 91 // Deklarasi Variabel Global
rahmadirizki18 5:3aa203218306 92 /*
rahmadirizki18 5:3aa203218306 93 * posX dan posY berdasarkan arah robot
rahmadirizki18 5:3aa203218306 94 * encoder Depan & Belakang sejajar sumbu Y
rahmadirizki18 5:3aa203218306 95 * encoder Kanan & Kiri sejajar sumbu X
rahmadirizki18 5:3aa203218306 96 */
rahmadirizki18 5:3aa203218306 97 float jarak, posX, posY;
rahmadirizki18 5:3aa203218306 98 //float keliling = pi*diameterRoda;
rahmadirizki18 5:3aa203218306 99
rahmadirizki18 5:3aa203218306 100 void detect_encoder(float speed);
rahmadirizki18 5:3aa203218306 101 void setCenter();
rahmadirizki18 5:3aa203218306 102 float getY();
rahmadirizki18 5:3aa203218306 103 float getX();
rahmadirizki18 5:3aa203218306 104 float getTetha();
rahmadirizki18 5:3aa203218306 105
rahmadirizki18 5:3aa203218306 106
rahmadirizki18 5:3aa203218306 107
fanny868 0:9072e932503c 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 6:68293bed71ea 233 /*if (ServoGo){
rahmadirizki18 6:68293bed71ea 234 servoS.position(90);
rahmadirizki18 6:68293bed71ea 235 servoB.position(-90);
rahmadirizki18 6:68293bed71ea 236 if (waktu.read_ms()>=15){
rahmadirizki18 6:68293bed71ea 237 pc.printf("Servo samping...");
rahmadirizki18 6:68293bed71ea 238 }else if ( waktu.read()<10) {
rahmadirizki18 6:68293bed71ea 239 // Delay
rahmadirizki18 6:68293bed71ea 240 servoB.position(90);
rahmadirizki18 6:68293bed71ea 241 pc.printf("Servo belakang...");
rahmadirizki18 6:68293bed71ea 242 } else {
rahmadirizki18 6:68293bed71ea 243 // Delay
rahmadirizki18 6:68293bed71ea 244 ServoGo = false;
rahmadirizki18 6:68293bed71ea 245 }
rahmadirizki18 6:68293bed71ea 246
rahmadirizki18 6:68293bed71ea 247 }else{
rahmadirizki18 6:68293bed71ea 248 servoS.position(90);
rahmadirizki18 6:68293bed71ea 249 servoB.position(0);
fanny868 0:9072e932503c 250
rahmadirizki18 6:68293bed71ea 251 }*/
rahmadirizki18 6:68293bed71ea 252
rahmadirizki18 6:68293bed71ea 253 // Motor Atas
rahmadirizki18 6:68293bed71ea 254 if (Launcher) {
rahmadirizki18 6:68293bed71ea 255 motorld.speed(0.2);
rahmadirizki18 6:68293bed71ea 256 motorlb.speed(0.2);
rahmadirizki18 6:68293bed71ea 257 }else{
rahmadirizki18 6:68293bed71ea 258 motorld.speed(0);
rahmadirizki18 6:68293bed71ea 259 motorlb.speed(0);
rahmadirizki18 6:68293bed71ea 260 }
rahmadirizki18 6:68293bed71ea 261
rahmadirizki18 6:68293bed71ea 262 // MOTOR Bawah
fanny868 0:9072e932503c 263 switch (case_ger)
fanny868 0:9072e932503c 264 {
rahmadirizki18 3:1287fccc11be 265 case (1):
fanny868 0:9072e932503c 266 {
rahmadirizki18 5:3aa203218306 267 Tetha = Tetha - 0.5;
fanny868 0:9072e932503c 268 pivka=true;
rahmadirizki18 3:1287fccc11be 269 maju=mundur=analog=kiri=kanan=saka=saki=sbka=sbki=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 270
rahmadirizki18 5:3aa203218306 271 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 272
rahmadirizki18 5:3aa203218306 273
fanny868 0:9072e932503c 274 break;
fanny868 0:9072e932503c 275 }
fanny868 0:9072e932503c 276 case (2):
fanny868 0:9072e932503c 277 {
rahmadirizki18 5:3aa203218306 278 Tetha = Tetha + 0.5;
fanny868 0:9072e932503c 279
fanny868 0:9072e932503c 280 pivki=true;
rahmadirizki18 3:1287fccc11be 281 maju=mundur=kiri=analog=kanan=saka=saki=sbka=sbki=pivka=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 282
rahmadirizki18 5:3aa203218306 283 pc.printf("pivKi Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 284
rahmadirizki18 5:3aa203218306 285
fanny868 0:9072e932503c 286 break;
fanny868 0:9072e932503c 287 }
fanny868 0:9072e932503c 288 case (3):
fanny868 0:9072e932503c 289 {
rahmadirizki18 5:3aa203218306 290 YT = YT + 0.01;
fanny868 0:9072e932503c 291
fanny868 0:9072e932503c 292 maju=true;
rahmadirizki18 3:1287fccc11be 293 mundur=kiri=kanan=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 294
rahmadirizki18 5:3aa203218306 295 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 296
fanny868 0:9072e932503c 297 break;
fanny868 0:9072e932503c 298 }
fanny868 0:9072e932503c 299 case (4):
fanny868 0:9072e932503c 300 {
rahmadirizki18 5:3aa203218306 301 YT = YT - 0.01;
fanny868 0:9072e932503c 302
fanny868 0:9072e932503c 303 mundur=true;
rahmadirizki18 3:1287fccc11be 304 maju=kiri=kanan=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 305
rahmadirizki18 5:3aa203218306 306 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 307
fanny868 0:9072e932503c 308
fanny868 0:9072e932503c 309 break;
fanny868 0:9072e932503c 310 }
fanny868 0:9072e932503c 311 case (5) :
fanny868 0:9072e932503c 312 {
rahmadirizki18 5:3aa203218306 313 XT = XT + 0.01;
rahmadirizki18 5:3aa203218306 314 YT = YT + 0.01;
fanny868 0:9072e932503c 315
fanny868 0:9072e932503c 316 saka=true;
rahmadirizki18 3:1287fccc11be 317 maju=mundur=kiri=kanan=sbka=saki=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 318
rahmadirizki18 5:3aa203218306 319 pc.printf("saka Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 320
fanny868 0:9072e932503c 321
fanny868 0:9072e932503c 322 break;
fanny868 0:9072e932503c 323 }
fanny868 0:9072e932503c 324 case (6) :
fanny868 0:9072e932503c 325 {
rahmadirizki18 5:3aa203218306 326 XT = XT + 0.01;
rahmadirizki18 5:3aa203218306 327 YT = YT - 0.01;
rahmadirizki18 5:3aa203218306 328
fanny868 0:9072e932503c 329
fanny868 0:9072e932503c 330 sbka=true;
rahmadirizki18 3:1287fccc11be 331 maju=mundur=kiri=kanan=saka=saki=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 332
rahmadirizki18 5:3aa203218306 333 pc.printf("sbka Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 334
fanny868 0:9072e932503c 335
fanny868 0:9072e932503c 336 break;
fanny868 0:9072e932503c 337 }
fanny868 0:9072e932503c 338 case (7) :
fanny868 0:9072e932503c 339 {
rahmadirizki18 5:3aa203218306 340 XT = XT - 0.01;
rahmadirizki18 5:3aa203218306 341 YT = YT + 0.01;
rahmadirizki18 5:3aa203218306 342
fanny868 0:9072e932503c 343
fanny868 0:9072e932503c 344 saki=true;
rahmadirizki18 3:1287fccc11be 345 maju=kiri=kanan=saka=mundur=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 346
rahmadirizki18 5:3aa203218306 347 pc.printf("saki Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 348
fanny868 0:9072e932503c 349
fanny868 0:9072e932503c 350 break;
fanny868 0:9072e932503c 351 }
fanny868 0:9072e932503c 352 case (8) :
fanny868 0:9072e932503c 353 {
rahmadirizki18 5:3aa203218306 354 XT = XT - 0.01;
rahmadirizki18 5:3aa203218306 355 YT = YT - 0.01;
rahmadirizki18 5:3aa203218306 356
rahmadirizki18 5:3aa203218306 357 pc.printf("sbki Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 358
fanny868 0:9072e932503c 359
fanny868 0:9072e932503c 360 break;
fanny868 0:9072e932503c 361 }
fanny868 0:9072e932503c 362 case (9) :
fanny868 0:9072e932503c 363 {
rahmadirizki18 5:3aa203218306 364 XT = XT + 0.01;
fanny868 0:9072e932503c 365 kanan=true;
rahmadirizki18 3:1287fccc11be 366 maju=kiri=mundur=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 367
rahmadirizki18 5:3aa203218306 368 pc.printf("Kanan Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 369
fanny868 0:9072e932503c 370 break;
fanny868 0:9072e932503c 371 }
fanny868 0:9072e932503c 372 case (10) :
fanny868 0:9072e932503c 373 {
rahmadirizki18 5:3aa203218306 374 XT = XT - 0.01;
fanny868 0:9072e932503c 375
fanny868 0:9072e932503c 376 kiri=true;
rahmadirizki18 3:1287fccc11be 377 maju=kanan=mundur=saka=saki=sbka=sbki=analog=pivka=pivki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 378
rahmadirizki18 5:3aa203218306 379 pc.printf("Kiri Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
fanny868 0:9072e932503c 380
fanny868 0:9072e932503c 381
fanny868 0:9072e932503c 382 break;
fanny868 0:9072e932503c 383 }
rahmadirizki18 3:1287fccc11be 384 case (11):
rahmadirizki18 3:1287fccc11be 385 {
rahmadirizki18 4:483c07cc22e1 386
rahmadirizki18 5:3aa203218306 387 XT = XT + 0.01*x;
rahmadirizki18 5:3aa203218306 388 YT = YT + 0.01*y;
rahmadirizki18 3:1287fccc11be 389
rahmadirizki18 3:1287fccc11be 390 analog=true;
rahmadirizki18 3:1287fccc11be 391 maju=mundur=kiri=kanan=saka=saki=sbka=sbki=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
rahmadirizki18 3:1287fccc11be 392
rahmadirizki18 5:3aa203218306 393 pc.printf("analog Xt =%.2f x=%.2f YT=%.2f y=%.2f \n",XT,x,YT,y);
rahmadirizki18 3:1287fccc11be 394
rahmadirizki18 3:1287fccc11be 395 break;
rahmadirizki18 3:1287fccc11be 396 }
rahmadirizki18 3:1287fccc11be 397 default :
fanny868 0:9072e932503c 398 {
rahmadirizki18 5:3aa203218306 399
rahmadirizki18 3:1287fccc11be 400 maju=mundur=kiri=kanan=saka=saki=sbka=sbki=analog=cw1=ccw1=cw2=ccw2=cw3=ccw3=false;
fanny868 0:9072e932503c 401 stop = true;
fanny868 0:9072e932503c 402
fanny868 0:9072e932503c 403 //s1 = 0;s2 =0; s3 =0; s4 =0;
fanny868 0:9072e932503c 404
rahmadirizki18 5:3aa203218306 405 // pc.printf("Stop V1=%.2f V2=%.2f V3=%.2f V4=%.2f\n",V1,V2,V3,V4);
rahmadirizki18 3:1287fccc11be 406 }
rahmadirizki18 5:3aa203218306 407 }
rahmadirizki18 5:3aa203218306 408 }
rahmadirizki18 5:3aa203218306 409
rahmadirizki18 5:3aa203218306 410 //Begin Encoder
rahmadirizki18 5:3aa203218306 411
rahmadirizki18 5:3aa203218306 412 void setCenter()
rahmadirizki18 5:3aa203218306 413 {
rahmadirizki18 5:3aa203218306 414 encoderDepan.reset();
rahmadirizki18 5:3aa203218306 415 encoderBelakang.reset();
rahmadirizki18 5:3aa203218306 416 encoderKanan.reset();
rahmadirizki18 5:3aa203218306 417 encoderKiri.reset();
rahmadirizki18 5:3aa203218306 418 }
rahmadirizki18 5:3aa203218306 419
rahmadirizki18 5:3aa203218306 420 float getX()
rahmadirizki18 5:3aa203218306 421 {
rahmadirizki18 5:3aa203218306 422 float jarakEncDpn, jarakEncBlk;
rahmadirizki18 5:3aa203218306 423 jarakEncDpn = (encoderDepan.getPulses())/(float)(2000.0)*K_enc;
rahmadirizki18 5:3aa203218306 424 jarakEncBlk = (encoderBelakang.getPulses())/(float)(2000.0)*K_enc;
rahmadirizki18 5:3aa203218306 425 return (jarakEncDpn-jarakEncBlk)/2;
rahmadirizki18 5:3aa203218306 426 }
rahmadirizki18 5:3aa203218306 427
rahmadirizki18 5:3aa203218306 428 float getY()
rahmadirizki18 5:3aa203218306 429 {
rahmadirizki18 5:3aa203218306 430 float jarakEncKir, jarakEncKan;
rahmadirizki18 5:3aa203218306 431 jarakEncKir = (encoderKiri.getPulses())/(float)(2000.0)*K_enc;
rahmadirizki18 5:3aa203218306 432 jarakEncKan = (encoderKanan.getPulses())/(float)(720.0)*K_enc;
rahmadirizki18 5:3aa203218306 433 return (jarakEncKir-jarakEncKan)/2;
rahmadirizki18 5:3aa203218306 434 }
rahmadirizki18 5:3aa203218306 435
rahmadirizki18 5:3aa203218306 436 float getTetha()
rahmadirizki18 5:3aa203218306 437 {
rahmadirizki18 5:3aa203218306 438 float busurDpn, busurBlk, busurKir, busurKan;
rahmadirizki18 5:3aa203218306 439 busurDpn = ((encoderDepan.getPulses())/(float)(2000.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 440 busurBlk = ((encoderBelakang.getPulses())/(float)(2000.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 441 busurKir = ((encoderKiri.getPulses())/(float)(2000.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 442 busurKan = ((encoderKanan.getPulses())/(float)(720.0)*K_enc)/K_robot*360.0;
rahmadirizki18 5:3aa203218306 443
rahmadirizki18 5:3aa203218306 444 return -(busurDpn+busurBlk+busurKir+busurKan)/4;
fanny868 0:9072e932503c 445 }
fanny868 0:9072e932503c 446
rahmadirizki18 5:3aa203218306 447 void gotoXYT(float xa, float ya, float Ta)
rahmadirizki18 5:3aa203218306 448 {
rahmadirizki18 5:3aa203218306 449
rahmadirizki18 5:3aa203218306 450 errX = xa-getX();
rahmadirizki18 5:3aa203218306 451 Vx = KpX*errX;
rahmadirizki18 5:3aa203218306 452
rahmadirizki18 5:3aa203218306 453 errY = ya-getY();
rahmadirizki18 5:3aa203218306 454 Vy = KpY*errY;
rahmadirizki18 5:3aa203218306 455
rahmadirizki18 5:3aa203218306 456 errT = Ta-getTetha();
rahmadirizki18 5:3aa203218306 457 Vt = Kp_tetha*errT;
rahmadirizki18 5:3aa203218306 458
rahmadirizki18 5:3aa203218306 459 V1 = Vx+Vy-Vt;
rahmadirizki18 5:3aa203218306 460 V2 = Vx-Vy-Vt;
rahmadirizki18 5:3aa203218306 461 V3 = -Vx-Vy-Vt;
rahmadirizki18 5:3aa203218306 462 V4 = -Vx+Vy-Vt;
rahmadirizki18 5:3aa203218306 463
rahmadirizki18 5:3aa203218306 464 if (V1>speed1)
rahmadirizki18 5:3aa203218306 465 { V1 = speed1; }
rahmadirizki18 5:3aa203218306 466 else if (V1<-speed1)
rahmadirizki18 5:3aa203218306 467 { V1 = -speed1; }
rahmadirizki18 5:3aa203218306 468
rahmadirizki18 5:3aa203218306 469 if (V2>speed2)
rahmadirizki18 5:3aa203218306 470 { V2 = speed2; }
rahmadirizki18 5:3aa203218306 471 else if (V2<-speed2)
rahmadirizki18 5:3aa203218306 472 { V2 = -speed2; }
rahmadirizki18 5:3aa203218306 473
rahmadirizki18 5:3aa203218306 474 if (V3>speed3)
rahmadirizki18 5:3aa203218306 475 { V3 = speed3; }
rahmadirizki18 5:3aa203218306 476 else if (V3<-speed3)
rahmadirizki18 5:3aa203218306 477 { V3 = -speed3; }
rahmadirizki18 5:3aa203218306 478
rahmadirizki18 5:3aa203218306 479 if (V4>speed4)
rahmadirizki18 5:3aa203218306 480 { V4 = speed4; }
rahmadirizki18 5:3aa203218306 481 else if (V4<-speed4)
rahmadirizki18 5:3aa203218306 482 { V4 = -speed4; }
rahmadirizki18 5:3aa203218306 483
rahmadirizki18 5:3aa203218306 484 if (((errX > 0.05) || (errX<-0.05)) || ((errY > 0.05) || (errY<-0.05)) || ((errT > 0.05) || (errT<-0.05)))
rahmadirizki18 5:3aa203218306 485 {
rahmadirizki18 5:3aa203218306 486 motor1.speed(V1);
rahmadirizki18 5:3aa203218306 487 motor2.speed(V2);
rahmadirizki18 5:3aa203218306 488 motor3.speed(V3);
rahmadirizki18 5:3aa203218306 489 motor4.speed(V4);
rahmadirizki18 5:3aa203218306 490 // pc.printf("V1=%.2f \ ", V1);
rahmadirizki18 5:3aa203218306 491 }
rahmadirizki18 5:3aa203218306 492 else
rahmadirizki18 5:3aa203218306 493 {
rahmadirizki18 5:3aa203218306 494 motor1.brake(1);
rahmadirizki18 5:3aa203218306 495 motor2.brake(1);
rahmadirizki18 5:3aa203218306 496 motor3.brake(1);
rahmadirizki18 5:3aa203218306 497 motor4.brake(1);
rahmadirizki18 5:3aa203218306 498 //_ms(1000);
rahmadirizki18 5:3aa203218306 499 }
rahmadirizki18 5:3aa203218306 500 //pc.printf("%f\t%f\t%f ", errX*100,errY*100,errT);
rahmadirizki18 5:3aa203218306 501 // wait_ms(10);
rahmadirizki18 5:3aa203218306 502
rahmadirizki18 5:3aa203218306 503 }
rahmadirizki18 5:3aa203218306 504 //End Encoder
rahmadirizki18 5:3aa203218306 505
fanny868 0:9072e932503c 506
fanny868 0:9072e932503c 507 int main (void)
fanny868 0:9072e932503c 508 {
fanny868 0:9072e932503c 509 // Set baud rate - 115200
fanny868 0:9072e932503c 510 joystick.setup();
rahmadirizki18 3:1287fccc11be 511 pc.baud(115200);
rahmadirizki18 6:68293bed71ea 512 wait_ms(1000);
rahmadirizki18 5:3aa203218306 513 setCenter();
rahmadirizki18 5:3aa203218306 514 wait_ms(500);
rahmadirizki18 5:3aa203218306 515
rahmadirizki18 5:3aa203218306 516 //Posisi Awal
rahmadirizki18 5:3aa203218306 517 XT = 0;
rahmadirizki18 5:3aa203218306 518 YT = 0;
rahmadirizki18 5:3aa203218306 519 Tetha = 0;
rahmadirizki18 3:1287fccc11be 520 pc.printf("Ready...\n");
rahmadirizki18 4:483c07cc22e1 521 kalibrasi();
rahmadirizki18 6:68293bed71ea 522 //servoS.position(90);
rahmadirizki18 6:68293bed71ea 523 //servoB.position(0);
rahmadirizki18 6:68293bed71ea 524 waktu.start();
fanny868 0:9072e932503c 525 while(1)
fanny868 0:9072e932503c 526 {
fanny868 0:9072e932503c 527 // Interrupt Serial
fanny868 0:9072e932503c 528 joystick.idle();
fanny868 0:9072e932503c 529 if(joystick.readable() ) {
fanny868 0:9072e932503c 530 // Panggil fungsi pembacaan joystik
fanny868 0:9072e932503c 531 joystick.baca_data();
fanny868 0:9072e932503c 532 // Panggil fungsi pengolahan data joystik
fanny868 0:9072e932503c 533 joystick.olah_data();
fanny868 0:9072e932503c 534 //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 535 case_ger = case_gerak();
rahmadirizki18 3:1287fccc11be 536 aktuator();
rahmadirizki18 6:68293bed71ea 537
rahmadirizki18 6:68293bed71ea 538 pc.printf("bacaS = %.2f\tbacaB = %.2f\n",servoS.read(), servoB.read());
rahmadirizki18 6:68293bed71ea 539
rahmadirizki18 3:1287fccc11be 540 //kalibrasi
rahmadirizki18 4:483c07cc22e1 541 if (joystick.START){
rahmadirizki18 4:483c07cc22e1 542 kalibrasi();}
rahmadirizki18 4:483c07cc22e1 543 // analog switch mode
rahmadirizki18 6:68293bed71ea 544 if (joystick.SELECT_click) {analog = !analog;}
rahmadirizki18 6:68293bed71ea 545 if (joystick.segitiga_click) {Launcher = !Launcher;}
rahmadirizki18 6:68293bed71ea 546 if (joystick.lingkaran_click){
rahmadirizki18 6:68293bed71ea 547 ServoGo = true;
rahmadirizki18 6:68293bed71ea 548 waktu.reset();
rahmadirizki18 6:68293bed71ea 549 }
rahmadirizki18 5:3aa203218306 550 if (joystick.silang) {
rahmadirizki18 5:3aa203218306 551 XT = 0;
rahmadirizki18 5:3aa203218306 552 YT = 0;
rahmadirizki18 5:3aa203218306 553 Tetha = 0;
rahmadirizki18 5:3aa203218306 554 pc.printf("x..\n");
rahmadirizki18 5:3aa203218306 555 }
rahmadirizki18 6:68293bed71ea 556
fanny868 0:9072e932503c 557 } else {
fanny868 0:9072e932503c 558 joystick.idle();
fanny868 0:9072e932503c 559
fanny868 0:9072e932503c 560 }
rahmadirizki18 5:3aa203218306 561 gotoXYT(XT,YT,Tetha);
rahmadirizki18 5:3aa203218306 562
fanny868 0:9072e932503c 563 }
fanny868 0:9072e932503c 564 }