AF Motor Shield v1, Need PWM

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
project_science
 
Posts: 190
Joined: Sun May 04, 2014 1:26 pm

AF Motor Shield v1, Need PWM

Post by project_science »

Hi,

I'm running a 4WD robot using the AF Motor Shield v1. I put slave code on the robot, and drive it from master code on a remote control I build on a bread board (4 buttons, 4 directions). It's pretty simple. When I push the left button, left wheels go in reverse, and right wheels go forward. So the robot turns in place when instructed to.

What I'd like to do is create a differential drive for the robot. So when I press the left button, the left wheels will not spin in reverse, but will spin forward at, for example, 50% those of the right wheels. Actually, I need to be able to alter the PWM as needed.

There are a few ways I have thought to do this, but nothing seems to work.

In the code, I want to alter the FULL CODE (shown at the bottom of this question) to be:

Code: Select all

// TURN LEFT
void Left_Turn(void)
{
    Serial1.println("LEFT TURN");
    
       // front wheels
    Motor_Left_Front.run(FORWARD*0.5);
    Motor_Right_Front.run(FORWARD);
    
    // rear wheels
    Motor_Left_Rear.run(FORWARD*0.5);
    Motor_Right_Rear.run(FORWARD);
    
    delay(10);    
}
This does not seem to work.

So I also tried to introduce 'double x' for % of speed and similar to 'double y' in the FULL CODE, where x and y are decimals between 0-1. But this does not seem to work either.

(You can see what I'm trying to do in the FULL CODE, void loop() part, under ' else if (incomingByte == 'L') // spin LEFT'.)

Code: Select all

// TURN LEFT
void Left_Turn(void)
{
    Serial1.println("LEFT TURN");
    
       // front wheels
    Motor_Left_Front.run(pwm*x);
    Motor_Right_Front.run(pwm*y);
    
    // rear wheels
    Motor_Left_Rear.run(pwm*x);
    Motor_Right_Rear.run(pwm*y);
    
    delay(10);    
}

Below is the FULL CODE that does work (variables are set to type 'double', but initially I used type 'int' and it worked too), but it does not contain any PWM modifications as I would like. Is there a simple solution to what I'm trying to do?

Code: Select all

#include <AFMotor.h>  // AF Motor shield library

const double speed = 100; // percent of maximum speed
double x = 1;  // start x at 100% of speed, but doesn't seem to work
double y = 1;  // start y at 100% of speed, but doesn't seem to work
double pwm;

int incomingByte;      // a variable to read incoming serial data into

bool value = false; // set variable once for RC signal received

// define wheels (individual)

  // front wheels
  AF_DCMotor Motor_Left_Front(4, MOTOR34_1KHZ); // motor 4
  AF_DCMotor Motor_Right_Front(3, MOTOR34_1KHZ); // motor 3

  // rear wheels
  AF_DCMotor Motor_Left_Rear(1, MOTOR12_64KHZ); // motor 1
  AF_DCMotor Motor_Right_Rear(2, MOTOR12_64KHZ); // motor 2


//*****MOTOR FUNCTIONS  
      
  // Forward, Backward, Release
  
void Drive_State(int DRIVE_STATE)
{
  
  if (DRIVE_STATE == FORWARD)
    {Serial1.println("FORWARD");}
    
    else if (DRIVE_STATE == BACKWARD)
      {Serial1.println("BACKWARD");}

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

  
      // front wheels
      Motor_Left_Front.run(DRIVE_STATE);
      Motor_Right_Front.run(DRIVE_STATE);
      
      // rear wheels
      Motor_Left_Rear.run(DRIVE_STATE);
      Motor_Right_Rear.run(DRIVE_STATE);
  
      delay(10);
}

// TURN LEFT
void Left_Turn(void)
{
    Serial1.println("LEFT TURN");
    
       // front wheels
    Motor_Left_Front.run(BACKWARD);
    Motor_Right_Front.run(FORWARD);
    
    // rear wheels
    Motor_Left_Rear.run(BACKWARD);
    Motor_Right_Rear.run(FORWARD);
    
    delay(10);    
}

// TURN RIGHT
void Right_Turn(void)
{
    Serial1.println("RIGHT TURN");
    
    // front wheels
    Motor_Left_Front.run(FORWARD);
    Motor_Right_Front.run(BACKWARD);
    
    // rear wheels
    Motor_Left_Rear.run(FORWARD);
    Motor_Right_Rear.run(BACKWARD);
    
    delay(10);   
}

void setup() 
{
  Serial1.begin(9600);           // set up Serial library at 9600 bps

// scale percent into pwm range
pwm = map(speed, 0, 100, 0, 255);  // takes range of 0-255 and maps it to 0-100 for variable 'speed'
                                   // actual pwm value is define as const at beginning of code
                                   
// front wheels
Motor_Left_Front.setSpeed(pwm);
Motor_Right_Front.setSpeed(pwm);

// rear wheels
Motor_Left_Rear.setSpeed(pwm);
Motor_Right_Rear.setSpeed(pwm);

}

void loop() 
{

   if (Serial1.available() > 0)   // see if there's incoming serial data:
    {
      incomingByte = Serial1.read(); // read the oldest byte in the serial buffer:
      value = true;
      {
        if (incomingByte == 'F')  // FORWARD
          {
             Drive_State(FORWARD);    // forward
          } 
      
           else if (incomingByte == 'B')  // BACKWARD
            {
              Drive_State(BACKWARD);   // backward  
            } 
            
          else if (incomingByte == 'L')  // spin LEFT
            {
              x = 0.7; // turn left wheels 70% speed of right           
              Left_Turn();
            } 
            
          else if (incomingByte == 'R')  // spin RIGHT
            {
              y = 0.7; // turn right wheels 70% speed of left           
              Right_Turn();
            } 
            
       }
     }
     
      else
      {
        if (value == true)  // done so not always pinging motors
        {
            Drive_State(RELEASE);    // stop 
           // x = y = 1; // set pwm back to 255
            value = false;
        }
      }

}
Last edited by project_science on Sat May 17, 2014 4:16 pm, edited 1 time in total.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: AF Motor Shield v2, Need PWM

Post by adafruit_support_bill »

Below is the FULL CODE that does work
Not if you are using a V2 motor shield. The AF_Motor library is for the V1 shield. Post a photo of the shield so we can be certain what you are referring to.

User avatar
project_science
 
Posts: 190
Joined: Sun May 04, 2014 1:26 pm

Re: AF Motor Shield v2, Need PWM

Post by project_science »

Sorry it is the v1 shield. I corrected the title.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: AF Motor Shield v1, Need PWM

Post by adafruit_support_bill »

Code: Select all

    Motor_Left_Front.run(FORWARD*0.5);
    Motor_Right_Front.run(FORWARD);
FORWARD is a direction, not a speed. You can't scale it like that. use the setSpeed() function in the library.

User avatar
project_science
 
Posts: 190
Joined: Sun May 04, 2014 1:26 pm

Re: AF Motor Shield v1, Need PWM

Post by project_science »

That seem to work - will try it out. Thanks!

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”