Robo car

One motor turns slightly faster than the other and the program I am using doesn't seem to have a way to adjust the speed of each motor to make up the difference. Would anyone suggest a way to add some code I could use to adjust a motor separately. Here is the program. It's an Arduino uno program.

#include<AFMotor.h>
#include <NewPing.h>
#include <Servo.h>

#define TRIG_PIN A4
#define ECHO_PIN A5
#define MAX_DISTANCE 200
#define MAX_SPEED 190 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);

Servo myservo;

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

myservo.attach(9);
myservo.write(90);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}

void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);

if(distance<=15)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);

if(distanceR>=distanceL)
{
turnRight();
moveStop();
}else
{
turnLeft();
moveStop();
}
}else
{
moveForward();
}
distance = readPing();
}

int lookRight()
{
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(90);
return distance;
}

int lookLeft()
{
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(90);
return distance;
delay(100);
}

int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}

void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}

void moveForward() {

if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(5);
}
}
}

void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
delay(5);
}
}

void turnRight() {
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(300);
motor1.run(FORWARD);
motor2.run(FORWARD);
}

void turnLeft() {
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(300);
motor1.run(FORWARD);
motor2.run(FORWARD);
}

Looks like MAX_SPEED_OFFSET is a trial-and-error attempt to do that, but I think you need to have an encoder of some kind on each motor and measure the actual speeds and adjust one until they're the same.

The code is already there and uses MAX_SPEED_OFFSET. In fact, this may cause the very problem that concerns you. You will have to choose different values here, compiling the code and downloading each time that you make a change.

In the line

#define MAX_SPEED_OFFSET 20

change the value 20 to other values. I would start with 0. You may even use negative values.

I thought that might be the answer but there was only one and thought there should be two. I will try your suggestions.
Thank you very much for your responses, I am grateful :slight_smile:

altronic:
there was only one

I suppose you could think of it as one wheel being at the "right" speed, so only the "wrong" one needs adjusting.

But that approach assumes the adjustment will be the same for all speeds, as an absolute value, whereas that might not be the case. The foolproof but obviously more complicated way is to measure and then correct in real time.

The foolproof but obviously more complicated way is to measure and then correct in real time.

A rotary encoder on each wheel would allow the individual wheel speeds to be measured and adjusted on the fly but then there is the problem of wheel slippage.

Been There, Done That, Didn't Get The Teeshirt

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile: