Pages: [1]   Go Down
Author Topic: How do I impliment Differential Drive  (Read 1337 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do I implement differential drive?
I've searched this forum and I can't find an explanation I can understand, or a snippet to reverse engineer.
I've yet to get my h-bridge working correctly also

I'm using the Tamiya twin motor gearbox, an L293 h-bridge and Arduino of course!

For some reason everytime I try and reverse the motors direction one just stops while the other keeps on truckin!
I've been at this for 2 weeks now and about to go insane.............What's that? Yes mother she must die..... ;D
BTW I'm a newbie if ya couldn't tell.
Logged

SubMicro
Electronics newbie & Home Machinist

Knivsta,Sweden
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is easier to find the problem if you post your code. Mark it with the # button.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is just what I've been using to test the h-bridge.

Also I'm just using an L293 plugged into a breadboard with no external parts added, although I did try adding caps but that didn't help. :-/

Code:
 // h bridge test  
  
  const int switchPin = 12;    // switch input
  const int enable1Pin = 9;    // Enable for Motor 1
  const int enable2Pin = 10;    // Enable for Motor 2
  const int motor1Pin = 7;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 8;    // H-bridge leg 2 (pin 7, 2A)
  const int motor3Pin = 4;    // H-bridge leg 3 (pin 10, 3A)
  const int motor4Pin = 2;    // H-bridge leg 4 (pin 15, 4A)
  const int ledPin = 13;      // LED
  
  
  void setup() {
    // set the switch as an input:
    pinMode(switchPin, INPUT);

    // set all the other pins you're using as outputs:
    pinMode(enable1Pin, OUTPUT);
    pinMode(enable2Pin, OUTPUT);
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(motor3Pin, OUTPUT);
    pinMode(motor4Pin, OUTPUT);
    pinMode(ledPin, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enable1Pin, HIGH); //Uncomment if enable pins are not tied to Vcc
    digitalWrite(enable2Pin, HIGH); //Uncomment if enable pins are not tied to Vcc
    digitalWrite(switchPin, HIGH);  // Enable internal pull up
  
  }

  void loop() {
    
    
    
    // if the switch is high, motor will turn on one direction:
    
      if (digitalRead(switchPin) == HIGH) {
      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
      }
    // if the switch is low, motor will turn in the other direction:
      else {
     delay(1000);
    
      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
    }
 }

« Last Edit: September 15, 2010, 04:09:03 pm by SubMicro » Logged

SubMicro
Electronics newbie & Home Machinist

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34581
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So what happens with this code?
It looks alright to me.
Maybe it is your hardware setup, can you post a schematic?

Why is this in the software section?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Motors turn when switch is pressed one motor stops the other just keeps going. And I've been getting all kinds of weird varriation of that behavior depending on what I try to correct it.

This is in software because I also was asking how to implement differential drive in code.
Logged

SubMicro
Electronics newbie & Home Machinist

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do I add pics?
Logged

SubMicro
Electronics newbie & Home Machinist

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Host the picture on a photo-sharing site like flickr. Then, post a link to the picture.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a Fritzing pic of my setup.
http://www.flickr.com/photos/33536645@N06/4994054469/

The only difference is in the pic the enable pins have been tied to Vcc
« Last Edit: September 15, 2010, 07:15:05 pm by SubMicro » Logged

SubMicro
Electronics newbie & Home Machinist

Spokane, Washington
Offline Offline
God Member
*****
Karma: 1
Posts: 686
My name is Bob, and I'm an addict.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm assuming they're mistakes, but couple things I noticed:

You don't have power going to the regulated 5V input or pin 8. I just use the 5v from the Arduino.

Also, the bottom motor isn't connect correctly, the green wire is on one of the INPUTS, and not the motor output.


Also one thing you could try doing is changing the pins that you're driving HIGH/LOW.


http://luckylarry.co.uk/arduino-projects/control-a-dc-motor-with-arduino-and-l293d-chip/
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I discovered the problem it was pin 7 was constantly high regardless of code. So I replaced the 168 with a 328pu works great!
Now I need to get differential drive going any pointers?

I found this test code and modified it a bit. Gonna use this as my starting point.

Code:
// Use this code to test your motor with the Arduino board:
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// -----------------------------------------------------Motors
int motor_left[] = {4, 5};
int motor_right[] = {6, 7};
// -----------------------------------------------------Enable pins
#define pwm1 11
#define pwm2 10
#define ledfwd 9
#define ledrev 12
#define ledStop 2
#define ledRturn 13
#define ledLturn 8

// -----------------------------------------------------Setup
void setup() {
Serial.begin(9600);
// Setup motors
pinMode(ledfwd,OUTPUT);
pinMode(ledrev,OUTPUT);
pinMode(pwm1,OUTPUT);
pinMode(pwm2,OUTPUT);
pinMode(ledStop,OUTPUT);
pinMode(ledLturn,OUTPUT);
pinMode(ledRturn,OUTPUT);
digitalWrite(pwm1,HIGH);
digitalWrite(pwm2,HIGH);

int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}
// -----------------------------------------------------Loop
void loop() {
Serial.println("Forward");
drive_forward();
delay(2500);
motor_stop();
delay(750);
Serial.println("Reverse");
drive_backward();
delay(2500);
motor_stop();
delay(750);
Serial.println("Turn Left");
turn_left();
delay(2500);
motor_stop();
delay(750);
Serial.println("Turn Right");
turn_right();
delay(2500);
motor_stop();
delay(750);
Serial.println("ALL STOP!");
motor_stop();
delay(2500);
}
// -----------------------------------------------------Drive
void motor_stop()
    {
     digitalWrite(ledStop, HIGH);
     digitalWrite(motor_left[0], LOW);
     digitalWrite(motor_left[1], LOW);
     digitalWrite(motor_right[0], LOW);
     digitalWrite(motor_right[1], LOW);
     digitalWrite(ledfwd, LOW);
     digitalWrite(ledrev, LOW);
     digitalWrite(ledRturn, LOW);
     digitalWrite(ledLturn, LOW);
     delay(25);
    }
void drive_forward()
    {
     digitalWrite(ledStop, LOW);
     digitalWrite(motor_left[0], HIGH);
     digitalWrite(motor_left[1], LOW);
     digitalWrite(motor_right[0], HIGH);
     digitalWrite(motor_right[1], LOW);
     digitalWrite(ledfwd, HIGH);
     digitalWrite(ledrev, LOW);
     digitalWrite(ledRturn, LOW);
     digitalWrite(ledLturn, LOW);
    }
void drive_backward()
    {
     digitalWrite(ledStop, LOW);
     digitalWrite(motor_left[0], LOW);
     digitalWrite(motor_left[1], HIGH);
     digitalWrite(motor_right[0], LOW);
     digitalWrite(motor_right[1], HIGH);
     digitalWrite(ledrev, HIGH);
     digitalWrite(ledfwd, LOW);
     digitalWrite(ledRturn, LOW);
     digitalWrite(ledLturn, LOW);
    }
void turn_left()
    {
     digitalWrite(ledStop, LOW);
     digitalWrite(motor_left[0], LOW);
     digitalWrite(motor_left[1], HIGH);
     digitalWrite(motor_right[0], HIGH);
     digitalWrite(motor_right[1], LOW);
     digitalWrite(ledfwd, LOW);
     digitalWrite(ledrev, LOW);
     digitalWrite(ledRturn, LOW);
     digitalWrite(ledLturn, HIGH);
    }
void turn_right()
    {
     digitalWrite(ledStop, LOW);
     digitalWrite(motor_left[0], HIGH);
     digitalWrite(motor_left[1], LOW);
     digitalWrite(motor_right[0], LOW);
     digitalWrite(motor_right[1], HIGH);
     digitalWrite(ledfwd, LOW);
     digitalWrite(ledrev, LOW);
     digitalWrite(ledRturn, HIGH);
     digitalWrite(ledLturn, LOW);
    }

SubMicro
Logged

SubMicro
Electronics newbie & Home Machinist

Pages: [1]   Go Up
Jump to: