Go Down

Topic: Robot not functioning as expected using nRF24L01 sensor and DC motors (Read 267 times) previous topic - next topic

jkazda

I have a joystick module sending commands to another bot using the nRF24L01 sensors. When it is connected through the usb connection to the computer the motor functions behave as expected.

When I try to power the robot through battery's only, the command is sent and received however the problem I'm having is when the command is executed. The motor functions don't execute as I expect them to.

At this point it won't even drive forward, just turning randomly.

I think it's a power issue, I first had the arduino uno and all sensors powered through a 6V battery pack, I also tried to have the motor driver board and the rf sensor each have their own power supply and nothing seems to help.

 Motor Driver Link

Here is the code for the receiver:
Code: [Select]
/*
/*  ===================================================================
*   FILE: Swarm_Algorithm_Master.c
*   MODULE: Swarm_Algorithm_Master
*
*   TITLE: Master Bot Algorithm for Formation #1
*
*   MODULE DESCRIPTION:
*   
*   MAIN FUNCTIONS:
*
*   HELPER FUNCTIONS:
*
*   ADDITIONAL INFORMATION:
*
*   --------------------------------------------------------------------
*   REVISION HISTORY:
*   Rev     Date        Init
*           Changes
*   -----   --------    -----
*   1.1     2019-03-05  JM
*           Initial Revision.
*/

  //////////////
  // INCLUDES //
  //////////////
 
  #include <SPI.h>   
  #include <Wire.h>
  #include <RF24.h>
  #include <printf.h>
  //#include "Adafruit_VL6180X.h"

  /////////////
  // DEFINES //
  /////////////
  #define STOP    0x00                                     
  #define FORWARD 0x01                                   
  #define REVERSE 0x02                                   
  #define LEFT    0x03                                     
  #define RIGHT   0x04     

  #define SLAVE 0

  #define DEBUG

  // State machine for slave bot //
  enum slaveState{
    S_IDLING = 0,
    S_MOVE
  };
  slaveState sState;

   // Characteristics of each bot //
  struct bot
  {
    float x;
    float y;
    unsigned int pipe;
    byte lastMove;
    byte direction;
  };
 
  ///////////////////////////
  // FUNCTION DECLARATIONS //
  ///////////////////////////

  // Interrupt //
//  void trackDistanceR();
//  void trackDistanceL();
// 
  // Motor //
  void forward();
  void backward(); 
  void rightTurn();
  void leftTurn();
  void stopMotor();


  // RF //
  void readPipe(uint8_t pipeNumber, uint8_t pipeAddress);
  void writePipe(uint8_t pipeAddress);

  ///////////////
  // VARIABLES //
  ///////////////

  const byte STDBYE = 12;
  const byte AIN1 = 4;
  const byte AIN2 = 5;
  const byte PWMA = 11;
  const byte BIN1 = 7;
  const byte BIN2 = 8;
  const byte PWMB = 10;
  const byte CE = 9;
  const byte CSN = 6;
  int state = sState;
  uint8_t command;
  uint8_t commandPipe[5]  = {'C','O','M','M','1'};
  uint8_t slaveAddresses[3][5] = {
    {'S','L','A','V','1'},
    {'S','L','A','V','2'},
    {'S','L','A','V','3'}
  };
 
  // int remoteNodeData[2] = {1,1};
 
  /////////////////////
  // INITIALIZATIION //
  /////////////////////
 
  RF24 radio(CE, CSN);
 // Adafruit_VL6180X distSensor = Adafruit_VL6180X();
 
void setup() {
    Serial.begin(115200);
   
    printf_begin();

   #ifdef DEBUG
    Serial.println("Initializing...");
    delay(500);
   #endif
   
    /* Begin radio object */
    radio.begin();

    /* Setup read pipe to remote node */
    radio.openReadingPipe(1,slaveAddresses[SLAVE]);
    radio.startListening();

    /* Set power level of the radio */
    radio.setPALevel(RF24_PA_LOW);

    /* Set radio channel to use - ensure all slaves match this */
    radio.setChannel(0x69);

    /* Set time between retries and max no. of retries */
    radio.setRetries(4, 10);

    /* Enable ackpayload - enables each slave to reply with data */
    //radio.enableAckPayload();

    /* Preload the payload with initial data */
    // radio.writeAckPayload(1, &remoteNodeData, sizeof(remoteNodeData));
   
    /* Print out radio configuration data on serial monitor */
    radio.printDetails();

    /* Initializes bot to no motor movement */
   
    stopMotor();
   
    #ifdef DEBUG
      Serial.println("Setup complete!");
      delay(500);
    #endif
}


