Skip to content

Commit b0f9832

Browse files
committed
Update
- the faster IP mode tests now work for PTC type heaters - various other fixes - updated examples - updated documentation
1 parent 0f8d8e1 commit b0f9832

File tree

19 files changed

+398
-344
lines changed

19 files changed

+398
-344
lines changed

examples/MAX31856_PTC_SSR/Get_All_Tunings/Get_All_Tunings.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*****************************************************************************
2-
sTune Get All Tunings Example (MAX31856, PTC Heater and SSR)
2+
sTune Get All Tunings Example (MAX31856, PTC Heater / SSR / Software PWM)
3+
This runs a fast inflection point test to determine tuning parameters.
4+
Open serial printer to view test progress and results.
35
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX31856_PTC_SSR
46
****************************************************************************/
57
#include <Adafruit_MAX31856.h>
@@ -12,13 +14,13 @@ const uint8_t drdyPin = 5;
1214

1315
// user settings
1416
uint32_t settleTimeSec = 10;
15-
uint32_t testTimeSec = 500;
17+
uint32_t testTimeSec = 500; // sample interval = testTimeSec / samples
1618
const uint16_t samples = 500;
17-
const float inputSpan = 220;
19+
const float inputSpan = 200;
1820
const float outputSpan = 1000;
1921
float outputStart = 0;
2022
float outputStep = 50;
21-
float tempLimit = 100;
23+
float tempLimit = 150;
2224

2325
// variables
2426
float Input, Output;
@@ -30,7 +32,6 @@ void setup() {
3032
pinMode(drdyPin, INPUT);
3133
pinMode(relayPin, OUTPUT);
3234
Serial.begin(115200);
33-
while (!Serial) delay(10);
3435
delay(3000);
3536
Output = 0;
3637
if (!maxthermo.begin()) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/***************************************************************************
2+
QuickPID Basic Example (MAX31856, PTC Heater / SSR / Software PWM)
3+
Update your user settings and tuning parameters, then run and view results
4+
using serial plotter.
5+
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX31856_PTC_SSR
6+
***************************************************************************/
7+
#include <Adafruit_MAX31856.h>
8+
#include <sTune.h>
9+
#include <QuickPID.h>
10+
11+
// pins
12+
const uint8_t inputPin = 0;
13+
const uint8_t relayPin = 3;
14+
const uint8_t drdyPin = 5;
15+
16+
uint32_t testTimeSec = 1; // runPid interval = testTimeSec / samples
17+
const uint16_t samples = 1;
18+
const float outputSpan = 1000; // window size
19+
float tempLimit = 150;
20+
21+
// variables
22+
float Input, Output, Setpoint = 80, Kp = 2.00, Ki = 0.02, Kd = 0.06;
23+
24+
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10); //SPI
25+
sTune tuner = sTune(); // for softPWM and tempLimit
26+
QuickPID myPID(&Input, &Output, &Setpoint);
27+
28+
void setup() {
29+
pinMode(relayPin, OUTPUT);
30+
Serial.begin(115200);
31+
delay(3000);
32+
if (!maxthermo.begin()) {
33+
Serial.println("Could not initialize thermocouple.");
34+
while (1) delay(10);
35+
}
36+
maxthermo.setThermocoupleType(MAX31856_TCTYPE_K);
37+
maxthermo.setConversionMode(MAX31856_CONTINUOUS);
38+
tuner.Configure(0, 0, 0, 0, testTimeSec, 0, samples);
39+
tuner.SetEmergencyStop(tempLimit);
40+
myPID.SetOutputLimits(0, outputSpan * 0.1);
41+
myPID.SetSampleTimeUs((outputSpan - 1) * 1000);
42+
myPID.SetMode(myPID.Control::automatic); // the PID is turned on
43+
myPID.SetProportionalMode(myPID.pMode::pOnMeas);
44+
myPID.SetAntiWindupMode(myPID.iAwMode::iAwClamp);
45+
myPID.SetTunings(Kp, Ki, Kd); // set PID gains
46+
}
47+
48+
void loop() {
49+
float optimumOutput = tuner.softPwm(relayPin, Input, Output, Setpoint, outputSpan, 0); // ssr mode
50+
if (myPID.Compute()) {
51+
if (!digitalRead(drdyPin)) Input = maxthermo.readThermocoupleTemperature();
52+
tuner.plotter(Input, optimumOutput, Setpoint, 0.5f, 3); // output scale 0.5, plot every 3rd sample
53+
}
54+
}
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/****************************************************************************
2-
sTune PID_v1 Example
3-
This sketch does on-the-fly tunning and PID SSR control of a
4-
PTC heater. Tunning parameters are quickly determined and
5-
applied during the temperature ramp-up to setpoint. Open
6-
the serial plotter to view the graphical results.
2+
sTune PID_v1 Example (MAX31856, PTC Heater / SSR / Software PWM)
3+
This sketch does on-the-fly tuning and PID control. Tuning parameters are
4+
quickly determined and applied during temperature ramp-up to setpoint.
5+
View results using serial plotter.
76
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX31856_PTC_SSR
87
****************************************************************************/
9-
108
#include <Adafruit_MAX31856.h>
119
#include <sTune.h>
1210
#include <PID_v1.h>
@@ -18,18 +16,18 @@ const uint8_t drdyPin = 5;
1816

