-
Notifications
You must be signed in to change notification settings - Fork 18
/
sTune_QuickPID.ino
73 lines (64 loc) · 2.51 KB
/
sTune_QuickPID.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/***************************************************************************
sTune QuickPID Example (MAX6675, PTC Heater / SSR / Software PWM)
This sketch does on-the-fly tuning and PID control. Tuning parameters are
quickly determined and applied during temperature ramp-up to setpoint.
View results using serial plotter.
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX6675_PTC_SSR
***************************************************************************/
#include <max6675.h>
#include <sTune.h>
#include <QuickPID.h>
// pins
const uint8_t inputPin = 0;
const uint8_t relayPin = 3;
const uint8_t SO = 12;
const uint8_t CS = 10;
const uint8_t sck = 13;
// user settings
uint32_t settleTimeSec = 10;
uint32_t testTimeSec = 500; // runPid interval = testTimeSec / samples
const uint16_t samples = 500;
const float inputSpan = 200;
const float outputSpan = 1000;
float outputStart = 0;
float outputStep = 50;
float tempLimit = 150;
uint8_t debounce = 1;
// variables
float Input, Output, Setpoint = 80, Kp, Ki, Kd;
MAX6675 module(sck, CS, SO); //SPI
sTune tuner = sTune(&Input, &Output, tuner.ZN_PID, tuner.directIP, tuner.printOFF);
QuickPID myPID(&Input, &Output, &Setpoint);
void setup() {
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
delay(3000);
Output = 0;
tuner.Configure(inputSpan, outputSpan, outputStart, outputStep, testTimeSec, settleTimeSec, samples);
tuner.SetEmergencyStop(tempLimit);
}
void loop() {
float optimumOutput = tuner.softPwm(relayPin, Input, Output, Setpoint, outputSpan, debounce);
switch (tuner.Run()) {
case tuner.sample: // active once per sample during test
Input = module.readCelsius();
tuner.plotter(Input, Output, Setpoint, 0.5f, 3); // output scale 0.5, plot every 3rd sample
break;
case tuner.tunings: // active just once when sTune is done
tuner.GetAutoTunings(&Kp, &Ki, &Kd); // sketch variables updated by sTune
myPID.SetOutputLimits(0, outputSpan * 0.1);
myPID.SetSampleTimeUs((outputSpan - 1) * 1000);
debounce = 0; // ssr mode
Output = outputStep;
myPID.SetMode(myPID.Control::automatic); // the PID is turned on
myPID.SetProportionalMode(myPID.pMode::pOnMeas);
myPID.SetAntiWindupMode(myPID.iAwMode::iAwClamp);
myPID.SetTunings(Kp, Ki, Kd); // update PID with the new tunings
break;
case tuner.runPid: // active once per sample after tunings
Input = module.readCelsius();
myPID.Compute();
tuner.plotter(Input, optimumOutput, Setpoint, 0.5f, 3);
break;
}
}