hi
My project is a Maze solving robot and i have problem with the motors when am sending the same PWM the robot instead of moving in straight line, drifts to the right.
am using :
Arduino uno
Ardumoto - Motor Driver Shield SparkFun Ardumoto - Motor Driver Shield - DEV-14129 - SparkFun Electronics
Pololu 42×19mm Wheel and Encoder Set Pololu 42×19mm Wheel and Encoder Set
i have also 3 x Sharp IR sensors (one front and two in the side of the robot)
here is my code:
#include <PololuWheelEncoders.h>
PololuWheelEncoders encoders;
int pwm_L = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_R = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
void setup()
{
Serial.begin(9600);
encoders.init(2,4,7,8);
pinMode(pwm_L, OUTPUT); //Set control pins to be outputs
pinMode(pwm_R, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
}
void loop()
{
forward(); //continue full speed forward
printCounts();
delay(1200); // takes 1,2 seconds for one rev
stopped(); // stop for 2 seconds
delay(500);
backward();
printCounts();
delay(1200);
stopped(); // stop for 2 seconds
delay(500);
}
void forward() //full speed forward
{
digitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 low
digitalWrite(dir_b, LOW); //Reverse motor direction, 3 low, 4 high
analogWrite(pwm_L, 40); //set both motors to run at (100/255 = 39)% duty cycle
analogWrite(pwm_R, 40);
}
void backward() //full speed backward
{
digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, HIGH); //Set motor direction, 3 high, 4 low
analogWrite(pwm_L, 40); //set both motors to run at 100% duty cycle (fast)
analogWrite(pwm_R, 40);
}
void stopped()
{
digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, HIGH); //Set motor direction, 3 high, 4 low
analogWrite(pwm_L, 0); //set both motors to run at 100% duty cycle (fast)
analogWrite(pwm_R, 0);
}
void printCounts() {
Serial.print( encoders.getCountsAndResetM1(), DEC );
Serial.print(", ");
Serial.print( encoders.getCountsAndResetM2(), DEC );
Serial.println();
}
and here is what i get from encoders:
-44, 47
44, -47
-44, 47
44, -48
-45, 48
45, -48
-47, 48
47, -48
-45, 46
44, -48
-45, 47
45, -47
-46, 47
45, -47
-45, 47
44, -48
-44, 47
44, -48
-45, 47
45, -47
-45, 48
45, -48
-45, 48
45, -48
-45, 48
44, -48
-44, 48
45, -48
-46, 48
46, -48
-45, 47
45, -47
-45, 48
therefore to travel in straight line count must be equal.
anyone who knows how to help me implement a PID or PI control to control the motor ????