Welcome, Guest. Please Login or Register
Arduino: Forum
06.09.2010 at 05:04:22


Pages: 1 2 3 4 5 
Playstation Controller (Read 13306 times)
elyas22
YaBB Newbies
*
Offline

Arduino rocks

Posts: 4

Re: Playstation Controller
Reply #30 - 14.02.2010 at 21:38:22
 
Hi, i have a very similar problem to Pol_c_j however i am using version 0018.

I can compile shutter's code, however when i attempt to compile the Psx library from arduino playground i get the same errors as above.

this is for every contributed library i place in \my documents\Arduino\Libraries\. NOT for a library that is included with the software.


can someone please tell me if it is a mistake on my part or if there is something wrong with the 0018 environment
Back to top
 
 
View Profile   IP Logged
Shutter
Junior Member
**
Offline

Simple Solution
Technologies

Posts: 71

Gender: male
Re: Playstation Controller
Reply #31 - 14.02.2010 at 23:59:59
 
Sounds like a problem with the enviorment
What are the compiler errors?
Back to top
 
 
View Profile   IP Logged
elyas22
YaBB Newbies
*
Offline

Arduino rocks

Posts: 4

Re: Playstation Controller
Reply #32 - 15.02.2010 at 12:34:15
 
thanks for replying

i get the following errors

c:/documents and settings/haroon/desktop/arduino-0018/hardware/tools/avr/lib/gcc/../../avr/includ
e/stdlib.h:111: error: expected unqualified-id before 'int'

c:/documents and settings/haroon/desktop/arduino-0018/hardware/tools/avr/lib/gcc/../../avr/includ
e/stdlib.h:111: error: expected `)' before 'int'

c:/documents and settings/haroon/desktop/arduino-0018/hardware/tools/avr/lib/gcc/../../avr/includ
e/stdlib.h:111: error: expected `)' before 'int'


i have tried other libraries and it works on and off eg AFMotor library
works, But the test library does not (from arduino website)

thanks
Back to top
 
 
View Profile   IP Logged
elyas22
YaBB Newbies
*
Offline

Arduino rocks

Posts: 4

Re: Playstation Controller
Reply #33 - 15.02.2010 at 13:49:35
 
Thanks Shutter for replying but i have found the solution to this problem (I think it works for me)

The solution was to replace

          #include "WConstants.h"
with     #include "WProgram.h"

in Psx.h
The member  mem explained the problem

"WConstants.h includes wiring.h and it is that file when included in a sketch that causes the problem.
The errors are because wiring.h defines macros for abs and round that conflict with function names in a system header file called math.h
Avoid  including wiring,h (or WConstants.h) prevents this problem."


thanks again
Back to top
 
 
View Profile   IP Logged
Shutter
Junior Member
**
Offline

Simple Solution
Technologies

Posts: 71

Gender: male
Re: Playstation Controller
Reply #34 - 15.02.2010 at 16:13:56
 
Ok, So should I make this code into an Update PSx lib or what?
Back to top
 
 
View Profile   IP Logged
Nani
YaBB Newbies
*
Offline

Arduino rocks

Posts: 15

Re: Playstation Controller
Reply #35 - 17.02.2010 at 03:14:30
 
yes please..  Smiley
is there still trouble with the Arduino Mega...?
do you think i should just use the Arduino Duemilanove ?

you've been super helpful!
i can't wait to get it working on thrusday!!
Back to top
 
 
View Profile   IP Logged
Shutter
Junior Member
**
Offline

Simple Solution
Technologies

Posts: 71

Gender: male
Re: Playstation Controller
Reply #36 - 17.02.2010 at 05:25:14
 
Nah, It is not a big deal, I THink it should work. Ill post the new code.
Back to top
 
 
View Profile   IP Logged
Nani
YaBB Newbies
*
Offline

Arduino rocks

Posts: 15

Re: Playstation Controller
Reply #37 - 18.02.2010 at 01:04:40
 
great!! you've been super helpful

i will be testing the code tomorrow.. hope all goes well
Back to top
 
 
View Profile   IP Logged
Shutter
Junior Member
**
Offline

Simple Solution
Technologies

Posts: 71

Gender: male
Re: Playstation Controller
Reply #38 - 18.02.2010 at 09:16:53
 
