problem with implimenting differential drive for robot

06 Oct 2010

My main problem is understanding how to use the motor library to drive an h-bridge with 2 motors.

I'm new to mbed and so far I love it, but i'm used to the simpler Arduino. Any pointers would be helpful.

P.S. Using L293 h-bridge and two small 6v motors for testing.

06 Oct 2010

How did you do it in Arduino? Can you post a small code snippet?

06 Oct 2010 . Edited: 06 Oct 2010

Here is the code I used to make sure the setup worked as exected.

 

///////////////////
// h bridge test //
///////////////////


int enablePin1 = 10;    // Enable for Motor 1
int enablePin2 = 11;    // Enable for Motor 2
int motor1Pin = 2;    // Motor1A 
int motor2Pin = 3;    // Motor1B
int motor3Pin = 4;    // Motor2A
int motor4Pin = 5;    // Motor2B

void setup() {

  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(motor3Pin, OUTPUT);
  pinMode(motor4Pin, OUTPUT);

  digitalWrite(enablePin1, HIGH); //comment line if enable pins are tied to Vcc
  digitalWrite(enablePin2, HIGH); //comment line if enable pins are tied to Vcc
}

void loop() {

  digitalWrite(motor1Pin, LOW);   // set leg 1A of the H-bridge low
  digitalWrite(motor2Pin, HIGH);  // set leg 2A of the H-bridge high
  digitalWrite(motor3Pin, LOW);   // set leg 1B of the H-bridge low
  digitalWrite(motor4Pin, HIGH);  // set leg 2B of the H-bridge high
  delay(2500);      
  digitalWrite(motor1Pin, HIGH);  // set leg 1A of the H-bridge high
  digitalWrite(motor2Pin, LOW);   // set leg 2A of the H-bridge low
  digitalWrite(motor3Pin, HIGH);  // set leg 1B of the H-bridge low
  digitalWrite(motor4Pin, LOW);   // set leg 2B of the H-bridge high
  delay(2500);  
}

Here is PWM test code

  const int motorAPin1 = 8;   // AIN1
  const int motorAPin2 = 9;   // AIN2
  const int motorBPin1 = 10;  // BIN1
  const int motorBPin2 = 11;  // BIN2
  const int enablePin = 2;    // H-bridge enable pin
  const int ledPin = 13;      // LED
  const int PWMA = 3;         // Pulse Width Modulation Motor A
  const int PWMB = 5;         // Pulse Width Modulation Motor B


  void setup() {
  
    // set outputs:
    pinMode(motorAPin1, OUTPUT);
    pinMode(motorAPin2, OUTPUT);
    pinMode(motorBPin1, OUTPUT);
    pinMode(motorBPin2, OUTPUT);
    pinMode(enablePin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(PWMA, OUTPUT);
    pinMode(PWMB, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
    
  }

  void loop() {
    analogWrite(motorAPin1, LOW);
    analogWrite(motorAPin2, HIGH);
    analogWrite(motorBPin1, LOW);
    analogWrite(motorBPin2, HIGH);
    analogWrite(PWMA, 60);
}

06 Oct 2010

The code for the mbed will be even simpler than the Arduino :)

You can use DigitalOut objects for the H-bridge enable/motor pins, e.g.

DigitalOut enablePin1 = p10; //Enable pin for Motor 1

instead of

int enablePin1 = 10;    // Enable for Motor 1

And PwmOut objects for H-bridge PWM pins e.g.

PwmOut pwmA = p3; //PWM Motor A
instead of

int PWMA = 3 // Pulse Width Modulation Motor A
You can check out the APIs for the DigitalOut and the PwmOut classes to find out how to use them.

 

07 Oct 2010 . Edited: 07 Oct 2010

Thank you

Your right that is simpler! :-) I thought this was gonna be a hard learning curve. Especially since I'm not that good with the Arduino!

P.S. how does the period efect the motor? e.g.

"

servo.period(0.020); // servo requires a 20ms period

"

Also in this statement does += mean the same as ++ as used with arduino?

"

while (1) {
        for(float offset=0.0; offset<0.001; offset+=0.0001) {
            servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
            wait(0.25);
"

SubMicro

07 Oct 2010 . Edited: 07 Oct 2010

+= is not the same as ++.

It is almost the same function, except when using += you can define the number of increments, either by a number or a variable.

Example of ++:

int var=1;

var++; //the same as var=var+1, -> var is now 2


Example of +=:

int var=1;

var+=2; //the same as var=var+2, var is now 3.


If you are new to C/C++, I recommend this webpage for the basics of the language:

http://www.cplusplus.com/

More specific to the last question... Operators:

http://www.cplusplus.com/doc/tutorial/operators/

07 Oct 2010

Thank you for the info and reference links.