Go Down

Topic: Robot not moving (Read 456 times) previous topic - next topic

project_science

Hi!

I've built a 4WD robot which uses the Mega board, and a Motor Shield from Adafruit (https://www.adafruit.com/product/1438).  The robot's purpose right now is just to move:  forward, backwards, and stop (release).  I was using it fine with functions, but when I tried to switch to classes (e.g. moving motor functions to a motor class), the motors didn't move. 

However, from the serial monitor, I see that they should have, which can be seen under the line:

Code: [Select]

void Drive_State(int DRIVE_STATE)


I'm assuming there's an easy answer, I just can't find the error.  Here's my code:


Code: [Select]

/****************************************
     LIBRARIES
****************************************/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

/****************************************
     VARIABLES - GLOBAL
****************************************/

// Wait time between motor calls
int motor_delay = 10;


/****************************************
     OBJECTS
****************************************/

/*****MOTOR OBJECTS*****/

// Create motor shield object with default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
       /* Or, create it with a different I2C address (say for stacking)
          Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
       */

// Define motors
     // *MotorX, where "X" is motor number (e.g. M1, M2...)
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);

/****************************************
     FUNCTIONS
****************************************/

// currently empty

/***********************************
            CLASS:  Motors
***********************************/

class Motors
{
  public:
 
    void Setup_Motors()
    {
      // set default speed for motors, 0 (off) to 255 (max speed)
    /* Notes:  (1) The differences between values is small, so take M1 to be
                representative of all speeds
               (2) The math is slightly changed from intro as to avoid truncation
    */

      // Set Speed of all motors
       int default_speed = 50;  // % of max speed, range: 0-100.  Change this only.
        // do NOT change line below.  It uses 'set_speed' in code
        float set_speed = default_speed * ((float(255))/(100)); 
     
    // Calibrated speeds of each motor (seperated into front / rear wheels
                                               
     // front wheels
        float M1 = set_speed;
        float M2 = ((float(M1) * 120) / M1);
   
     // rear wheels
        float M3 = ((float(M1) * 140) / M1);
        float M4 = ((float(M1) * 140) / M1);

     // Turn off all motors to start by "releasing them"
        Motor1->run(RELEASE);
        Motor2->run(RELEASE);
        Motor3->run(RELEASE);
        Motor4->run(RELEASE);

    }

  //  set motors: FORWARD, BACKWARD, or RELEASE
void Drive_State(int DRIVE_STATE)
{
  if (DRIVE_STATE == FORWARD)
    {Serial.println("FORWARD");}
   
    else if (DRIVE_STATE == BACKWARD)
      {Serial.println("BACKWARD");}

    else 
      {Serial.println("RELEASE");}

 
      // front wheels
        Motor1->run(DRIVE_STATE);
        Motor2->run(DRIVE_STATE);
     
      // rear wheels
       Motor3->run(DRIVE_STATE);
       Motor4->run(DRIVE_STATE);
 
       Serial.println();
     
      delay(motor_delay);
}

   private:
            // nothing is currently here
           
};

/***********************************
          CLASSES SETUP, FORM:
         Class [space] Object  ;
***********************************/

// motors class
Motors motors_function;

/****************************************
     SETUP
****************************************/

void setup()
{
  Serial.begin(9600);
   
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz

// Motors setup
motors_function.Setup_Motors();

delay(2000); // wait 2 seconds at start up

}

/****************************************
     MAIN LOOP
****************************************/

void loop()
{
  motors_function.Drive_State(FORWARD); // set motors either FORWARD or BACKWARD
}

vinceherman

Have you confirmed that the hardware is still working by uploading the old code that worked?

project_science

Yes,  I did just that, and the motors ran as expected.

PaulS

Where have you set the speed that the motors should run at? Perhaps they are running, but very slowly. Put a chalk mark on the tires, and come back in a couple of lifetimes, and see if the marks have moved.
The art of getting good answers lies in asking good questions.

MarkT

Your class is just two functions wrapped inside a class declaration, and the first one
doesn't setup the motors AFAICT - it calculates some speeds and throws the results away.

