SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Talk about Arduino board, sheilds. Sharing Arduino projects, program, problems, solutions, suggestions..... many more, all are welcome.

SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Moe1 » Wed Mar 09, 2016 3:23 am

Hey everyone, i've got a problem with the sample code of the tutorial http://tutorial.cytron.com.my/2015/06/24/arduino-ps2-shield-mdds10-for-mobile-robot-control/.
Everything is working ok, except the turn left and the stop position.

Maybe I need a "death centre" in the middle of the joystick. Because in the middle position I get a value of 132 on the x axis and a value of 123 on the y axis insted of the expected value of 128. But unfortunately I dont know how to create that.

Does anyone got an idea?

Thanks a lot!

Moe

CODE: SELECT_ALL_CODE
/*
PWM input mode with microcontroller (Independent Both), DIP switch 1 0 1 1 0 1 0 0 .
This code is using PS2 shield library and math library.
This code is tested with CT-Uno,Smart Drive Duo 10(MDDS10),PS2 shield and PS2 wireless.
*/
#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h> //PS2 shield library
Cytron_PS2Shield ps2(2, 3); // SoftwareSerial: Rx and Tx pin
#include<math.h>

int dig1=7;  //pin signal for motor left
int dig2=4;  //pin signal for motor right
int an1=6;   //pin pwm for motor left
int an2=5;   //pin pwm for motor right


int ly=0;
int lx=0;
int xaxis=0;
int yaxis=0;
int acc=0;


void setup()
{
  ps2.begin(9600); // This baudrate must same with the jumper setting at PS2 shield
  ps2.reset(1);             //call to reset Shield-PS2
  delay(100);
  ps2.reset(0);
  pinMode(dig1,OUTPUT);      //initialize for all the input and output
  pinMode(dig2,OUTPUT);
  pinMode(an1,INPUT);
  pinMode(an2,INPUT);
  Serial.begin(9600);
  analogWrite(an1,0);  //Based on the datasheet, the MDDS10 analog pin should get 0 input upon start
  analogWrite(an2,0);  //thus we need to send 0 value at the beginning
  delay(1000);                       
 
}

