Go Down

Topic: USB host Shield and PS3 controller, How to use rumble function in <PS3USB.h> lib (Read 4131 times) previous topic - next topic

Ductor

Jan 28, 2015, 12:37 am Last Edit: Jan 28, 2015, 10:38 am by Ductor Reason: uppdate subject field
I'm using Uno and doing som testing with the PS3 controller connected over usb wire (Do not have BT dongle yet).  Using usb host shield library.  have no trouble using buttons , both analog and digital, and also gyro and manage to control a servo . Have tried the library on a mini, works well there also.
I want to use an input on the Arduino to activate the rumble motor in the PS3 controller.
I cant figure out the right code syntax to do this, have searched but doesn't find any code examples on this?
Anyone done this?

current test sketch
just one servo to control and one led output

Code: [Select]

#include <PS3USB.h>

USB Usb;
/* You can create the instance of the class in two ways */
PS3USB PS3(&Usb); // This will just create the instance
//PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch

#include <Servo.h>
Servo servo;  // create servo object to control a servo

const uint8_t LED = 2;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  servo.attach(9); // Use pin 9, as signal pin
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}

void loop() {
  Usb.Task();
  if (PS3.PS3Connected)
    digitalWrite(LED, PS3.getButtonPress(CROSS));
  else
    digitalWrite(LED, LOW);
  if (PS3.PS3Connected)
    servo.write(map(PS3.getAngle(Pitch), 0, 359, 170, 0));
  else
    servo.write(85); // Set to middle position
}




Mrbobokins

What is your rumble motor connected to. Because you could connect its wires to ground and a digital pin to make it spin when you tell it to.

Ductor

#2
Jan 28, 2015, 10:36 am Last Edit: Jan 28, 2015, 12:45 pm by Ductor Reason: codelines from github
The rumble motor(s) are inside the PS3 controller (the force feedback in controller)..  So this is atually a question on how to use the <PS3USB.h> library.

The library have some functiuns on how to activate the motor(s),  something like this
I think this is the rumble code inside the <PS3USB.h> library.
line 199 to 213 copied from https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS3USB.h
Code: [Select]

        void setRumbleOff();
        /**
         * Turn on rumble.
         * @param mode Either ::RumbleHigh or ::RumbleLow.
         */
        void setRumbleOn(RumbleEnum mode);
        /**
         * Turn on rumble using custom duration and power.
         * @param rightDuration The duration of the right/low rumble effect.
         * @param rightPower The intensity of the right/low rumble effect.
         * @param leftDuration The duration of the left/high rumble effect.
         * @param leftPower The intensity of the left/high rumble effect.
         */
        void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);



But I havent found any examples on the use of the rumble motors..  I got everthing else to work on the controller, every buttons, and also to control the led on the controller.
Its an orginal sony controller.

Ductor

Today I got some small BT dongles, and the they all work just perfect connecting the Uno to the PS3 controller wireless.

After some testing I find out that the PS3.getAnalogButton(R2_ANALOG) and PS3.getAngle(Pitch) do not work with the library <PS3BT.h>, but works fine with the library <PS3USB.h>. Meaning when I'm using the controller connected with usb cabel all functions works(except still not figured out the rumble), but not when using wireless..
Have changed my code a little bit with the input and output to use more pwm as servocontrol.
Code: [Select]

//#include
#include <PS3BT.h>
USB Usb;
/* You can create the instance of the class in two ways */
//PS3USB PS3(&Usb); // This will just create the instance
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
PS3BT PS3(&Btd); // This will just create the instance
//PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
#include
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
Servo servo3; // create servo object to control a servo
const uint8_t LED = 2;
const uint8_t Shake = 4;
const uint8_t Buzzer = 7;
// variables will change:
//int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(LED, OUTPUT); // just a led to test buttons..
pinMode(Buzzer, OUTPUT); // A buzzer connected to the output
pinMode(Shake, INPUT); // the input I wanna use to activate the rumble in the controller(an IR-08H sensor is connected)
Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while(1); //halt
}
servo1.attach(3); // Use pin 3, as signal pin
servo2.attach(5); // Use pin 5, as signal pin
servo3.attach(6); // Use pin 6, as signal pin
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}
void loop() {
// setRumbleOn();
Usb.Task();
if (PS3.PS3Connected)
digitalWrite(LED, PS3.getButtonPress(CROSS));
else
digitalWrite(LED, LOW);
if (PS3.PS3Connected)
servo1.write(map(PS3.getAnalogButton(R2_ANALOG), 0, 255, 180, 0));
else
servo1.write(180); // Set to position
if (PS3.PS3Connected)
servo2.write(map(PS3.getAnalogHat(RightHatX), 0, 255, 180, 0));
else
servo2.write(90); // Set to position
if (PS3.PS3Connected)
servo3.write(map(PS3.getAngle(Pitch), 0, 255, 180, 0));
else
servo3.write(90); // Set to position
//read the input value into a variable
int sensorVal = digitalRead(4);
if (sensorVal == HIGH) {
digitalWrite(Buzzer, HIGH);
}
else {
digitalWrite(Buzzer, LOW);
}
}
void setRumbleOn();