void loop() {
  //command = STOP;
  #ifdef DEBUG
      Serial.println("Starting main loop...");
  #endif
 
  //while(radio.available()){
  radio.flush_rx();
   
  delay(1000);
  switch(state)
  {
  /***********************************************************************/
  case S_IDLING:
    #ifdef DEBUG
      Serial.println("IDLE");
    #endif
   
    while(command == STOP)
    {
      radio.openReadingPipe(1, slaveAddresses[SLAVE]);
      radio.startListening();
      radio.read(&command, sizeof(command));       
    } 

    #ifdef DEBUG
      Serial.println("IDLE complete!");
      Serial.println(command);
    #endif
    //radio.stopListening();
    state++;
  break;
  /***********************************************************************/
  case S_MOVE:

    #ifdef DEBUG
          Serial.println("MOVE");
          Serial.print("Command:");
          Serial.println(command);
        #endif

         /* Master moves according to command data */
         switch(command){
            case FORWARD:
              Serial.println('A');
               
              forward();
              delay(100);
              break;

            case REVERSE:
              Serial.println('B');
               
              backward();
              delay(100);
              break;

            case LEFT:
              Serial.println('C');
               
              leftTurn();
              break;

            case RIGHT:
              Serial.println('D');
               
              rightTurn();
              break;

            case STOP:
              Serial.println('E');
               
              stopMotor();
              break;

            default:
              Serial.println('F');
               
              stopMotor();
              break;
          }
      Serial.println('G');
      stopMotor();
      Serial.println('H');
      command = STOP;
       
      state = S_IDLING;
   
    #ifdef DEBUG
      Serial.println("MOVE complete");
    #endif
  break;
  /***********************************************************************/
  }
 
}

void forward() {
  digitalWrite(STDBYE, HIGH);
  digitalWrite(AIN1, LOW);
  digitalWrite(AIN2, HIGH);
  digitalWrite(BIN1, LOW);
  digitalWrite(BIN2, HIGH);
  analogWrite(PWMA, 50);
  analogWrite(PWMB, 50);
  delay(800);
  stopMotor();
}

void rightTurn() {
  digitalWrite(STDBYE, HIGH);
  digitalWrite(AIN1, LOW);
  digitalWrite(AIN2, HIGH);
  digitalWrite(BIN1, HIGH);
  digitalWrite(BIN2, LOW);
  analogWrite(PWMA, 75);
  analogWrite(PWMB, 75);
  delay(700);
  stopMotor();
}

void leftTurn() {
  digitalWrite(STDBYE, HIGH);
  digitalWrite(AIN1, HIGH);
  digitalWrite(AIN2, LOW);
  digitalWrite(BIN1, LOW);
  digitalWrite(BIN2, HIGH);
  analogWrite(PWMA, 75);
  analogWrite(PWMB, 75);
  delay(700);
  stopMotor();
}

void backward() {
  digitalWrite(STDBYE, HIGH);
  digitalWrite(AIN1, HIGH);
  digitalWrite(AIN2, LOW);
  digitalWrite(BIN1, HIGH);
  digitalWrite(BIN2, LOW);
  analogWrite(PWMA, 50);
  analogWrite(PWMB, 50  );
  delay(800);
  stopMotor();
}

void stopMotor() {
  digitalWrite(STDBYE, LOW);
  digitalWrite(AIN1, LOW);
  digitalWrite(AIN2, LOW);
  digitalWrite(BIN1, LOW);
  digitalWrite(BIN2, LOW);
  analogWrite(PWMA, 0);
  analogWrite(PWMB, 0);
}


void readPipe(uint8_t pipeNumber, uint8_t pipeAddress)
{
  radio.openReadingPipe(pipeNumber, pipeAddress);
  radio.startListening();
}

void writePipe(uint8_t pipeAddress)
{
  radio.openWritingPipe(pipeAddress);
  radio.stopListening(); 
}

neiklot

I'm not trying to be funny, but what's your question?

Do you want someone to dis/agree with this:

I think it's a power issue
?

Or what?


Go Up