Update 18 Februari 2017, PID laucnher dan Base sudah diperbarui

Dependencies:   Motor PID Joystick_OrdoV5 mbed millis

Fork of MainProgram_BaseBaru_otomatis-reloader by KRAI 2017

Committer:
rahmadirizki18
Date:
Fri Dec 09 10:27:56 2016 +0000
Revision:
13:8ab42383a2ca
Parent:
12:e07c59c28c29
Child:
20:54dc93e7b016
tambahan fungsi r2 click dan l2 click

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