paling baru

Dependencies:   DigitDisplay Motor PID Ping mbed millis

Fork of MainProgram_BaseBaru_fix_omni_11April by KRAI 2017

Committer:
MarchioKevin
Date:
Sat Jan 28 04:33:39 2017 +0000
Revision:
20:54dc93e7b016
Parent:
13:8ab42383a2ca
Child:
32:581d4a2373f0
Mode encoder sumbu X: Perpindahan; Bersih-bersih kodinga wkwk

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fanny868 0:9072e932503c 1 #ifndef MBED_H
fanny868 0:9072e932503c 2 #include "mbed.h"
fanny868 0:9072e932503c 3 #endif
franshendri 12:e07c59c28c29 4
fanny868 0:9072e932503c 5 #ifndef JoystickPS3__serialDEFAULT_BAUD
fanny868 0:9072e932503c 6 #define JoystickPS3__serialDEFAULT_BAUD 115200
fanny868 0:9072e932503c 7 #endif
franshendri 12:e07c59c28c29 8
fanny868 0:9072e932503c 9 Serial debug(USBTX,USBRX);
franshendri 12:e07c59c28c29 10
fanny868 0:9072e932503c 11 namespace JoystickPS3 {
franshendri 12:e07c59c28c29 12
fanny868 0:9072e932503c 13 class joysticknucleo {
fanny868 0:9072e932503c 14 public:
fanny868 0:9072e932503c 15 joysticknucleo(PinName tx, PinName rx) : _serial(tx, rx)
fanny868 0:9072e932503c 16 {
fanny868 0:9072e932503c 17
fanny868 0:9072e932503c 18 }
fanny868 0:9072e932503c 19
fanny868 0:9072e932503c 20 // Deklarasi variabel tombol analog
fanny868 0:9072e932503c 21 unsigned char LX, LY, RX, RY, R2, L2;
fanny868 0:9072e932503c 22
fanny868 0:9072e932503c 23 unsigned char button;
fanny868 0:9072e932503c 24 unsigned char RL;
fanny868 0:9072e932503c 25 unsigned char button_click;
fanny868 0:9072e932503c 26 unsigned char RL_click;
fanny868 0:9072e932503c 27
fanny868 0:9072e932503c 28 void setup(){
fanny868 0:9072e932503c 29 _serial.baud(JoystickPS3__serialDEFAULT_BAUD);
fanny868 0:9072e932503c 30 debug.baud(9600);
fanny868 0:9072e932503c 31 }
franshendri 12:e07c59c28c29 32
fanny868 0:9072e932503c 33 /*********************************************************************************************/
fanny868 0:9072e932503c 34 /** **/
fanny868 0:9072e932503c 35 /** FUNGSI PEMBACAAN DATA **/
fanny868 0:9072e932503c 36 /** - Data yang diterima dari Serial Arduino berbentuk 8-bit **/
fanny868 0:9072e932503c 37 /** - Data yang diterima diolah menjadi boolean / 1-bit untuk data tombol button dan RL **/
fanny868 0:9072e932503c 38 /** karena data yang digunakan adalah 1-bit (true/false) **/
fanny868 0:9072e932503c 39 /** - Untuk analog data yang diterima tidak diolah karena rentang data yang dikirimkan **/
fanny868 0:9072e932503c 40 /** memiliki rentang 0-255 / 8-bit, dan data yang akan digunakan adalah data 8-bit **/
fanny868 0:9072e932503c 41 /** **/
fanny868 0:9072e932503c 42 /** |------|-------|-------|------|-------|--------|-----------|----------| **/
fanny868 0:9072e932503c 43 /** Bit Ke | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | **/
fanny868 0:9072e932503c 44 /** |------|-------|-------|------|-------|--------|-----------|----------| **/
fanny868 0:9072e932503c 45 /** Data | kiri | bawah | kanan | atas | kotak | silang | lingkaran | segitiga | **/
fanny868 0:9072e932503c 46 /** |------|-------|-------|------|-------|--------|-----------|----------| **/
fanny868 0:9072e932503c 47 /** **/
fanny868 0:9072e932503c 48 /** - Penggabungan data R1, R2, L1, L2, R3, L3, START, dan SELECT disimpan dalam **/
fanny868 0:9072e932503c 49 /** variabel "RL" **/
fanny868 0:9072e932503c 50 /** - Urutan data pada variabel "RL" dan "RL_click" adalah **/
fanny868 0:9072e932503c 51 /** sebagai berikut **/
fanny868 0:9072e932503c 52 /** **/
fanny868 0:9072e932503c 53 /** |----|--------|-------|----|----|----|----| **/
fanny868 0:9072e932503c 54 /** Bit Ke | 6 | 5 | 4 | 3 | 2 | 1 | 0 | **/
fanny868 0:9072e932503c 55 /** |----|--------|-------|----|----|----|----| **/
fanny868 0:9072e932503c 56 /** Data | PS | SELECT | START | L3 | L1 | R3 | R1 | **/
fanny868 0:9072e932503c 57 /** |----|--------|-------|----|----|----|----| **/
fanny868 0:9072e932503c 58 /** **/
fanny868 0:9072e932503c 59 /*********************************************************************************************/
fanny868 0:9072e932503c 60
fanny868 0:9072e932503c 61 void olah_data()
fanny868 0:9072e932503c 62 {
fanny868 0:9072e932503c 63 // Pengolahan data dari data "button"
fanny868 0:9072e932503c 64 segitiga = (bool)((button >> 0) & 0x1);
fanny868 0:9072e932503c 65 lingkaran = (bool)((button >> 1) & 0x1);
fanny868 0:9072e932503c 66 silang = (bool)((button >> 2) & 0x1);
fanny868 0:9072e932503c 67 kotak = (bool)((button >> 3) & 0x1);
fanny868 0:9072e932503c 68 atas = (bool)((button >> 4) & 0x1);
fanny868 0:9072e932503c 69 kanan = (bool)((button >> 5) & 0x1);
fanny868 0:9072e932503c 70 bawah = (bool)((button >> 6) & 0x1);
fanny868 0:9072e932503c 71 kiri = (bool)((button >> 7) & 0x1);
fanny868 0:9072e932503c 72
fanny868 0:9072e932503c 73 // Pengolahan data dari data "RL"
fanny868 0:9072e932503c 74 R1 = (bool)((RL >> 0) & 0x1);
fanny868 0:9072e932503c 75 R3 = (bool)((RL >> 1) & 0x1);
fanny868 0:9072e932503c 76 L1 = (bool)((RL >> 2) & 0x1);
fanny868 0:9072e932503c 77 L3 = (bool)((RL >> 3) & 0x1);
fanny868 0:9072e932503c 78 START = (bool)((RL >> 4) & 0x1);
fanny868 0:9072e932503c 79 SELECT = (bool)((RL >> 5) & 0x1);
fanny868 0:9072e932503c 80 PS = (bool)((RL >> 6) & 0x1);
fanny868 0:9072e932503c 81
rahmadirizki18 13:8ab42383a2ca 82 // R2 click dan L2 click
rahmadirizki18 13:8ab42383a2ca 83 if (R2 > 100) {
rahmadirizki18 13:8ab42383a2ca 84 if ( R2sebelum) { R2_click = false;
rahmadirizki18 13:8ab42383a2ca 85 } else { R2_click = true;}
rahmadirizki18 13:8ab42383a2ca 86 R2sebelum = true;
rahmadirizki18 13:8ab42383a2ca 87 }else {
rahmadirizki18 13:8ab42383a2ca 88 R2sebelum = false;
rahmadirizki18 13:8ab42383a2ca 89 R2_click = false;
rahmadirizki18 13:8ab42383a2ca 90 }
rahmadirizki18 13:8ab42383a2ca 91 if (L2 > 100) {
rahmadirizki18 13:8ab42383a2ca 92 if ( R2sebelum) { L2_click = false;
rahmadirizki18 13:8ab42383a2ca 93 } else { L2_click = true;}
rahmadirizki18 13:8ab42383a2ca 94 L2sebelum = true;
rahmadirizki18 13:8ab42383a2ca 95 }else { L2sebelum = false;
rahmadirizki18 13:8ab42383a2ca 96 L2_click = false;
rahmadirizki18 13:8ab42383a2ca 97 }
rahmadirizki18 13:8ab42383a2ca 98
fanny868 0:9072e932503c 99 segitiga_click = (bool)((button_click >> 0) & 0x1);
fanny868 0:9072e932503c 100 lingkaran_click = (bool)((button_click >> 1) & 0x1);
fanny868 0:9072e932503c 101 silang_click = (bool)((button_click >> 2) & 0x1);
fanny868 0:9072e932503c 102 kotak_click = (bool)((button_click >> 3) & 0x1);
fanny868 0:9072e932503c 103 atas_click = (bool)((button_click >> 4) & 0x1);
fanny868 0:9072e932503c 104 kanan_click = (bool)((button_click >> 5) & 0x1);
fanny868 0:9072e932503c 105 bawah_click = (bool)((button_click >> 6) & 0x1);
fanny868 0:9072e932503c 106 kiri_click = (bool)((button_click >> 7) & 0x1);
fanny868 0:9072e932503c 107
fanny868 0:9072e932503c 108 // Pengolahan data dari data "RL"
fanny868 0:9072e932503c 109 R1_click = (bool)((RL_click >> 0) & 0x1);
fanny868 0:9072e932503c 110 R3_click = (bool)((RL_click >> 1) & 0x1);
fanny868 0:9072e932503c 111 L1_click = (bool)((RL_click >> 2) & 0x1);
fanny868 0:9072e932503c 112 L3_click = (bool)((RL_click >> 3) & 0x1);
fanny868 0:9072e932503c 113 START_click = (bool)((RL_click >> 4) & 0x1);
fanny868 0:9072e932503c 114 SELECT_click = (bool)((RL_click >> 5) & 0x1);
fanny868 0:9072e932503c 115 PS_click = (bool)((RL_click >> 6) & 0x1);
fanny868 0:9072e932503c 116 }
fanny868 0:9072e932503c 117
fanny868 0:9072e932503c 118 /*********************************************************************************************/
fanny868 0:9072e932503c 119 /** **/
fanny868 0:9072e932503c 120 /** FUNGSI IDLE **/
fanny868 0:9072e932503c 121 /** - Fungsi dijalankan saat Arduino mengirimkan data yang merupakan **/
fanny868 0:9072e932503c 122 /** kondisi PS3 Disconnected **/
fanny868 0:9072e932503c 123 /** - Fungsi membuat semua data joystik bernilai 0 **/
fanny868 0:9072e932503c 124 /** **/
fanny868 0:9072e932503c 125 /*********************************************************************************************/
fanny868 0:9072e932503c 126
fanny868 0:9072e932503c 127 void idle(){
fanny868 0:9072e932503c 128 // Set 0
fanny868 0:9072e932503c 129 button = 0;
fanny868 0:9072e932503c 130 RL = 0;
fanny868 0:9072e932503c 131 button_click = 0;
fanny868 0:9072e932503c 132 RL_click = 0;
rahmadirizki18 13:8ab42383a2ca 133 R2_click =0;
rahmadirizki18 13:8ab42383a2ca 134 L2_click =0;
fanny868 0:9072e932503c 135 R2 = 0;
fanny868 0:9072e932503c 136 L2 = 0;
fanny868 0:9072e932503c 137 RX = 0;
fanny868 0:9072e932503c 138 RY = 0;
fanny868 0:9072e932503c 139 LX = 0;
fanny868 0:9072e932503c 140 LY = 0;
fanny868 0:9072e932503c 141
fanny868 0:9072e932503c 142 }
fanny868 0:9072e932503c 143
fanny868 0:9072e932503c 144 /*********************************************************************************************/
fanny868 0:9072e932503c 145 /** **/
fanny868 0:9072e932503c 146 /** FUNGSI PEMBACAAN DATA **/
fanny868 0:9072e932503c 147 /** - Fungsi pembacaan data yang dikirim dari arduino **/
fanny868 0:9072e932503c 148 /** - Data yang dikirim dari arduino merupakan paket data dengan format pengiriman **/
fanny868 0:9072e932503c 149 /** **/
fanny868 0:9072e932503c 150 /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/
fanny868 0:9072e932503c 151 /** | 0x88 | 0x08 | button | RL | button_click | RL_click | R2 | L2 | RX | RY | LX | LY | **/
fanny868 0:9072e932503c 152 /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/
fanny868 0:9072e932503c 153 /** **/
fanny868 0:9072e932503c 154 /** |------|------| **/
fanny868 0:9072e932503c 155 /** | 0x88 | 0x09 | **/
fanny868 0:9072e932503c 156 /** |------|------| **/
fanny868 0:9072e932503c 157 /** **/
fanny868 0:9072e932503c 158 /** - Jika urutan data yang diterima seperti tabel diatas, maka data tersebut akan **/
fanny868 0:9072e932503c 159 /** diolah untuk input ke aktuator **/
fanny868 0:9072e932503c 160 /** **/
fanny868 0:9072e932503c 161 /*********************************************************************************************/
fanny868 0:9072e932503c 162
fanny868 0:9072e932503c 163 void baca_data()
fanny868 0:9072e932503c 164 {
fanny868 0:9072e932503c 165 // Interrupt Serial
fanny868 0:9072e932503c 166 if(_serial.readable()&&(_serial.getc()==0x88)) {
fanny868 0:9072e932503c 167 // Pembacaan data dilakukan jika data awal yang diterima adalah 0x88 kemudian 0x08
fanny868 0:9072e932503c 168 if(_serial.getc()==0x08){
fanny868 0:9072e932503c 169 // Proses Pembacaan Data
fanny868 0:9072e932503c 170 button = _serial.getc();
fanny868 0:9072e932503c 171 RL = _serial.getc();
fanny868 0:9072e932503c 172 button_click = _serial.getc();
fanny868 0:9072e932503c 173 RL_click = _serial.getc();
fanny868 0:9072e932503c 174 R2 = _serial.getc();
fanny868 0:9072e932503c 175 L2 = _serial.getc();
fanny868 0:9072e932503c 176 RX = _serial.getc();
fanny868 0:9072e932503c 177 RY = _serial.getc();
fanny868 0:9072e932503c 178 LX = _serial.getc();
fanny868 0:9072e932503c 179 LY = _serial.getc();
fanny868 0:9072e932503c 180 } else if(_serial.getc()==0x09) {
fanny868 0:9072e932503c 181 // PS3 Disconnected
fanny868 0:9072e932503c 182 idle();
fanny868 0:9072e932503c 183 } else {
fanny868 0:9072e932503c 184 idle(); }
fanny868 0:9072e932503c 185 // Indikator - Print data pada monitor PC
MarchioKevin 20:54dc93e7b016 186 // debug.printf("%2x %2x %2x %2x %3d %3d %3d %3d %3d %3d\n\r",button, RL, button_click, RL_click, R2, L2, RX, RY, LX, LY);
fanny868 0:9072e932503c 187 }
fanny868 0:9072e932503c 188 }
franshendri 12:e07c59c28c29 189
MarchioKevin 20:54dc93e7b016 190
fanny868 0:9072e932503c 191 int readable(){
fanny868 0:9072e932503c 192 return _serial.readable();
fanny868 0:9072e932503c 193 }
fanny868 0:9072e932503c 194
fanny868 0:9072e932503c 195 public:
fanny868 0:9072e932503c 196 // Deklarasi variabel tombol joystik
fanny868 0:9072e932503c 197 bool kiri, kanan, atas, bawah;
fanny868 0:9072e932503c 198 bool segitiga, lingkaran, kotak, silang;
fanny868 0:9072e932503c 199 bool L1, R1, L3, R3, START, SELECT, PS;
fanny868 0:9072e932503c 200
fanny868 0:9072e932503c 201 bool kiri_click, kanan_click, atas_click, bawah_click;
fanny868 0:9072e932503c 202 bool segitiga_click, lingkaran_click, kotak_click, silang_click;
rahmadirizki18 13:8ab42383a2ca 203 bool L1_click, R1_click, L3_click, R3_click, R2_click, L2_click;
rahmadirizki18 13:8ab42383a2ca 204 bool R2sebelum,L2sebelum;
fanny868 0:9072e932503c 205 bool START_click, SELECT_click, PS_click;
fanny868 0:9072e932503c 206
fanny868 0:9072e932503c 207 protected:
fanny868 0:9072e932503c 208 virtual int _getc(){return _serial.getc();}
fanny868 0:9072e932503c 209 Serial _serial;
fanny868 0:9072e932503c 210 };
franshendri 12:e07c59c28c29 211
fanny868 0:9072e932503c 212 };
franshendri 12:e07c59c28c29 213
fanny868 0:9072e932503c 214 using namespace JoystickPS3;