I optomized  PSButton() -

It is now:
Code:
    uint16_t buttons = *(uint16_t*)(PS2data+3);
    return (~buttons & button > 0); 


instead of
Code:
int byte = 3;
if (button >= 0x100) {
  byte = 4;
  button = button >> 8;
}
  if (~PS2data[byte] & button)
  return true;
  else
  return false;
 



Code:
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <avr/io.h>

#define LED_PIN 13
#define DELAY(wait) digitalWrite(LED_PIN,LOW); delay(wait); digitalWrite(LED_PIN,HIGH);

/* These are AVR PORTB pins, +8 to convert to Arduino pins */

#if defined(__AVR_ATmega1280__)
    //PortB pin mapping on arduino Mega
    #pragma message("Arduino Mega")
    #define PS2clk 7  //13
    #define PS2cmd 5  //11
    #define PS2att 4  //10
    #define PS2dat 6  //12
#else
    #pragma message("Arduino")
    //PortB mapping on arduino168/8 bassed boards
    #define PS2clk 5  //13
    #define PS2cmd 3  //11
    #define PS2att 2  //10
    #define PS2dat 4  //12
#endif

#define PS2PORT         PORTB
#define PS2IN           PINB
#define PS2DDR          DDRB
#define CTRL_CLK        20
#define CTRL_BYTE_DELAY 20

//These are our button constants
#define PSB_SELECT      0x0001
#define PSB_L3          0x0002
#define PSB_R3          0x0004
#define PSB_START       0x0008
#define PSB_PAD_UP      0x0010
#define PSB_PAD_RIGHT   0x0020
#define PSB_PAD_DOWN    0x0040
#define PSB_PAD_LEFT    0x0080
#define PSB_L2          0x0100
#define PSB_R2          0x0200
#define PSB_L1          0x0400
#define PSB_R1          0x0800
#define PSB_GREEN       0x1000
#define PSB_RED         0x2000
#define PSB_BLUE        0x4000
#define PSB_PINK        0x8000

#define SET(x,y) (x|=(1<<y))
#define CLR(x,y) (x&=(~(1<<y)))
#define CHK(x,y) (x & (1<<y))
#define TOG(x,y) (x^=(1<<y))

boolean PSButton(uint16_t button);
unsigned char PS2data[9];
void read_gamepad();
void config_gampad();
unsigned char get_gamepad_mode();
unsigned char i;

void setup() {
    Serial.begin(57600);
    pinMode(LED_PIN,OUTPUT);
    digitalWrite(LED_PIN,HIGH);

    //similar to pinmod(pin,OUTPUT)
    //Setting up the pin to be output
    SET(PS2DDR,PS2clk);
    SET(PS2DDR,PS2att);
    SET(PS2DDR,PS2cmd);
    //Sets pin to Input mode
    CLR(PS2DDR,PS2dat);

    SET(PS2PORT,PS2dat);

    config_gampad();
}

void loop () {
    read_gamepad();
    if(PSButton(PSB_SELECT))
     Serial.print("Select\n");
    if(PSButton(PSB_L3))
     Serial.print("L3\n");
    if(PSButton(PSB_R3))
     Serial.print("R3\n");
    if(PSButton(PSB_START))
     Serial.print("Start\n");
    if(PSButton(PSB_PAD_UP))
     Serial.print("Up\n");
    if(PSButton(PSB_PAD_RIGHT))
     Serial.print("Right\n");
    if(PSButton(PSB_PAD_LEFT))
     Serial.print("LEFT\n");
    if(PSButton(PSB_PAD_DOWN))
     Serial.print("DOWN\n");
    if(PSButton(PSB_L1))
     Serial.print("L1\n");
    if(PSButton(PSB_R1))
     Serial.print("R1\n");
    if(PSButton(PSB_L2))
     Serial.print("L2\n");
    if(PSButton(PSB_R2))
     Serial.print("R2\n");
    if(PSButton(PSB_GREEN))
     Serial.print("Triangle\n");
    if(PSButton(PSB_RED))
     Serial.print("Circle\n");
    if(PSButton(PSB_PINK))
     Serial.print("Square\n");
    if(PSButton(PSB_BLUE))
     Serial.print("X\n");
}

