Go Down

Topic: Arduino autotune pid libraly (Read 4053 times) previous topic - next topic

stunito0o0

Hey guys i have some questions for this library.
1.Is this autotune library made to work itself or you have to combine it with the original arduino pid library?
2.Where exactly i should put the values i  have to get the right output?
3.What is  Kp, Ki and Kd?
Thanks for your answers in advance guys, sorry for my bad english.
P.S Almost forgot here are the links to the libraries

http://playground.arduino.cc//Code/PIDAutotuneLibrary
http://playground.arduino.cc/Code/PIDLibrary

PeterH


3.What is  Kp, Ki and Kd?


Do you understand what the PID algorithm is?

stunito0o0


PeterH

#3
Aug 28, 2013, 06:58 pm Last Edit: Aug 28, 2013, 06:59 pm by PeterH Reason: 1

No i am new  :)


Well, trying to tune a PID implementation without understanding what PID is, seems like a futile exercise.

You might notice that those variable names correspond to the three letters that spell PID. Once you understand what PID is, the meaning of the three tuning parameters should become obvious.

stunito0o0

#4
Aug 28, 2013, 07:22 pm Last Edit: Aug 28, 2013, 08:01 pm by stunito0o0 Reason: 1
ok,
so i understood from wikipedia that,
Kp is proportion and its for direct correction of the error and when it goes bigger  error is getting smaller
Ki is integral and its for faster correction of bigger errors
Kd is derivative and its for faster correction of the changing error and correcting it
But i dont want to now how it works  :smiley-mr-green: .Its enough for me that there is a library ready for use by newbies like me. So i just want to understand the basics of the code like where to put the IMU data, where to put the motor speed data i want and such.Thank you !

PeterH


But i dont want to now how it works


IMO you NEED to understand what the PID algorithm is in order to make use of it in your code. It's quite simple, although the summary you gave doesn't seem quite accurate to me and I hope that wasn't taken directly from Wikipedia.

Once you understand that the PID algorithm provides three feedback components and the parameters you listed define the gain of the three components, using them is simple. But unless/until you understand what PID is and what PID does, it is IMO futile for you to try to tune it.

stunito0o0

ok can you explain it a little simpler ,english is not my mother language and i can't understand it very well.

PeterH

I assume you have got the concept that PID provides three feedback components which are intended to reduce an error to zero, where the error is the difference between the actual state of your system, and your desired state.

The proportional component produces a correction which is proportional to the error. In mechanical terms, you could think of it as a spring.

The derivative component produces a correction which opposes changes in the error. In mechanical terms you could think of it as a damper.

The integral term produces a correction which is proportional to the sustained error over some period. If you're still thinking in mechanical terms then you could visualise this as a ride height leveling system on a car's suspension.

stunito0o0

I get it now.Thanks.
By the way i was looking at the autotune code->
Code: [Select]
#include <PID_v1.h>
#include <PID_AutoTune_v0.h>

byte ATuneModeRemember=2;
double input=80, output=50, setpoint=180;
double kp=2,ki=0.5,kd=2;

double kpmodel=1.5, taup=100, theta[50];
double outputStart=5;
double aTuneStep=50, aTuneNoise=1, aTuneStartValue=100;
unsigned int aTuneLookBack=20;

boolean tuning = false;
unsigned long  modelTime, serialTime;

PID myPID(&input, &output, &setpoint,kp,ki,kd, DIRECT);
PID_ATune aTune(&input, &output);


void setup()
{
  //Setup the pid
  myPID.SetMode(AUTOMATIC);

  if(tuning)
  {
    tuning=false;
    changeAutoTune();
    tuning=true;
  }
 
  serialTime = 0;
  Serial.begin(9600);

}

void loop()
{

  unsigned long now = millis();

  //pull the input in from the real world
    input = 50;

 
  if(tuning)
  {
    byte val = (aTune.Runtime());
    if (val!=0)
    {
      tuning = false;
    }
    if(!tuning)
    { //we're done, set the tuning parameters
      kp = aTune.GetKp();
      ki = aTune.GetKi();
      kd = aTune.GetKd();
      myPID.SetTunings(kp,ki,kd);
      AutoTuneHelper(false);
    }
  }
  else myPID.Compute();
 
 
     //Serial.print(output, DEC);

 
  //send-receive with processing if it's time
  if(millis()>serialTime)
  {
    SerialReceive();
    SerialSend();
    serialTime+=500;
  }
}

void changeAutoTune()
{
if(!tuning)
  {
    //Set the output to the desired starting frequency.
    output=aTuneStartValue;
    aTune.SetNoiseBand(aTuneNoise);
    aTune.SetOutputStep(aTuneStep);
    aTune.SetLookbackSec((int)aTuneLookBack);
    AutoTuneHelper(true);
    tuning = true;
  }
  else
  { //cancel autotune
    aTune.Cancel();
    tuning = false;
    AutoTuneHelper(false);
  }
}

void AutoTuneHelper(boolean start)
{
  if(start)
    ATuneModeRemember = myPID.GetMode();
  else
    myPID.SetMode(ATuneModeRemember);
}


void SerialSend()
{
  Serial.print("setpoint: ");Serial.print(setpoint); Serial.print(" ");
  Serial.print("input: ");Serial.print(input); Serial.print(" ");
  Serial.print("output: ");Serial.print(output); Serial.print(" ");
  if(tuning){
    Serial.println("tuning mode");
  } else {
    Serial.print("kp: ");Serial.print(myPID.GetKp());Serial.print(" ");
    Serial.print("ki: ");Serial.print(myPID.GetKi());Serial.print(" ");
    Serial.print("kd: ");Serial.print(myPID.GetKd());Serial.println();
  }
}

void SerialReceive()
{
  if(Serial.available())
  {
   char b = Serial.read();
   Serial.flush();
   if((b=='1' && !tuning) || (b!='1' && tuning))changeAutoTune();
  }
}

void DoModel()
{
  //cycle the dead time
  for(byte i=0;i<49;i++)
  {
    theta[i] = theta[i+1];
  }
  //compute the input
  input = (kpmodel / taup) *(theta[0]-outputStart) + input*(1-1/taup) + ((float)random(-10,10))/100;

}

and  i am wondering 2 things:
1. When i put x axis(like 50degrees) of the  IMU as input and a different from the input setpoint(like 80 degrees) is the output  in degrees too?
2. Why when the setpoint is smaller than the input the output always shows 0.0000000 ?

Go Up