The class should encapsulate the stuff, such as the four motors, which is it handles.  The speeds
should be instance variables and would be passed to the motors when needed.  Currently your motors
are global in scope and your class has no instance variables.

If you don't really understand what I've just said, get rid of the class, just use functions for now,
you'll have a much easier time understanding the compiler errors that way...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

project_science

Ok, I think I see what you're getting at, and I see now that I never set any of the motor's speeds!

I've simplified it, and I moved all the motor variables to the "private" part of the class.  It seems there's an issue with setting the motor speeds because when I try to upload, I get the error msg:

"'Motor1' does not name a type"
(it's the same for all 4 motors)

(In my previous code when just using functions, I never declared a type.)

Code: [Select]

  // set speed for motors (these are defined in the variables)
        Motor1->setSpeed(M1);
        Motor2->setSpeed(M2);
        Motor3->setSpeed(M3);
        Motor4->setSpeed(M4);


Here's my updated code:

Code: [Select]

/****************************************
     LIBRARIES
****************************************/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

/****************************************
     VARIABLES - GLOBAL
****************************************/



/****************************************
     OBJECTS
****************************************/

/*****MOTOR OBJECTS*****/

// Create motor shield object with default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
       /* Or, create it with a different I2C address (say for stacking)
          Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
       */


/***********************************
            CLASS:  Motors
***********************************/

class Motors
{
  public:
 
    void Setup_Motors()
    {

      // Define motors
        // *MotorX, where "X" is motor number (e.g. M1, M2...)
        Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
        Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
        Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
        Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);
   
       // Turn off all motors to start by "releasing them"
        Motor1->run(RELEASE);
        Motor2->run(RELEASE);
        Motor3->run(RELEASE);
        Motor4->run(RELEASE);

    }

  //  set motors: FORWARD, BACKWARD, or RELEASE
void Drive_State(int DRIVE_STATE)
{
  if (DRIVE_STATE == FORWARD)
    {Serial.println("FORWARD");}
   
    else if (DRIVE_STATE == BACKWARD)
      {Serial.println("BACKWARD");}

    else 
      {Serial.println("RELEASE");}

 
      // front wheels
        Motor1->run(DRIVE_STATE);
        Motor2->run(DRIVE_STATE);
     
      // rear wheels
       Motor3->run(DRIVE_STATE);
       Motor4->run(DRIVE_STATE);
 
       Serial.println();
     
      delay(motor_delay);

     
}


   private:
       
        // set default speed for motors, 0 (off) to 255 (max speed)
    /* Notes:  (1) The differences between values is small, so take M1 to be
                representative of all speeds
               (2) The math is slightly changed from intro as to avoid truncation
    */

      // Set Speed of all motors
      int default_speed = 50;  // % of max speed, range: 0-100.  Change this only.
        // do NOT change line below.  It uses 'set_speed' in code
      float set_speed = default_speed * ((float(255))/(100)); 

           
    // Calibrated speeds of each motor (seperated into front / rear wheels
                                               
     // front wheels
        float M1 = set_speed;
        float M2 = ((float(M1) * 120) / M1);
   
     // rear wheels
        float M3 = ((float(M1) * 140) / M1);
        float M4 = ((float(M1) * 140) / M1);

     // set speed for motors (these are defined in the variables)
        Motor1->setSpeed(M1);
        Motor2->setSpeed(M2);
        Motor3->setSpeed(M3);
        Motor4->setSpeed(M4);

    // Wait time between motor calls
        static const int motor_delay = 10;
           
};

/***********************************
          CLASSES SETUP, FORM:
         Class [space] Object  ;
***********************************/

// motors class
Motors motors_function;

/****************************************
     SETUP
****************************************/

void setup()
{
  Serial.begin(9600);
   
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz

// Motors setup
motors_function.Setup_Motors();

delay(2000); // wait 2 seconds at start up

}

/****************************************
     MAIN LOOP
****************************************/

void loop()
{

motors_function.Drive_State(FORWARD); // set motors either FORWARD or BACKWARD

}

MarkT

Get rid of the class, just use functions for now, you can read up about classes after
you've got your motors running (your understanding isn't there yet I think)
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

project_science

I've got things working now, thanks!

Go Up