Do I have to change things inside the library files?

Ductor

Just realized that i had an older USB Host shield library installed..
Removed that one and installed USB Host Shield 2.0 Master.

But still the same..  only differense I can se is that the function PS3.getAnalogButton(R2_ANALOG) hve a new name like this PS3.getAnalogButton(R2) and my Binary sketch size: 31 248 bytes (of a 32 256 byte maximum)
has get alot bigger... with the first vesion I used the file was around 26 Kbytes.


Ductor

Finally  I got the handle on the rumble function!! :-)

Pretty darn easy actually! There are two rumble motors inside the controller, one #heavy" and one "light".
This command and you got the most "rumble" for the longest duration:
PS3.setRumbleOn(RumbleHigh)

With this you got the "hardest" light rumble for long duration:
PS3.setRumbleOn(RumbleLow)

If you use numbers instead, you can set both duration an level for both motors:
PS3.setRumbleOn(0x10,0xFF,0x08,0xFF)  here have the little motor slightly longer duration than the strong motor. Both on the strongest level.


Code: [Select]

//#include <PS3USB.h>
#include <PS3BT.h>
USB Usb;
/* You can create the instance of the class in two ways */
//PS3USB PS3(&Usb); // This will just create the instance
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
PS3BT PS3(&Btd); // This will just create the instance
//PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch

#include <Servo.h>
Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
Servo servo3;  // create servo object to control a servo

const uint8_t LED = 2;
const uint8_t Shake = 4;
const uint8_t Buzzer = 7;

// variables will change:

void setup() {
  pinMode(LED, OUTPUT);  // just a led to test buttons..
  pinMode(Buzzer, OUTPUT);    // A buzzer connected to the output
  pinMode(Shake, INPUT);  // the input I wanna use to activate the rumble in the controller(an IR-08H sensor is connected)
  Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  servo1.attach(3); // Use pin 3, as signal pin
  servo2.attach(5); // Use pin 5, as signal pin
  servo3.attach(6); // Use pin 6, as signal pin
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}

void loop() {
  Usb.Task();
  if (PS3.PS3Connected)
    digitalWrite(LED, PS3.getButtonPress(CROSS));
  else
    digitalWrite(LED, LOW);
  if (PS3.PS3Connected)
    servo1.write(map(PS3.getAnalogButton(R2), 0, 255, 180, 0));
  else
    servo1.write(180); // Set to position
  if (PS3.PS3Connected)
    servo2.write(map(PS3.getAnalogHat(RightHatX), 0, 255, 180, 0));
  else
    servo2.write(90); // Set to position
  if (PS3.PS3Connected)
    servo3.write(map(PS3.getAngle(Roll), 0, 360, 180, 0));
  else
    servo3.write(90); // Set to position
  if (PS3.getButtonClick(PS))
  {
    Serial.print(F("\r\nPS"));
    PS3.disconnect();
  }
  if (digitalRead(4) == HIGH) {
    digitalWrite(Buzzer, HIGH);   
  }
  else {
    digitalWrite(Buzzer, LOW);
    PS3.setRumbleOn(0x10,0xFF,0x08,0xFF);
  }
}


But still cant get some analog values from controller when using the BT library. And that I dont get, because the syntax and code are the same in both library. And this is not just with my code, also the same result running the example code.
Neither the PS3.getAnalogButton(R2) or PS3.getAngle(Roll) works when using PS3BT.h , but works with the PS3USB.h 

Ductor

Problem solved, it was just another USB dongle case ..   tried another type of dongle and then every function worked  :-)

Works also with a pro mini and mini usb host shield

https://www.youtube.com/edit?o=U&video_id=Hfs-QCxDX8A

Go Up