boolean PSButton(uint16_t button) {
    uint16_t buttons = *(uint16_t*)(PS2data+3);
    return (~buttons & button > 0);
}

unsigned char _gamepad_shiftinout (char byte) {
   unsigned char tmp = 0;
   for(i=0;i<8;i++) {
	if(CHK(byte,i)) SET(PS2PORT,PS2cmd);
	  else  CLR(PS2PORT,PS2cmd);
	CLR(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
	  if(CHK(PS2IN,PS2dat)) SET(tmp,i);
	SET(PS2PORT,PS2clk);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
   return tmp;
}
void _gamepad_shiftout (char byte) {
   for(i=0;i<8;i++) {
	if(CHK(byte,i)) SET(PS2PORT,PS2cmd);
	  else  CLR(PS2PORT,PS2cmd);
	CLR(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
	SET(PS2PORT,PS2clk);
	//delayMicroseconds(CTRL_CLK);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
}
unsigned char _gamepad_shiftin() {
   unsigned char tmp = 0;
   for(i=0;i<8;i++) {
	CLR(PS2PORT,PS2cmd);
	CLR(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
	  if(CHK(PS2IN,PS2dat)) SET(tmp,i);
	SET(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
   return tmp;
}
void read_gamepad() {
    SET(PS2PORT,PS2cmd);
    SET(PS2PORT,PS2clk);
    CLR(PS2PORT,PS2att); // low enable joystick
    delayMicroseconds(CTRL_BYTE_DELAY);
    //Send the command to send button and joystick data;
    char dword[9] = {0x01,0x42,0,0,0,0,0,0,0};
    for (int i = 0; i<9; i++) {
	  PS2data[i] = _gamepad_shiftinout(dword[i]);
    }
    SET(PS2PORT,PS2att); // HI disable joystick
}
unsigned char get_gamepad_mode() {
    SET(PS2PORT,PS2cmd);
    SET(PS2PORT,PS2clk);
    CLR(PS2PORT,PS2att); // low enable joystick
    _gamepad_shiftout(0x01);
    unsigned char x = _gamepad_shiftin();
    SET(PS2PORT,PS2att); // HI disable joystick
    return x;
}
void config_gampad() {
   SET(PS2PORT,PS2cmd);
   SET(PS2PORT,PS2clk);
   CLR(PS2PORT,PS2att); // low enable joystick
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x43);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x00);
    // Lock to Analog Mode on Stick
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x44);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x03);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    // Vibration
    /*
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x4D);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    */
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x4F);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0xFF);
    _gamepad_shiftout(0xFF);
    _gamepad_shiftout(0x03);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x43);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
   SET(PS2PORT,PS2att);
} 



A well made guide
Back to top
 
 
View Profile   IP Logged
Nani
YaBB Newbies
*
Offline

Arduino rocks

Posts: 15

Re: Playstation Controller
Reply #39 - 23.02.2010 at 20:23:49
 
has anyone tested the new code..
it is not working for me.. Sad
Back to top
 
 
View Profile   IP Logged
Shutter
Junior Member
**
Offline

Simple Solution
Technologies

Posts: 71

Gender: male
Re: Playstation Controller
Reply #40 - 23.02.2010 at 20:30:06
 
IMPORTANT, LAST CODE HAD BUGS IN IT!

In PSButton:
return (~buttons & button > 0); -> return ((~buttons & button) > 0);

Tested on:
Arduino Decimila (should work for any atmega168 or atmega 8 board if pinouts are the same!)
Arduino MEGA (VERY BUGGY! Needs to be looked at under oscilloscope)

On the mega when you click the analog buttons all buttons come trough clearly, except the fact select is always pressed. Select happens to be a signed bit if the button intiger ws signed, but it is unsigned :/


Code:
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <avr/io.h>

#define LED_PIN 13
#define DELAY(wait) digitalWrite(LED_PIN,LOW); delay(wait); digitalWrite(LED_PIN,HIGH);

/* These are AVR PORTB pins, +8 to convert to Arduino pins */

#if defined(__AVR_ATmega1280__)
    //PortB pin mapping on arduino Mega
    #pragma message("Arduino Mega")
    #define PS2clk 7  //13
    #define PS2cmd 5  //11
    #define PS2att 4  //10
    #define PS2dat 6  //12