1917
// user settings
2018
uint32_t settleTimeSec = 10;
21-
uint32_t testTimeSec = 500;
19+
uint32_t testTimeSec = 500; // runPid interval = testTimeSec / samples
2220
const uint16_t samples = 500;
2321
const float inputSpan = 200;
2422
const float outputSpan = 1000;
2523
float outputStart = 0;
26-
float outputStep = 30;
27-
float tempLimit = 75;
24+
float outputStep = 50;
25+
float tempLimit = 150;
2826
uint8_t debounce = 1;
2927

3028
// variables
31-
double input, output, setpoint = 50, kp, ki, kd; // PID_v1
32-
float Input, Output, Setpoint = 50, Kp, Ki, Kd; // sTune
29+
double input, output, setpoint = 80, kp, ki, kd; // PID_v1
30+
float Input, Output, Setpoint = 80, Kp, Ki, Kd; // sTune
3331

3432
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10); //SPI
3533
sTune tuner = sTune(&Input, &Output, tuner.ZN_PID, tuner.directIP, tuner.printOFF);
@@ -39,7 +37,6 @@ void setup() {
3937
pinMode(drdyPin, INPUT);
4038
pinMode(relayPin, OUTPUT);
4139
Serial.begin(115200);
42-
while (!Serial) delay(10);
4340
delay(3000);
4441
if (!maxthermo.begin()) {
4542
Serial.println("Could not initialize thermocouple.");
@@ -57,18 +54,17 @@ void loop() {
5754
switch (tuner.Run()) {
5855
case tuner.sample: // active once per sample during test
5956
if (!digitalRead(drdyPin)) Input = maxthermo.readThermocoupleTemperature();
60-
tuner.plotter(Input, Output, Setpoint, 1, 3);
57+
tuner.plotter(Input, Output, Setpoint, 0.5f, 3); // output scale 0.5, plot every 3rd sample
6158
break;
6259

6360
case tuner.tunings: // active just once when sTune is done
6461
tuner.GetAutoTunings(&Kp, &Ki, &Kd); // sketch variables updated by sTune
6562
myPID.SetOutputLimits(0, outputSpan * 0.1);
66-
myPID.SetSampleTime(outputSpan * 0.4);
67-
debounce = 0; // switch to SSR optimum cycle mode
63+
myPID.SetSampleTime(outputSpan - 1);
64+
debounce = 0; // ssr mode
6865
setpoint = Setpoint, output = outputStep, kp = Kp, ki = Ki, kd = Kd;
66+
Output = outputStep;
6967
myPID.SetMode(AUTOMATIC); // the PID is turned on
70-
output = outputStep;
71-
Output = output;
7268
myPID.SetTunings(kp, ki, kd); // update PID with the new tunings
7369
break;
7470

@@ -77,7 +73,7 @@ void loop() {
7773
input = Input;
7874
myPID.Compute();
7975
Output = output;
80-
tuner.plotter(Input, optimumOutput, Setpoint, 1, 3);
76+
tuner.plotter(Input, optimumOutput, Setpoint, 0.5f, 3);
8177
break;
8278
}
8379
}

examples/MAX31856_PTC_SSR/sTune_QuickPID/sTune_QuickPID.ino

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/***************************************************************************
2-
sTune QuickPID Example
3-
This sketch does on-the-fly tunning and PID SSR control of a
4-
PTC heater. Tunning parameters are quickly determined and
5-
applied during the temperature ramp-up to setpoint. Open
6-
the serial plotter to view the graphical results.
2+
sTune QuickPID Example (MAX31856, PTC Heater / SSR / Software PWM)
3+
This sketch does on-the-fly tuning and PID control. Tuning parameters are
4+
quickly determined and applied during temperature ramp-up to setpoint.
5+
View results using serial plotter.
76
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX31856_PTC_SSR
87
***************************************************************************/
9-
108
#include <Adafruit_MAX31856.h>
119
#include <sTune.h>
1210
#include <QuickPID.h>
@@ -18,17 +16,17 @@ const uint8_t drdyPin = 5;
1816

1917
// user settings
2018
uint32_t settleTimeSec = 10;
21-
uint32_t testTimeSec = 500;
19+
uint32_t testTimeSec = 500; // runPid interval = testTimeSec / samples
2220
const uint16_t samples = 500;
2321
const float inputSpan = 200;
2422
const float outputSpan = 1000;
2523
float outputStart = 0;
26-
float outputStep = 30;
27-
float tempLimit = 75;
24+
float outputStep = 50;
25+
float tempLimit = 150;
2826
uint8_t debounce = 1;
2927

3028
// variables
31-
float Input, Output, Setpoint = 50, Kp, Ki, Kd;
29+
float Input, Output, Setpoint = 80, Kp, Ki, Kd;
3230

3331
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10); //SPI
3432
sTune tuner = sTune(&Input, &Output, tuner.ZN_PID, tuner.directIP, tuner.printOFF);
@@ -40,6 +38,7 @@ void setup() {
4038
Serial.begin(115200);
4139
while (!Serial) delay(10);
4240
delay(3000);
41+
Output = 0;
4342
if (!maxthermo.begin()) {
4443
Serial.println("Could not initialize thermocouple.");
4544
while (1) delay(10);
@@ -56,14 +55,15 @@ void loop() {
5655
switch (tuner.Run()) {
5756
case tuner.sample: // active once per sample during test
5857
if (!digitalRead(drdyPin)) Input = maxthermo.readThermocoupleTemperature();
59-
tuner.plotter(Input, Output, Setpoint, 1, 3);
58+
tuner.plotter(Input, Output, Setpoint, 0.5f, 3); // output scale 0.5, plot every 3rd sample
6059
break;
6160

6261
case tuner.tunings: // active just once when sTune is done
6362
tuner.GetAutoTunings(&Kp, &Ki, &Kd); // sketch variables updated by sTune
6463
myPID.SetOutputLimits(0, outputSpan * 0.1);
65-
myPID.SetSampleTimeUs(outputSpan * 1000 * 0.4);
66-
debounce = 0; // switch to SSR optimum cycle mode
64+
myPID.SetSampleTimeUs((outputSpan - 1) * 1000);
65+
debounce = 0; // ssr mode
66+
Output = outputStep;
6767
myPID.SetMode(myPID.Control::automatic); // the PID is turned on
6868
myPID.SetProportionalMode(myPID.pMode::pOnMeas);
6969
myPID.SetAntiWindupMode(myPID.iAwMode::iAwClamp);
@@ -73,7 +73,7 @@ void loop() {
7373
case tuner.runPid: // active once per sample after tunings
7474
if (!digitalRead(drdyPin)) Input = maxthermo.readThermocoupleTemperature();
7575
myPID.Compute();
76-
tuner.plotter(Input, optimumOutput, Setpoint, 1, 3);
76+
tuner.plotter(Input, optimumOutput, Setpoint, 0.5f, 3);
7777
break;
7878
}
7979
}

examples/MAX31856_PTC_SSR/sTune_QuickPID_Adaptive/sTune_QuickPID_Adaptive.ino

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)