Ajout suite code Arduino moteur DC
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
#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);
|
||||
|
||||
//set to false to connect to the real world
|
||||
boolean useSimulation = true;
|
||||
|
||||
void setup()
|
||||
{
|
||||
if(useSimulation)
|
||||
{
|
||||
for(byte i=0;i<50;i++)
|
||||
{
|
||||
theta[i]=outputStart;
|
||||
}
|
||||
modelTime = 0;
|
||||
}
|
||||
//Setup the pid
|
||||
myPID.SetMode(AUTOMATIC);
|
||||
|
||||
if(tuning)
|
||||
{
|
||||
tuning=false;
|
||||
changeAutoTune();
|
||||
tuning=true;
|
||||
}
|
||||
|
||||
serialTime = 0;
|
||||
Serial.begin(9600);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
unsigned long now = millis();
|
||||
|
||||
if(!useSimulation)
|
||||
{ //pull the input in from the real world
|
||||
input = analogRead(0);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if(useSimulation)
|
||||
{
|
||||
theta[30]=output;
|
||||
if(now>=modelTime)
|
||||
{
|
||||
modelTime +=100;
|
||||
DoModel();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
analogWrite(0,output);
|
||||
}
|
||||
|
||||
//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;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
#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);
|
||||
|
||||
//set to false to connect to the real world
|
||||
boolean useSimulation = true;
|
||||
|
||||
void setup()
|
||||
{
|
||||
if(useSimulation)
|
||||
{
|
||||
for(byte i=0;i<50;i++)
|
||||
{
|
||||
theta[i]=outputStart;
|
||||
}
|
||||
modelTime = 0;
|
||||
}
|
||||
//Setup the pid
|
||||
myPID.SetMode(AUTOMATIC);
|
||||
|
||||
if(tuning)
|
||||
{
|
||||
tuning=false;
|
||||
changeAutoTune();
|
||||
tuning=true;
|
||||
}
|
||||
|
||||
serialTime = 0;
|
||||
Serial.begin(9600);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
unsigned long now = millis();
|
||||
|
||||
if(!useSimulation)
|
||||
{ //pull the input in from the real world
|
||||
input = analogRead(0);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if(useSimulation)
|
||||
{
|
||||
theta[30]=output;
|
||||
if(now>=modelTime)
|
||||
{
|
||||
modelTime +=100;
|
||||
DoModel();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
analogWrite(0,output);
|
||||
}
|
||||
|
||||
//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;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include <PID_AutoTune_v0.h>
|
||||
|
||||
|
||||
PID_ATune::PID_ATune(double* Input, double* Output)
|
||||
{
|
||||
input = Input;
|
||||
output = Output;
|
||||
controlType =0 ; //default to PI
|
||||
noiseBand = 0.5;
|
||||
running = false;
|
||||
oStep = 30;
|
||||
SetLookbackSec(10);
|
||||
lastTime = millis();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PID_ATune::Cancel()
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
|
||||
int PID_ATune::Runtime()
|
||||
{
|
||||
justevaled=false;
|
||||
if(peakCount>9 && running)
|
||||
{
|
||||
running = false;
|
||||
FinishUp();
|
||||
return 1;
|
||||
}
|
||||
unsigned long now = millis();
|
||||
|
||||
if((now-lastTime)<sampleTime) return false;
|
||||
lastTime = now;
|
||||
double refVal = *input;
|
||||
justevaled=true;
|
||||
if(!running)
|
||||
{ //initialize working variables the first time around
|
||||
peakType = 0;
|
||||
peakCount=0;
|
||||
justchanged=false;
|
||||
absMax=refVal;
|
||||
absMin=refVal;
|
||||
setpoint = refVal;
|
||||
running = true;
|
||||
outputStart = *output;
|
||||
*output = outputStart+oStep;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(refVal>absMax)absMax=refVal;
|
||||
if(refVal<absMin)absMin=refVal;
|
||||
}
|
||||
|
||||
//oscillate the output base on the input's relation to the setpoint
|
||||
|
||||
if(refVal>setpoint+noiseBand) *output = outputStart-oStep;
|
||||
else if (refVal<setpoint-noiseBand) *output = outputStart+oStep;
|
||||
|
||||
|
||||
//bool isMax=true, isMin=true;
|
||||
isMax=true;isMin=true;
|
||||
//id peaks
|
||||
for(int i=nLookBack-1;i>=0;i--)
|
||||
{
|
||||
double val = lastInputs[i];
|
||||
if(isMax) isMax = refVal>val;
|
||||
if(isMin) isMin = refVal<val;
|
||||
lastInputs[i+1] = lastInputs[i];
|
||||
}
|
||||
lastInputs[0] = refVal;
|
||||
if(nLookBack<9)
|
||||
{ //we don't want to trust the maxes or mins until the inputs array has been filled
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(isMax)
|
||||
{
|
||||
if(peakType==0)peakType=1;
|
||||
if(peakType==-1)
|
||||
{
|
||||
peakType = 1;
|
||||
justchanged=true;
|
||||
peak2 = peak1;
|
||||
}
|
||||
peak1 = now;
|
||||
peaks[peakCount] = refVal;
|
||||
|
||||
}
|
||||
else if(isMin)
|
||||
{
|
||||
if(peakType==0)peakType=-1;
|
||||
if(peakType==1)
|
||||
{
|
||||
peakType=-1;
|
||||
peakCount++;
|
||||
justchanged=true;
|
||||
}
|
||||
|
||||
if(peakCount<10)peaks[peakCount] = refVal;
|
||||
}
|
||||
|
||||
if(justchanged && peakCount>2)
|
||||
{ //we've transitioned. check if we can autotune based on the last peaks
|
||||
double avgSeparation = (abs(peaks[peakCount-1]-peaks[peakCount-2])+abs(peaks[peakCount-2]-peaks[peakCount-3]))/2;
|
||||
if( avgSeparation < 0.05*(absMax-absMin))
|
||||
{
|
||||
FinishUp();
|
||||
running = false;
|
||||
return 1;
|
||||
|
||||
}
|
||||
}
|
||||
justchanged=false;
|
||||
return 0;
|
||||
}
|
||||
void PID_ATune::FinishUp()
|
||||
{
|
||||
*output = outputStart;
|
||||
//we can generate tuning parameters!
|
||||
Ku = 4*(2*oStep)/((absMax-absMin)*3.14159);
|
||||
Pu = (double)(peak1-peak2) / 1000;
|
||||
}
|
||||
|
||||
double PID_ATune::GetKp()
|
||||
{
|
||||
return controlType==1 ? 0.6 * Ku : 0.4 * Ku;
|
||||
}
|
||||
|
||||
double PID_ATune::GetKi()
|
||||
{
|
||||
return controlType==1? 1.2*Ku / Pu : 0.48 * Ku / Pu; // Ki = Kc/Ti
|
||||
}
|
||||
|
||||
double PID_ATune::GetKd()
|
||||
{
|
||||
return controlType==1? 0.075 * Ku * Pu : 0; //Kd = Kc * Td
|
||||
}
|
||||
|
||||
void PID_ATune::SetOutputStep(double Step)
|
||||
{
|
||||
oStep = Step;
|
||||
}
|
||||
|
||||
double PID_ATune::GetOutputStep()
|
||||
{
|
||||
return oStep;
|
||||
}
|
||||
|
||||
void PID_ATune::SetControlType(int Type) //0=PI, 1=PID
|
||||
{
|
||||
controlType = Type;
|
||||
}
|
||||
int PID_ATune::GetControlType()
|
||||
{
|
||||
return controlType;
|
||||
}
|
||||
|
||||
void PID_ATune::SetNoiseBand(double Band)
|
||||
{
|
||||
noiseBand = Band;
|
||||
}
|
||||
|
||||
double PID_ATune::GetNoiseBand()
|
||||
{
|
||||
return noiseBand;
|
||||
}
|
||||
|
||||
void PID_ATune::SetLookbackSec(int value)
|
||||
{
|
||||
if (value<1) value = 1;
|
||||
|
||||
if(value<25)
|
||||
{
|
||||
nLookBack = value * 4;
|
||||
sampleTime = 250;
|
||||
}
|
||||
else
|
||||
{
|
||||
nLookBack = 100;
|
||||
sampleTime = value*10;
|
||||
}
|
||||
}
|
||||
|
||||
int PID_ATune::GetLookbackSec()
|
||||
{
|
||||
return nLookBack * sampleTime / 1000;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef PID_AutoTune_v0
|
||||
#define PID_AutoTune_v0
|
||||
#define LIBRARY_VERSION 0.0.1
|
||||
|
||||
class PID_ATune
|
||||
{
|
||||
|
||||
|
||||
public:
|
||||
//commonly used functions **************************************************************************
|
||||
PID_ATune(double*, double*); // * Constructor. links the Autotune to a given PID
|
||||
int Runtime(); // * Similar to the PID Compue function, returns non 0 when done
|
||||
void Cancel(); // * Stops the AutoTune
|
||||
|
||||
void SetOutputStep(double); // * how far above and below the starting value will the output step?
|
||||
double GetOutputStep(); //
|
||||
|
||||
void SetControlType(int); // * Determies if the tuning parameters returned will be PI (D=0)
|
||||
int GetControlType(); // or PID. (0=PI, 1=PID)
|
||||
|
||||
void SetLookbackSec(int); // * how far back are we looking to identify peaks
|
||||
int GetLookbackSec(); //
|
||||
|
||||
void SetNoiseBand(double); // * the autotune will ignore signal chatter smaller than this value
|
||||
double GetNoiseBand(); // this should be acurately set
|
||||
|
||||
double GetKp(); // * once autotune is complete, these functions contain the
|
||||
double GetKi(); // computed tuning parameters.
|
||||
double GetKd(); //
|
||||
|
||||
private:
|
||||
void FinishUp();
|
||||
bool isMax, isMin;
|
||||
double *input, *output;
|
||||
double setpoint;
|
||||
double noiseBand;
|
||||
int controlType;
|
||||
bool running;
|
||||
unsigned long peak1, peak2, lastTime;
|
||||
int sampleTime;
|
||||
int nLookBack;
|
||||
int peakType;
|
||||
double lastInputs[101];
|
||||
double peaks[10];
|
||||
int peakCount;
|
||||
bool justchanged;
|
||||
bool justevaled;
|
||||
double absMax, absMin;
|
||||
double oStep;
|
||||
double outputStart;
|
||||
double Ku, Pu;
|
||||
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
/**********************************************************************************************
|
||||
* Arduino PID Library - Version 1
|
||||
* by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com
|
||||
*
|
||||
* This Code is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
|
||||
**********************************************************************************************/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "PID_v1.h"
|
||||
|
||||
/*Constructor (...)*********************************************************
|
||||
* The parameters specified here are those for for which we can't set up
|
||||
* reliable defaults, so we need to have the user set them.
|
||||
***************************************************************************/
|
||||
PID::PID(double* Input, double* Output, double* Setpoint,
|
||||
double Kp, double Ki, double Kd, int ControllerDirection)
|
||||
{
|
||||
PID::SetOutputLimits(0, 255); //default output limit corresponds to
|
||||
//the arduino pwm limits
|
||||
|
||||
SampleTime = 100; //default Controller Sample Time is 0.1 seconds
|
||||
|
||||
PID::SetControllerDirection(ControllerDirection);
|
||||
PID::SetTunings(Kp, Ki, Kd);
|
||||
|
||||
lastTime = millis()-SampleTime;
|
||||
inAuto = false;
|
||||
myOutput = Output;
|
||||
myInput = Input;
|
||||
mySetpoint = Setpoint;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* Compute() **********************************************************************
|
||||
* This, as they say, is where the magic happens. this function should be called
|
||||
* every time "void loop()" executes. the function will decide for itself whether a new
|
||||
* pid Output needs to be computed
|
||||
**********************************************************************************/
|
||||
void PID::Compute()
|
||||
{
|
||||
if(!inAuto) return;
|
||||
unsigned long now = millis();
|
||||
int timeChange = (now - lastTime);
|
||||
if(timeChange>=SampleTime)
|
||||
{
|
||||
/*Compute all the working error variables*/
|
||||
double input = *myInput;
|
||||
double error = *mySetpoint - input;
|
||||
ITerm+= (ki * error);
|
||||
if(ITerm > outMax) ITerm= outMax;
|
||||
else if(ITerm < outMin) ITerm= outMin;
|
||||
double dInput = (input - lastInput);
|
||||
|
||||
/*Compute PID Output*/
|
||||
double output = kp * error + ITerm- kd * dInput;
|
||||
|
||||
if(output > outMax) output = outMax;
|
||||
else if(output < outMin) output = outMin;
|
||||
*myOutput = output;
|
||||
|
||||
/*Remember some variables for next time*/
|
||||
lastInput = input;
|
||||
lastTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* SetTunings(...)*************************************************************
|
||||
* This function allows the controller's dynamic performance to be adjusted.
|
||||
* it's called automatically from the constructor, but tunings can also
|
||||
* be adjusted on the fly during normal operation
|
||||
******************************************************************************/
|
||||
void PID::SetTunings(double Kp, double Ki, double Kd)
|
||||
{
|
||||
if (Kp<0 || Ki<0 || Kd<0) return;
|
||||
|
||||
dispKp = Kp; dispKi = Ki; dispKd = Kd;
|
||||
|
||||
double SampleTimeInSec = ((double)SampleTime)/1000;
|
||||
kp = Kp;
|
||||
ki = Ki * SampleTimeInSec;
|
||||
kd = Kd / SampleTimeInSec;
|
||||
|
||||
if(controllerDirection ==REVERSE)
|
||||
{
|
||||
kp = (0 - kp);
|
||||
ki = (0 - ki);
|
||||
kd = (0 - kd);
|
||||
}
|
||||
}
|
||||
|
||||
/* SetSampleTime(...) *********************************************************
|
||||
* sets the period, in Milliseconds, at which the calculation is performed
|
||||
******************************************************************************/
|
||||
void PID::SetSampleTime(int NewSampleTime)
|
||||
{
|
||||
if (NewSampleTime > 0)
|
||||
{
|
||||
double ratio = (double)NewSampleTime
|
||||
/ (double)SampleTime;
|
||||
ki *= ratio;
|
||||
kd /= ratio;
|
||||
SampleTime = (unsigned long)NewSampleTime;
|
||||
}
|
||||
}
|
||||
|
||||
/* SetOutputLimits(...)****************************************************
|
||||
* This function will be used far more often than SetInputLimits. while
|
||||
* the input to the controller will generally be in the 0-1023 range (which is
|
||||
* the default already,) the output will be a little different. maybe they'll
|
||||
* be doing a time window and will need 0-8000 or something. or maybe they'll
|
||||
* want to clamp it from 0-125. who knows. at any rate, that can all be done
|
||||
* here.
|
||||
**************************************************************************/
|
||||
void PID::SetOutputLimits(double Min, double Max)
|
||||
{
|
||||
if(Min >= Max) return;
|
||||
outMin = Min;
|
||||
outMax = Max;
|
||||
|
||||
if(inAuto)
|
||||
{
|
||||
if(*myOutput > outMax) *myOutput = outMax;
|
||||
else if(*myOutput < outMin) *myOutput = outMin;
|
||||
|
||||
if(ITerm > outMax) ITerm= outMax;
|
||||
else if(ITerm < outMin) ITerm= outMin;
|
||||
}
|
||||
}
|
||||
|
||||
/* SetMode(...)****************************************************************
|
||||
* Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
|
||||
* when the transition from manual to auto occurs, the controller is
|
||||
* automatically initialized
|
||||
******************************************************************************/
|
||||
void PID::SetMode(int Mode)
|
||||
{
|
||||
bool newAuto = (Mode == AUTOMATIC);
|
||||
if(newAuto == !inAuto)
|
||||
{ /*we just went from manual to auto*/
|
||||
PID::Initialize();
|
||||
}
|
||||
inAuto = newAuto;
|
||||
}
|
||||
|
||||
/* Initialize()****************************************************************
|
||||
* does all the things that need to happen to ensure a bumpless transfer
|
||||
* from manual to automatic mode.
|
||||
******************************************************************************/
|
||||
void PID::Initialize()
|
||||
{
|
||||
ITerm = *myOutput;
|
||||
lastInput = *myInput;
|
||||
if(ITerm > outMax) ITerm = outMax;
|
||||
else if(ITerm < outMin) ITerm = outMin;
|
||||
}
|
||||
|
||||
/* SetControllerDirection(...)*************************************************
|
||||
* The PID will either be connected to a DIRECT acting process (+Output leads
|
||||
* to +Input) or a REVERSE acting process(+Output leads to -Input.) we need to
|
||||
* know which one, because otherwise we may increase the output when we should
|
||||
* be decreasing. This is called from the constructor.
|
||||
******************************************************************************/
|
||||
void PID::SetControllerDirection(int Direction)
|
||||
{
|
||||
if(inAuto && Direction !=controllerDirection)
|
||||
{
|
||||
kp = (0 - kp);
|
||||
ki = (0 - ki);
|
||||
kd = (0 - kd);
|
||||
}
|
||||
controllerDirection = Direction;
|
||||
}
|
||||
|
||||
/* Status Funcions*************************************************************
|
||||
* Just because you set the Kp=-1 doesn't mean it actually happened. these
|
||||
* functions query the internal state of the PID. they're here for display
|
||||
* purposes. this are the functions the PID Front-end uses for example
|
||||
******************************************************************************/
|
||||
double PID::GetKp(){ return dispKp; }
|
||||
double PID::GetKi(){ return dispKi;}
|
||||
double PID::GetKd(){ return dispKd;}
|
||||
int PID::GetMode(){ return inAuto ? AUTOMATIC : MANUAL;}
|
||||
int PID::GetDirection(){ return controllerDirection;}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
#ifndef PID_v1_h
|
||||
#define PID_v1_h
|
||||
#define LIBRARY_VERSION 1.0.0
|
||||
|
||||
class PID
|
||||
{
|
||||
|
||||
|
||||
public:
|
||||
|
||||
//Constants used in some of the functions below
|
||||
#define AUTOMATIC 1
|
||||
#define MANUAL 0
|
||||
#define DIRECT 0
|
||||
#define REVERSE 1
|
||||
|
||||
//commonly used functions **************************************************************************
|
||||
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
|
||||
double, double, double, int); // Setpoint. Initial tuning parameters are also set here
|
||||
|
||||
void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
|
||||
|
||||
void Compute(); // * performs the PID calculation. it should be
|
||||
// called every time loop() cycles. ON/OFF and
|
||||
// calculation frequency can be set using SetMode
|
||||
// SetSampleTime respectively
|
||||
|
||||
void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
|
||||
//it's likely the user will want to change this depending on
|
||||
//the application
|
||||
|
||||
|
||||
|
||||
//available but not commonly used functions ********************************************************
|
||||
void SetTunings(double, double, // * While most users will set the tunings once in the
|
||||
double); // constructor, this function gives the user the option
|
||||
// of changing tunings during runtime for Adaptive control
|
||||
void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
|
||||
// means the output will increase when error is positive. REVERSE
|
||||
// means the opposite. it's very unlikely that this will be needed
|
||||
// once it is set in the constructor.
|
||||
void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
|
||||
// the PID calculation is performed. default is 100
|
||||
|
||||
|
||||
|
||||
//Display functions ****************************************************************
|
||||
double GetKp(); // These functions query the pid for interal values.
|
||||
double GetKi(); // they were created mainly for the pid front-end,
|
||||
double GetKd(); // where it's important to know what is actually
|
||||
int GetMode(); // inside the PID.
|
||||
int GetDirection(); //
|
||||
|
||||
private:
|
||||
void Initialize();
|
||||
|
||||
double dispKp; // * we'll hold on to the tuning parameters in user-entered
|
||||
double dispKi; // format for display purposes
|
||||
double dispKd; //
|
||||
|
||||
double kp; // * (P)roportional Tuning Parameter
|
||||
double ki; // * (I)ntegral Tuning Parameter
|
||||
double kd; // * (D)erivative Tuning Parameter
|
||||
|
||||
int controllerDirection;
|
||||
|
||||
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
|
||||
double *myOutput; // This creates a hard link between the variables and the
|
||||
double *mySetpoint; // PID, freeing the user from having to constantly tell us
|
||||
// what these values are. with pointers we'll just know.
|
||||
|
||||
unsigned long lastTime;
|
||||
double ITerm, lastInput;
|
||||
|
||||
int SampleTime;
|
||||
double outMin, outMax;
|
||||
bool inAuto;
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
#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);
|
||||
|
||||
//set to false to connect to the real world
|
||||
boolean useSimulation = true;
|
||||
|
||||
void setup()
|
||||
{
|
||||
if(useSimulation)
|
||||
{
|
||||
for(byte i=0;i<50;i++)
|
||||
{
|
||||
theta[i]=outputStart;
|
||||
}
|
||||
modelTime = 0;
|
||||
}
|
||||
//Setup the pid
|
||||
myPID.SetMode(AUTOMATIC);
|
||||
|
||||
if(tuning)
|
||||
{
|
||||
tuning=false;
|
||||
changeAutoTune();
|
||||
tuning=true;
|
||||
}
|
||||
|
||||
serialTime = 0;
|
||||
Serial.begin(9600);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
unsigned long now = millis();
|
||||
|
||||
if(!useSimulation)
|
||||
{ //pull the input in from the real world
|
||||
input = analogRead(0);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if(useSimulation)
|
||||
{
|
||||
theta[30]=output;
|
||||
if(now>=modelTime)
|
||||
{
|
||||
modelTime +=100;
|
||||
DoModel();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
analogWrite(0,output);
|
||||
}
|
||||
|
||||
//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;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include <PID_AutoTune_v0.h>
|
||||
|
||||
|
||||
PID_ATune::PID_ATune(double* Input, double* Output)
|
||||
{
|
||||
input = Input;
|
||||
output = Output;
|
||||
controlType =0 ; //default to PI
|
||||
noiseBand = 0.5;
|
||||
running = false;
|
||||
oStep = 30;
|
||||
SetLookbackSec(10);
|
||||
lastTime = millis();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PID_ATune::Cancel()
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
|
||||
int PID_ATune::Runtime()
|
||||
{
|
||||
justevaled=false;
|
||||
if(peakCount>9 && running)
|
||||
{
|
||||
running = false;
|
||||
FinishUp();
|
||||
return 1;
|
||||
}
|
||||
unsigned long now = millis();
|
||||
|
||||
if((now-lastTime)<sampleTime) return false;
|
||||
lastTime = now;
|
||||
double refVal = *input;
|
||||
justevaled=true;
|
||||
if(!running)
|
||||
{ //initialize working variables the first time around
|
||||
peakType = 0;
|
||||
peakCount=0;
|
||||
justchanged=false;
|
||||
absMax=refVal;
|
||||
absMin=refVal;
|
||||
setpoint = refVal;
|
||||
running = true;
|
||||
outputStart = *output;
|
||||
*output = outputStart+oStep;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(refVal>absMax)absMax=refVal;
|
||||
if(refVal<absMin)absMin=refVal;
|
||||
}
|
||||
|
||||
//oscillate the output base on the input's relation to the setpoint
|
||||
|
||||
if(refVal>setpoint+noiseBand) *output = outputStart-oStep;
|
||||
else if (refVal<setpoint-noiseBand) *output = outputStart+oStep;
|
||||
|
||||
|
||||
//bool isMax=true, isMin=true;
|
||||
isMax=true;isMin=true;
|
||||
//id peaks
|
||||
for(int i=nLookBack-1;i>=0;i--)
|
||||
{
|
||||
double val = lastInputs[i];
|
||||
if(isMax) isMax = refVal>val;
|
||||
if(isMin) isMin = refVal<val;
|
||||
lastInputs[i+1] = lastInputs[i];
|
||||
}
|
||||
lastInputs[0] = refVal;
|
||||
if(nLookBack<9)
|
||||
{ //we don't want to trust the maxes or mins until the inputs array has been filled
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(isMax)
|
||||
{
|
||||
if(peakType==0)peakType=1;
|
||||
if(peakType==-1)
|
||||
{
|
||||
peakType = 1;
|
||||
justchanged=true;
|
||||
peak2 = peak1;
|
||||
}
|
||||
peak1 = now;
|
||||
peaks[peakCount] = refVal;
|
||||
|
||||
}
|
||||
else if(isMin)
|
||||
{
|
||||
if(peakType==0)peakType=-1;
|
||||
if(peakType==1)
|
||||
{
|
||||
peakType=-1;
|
||||
peakCount++;
|
||||
justchanged=true;
|
||||
}
|
||||
|
||||
if(peakCount<10)peaks[peakCount] = refVal;
|
||||
}
|
||||
|
||||
if(justchanged && peakCount>2)
|
||||
{ //we've transitioned. check if we can autotune based on the last peaks
|
||||
double avgSeparation = (abs(peaks[peakCount-1]-peaks[peakCount-2])+abs(peaks[peakCount-2]-peaks[peakCount-3]))/2;
|
||||
if( avgSeparation < 0.05*(absMax-absMin))
|
||||
{
|
||||
FinishUp();
|
||||
running = false;
|
||||
return 1;
|
||||
|
||||
}
|
||||
}
|
||||
justchanged=false;
|
||||
return 0;
|
||||
}
|
||||
void PID_ATune::FinishUp()
|
||||
{
|
||||
*output = outputStart;
|
||||
//we can generate tuning parameters!
|
||||
Ku = 4*(2*oStep)/((absMax-absMin)*3.14159);
|
||||
Pu = (double)(peak1-peak2) / 1000;
|
||||
}
|
||||
|
||||
double PID_ATune::GetKp()
|
||||
{
|
||||
return controlType==1 ? 0.6 * Ku : 0.4 * Ku;
|
||||
}
|
||||
|
||||
double PID_ATune::GetKi()
|
||||
{
|
||||
return controlType==1? 1.2*Ku / Pu : 0.48 * Ku / Pu; // Ki = Kc/Ti
|
||||
}
|
||||
|
||||
double PID_ATune::GetKd()
|
||||
{
|
||||
return controlType==1? 0.075 * Ku * Pu : 0; //Kd = Kc * Td
|
||||
}
|
||||
|
||||
void PID_ATune::SetOutputStep(double Step)
|
||||
{
|
||||
oStep = Step;
|
||||
}
|
||||
|
||||
double PID_ATune::GetOutputStep()
|
||||
{
|
||||
return oStep;
|
||||
}
|
||||
|
||||
void PID_ATune::SetControlType(int Type) //0=PI, 1=PID
|
||||
{
|
||||
controlType = Type;
|
||||
}
|
||||
int PID_ATune::GetControlType()
|
||||
{
|
||||
return controlType;
|
||||
}
|
||||
|
||||
void PID_ATune::SetNoiseBand(double Band)
|
||||
{
|
||||
noiseBand = Band;
|
||||
}
|
||||
|
||||
double PID_ATune::GetNoiseBand()
|
||||
{
|
||||
return noiseBand;
|
||||
}
|
||||
|
||||
void PID_ATune::SetLookbackSec(int value)
|
||||
{
|
||||
if (value<1) value = 1;
|
||||
|
||||
if(value<25)
|
||||
{
|
||||
nLookBack = value * 4;
|
||||
sampleTime = 250;
|
||||
}
|
||||
else
|
||||
{
|
||||
nLookBack = 100;
|
||||
sampleTime = value*10;
|
||||
}
|
||||
}
|
||||
|
||||
int PID_ATune::GetLookbackSec()
|
||||
{
|
||||
return nLookBack * sampleTime / 1000;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef PID_AutoTune_v0
|
||||
#define PID_AutoTune_v0
|
||||
#define LIBRARY_VERSION 0.0.1
|
||||
|
||||
class PID_ATune
|
||||
{
|
||||
|
||||
|
||||
public:
|
||||
//commonly used functions **************************************************************************
|
||||
PID_ATune(double*, double*); // * Constructor. links the Autotune to a given PID
|
||||
int Runtime(); // * Similar to the PID Compue function, returns non 0 when done
|
||||
void Cancel(); // * Stops the AutoTune
|
||||
|
||||
void SetOutputStep(double); // * how far above and below the starting value will the output step?
|
||||
double GetOutputStep(); //
|
||||
|
||||
void SetControlType(int); // * Determies if the tuning parameters returned will be PI (D=0)
|
||||
int GetControlType(); // or PID. (0=PI, 1=PID)
|
||||
|
||||
void SetLookbackSec(int); // * how far back are we looking to identify peaks
|
||||
int GetLookbackSec(); //
|
||||
|
||||
void SetNoiseBand(double); // * the autotune will ignore signal chatter smaller than this value
|
||||
double GetNoiseBand(); // this should be acurately set
|
||||
|
||||
double GetKp(); // * once autotune is complete, these functions contain the
|
||||
double GetKi(); // computed tuning parameters.
|
||||
double GetKd(); //
|
||||
|
||||
private:
|
||||
void FinishUp();
|
||||
bool isMax, isMin;
|
||||
double *input, *output;
|
||||
double setpoint;
|
||||
double noiseBand;
|
||||
int controlType;
|
||||
bool running;
|
||||
unsigned long peak1, peak2, lastTime;
|
||||
int sampleTime;
|
||||
int nLookBack;
|
||||
int peakType;
|
||||
double lastInputs[101];
|
||||
double peaks[10];
|
||||
int peakCount;
|
||||
bool justchanged;
|
||||
bool justevaled;
|
||||
double absMax, absMin;
|
||||
double oStep;
|
||||
double outputStart;
|
||||
double Ku, Pu;
|
||||
|
||||
};
|
||||
#endif
|
||||
|
||||
37
Moteur DC/Arduino-PID-AutoTune-Library/README.txt
Normal file
37
Moteur DC/Arduino-PID-AutoTune-Library/README.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
/**********************************************************************************************
|
||||
* Arduino PID AutoTune Library - Version 0.0.1
|
||||
* by Brett Beauregard <br3ttb@gmail.com> brettbeauregard.com
|
||||
*
|
||||
* This Library is ported from the AutotunerPID Toolkit by William Spinelli
|
||||
* (http://www.mathworks.com/matlabcentral/fileexchange/4652)
|
||||
* Copyright (c) 2004
|
||||
*
|
||||
* This Library is licensed under the BSD License:
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the distribution
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
**********************************************************************************************/
|
||||
|
||||
Note: I'd really hoped to have this more polished before release, but with the
|
||||
osPID coming out I felt that this needed to be out there NOW. if you
|
||||
encounter any issues please contact me, or post to the diy-pid-control
|
||||
google group.
|
||||
|
||||
Reference in New Issue
Block a user