Go Down

Topic: Robot joystick nRF24L01 (Read 96 times) previous topic - next topic

arduiNICK

Hello !!!
I have a robot ,a joystick and 2 nRF24L01 modules in 2 arduinos (mega and uno) and I have a problem with joystick.
When push the joystick forward or left all is ok (4 wheels move),but when I push it backward or right only one wheel do not move (I do not know why)

Reciever code (arduino mega):

Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(7, 8);

const int IN_1_back_left = 6;
const int IN_2_back_left = 9;
const int PWM_back_left = 10;
const int IN_1_back_right = 48;
const int IN_2_back_right = 46;
const int PWM_back_right = 11;
const int IN_1_front_left = 2;
const int IN_2_front_left = 3;
const int PWM_front_left = 13;
const int IN_1_front_right = 42;
const int IN_2_front_right = 44;
const int PWM_front_right = 12;
int forwardSpeed = 0;         //define the forward speed of motors
int backwardSpeed = 0;        //define the backward speed of motors
int rightSpeed = 0;           //define the right turn only speed of motors
int leftSpeed = 0;            //define the left turn only speed of motors
int joystick[2];              // 2 element array holding Joystick readings
int flagStop = 0;


void setup()
{
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  pinMode(PWM_back_left, OUTPUT);
  pinMode(PWM_back_right, OUTPUT);
  pinMode(PWM_front_left, OUTPUT);
  pinMode(PWM_front_right, OUTPUT);

  pinMode(IN_1_back_left, OUTPUT);
  pinMode(IN_2_back_left, OUTPUT);
  pinMode(IN_1_back_right, OUTPUT);
  pinMode(IN_2_back_right, OUTPUT);

  pinMode(IN_1_front_left, OUTPUT);
  pinMode(IN_2_front_left, OUTPUT);
  pinMode(IN_1_front_right, OUTPUT);
  pinMode(IN_2_front_right, OUTPUT);

  analogWrite(PWM_back_left, 0);
  analogWrite(PWM_back_right, 0);
  analogWrite(PWM_front_left, 0);
  analogWrite(PWM_front_right, 0);
}


void FORWARD()
{
  digitalWrite(IN_1_back_left , HIGH);      //forward
  digitalWrite(IN_2_back_left , LOW);
  digitalWrite(IN_1_back_right , HIGH);
  digitalWrite(IN_2_back_right , LOW);

  digitalWrite(IN_1_front_left , HIGH);
  digitalWrite(IN_2_front_left , LOW);
  digitalWrite(IN_1_front_right , HIGH);
  digitalWrite(IN_2_front_right , LOW);
}


void BACKWARD()
{
  digitalWrite(IN_1_back_left , LOW);       //backward
  digitalWrite(IN_2_back_left , HIGH);
  digitalWrite(IN_1_back_right , LOW);
  digitalWrite(IN_2_back_right , HIGH);

  digitalWrite(IN_1_front_left , LOW);
  digitalWrite(IN_2_front_left , HIGH);
  digitalWrite(IN_1_front_right , LOW);
  digitalWrite(IN_2_front_right , HIGH);
}


void STOP()
{
  digitalWrite(IN_1_back_left , LOW);       //stop
  digitalWrite(IN_2_back_left , LOW);
  digitalWrite(IN_1_back_right , LOW);
  digitalWrite(IN_2_back_right , LOW);

  digitalWrite(IN_1_front_left , LOW);
  digitalWrite(IN_2_front_left , LOW);
  digitalWrite(IN_1_front_right , LOW);
  digitalWrite(IN_2_front_right , LOW);
}


void RIGHT()
{
  digitalWrite(IN_1_back_left , HIGH);      //right
  digitalWrite(IN_2_back_left , LOW);
  digitalWrite(IN_1_back_right , LOW);
  digitalWrite(IN_2_back_right , HIGH);

  digitalWrite(IN_1_front_left , HIGH);
  digitalWrite(IN_2_front_left , LOW);
  digitalWrite(IN_1_front_right , LOW);
  digitalWrite(IN_2_front_right , HIGH);
}


void LEFT()
{
  digitalWrite(IN_1_back_left , LOW);      //left
  digitalWrite(IN_2_back_left , HIGH);
  digitalWrite(IN_1_back_right , HIGH);
  digitalWrite(IN_2_back_right , LOW);

  digitalWrite(IN_1_front_left , LOW);
  digitalWrite(IN_2_front_left , HIGH);
  digitalWrite(IN_1_front_right , HIGH);
  digitalWrite(IN_2_front_right , LOW);
}



void forward()
{
  forwardSpeed = map(joystick[1], 510, 1023, 0, 255);
  FORWARD();
  analogWrite(PWM_back_left, forwardSpeed);
  analogWrite(PWM_back_right, forwardSpeed);
  analogWrite(PWM_front_left, forwardSpeed);
  analogWrite(PWM_front_right, forwardSpeed);
}
void backward()
{
  backwardSpeed = map(joystick[1], 506, 0, 0, 255);
  BACKWARD();
  analogWrite(PWM_back_left, backwardSpeed);
  analogWrite(PWM_back_right, backwardSpeed);
  analogWrite(PWM_front_left, backwardSpeed);
  analogWrite(PWM_front_right, backwardSpeed);
}

