Go Down

Topic: Motor stuttering with the PS2X Library (Read 1 time) previous topic - next topic

matty0997

Hi guys,

I have started my own rover project where the rover is being controlled by a ps2 remote. To control the robot I am using the PS2X library. The problem that I am getting is that the motors keep stuttering when the button is pressed instead of a constant motion.

I am fairly new to this so can someone point out where I am going wrong please. The code that I have created is below.

Thanks.

#include <PS2X_lib.h>

#include <Servo.h>

#define clk 9
#define cmd 7
#define att 8
#define dat 6

#define Deadzone 2

#define lservo_pin 13
#define rservo_pin 12

#define pressures true
#define rumble true

Servo leftservo;
Servo rightservo;

PS2X ps2;
int error = 0 ;
byte type = 0;


void setup() {
  Serial.begin(9600);
  delay(300);

  pinMode(7, INPUT);

  error = ps2.config_gamepad(clk, cmd, att, dat, pressures, rumble);

   
leftservo.attach(lservo_pin);
rightservo.attach(rservo_pin);

}
void loop() {
 
  ps2.read_gamepad();

  if(ps2.Button(PSB_PAD_UP))
    {
      leftservo.write(120);
      rightservo.write(120);
    }

  else if(ps2.Button(PSB_PAD_DOWN))
      {
        leftservo.write(40);
        rightservo.write(40);
      }
   
  else if (ps2.Button(PSB_PAD_LEFT))
    {
      leftservo.write(40);
      rightservo.write(140);
    }
  else if(ps2.Button(PSB_PAD_RIGHT))
    {
      leftservo.write(140);
      rightservo.write(40);
    }
 
  else
      {
      leftservo.write(90);
      rightservo.write(90);
      }
   
    delay(50);
}

PaulS

Quote
The problem that I am getting is that the motors keep stuttering when the button is pressed instead of a constant motion.
What do you mean by "stuttering"?

Which button?

There is nothing in the code to cause a smooth motion. The servos are told to go to some position, but not how to get there.

How are the servos powered?
The art of getting good answers lies in asking good questions.

matty0997

All of the buttons cause the juddering movement- instead of moving smoothly the tracks rotate for a second or so and then stops but then carries on again.

I'm using two spur gear head motors which are attached to a motor controller. The motors are powered by a 12v power supply.

You mentioned that I haven't told the servos how to get to the position, could you be a bit clearer please.

PaulS

Quote
You mentioned that I haven't told the servos how to get to the position, could you be a bit clearer please.
Suppose you asked me how to get to my house. Suppose that I told you to drive to 123 West Smith street. Does that tell you how to get there? No, it simply tells you where to do. You are free to choose any route that eventually gets you there.

All you have told the servo to do is to go to 120, 40, 140, or 90. You have not told it to go from 40 to 41, pause, go from 41 to 42, pause, etc. that would create a smooth(er) movement.

Quote
I'm using two spur gear head motors which are attached to a motor controller.
And driving them with the Servo library? Does that make sense?
The art of getting good answers lies in asking good questions.

matty0997

Thank you for explaing more indepth about how to create a smoother cycle through your use of sarcasm. As for the use of using the servo libary to control the motors does makes sense as you are forgetting that a motor controller allows a motor to be programed as a servo, just have a look at the sabertooth motor controller which I am using in my project.

DuaneDegn

#5
Dec 21, 2015, 09:31 pm Last Edit: Dec 21, 2015, 09:31 pm by DuaneDegn
I posted some code to smoothly move servos when controlled with a joystick. The code could be adapted for use with a Playstation 2 controller.

There are constants such as acceleration and max speed which give the servos smooth motion.

The program is intended for use with normal servos, but the way the speed values are accelerated over time could be applied to your robot.

Please follow the directions in "How use use this forum - please read" to learn about posting code to the forum.

Besides posting the complete program, also post links to any libraries you're using.

I've used PS2 controllers with a different microcontroller and I'm interested in learning to use these controllers with the Arduino. If you post your code (and links to libraries) I'll try to take a look at your code and offer additional suggestions.

DuaneDegn

#6
Dec 21, 2015, 09:34 pm Last Edit: Dec 21, 2015, 09:35 pm by DuaneDegn
I'll also mention using "writeMicroseconds" provides much better control than "write". (Though this change wouldn't affect your robot's behaviour without additional modifications to your code.)

PaulS

Quote
just have a look at the sabertooth motor controller which I am using in my project.
About time you mentioned that.
The art of getting good answers lies in asking good questions.

matty0997

Here is the link tot he website where I downloaded the PS2X library

http://www.billporter.info/2010/06/05/playstation-2-controller-arduino-library-v1-0/



This is the code that I have used:

Code: [Select]
#include <PS2X_lib.h>

#include <Servo.h>

#define clk 9
#define cmd 7
#define att 8
#define dat 6

#define Deadzone 2

#define lservo_pin 13
#define rservo_pin 12

#define pressures true
#define rumble true

Servo leftservo;
Servo rightservo;

PS2X ps2;
int error = 0 ;
byte type = 0;


void setup() {
  Serial.begin(9600);
  delay(300);

  pinMode(7, INPUT);

  error = ps2.config_gamepad(clk, cmd, att, dat, pressures, rumble);

   
leftservo.attach(lservo_pin);
rightservo.attach(rservo_pin);

}
void loop() {
 
  ps2.read_gamepad();

  if(ps2.Button(PSB_PAD_UP))
    {
      leftservo.write(120);
      rightservo.write(120);
    }

  else if(ps2.Button(PSB_PAD_DOWN))
      {
        leftservo.write(40);
        rightservo.write(40);
      }
   
  else if (ps2.Button(PSB_PAD_LEFT))
    {
      leftservo.write(40);
      rightservo.write(140);
    }
  else if(ps2.Button(PSB_PAD_RIGHT))
    {
      leftservo.write(140);
      rightservo.write(40);
    }
 
  else
      {
      leftservo.write(90);
      rightservo.write(90);
      }
   
    delay(100);
}/code]


DuaneDegn

Here is the link tot he website where I downloaded the PS2X library

http://www.billporter.info/2010/06/05/playstation-2-controller-arduino-library-v1-0/



This is the code that I have used:

I'll take a look at your code when I get a chance.

Do you have a link to the PS2 controller you're using? I've had bad luck with a couple wireless PS2 controllers I've tried. I've been trying to find a wireless PS2 controller which works as well as the original Sony controllers. If you like the controller you're using, I hope you share where you purchased it with us.

matty0997

Thanks,

The controller that I use is very reliable, I haven't encountered any problems so far.

Here is the link to the website that I bought the controller from. I have also added a link to show you how the parts for the receiver connect together. I hope this is useful.

http://www.robotshop.com/en/lynxmotion-ps2-controller-v3.html

http://www.lynxmotion.com/driver.aspx?Topic=ps2-versions

Go Up