bug : pwm full di launcher

Dependencies:   Motor PID Joystick_OrdoV5 mbed millis

Fork of MainProgram_BaseBaru by KRAI 2017

Committer:
fanny868
Date:
Thu May 28 14:50:05 2015 +0000
Revision:
0:9072e932503c
Child:
2:ac21a024ac80
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fanny868 0:9072e932503c 1 /*********************************************************************************************/
fanny868 0:9072e932503c 2 /** **/
fanny868 0:9072e932503c 3 /** PROGRAM KOMUNIKASI DATA JOYSTIK PS3 **/
fanny868 0:9072e932503c 4 /** **/
fanny868 0:9072e932503c 5 /** Joystik PS3 -> Arduino -> STM32Nucleo **/
fanny868 0:9072e932503c 6 /** **/
fanny868 0:9072e932503c 7 /** Fanny Achmad Hindrarta **/
fanny868 0:9072e932503c 8 /** EL'12 - 13212076 **/
fanny868 0:9072e932503c 9 /** **/
fanny868 0:9072e932503c 10 /** Last Update : 15 Januari 2015, 20.30 **/
fanny868 0:9072e932503c 11 /*********************************************************************************************/
fanny868 0:9072e932503c 12
fanny868 0:9072e932503c 13 #include <PS3BT.h>
fanny868 0:9072e932503c 14 #include <usbhub.h>
fanny868 0:9072e932503c 15 #include "Arduino.h"
fanny868 0:9072e932503c 16 // Satisfy IDE, which only needs to see the include statment in the ino.
fanny868 0:9072e932503c 17 #ifdef dobogusinclude
fanny868 0:9072e932503c 18 #include <spi4teensy3.h>
fanny868 0:9072e932503c 19 #endif
fanny868 0:9072e932503c 20
fanny868 0:9072e932503c 21 USB Usb;
fanny868 0:9072e932503c 22 /* You can create the instance of the class in two ways */
fanny868 0:9072e932503c 23 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
fanny868 0:9072e932503c 24 /* You can create the instance of the class in two ways */
fanny868 0:9072e932503c 25 PS3BT PS3(&Btd); // This will just create the instance
fanny868 0:9072e932503c 26 //PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
fanny868 0:9072e932503c 27
fanny868 0:9072e932503c 28 boolean printAngle;
fanny868 0:9072e932503c 29 uint8_t state = 0;
fanny868 0:9072e932503c 30
fanny868 0:9072e932503c 31 // Deklarasi tombol
fanny868 0:9072e932503c 32 boolean kiri_click=0, kanan_click=0, atas_click=0, bawah_click=0;
fanny868 0:9072e932503c 33 boolean segitiga_click=0, lingkaran_click=0, kotak_click=0, silang_click=0;
fanny868 0:9072e932503c 34 boolean L1_click=0, R1_click=0, L3_click=0, R3_click=0;
fanny868 0:9072e932503c 35 boolean START_click=0, SELECT_click=0, PS_click=0;
fanny868 0:9072e932503c 36
fanny868 0:9072e932503c 37 // Deklarasi variabel tombol analog
fanny868 0:9072e932503c 38 unsigned char LX, LY, RX, RY, aL2, aR2;
fanny868 0:9072e932503c 39
fanny868 0:9072e932503c 40 // Deklarasi varibel data yang dikirim
fanny868 0:9072e932503c 41 unsigned char button;
fanny868 0:9072e932503c 42 unsigned char RL;
fanny868 0:9072e932503c 43 unsigned int button_click;
fanny868 0:9072e932503c 44 unsigned int RL_click;
fanny868 0:9072e932503c 45
fanny868 0:9072e932503c 46 void setup_joystik()
fanny868 0:9072e932503c 47 {
fanny868 0:9072e932503c 48 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
fanny868 0:9072e932503c 49 if (Usb.Init() == -1) {
fanny868 0:9072e932503c 50 Serial.print(F("\r\nOSC did not start"));
fanny868 0:9072e932503c 51 while (1); //halt
fanny868 0:9072e932503c 52 }
fanny868 0:9072e932503c 53 Serial.print(F("\r\nPS3 USB Library Started"));
fanny868 0:9072e932503c 54 }
fanny868 0:9072e932503c 55
fanny868 0:9072e932503c 56 /*********************************************************************************************/
fanny868 0:9072e932503c 57 /** **/
fanny868 0:9072e932503c 58 /** FUNGSI PENGIRIMAN DATA **/
fanny868 0:9072e932503c 59 /** - Data yang akan dikirim adalah paket data 8-bit dengan urutan sebagai berikut **/
fanny868 0:9072e932503c 60 /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/
fanny868 0:9072e932503c 61 /** | 0x88 | 0x08 | button | RL | button_click | RL_click | R2 | L2 | RX | RY | LX | LY | **/
fanny868 0:9072e932503c 62 /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/
fanny868 0:9072e932503c 63 /** **/
fanny868 0:9072e932503c 64 /*********************************************************************************************/
fanny868 0:9072e932503c 65
fanny868 0:9072e932503c 66 void kirimdatajoystik()
fanny868 0:9072e932503c 67 {
fanny868 0:9072e932503c 68
fanny868 0:9072e932503c 69 Serial.write(0x88);
fanny868 0:9072e932503c 70 Serial.write(0x08);
fanny868 0:9072e932503c 71 Serial.write(button);
fanny868 0:9072e932503c 72 Serial.write(RL);
fanny868 0:9072e932503c 73 Serial.write(button_click);
fanny868 0:9072e932503c 74 Serial.write(RL_click);
fanny868 0:9072e932503c 75 Serial.write(aR2);
fanny868 0:9072e932503c 76 Serial.write(aL2);
fanny868 0:9072e932503c 77 Serial.write(RX);
fanny868 0:9072e932503c 78 Serial.write(RY);
fanny868 0:9072e932503c 79 Serial.write(LX);
fanny868 0:9072e932503c 80 Serial.write(LY);
fanny868 0:9072e932503c 81 // Debug
fanny868 0:9072e932503c 82
fanny868 0:9072e932503c 83 /*
fanny868 0:9072e932503c 84 Serial.print(millis());
fanny868 0:9072e932503c 85 Serial.print("\t");
fanny868 0:9072e932503c 86 Serial.print(button);
fanny868 0:9072e932503c 87 Serial.print("\t");
fanny868 0:9072e932503c 88 Serial.print(RL);
fanny868 0:9072e932503c 89 Serial.print("\t");
fanny868 0:9072e932503c 90 Serial.print(button_click);
fanny868 0:9072e932503c 91 Serial.print("\t");
fanny868 0:9072e932503c 92 Serial.print(RL_click);
fanny868 0:9072e932503c 93 Serial.print("\t");
fanny868 0:9072e932503c 94 Serial.print(aR2);
fanny868 0:9072e932503c 95 Serial.print("\t");
fanny868 0:9072e932503c 96 Serial.print(aL2);
fanny868 0:9072e932503c 97 Serial.print("\t");
fanny868 0:9072e932503c 98 Serial.print(RX);
fanny868 0:9072e932503c 99 Serial.print("\t");
fanny868 0:9072e932503c 100 Serial.print(RY);
fanny868 0:9072e932503c 101 Serial.print("\t");
fanny868 0:9072e932503c 102 Serial.print(LX);
fanny868 0:9072e932503c 103 Serial.print("\t");
fanny868 0:9072e932503c 104 Serial.println(LY);
fanny868 0:9072e932503c 105 */
fanny868 0:9072e932503c 106 }
fanny868 0:9072e932503c 107
fanny868 0:9072e932503c 108 /*****************************************************************************************/
fanny868 0:9072e932503c 109 /** SETUP REGISTER dan INISIALISASI **/
fanny868 0:9072e932503c 110 /** - Setup Joystik **/
fanny868 0:9072e932503c 111 /** - Baud Rate Serial 115200, 8-bit, 1 stop, 0 parity **/
fanny868 0:9072e932503c 112 /*****************************************************************************************/
fanny868 0:9072e932503c 113
fanny868 0:9072e932503c 114 void setup() {
fanny868 0:9072e932503c 115 Serial.begin(115200);
fanny868 0:9072e932503c 116
fanny868 0:9072e932503c 117 setup_joystik();
fanny868 0:9072e932503c 118 }
fanny868 0:9072e932503c 119
fanny868 0:9072e932503c 120
fanny868 0:9072e932503c 121 /*****************************************************************************************/
fanny868 0:9072e932503c 122 /** **/
fanny868 0:9072e932503c 123 /** FUNGSI PEMBACAAN DATA JOYSTIK **/
fanny868 0:9072e932503c 124 /** - Data dari Joystik dikirim melalui Bluetooth. **/
fanny868 0:9072e932503c 125 /** Lihat Library "PS3BT.h" **/
fanny868 0:9072e932503c 126 /** - Data tombol dari joystik adalah data 1-bit. Pengiriman data melalui USART **/
fanny868 0:9072e932503c 127 /** adalah 8-bit. Agar pengiriman data efisien, maka data 1-bit digabungkan **/
fanny868 0:9072e932503c 128 /** agar terbentuk data 8-bit **/
fanny868 0:9072e932503c 129 /** - Penggabungan data segitiga, longkaran, silang, kotak, arah atas, arah kanan, **/
fanny868 0:9072e932503c 130 /** arah bawah, arah kiri disimpan dalam variabel "button" **/
fanny868 0:9072e932503c 131 /** - Urutan data pada variabel "button" dan "button_click" **/
fanny868 0:9072e932503c 132 /** adalah sebagai berikut **/
fanny868 0:9072e932503c 133 /** **/
fanny868 0:9072e932503c 134 /** |------|-------|-------|------|-------|--------|-----------|----------| **/
fanny868 0:9072e932503c 135 /** Bit Ke | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | **/
fanny868 0:9072e932503c 136 /** |------|-------|-------|------|-------|--------|-----------|----------| **/
fanny868 0:9072e932503c 137 /** Data | kiri | bawah | kanan | atas | kotak | silang | lingkaran | segitiga | **/
fanny868 0:9072e932503c 138 /** |------|-------|-------|------|-------|--------|-----------|----------| **/
fanny868 0:9072e932503c 139 /** **/
fanny868 0:9072e932503c 140 /** - Penggabungan data R1, R2, L1, L2, R3, L3, START, dan SELECT disimpan dalam **/
fanny868 0:9072e932503c 141 /** variabel "RL" **/
fanny868 0:9072e932503c 142 /** - Urutan data pada variabel "RL" dan "RL_click" adalah **/
fanny868 0:9072e932503c 143 /** sebagai berikut **/
fanny868 0:9072e932503c 144 /** **/
fanny868 0:9072e932503c 145 /** |----|--------|-------|----|----|----|----| **/
fanny868 0:9072e932503c 146 /** Bit Ke | 6 | 5 | 4 | 3 | 2 | 1 | 0 | **/
fanny868 0:9072e932503c 147 /** |----|--------|-------|----|----|----|----| **/
fanny868 0:9072e932503c 148 /** Data | PS | SELECT | START | L3 | L1 | R3 | R1 | **/
fanny868 0:9072e932503c 149 /** |----|--------|-------|----|----|----|----| **/
fanny868 0:9072e932503c 150 /** **/
fanny868 0:9072e932503c 151 /*****************************************************************************************/
fanny868 0:9072e932503c 152
fanny868 0:9072e932503c 153 void loop() {
fanny868 0:9072e932503c 154 button = 0;
fanny868 0:9072e932503c 155 RL = 0;
fanny868 0:9072e932503c 156
fanny868 0:9072e932503c 157 Usb.Task();
fanny868 0:9072e932503c 158
fanny868 0:9072e932503c 159 // Pembacaan data joystik dilakukan jika PS3 tersambung ke Arduino
fanny868 0:9072e932503c 160 if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
fanny868 0:9072e932503c 161 // Pembacaan dan penggabungan data segitiga, lingkaran, silang, kotak, atas, kanan, bawah, kiri
fanny868 0:9072e932503c 162 // Data bernilai '1' jika tombol ditekan
fanny868 0:9072e932503c 163 if(PS3.getButtonPress(TRIANGLE)){
fanny868 0:9072e932503c 164 button = button + (0x1 << 0);
fanny868 0:9072e932503c 165 }
fanny868 0:9072e932503c 166 if(PS3.getButtonPress(CIRCLE)){
fanny868 0:9072e932503c 167 button = button + (0x1 << 1);
fanny868 0:9072e932503c 168 }
fanny868 0:9072e932503c 169 if(PS3.getButtonPress(CROSS)){
fanny868 0:9072e932503c 170 button = button + (0x1 << 2);
fanny868 0:9072e932503c 171 }
fanny868 0:9072e932503c 172 if(PS3.getButtonPress(SQUARE)){
fanny868 0:9072e932503c 173 button = button + (0x1 << 3);
fanny868 0:9072e932503c 174 }
fanny868 0:9072e932503c 175 if(PS3.getButtonPress(UP)){
fanny868 0:9072e932503c 176 button = button + (0x1 << 4);
fanny868 0:9072e932503c 177 }
fanny868 0:9072e932503c 178 if(PS3.getButtonPress(RIGHT)){
fanny868 0:9072e932503c 179 button = button + (0x1 << 5);
fanny868 0:9072e932503c 180 }
fanny868 0:9072e932503c 181 if(PS3.getButtonPress(DOWN)){
fanny868 0:9072e932503c 182 button = button + (0x1 << 6);
fanny868 0:9072e932503c 183 }
fanny868 0:9072e932503c 184 if(PS3.getButtonPress(LEFT)){
fanny868 0:9072e932503c 185 button = button + (0x1 << 7);
fanny868 0:9072e932503c 186 }
fanny868 0:9072e932503c 187
fanny868 0:9072e932503c 188 // Pembacaan dan penggabungan data R1, R3, L1, L3, START, SELECT, dan PS
fanny868 0:9072e932503c 189 // Data bernilai '1' jika tombol ditekan
fanny868 0:9072e932503c 190 if(PS3.getButtonPress(R1)){
fanny868 0:9072e932503c 191 RL = RL + (0x1 << 0);
fanny868 0:9072e932503c 192 }
fanny868 0:9072e932503c 193 if(PS3.getButtonPress(R3)){
fanny868 0:9072e932503c 194 RL = RL + (0x1 << 1);
fanny868 0:9072e932503c 195 }
fanny868 0:9072e932503c 196 if(PS3.getButtonPress(L1)){
fanny868 0:9072e932503c 197 RL = RL + (0x1 << 2);
fanny868 0:9072e932503c 198 }
fanny868 0:9072e932503c 199 if(PS3.getButtonPress(L3)){
fanny868 0:9072e932503c 200 RL = RL + (0x1 << 3);
fanny868 0:9072e932503c 201 }
fanny868 0:9072e932503c 202 if(PS3.getButtonPress(START)){
fanny868 0:9072e932503c 203 RL = RL + (0x1 << 4);
fanny868 0:9072e932503c 204 }
fanny868 0:9072e932503c 205 if(PS3.getButtonPress(SELECT)){
fanny868 0:9072e932503c 206 RL = RL + (0x1 << 5);
fanny868 0:9072e932503c 207 }
fanny868 0:9072e932503c 208 if(PS3.getButtonPress(PS)){
fanny868 0:9072e932503c 209 RL = RL + (0x1 << 6);
fanny868 0:9072e932503c 210 }
fanny868 0:9072e932503c 211
fanny868 0:9072e932503c 212 // Pembacaan dan penggabungan data segitiga, lingkaran, silang, kotak, atas, kanan, bawah, kiri
fanny868 0:9072e932503c 213 // Data bernilai '1' hanya saat tombol pertama kali ditekan
fanny868 0:9072e932503c 214 if(PS3.getButtonClick(TRIANGLE)){
fanny868 0:9072e932503c 215 segitiga_click = !segitiga_click;
fanny868 0:9072e932503c 216 }
fanny868 0:9072e932503c 217 if(PS3.getButtonClick(CIRCLE)){
fanny868 0:9072e932503c 218 lingkaran_click = !lingkaran_click;
fanny868 0:9072e932503c 219 }
fanny868 0:9072e932503c 220 if(PS3.getButtonClick(CROSS)){
fanny868 0:9072e932503c 221 silang_click = !silang_click;
fanny868 0:9072e932503c 222 }
fanny868 0:9072e932503c 223 if(PS3.getButtonClick(SQUARE)){
fanny868 0:9072e932503c 224 kotak_click = !kotak_click;
fanny868 0:9072e932503c 225 }
fanny868 0:9072e932503c 226 if(PS3.getButtonClick(UP)){
fanny868 0:9072e932503c 227 atas_click = !atas_click;
fanny868 0:9072e932503c 228 }
fanny868 0:9072e932503c 229 if(PS3.getButtonClick(RIGHT)){
fanny868 0:9072e932503c 230 kanan_click = !kanan_click;
fanny868 0:9072e932503c 231 }
fanny868 0:9072e932503c 232 if(PS3.getButtonClick(DOWN)){
fanny868 0:9072e932503c 233 bawah_click = !bawah_click;
fanny868 0:9072e932503c 234 }
fanny868 0:9072e932503c 235 if(PS3.getButtonClick(LEFT)){
fanny868 0:9072e932503c 236 kiri_click = !kiri_click;
fanny868 0:9072e932503c 237 }
fanny868 0:9072e932503c 238
fanny868 0:9072e932503c 239 button_click = 0;
fanny868 0:9072e932503c 240 button_click = (segitiga_click << 0) + (lingkaran_click << 1) + (silang_click << 2) + (kotak_click << 3) + (atas_click << 4) + (kanan_click << 5) + (bawah_click << 6) + (kiri_click << 7);
fanny868 0:9072e932503c 241
fanny868 0:9072e932503c 242 if(PS3.getButtonClick(R1)){
fanny868 0:9072e932503c 243 R1_click = !R1_click;
fanny868 0:9072e932503c 244 }
fanny868 0:9072e932503c 245 if(PS3.getButtonClick(R3)){
fanny868 0:9072e932503c 246 R3_click = !R3_click;
fanny868 0:9072e932503c 247 }
fanny868 0:9072e932503c 248 if(PS3.getButtonClick(L1)){
fanny868 0:9072e932503c 249 L1_click = !L1_click;
fanny868 0:9072e932503c 250 }
fanny868 0:9072e932503c 251 if(PS3.getButtonClick(L3)){
fanny868 0:9072e932503c 252 L3_click = !L3_click;
fanny868 0:9072e932503c 253 }
fanny868 0:9072e932503c 254 if(PS3.getButtonClick(START)){
fanny868 0:9072e932503c 255 START_click = !START_click;
fanny868 0:9072e932503c 256 }
fanny868 0:9072e932503c 257 if(PS3.getButtonClick(SELECT)){
fanny868 0:9072e932503c 258 SELECT_click = !SELECT_click;
fanny868 0:9072e932503c 259 }
fanny868 0:9072e932503c 260 if(PS3.getButtonClick(PS)){
fanny868 0:9072e932503c 261 PS_click = !PS_click;
fanny868 0:9072e932503c 262 }
fanny868 0:9072e932503c 263
fanny868 0:9072e932503c 264 RL_click = 0;
fanny868 0:9072e932503c 265 RL_click = (R1_click << 0) + (R3_click << 1) + (L1_click << 2) + (L3_click << 3) + (START_click << 4) + (SELECT_click << 5) + (PS_click << 6);
fanny868 0:9072e932503c 266
fanny868 0:9072e932503c 267 // Tombol Analog
fanny868 0:9072e932503c 268 LX = PS3.getAnalogHat(LeftHatX);
fanny868 0:9072e932503c 269 LY = PS3.getAnalogHat(LeftHatY);
fanny868 0:9072e932503c 270 RX = PS3.getAnalogHat(RightHatX);
fanny868 0:9072e932503c 271 RY = PS3.getAnalogHat(RightHatY);
fanny868 0:9072e932503c 272
fanny868 0:9072e932503c 273 aL2 = PS3.getAnalogButton(L2);
fanny868 0:9072e932503c 274 aR2 = PS3.getAnalogButton(R2);
fanny868 0:9072e932503c 275 } else {
fanny868 0:9072e932503c 276 // PS3 Disconnected
fanny868 0:9072e932503c 277 Serial.write(0x88);
fanny868 0:9072e932503c 278 Serial.write(0x09);
fanny868 0:9072e932503c 279 }
fanny868 0:9072e932503c 280
fanny868 0:9072e932503c 281 kirimdatajoystik();
fanny868 0:9072e932503c 282 delay(5);
fanny868 0:9072e932503c 283 }