void loop()
{
    //joystick value
    ly=ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS);
    lx=ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS);
    acc=(ps2.readButton(PS2_LEFT_2) == 0);
   
     
     if(acc==HIGH)
     {
     Normal();}         //function call for normal speed
     else{
     Acceleration();}   //function call for higher speed
     Movement();
   
   
}
//funtion for the movement
void forward(int pwm){
  digitalWrite(dig1,HIGH);     
  digitalWrite(dig2,HIGH);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void reverse(int pwm){
  digitalWrite(dig1,LOW);     
  digitalWrite(dig2,LOW);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void left(int pwm){
  digitalWrite(dig1,HIGH);     
  digitalWrite(dig2,LOW);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void right(int pwm){
  digitalWrite(dig1,LOW);     
  digitalWrite(dig2,HIGH);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void Stop(int pwm){
  digitalWrite(dig1,LOW);     
  digitalWrite(dig2,HIGH);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void Acceleration(){

  yaxis=map(ly,0,255,250,-250);      //mapping for the value from the joystick to our desired value
  xaxis=map(lx,0,255,-250,250);
}
void Normal(){

  yaxis=map(ly,0,255,180,-180);
  xaxis=map(lx,0,255,-180,180);
}
//Delete the "//" before the Serial.print to see the output from serial monitor
void Movement(){
 
   if( yaxis>0 && yaxis>xaxis){
           forward(yaxis);
           //Serial.print("forward");
     
     }     
   else if( yaxis<0 && yaxis< abs(xaxis)){
           reverse(abs(yaxis));
           //Serial.print("reverse");
     } 
   else if( xaxis>0 && xaxis>yaxis){
           right(xaxis);
           //Serial.print("right");
     } 
   else if( xaxis<0 && xaxis<abs(yaxis)){
           left(abs(xaxis));
           //Serial.print("left");
     }
   else if( yaxis==1 && xaxis==0){
           Stop(0);
           //Serial.print("Stop");
     }   
}
Moe1
Freshie
 
Posts: 5
Joined: Wed Mar 09, 2016 3:14 am

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Idris » Wed Mar 09, 2016 9:08 am

Hi Moe,

First, you need to understand about mapping function, please read here.

In the tutorial, the code given is mapping the raw value from min to max in one call. It is working if the center value of analog joystick is really center, e.g. 255/2 = 127.5 (127 or 128). However, in your case, it is not, so my suggestion is do mapping half by half. Please refer to the code below. The mapping process is located under Acceleration() and Normal() function.

CODE: SELECT_ALL_CODE
#define X_CENTER  132
#define Y_CENTER  123

void Acceleration()
{
  if(lx < X_CENTER) {
    xaxis = map(lx, 0, X_CENTER, -250, 0);
  }
  else {
    xaxis = map(lx, X_CENTER, 255, 0, 250);
  }

  if(ly < Y_CENTER) {
    yaxis = map(ly, 0, Y_CENTER, 250, 0);
  }
  else {
    yaxis = map(ly, Y_CENTER, 255, 0, -250);
  }
}

void Normal()
{
  if(lx < X_CENTER) {
    xaxis = map(lx, 0, X_CENTER, -180, 0);
  }
  else {
    xaxis = map(lx, X_CENTER, 255, 0, 180);
  }

  if(ly < Y_CENTER) {
    yaxis = map(ly, 0, Y_CENTER, 180, 0);
  }
  else {
    yaxis = map(ly, Y_CENTER, 255, 0, -180);
  }
}

I'm not compiling the code, not sure it is working or not, but the idea is there. Thanks.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Moe1 » Thu Mar 10, 2016 1:12 am

Thank you very much for your super quick response!

The new code is running. It is possible to drive left :)
But there are few new problems:

-the reaktion of the joystick value is very slow. Speeding up is not the problem. If you are on full speed and let the stick comes back quickly to the middel position the value is still on full speed.

- another problem is, that there is no point were the engine is not moving

regards Moe

CODE: SELECT_ALL_CODE
/*
PWM input mode with microcontroller (Independent Both), DIP switch 1 0 1 1 0 1 0 0 .
This code is using PS2 shield library and math library.
This code is tested with CT-Uno,Smart Drive Duo 10(MDDS10),PS2 shield and PS2 wireless.
*/
#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h> //PS2 shield library
Cytron_PS2Shield ps2(2, 3); // SoftwareSerial: Rx and Tx pin
#include<math.h>

int X_CENTER=132;
int Y_CENTER=123;

int dig1=7;  //pin signal for motor left
int dig2=4;  //pin signal for motor right
int an1=6;   //pin pwm for motor left
int an2=5;   //pin pwm for motor right

int ly=0;
int lx=0;

int xaxis=0;
int yaxis=0;
int acc1=0;
int acc2=0;

void setup()
{
  ps2.begin(9600); // This baudrate must same with the jumper setting at PS2 shield
  ps2.reset(1);             //call to reset Shield-PS2
  delay(100);
  ps2.reset(0);
  pinMode(dig1,OUTPUT);      //initialize for all the input and output
  pinMode(dig2,OUTPUT);
  pinMode(an1,OUTPUT);
  pinMode(an2,OUTPUT);     
  Serial.begin(9600);
  analogWrite(an1,0);  //Based on the datasheet, the MDDS10 analog pin should get 0 input upon start
  analogWrite(an2,0);  //thus we need to send 0 value at the beginning
  delay(1000);                       
 
}

void loop()
{
    //joystick value
    ly=ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS);
    lx=ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS);
    acc1=(ps2.readButton(PS2_LEFT_2) == 0);   // L2 and R2 need to be pressed for full speed
    acc2=(ps2.readButton(PS2_RIGHT_2) == 0);  // L2 and R2 need to be pressed for full speed
   
           
     if(acc1==HIGH & acc2==HIGH)
     {
     Normal();}         //function call for normal speed
     else{
     Acceleration();}   //function call for higher speed
     Movement();
   

}
//funtion for the movement

void forward(int pwm){
  digitalWrite(dig1,HIGH);     
  digitalWrite(dig2,HIGH);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void reverse(int pwm){
  digitalWrite(dig1,LOW);     
  digitalWrite(dig2,LOW);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void left(int pwm){
  digitalWrite(dig1,HIGH);     
  digitalWrite(dig2,LOW);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void right(int pwm){
  digitalWrite(dig1,LOW);     
  digitalWrite(dig2,HIGH);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}
void Stop(int pwm){
  digitalWrite(dig1,LOW);     
  digitalWrite(dig2,HIGH);
  analogWrite(an1,pwm);
  analogWrite(an2,pwm);
  Serial.println(pwm);
}

void Acceleration()
{
  if(lx < X_CENTER) {
    xaxis = map(lx, 0, X_CENTER, -250, 0);
  }
  else {
    xaxis = map(lx, X_CENTER, 255, 0, 250);
  }

  if(ly < Y_CENTER) {
    yaxis = map(ly, 0, Y_CENTER, 250, 0);
  }
  else {
    yaxis = map(ly, Y_CENTER, 255, 0, -250);
  }
}

void Normal()
{
  if(lx < X_CENTER) {
    xaxis = map(lx, 0, X_CENTER, -180, 0);
  }
  else {
    xaxis = map(lx, X_CENTER, 255, 0, 180);
  }

  if(ly < Y_CENTER) {
    yaxis = map(ly, 0, Y_CENTER, 180, 0);
  }
  else {
    yaxis = map(ly, Y_CENTER, 255, 0, -180);
  }
}

void Movement(){
 
   if( yaxis>0 && yaxis>xaxis){
           forward(yaxis);
           Serial.print("forward");
     }     
   else if( yaxis<0 && yaxis< abs(xaxis)){
           reverse(abs(yaxis));
           Serial.print("reverse");
     } 
   else if( xaxis>0 && xaxis>yaxis){
           right(xaxis);
           Serial.print("right");
     } 
   else if( xaxis<0 && xaxis<abs(yaxis)){
           left(abs(xaxis));
           Serial.print("left");
     }
   else if( yaxis==1 && xaxis==0){
           Stop(0);
           Serial.print("Stop");
     }   
}
Moe1
Freshie
 
Posts: 5
Joined: Wed Mar 09, 2016 3:14 am

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Idris » Thu Mar 10, 2016 10:48 am

Hi Moe,

Moe WROTE:The new code is running. It is possible to drive left :)

Glad to hear that! I hope you understand the code.

Moe WROTE:-the reaktion of the joystick value is very slow. Speeding up is not the problem. If you are on full speed and let the stick comes back quickly to the middel position the value is still on full speed.

You mean the motor is still keep running even the joystick at the stop position? Is it take too long for the motor to stop? Or it didn't stop?

Moe WROTE:- another problem is, that there is no point were the engine is not moving

Emm could you elaborate more about this problem? Not really understand.

If you can share the video and clearly tell about the problem, it will help much.

Thanks.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Moe1 » Thu Mar 10, 2016 7:37 pm

Hi,
yes the motor is still keep running even when the joystick at the stop position. I've tryed to make a video to show you what I mean.

https://youtu.be/K8JpvbWOolQ

sorry for my bad english :oops:
Moe1
Freshie
 
Posts: 5
Joined: Wed Mar 09, 2016 3:14 am

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Idris » Fri Mar 11, 2016 9:31 am

Hi Moe,

This is my short suggestion, try to put a few delay in loop() function.
CODE: SELECT_ALL_CODE
void loop()
{
  ly = ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS);
  lx = ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS);
  acc1 = (ps2.readButton(PS2_LEFT_2) == 0); // L2 and R2 need to be pressed for full speed
  acc2 = (ps2.readButton(PS2_RIGHT_2) == 0); // L2 and R2 need to be pressed for full speed

  if(acc1 == HIGH & acc2 == HIGH) {
    Normal();
  }
  else {
    Acceleration();
  }
  Movement();

  delay(100);
}

Another one, try to change the stop condition in Movement() function.
CODE: SELECT_ALL_CODE
void Movement()
{
  if(yaxis > 0 && yaxis > xaxis) {
    forward(yaxis);
    Serial.print("forward");
  }
  else if(yaxis < 0 && yaxis < abs(xaxis)) {
    reverse(abs(yaxis));
    Serial.print("reverse");
  }
  else if(xaxis > 0 && xaxis > yaxis) {
    right(xaxis);
    Serial.print("right");
  }
  else if(xaxis < 0 && xaxis < abs(yaxis)) {
    left(abs(xaxis));
    Serial.print("left");
  }
  else {
    Stop(0);
    Serial.print("Stop");
  }
}

Thanks.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Moe1 » Fri Mar 11, 2016 8:34 pm

Hi, to change the stop condition in Movement() function was the solution of the problem :)

Thanks a lot!
Moe1
Freshie
 
Posts: 5
Joined: Wed Mar 09, 2016 3:14 am

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby Moe1 » Mon Apr 04, 2016 1:02 am

Hi everybody,
there is a new problem. Last weekend I tested the code on the Arduino controlling the SmartDriveDuo-60. The response of the motor to the joystick movement is again very slow or there is no reaction. It has also happened that the motor starts speeding up and down with no touching of the joystick. When the Arduino is connected to the SmartDriveDuo and I open the serial monitor, there is always a delay in the reaction of the serial monitor and it lags. When I disconnect the SmartDriveDuo, the code runs fluently in the serial monitor. Even if I connect LEDs to the Arduino to check if the code is functioning, it also runs fluently and without problems.
I reckon there is a problem in the communication between the Arduino and the SmartDriveDuo. Is it possible that there is a problem with the PWM-frequency of the Arduino?
Unfortunately, I forgot to make a video.

Regards, Moe
Moe1
Freshie
 
Posts: 5
Joined: Wed Mar 09, 2016 3:14 am

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby ober » Mon Apr 04, 2016 9:15 am

Video will helps a lot.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: SmartDriveDuo-60 with PS2 Shield and Arduino Uno

Postby annarosy » Tue Dec 20, 2016 5:21 pm

The motor is still keep running even when the joystick at the stop position.
annarosy
Freshie
 
Posts: 4
Joined: Tue Aug 16, 2016 3:46 pm


Return to Arduino Based

Who is online

Users browsing this forum: No registered users and 16 guests

cron