#else
    #pragma message("Arduino")
    //PortB mapping on arduino168/8 bassed boards
    #define PS2clk 5  //13
    #define PS2cmd 3  //11
    #define PS2att 2  //10
    #define PS2dat 4  //12
#endif

#define PS2PORT         PORTB
#define PS2IN           PINB
#define PS2DDR          DDRB
#define CTRL_CLK        20
#define CTRL_BYTE_DELAY 20

//These are our button constants
#define PSB_SELECT      0x0001
#define PSB_L3          0x0002
#define PSB_R3          0x0004
#define PSB_START       0x0008
#define PSB_PAD_UP      0x0010
#define PSB_PAD_RIGHT   0x0020
#define PSB_PAD_DOWN    0x0040
#define PSB_PAD_LEFT    0x0080
#define PSB_L2          0x0100
#define PSB_R2          0x0200
#define PSB_L1          0x0400
#define PSB_R1          0x0800
#define PSB_GREEN       0x1000
#define PSB_RED         0x2000
#define PSB_BLUE        0x4000
#define PSB_PINK        0x8000

#define SET(x,y) (x|=(1<<y))
#define CLR(x,y) (x&=(~(1<<y)))
#define CHK(x,y) (x & (1<<y))
#define TOG(x,y) (x^=(1<<y))

boolean PSButton(uint16_t button);
unsigned char PS2data[9];
void read_gamepad();
void config_gampad();
unsigned char get_gamepad_mode();
unsigned char i;

void setup() {
    Serial.begin(57600);
    pinMode(LED_PIN,OUTPUT);
    digitalWrite(LED_PIN,HIGH);

    //similar to pinmod(pin,OUTPUT)
    //Setting up the pin to be output
    SET(PS2DDR,PS2clk);
    SET(PS2DDR,PS2att);
    SET(PS2DDR,PS2cmd);
    //Sets pin to Input mode
    CLR(PS2DDR,PS2dat);

    SET(PS2PORT,PS2dat);

    config_gampad();
}

void loop () {
    read_gamepad();
    if(PSButton(PSB_SELECT))
     Serial.print("Select\n");
    if(PSButton(PSB_L3))
     Serial.print("L3\n");
    if(PSButton(PSB_R3))
     Serial.print("R3\n");
    if(PSButton(PSB_START))
     Serial.print("Start\n");
    if(PSButton(PSB_PAD_UP))
     Serial.print("Up\n");
    if(PSButton(PSB_PAD_RIGHT))
     Serial.print("Right\n");
    if(PSButton(PSB_PAD_LEFT))
     Serial.print("LEFT\n");
    if(PSButton(PSB_PAD_DOWN))
     Serial.print("DOWN\n");
    if(PSButton(PSB_L1))
     Serial.print("L1\n");
    if(PSButton(PSB_R1))
     Serial.print("R1\n");
    if(PSButton(PSB_L2))
     Serial.print("L2\n");
    if(PSButton(PSB_R2))
     Serial.print("R2\n");
    if(PSButton(PSB_GREEN))
     Serial.print("Triangle\n");
    if(PSButton(PSB_RED))
     Serial.print("Circle\n");
    if(PSButton(PSB_PINK))
     Serial.print("Square\n");
    if(PSButton(PSB_BLUE))
     Serial.print("X\n");
}

boolean PSButton(uint16_t button) {
    uint16_t buttons = *(uint16_t*)(PS2data+3);
    return ((~buttons & button) > 0);
}

