convert_KeilToMbed
Dependencies: DigitDisplay Motor PID mbed millis
Fork of DagonFly__RoadToJapan_15Mei_Ultimate by
main.cpp@33:69d266bc3fe9, 2017-02-14 (annotated)
- Committer:
- gustavaditya
- Date:
- Tue Feb 14 10:34:39 2017 +0000
- Revision:
- 33:69d266bc3fe9
- Parent:
- 32:581d4a2373f0
- Child:
- 34:1cfd9b1c7d27
14 Februari 2017
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rahmadirizki18 | 5:3aa203218306 | 1 | /****************************************************************************/ |
rahmadirizki18 | 5:3aa203218306 | 2 | /* PROGRAM UNTUK PID CLOSED LOOP */ |
rahmadirizki18 | 5:3aa203218306 | 3 | /* */ |
gustavaditya | 33:69d266bc3fe9 | 4 | /* Last Update : 14 Februari 2017 */ |
gustavaditya | 31:d5cbda07fd95 | 5 | /* */ |
rahmadirizki18 | 5:3aa203218306 | 6 | /* - Digunakan encoder autonics */ |
rahmadirizki18 | 5:3aa203218306 | 7 | /* - Konfigurasi Motor dan Encoder sbb : */ |
MarchioKevin | 22:4632f58461e0 | 8 | /* ______________________ */ |
MarchioKevin | 22:4632f58461e0 | 9 | /* / \ Rode Depan Belakang: */ |
MarchioKevin | 22:4632f58461e0 | 10 | /* / 2 (Belakang) \ Omniwheel */ |
MarchioKevin | 22:4632f58461e0 | 11 | /* | | */ |
MarchioKevin | 22:4632f58461e0 | 12 | /* | 3 (Encoder) 4 | Roda Kiri Kanan: */ |
MarchioKevin | 22:4632f58461e0 | 13 | /* | | Fixed Wheel */ |
MarchioKevin | 22:4632f58461e0 | 14 | /* \ 1 (Depan) / */ |
MarchioKevin | 22:4632f58461e0 | 15 | /* \______________________/ Putaran berlawanan arah */ |
MarchioKevin | 22:4632f58461e0 | 16 | /* jarum jam positif */ |
rahmadirizki18 | 5:3aa203218306 | 17 | /* SETTINGS (WAJIB!) : */ |
rahmadirizki18 | 5:3aa203218306 | 18 | /* 1. Settings Pin Encoder, Resolusi, dan Tipe encoding di omniPos.h */ |
rahmadirizki18 | 5:3aa203218306 | 19 | /* 2. Deklarasi penggunaan library omniPos pada bagian deklarasi encoder */ |
rahmadirizki18 | 5:3aa203218306 | 20 | /* */ |
rahmadirizki18 | 5:3aa203218306 | 21 | /****************************************************************************/ |
rahmadirizki18 | 6:68293bed71ea | 22 | /* */ |
MarchioKevin | 20:54dc93e7b016 | 23 | /* Joystick */ |
Sufa | 30:d69cc27ac644 | 24 | /* Kanan => */ |
Sufa | 30:d69cc27ac644 | 25 | /* Kiri => */ |
rahmadirizki18 | 6:68293bed71ea | 26 | /* */ |
Sufa | 30:d69cc27ac644 | 27 | /* Tombol silang => Power Screw Aktif */ |
Sufa | 30:d69cc27ac644 | 28 | /* Tombol segitiga => Aktif motor Launcher */ |
Sufa | 30:d69cc27ac644 | 29 | /* Tombol lingkaran => Aktif Pneumatik Launcher */ |
Sufa | 30:d69cc27ac644 | 30 | /* Tombol L1 => Pivot Kiri */ |
Sufa | 30:d69cc27ac644 | 31 | /* Tombol R1 => Pivot Kanan */ |
Sufa | 30:d69cc27ac644 | 32 | /* Tombol L3 => PWM Motor Belakang Dikurangi */ |
Sufa | 30:d69cc27ac644 | 33 | /* Tombol R3 => PWM Motor Belakang Ditambah */ |
Sufa | 30:d69cc27ac644 | 34 | /* Tombol L2 => PWM Motor Depan Dikurangi */ |
Sufa | 30:d69cc27ac644 | 35 | /* Tombol R2 => PWM Motor Depan Ditambah */ |
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 | 6:68293bed71ea | 44 | #include "Servo.h" |
rahmadirizki18 | 5:3aa203218306 | 45 | #include "encoderKRAI.h" |
be_bryan | 26:256160a1a82d | 46 | #include "millis.h" |
rahmadirizki18 | 5:3aa203218306 | 47 | |
calmantara186 | 16:90119f03c5d1 | 48 | /***********************************************/ |
calmantara186 | 16:90119f03c5d1 | 49 | /* Konstanta dan Variabel */ |
calmantara186 | 16:90119f03c5d1 | 50 | /***********************************************/ |
calmantara186 | 16:90119f03c5d1 | 51 | #define PI 3.14159265 |
be_bryan | 26:256160a1a82d | 52 | #define D_ENCODER 10 // Diameter Roda Encoder |
be_bryan | 26:256160a1a82d | 53 | #define D_ROBOT 80 // Diameter Roda Robot |
rahmadirizki18 | 17:e4229d77a5ab | 54 | #define PERPINDAHAN 1 // Perpindahan ke kanan dan kiri |
rahmadirizki18 | 5:3aa203218306 | 55 | |
be_bryan | 26:256160a1a82d | 56 | // Variable Atas |
gustavaditya | 32:581d4a2373f0 | 57 | double speed, speed2, maxSpeed = 0.95, minSpeed = 0; |
be_bryan | 26:256160a1a82d | 58 | double kpA=0.6757, kdA=0.6757, kiA=0.00006757; |
be_bryan | 26:256160a1a82d | 59 | double p,i,d; |
be_bryan | 27:68efb1622985 | 60 | double p2,i2,d2; |
be_bryan | 26:256160a1a82d | 61 | double last_error = 0, current_error, sum_error = 0; |
be_bryan | 27:68efb1622985 | 62 | double last_error2 = 0, current_error2, sum_error2 = 0; |
gustavaditya | 31:d5cbda07fd95 | 63 | float rpm, target_rpm = 8.0; |
gustavaditya | 31:d5cbda07fd95 | 64 | float rpm2, target_rpm2 = 8.0; |
gustavaditya | 33:69d266bc3fe9 | 65 | float pwmPowerUp = 0.70; |
gustavaditya | 33:69d266bc3fe9 | 66 | float pwmPowerDown = -0.80; |
Sufa | 30:d69cc27ac644 | 67 | bool henti=false, hentis=false; |
be_bryan | 26:256160a1a82d | 68 | |
be_bryan | 26:256160a1a82d | 69 | // Variable Bawah |
Joshua23 | 25:054d3048dd03 | 70 | float Vt; |
gustavaditya | 31:d5cbda07fd95 | 71 | float keliling_enc = PI*D_ENCODER; |
gustavaditya | 31:d5cbda07fd95 | 72 | float keliling_robot = PI*D_ROBOT; |
gustavaditya | 31:d5cbda07fd95 | 73 | float speedT = 0.2; |
gustavaditya | 31:d5cbda07fd95 | 74 | float vpid = 0; |
gustavaditya | 33:69d266bc3fe9 | 75 | float PIVOT = 0.27; // PWM Pivot Kanan, Pivot Kiri |
gustavaditya | 33:69d266bc3fe9 | 76 | float tuneDpn = 0.35; // Tunning PWM motor Depan |
gustavaditya | 33:69d266bc3fe9 | 77 | float tuneBlk = 0.3; // Tunning PWM motor belakang |
gustavaditya | 31:d5cbda07fd95 | 78 | |
gustavaditya | 31:d5cbda07fd95 | 79 | /* Variabel Encoder Bawah */ |
gustavaditya | 31:d5cbda07fd95 | 80 | float errTetha, Tetha; // Variabel yang didapatkan encoder |
gustavaditya | 31:d5cbda07fd95 | 81 | |
gustavaditya | 31:d5cbda07fd95 | 82 | /* Deklarasi Variable Millis */ |
gustavaditya | 33:69d266bc3fe9 | 83 | unsigned long int previousMillis = 0; // PID launcher |
gustavaditya | 31:d5cbda07fd95 | 84 | unsigned long int currentMillis; |
gustavaditya | 33:69d266bc3fe9 | 85 | unsigned long int previousMillis2 = 0; // PID launcher |
gustavaditya | 31:d5cbda07fd95 | 86 | unsigned long int currentMillis2; |
gustavaditya | 33:69d266bc3fe9 | 87 | unsigned long int previousMillis3 = 0; // Pneumatik |
MarchioKevin | 22:4632f58461e0 | 88 | |
gustavaditya | 31:d5cbda07fd95 | 89 | /* Variabel Stick */ |
gustavaditya | 31:d5cbda07fd95 | 90 | //Logic untuk masuk aktuator |
gustavaditya | 31:d5cbda07fd95 | 91 | int case_joy; |
gustavaditya | 31:d5cbda07fd95 | 92 | bool isLauncher = false; |
gustavaditya | 31:d5cbda07fd95 | 93 | bool isReload = false; |
gustavaditya | 31:d5cbda07fd95 | 94 | int flagLauncher = 1; |
gustavaditya | 31:d5cbda07fd95 | 95 | bool flag_Pneu = false; |
gustavaditya | 31:d5cbda07fd95 | 96 | |
gustavaditya | 31:d5cbda07fd95 | 97 | /*****************************************************/ |
gustavaditya | 31:d5cbda07fd95 | 98 | /* Definisi Prosedur, Fungsi dan Setting Pinout */ |
gustavaditya | 31:d5cbda07fd95 | 99 | /*****************************************************/ |
gustavaditya | 31:d5cbda07fd95 | 100 | |
gustavaditya | 31:d5cbda07fd95 | 101 | /* Fungsi dan Procedur Encoder */ |
gustavaditya | 31:d5cbda07fd95 | 102 | void init_speed(); // |
gustavaditya | 31:d5cbda07fd95 | 103 | void aktuator(); // Pergerakan aktuator berdasarkan case joystick |
gustavaditya | 31:d5cbda07fd95 | 104 | int case_joystick(); // Mendapatkan case dari joystick |
gustavaditya | 33:69d266bc3fe9 | 105 | //void speedKalibrasiMotor(); // Pertambahan target RPM motor atas melalui joystick |
gustavaditya | 31:d5cbda07fd95 | 106 | void setCenter(); // Prosedur reset encoder, posisi saat itu diset jadi titik (0,0) |
gustavaditya | 31:d5cbda07fd95 | 107 | float getTetha(); // Fungsi mendapatkan error Tetha |
gustavaditya | 31:d5cbda07fd95 | 108 | |
gustavaditya | 31:d5cbda07fd95 | 109 | /* Inisialisasi Pin TX-RX Joystik dan PC */ |
gustavaditya | 31:d5cbda07fd95 | 110 | joysticknucleo joystick(PA_0,PA_1); |
gustavaditya | 31:d5cbda07fd95 | 111 | Serial pc(USBTX,USBRX); |
calmantara186 | 16:90119f03c5d1 | 112 | |
be_bryan | 26:256160a1a82d | 113 | /* Deklarasi Encoder Base */ |
gustavaditya | 31:d5cbda07fd95 | 114 | encoderKRAI encoderBase(PC_4, PB_15, 2000, encoderKRAI::X2_ENCODING); //inA, inB, pin Indeks (NC = tak ada), 2xresolusi, mode pembacaan |
rahmadirizki18 | 15:98f0d56b14f0 | 115 | |
be_bryan | 26:256160a1a82d | 116 | /* Deklarasi Encoder Launcher */ |
gustavaditya | 31:d5cbda07fd95 | 117 | encoderKRAI encLauncherBlk( PC_10, PC_11, 28, encoderKRAI::X2_ENCODING); |
gustavaditya | 31:d5cbda07fd95 | 118 | encoderKRAI encLauncherDpn( PD_2, PC_12, 28, encoderKRAI::X2_ENCODING); |
be_bryan | 26:256160a1a82d | 119 | |
calmantara186 | 16:90119f03c5d1 | 120 | /* Deklarasi Motor Base */ |
gustavaditya | 31:d5cbda07fd95 | 121 | Motor motorDpn(PB_9, PA_12, PC_5); // pwm, fwd, rev |
gustavaditya | 31:d5cbda07fd95 | 122 | Motor motorBlk(PB_6, PB_1, PB_12); // pwm, fwd, rev |
fanny868 | 0:9072e932503c | 123 | |
calmantara186 | 16:90119f03c5d1 | 124 | /* Deklarasi Motor Launcher */ |
gustavaditya | 31:d5cbda07fd95 | 125 | Motor launcherDpn(PA_8,PC_2,PC_1); // pwm ,fwd, rev |
gustavaditya | 31:d5cbda07fd95 | 126 | Motor launcherBlk(PA_10, PC_3, PC_0); // pwm, fwd, rev |
gustavaditya | 31:d5cbda07fd95 | 127 | Motor powerScrew(PB_7, PA_14, PA_15); // pwm, fwd, rev |
rahmadirizki18 | 5:3aa203218306 | 128 | |
be_bryan | 26:256160a1a82d | 129 | /* Deklarasi Penumatik Launcher */ |
Sufa | 29:7b372b0aaa61 | 130 | DigitalOut pneumatik(PB_3, PullUp); |
be_bryan | 26:256160a1a82d | 131 | |
be_bryan | 27:68efb1622985 | 132 | /*Dekalrasi Limit Switch */ |
gustavaditya | 31:d5cbda07fd95 | 133 | DigitalIn limitAtas(PC_9, PullUp); |
gustavaditya | 31:d5cbda07fd95 | 134 | DigitalIn limitTengah(PB_10, PullUp); |
gustavaditya | 31:d5cbda07fd95 | 135 | DigitalIn limitBawah(PC_8, PullUp); |
rahmadirizki18 | 5:3aa203218306 | 136 | |
fanny868 | 0:9072e932503c | 137 | |
MarchioKevin | 22:4632f58461e0 | 138 | /****************************************************/ |
MarchioKevin | 22:4632f58461e0 | 139 | /* Deklarasi Fungsi dan Procedure */ |
MarchioKevin | 22:4632f58461e0 | 140 | /****************************************************/ |
gustavaditya | 31:d5cbda07fd95 | 141 | int case_joystick() |
gustavaditya | 31:d5cbda07fd95 | 142 | { |
gustavaditya | 31:d5cbda07fd95 | 143 | //---------------------------------------------------// |
gustavaditya | 31:d5cbda07fd95 | 144 | // Gerak Motor Base // |
gustavaditya | 31:d5cbda07fd95 | 145 | // Case 1 : Pivot kanan // |
gustavaditya | 31:d5cbda07fd95 | 146 | // Case 2 : Pivot Kiri // |
gustavaditya | 31:d5cbda07fd95 | 147 | // Case 3 : Kanan // |
gustavaditya | 31:d5cbda07fd95 | 148 | // Case 4 : Kiri // |
gustavaditya | 31:d5cbda07fd95 | 149 | // Case 5 : Break // |
gustavaditya | 31:d5cbda07fd95 | 150 | //---------------------------------------------------// |
gustavaditya | 31:d5cbda07fd95 | 151 | |
gustavaditya | 31:d5cbda07fd95 | 152 | int caseJoystick; |
gustavaditya | 31:d5cbda07fd95 | 153 | if (!joystick.L1 && joystick.R1) { |
gustavaditya | 31:d5cbda07fd95 | 154 | // Pivot Kanan |
gustavaditya | 31:d5cbda07fd95 | 155 | caseJoystick = 1; |
gustavaditya | 31:d5cbda07fd95 | 156 | } |
gustavaditya | 31:d5cbda07fd95 | 157 | else if (!joystick.R1 && joystick.L1) { |
gustavaditya | 31:d5cbda07fd95 | 158 | // Pivot Kiri |
gustavaditya | 31:d5cbda07fd95 | 159 | caseJoystick = 2; |
gustavaditya | 31:d5cbda07fd95 | 160 | } |
gustavaditya | 31:d5cbda07fd95 | 161 | else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(joystick.kanan)&&(!joystick.kiri)) { |
gustavaditya | 31:d5cbda07fd95 | 162 | // Kanan |
gustavaditya | 31:d5cbda07fd95 | 163 | caseJoystick = 3; |
gustavaditya | 33:69d266bc3fe9 | 164 | //pc.printf("kanan"); |
gustavaditya | 31:d5cbda07fd95 | 165 | } |
gustavaditya | 31:d5cbda07fd95 | 166 | else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(joystick.kiri)) { |
gustavaditya | 31:d5cbda07fd95 | 167 | // Kiri |
gustavaditya | 31:d5cbda07fd95 | 168 | caseJoystick = 4; |
gustavaditya | 33:69d266bc3fe9 | 169 | //pc.printf("kiri"); |
gustavaditya | 31:d5cbda07fd95 | 170 | } |
gustavaditya | 31:d5cbda07fd95 | 171 | else if (joystick.segitiga_click && flagLauncher){ |
gustavaditya | 31:d5cbda07fd95 | 172 | // Motor Launcher |
gustavaditya | 31:d5cbda07fd95 | 173 | caseJoystick = 5; |
gustavaditya | 31:d5cbda07fd95 | 174 | } |
gustavaditya | 32:581d4a2373f0 | 175 | else if (joystick.R2_click && (target_rpm2 < 20)){ |
gustavaditya | 31:d5cbda07fd95 | 176 | // Target Pulse PID ++ Motor Depan |
gustavaditya | 31:d5cbda07fd95 | 177 | caseJoystick = 6; |
gustavaditya | 31:d5cbda07fd95 | 178 | } |
gustavaditya | 31:d5cbda07fd95 | 179 | else if (joystick.L2_click && (target_rpm2 > 1)){ |
gustavaditya | 31:d5cbda07fd95 | 180 | // Target Pulse PID -- Motor Depan |
gustavaditya | 31:d5cbda07fd95 | 181 | caseJoystick = 7; |
gustavaditya | 31:d5cbda07fd95 | 182 | } |
gustavaditya | 32:581d4a2373f0 | 183 | else if (joystick.R3_click && (target_rpm < 20 )){ |
gustavaditya | 31:d5cbda07fd95 | 184 | // Target Pulse PID ++ Motor Belakang |
gustavaditya | 31:d5cbda07fd95 | 185 | caseJoystick = 8; |
gustavaditya | 31:d5cbda07fd95 | 186 | } |
gustavaditya | 31:d5cbda07fd95 | 187 | else if (joystick.L3_click && (target_rpm > 1)){ |
gustavaditya | 31:d5cbda07fd95 | 188 | // Target Pulse PID -- Motor Belakang |
gustavaditya | 31:d5cbda07fd95 | 189 | caseJoystick = 9; |
gustavaditya | 31:d5cbda07fd95 | 190 | } |
gustavaditya | 31:d5cbda07fd95 | 191 | else if (joystick.silang_click){ |
gustavaditya | 31:d5cbda07fd95 | 192 | // Pnemuatik ON |
gustavaditya | 31:d5cbda07fd95 | 193 | caseJoystick = 10; |
gustavaditya | 31:d5cbda07fd95 | 194 | } |
gustavaditya | 31:d5cbda07fd95 | 195 | else if ((joystick.atas)&&(!joystick.bawah)) { |
gustavaditya | 31:d5cbda07fd95 | 196 | // Power Screw Up |
gustavaditya | 31:d5cbda07fd95 | 197 | caseJoystick = 11; |
gustavaditya | 31:d5cbda07fd95 | 198 | } |
gustavaditya | 31:d5cbda07fd95 | 199 | else if ((!joystick.atas)&&(joystick.bawah)) { |
gustavaditya | 31:d5cbda07fd95 | 200 | // Power Screw Down |
gustavaditya | 31:d5cbda07fd95 | 201 | caseJoystick = 12; |
gustavaditya | 31:d5cbda07fd95 | 202 | } |
gustavaditya | 31:d5cbda07fd95 | 203 | else if ((!joystick.R1)&&(!joystick.L1)&&(!joystick.atas)&&(!joystick.bawah)&&(!joystick.kanan)&&(!joystick.kiri)) { |
gustavaditya | 31:d5cbda07fd95 | 204 | // Break |
gustavaditya | 31:d5cbda07fd95 | 205 | caseJoystick = 13; |
gustavaditya | 31:d5cbda07fd95 | 206 | } |
gustavaditya | 31:d5cbda07fd95 | 207 | |
gustavaditya | 31:d5cbda07fd95 | 208 | return(caseJoystick); |
gustavaditya | 31:d5cbda07fd95 | 209 | } |
gustavaditya | 31:d5cbda07fd95 | 210 | |
gustavaditya | 31:d5cbda07fd95 | 211 | float getTetha(){ |
gustavaditya | 31:d5cbda07fd95 | 212 | // Fungsi untuk mendapatkan nilai tetha |
gustavaditya | 31:d5cbda07fd95 | 213 | float busur, tetha; |
gustavaditya | 31:d5cbda07fd95 | 214 | busur = ((encoderBase.getPulses())/(float)(2000.0)*keliling_enc); |
gustavaditya | 31:d5cbda07fd95 | 215 | tetha = busur/keliling_robot*360; |
gustavaditya | 31:d5cbda07fd95 | 216 | |
gustavaditya | 31:d5cbda07fd95 | 217 | return -(tetha); |
gustavaditya | 31:d5cbda07fd95 | 218 | } |
gustavaditya | 31:d5cbda07fd95 | 219 | |
be_bryan | 26:256160a1a82d | 220 | float pidBase(float Kp, float Ki, float Kd) |
Joshua23 | 25:054d3048dd03 | 221 | { |
Joshua23 | 25:054d3048dd03 | 222 | int errorP; |
Joshua23 | 25:054d3048dd03 | 223 | errorP = getTetha(); |
gustavaditya | 33:69d266bc3fe9 | 224 | if (errorP<3.5 && errorP>(-3.5)) |
gustavaditya | 33:69d266bc3fe9 | 225 | errorP = 0; |
Joshua23 | 25:054d3048dd03 | 226 | return (float)Kp*errorP; |
Joshua23 | 25:054d3048dd03 | 227 | } |
Joshua23 | 25:054d3048dd03 | 228 | |
gustavaditya | 31:d5cbda07fd95 | 229 | void init_speed (){ |
gustavaditya | 31:d5cbda07fd95 | 230 | if (speed>maxSpeed){ |
gustavaditya | 31:d5cbda07fd95 | 231 | speed = maxSpeed; |
gustavaditya | 31:d5cbda07fd95 | 232 | } |
gustavaditya | 31:d5cbda07fd95 | 233 | if (speed<minSpeed){ |
gustavaditya | 31:d5cbda07fd95 | 234 | speed = minSpeed; |
calmantara186 | 16:90119f03c5d1 | 235 | } |
gustavaditya | 31:d5cbda07fd95 | 236 | if (speed2>maxSpeed){ |
gustavaditya | 31:d5cbda07fd95 | 237 | speed2 = maxSpeed; |
gustavaditya | 31:d5cbda07fd95 | 238 | } |
gustavaditya | 31:d5cbda07fd95 | 239 | if (speed2<minSpeed){ |
gustavaditya | 31:d5cbda07fd95 | 240 | speed2 = minSpeed; |
gustavaditya | 31:d5cbda07fd95 | 241 | } |
gustavaditya | 31:d5cbda07fd95 | 242 | } |
gustavaditya | 31:d5cbda07fd95 | 243 | |
gustavaditya | 31:d5cbda07fd95 | 244 | void setCenter(){ |
gustavaditya | 31:d5cbda07fd95 | 245 | // Fungsi untuk menentukan center dari robot |
gustavaditya | 31:d5cbda07fd95 | 246 | encoderBase.reset(); |
fanny868 | 0:9072e932503c | 247 | } |
fanny868 | 0:9072e932503c | 248 | |
gustavaditya | 31:d5cbda07fd95 | 249 | void aktuator() |
gustavaditya | 31:d5cbda07fd95 | 250 | { |
gustavaditya | 31:d5cbda07fd95 | 251 | switch (case_joy) { |
gustavaditya | 31:d5cbda07fd95 | 252 | case (1): |
gustavaditya | 31:d5cbda07fd95 | 253 | { |
gustavaditya | 31:d5cbda07fd95 | 254 | // Pivot Kanan |
gustavaditya | 31:d5cbda07fd95 | 255 | motorDpn.speed(-PIVOT); |
gustavaditya | 31:d5cbda07fd95 | 256 | motorBlk.speed(-PIVOT); |
gustavaditya | 31:d5cbda07fd95 | 257 | setCenter(); |
gustavaditya | 31:d5cbda07fd95 | 258 | break; |
gustavaditya | 31:d5cbda07fd95 | 259 | } |
gustavaditya | 31:d5cbda07fd95 | 260 | case (2): |
gustavaditya | 31:d5cbda07fd95 | 261 | { |
gustavaditya | 31:d5cbda07fd95 | 262 | // Pivot Kiri |
gustavaditya | 31:d5cbda07fd95 | 263 | motorDpn.speed(PIVOT); |
gustavaditya | 31:d5cbda07fd95 | 264 | motorBlk.speed(PIVOT); |
gustavaditya | 31:d5cbda07fd95 | 265 | setCenter(); |
gustavaditya | 31:d5cbda07fd95 | 266 | break; |
gustavaditya | 31:d5cbda07fd95 | 267 | } |
gustavaditya | 31:d5cbda07fd95 | 268 | case (3) : |
gustavaditya | 31:d5cbda07fd95 | 269 | { |
gustavaditya | 31:d5cbda07fd95 | 270 | // Kanan |
gustavaditya | 33:69d266bc3fe9 | 271 | motorDpn.speed(-tuneDpn + pidBase(0.009,0,0)); |
gustavaditya | 33:69d266bc3fe9 | 272 | motorBlk.speed(tuneBlk + pidBase(0.009,0,0)); |
gustavaditya | 33:69d266bc3fe9 | 273 | //speedDpn = tuneDpn + pidBase(0.009,0,0) |
gustavaditya | 33:69d266bc3fe9 | 274 | //speedBlk = tuneBlk + pidBase(0.009,0,0) |
gustavaditya | 33:69d266bc3fe9 | 275 | //motorDpn.speed(-tuneDpn); |
gustavaditya | 33:69d266bc3fe9 | 276 | //motorBlk.speed(tuneBlk); |
gustavaditya | 31:d5cbda07fd95 | 277 | break; |
gustavaditya | 31:d5cbda07fd95 | 278 | } |
gustavaditya | 31:d5cbda07fd95 | 279 | case (4) : |
gustavaditya | 31:d5cbda07fd95 | 280 | { |
gustavaditya | 31:d5cbda07fd95 | 281 | // Kiri |
gustavaditya | 33:69d266bc3fe9 | 282 | motorDpn.speed(tuneDpn + pidBase(0.009,0,0)); |
gustavaditya | 33:69d266bc3fe9 | 283 | motorBlk.speed(-tuneBlk + pidBase(0.009,0,0)); |
gustavaditya | 33:69d266bc3fe9 | 284 | //motorDpn.speed(tuneDpn); |
gustavaditya | 33:69d266bc3fe9 | 285 | //motorBlk.speed(-tuneBlk); |
gustavaditya | 31:d5cbda07fd95 | 286 | break; |
gustavaditya | 31:d5cbda07fd95 | 287 | } |
gustavaditya | 31:d5cbda07fd95 | 288 | case (5) : |
gustavaditya | 31:d5cbda07fd95 | 289 | { |
gustavaditya | 31:d5cbda07fd95 | 290 | // Aktifkan motor atas |
gustavaditya | 31:d5cbda07fd95 | 291 | isLauncher = !isLauncher; |
gustavaditya | 31:d5cbda07fd95 | 292 | break; |
Joshua23 | 8:0711dea61312 | 293 | } |
gustavaditya | 31:d5cbda07fd95 | 294 | case (6) : |
gustavaditya | 31:d5cbda07fd95 | 295 | { |
gustavaditya | 31:d5cbda07fd95 | 296 | // Target Pulse PID ++ Motor Depan |
gustavaditya | 31:d5cbda07fd95 | 297 | target_rpm2 = target_rpm2++; |
gustavaditya | 31:d5cbda07fd95 | 298 | break; |
gustavaditya | 31:d5cbda07fd95 | 299 | } |
gustavaditya | 31:d5cbda07fd95 | 300 | case (7) : |
gustavaditya | 31:d5cbda07fd95 | 301 | { |
gustavaditya | 31:d5cbda07fd95 | 302 | // Target Pulse PID -- Motor Depan |
gustavaditya | 31:d5cbda07fd95 | 303 | target_rpm2 = target_rpm2--; |
gustavaditya | 31:d5cbda07fd95 | 304 | break; |
gustavaditya | 31:d5cbda07fd95 | 305 | } |
gustavaditya | 31:d5cbda07fd95 | 306 | case (8) : |
gustavaditya | 31:d5cbda07fd95 | 307 | { |
gustavaditya | 32:581d4a2373f0 | 308 | // Target Pulse PID ++ Motor Belakang= |
gustavaditya | 31:d5cbda07fd95 | 309 | target_rpm = target_rpm++; |
gustavaditya | 31:d5cbda07fd95 | 310 | break; |
gustavaditya | 31:d5cbda07fd95 | 311 | } |
gustavaditya | 31:d5cbda07fd95 | 312 | case (9) : |
be_bryan | 27:68efb1622985 | 313 | { |
gustavaditya | 31:d5cbda07fd95 | 314 | // Target Pulse PID -- Motor Belakang |
gustavaditya | 31:d5cbda07fd95 | 315 | target_rpm = target_rpm--; |
gustavaditya | 31:d5cbda07fd95 | 316 | break; |
gustavaditya | 31:d5cbda07fd95 | 317 | } |
gustavaditya | 31:d5cbda07fd95 | 318 | case (10) : |
gustavaditya | 31:d5cbda07fd95 | 319 | { |
gustavaditya | 31:d5cbda07fd95 | 320 | // Pneumatik |
gustavaditya | 31:d5cbda07fd95 | 321 | pneumatik = 0; |
gustavaditya | 31:d5cbda07fd95 | 322 | previousMillis3 = millis(); |
gustavaditya | 31:d5cbda07fd95 | 323 | flag_Pneu = true; |
gustavaditya | 31:d5cbda07fd95 | 324 | break; |
gustavaditya | 31:d5cbda07fd95 | 325 | } |
gustavaditya | 31:d5cbda07fd95 | 326 | case (11) : |
gustavaditya | 31:d5cbda07fd95 | 327 | { |
gustavaditya | 31:d5cbda07fd95 | 328 | // Power Screw Up |
gustavaditya | 31:d5cbda07fd95 | 329 | powerScrew.speed(pwmPowerUp); |
gustavaditya | 31:d5cbda07fd95 | 330 | break; |
gustavaditya | 31:d5cbda07fd95 | 331 | } |
gustavaditya | 31:d5cbda07fd95 | 332 | case (12) : |
gustavaditya | 31:d5cbda07fd95 | 333 | { |
gustavaditya | 31:d5cbda07fd95 | 334 | // Power Screw Down |
gustavaditya | 31:d5cbda07fd95 | 335 | powerScrew.speed(pwmPowerDown); |
gustavaditya | 31:d5cbda07fd95 | 336 | break; |
gustavaditya | 31:d5cbda07fd95 | 337 | } |
gustavaditya | 31:d5cbda07fd95 | 338 | default : |
gustavaditya | 31:d5cbda07fd95 | 339 | { |
gustavaditya | 31:d5cbda07fd95 | 340 | motorDpn.brake(1); |
gustavaditya | 31:d5cbda07fd95 | 341 | motorBlk.brake(1); |
gustavaditya | 31:d5cbda07fd95 | 342 | if (!limitAtas) |
be_bryan | 27:68efb1622985 | 343 | { |
gustavaditya | 31:d5cbda07fd95 | 344 | powerScrew.brake(1); |
gustavaditya | 31:d5cbda07fd95 | 345 | } |
gustavaditya | 31:d5cbda07fd95 | 346 | if (!limitTengah) |
gustavaditya | 31:d5cbda07fd95 | 347 | { |
gustavaditya | 31:d5cbda07fd95 | 348 | case_joy = 12; |
gustavaditya | 31:d5cbda07fd95 | 349 | aktuator(); |
gustavaditya | 31:d5cbda07fd95 | 350 | } |
gustavaditya | 31:d5cbda07fd95 | 351 | if (!limitBawah) |
Sufa | 30:d69cc27ac644 | 352 | { |
gustavaditya | 31:d5cbda07fd95 | 353 | case_joy = 11; |
gustavaditya | 31:d5cbda07fd95 | 354 | aktuator(); |
be_bryan | 27:68efb1622985 | 355 | } |
gustavaditya | 31:d5cbda07fd95 | 356 | } |
gustavaditya | 31:d5cbda07fd95 | 357 | } // End Switch |
gustavaditya | 31:d5cbda07fd95 | 358 | } |
gustavaditya | 31:d5cbda07fd95 | 359 | |
gustavaditya | 31:d5cbda07fd95 | 360 | void launcher() |
gustavaditya | 31:d5cbda07fd95 | 361 | { |
gustavaditya | 31:d5cbda07fd95 | 362 | if (isLauncher) |
Sufa | 30:d69cc27ac644 | 363 | { |
be_bryan | 26:256160a1a82d | 364 | startMillis(); |
be_bryan | 28:2d0746dc2d7d | 365 | currentMillis = millis(); |
be_bryan | 28:2d0746dc2d7d | 366 | currentMillis2 = millis(); |
be_bryan | 28:2d0746dc2d7d | 367 | |
gustavaditya | 31:d5cbda07fd95 | 368 | // PID Launcher Depan |
be_bryan | 26:256160a1a82d | 369 | if (currentMillis-previousMillis>=10) |
be_bryan | 26:256160a1a82d | 370 | { |
gustavaditya | 31:d5cbda07fd95 | 371 | rpm = (float)encLauncherBlk.getPulses(); |
be_bryan | 26:256160a1a82d | 372 | current_error = target_rpm - rpm; |
be_bryan | 26:256160a1a82d | 373 | sum_error = sum_error + current_error; |
be_bryan | 26:256160a1a82d | 374 | p = current_error*kpA; |
be_bryan | 26:256160a1a82d | 375 | d = (current_error-last_error)*kdA/10.0; |
be_bryan | 26:256160a1a82d | 376 | i = sum_error*kiA*10.0; |
be_bryan | 26:256160a1a82d | 377 | speed = p + d + i; |
be_bryan | 26:256160a1a82d | 378 | init_speed(); |
gustavaditya | 31:d5cbda07fd95 | 379 | launcherBlk.speed(speed); |
be_bryan | 26:256160a1a82d | 380 | last_error = current_error; |
gustavaditya | 31:d5cbda07fd95 | 381 | encLauncherBlk.reset(); |
be_bryan | 26:256160a1a82d | 382 | //pc.printf("%.04lf\n",rpm); |
be_bryan | 26:256160a1a82d | 383 | previousMillis = currentMillis; |
be_bryan | 26:256160a1a82d | 384 | } |
be_bryan | 27:68efb1622985 | 385 | if (currentMillis2-previousMillis2>=10) |
be_bryan | 27:68efb1622985 | 386 | { |
gustavaditya | 31:d5cbda07fd95 | 387 | rpm2 = (float)encLauncherDpn.getPulses(); |
be_bryan | 27:68efb1622985 | 388 | current_error2 = target_rpm2 - rpm2; |
be_bryan | 27:68efb1622985 | 389 | sum_error2 = sum_error2 + current_error2; |
be_bryan | 27:68efb1622985 | 390 | p2 = current_error2*kpA; |
be_bryan | 27:68efb1622985 | 391 | d2 = (current_error2-last_error2)*kdA/10.0; |
be_bryan | 27:68efb1622985 | 392 | i2 = sum_error2*kiA*10.0; |
be_bryan | 27:68efb1622985 | 393 | speed2 = p2 + d2 + i2; |
be_bryan | 27:68efb1622985 | 394 | init_speed(); |
gustavaditya | 31:d5cbda07fd95 | 395 | launcherDpn.speed(speed2); |
be_bryan | 27:68efb1622985 | 396 | last_error2 = current_error2; |
gustavaditya | 31:d5cbda07fd95 | 397 | encLauncherDpn.reset(); |
be_bryan | 27:68efb1622985 | 398 | previousMillis2 = currentMillis2; |
be_bryan | 27:68efb1622985 | 399 | } |
rahmadirizki18 | 6:68293bed71ea | 400 | } |
Sufa | 29:7b372b0aaa61 | 401 | else |
Sufa | 29:7b372b0aaa61 | 402 | { |
gustavaditya | 31:d5cbda07fd95 | 403 | launcherDpn.brake(1); |
gustavaditya | 31:d5cbda07fd95 | 404 | launcherBlk.brake(1); |
gustavaditya | 31:d5cbda07fd95 | 405 | } |
rahmadirizki18 | 5:3aa203218306 | 406 | } |
gustavaditya | 31:d5cbda07fd95 | 407 | |
MarchioKevin | 22:4632f58461e0 | 408 | /*********************************************************/ |
MarchioKevin | 22:4632f58461e0 | 409 | /* Main Function */ |
MarchioKevin | 22:4632f58461e0 | 410 | /*********************************************************/ |
calmantara186 | 16:90119f03c5d1 | 411 | |
gustavaditya | 31:d5cbda07fd95 | 412 | int main (void) |
gustavaditya | 31:d5cbda07fd95 | 413 | { |
gustavaditya | 31:d5cbda07fd95 | 414 | // Set baud rate - 115200 |
fanny868 | 0:9072e932503c | 415 | joystick.setup(); |
rahmadirizki18 | 23:023b522977b2 | 416 | pc.baud(115200); |
rahmadirizki18 | 6:68293bed71ea | 417 | wait_ms(1000); |
rahmadirizki18 | 5:3aa203218306 | 418 | setCenter(); |
rahmadirizki18 | 5:3aa203218306 | 419 | wait_ms(500); |
rahmadirizki18 | 23:023b522977b2 | 420 | pc.printf("ready...."); |
fanny868 | 0:9072e932503c | 421 | while(1) |
fanny868 | 0:9072e932503c | 422 | { |
fanny868 | 0:9072e932503c | 423 | // Interrupt Serial |
calmantara186 | 16:90119f03c5d1 | 424 | joystick.idle(); |
gustavaditya | 31:d5cbda07fd95 | 425 | if(joystick.readable()) |
gustavaditya | 31:d5cbda07fd95 | 426 | { |
fanny868 | 0:9072e932503c | 427 | // Panggil fungsi pembacaan joystik |
fanny868 | 0:9072e932503c | 428 | joystick.baca_data(); |
fanny868 | 0:9072e932503c | 429 | // Panggil fungsi pengolahan data joystik |
fanny868 | 0:9072e932503c | 430 | joystick.olah_data(); |
gustavaditya | 31:d5cbda07fd95 | 431 | // Masuk ke case joystick |
gustavaditya | 31:d5cbda07fd95 | 432 | case_joy = case_joystick(); |
rahmadirizki18 | 3:1287fccc11be | 433 | aktuator(); |
gustavaditya | 31:d5cbda07fd95 | 434 | launcher(); |
gustavaditya | 31:d5cbda07fd95 | 435 | if ((millis()-previousMillis3 >= 700)&&(flag_Pneu)){ |
be_bryan | 26:256160a1a82d | 436 | pneumatik = 1; |
gustavaditya | 31:d5cbda07fd95 | 437 | flag_Pneu = false; |
be_bryan | 26:256160a1a82d | 438 | } |
be_bryan | 26:256160a1a82d | 439 | } |
gustavaditya | 31:d5cbda07fd95 | 440 | else |
gustavaditya | 31:d5cbda07fd95 | 441 | { |
gustavaditya | 31:d5cbda07fd95 | 442 | joystick.idle(); |
MarchioKevin | 21:da2f3d04468f | 443 | } |
fanny868 | 0:9072e932503c | 444 | } |
be_bryan | 28:2d0746dc2d7d | 445 | } |