Go Down

Topic: PID to control robot move in straight line!!!! urgent please (Read 9676 times) previous topic - next topic

sourosael

Mar 10, 2014, 09:25 pm Last Edit: Mar 10, 2014, 09:33 pm by sourosael Reason: 1
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 https://www.sparkfun.com/products/9815
http://www.pololu.com/product/1218 http://www.pololu.com/product/1218
i have also 3 x Sharp IR sensors (one front and two in the side of the robot)

here is my code:
Code: [Select]

#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:
Code: [Select]
-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 ????

solderspot

I've been using PID for my bot: http://solderspot.wordpress.com/tag/pid-controllers/

You can look at the code here: https://github.com/solderspot/WallieBot
(version 3, 4 and 5.)

PID is straight forward and a lot of fun! :)

Erni

solderspot
Thank you for your link and blogpost,
well written and understandable for a PID beginner like me

solderspot


Thank you for your link and blogpost,
well written and understandable for a PID beginner like me


Thank you Erni. Glad it is helpful. I enjoy keeping a journal and it helps me to learn better too.

mirith

FYI, all motors are slightly different and thus two 'identical' motors with the same voltage across them won't spin at the same speed, and without having some internal feedback or calibration, it won't go straight.  You'll need to figure out a way to calibrate the speed of the two motors against each other.

Another option is to install encoders on the wheels and use a PID loop to maintain the same speed on each.

syedmoosa

i am using 2 encoders to control the motor and move the motor in straight path but i cant able to run the motors on straight path due to the difference in speed in both the motors.
my encoder has total 20 slots.

please send a code to run both motors in straight path using encoders.

thanks in advance

vinceherman

@syedmoosa, rather than resurrect an old post, it will be better if you start a thread of your own.

You are a new forum member.  It will be helpful for you to read through the sticky post at the top of this and every forum named "How to use this forum - please read."

And rather than sending you code, we will help you write your own.
Describe what you are trying to do.
Post code (using code tags) that represents your best effort at accomplishing your goal.
Describe how the actual results differ from your expectations.

If you have questions about how to do something that you would like answered before you write code, that is OK.  Ask. 

Go Up