Saturday 26 October 2013

[Arduino Project] Interface Joystick PlayStation dengan Arduino


Dapat project dari temen disuruh buat robosoccer. Dia punya ide memakai stick PlayStation yang wireless. Lumayanlah stick wireless sekarang sudah semakin murah dipasaran.

Browsing di internet ternyata stick PS menggunakan komunikasi SPI sebagai interfacenya. Bill porter ternyata sudah meriset ini dengan sangat baik dan membuatkan librarinya. Bill menggunakan SPI dengan software, artinya bukan pada pin MOSI MISO SCK pada arduino melainkan menggunakan pin bebas sesuai selera kita. Website Bill Porter dapat di buka disini. Librarinya bisa di didownload disitu juga. Apabila menginginkan program saya sekaligus library yang sudah saya edit bisa didownload disini

Gambar dibawah merupakan wiring dari pinout konektor PS2. Diambil dari website Bill porter


Saya menggunakan kabel untuk menjumper arduino ke connector PS tersebut. Konektornya wiringnya sebagai berikut:

Sedangkan pada arduino wiringnya sebagai berikut:

Saya mengedit software examplenya dengan mendeteksi tombol ditekan dan tombol dilepaskan sebagai berikut:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <PS2X_lib.h>  //for v1.6

PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int error = 0;
byte type = 0;
byte vibrate = 0;
int LY, LX, RY, RX, LYold, LXold, RYold, RXold;

