Hi there,
I just bought motor driver from
http://robokits.co.in/shop/index.php?main_page=product_info&cPath=73&products_id=342
which i am using in my current project.
I am using 4 motors for 4 wheel bot. 2 connected in parallel to one slot & other 2 in second slot.I am using PS2 controller to control bot.I want to use 2 joysticks to control movement of motors. Left joystick to control left side of 2 motors & right joystick for right side of 2 motors.
What i want is that when i move both joystick upward, the bot should move forward & also when i move it downwards bot should move backward. I also want to control speed of motor using joysticks values.
I tried following code:
#include<PS2X_lib.h>
#define pressures true
#define rumble true
byte vibrate = 0;
PS2X ps2x;
int error = 0;
int type = 0;
int LX; //Variables for reading stick values
int LY;
int RX;
int RY;
int L_DIR = 8; //Pins For left side of motors
int L_PWM = 9;
int L_BRK = 10;
int R_DIR = 12; //Pins for right side of motors
int R_PWM = 11;
int R_BRK = 13;
void setup()
{
Serial.begin(57600);
delay(300);
pinMode(L_DIR, OUTPUT);
pinMode(L_PWM, OUTPUT);
pinMode(L_BRK, OUTPUT);
pinMode(R_PWM, OUTPUT);
pinMode(R_PWM, OUTPUT);
pinMode(R_BRK, OUTPUT);
void l_forward(unsigned int );
void l_backward(unsigned int );
void r_forward(unsigned int );
void r_backward(unsigned int );
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(4,5,6,7,pressures,rumble);
if(error == 0)
{
Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
}
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.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
case 2:
Serial.print("GuitarHero Controller found ");
break;
case 3:
Serial.print("Wireless Sony DualShock Controller found ");
break;
}
}
void loop()
{
if(error == 1) //skip loop if no controller found
return;
ps2x.read_gamepad(false, vibrate);
LY = ps2x.Analog(PSS_LY); // Left axis +Y,-Y values
delay(10);
LX = ps2x.Analog(PSS_LX); // left axis +X,-X values
delay(10);
RY = ps2x.Analog(PSS_RY); // Right axis +Y,-Y values
delay(10);
RX = ps2x.Analog(PSS_RX); // Right axis +X, -X values
delay(10);
digitalWrite(L_BRK, LOW);
digitalWrite(R_BRK, LOW);
if(LY < 127 && LX > 0 && LX < 255)
{
while(LY < 127 && LX > 0 && LX < 255)
{
l_forward(255-LY); //Passing LY value as speed of motors
ps2x.read_gamepad();
LY = ps2x.Analog(PSS_LY);
LX = ps2x.Analog(PSS_LX);
}
}
else if(LY > 127 && LX > 0 && LX < 255)
{
while(LY > 127 && LX > 0 && LX < 255)
{
l_backward(LY); //Passing LY value as speed of motors
ps2x.read_gamepad();
LY = ps2x.Analog(PSS_LY);
LX = ps2x.Analog(PSS_LX);
}
}
else if(RY < 127 && RX > 0 && RX < 255)
{
while(RY < 127 && RX > 0 && RX < 255)
{
r_forward(255-RY); //Passing RY value as speed of motors
ps2x.read_gamepad();
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
}
}
else if(RY > 127 && RX > 0 && RX < 255)
{
while(RY > 127 && RX > 0 && RX < 255)
{
r_backward(RY); //Passing RY value as speed of motors
ps2x.read_gamepad();
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
}
}
/*else if((LY < 127 && LX > 0 && LX < 255) && (RY < 127 && RX > 0 && RX < 255))
{
while((LY < 127 && LX > 0 && LX < 255) && (RY < 127 && RX > 0 && RX < 255))
{
l_forward(LY); //Passing LY value as speed of motors
r_forward(RY);
ps2x.read_gamepad();
LY = ps2x.Analog(PSS_LY);
LX = ps2x.Analog(PSS_LX);
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
}
}
else if((LY > 127 && LX > 0 && LX < 255) && (RY > 127 && RX > 0 && RX < 255))
{
while((LY > 127 && LX > 0 && LX < 255) && (RY > 127 && RX > 0 && RX < 255))
{
l_backward(LY); //Passing LY value as speed of motors
r_backward(RY);
ps2x.read_gamepad();
LY = ps2x.Analog(PSS_LY);
LX = ps2x.Analog(PSS_LX);
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
}
}*/
else if(LY == 127 && LX == 128 && RY == 127 && RX == 128)
{
digitalWrite(L_BRK, HIGH);
digitalWrite(R_BRK, HIGH);
}
}
void l_forward(unsigned int LY)
{
digitalWrite(L_DIR, LOW);
analogWrite(L_PWM, LY);
delay(10);
}
void r_forward(unsigned int RY)
{
digitalWrite(R_DIR, LOW);
analogWrite(R_PWM, RY);
delay(10);
}
void l_backward(unsigned int LY)
{
digitalWrite(L_DIR, HIGH);
analogWrite(L_PWM, LY);
delay(10);
}
void r_backward(unsigned int RY)
{
digitalWrite(R_DIR, HIGH);
analogWrite(R_PWM, RY);
delay(10);
}
I am not getting my desired output. there is something wrong in code which i unable to figure out.
If there any issue with the code, please help me rectify it.