unsigned char _gamepad_shiftinout (char byte) {
   unsigned char tmp = 0;
   for(i=0;i<8;i++) {
	if(CHK(byte,i)) SET(PS2PORT,PS2cmd);
	  else  CLR(PS2PORT,PS2cmd);
	CLR(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
	  if(CHK(PS2IN,PS2dat)) SET(tmp,i);
	SET(PS2PORT,PS2clk);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
   return tmp;
}
void _gamepad_shiftout (char byte) {
   for(i=0;i<8;i++) {
	if(CHK(byte,i)) SET(PS2PORT,PS2cmd);
	  else  CLR(PS2PORT,PS2cmd);
	CLR(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
	SET(PS2PORT,PS2clk);
	//delayMicroseconds(CTRL_CLK);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
}
unsigned char _gamepad_shiftin() {
   unsigned char tmp = 0;
   for(i=0;i<8;i++) {
	CLR(PS2PORT,PS2cmd);
	CLR(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
	  if(CHK(PS2IN,PS2dat)) SET(tmp,i);
	SET(PS2PORT,PS2clk);
	delayMicroseconds(CTRL_CLK);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
   return tmp;
}
void read_gamepad() {
    SET(PS2PORT,PS2cmd);
    SET(PS2PORT,PS2clk);
    CLR(PS2PORT,PS2att); // low enable joystick
    delayMicroseconds(CTRL_BYTE_DELAY);
    //Send the command to send button and joystick data;
    char dword[9] = {0x01,0x42,0,0,0,0,0,0,0};
    for (int i = 0; i<9; i++) {
	  PS2data[i] = _gamepad_shiftinout(dword[i]);
    }
    SET(PS2PORT,PS2att); // HI disable joystick
}
unsigned char get_gamepad_mode() {
    SET(PS2PORT,PS2cmd);
    SET(PS2PORT,PS2clk);
    CLR(PS2PORT,PS2att); // low enable joystick
    _gamepad_shiftout(0x01);
    unsigned char x = _gamepad_shiftin();
    SET(PS2PORT,PS2att); // HI disable joystick
    return x;
}
void config_gampad() {
   SET(PS2PORT,PS2cmd);
   SET(PS2PORT,PS2clk);
   CLR(PS2PORT,PS2att); // low enable joystick
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x43);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x00);
    // Lock to Analog Mode on Stick
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x44);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x03);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    // Vibration
    /*
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x4D);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    */
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x4F);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0xFF);
    _gamepad_shiftout(0xFF);
    _gamepad_shiftout(0x03);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x43);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
   SET(PS2PORT,PS2att);
}
 

Back to top
 
 
View Profile   IP Logged
Nani
YaBB Newbies
*
Offline

Arduino rocks

Posts: 15

Re: Playstation Controller
Reply #41 - 23.02.2010 at 23:36:06
 
great.. yo are like and arduino angel!! Grin  going to test it later tonight!
so to sum up
i should just copy and paste the code exactly as is

then:
    the clk is in pin 13 in PWN
    the data is in pin 12 in PWN
    the cmd is in pin 11 in PWN
    the clk is in pin 10 in PWN
    the pwr is in pin 5+ straight from the arduino
    the grd is in the grd next to 5+

does the arduinomega need any more external power besides the usb?
  exactly what brand controller are you using?
   ahh.. also does this code read the thumbsticks?

thank you soo soo much
Back to top
 
 
View Profile   IP Logged
Guy Cothal
YaBB Newbies
*
Offline

Arduino rocks

Posts: 3

Re: Playstation Controller
Reply #42 - 03.03.2010 at 09:58:51
 
anyone tested this on the Mega yet???
Back to top
 
 
View Profile   IP Logged
Ian W.
Junior Member
**
Offline

Arduino Mega rocks

Posts: 52

Re: Playstation Controller
Reply #43 - 11.03.2010 at 22:37:29
 
Plus one waiting for a mega update, because if this works im going to give up on the Wii Nunchuck on the mega
Back to top
 
 
View Profile | AIM   IP Logged
mannyr7
YaBB Newbies
*
Offline

Arduino rocks

Posts: 5

Re: Playstation Controller
Reply #44 - 14.03.2010 at 05:32:41
 
Ok, so shutter's code compiles fine on Arduino 018. Now how can I integrate this to my sketch to control my robot? I mean how would I structure my 'if...else' statements to call my movement functions based on the analog sticks' input?

UPDATE: I've upload to my Freeduino w/328P board, no problem. Hooked up power, ground, and the following.
#define PS2clk 13 //BLUE
#define PS2cmd 11 //ORG
#define PS2att 10 //YELLOW
#define PS2dat 12 //BROWN
When I open the terminal, I get a very fast constant stream of various random button presses.
Oh yeah, I'm using a Lynxmotion ps2 wireless controller which does sync immediately.
Back to top
 
« Last Edit: 14.03.2010 at 07:12:23 by mannyr7 »  
View Profile   IP Logged
Pages: 1 2 3 4 5