Fix pisan inimah plis jangan revisi ultimate mantep

Dependencies:   DigitDisplay Motor PID mbed millis

Fork of DagonFly__RoadToJapan_15Mei by KRAI 2017

Committer:
Najib_irvani
Date:
Sat May 13 05:47:10 2017 +0000
Revision:
47:6cac4f1a3c8e
Parent:
46:85169ae8659b
Child:
48:3006386dc8df
road to japan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Najib_irvani 47:6cac4f1a3c8e 1 /*tuning motor baru untuk konstanta pid baru */
rahmadirizki18 5:3aa203218306 2 /****************************************************************************/
rahmadirizki18 5:3aa203218306 3 /* PROGRAM UNTUK PID CLOSED LOOP */
rahmadirizki18 5:3aa203218306 4 /* */
Najib_irvani 47:6cac4f1a3c8e 5 /* Last Update : 16 April 2017 */
gustavaditya 31:d5cbda07fd95 6 /* */
rahmadirizki18 5:3aa203218306 7 /* - Digunakan encoder autonics */
rahmadirizki18 5:3aa203218306 8 /* - Konfigurasi Motor dan Encoder sbb : */
MarchioKevin 22:4632f58461e0 9 /* ______________________ */
MarchioKevin 22:4632f58461e0 10 /* / \ Rode Depan Belakang: */
MarchioKevin 22:4632f58461e0 11 /* / 2 (Belakang) \ Omniwheel */
MarchioKevin 22:4632f58461e0 12 /* | | */
Najib_irvani 43:3807a95aa284 13 /* | 3 (kiri) 4 (kanan) | Roda Kiri Kanan: */
Najib_irvani 43:3807a95aa284 14 /* | | Omniwheel */
MarchioKevin 22:4632f58461e0 15 /* \ 1 (Depan) / */
MarchioKevin 22:4632f58461e0 16 /* \______________________/ Putaran berlawanan arah */
MarchioKevin 22:4632f58461e0 17 /* jarum jam positif */
rahmadirizki18 5:3aa203218306 18 /* SETTINGS (WAJIB!) : */
rahmadirizki18 5:3aa203218306 19 /* 1. Settings Pin Encoder, Resolusi, dan Tipe encoding di omniPos.h */
rahmadirizki18 5:3aa203218306 20 /* 2. Deklarasi penggunaan library omniPos pada bagian deklarasi encoder */
rahmadirizki18 5:3aa203218306 21 /* */
rahmadirizki18 5:3aa203218306 22 /****************************************************************************/
rahmadirizki18 6:68293bed71ea 23 /* */
MarchioKevin 20:54dc93e7b016 24 /* Joystick */
Sufa 30:d69cc27ac644 25 /* Kanan => */
Sufa 30:d69cc27ac644 26 /* Kiri => */
rahmadirizki18 6:68293bed71ea 27 /* */
Najib_irvani 44:452c214d9cf5 28 /* Tombol silang => Pneumatik aktif */
Sufa 30:d69cc27ac644 29 /* Tombol segitiga => Aktif motor Launcher */
Najib_irvani 44:452c214d9cf5 30 /* Tombol lingkaran => Reloader naik */
Najib_irvani 44:452c214d9cf5 31 /* Tombol kotak => Reloader turun */
Sufa 30:d69cc27ac644 32 /* Tombol L1 => Pivot Kiri */
Sufa 30:d69cc27ac644 33 /* Tombol R1 => Pivot Kanan */
Najib_irvani 44:452c214d9cf5 34 /* Tombol L2 => Kurang PWM Motor Launcher */
Najib_irvani 44:452c214d9cf5 35 /* Tombol R2 => Tambah PWM Motor Launcher */
rahmadirizki18 13:8ab42383a2ca 36 /* */
calmantara186 16:90119f03c5d1 37 /* Bismillahirahmanirrahim */
MarchioKevin 20:54dc93e7b016 38 /* Jagalah Kebersihan Kodingan */
rahmadirizki18 6:68293bed71ea 39 /****************************************************************************/
rahmadirizki18 6:68293bed71ea 40
fanny868 0:9072e932503c 41 #include "mbed.h"
fanny868 0:9072e932503c 42 #include "JoystickPS3.h"
fanny868 0:9072e932503c 43 #include "Motor.h"
rahmadirizki18 5:3aa203218306 44 #include "encoderKRAI.h"
be_bryan 26:256160a1a82d 45 #include "millis.h"
gustavaditya 40:5b937cac959a 46 #include "Ping.h"
franshendri 42:6caf8bd5abbc 47 #include "DigitDisplay.h"
franshendri 42:6caf8bd5abbc 48
calmantara186 16:90119f03c5d1 49 /***********************************************/
calmantara186 16:90119f03c5d1 50 /* Konstanta dan Variabel */
calmantara186 16:90119f03c5d1 51 /***********************************************/
calmantara186 16:90119f03c5d1 52 #define PI 3.14159265
be_bryan 26:256160a1a82d 53 #define D_ENCODER 10 // Diameter Roda Encoder
be_bryan 26:256160a1a82d 54 #define D_ROBOT 80 // Diameter Roda Robot
rahmadirizki18 5:3aa203218306 55
Najib_irvani 46:85169ae8659b 56 // Variable Atas
Najib_irvani 46:85169ae8659b 57 // indek 2 untuk motor depan, 1 untuk motor belakang
gustavaditya 38:3ef6754bd8d8 58 double speed, speed2;
Najib_irvani 47:6cac4f1a3c8e 59 const double maxSpeed = 0.95, minSpeed = -0.95, Ts = 12.5;
Najib_irvani 46:85169ae8659b 60 const double kpA1=0.1478, kdA1=0.9295, kiA1=0.0004226;
Najib_irvani 47:6cac4f1a3c8e 61 const double kpA2=0.13978, kdA2=0.9222, kiA2=0.00038636;
Najib_irvani 46:85169ae8659b 62 double a1,b1,c1;
Najib_irvani 46:85169ae8659b 63 double a2,b2,c2;
Najib_irvani 46:85169ae8659b 64 double current_error1, previous_error1_1 = 0, previous_error1_2 = 0;
Najib_irvani 46:85169ae8659b 65 double current_error2, previous_error2_1 = 0, previous_error2_2 = 0;
Najib_irvani 46:85169ae8659b 66 double previous_speed1 = 0;
Najib_irvani 46:85169ae8659b 67 double previous_speed2 = 0;
Najib_irvani 46:85169ae8659b 68
gustavaditya 38:3ef6754bd8d8 69 float rpm, rpm2;
Najib_irvani 47:6cac4f1a3c8e 70 double target_rpm = 18.0, target_rpm2 = 18.0; // selisih 4 bagus, sama bagus
Najib_irvani 46:85169ae8659b 71 const float maxRPM = 35, minRPM = 0; // Limit 25 atau 27
gustavaditya 38:3ef6754bd8d8 72
Najib_irvani 44:452c214d9cf5 73 const float pwmPowerUp = 1.0;
Najib_irvani 44:452c214d9cf5 74 const float pwmPowerDown = -1.0;
gustavaditya 40:5b937cac959a 75
Najib_irvani 46:85169ae8659b 76 double jarak_ping=0;
Najib_irvani 47:6cac4f1a3c8e 77 double ping_target = 14;
Najib_irvani 46:85169ae8659b 78 double ping_current_error, ping_previous_error1 = 0, ping_sum_error=0;
Najib_irvani 47:6cac4f1a3c8e 79 double ping_Kp = -0.2747, ping_Kd =0, ping_Ts=10;
Najib_irvani 46:85169ae8659b 80 double ping_pwm, ping_previous_pwm = 0;
Najib_irvani 46:85169ae8659b 81
be_bryan 26:256160a1a82d 82 // Variable Bawah
Najib_irvani 44:452c214d9cf5 83 float PIVOT = 0.17; // PWM Pivot Kanan, Pivot Kiri
Najib_irvani 46:85169ae8659b 84 float tuneDpn = 1.0; // Tunning PWM motor Depan
Najib_irvani 46:85169ae8659b 85 float tuneBlk = 1.0; // Tunning PWM motor belakang
gustavaditya 45:964ae71a30e3 86 float tuneAksel = 0.6;
gustavaditya 45:964ae71a30e3 87 int aksel = 0;
gustavaditya 45:964ae71a30e3 88 float tuneAkselBlk = 0.4;
Najib_irvani 46:85169ae8659b 89 float tuneR = 1.0;
Najib_irvani 46:85169ae8659b 90 float tuneL = 1.0;
Najib_irvani 44:452c214d9cf5 91 float serong = 0.4;
Najib_irvani 47:6cac4f1a3c8e 92 float rasio = 1.4545;
Najib_irvani 47:6cac4f1a3c8e 93 float t_new = 0.1;
Najib_irvani 43:3807a95aa284 94
Najib_irvani 43:3807a95aa284 95 /* variable tunning */
Najib_irvani 43:3807a95aa284 96 float tunning_L;
Najib_irvani 43:3807a95aa284 97 float tunning_R;
Najib_irvani 43:3807a95aa284 98 float tunning_Dpn;
Najib_irvani 43:3807a95aa284 99 float tunning_Blk;
gustavaditya 31:d5cbda07fd95 100
gustavaditya 31:d5cbda07fd95 101 /* Deklarasi Variable Millis */
gustavaditya 33:69d266bc3fe9 102 unsigned long int previousMillis = 0; // PID launcher
gustavaditya 31:d5cbda07fd95 103 unsigned long int currentMillis;
gustavaditya 33:69d266bc3fe9 104 unsigned long int previousMillis2 = 0; // PID launcher
gustavaditya 31:d5cbda07fd95 105 unsigned long int currentMillis2;
gustavaditya 33:69d266bc3fe9 106 unsigned long int previousMillis3 = 0; // Pneumatik
gustavaditya 40:5b937cac959a 107 unsigned long int previousMillis4 = 0; // Ping
franshendri 42:6caf8bd5abbc 108 unsigned long int previousMillis5 = 0; // Display
MarchioKevin 22:4632f58461e0 109
gustavaditya 31:d5cbda07fd95 110 /* Variabel Stick */
gustavaditya 31:d5cbda07fd95 111 //Logic untuk masuk aktuator
gustavaditya 31:d5cbda07fd95 112 int case_joy;
gustavaditya 31:d5cbda07fd95 113 bool isLauncher = false;
gustavaditya 31:d5cbda07fd95 114 bool isReload = false;
rizqicahyo 36:5963c9a49485 115 bool ReloadOn = false;
gustavaditya 31:d5cbda07fd95 116 bool flag_Pneu = false;
Najib_irvani 47:6cac4f1a3c8e 117 bool flag_paku = false;
Najib_irvani 44:452c214d9cf5 118 bool ready = false;
gustavaditya 31:d5cbda07fd95 119
gustavaditya 31:d5cbda07fd95 120 /*****************************************************/
gustavaditya 31:d5cbda07fd95 121 /* Definisi Prosedur, Fungsi dan Setting Pinout */
gustavaditya 31:d5cbda07fd95 122 /*****************************************************/
gustavaditya 31:d5cbda07fd95 123
gustavaditya 31:d5cbda07fd95 124 /* Fungsi dan Procedur Encoder */
gustavaditya 31:d5cbda07fd95 125 void init_speed(); //
gustavaditya 31:d5cbda07fd95 126 void aktuator(); // Pergerakan aktuator berdasarkan case joystick
gustavaditya 31:d5cbda07fd95 127 int case_joystick(); // Mendapatkan case dari joystick
gustavaditya 33:69d266bc3fe9 128 //void speedKalibrasiMotor(); // Pertambahan target RPM motor atas melalui joystick
gustavaditya 31:d5cbda07fd95 129
gustavaditya 31:d5cbda07fd95 130 /* Inisialisasi Pin TX-RX Joystik dan PC */
gustavaditya 31:d5cbda07fd95 131 joysticknucleo joystick(PA_0,PA_1);
gustavaditya 31:d5cbda07fd95 132 Serial pc(USBTX,USBRX);
calmantara186 16:90119f03c5d1 133
be_bryan 26:256160a1a82d 134 /* Deklarasi Encoder Base */
gustavaditya 31:d5cbda07fd95 135 encoderKRAI encoderBase(PC_4, PB_15, 2000, encoderKRAI::X2_ENCODING); //inA, inB, pin Indeks (NC = tak ada), 2xresolusi, mode pembacaan
rahmadirizki18 15:98f0d56b14f0 136
be_bryan 26:256160a1a82d 137 /* Deklarasi Encoder Launcher */
Najib_irvani 44:452c214d9cf5 138 encoderKRAI encLauncherDpn( PC_10, PC_11, 28, encoderKRAI::X4_ENCODING);
Najib_irvani 44:452c214d9cf5 139 encoderKRAI encLauncherBlk( PC_12, PD_2, 28, encoderKRAI::X4_ENCODING);
be_bryan 26:256160a1a82d 140
calmantara186 16:90119f03c5d1 141 /* Deklarasi Motor Base */
Najib_irvani 44:452c214d9cf5 142 Motor motorDpn(PB_7, PC_3, PC_0); //(PB_9, PA_12, PC_5);
Najib_irvani 44:452c214d9cf5 143 //Motor motorBlk(PB_6, PC_14, PC_13); //(PB_6, PB_1, PB_12); (PC_7, PC_14, PC_13);
Najib_irvani 44:452c214d9cf5 144 Motor motorBlk(PB_2, PB_15, PB_1);
Najib_irvani 44:452c214d9cf5 145 Motor motorL (PB_9, PA_12, PA_6);
Najib_irvani 46:85169ae8659b 146 Motor motorR (PB_8, PC_6, PC_5); //(PC_6, PB_4, PB_5);
fanny868 0:9072e932503c 147
calmantara186 16:90119f03c5d1 148 /* Deklarasi Motor Launcher */
Najib_irvani 47:6cac4f1a3c8e 149 Motor launcherDpn(PA_5,PA_11,PB_12); // pwm ,fwd, rev
Najib_irvani 44:452c214d9cf5 150 Motor launcherBlk(PB_3, PC_4, PA_10); // pwm, fwd, rev
Najib_irvani 44:452c214d9cf5 151 Motor powerScrew(PB_10, PB_14, PB_13); // pwm, fwd, rev
rahmadirizki18 5:3aa203218306 152
be_bryan 26:256160a1a82d 153 /* Deklarasi Penumatik Launcher */
Najib_irvani 44:452c214d9cf5 154 DigitalOut pneumatik(PA_4, PullUp);
Najib_irvani 47:6cac4f1a3c8e 155 DigitalOut pneu_paku(PC_2, PullUp);
be_bryan 26:256160a1a82d 156
be_bryan 27:68efb1622985 157 /*Dekalrasi Limit Switch */
gustavaditya 40:5b937cac959a 158 //DigitalIn infraAtas(PC_9, PullUp);
Najib_irvani 44:452c214d9cf5 159 DigitalIn limitTengah(PA_9, PullUp);
Najib_irvani 44:452c214d9cf5 160 DigitalIn limitBawah(PC_7, PullUp);
Najib_irvani 44:452c214d9cf5 161 DigitalIn limitBawah1(PA_7, PullUp);
rahmadirizki18 5:3aa203218306 162
gustavaditya 40:5b937cac959a 163 /*deklarasi PING ultrasonic*/
Najib_irvani 44:452c214d9cf5 164 Ping pingAtas(PC_15);
fanny868 0:9072e932503c 165
franshendri 42:6caf8bd5abbc 166 /*Deklarasi Display*/
Najib_irvani 44:452c214d9cf5 167 DigitDisplay display (PA_8, PC_8);
franshendri 42:6caf8bd5abbc 168
MarchioKevin 22:4632f58461e0 169 /****************************************************/
MarchioKevin 22:4632f58461e0 170 /* Deklarasi Fungsi dan Procedure */
MarchioKevin 22:4632f58461e0 171 /****************************************************/
gustavaditya 31:d5cbda07fd95 172 int case_joystick()
gustavaditya 31:d5cbda07fd95 173 {
gustavaditya 31:d5cbda07fd95 174 //---------------------------------------------------//
gustavaditya 31:d5cbda07fd95 175 // Gerak Motor Base //
gustavaditya 31:d5cbda07fd95 176 // Case 1 : Pivot kanan //
gustavaditya 31:d5cbda07fd95 177 // Case 2 : Pivot Kiri //
gustavaditya 31:d5cbda07fd95 178 // Case 3 : Kanan //
gustavaditya 31:d5cbda07fd95 179 // Case 4 : Kiri //
gustavaditya 31:d5cbda07fd95 180 // Case 5 : Break //
gustavaditya 31:d5cbda07fd95 181 //---------------------------------------------------//
gustavaditya 31:d5cbda07fd95 182
gustavaditya 31:d5cbda07fd95 183 int caseJoystick;
Najib_irvani 43:3807a95aa284 184 if ((joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
gustavaditya 31:d5cbda07fd95 185 // Pivot Kanan
gustavaditya 31:d5cbda07fd95 186 caseJoystick = 1;
gustavaditya 31:d5cbda07fd95 187 }
Najib_irvani 43:3807a95aa284 188 else if ((!joystick.R1)&&(joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
gustavaditya 31:d5cbda07fd95 189 // Pivot Kiri
gustavaditya 31:d5cbda07fd95 190 caseJoystick = 2;
Najib_irvani 43:3807a95aa284 191 }
Najib_irvani 44:452c214d9cf5 192 else if ((joystick.START_click)&&(!joystick.SELECT_click)&&(!joystick.R3_click)) {
Najib_irvani 44:452c214d9cf5 193 // tambah rpm dengan nilai tertentu
Najib_irvani 44:452c214d9cf5 194 caseJoystick = 31;
Najib_irvani 44:452c214d9cf5 195 }
Najib_irvani 44:452c214d9cf5 196 else if ((!joystick.START_click)&&(joystick.SELECT_click)&&(!joystick.R3_click)) {
Najib_irvani 44:452c214d9cf5 197 // kurangi rpm dengan nilai tertentu
Najib_irvani 44:452c214d9cf5 198 caseJoystick = 32;
Najib_irvani 44:452c214d9cf5 199 }
Najib_irvani 44:452c214d9cf5 200 else if ((!joystick.START_click)&&(!joystick.SELECT_click)&&(joystick.R3_click)) {
Najib_irvani 44:452c214d9cf5 201 // kurangi rpm dengan nilai tertentu
Najib_irvani 44:452c214d9cf5 202 caseJoystick = 33;
Najib_irvani 44:452c214d9cf5 203 }
Najib_irvani 43:3807a95aa284 204 else if ((joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 205 // Kanan + Rotasi kanan
Najib_irvani 43:3807a95aa284 206 caseJoystick = 17;
Najib_irvani 43:3807a95aa284 207 }
Najib_irvani 43:3807a95aa284 208 else if ((!joystick.R1)&&(joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 209 // Kanan + Rotasi kiri
Najib_irvani 43:3807a95aa284 210 caseJoystick = 18;
Najib_irvani 43:3807a95aa284 211 }
Najib_irvani 43:3807a95aa284 212 else if ((joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) {
Najib_irvani 43:3807a95aa284 213 // Kiri + Rotasi kanan
Najib_irvani 43:3807a95aa284 214 caseJoystick = 19;
Najib_irvani 43:3807a95aa284 215 }
Najib_irvani 43:3807a95aa284 216 else if ((!joystick.R1)&&(joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) {
Najib_irvani 43:3807a95aa284 217 // Kanan + Rotasi kiri
Najib_irvani 43:3807a95aa284 218 caseJoystick = 20;
Najib_irvani 43:3807a95aa284 219 }
Najib_irvani 43:3807a95aa284 220 else if ((joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 221 // Maju + Rotasi kanan
Najib_irvani 43:3807a95aa284 222 caseJoystick = 21;
Najib_irvani 43:3807a95aa284 223 }
Najib_irvani 43:3807a95aa284 224 else if ((!joystick.R1)&&(joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 225 // Maju + Rotasi kiri
Najib_irvani 43:3807a95aa284 226 caseJoystick = 22;
Najib_irvani 43:3807a95aa284 227 }
Najib_irvani 43:3807a95aa284 228 else if ((joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 229 // Mundur + Rotasi kanan
Najib_irvani 43:3807a95aa284 230 caseJoystick = 23;
Najib_irvani 43:3807a95aa284 231 }
Najib_irvani 43:3807a95aa284 232 else if ((!joystick.R1)&&(joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 233 // Mundur + Rotasi kiri
Najib_irvani 43:3807a95aa284 234 caseJoystick = 24;
Najib_irvani 43:3807a95aa284 235 }
Najib_irvani 43:3807a95aa284 236 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)&&(joystick.segitiga_click)) {
Najib_irvani 43:3807a95aa284 237 // Kanan + segitiga
Najib_irvani 43:3807a95aa284 238 caseJoystick = 25;
Najib_irvani 43:3807a95aa284 239 }
Najib_irvani 43:3807a95aa284 240 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)&&(joystick.segitiga_click)) {
Najib_irvani 43:3807a95aa284 241 // Kiri + segitiga
Najib_irvani 43:3807a95aa284 242 caseJoystick = 26;
Najib_irvani 43:3807a95aa284 243 }
Najib_irvani 43:3807a95aa284 244 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)&&(joystick.lingkaran_click)) {
Najib_irvani 43:3807a95aa284 245 // Kanan + lingkaran
Najib_irvani 43:3807a95aa284 246 caseJoystick = 27;
Najib_irvani 43:3807a95aa284 247 }
Najib_irvani 43:3807a95aa284 248 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)&&(joystick.lingkaran_click)) {
Najib_irvani 43:3807a95aa284 249 // Kiri + lingkaran
Najib_irvani 43:3807a95aa284 250 caseJoystick = 28;
Najib_irvani 43:3807a95aa284 251 }
Najib_irvani 43:3807a95aa284 252 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)&&(joystick.kotak_click)) {
Najib_irvani 43:3807a95aa284 253 // Kanan + kotak
Najib_irvani 43:3807a95aa284 254 caseJoystick = 29;
Najib_irvani 43:3807a95aa284 255 }
Najib_irvani 43:3807a95aa284 256 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)&&(joystick.kotak_click)) {
Najib_irvani 43:3807a95aa284 257 // Kiri + kotak
Najib_irvani 43:3807a95aa284 258 caseJoystick = 30;
Najib_irvani 43:3807a95aa284 259 }
Najib_irvani 43:3807a95aa284 260 else if ((!joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 261 // Serong kanan maju
Najib_irvani 44:452c214d9cf5 262 caseJoystick = 13;
Najib_irvani 43:3807a95aa284 263 }
Najib_irvani 43:3807a95aa284 264 else if ((!joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) {
Najib_irvani 43:3807a95aa284 265 // Serong kiri maju
Najib_irvani 44:452c214d9cf5 266 caseJoystick = 14;
Najib_irvani 43:3807a95aa284 267 }
Najib_irvani 43:3807a95aa284 268 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 269 // Serong kanan mundur
Najib_irvani 44:452c214d9cf5 270 caseJoystick = 15;
Najib_irvani 43:3807a95aa284 271 }
Najib_irvani 43:3807a95aa284 272 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) {
Najib_irvani 43:3807a95aa284 273 // Serong kiri mundur
Najib_irvani 44:452c214d9cf5 274 caseJoystick = 16;
gustavaditya 31:d5cbda07fd95 275 }
gustavaditya 31:d5cbda07fd95 276 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) {
gustavaditya 31:d5cbda07fd95 277 // Kanan
gustavaditya 31:d5cbda07fd95 278 caseJoystick = 3;
gustavaditya 31:d5cbda07fd95 279 }
gustavaditya 31:d5cbda07fd95 280 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) {
gustavaditya 31:d5cbda07fd95 281 // Kiri
gustavaditya 31:d5cbda07fd95 282 caseJoystick = 4;
Najib_irvani 43:3807a95aa284 283 }
Najib_irvani 43:3807a95aa284 284 else if ((!joystick.R1)&&(!joystick.L1)&&(joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 285 // Atas -- Maju
Najib_irvani 44:452c214d9cf5 286 caseJoystick = 8;
Najib_irvani 43:3807a95aa284 287 }
Najib_irvani 43:3807a95aa284 288 else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) {
Najib_irvani 43:3807a95aa284 289 // Bawah -- Mundur
Najib_irvani 44:452c214d9cf5 290 caseJoystick = 9;
gustavaditya 31:d5cbda07fd95 291 }
gustavaditya 38:3ef6754bd8d8 292 else if (joystick.segitiga_click){
gustavaditya 31:d5cbda07fd95 293 // Motor Launcher
gustavaditya 31:d5cbda07fd95 294 caseJoystick = 5;
gustavaditya 31:d5cbda07fd95 295 }
gustavaditya 38:3ef6754bd8d8 296 else if (joystick.R2_click){
gustavaditya 31:d5cbda07fd95 297 // Target Pulse PID ++ Motor Depan
gustavaditya 31:d5cbda07fd95 298 caseJoystick = 6;
gustavaditya 31:d5cbda07fd95 299 }
gustavaditya 38:3ef6754bd8d8 300 else if (joystick.L2_click){
Najib_irvani 43:3807a95aa284 301 // Target Pulse PID -- Motor
gustavaditya 31:d5cbda07fd95 302 caseJoystick = 7;
gustavaditya 31:d5cbda07fd95 303 }
gustavaditya 31:d5cbda07fd95 304 else if (joystick.silang_click){
gustavaditya 31:d5cbda07fd95 305 // Pnemuatik ON
gustavaditya 31:d5cbda07fd95 306 caseJoystick = 10;
gustavaditya 31:d5cbda07fd95 307 }
Najib_irvani 43:3807a95aa284 308 else if ((joystick.lingkaran_click)&&(!joystick.kotak_click)) {
gustavaditya 31:d5cbda07fd95 309 // Power Screw Up
gustavaditya 31:d5cbda07fd95 310 caseJoystick = 11;
gustavaditya 31:d5cbda07fd95 311 }
Najib_irvani 43:3807a95aa284 312 else if ((joystick.kotak_click)&&(!joystick.lingkaran_click)) {
gustavaditya 31:d5cbda07fd95 313 // Power Screw Down
gustavaditya 31:d5cbda07fd95 314 caseJoystick = 12;
gustavaditya 31:d5cbda07fd95 315 }
Najib_irvani 47:6cac4f1a3c8e 316 else if (joystick.L3){
Najib_irvani 47:6cac4f1a3c8e 317 // Paku Bumi ON/OFF
Najib_irvani 47:6cac4f1a3c8e 318 caseJoystick = 34;
Najib_irvani 47:6cac4f1a3c8e 319 }
gustavaditya 45:964ae71a30e3 320 else
gustavaditya 45:964ae71a30e3 321 {
gustavaditya 45:964ae71a30e3 322 tuneAksel = 0.6;
gustavaditya 45:964ae71a30e3 323 aksel = 0;
gustavaditya 45:964ae71a30e3 324 }
gustavaditya 31:d5cbda07fd95 325
gustavaditya 31:d5cbda07fd95 326 return(caseJoystick);
gustavaditya 31:d5cbda07fd95 327 }
gustavaditya 31:d5cbda07fd95 328
fanny868 0:9072e932503c 329
gustavaditya 37:67d54563af90 330 void init_rpm (){
gustavaditya 38:3ef6754bd8d8 331 if (target_rpm>maxRPM-2){
gustavaditya 38:3ef6754bd8d8 332 target_rpm = maxRPM-2;
gustavaditya 37:67d54563af90 333 }
franshendri 42:6caf8bd5abbc 334 if (target_rpm<minRPM){
gustavaditya 37:67d54563af90 335 target_rpm = minRPM;
gustavaditya 37:67d54563af90 336 }
gustavaditya 37:67d54563af90 337 if (target_rpm2>maxRPM){
gustavaditya 37:67d54563af90 338 target_rpm2 = maxRPM;
gustavaditya 37:67d54563af90 339 }
franshendri 42:6caf8bd5abbc 340 if (target_rpm2<minRPM+2){
gustavaditya 38:3ef6754bd8d8 341 target_rpm2 = minRPM+2;
gustavaditya 37:67d54563af90 342 }
gustavaditya 37:67d54563af90 343 }
gustavaditya 37:67d54563af90 344
gustavaditya 31:d5cbda07fd95 345 void aktuator()
gustavaditya 31:d5cbda07fd95 346 {
gustavaditya 31:d5cbda07fd95 347 switch (case_joy) {
gustavaditya 31:d5cbda07fd95 348 case (1):
gustavaditya 31:d5cbda07fd95 349 {
gustavaditya 31:d5cbda07fd95 350 // Pivot Kanan
gustavaditya 31:d5cbda07fd95 351 motorDpn.speed(-PIVOT);
gustavaditya 31:d5cbda07fd95 352 motorBlk.speed(-PIVOT);
Najib_irvani 47:6cac4f1a3c8e 353 motorR.speed(-rasio*PIVOT-t_new);
Najib_irvani 47:6cac4f1a3c8e 354 motorL.speed(-rasio*PIVOT-t_new);
gustavaditya 31:d5cbda07fd95 355 break;
gustavaditya 31:d5cbda07fd95 356 }
gustavaditya 31:d5cbda07fd95 357 case (2):
gustavaditya 31:d5cbda07fd95 358 {
gustavaditya 31:d5cbda07fd95 359 // Pivot Kiri
gustavaditya 31:d5cbda07fd95 360 motorDpn.speed(PIVOT);
gustavaditya 31:d5cbda07fd95 361 motorBlk.speed(PIVOT);
Najib_irvani 47:6cac4f1a3c8e 362 motorR.speed(rasio*PIVOT+t_new);
Najib_irvani 47:6cac4f1a3c8e 363 motorL.speed(rasio*PIVOT+t_new);
Najib_irvani 43:3807a95aa284 364 break;
Najib_irvani 43:3807a95aa284 365 }
Najib_irvani 43:3807a95aa284 366 case (17):
Najib_irvani 43:3807a95aa284 367 {
Najib_irvani 43:3807a95aa284 368 // Kanan + Rotasi Kanan
Najib_irvani 43:3807a95aa284 369 motorDpn.speed(-PIVOT);
Najib_irvani 43:3807a95aa284 370 motorBlk.speed(-PIVOT);
Najib_irvani 47:6cac4f1a3c8e 371 motorR.speed(-rasio*PIVOT-t_new);
Najib_irvani 47:6cac4f1a3c8e 372 motorL.speed(-rasio*PIVOT-t_new);
Najib_irvani 43:3807a95aa284 373 break;
Najib_irvani 43:3807a95aa284 374 }
Najib_irvani 43:3807a95aa284 375 case (18):
Najib_irvani 43:3807a95aa284 376 {
Najib_irvani 43:3807a95aa284 377 // Kanan + Rotasi Kiri
Najib_irvani 43:3807a95aa284 378 motorDpn.speed(PIVOT);
Najib_irvani 43:3807a95aa284 379 motorBlk.speed(PIVOT);
Najib_irvani 47:6cac4f1a3c8e 380 motorR.speed(rasio*PIVOT+t_new);
Najib_irvani 47:6cac4f1a3c8e 381 motorL.speed(rasio*PIVOT+t_new);
Najib_irvani 43:3807a95aa284 382 break;
Najib_irvani 43:3807a95aa284 383 }
Najib_irvani 43:3807a95aa284 384 case (19):
Najib_irvani 43:3807a95aa284 385 {
Najib_irvani 43:3807a95aa284 386 // Kiri + Rotasi Kanan
Najib_irvani 43:3807a95aa284 387 motorDpn.speed(-PIVOT);
Najib_irvani 43:3807a95aa284 388 motorBlk.speed(-PIVOT);
Najib_irvani 47:6cac4f1a3c8e 389 motorR.speed(-rasio*PIVOT-t_new);
Najib_irvani 47:6cac4f1a3c8e 390 motorL.speed(-rasio*PIVOT-t_new);
Najib_irvani 43:3807a95aa284 391 break;
Najib_irvani 43:3807a95aa284 392 }
Najib_irvani 43:3807a95aa284 393 case (20):
Najib_irvani 43:3807a95aa284 394 {
Najib_irvani 43:3807a95aa284 395 // Kiri + Rotasi Kiri
Najib_irvani 43:3807a95aa284 396 motorDpn.speed(PIVOT);
Najib_irvani 43:3807a95aa284 397 motorBlk.speed(PIVOT);
Najib_irvani 47:6cac4f1a3c8e 398 motorR.speed(rasio*PIVOT+t_new);
Najib_irvani 47:6cac4f1a3c8e 399 motorL.speed(rasio*PIVOT+t_new);
Najib_irvani 43:3807a95aa284 400 break;
Najib_irvani 43:3807a95aa284 401 }
Najib_irvani 43:3807a95aa284 402 case (21):
Najib_irvani 43:3807a95aa284 403 {
Najib_irvani 43:3807a95aa284 404 // Maju + Rotasi Kanan
Najib_irvani 43:3807a95aa284 405 motorDpn.speed(-PIVOT);
Najib_irvani 43:3807a95aa284 406 motorBlk.speed(-PIVOT);
Najib_irvani 47:6cac4f1a3c8e 407 motorR.speed(-rasio*PIVOT-t_new);
Najib_irvani 47:6cac4f1a3c8e 408 motorL.speed(-rasio*PIVOT-t_new);
Najib_irvani 43:3807a95aa284 409 break;
Najib_irvani 43:3807a95aa284 410 }
Najib_irvani 43:3807a95aa284 411 case (22):
Najib_irvani 43:3807a95aa284 412 {
Najib_irvani 43:3807a95aa284 413 // Maju + Rotasi Kiri
Najib_irvani 43:3807a95aa284 414 motorDpn.speed(PIVOT);
Najib_irvani 43:3807a95aa284 415 motorBlk.speed(PIVOT);
Najib_irvani 47:6cac4f1a3c8e 416 motorR.speed(rasio*PIVOT+t_new);
Najib_irvani 47:6cac4f1a3c8e 417 motorL.speed(rasio*PIVOT+t_new);
Najib_irvani 43:3807a95aa284 418 break;
Najib_irvani 43:3807a95aa284 419 }
Najib_irvani 43:3807a95aa284 420 case (23):
Najib_irvani 43:3807a95aa284 421 {
Najib_irvani 43:3807a95aa284 422 // Mundur + Rotasi Kanan
Najib_irvani 43:3807a95aa284 423 motorDpn.speed(-PIVOT);
Najib_irvani 43:3807a95aa284 424 motorBlk.speed(-PIVOT);
Najib_irvani 47:6cac4f1a3c8e 425 motorR.speed(-rasio*PIVOT-t_new);
Najib_irvani 47:6cac4f1a3c8e 426 motorL.speed(-rasio*PIVOT-t_new);
Najib_irvani 43:3807a95aa284 427 break;
Najib_irvani 43:3807a95aa284 428 }
Najib_irvani 43:3807a95aa284 429 case (24):
Najib_irvani 43:3807a95aa284 430 {
Najib_irvani 43:3807a95aa284 431 // Mundur + Rotasi Kiri
Najib_irvani 43:3807a95aa284 432 motorDpn.speed(PIVOT);
Najib_irvani 43:3807a95aa284 433 motorBlk.speed(PIVOT);
Najib_irvani 47:6cac4f1a3c8e 434 motorR.speed(rasio*PIVOT+t_new);
Najib_irvani 47:6cac4f1a3c8e 435 motorL.speed(rasio*PIVOT+t_new);
Najib_irvani 43:3807a95aa284 436 break;
Najib_irvani 43:3807a95aa284 437 }
Najib_irvani 43:3807a95aa284 438 case (25):
Najib_irvani 43:3807a95aa284 439 {
Najib_irvani 43:3807a95aa284 440 // Kanan + segitiga
Najib_irvani 43:3807a95aa284 441 isLauncher = !isLauncher;
Najib_irvani 43:3807a95aa284 442 break;
Najib_irvani 43:3807a95aa284 443 }
Najib_irvani 43:3807a95aa284 444 case (26):
Najib_irvani 43:3807a95aa284 445 {
Najib_irvani 43:3807a95aa284 446 // Kiri + segitiga
Najib_irvani 43:3807a95aa284 447 isLauncher = !isLauncher;
Najib_irvani 43:3807a95aa284 448 break;
Najib_irvani 43:3807a95aa284 449 }
Najib_irvani 43:3807a95aa284 450 case (27):
Najib_irvani 43:3807a95aa284 451 {
Najib_irvani 43:3807a95aa284 452 // Kanan + lingkaran
Najib_irvani 43:3807a95aa284 453 ReloadOn = !ReloadOn;
Najib_irvani 43:3807a95aa284 454 isReload = false;
Najib_irvani 43:3807a95aa284 455 break;
Najib_irvani 43:3807a95aa284 456 }
Najib_irvani 43:3807a95aa284 457 case (28):
Najib_irvani 43:3807a95aa284 458 {
Najib_irvani 43:3807a95aa284 459 // Kiri + lingkaran
Najib_irvani 43:3807a95aa284 460 ReloadOn = !ReloadOn;
Najib_irvani 43:3807a95aa284 461 isReload = false;
Najib_irvani 43:3807a95aa284 462 break;
Najib_irvani 43:3807a95aa284 463 }
Najib_irvani 43:3807a95aa284 464 case (29):
Najib_irvani 43:3807a95aa284 465 {
Najib_irvani 43:3807a95aa284 466 // Kanan + kotak
Najib_irvani 43:3807a95aa284 467 ReloadOn = !ReloadOn;
Najib_irvani 43:3807a95aa284 468 isReload = true;
Najib_irvani 43:3807a95aa284 469 break;
Najib_irvani 43:3807a95aa284 470 }
Najib_irvani 43:3807a95aa284 471 case (30):
Najib_irvani 43:3807a95aa284 472 {
Najib_irvani 43:3807a95aa284 473 // Kiri + kotak
Najib_irvani 43:3807a95aa284 474 ReloadOn = !ReloadOn;
Najib_irvani 43:3807a95aa284 475 isReload = true;
Najib_irvani 43:3807a95aa284 476 break;
Najib_irvani 43:3807a95aa284 477 }
Najib_irvani 43:3807a95aa284 478 case (13) :
Najib_irvani 43:3807a95aa284 479 {
Najib_irvani 43:3807a95aa284 480 // Serong kanan maju
Najib_irvani 43:3807a95aa284 481 motorDpn.speed(-serong);
Najib_irvani 46:85169ae8659b 482 motorL.speed(-serong-t_new);
Najib_irvani 43:3807a95aa284 483 motorBlk.speed(serong);
Najib_irvani 46:85169ae8659b 484 motorR.speed(serong+t_new);
Najib_irvani 43:3807a95aa284 485 break;
Najib_irvani 43:3807a95aa284 486 }
Najib_irvani 43:3807a95aa284 487 case (14) :
Najib_irvani 43:3807a95aa284 488 {
Najib_irvani 43:3807a95aa284 489 // Serong kiri maju
Najib_irvani 43:3807a95aa284 490 motorDpn.speed(serong);
Najib_irvani 46:85169ae8659b 491 motorR.speed(serong+t_new);
Najib_irvani 43:3807a95aa284 492 motorBlk.speed(-serong);
Najib_irvani 46:85169ae8659b 493 motorL.speed(-serong-t_new);
Najib_irvani 43:3807a95aa284 494 break;
Najib_irvani 43:3807a95aa284 495 }
Najib_irvani 43:3807a95aa284 496 case (15) :
Najib_irvani 43:3807a95aa284 497 {
Najib_irvani 43:3807a95aa284 498 // Serong kanan mundur
Najib_irvani 44:452c214d9cf5 499 motorDpn.speed(-serong);
Najib_irvani 46:85169ae8659b 500 motorR.speed(-serong-t_new);
Najib_irvani 43:3807a95aa284 501 motorBlk.speed(serong);
Najib_irvani 46:85169ae8659b 502 motorL.speed(serong+t_new);
Najib_irvani 43:3807a95aa284 503 break;
Najib_irvani 43:3807a95aa284 504 }
Najib_irvani 43:3807a95aa284 505 case (16) :
Najib_irvani 43:3807a95aa284 506 {
Najib_irvani 43:3807a95aa284 507 // Serong kiri mundur
Najib_irvani 43:3807a95aa284 508 motorDpn.speed(serong);
Najib_irvani 46:85169ae8659b 509 motorL.speed(serong+t_new);
Najib_irvani 43:3807a95aa284 510 motorBlk.speed(-serong);
Najib_irvani 46:85169ae8659b 511 motorR.speed(-serong-t_new);
gustavaditya 31:d5cbda07fd95 512 break;
gustavaditya 31:d5cbda07fd95 513 }
gustavaditya 31:d5cbda07fd95 514 case (3) :
gustavaditya 31:d5cbda07fd95 515 {
gustavaditya 31:d5cbda07fd95 516 // Kanan
gustavaditya 45:964ae71a30e3 517 aksel++;
gustavaditya 45:964ae71a30e3 518 if (aksel>300)
gustavaditya 45:964ae71a30e3 519 tuneAksel = 0.9;
gustavaditya 45:964ae71a30e3 520
gustavaditya 45:964ae71a30e3 521 motorDpn.speed(-tuneAksel);
gustavaditya 45:964ae71a30e3 522 motorBlk.speed(tuneAksel);
Najib_irvani 43:3807a95aa284 523 motorR.brake(1);
Najib_irvani 43:3807a95aa284 524 motorL.brake(1);
gustavaditya 31:d5cbda07fd95 525 break;
gustavaditya 31:d5cbda07fd95 526 }
gustavaditya 31:d5cbda07fd95 527 case (4) :
gustavaditya 31:d5cbda07fd95 528 {
gustavaditya 31:d5cbda07fd95 529 // Kiri
gustavaditya 45:964ae71a30e3 530 aksel++;
gustavaditya 45:964ae71a30e3 531 if (aksel>300)
gustavaditya 45:964ae71a30e3 532 tuneAksel = 0.9;
gustavaditya 45:964ae71a30e3 533
gustavaditya 45:964ae71a30e3 534 motorDpn.speed(tuneAksel);
gustavaditya 45:964ae71a30e3 535 motorBlk.speed(-tuneAksel);
Najib_irvani 43:3807a95aa284 536 motorR.brake(1);
Najib_irvani 43:3807a95aa284 537 motorL.brake(1);
Najib_irvani 43:3807a95aa284 538 break;
Najib_irvani 43:3807a95aa284 539 }
Najib_irvani 43:3807a95aa284 540 case (8) :
Najib_irvani 43:3807a95aa284 541 {
Najib_irvani 43:3807a95aa284 542 // Maju
gustavaditya 45:964ae71a30e3 543 aksel++;
gustavaditya 45:964ae71a30e3 544 if (aksel>300)
gustavaditya 45:964ae71a30e3 545 tuneAksel = 0.9;
gustavaditya 45:964ae71a30e3 546
Najib_irvani 47:6cac4f1a3c8e 547 motorR.speed(tuneAksel);
Najib_irvani 47:6cac4f1a3c8e 548 motorL.speed(-tuneAksel);
Najib_irvani 43:3807a95aa284 549 motorDpn.brake(1);
Najib_irvani 43:3807a95aa284 550 motorBlk.brake(1);
Najib_irvani 43:3807a95aa284 551 break;
Najib_irvani 43:3807a95aa284 552 }
Najib_irvani 43:3807a95aa284 553 case (9) :
Najib_irvani 43:3807a95aa284 554 {
Najib_irvani 43:3807a95aa284 555 // Mundur
gustavaditya 45:964ae71a30e3 556 aksel++;
gustavaditya 45:964ae71a30e3 557 if (aksel>300)
gustavaditya 45:964ae71a30e3 558 tuneAksel = 0.9;
gustavaditya 45:964ae71a30e3 559
Najib_irvani 47:6cac4f1a3c8e 560 motorR.speed(-tuneAksel);
Najib_irvani 47:6cac4f1a3c8e 561 motorL.speed(tuneAksel);
Najib_irvani 43:3807a95aa284 562 motorDpn.brake(1);
Najib_irvani 43:3807a95aa284 563 motorBlk.brake(1);
gustavaditya 31:d5cbda07fd95 564 break;
gustavaditya 31:d5cbda07fd95 565 }
gustavaditya 31:d5cbda07fd95 566 case (5) :
gustavaditya 31:d5cbda07fd95 567 {
gustavaditya 31:d5cbda07fd95 568 // Aktifkan motor atas
gustavaditya 31:d5cbda07fd95 569 isLauncher = !isLauncher;
gustavaditya 31:d5cbda07fd95 570 break;
Joshua23 8:0711dea61312 571 }
gustavaditya 31:d5cbda07fd95 572 case (6) :
gustavaditya 31:d5cbda07fd95 573 {
gustavaditya 31:d5cbda07fd95 574 // Target Pulse PID ++ Motor Depan
gustavaditya 39:11358f3f61ff 575 target_rpm2 = target_rpm2+1.0;
gustavaditya 39:11358f3f61ff 576 target_rpm = target_rpm+1.0;
gustavaditya 37:67d54563af90 577 init_rpm();
gustavaditya 31:d5cbda07fd95 578 break;
gustavaditya 31:d5cbda07fd95 579 }
gustavaditya 31:d5cbda07fd95 580 case (7) :
gustavaditya 31:d5cbda07fd95 581 {
gustavaditya 31:d5cbda07fd95 582 // Target Pulse PID -- Motor Depan
gustavaditya 39:11358f3f61ff 583 target_rpm2 = target_rpm2-1.0;
gustavaditya 39:11358f3f61ff 584 target_rpm = target_rpm-1.0;
gustavaditya 37:67d54563af90 585 init_rpm();
gustavaditya 31:d5cbda07fd95 586 break;
gustavaditya 31:d5cbda07fd95 587 }
gustavaditya 31:d5cbda07fd95 588 case (10) :
gustavaditya 31:d5cbda07fd95 589 {
gustavaditya 31:d5cbda07fd95 590 // Pneumatik
Najib_irvani 44:452c214d9cf5 591 if (ready)
Najib_irvani 44:452c214d9cf5 592 {
Najib_irvani 44:452c214d9cf5 593 pneumatik = 0;
Najib_irvani 44:452c214d9cf5 594 previousMillis3 = millis();
Najib_irvani 44:452c214d9cf5 595 flag_Pneu = true;
Najib_irvani 44:452c214d9cf5 596 ready = false;
gustavaditya 45:964ae71a30e3 597
Najib_irvani 44:452c214d9cf5 598 }
gustavaditya 31:d5cbda07fd95 599 break;
gustavaditya 31:d5cbda07fd95 600 }
gustavaditya 31:d5cbda07fd95 601 case (11) :
gustavaditya 31:d5cbda07fd95 602 {
gustavaditya 31:d5cbda07fd95 603 // Power Screw Up
gustavaditya 40:5b937cac959a 604 ReloadOn = !ReloadOn;
Najib_irvani 43:3807a95aa284 605 isReload = false;
gustavaditya 31:d5cbda07fd95 606 break;
gustavaditya 31:d5cbda07fd95 607 }
Najib_irvani 44:452c214d9cf5 608 case (31) :
Najib_irvani 44:452c214d9cf5 609 {
Najib_irvani 44:452c214d9cf5 610 // start
Najib_irvani 47:6cac4f1a3c8e 611 target_rpm2 = 23;
Najib_irvani 47:6cac4f1a3c8e 612 target_rpm = 23;
Najib_irvani 44:452c214d9cf5 613 init_rpm();
Najib_irvani 44:452c214d9cf5 614 break;
Najib_irvani 44:452c214d9cf5 615 }
Najib_irvani 44:452c214d9cf5 616 case (32) :
Najib_irvani 44:452c214d9cf5 617 {
Najib_irvani 44:452c214d9cf5 618 // select
Najib_irvani 46:85169ae8659b 619 target_rpm2 = 11;
Najib_irvani 46:85169ae8659b 620 target_rpm = 11;
Najib_irvani 44:452c214d9cf5 621 init_rpm();
Najib_irvani 44:452c214d9cf5 622 break;
Najib_irvani 44:452c214d9cf5 623 }
Najib_irvani 44:452c214d9cf5 624 case (33) :
Najib_irvani 44:452c214d9cf5 625 {
Najib_irvani 44:452c214d9cf5 626 // R3
Najib_irvani 47:6cac4f1a3c8e 627 target_rpm2 = 18;
Najib_irvani 47:6cac4f1a3c8e 628 target_rpm = 18;
Najib_irvani 44:452c214d9cf5 629 init_rpm();
Najib_irvani 44:452c214d9cf5 630 break;
Najib_irvani 44:452c214d9cf5 631 }
gustavaditya 31:d5cbda07fd95 632 case (12) :
gustavaditya 31:d5cbda07fd95 633 {
gustavaditya 31:d5cbda07fd95 634 // Power Screw Down
Najib_irvani 43:3807a95aa284 635 ReloadOn = !ReloadOn;
Najib_irvani 43:3807a95aa284 636 isReload = true;
gustavaditya 31:d5cbda07fd95 637 break;
gustavaditya 31:d5cbda07fd95 638 }
Najib_irvani 47:6cac4f1a3c8e 639 case (34) :{
Najib_irvani 47:6cac4f1a3c8e 640 pneu_paku = !pneu_paku;
Najib_irvani 47:6cac4f1a3c8e 641 wait_ms(50);
Najib_irvani 47:6cac4f1a3c8e 642 if (pneu_paku == 1){
Najib_irvani 47:6cac4f1a3c8e 643 PIVOT = 0.17;
Najib_irvani 47:6cac4f1a3c8e 644 }else{
Najib_irvani 47:6cac4f1a3c8e 645 PIVOT = 0.8;
Najib_irvani 47:6cac4f1a3c8e 646 }
Najib_irvani 47:6cac4f1a3c8e 647 }
gustavaditya 31:d5cbda07fd95 648 default :
gustavaditya 31:d5cbda07fd95 649 {
gustavaditya 45:964ae71a30e3 650 tuneAksel = 0.6;
gustavaditya 45:964ae71a30e3 651 aksel = 0;
gustavaditya 31:d5cbda07fd95 652 motorDpn.brake(1);
gustavaditya 31:d5cbda07fd95 653 motorBlk.brake(1);
Najib_irvani 43:3807a95aa284 654 motorR.brake(1);
Najib_irvani 43:3807a95aa284 655 motorL.brake(1);
gustavaditya 31:d5cbda07fd95 656 }
gustavaditya 31:d5cbda07fd95 657 } // End Switch
gustavaditya 31:d5cbda07fd95 658 }
gustavaditya 31:d5cbda07fd95 659
gustavaditya 40:5b937cac959a 660 void reloader()
rizqicahyo 36:5963c9a49485 661 {
rizqicahyo 36:5963c9a49485 662 if(ReloadOn){
rizqicahyo 36:5963c9a49485 663 if(isReload){
rizqicahyo 36:5963c9a49485 664 powerScrew.speed(pwmPowerDown);
Najib_irvani 47:6cac4f1a3c8e 665 pc.printf("%.2f\n", jarak_ping);
Najib_irvani 44:452c214d9cf5 666 if((!limitBawah)||(!limitBawah1)){
rizqicahyo 36:5963c9a49485 667 isReload = false;
rizqicahyo 36:5963c9a49485 668 ReloadOn = false;
rizqicahyo 36:5963c9a49485 669 }
rizqicahyo 36:5963c9a49485 670 }
rizqicahyo 36:5963c9a49485 671 else if(!limitTengah){
rizqicahyo 36:5963c9a49485 672 isReload = true;
rizqicahyo 36:5963c9a49485 673 }
Najib_irvani 46:85169ae8659b 674 else if(!flag_Pneu){
Najib_irvani 46:85169ae8659b 675 //pc.printf("%.2f\n", ping_pwm);
Najib_irvani 46:85169ae8659b 676 ping_current_error = (double) (ping_target-jarak_ping);
Najib_irvani 46:85169ae8659b 677
Najib_irvani 46:85169ae8659b 678 ping_sum_error += ping_current_error*ping_Ts;
Najib_irvani 46:85169ae8659b 679 ping_pwm = (double) ping_Kp*ping_current_error + ping_Kd*(ping_current_error-ping_previous_error1)/ping_Ts;
Najib_irvani 46:85169ae8659b 680
Najib_irvani 47:6cac4f1a3c8e 681 pc.printf("%.2f\n", jarak_ping);
Najib_irvani 46:85169ae8659b 682 powerScrew.speed(ping_pwm);
Najib_irvani 46:85169ae8659b 683
Najib_irvani 46:85169ae8659b 684 ping_previous_error1 = ping_current_error;
gustavaditya 40:5b937cac959a 685 }
Najib_irvani 47:6cac4f1a3c8e 686 if ((jarak_ping>(ping_target-2))&&(jarak_ping<(ping_target+2))){
Najib_irvani 46:85169ae8659b 687 ready = true;
Najib_irvani 46:85169ae8659b 688 }else{
Najib_irvani 44:452c214d9cf5 689 ready = false;
rizqicahyo 36:5963c9a49485 690 }
rizqicahyo 36:5963c9a49485 691 }
rizqicahyo 36:5963c9a49485 692 else{
rizqicahyo 36:5963c9a49485 693 powerScrew.brake(1);
rizqicahyo 36:5963c9a49485 694 }
gustavaditya 40:5b937cac959a 695 }
rizqicahyo 36:5963c9a49485 696
rizqicahyo 36:5963c9a49485 697
gustavaditya 31:d5cbda07fd95 698 void launcher()
gustavaditya 31:d5cbda07fd95 699 {
gustavaditya 31:d5cbda07fd95 700 if (isLauncher)
Sufa 30:d69cc27ac644 701 {
be_bryan 28:2d0746dc2d7d 702 currentMillis = millis();
be_bryan 28:2d0746dc2d7d 703 currentMillis2 = millis();
be_bryan 28:2d0746dc2d7d 704
Najib_irvani 46:85169ae8659b 705 // PID Launcher Belakang
Najib_irvani 46:85169ae8659b 706 if (currentMillis-previousMillis>=Ts)
be_bryan 26:256160a1a82d 707 {
gustavaditya 31:d5cbda07fd95 708 rpm = (float)encLauncherBlk.getPulses();
Najib_irvani 46:85169ae8659b 709 current_error1 = target_rpm - rpm;
Najib_irvani 46:85169ae8659b 710 a1 = kpA1 + kiA1*Ts/2 + kdA1/Ts;
Najib_irvani 46:85169ae8659b 711 b1 = -kpA1 + kiA1*Ts/2 - 2*kdA1/Ts;
Najib_irvani 46:85169ae8659b 712 c1 = kdA1/Ts;
Najib_irvani 46:85169ae8659b 713 speed = previous_speed1 + a1*current_error1 + b1*previous_error1_1 + c1*previous_error1_2;
gustavaditya 38:3ef6754bd8d8 714 //init_speed();
gustavaditya 38:3ef6754bd8d8 715 if(speed > maxSpeed){
gustavaditya 38:3ef6754bd8d8 716 launcherBlk.speed(maxSpeed);
gustavaditya 38:3ef6754bd8d8 717 }
gustavaditya 39:11358f3f61ff 718 else if ( speed < minSpeed){
gustavaditya 39:11358f3f61ff 719 launcherBlk.speed(minSpeed);
gustavaditya 39:11358f3f61ff 720 }
gustavaditya 38:3ef6754bd8d8 721 else {
gustavaditya 38:3ef6754bd8d8 722 launcherBlk.speed(speed);
gustavaditya 38:3ef6754bd8d8 723 }
Najib_irvani 46:85169ae8659b 724 previous_speed1 = speed;
Najib_irvani 46:85169ae8659b 725 previous_error1_2 = previous_error1_1;
Najib_irvani 46:85169ae8659b 726 previous_error1_1 = current_error1;
gustavaditya 31:d5cbda07fd95 727 encLauncherBlk.reset();
be_bryan 26:256160a1a82d 728 previousMillis = currentMillis;
Najib_irvani 46:85169ae8659b 729
be_bryan 26:256160a1a82d 730 }
Najib_irvani 46:85169ae8659b 731 // PID Launcher Depan
Najib_irvani 46:85169ae8659b 732 if (currentMillis2-previousMillis2>=Ts)
be_bryan 27:68efb1622985 733 {
gustavaditya 31:d5cbda07fd95 734 rpm2 = (float)encLauncherDpn.getPulses();
be_bryan 27:68efb1622985 735 current_error2 = target_rpm2 - rpm2;
Najib_irvani 46:85169ae8659b 736 a2 = kpA2 + kiA2*Ts/2 + kdA2/Ts;
Najib_irvani 46:85169ae8659b 737 b2 = -kpA2 + kiA2*Ts/2 - 2*kdA2/Ts;
Najib_irvani 46:85169ae8659b 738 c2 = kdA2/Ts;
Najib_irvani 46:85169ae8659b 739 speed2 = previous_speed2 + a2*current_error2 + b2*previous_error2_1 + c2*previous_error2_2;
gustavaditya 38:3ef6754bd8d8 740 //init_speed();
gustavaditya 38:3ef6754bd8d8 741 if (speed2 > maxSpeed){
gustavaditya 38:3ef6754bd8d8 742 launcherDpn.speed(maxSpeed);
gustavaditya 38:3ef6754bd8d8 743 }
Najib_irvani 46:85169ae8659b 744 else if ( speed2 < minSpeed){
gustavaditya 39:11358f3f61ff 745 launcherDpn.speed(minSpeed);
gustavaditya 39:11358f3f61ff 746 }
gustavaditya 38:3ef6754bd8d8 747 else{
gustavaditya 38:3ef6754bd8d8 748 launcherDpn.speed(speed2);
gustavaditya 38:3ef6754bd8d8 749 }
Najib_irvani 46:85169ae8659b 750 previous_speed2 = speed2;
Najib_irvani 46:85169ae8659b 751 previous_error2_2 = previous_error2_1;
Najib_irvani 46:85169ae8659b 752 previous_error2_1 = current_error2;
gustavaditya 31:d5cbda07fd95 753 encLauncherDpn.reset();
be_bryan 27:68efb1622985 754 previousMillis2 = currentMillis2;
be_bryan 27:68efb1622985 755 }
Najib_irvani 47:6cac4f1a3c8e 756 //pc.printf("%.2f\t%.2f\n",rpm,rpm2);
rahmadirizki18 6:68293bed71ea 757 }
Sufa 29:7b372b0aaa61 758 else
Sufa 29:7b372b0aaa61 759 {
gustavaditya 31:d5cbda07fd95 760 launcherDpn.brake(1);
gustavaditya 31:d5cbda07fd95 761 launcherBlk.brake(1);
Najib_irvani 46:85169ae8659b 762
Najib_irvani 46:85169ae8659b 763 previous_error1_1 = 0;
Najib_irvani 46:85169ae8659b 764 previous_error1_2 = 0;
Najib_irvani 46:85169ae8659b 765 previous_error2_1 = 0;
Najib_irvani 46:85169ae8659b 766 previous_error2_2 = 0;
Najib_irvani 46:85169ae8659b 767 previous_speed1 = 0;
Najib_irvani 46:85169ae8659b 768 previous_speed2 = 0;
gustavaditya 31:d5cbda07fd95 769 }
rahmadirizki18 5:3aa203218306 770 }
gustavaditya 31:d5cbda07fd95 771
MarchioKevin 22:4632f58461e0 772 /*********************************************************/
MarchioKevin 22:4632f58461e0 773 /* Main Function */
MarchioKevin 22:4632f58461e0 774 /*********************************************************/
calmantara186 16:90119f03c5d1 775
gustavaditya 31:d5cbda07fd95 776 int main (void)
gustavaditya 31:d5cbda07fd95 777 {
gustavaditya 31:d5cbda07fd95 778 // Set baud rate - 115200
fanny868 0:9072e932503c 779 joystick.setup();
rahmadirizki18 23:023b522977b2 780 pc.baud(115200);
rahmadirizki18 6:68293bed71ea 781 wait_ms(1000);
gustavaditya 40:5b937cac959a 782
gustavaditya 40:5b937cac959a 783 // initializing encoder
gustavaditya 41:336a19289c2d 784 pneumatik =1;
gustavaditya 41:336a19289c2d 785
rahmadirizki18 5:3aa203218306 786 wait_ms(500);
gustavaditya 40:5b937cac959a 787
gustavaditya 40:5b937cac959a 788 //initializing PING
gustavaditya 40:5b937cac959a 789 pingAtas.Send();
gustavaditya 40:5b937cac959a 790
rahmadirizki18 23:023b522977b2 791 pc.printf("ready....");
gustavaditya 35:69a47b4cb3fc 792 startMillis();
fanny868 0:9072e932503c 793 while(1)
gustavaditya 40:5b937cac959a 794 {
gustavaditya 40:5b937cac959a 795 // interupsi pembacaan PING setiap 30 ms
Najib_irvani 46:85169ae8659b 796 if(millis() - previousMillis4 >= 10){ //30
Najib_irvani 46:85169ae8659b 797 jarak_ping = (float)pingAtas.Read_cm();
gustavaditya 40:5b937cac959a 798
gustavaditya 40:5b937cac959a 799 pingAtas.Send();
gustavaditya 40:5b937cac959a 800 previousMillis4 = millis();
gustavaditya 40:5b937cac959a 801 }
gustavaditya 40:5b937cac959a 802
fanny868 0:9072e932503c 803 // Interrupt Serial
calmantara186 16:90119f03c5d1 804 joystick.idle();
gustavaditya 31:d5cbda07fd95 805 if(joystick.readable())
gustavaditya 31:d5cbda07fd95 806 {
fanny868 0:9072e932503c 807 // Panggil fungsi pembacaan joystik
fanny868 0:9072e932503c 808 joystick.baca_data();
fanny868 0:9072e932503c 809 // Panggil fungsi pengolahan data joystik
fanny868 0:9072e932503c 810 joystick.olah_data();
gustavaditya 31:d5cbda07fd95 811 // Masuk ke case joystick
gustavaditya 31:d5cbda07fd95 812 case_joy = case_joystick();
Najib_irvani 44:452c214d9cf5 813 //pc.printf("%d\n",case_joy);
rahmadirizki18 3:1287fccc11be 814 aktuator();
gustavaditya 31:d5cbda07fd95 815 launcher();
gustavaditya 40:5b937cac959a 816 reloader();
Najib_irvani 43:3807a95aa284 817 if ((millis()-previousMillis3 >= 320)&&(flag_Pneu)){
be_bryan 26:256160a1a82d 818 pneumatik = 1;
gustavaditya 31:d5cbda07fd95 819 flag_Pneu = false;
be_bryan 26:256160a1a82d 820 }
be_bryan 26:256160a1a82d 821 }
gustavaditya 31:d5cbda07fd95 822 else
gustavaditya 31:d5cbda07fd95 823 {
gustavaditya 31:d5cbda07fd95 824 joystick.idle();
MarchioKevin 21:da2f3d04468f 825 }
franshendri 42:6caf8bd5abbc 826
Najib_irvani 44:452c214d9cf5 827 if(millis() - previousMillis5 >= 400)
Najib_irvani 44:452c214d9cf5 828 {
Najib_irvani 43:3807a95aa284 829 display.write(0,((int) rpm2) / 10);
Najib_irvani 43:3807a95aa284 830 display.write(1,((int)rpm2) % 10);
franshendri 42:6caf8bd5abbc 831 display.write(2, (int)target_rpm2 / 10);
franshendri 42:6caf8bd5abbc 832 display.write(3, (int)target_rpm2 % 10);
franshendri 42:6caf8bd5abbc 833 display.setColon(true);
franshendri 42:6caf8bd5abbc 834
franshendri 42:6caf8bd5abbc 835 previousMillis5 = millis();
franshendri 42:6caf8bd5abbc 836 }
fanny868 0:9072e932503c 837 }
be_bryan 28:2d0746dc2d7d 838 }