pid auto-tune
This commit is contained in:
parent
a27b9c0333
commit
e5bb803869
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* pid_auto_tune.cpp
|
||||
*
|
||||
* Created on: Sep 13, 2017
|
||||
* @author Andrey Belomutskiy, (c) 2012-2017
|
||||
*/
|
||||
|
||||
#include "pid_auto_tune.h"
|
||||
|
||||
PID_AutoTune::PID_AutoTune() {
|
||||
running = false;
|
||||
nLookBack = 50;
|
||||
oStep = 10;
|
||||
noiseBand = 0.5;
|
||||
}
|
||||
|
||||
int PID_AutoTune::Runtime() {
|
||||
justevaled = false;
|
||||
if (peakCount > 9 && running) {
|
||||
running = false;
|
||||
FinishUp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
justevaled = true;
|
||||
if (!running) {
|
||||
//initialize working variables the first time around
|
||||
peakType = 0;
|
||||
peakCount = 0;
|
||||
justchanged = false;
|
||||
absMax = input;
|
||||
absMin = input;
|
||||
setpoint = input;
|
||||
running = true;
|
||||
outputStart = output;
|
||||
output = outputStart + oStep;
|
||||
|
||||
for (int i = nLookBack - 1; i >= 0; i--) {
|
||||
// default values for history array
|
||||
lastInputs[i] = input;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (input > absMax)
|
||||
absMax = input;
|
||||
if (input < absMin)
|
||||
absMin = input;
|
||||
}
|
||||
|
||||
//oscillate the output base on the input's relation to the setpoint
|
||||
|
||||
if (input > setpoint + noiseBand)
|
||||
output = outputStart - oStep;
|
||||
else if (input < setpoint - noiseBand)
|
||||
output = outputStart + oStep;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* pid_auto_tune.h
|
||||
*
|
||||
* http://brettbeauregard.com/blog/2012/01/arduino-pid-autotune-library/
|
||||
*
|
||||
*
|
||||
* Created on: Sep 13, 2017
|
||||
* @author Andrey Belomutskiy, (c) 2012-2017
|
||||
*/
|
||||
|
||||
#ifndef CONTROLLERS_MATH_PID_AUTO_TUNE_H_
|
||||
#define CONTROLLERS_MATH_PID_AUTO_TUNE_H_
|
||||
|
||||
class PID_AutoTune {
|
||||
public:
|
||||
PID_AutoTune();
|
||||
void reset();
|
||||
void FinishUp();
|
||||
int Runtime();
|
||||
bool isMax, isMin;
|
||||
/**
|
||||
* sensor position
|
||||
*/
|
||||
float input;
|
||||
/**
|
||||
* actuator duty cycle
|
||||
*/
|
||||
float output;
|
||||
/**
|
||||
* trigger line
|
||||
*/
|
||||
float setpoint;
|
||||
float noiseBand;
|
||||
//int controlType = 1;
|
||||
bool running;
|
||||
unsigned int peak1, peak2, lastTime;
|
||||
//int sampleTime;
|
||||
int nLookBack;
|
||||
int peakType; // todo: convert to enum
|
||||
float lastInputs[101];
|
||||
float peaks[10];
|
||||
int peakCount;
|
||||
bool justchanged;
|
||||
bool justevaled;
|
||||
float absMax, absMin;
|
||||
float oStep;
|
||||
float outputStart;
|
||||
float Ku, Pu;
|
||||
};
|
||||
|
||||
#endif /* CONTROLLERS_MATH_PID_AUTO_TUNE_H_ */
|
Loading…
Reference in New Issue