Go Down

### Topic: How do I impliment Differential Drive (Read 3609 times)previous topic - next topic

#### SubMicro

##### Sep 15, 2010, 10:44 pm
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.....
BTW I'm a newbie if ya couldn't tell.
SubMicro
Electronics newbie
Home Machinist

#### drhex

#1
##### Sep 15, 2010, 10:54 pm
It is easier to find the problem if you post your code. Mark it with the # button.

#### SubMicro

#2
##### Sep 15, 2010, 11:05 pmLast Edit: Sep 15, 2010, 11:09 pm by SubMicro Reason: 1
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: [Select]
// 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:

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
}
}

SubMicro
Electronics newbie
Home Machinist

#### Grumpy_Mike

#3
##### Sep 15, 2010, 11:08 pm
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?

#### SubMicro

#4
##### Sep 15, 2010, 11:12 pm
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.
SubMicro
Electronics newbie
Home Machinist

#### SubMicro

#5
##### Sep 15, 2010, 11:14 pm
SubMicro
Electronics newbie
Home Machinist

#### PaulS

#6
##### Sep 16, 2010, 01:26 am
Host the picture on a photo-sharing site like flickr. Then, post a link to the picture.

#### SubMicro

#7
##### Sep 16, 2010, 02:12 amLast Edit: Sep 16, 2010, 02:15 am by SubMicro Reason: 1
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
SubMicro
Electronics newbie
Home Machinist

#### thoed

#8
##### Sep 16, 2010, 09:29 am
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/

#### SubMicro

#9
##### Nov 18, 2010, 03:47 am
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: [Select]
// 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
SubMicro
Electronics newbie
Home Machinist

Go Up