void left()
{
  leftSpeed = map(joystick[0], 517, 1023, 0, 255);
  LEFT();
  analogWrite(PWM_back_left, leftSpeed);
  analogWrite(PWM_back_right, leftSpeed);
  analogWrite(PWM_front_left, leftSpeed);
  analogWrite(PWM_front_right, leftSpeed);
}
void right()
{
  rightSpeed = map(joystick[0], 517, 0, 0, 255);
  RIGHT();
  analogWrite(PWM_back_left, rightSpeed);
  analogWrite(PWM_back_right, rightSpeed);
  analogWrite(PWM_front_left, rightSpeed);
  analogWrite(PWM_front_right, rightSpeed);
}
void forandright()
{
  forwardSpeed = map(joystick[1], 513, 1023, 0, 255);
  FORWARD();
  analogWrite(PWM_back_left, forwardSpeed);
  analogWrite(PWM_back_right, forwardSpeed / 2);
  analogWrite(PWM_front_left, forwardSpeed);
  analogWrite(PWM_front_right, forwardSpeed / 2);
}

void forandleft()
{
  forwardSpeed = map(joystick[1], 513, 1023, 0, 255);
  FORWARD();
  analogWrite(PWM_back_left, forwardSpeed / 2);
  analogWrite(PWM_back_right, forwardSpeed);
  analogWrite(PWM_front_left, forwardSpeed / 2);
  analogWrite(PWM_front_right, forwardSpeed);
}

void backandright()
{
  backwardSpeed = map(joystick[1], 517, 0, 255, 0);
  BACKWARD();
  analogWrite(PWM_back_left, backwardSpeed);
  analogWrite(PWM_back_right, backwardSpeed / 2);
  analogWrite(PWM_front_left, backwardSpeed);
  analogWrite(PWM_front_right, backwardSpeed / 2);
}

void backandleft()
{
  backwardSpeed = map(joystick[1], 517, 0, 0, 255);
  BACKWARD();
  analogWrite(PWM_back_left, backwardSpeed / 2);
  analogWrite(PWM_back_right, backwardSpeed);
  analogWrite(PWM_front_left, backwardSpeed / 2);
  analogWrite(PWM_front_right, backwardSpeed);
}



void loop()
{
  if (radio.available())
  {
    radio.read(joystick, sizeof(joystick));
    flagStop = 0;
  }

  if (joystick[1] > 502 && joystick[1] < 510 && joystick[0] >= 509 && joystick[0] <= 510)
  {
    STOP();
    flagStop = 1;
  }

  else
  {
    flagStop = 0;
  }

  if (flagStop == 0)
  {
    if (joystick[0] <= 508 && joystick[1] >= 516)
    {
      forandright();
    }

    else if (joystick[0] >= 513 && joystick[1] >= 516)
    {
      forandleft();
    }

    else if (joystick[0] <= 508 && joystick[1] <= 502)
    {
      backandright();
    }
    else if (joystick[0] >= 513 && joystick[1] <= 502)
    {
      backandleft();
    }

    else if (joystick[1] <= 505 && joystick[0] >= 509 && joystick[0] <= 513)
    {
      backward();
    }
    else if (joystick[1] >= 510 && joystick[0] <= 513 && joystick[0] >= 509)
    {
      forward();
    }

    else if (joystick[0] >= 516)
    {
      left();
    }

    else if (joystick[0] <= 508)
    {
      right();
    }
  }
  else
  {
    STOP();
    flagStop = 1;
  }
}




Transmitter code (arduino uno):

Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define JOYSTICK_X A0
#define JOYSTICK_Y A1

const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(7, 8);
int joystick[2];           // 2 element array holding Joystick readings

void setup()
{
  radio.begin();
  radio.openWritingPipe(pipe);
}


void loop()
{
  joystick[0] = analogRead(JOYSTICK_X);
  joystick[1] = analogRead(JOYSTICK_Y);
  radio.write(joystick, sizeof(joystick));
}



gfvalvo

Have you directly tested that it moves as desired in all modes without the RF link?

What do your Serial debug prints show?
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

arduiNICK

No but this is not the problem because all wheels are moving when I push forward or left the joystick.Only in right and backward 3/4 wheels are moving and I dont know why this is happening..


Delta_G

No but this is not the problem because all wheels are moving when I push forward or left the joystick.Only in right and backward 3/4 wheels are moving and I dont know why this is happening..


And we are trying to figure that out.  And you've been asked some questions for troubleshooting.  Instead of saying it isn't the problem, why don't you get the information requested.  Unless you already know what the problem is, in which case why are you wasting our time asking here.  If you don't know what the problem is then stop pretending that you do and answer the questions asked of you. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

vinceherman

On your receiver, comment out all the code in the loop that makes the motors move.
Put in code that writes the 2 joystick values to the serial monitor.
Run this and confirm that the values being printed on the serial monitor match what you expect to receive for various joystick positions.

Troubleshooting will be much more direct if you first get confidence that the serial communication is working as expected.

gfvalvo

#5
Jul 19, 2019, 01:17 am Last Edit: Jul 19, 2019, 01:17 am by gfvalvo
On your receiver, comment out all the code in the loop that makes the motors move.
Yes, divide and conquer. My suggestion in Reply #1 was to check out the drive part first (but this way is just as valid). That was rejected by @arduiNICK because he apparently knows where the problem is.

My hint about Serial debug prints was also ignored.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

arduiNICK


Go Up