control dc motor position with PID?

LINIX Brushless, VEXTA Brushless, RC Servo, DC Geared, Linear, Stepper, Tamiya.....

control dc motor position with PID?

Postby j6433 » Tue May 07, 2013 3:31 pm

after reading up robot head to toe volume 4, i adapted the code for pic onto arduino and came out with this function calcPID to move forward 10cm...
CODE: SELECT_ALL_CODE
void calcPID ()         
{
   signed long error_value = 0;
   static signed long pre_error = 0;
   static signed long integral = 0;
   signed long derivative = 0;
   signed long output = 0;
   signed int motor_direction = 0;
    unsigned char pwm_value=0;                     

   error_value = set_value - feedback_value;            // Calculate the error.
   integral = integral + error_value;               // Calculate integral.
   derivative = error_value - pre_error;               // Calculate derivative.
      
   output = (kp * error_value) + (ki * integral) + (kd * derivative);   // Calculate the output, pwm.
      
   if (output > 255) output = 255;                  // Limit the output to maximum 255.
   else if (output < -255) output = -255;
      
   if (output > 0)   goRight((unsigned char)output);         // When output is positive, turn right.
   else if (output < 0) goLeft((unsigned char)(-output));      // When error is negative, turn left.
   else tempStop();                     // When ouput is 0, stop the motor.
      
   pre_error = error_value;                  // Save as previous error.   
}


the program works fine when put inside
CODE: SELECT_ALL_CODE
void loop()
{
   calcPID();
}


but when i add up some other things like below, the motor was not able to reach steady state and becomes slow reacting...wheels rotating left right, left right...

CODE: SELECT_ALL_CODE
void loop()
{
     calcPID();
     Serial.print(getLeftEncoder()); //print out left encoder reading
}


ok believing its because arduino will read calcPID, then before calcPID reach steady state it print out encoder value, causing lags, i try to wrap the calcPID(); inside a while loop, making sure that if not reaching set_value i want, hopefully it will keep doing the function calcPID like this

CODE: SELECT_ALL_CODE
void loop()
{
    while((feedback_value = getRightEncoder()) != set_value)
    {
          calcPID();
    }
Serial.print(getLeftEncoder()); //print out left encoder reading
}


again the result is the same, the pid was slow reacting..

i need to do this because i want the robot to move forward for 10cm when it meets line following junction..

... and how arduino acts is it does not finish Doing the calcPID even if its inside while loop, then already go outside to print out the values of encoder... is there any ways to write this PID? other than using this code i have tried to use PID library from arduino playground, the result is the same...
j6433
Novice
 
Posts: 20
Joined: Tue Oct 16, 2012 2:08 pm

Return to DC Motor

Who is online

Users browsing this forum: No registered users and 9 guests

cron