bug : pwm full di launcher

Dependencies:   Motor PID Joystick_OrdoV5 mbed millis

Fork of MainProgram_BaseBaru by KRAI 2017

Committer:
Joshua23
Date:
Tue Nov 29 11:51:42 2016 +0000
Revision:
8:0711dea61312
Parent:
7:d138c56dab20
Child:
9:5a50782510fb
Tambahan Servo

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 8:0711dea61312 46 #define pinservo1 PC_7
rahmadirizki18 7:d138c56dab20 47 //PC 9
Joshua23 8:0711dea61312 48 #define pinservo2 PA_0
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;
rahmadirizki18 7:d138c56dab20 57 float speedL=0.2;
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
rahmadirizki18 5:3aa203218306 74 Motor motor1(PB_8, PB_1 , PA_13); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 75 Motor motor2(PB_9, PA_12, PC_5); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 76 Motor motor3(PB_6, PA_7 , PB_12); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 77 Motor motor4(PB_7, PA_14 ,PA_15); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 78
rahmadirizki18 6:68293bed71ea 79 //Motor Atas
rahmadirizki18 6:68293bed71ea 80 Motor motorld(PA_8, PB_0 ,PC_15); // pwm, fwd, rev
rahmadirizki18 7:d138c56dab20 81 Motor motorlb(PA_11, PA_5 ,PA_6); // pwm, fwd, rev
rahmadirizki18 6:68293bed71ea 82
rahmadirizki18 6:68293bed71ea 83 //Servo Atas
rahmadirizki18 6:68293bed71ea 84 Servo servoS(pinservo1);
rahmadirizki18 6:68293bed71ea 85 Servo servoB(pinservo2);
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){
Joshua23 8:0711dea61312 236 servoS.position(-90);
Joshua23 8:0711dea61312 237 wait_ms(500);
Joshua23 8:0711dea61312 238 servoS.position(10);
Joshua23 8:0711dea61312 239 wait_ms(500);
Joshua23 8:0711dea61312 240 for (int i = 0; i<=90; 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{
Joshua23 8:0711dea61312 249 servoS.position(10);
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);
rahmadirizki18 7:d138c56dab20 257 motorlb.speed(speedL);
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 {
rahmadirizki18 5:3aa203218306 291 YT = YT + 0.01;
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 {
rahmadirizki18 5:3aa203218306 302 YT = YT - 0.01;
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 {
rahmadirizki18 5:3aa203218306 314 XT = XT + 0.01;
rahmadirizki18 5:3aa203218306 315 YT = YT + 0.01;
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 {
rahmadirizki18 5:3aa203218306 327 XT = XT + 0.01;
Joshua23 8:0711dea61312 328 YT = YT - 0.01;
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 {
rahmadirizki18 5:3aa203218306 341 XT = XT - 0.01;
rahmadirizki18 5:3aa203218306 342 YT = YT + 0.01;
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 {
rahmadirizki18 5:3aa203218306 355 XT = XT - 0.01;
Joshua23 8:0711dea61312 356 YT = YT - 0.01;
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 {
rahmadirizki18 5:3aa203218306 365 XT = XT + 0.01;
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 {
rahmadirizki18 5:3aa203218306 375 XT = XT - 0.01;
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
rahmadirizki18 5:3aa203218306 388 XT = XT + 0.01*x;
rahmadirizki18 5:3aa203218306 389 YT = YT + 0.01*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 {
rahmadirizki18 7:d138c56dab20 509 if (joystick.R3_click and speedL < 0.7){
rahmadirizki18 7:d138c56dab20 510 speedL = speedL + 0.1;}
rahmadirizki18 7:d138c56dab20 511 if (joystick.L3_click and speedL > 0.1){
rahmadirizki18 7:d138c56dab20 512 speedL = speedL - 0.1;}
rahmadirizki18 7:d138c56dab20 513
rahmadirizki18 7:d138c56dab20 514 }
fanny868 0:9072e932503c 515
fanny868 0:9072e932503c 516 int main (void)
fanny868 0:9072e932503c 517 {
fanny868 0:9072e932503c 518 // Set baud rate - 115200
fanny868 0:9072e932503c 519 joystick.setup();
rahmadirizki18 3:1287fccc11be 520 pc.baud(115200);
rahmadirizki18 6:68293bed71ea 521 wait_ms(1000);
rahmadirizki18 5:3aa203218306 522 setCenter();
rahmadirizki18 5:3aa203218306 523 wait_ms(500);
rahmadirizki18 5:3aa203218306 524
rahmadirizki18 5:3aa203218306 525 //Posisi Awal
rahmadirizki18 5:3aa203218306 526 XT = 0;
rahmadirizki18 5:3aa203218306 527 YT = 0;
rahmadirizki18 5:3aa203218306 528 Tetha = 0;
rahmadirizki18 3:1287fccc11be 529 pc.printf("Ready...\n");
rahmadirizki18 4:483c07cc22e1 530 kalibrasi();
rahmadirizki18 7:d138c56dab20 531 servoS.position(90);
rahmadirizki18 7:d138c56dab20 532 servoB.position(0);
rahmadirizki18 6:68293bed71ea 533 waktu.start();
fanny868 0:9072e932503c 534 while(1)
fanny868 0:9072e932503c 535 {
fanny868 0:9072e932503c 536 // Interrupt Serial
fanny868 0:9072e932503c 537 joystick.idle();
fanny868 0:9072e932503c 538 if(joystick.readable() ) {
fanny868 0:9072e932503c 539 // Panggil fungsi pembacaan joystik
fanny868 0:9072e932503c 540 joystick.baca_data();
fanny868 0:9072e932503c 541 // Panggil fungsi pengolahan data joystik
fanny868 0:9072e932503c 542 joystick.olah_data();
fanny868 0:9072e932503c 543 //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 544 case_ger = case_gerak();
rahmadirizki18 3:1287fccc11be 545 aktuator();
rahmadirizki18 6:68293bed71ea 546
rahmadirizki18 6:68293bed71ea 547 pc.printf("bacaS = %.2f\tbacaB = %.2f\n",servoS.read(), servoB.read());
rahmadirizki18 6:68293bed71ea 548
rahmadirizki18 3:1287fccc11be 549 //kalibrasi
rahmadirizki18 4:483c07cc22e1 550 if (joystick.START){
rahmadirizki18 4:483c07cc22e1 551 kalibrasi();}
rahmadirizki18 4:483c07cc22e1 552 // analog switch mode
rahmadirizki18 6:68293bed71ea 553 if (joystick.SELECT_click) {analog = !analog;}
rahmadirizki18 6:68293bed71ea 554 if (joystick.segitiga_click) {Launcher = !Launcher;}
rahmadirizki18 6:68293bed71ea 555 if (joystick.lingkaran_click){
rahmadirizki18 6:68293bed71ea 556 ServoGo = true;
rahmadirizki18 6:68293bed71ea 557 waktu.reset();
rahmadirizki18 6:68293bed71ea 558 }
rahmadirizki18 5:3aa203218306 559 if (joystick.silang) {
rahmadirizki18 5:3aa203218306 560 XT = 0;
rahmadirizki18 5:3aa203218306 561 YT = 0;
rahmadirizki18 5:3aa203218306 562 Tetha = 0;
rahmadirizki18 5:3aa203218306 563 pc.printf("x..\n");
rahmadirizki18 5:3aa203218306 564 }
rahmadirizki18 7:d138c56dab20 565 speedLauncher();
fanny868 0:9072e932503c 566 } else {
fanny868 0:9072e932503c 567 joystick.idle();
fanny868 0:9072e932503c 568
fanny868 0:9072e932503c 569 }
rahmadirizki18 5:3aa203218306 570 gotoXYT(XT,YT,Tetha);
rahmadirizki18 5:3aa203218306 571
fanny868 0:9072e932503c 572 }
fanny868 0:9072e932503c 573 }