Source Code Robot Lama

Dependencies:   DigitDisplay Motor PID Ping mbed millis

Fork of DagonFly__RoadToJapan_15Mei_Ultimate by KRAI 2017

Committer:
franshendri
Date:
Sat Dec 03 05:43:10 2016 +0000
Revision:
12:e07c59c28c29
Parent:
3:1287fccc11be
Child:
13:8ab42383a2ca
tes H

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
fanny868 0:9072e932503c 84 segitiga_click = (bool)((button_click >> 0) & 0x1);
fanny868 0:9072e932503c 85 lingkaran_click = (bool)((button_click >> 1) & 0x1);
fanny868 0:9072e932503c 86 silang_click = (bool)((button_click >> 2) & 0x1);
fanny868 0:9072e932503c 87 kotak_click = (bool)((button_click >> 3) & 0x1);
fanny868 0:9072e932503c 88 atas_click = (bool)((button_click >> 4) & 0x1);
fanny868 0:9072e932503c 89 kanan_click = (bool)((button_click >> 5) & 0x1);
fanny868 0:9072e932503c 90 bawah_click = (bool)((button_click >> 6) & 0x1);
fanny868 0:9072e932503c 91 kiri_click = (bool)((button_click >> 7) & 0x1);
fanny868 0:9072e932503c 92
fanny868 0:9072e932503c 93 // Pengolahan data dari data "RL"
fanny868 0:9072e932503c 94 R1_click = (bool)((RL_click >> 0) & 0x1);
fanny868 0:9072e932503c 95 R3_click = (bool)((RL_click >> 1) & 0x1);
fanny868 0:9072e932503c 96 L1_click = (bool)((RL_click >> 2) & 0x1);
fanny868 0:9072e932503c 97 L3_click = (bool)((RL_click >> 3) & 0x1);
fanny868 0:9072e932503c 98 START_click = (bool)((RL_click >> 4) & 0x1);
fanny868 0:9072e932503c 99 SELECT_click = (bool)((RL_click >> 5) & 0x1);
fanny868 0:9072e932503c 100 PS_click = (bool)((RL_click >> 6) & 0x1);
fanny868 0:9072e932503c 101 }
fanny868 0:9072e932503c 102
fanny868 0:9072e932503c 103 /*********************************************************************************************/
fanny868 0:9072e932503c 104 /** **/
fanny868 0:9072e932503c 105 /** FUNGSI IDLE **/
fanny868 0:9072e932503c 106 /** - Fungsi dijalankan saat Arduino mengirimkan data yang merupakan **/
fanny868 0:9072e932503c 107 /** kondisi PS3 Disconnected **/
fanny868 0:9072e932503c 108 /** - Fungsi membuat semua data joystik bernilai 0 **/
fanny868 0:9072e932503c 109 /** **/
fanny868 0:9072e932503c 110 /*********************************************************************************************/
fanny868 0:9072e932503c 111
fanny868 0:9072e932503c 112 void idle(){
fanny868 0:9072e932503c 113 // Set 0
fanny868 0:9072e932503c 114 button = 0;
fanny868 0:9072e932503c 115 RL = 0;
fanny868 0:9072e932503c 116 button_click = 0;
fanny868 0:9072e932503c 117 RL_click = 0;
fanny868 0:9072e932503c 118 R2 = 0;
fanny868 0:9072e932503c 119 L2 = 0;
fanny868 0:9072e932503c 120 RX = 0;
fanny868 0:9072e932503c 121 RY = 0;
fanny868 0:9072e932503c 122 LX = 0;
fanny868 0:9072e932503c 123 LY = 0;
fanny868 0:9072e932503c 124
fanny868 0:9072e932503c 125 }
fanny868 0:9072e932503c 126
fanny868 0:9072e932503c 127
fanny868 0:9072e932503c 128 /*********************************************************************************************/
fanny868 0:9072e932503c 129 /** **/
fanny868 0:9072e932503c 130 /** FUNGSI PEMBACAAN DATA **/
fanny868 0:9072e932503c 131 /** - Fungsi pembacaan data yang dikirim dari arduino **/
fanny868 0:9072e932503c 132 /** - Data yang dikirim dari arduino merupakan paket data dengan format pengiriman **/
fanny868 0:9072e932503c 133 /** **/
fanny868 0:9072e932503c 134 /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/
fanny868 0:9072e932503c 135 /** | 0x88 | 0x08 | button | RL | button_click | RL_click | R2 | L2 | RX | RY | LX | LY | **/
fanny868 0:9072e932503c 136 /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/
fanny868 0:9072e932503c 137 /** **/
fanny868 0:9072e932503c 138 /** |------|------| **/
fanny868 0:9072e932503c 139 /** | 0x88 | 0x09 | **/
fanny868 0:9072e932503c 140 /** |------|------| **/
fanny868 0:9072e932503c 141 /** **/
fanny868 0:9072e932503c 142 /** - Jika urutan data yang diterima seperti tabel diatas, maka data tersebut akan **/
fanny868 0:9072e932503c 143 /** diolah untuk input ke aktuator **/
fanny868 0:9072e932503c 144 /** **/
fanny868 0:9072e932503c 145 /*********************************************************************************************/
fanny868 0:9072e932503c 146
fanny868 0:9072e932503c 147 void baca_data()
fanny868 0:9072e932503c 148 {
fanny868 0:9072e932503c 149 // Interrupt Serial
fanny868 0:9072e932503c 150 if(_serial.readable()&&(_serial.getc()==0x88)) {
fanny868 0:9072e932503c 151 // Pembacaan data dilakukan jika data awal yang diterima adalah 0x88 kemudian 0x08
fanny868 0:9072e932503c 152 if(_serial.getc()==0x08){
fanny868 0:9072e932503c 153 // Proses Pembacaan Data
fanny868 0:9072e932503c 154 button = _serial.getc();
fanny868 0:9072e932503c 155 RL = _serial.getc();
fanny868 0:9072e932503c 156 button_click = _serial.getc();
fanny868 0:9072e932503c 157 RL_click = _serial.getc();
fanny868 0:9072e932503c 158 R2 = _serial.getc();
fanny868 0:9072e932503c 159 L2 = _serial.getc();
fanny868 0:9072e932503c 160 RX = _serial.getc();
fanny868 0:9072e932503c 161 RY = _serial.getc();
fanny868 0:9072e932503c 162 LX = _serial.getc();
fanny868 0:9072e932503c 163 LY = _serial.getc();
fanny868 0:9072e932503c 164 } else if(_serial.getc()==0x09) {
fanny868 0:9072e932503c 165 // PS3 Disconnected
fanny868 0:9072e932503c 166 idle();
fanny868 0:9072e932503c 167 } else {
fanny868 0:9072e932503c 168 idle(); }
fanny868 0:9072e932503c 169 // Indikator - Print data pada monitor PC
fanny868 0:9072e932503c 170 // 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 171 }
fanny868 0:9072e932503c 172 }
franshendri 12:e07c59c28c29 173
franshendri 12:e07c59c28c29 174
fanny868 0:9072e932503c 175
fanny868 0:9072e932503c 176 int readable(){
fanny868 0:9072e932503c 177 return _serial.readable();
fanny868 0:9072e932503c 178 }
fanny868 0:9072e932503c 179
fanny868 0:9072e932503c 180 public:
fanny868 0:9072e932503c 181 // Deklarasi variabel tombol joystik
fanny868 0:9072e932503c 182 bool kiri, kanan, atas, bawah;
fanny868 0:9072e932503c 183 bool segitiga, lingkaran, kotak, silang;
fanny868 0:9072e932503c 184 bool L1, R1, L3, R3, START, SELECT, PS;
fanny868 0:9072e932503c 185
fanny868 0:9072e932503c 186 bool kiri_click, kanan_click, atas_click, bawah_click;
fanny868 0:9072e932503c 187 bool segitiga_click, lingkaran_click, kotak_click, silang_click;
fanny868 0:9072e932503c 188 bool L1_click, R1_click, L3_click, R3_click;
fanny868 0:9072e932503c 189 bool START_click, SELECT_click, PS_click;
fanny868 0:9072e932503c 190
fanny868 0:9072e932503c 191 protected:
fanny868 0:9072e932503c 192 virtual int _getc(){return _serial.getc();}
fanny868 0:9072e932503c 193 Serial _serial;
fanny868 0:9072e932503c 194 };
franshendri 12:e07c59c28c29 195
fanny868 0:9072e932503c 196 };
franshendri 12:e07c59c28c29 197
fanny868 0:9072e932503c 198 using namespace JoystickPS3;