void setup(){
  Serial.begin(115200);

  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  delay(500);

  error = ps2x.config_gamepad(13,11,10,12, false, false);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error

  if(error == 0){
    Serial.println("Found Controller, configured successful");
  }

  else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");

  else if(error == 2)
    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  else if(error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

  //Serial.print(ps2x.Analog(1), HEX);

  type = ps2x.readType();
  switch(type) {
  case 0:
    Serial.println("Unknown Controller type");
    break;
  case 1:
    Serial.println("DualShock Controller Found");
    break;
  case 2:
    Serial.println("GuitarHero Controller Found");
    break;
  }

}

void loop()
{
  ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed

  if(ps2x.ButtonPressed(PSB_PAD_UP))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_UP just pressed");
  if(ps2x.ButtonReleased(PSB_PAD_UP))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_UP just released");

  if(ps2x.ButtonPressed(PSB_PAD_DOWN))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_DOWN just pressed");
  if(ps2x.ButtonReleased(PSB_PAD_DOWN))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_DOWN just released");

  if(ps2x.ButtonPressed(PSB_PAD_LEFT))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_LEFT just pressed");
  if(ps2x.ButtonReleased(PSB_PAD_LEFT))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_LEFT just released");

  if(ps2x.ButtonPressed(PSB_PAD_RIGHT))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_RIGHT just pressed");
  if(ps2x.ButtonReleased(PSB_PAD_RIGHT))             //will be TRUE if button was JUST pressed
    Serial.println("PAD_RIGHT just released");

  if(ps2x.ButtonPressed(PSB_START))             //will be TRUE if button was JUST pressed
    Serial.println("START just pressed");
  if(ps2x.ButtonReleased(PSB_START))             //will be TRUE if button was JUST pressed
    Serial.println("START just released");

  if(ps2x.ButtonPressed(PSB_SELECT))             //will be TRUE if button was JUST pressed
    Serial.println("SELECT just pressed");
  if(ps2x.ButtonReleased(PSB_SELECT))             //will be TRUE if button was JUST pressed
    Serial.println("SELECT just released");

  if(ps2x.ButtonPressed(PSB_TRIANGLE))             //will be TRUE if button was JUST pressed
    Serial.println("Triangle just pressed");
  if(ps2x.ButtonReleased(PSB_TRIANGLE))             //will be TRUE if button was JUST pressed
    Serial.println("Triangle just released");

  if(ps2x.ButtonPressed(PSB_CIRCLE))             //will be TRUE if button was JUST pressed
    Serial.println("Circle just pressed");
  if(ps2x.ButtonReleased(PSB_CIRCLE))             //will be TRUE if button was JUST pressed
    Serial.println("Circle just released");

  if(ps2x.ButtonPressed(PSB_SQUARE))             //will be TRUE if button was JUST pressed
    Serial.println("Square just pressed");
  if(ps2x.ButtonReleased(PSB_SQUARE))             //will be TRUE if button was JUST released
    Serial.println("Square just released");     

  if(ps2x.ButtonPressed(PSB_CROSS))             //will be TRUE if button was JUST pressed
    Serial.println("X just pressed");
  if(ps2x.ButtonReleased(PSB_CROSS))             //will be TRUE if button was JUST released
    Serial.println("X just released");     

  if(ps2x.ButtonPressed(PSB_L1))             //will be TRUE if button was JUST pressed
    Serial.println("L1 just pressed");
  if(ps2x.ButtonReleased(PSB_L1))             //will be TRUE if button was JUST released
    Serial.println("L1 just released");  

  if(ps2x.ButtonPressed(PSB_R1))             //will be TRUE if button was JUST pressed
    Serial.println("R1 just pressed");
  if(ps2x.ButtonReleased(PSB_R1))             //will be TRUE if button was JUST released
    Serial.println("R1 just released");

  if(ps2x.ButtonPressed(PSB_L2))             //will be TRUE if button was JUST pressed
    Serial.println("L2 just pressed");
  if(ps2x.ButtonReleased(PSB_L2))             //will be TRUE if button was JUST released
    Serial.println("L2 just released");  

  if(ps2x.ButtonPressed(PSB_R2))             //will be TRUE if button was JUST pressed
    Serial.println("R2 just pressed");
  if(ps2x.ButtonReleased(PSB_R2))             //will be TRUE if button was JUST released
    Serial.println("R2 just released");

  if(ps2x.ButtonPressed(PSB_L3))             //will be TRUE if button was JUST pressed
    Serial.println("L3 just pressed");
  if(ps2x.ButtonReleased(PSB_L3))             //will be TRUE if button was JUST released
    Serial.println("L3 just released");  

  if(ps2x.ButtonPressed(PSB_R3))             //will be TRUE if button was JUST pressed
    Serial.println("R3 just pressed");
  if(ps2x.ButtonReleased(PSB_R3))             //will be TRUE if button was JUST released
    Serial.println("R3 just released");  

  LY = (int) ps2x.Analog(PSS_LY);
  LX = (int) ps2x.Analog(PSS_LX);
  RY = (int) ps2x.Analog(PSS_RY);
  RX = (int) ps2x.Analog(PSS_RX);

  if(LX != LXold || LY != LYold)
  {
    Serial.print("L Stick Values:");
    Serial.print(LY, DEC);
    Serial.print(",");
    Serial.println(LX, DEC);
  }
  if(RX != RXold || RY != RYold)
  {
    Serial.print("R Stick Values:");
    Serial.print(RY, DEC);
    Serial.print(",");
    Serial.println(RX, DEC);  
  }

  LYold = LY;
  LXold = LX;
  RYold = RY;
  RXold = RX;


  delay(20);

}

Program diatas hanya men-scan tombol dan mendeteksi perubahan tombolnya termasuk perubahan analog joysticknya. Saya juga mematikan beberapa fitur seperti getar dan analog pada tombol PAD(tombol arah). Akan tetapi program diatas agak sering beberapa kali tidak mendeteksi stick PSnya, wahhhh. Akhirnya saya browsing lagi menemukan beberapa stick PS menggunakan SPI clock yang lambat. Saya mengambil kesimpulan kalau library Bill Porter ini terlalu cepat buat stick PS punya saya (buatan china). Akhirnya saya terpaksa mengedit Librarinya akhirnya menemukan cara memperlambat SCKnya. Saya mengedit PS2X_lib.h sebagai berikut :

92
93
94
95
96
97
#ifdef __AVR__
  // AVR
  #include <avr/io.h>
  #define CTRL_CLK        4
  #define CTRL_BYTE_DELAY 3
#else

saya edit menjadi:

92
93
94
95
96
97
#ifdef __AVR__
  // AVR
  #include <avr/io.h>
  #define CTRL_CLK        10
  #define CTRL_BYTE_DELAY 10
#else

Akhirnya stick PS langsung terdeteksi setiap kali di reset Arduinonya. Tombolnya juga responsif, joystick analognya akan di print apabila ketika ada perubahan saja, jadi data tidak mengirim terus-terusan.


Selamat Mencoba.


8 comments:

  1. Wah menarik sekali pak Arif .

    ReplyDelete
  2. terimakasih pak, sangat membantu memecahkan masalah

    ReplyDelete
  3. sangat membantu mas ijin copy paste sama buat refrensi dan ijin mau nyoba masss.... :)

    salam programer.... :)

    ReplyDelete
  4. Saya mencoba dan hasilnya "No controller found" apa ada solusi? Terimakasih

    ReplyDelete
  5. Wah bagus mas. Waktu dulu saya oprek stick ps arduino masi belum populer, jadi bikin sistem minimum sendiri. untuk referensi bisa disini https://hamdi88.wordpress.com/2010/12/05/interfacing_joy_stick_ps2/

    ReplyDelete
  6. No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips
    Unknown Controller type
    L Stick Values:255,255
    R Stick Values:255,255

    saya sudah mengikuti langkah2 nya tetapi masih muncul tulisan diatas, mohon penjelasannya, stick ps2 yg saya gunakan persis sama dengan anda

    ReplyDelete

Develop STM32 dengan STM32cubeMx + OpenSTM32 IDE

Banyak temen ² yang masih ngoprek STM32 di Keil, yang harus nyari software crack ²an. Bebas sih, tapi saya sepaham dengan dosen saya saat ...