how to do 2 PID continuously...

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

how to do 2 PID continuously...

Postby j6433 » Tue May 14, 2013 1:48 am

how can we adapt this code(from robot head to toe volume 4, PID for embedded design) to make the 2 dc motors with encoders to move forward 180° , after that immediately make robot turn right by making 1 motor forward and 1 motor backward inside Arduino?
CODE: SELECT_ALL_CODE

signed int set_value = 180;
signed int feedback_value = 0;
unsigned char kp = 2;
unsigned char ki = 0;
unsigned char kd = 1;

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


i need to make it move forward, then reset encoder reading, then move right...
but only managed to do this...
CODE: SELECT_ALL_CODE
 

void loop()
{
while((feedback_value = getLeftEncoder()) != set_value)
  {
  calcPID();     //pid to move the robot forward
  }
resetEncoder();


while((feedback_value = getLeftEncoder()) != set_valueRotate)
{
  calcPIDRight();     //pid to move the robot right 90°
}

}


but what happens is it wont make the robot go right... it stucks in the first while loop... is there a good way to write looping to do 2 PID continuously?
j6433
Novice
 
Posts: 20
Joined: Tue Oct 16, 2012 2:08 pm

Re: how to do 2 PID continuously...

Postby waiweng83 » Tue May 14, 2013 9:56 am

Your robot continues to move forward or it just goes forward reverse forward reverse when it reaches the first target?
With the power of dream, nothing is impossible...
User avatar
waiweng83
Moderator
 
Posts: 205
Joined: Wed Apr 15, 2009 2:17 pm

Re: how to do 2 PID continuously...

Postby j6433 » Tue May 14, 2013 5:01 pm

waiweng83 WROTE:Your robot continues to move forward or it just goes forward reverse forward reverse when it reaches the first target?


last few program that i attempted, it will forward reverse forward reverse unable to reach steady state probably because of delay from doing other things like Serial.print...

CODE: SELECT_ALL_CODE
while((feedback_value = getLeftEncoder()) != set_value)
  {
  calcPID();     //pid to move the robot forward
  }
resetEncoder();


this loop makes the robot stop in the first while loop because when it stops, it is so near to the setpoint, that the pwm is too small unable to move the robot, so it keep looping...
j6433
Novice
 
Posts: 20
Joined: Tue Oct 16, 2012 2:08 pm

Re: how to do 2 PID continuously...

Postby waiweng83 » Wed May 15, 2013 12:08 pm

I suspect that it's because your robot cannot reach exactly at the set_value. I suggest that you modify your code so that when it reaches a range around the set_value, it's considered it reaches its target.

For example, lets say your set_value is 180, instead of waiting your robot encoder count to reach exactly 180, maybe you can consider your robot has reached its target when the encoder count is within the range of 170 - 190. Of course you may need to tune the range a bit depends on the precision you need.
With the power of dream, nothing is impossible...
User avatar
waiweng83
Moderator
 
Posts: 205
Joined: Wed Apr 15, 2009 2:17 pm

Re: how to do 2 PID continuously...

Postby j6433 » Thu May 16, 2013 12:17 am

thanks... i made it at last...by making the range bigger, but i have problem scanning rfid tags while PID has not attained its steady state? because while the robot move forward using PID, it will pass through rfid tags... :roll: i have found a good library for getting rfid card codes, just not sure where to put them in the program... inside the While loop?
j6433
Novice
 
Posts: 20
Joined: Tue Oct 16, 2012 2:08 pm

Re: how to do 2 PID continuously...

Postby waiweng83 » Thu May 16, 2013 9:19 am

Yes, you can try to put it inside the loop. But this may affect the timing of the loop and you may need to retune your Ki and Kd.
With the power of dream, nothing is impossible...
User avatar
waiweng83
Moderator
 
Posts: 205
Joined: Wed Apr 15, 2009 2:17 pm


Return to DC Motor

Who is online

Users browsing this forum: No registered users and 11 guests

cron