Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
New examples for MAX6675 and PTC heater
  • Loading branch information
Dlloydev committed Feb 3, 2022
1 parent 3f5dc0c commit d26fd3d
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Accurate determination of the inflection point was given high priority for this

#### S-shaped Step Response

![Reaction Curve](https://user-images.githubusercontent.com/63488701/151797252-63b6c4d1-2781-459a-81f8-22f931a4a96b.png)
![Reaction Curve](https://user-images.githubusercontent.com/63488701/151890696-d574d77b-b849-4079-81e2-71e4ee416fa3.png)

#### Configuration

Expand Down
5 changes: 3 additions & 2 deletions examples/MAX31856_PTC_SSR/Get_All_Tunings/Get_All_Tunings.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/***************************************************************
/*****************************************************************************
sTune Get All Tunings Example (MAX31856, PTC Heater and SSR)
***************************************************************/
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX31856_PTC_SSR
****************************************************************************/
#include <Adafruit_MAX31856.h>
#include <sTune.h>

Expand Down
16 changes: 10 additions & 6 deletions examples/MAX31856_PTC_SSR/sTune_PID_v1/sTune_PID_v1.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/***************************************************************
/****************************************************************************
sTune PID_v1 Example
This sketch does on-the-fly tunning and PID SSR control of a
PTC heater. Tunning parameters are quickly determined and
applied during the temperature ramp-up to setpoint. Open
the serial plotter to view the graphical results.
***************************************************************/
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX31856_PTC_SSR
****************************************************************************/

#include <Adafruit_MAX31856.h>
#include <sTune.h>
Expand Down Expand Up @@ -32,7 +33,7 @@ float Input, Output, Setpoint = 50, Kp, Ki, Kd; // sTune

Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10); //SPI
sTune tuner = sTune(&Input, &Output, tuner.ZN_PID, tuner.directIP, tuner.printOFF);
PID myPID(&input, &output, &setpoint, 0, 0, 0, P_ON_M, DIRECT);
PID myPID(&input, &output, &setpoint, kp, ki, kd, P_ON_M, DIRECT);

void setup() {
pinMode(drdyPin, INPUT);
Expand Down Expand Up @@ -64,15 +65,18 @@ void loop() {
myPID.SetOutputLimits(0, outputSpan * 0.1);
myPID.SetSampleTime(outputSpan * 0.4);
debounce = 0; // switch to SSR optimum cycle mode
output = outputStep, kp = Kp, ki = Ki, kd = Kd;
setpoint = Setpoint, output = outputStep, kp = Kp, ki = Ki, kd = Kd;
myPID.SetMode(AUTOMATIC); // the PID is turned on
output = outputStep;
Output = output;
myPID.SetTunings(kp, ki, kd); // update PID with the new tunings
break;

case tuner.runPid: // active once per sample after tunings
if (!digitalRead(drdyPin)) input = maxthermo.readThermocoupleTemperature();
if (!digitalRead(drdyPin)) Input = maxthermo.readThermocoupleTemperature();
input = Input;
myPID.Compute();
Input = input, Output = output;
Output = output;
tuner.plotter(Input, optimumOutput, Setpoint, 1, 3);
break;
}
Expand Down
5 changes: 3 additions & 2 deletions examples/MAX31856_PTC_SSR/sTune_QuickPID/sTune_QuickPID.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**************************************************************
/***************************************************************************
sTune QuickPID Example
This sketch does on-the-fly tunning and PID SSR control of a
PTC heater. Tunning parameters are quickly determined and
applied during the temperature ramp-up to setpoint. Open
the serial plotter to view the graphical results.
*************************************************************/
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX31856_PTC_SSR
***************************************************************************/

#include <Adafruit_MAX31856.h>
#include <sTune.h>
Expand Down
74 changes: 74 additions & 0 deletions examples/MAX6675_PTC_SSR/Get_All_Tunings/Get_All_Tunings.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/***************************************************************************
sTune Get All Tunings Example (MAX6675, PTC Heater and SSR)
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX6675_PTC_SSR
***************************************************************************/

#include <max6675.h>
#include <sTune.h>

// pins
const uint8_t inputPin = 0;
const uint8_t relayPin = 3;
const uint8_t drdyPin = 5;
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;
const uint16_t samples = 500;
const float inputSpan = 220;
const float outputSpan = 1000;
float outputStart = 0;
float outputStep = 50;
float tempLimit = 100;

// variables
float Input, Output;

MAX6675 module(sck, CS, SO); //SPI
sTune tuner = sTune(&Input, &Output, tuner.ZN_PID, tuner.directIP, tuner.printALL);

void setup() {
pinMode(drdyPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
while (!Serial) delay(10);
delay(3000);
Output = 0;
tuner.Configure(inputSpan, outputSpan, outputStart, outputStep, testTimeSec, settleTimeSec, samples);
tuner.SetEmergencyStop(tempLimit);
}

void loop() {
tuner.softPwm(relayPin, Input, Output, 0, outputSpan, 1);

switch (tuner.Run()) {
case tuner.sample: // active once per sample during test
Input = module.readCelsius();
break;

case tuner.tunings: // active just once when sTune is done
Output = 0;
tuner.SetTuningMethod(tuner.TuningMethod::DampedOsc_PID);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::NoOvershoot_PID);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::CohenCoon_PID);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::Mixed_PID);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::ZN_PI);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::DampedOsc_PI);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::NoOvershoot_PI);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::CohenCoon_PI);
tuner.printTunings();
tuner.SetTuningMethod(tuner.TuningMethod::Mixed_PI);
tuner.printTunings();
break;
}
}
2 changes: 0 additions & 2 deletions examples/MAX6675_PTC_SSR/README.txt

This file was deleted.

80 changes: 80 additions & 0 deletions examples/MAX6675_PTC_SSR/sTune_PID_v1/sTune_PID_v1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/***************************************************************************
sTune PID_v1 Example
This sketch does on-the-fly tunning and PID SSR control of a
PTC heater. Tunning parameters are quickly determined and
applied during the temperature ramp-up to setpoint. Open
Reference: https://github.com/Dlloydev/sTune/wiki/Examples_MAX6675_PTC_SSR
***************************************************************************/

#include <max6675.h>
#include <sTune.h>
#include <PID_v1.h>

// pins
const uint8_t inputPin = 0;
const uint8_t relayPin = 3;
const uint8_t drdyPin = 5;
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;
const uint16_t samples = 500;
const float inputSpan = 200;
const float outputSpan = 1000;
float outputStart = 0;
float outputStep = 30;
float tempLimit = 75;
uint8_t debounce = 1;

// variables
double input, output, setpoint = 50, kp, ki, kd; // PID_v1
float Input, Output, Setpoint = 50, Kp, Ki, Kd; // sTune

MAX6675 module(sck, CS, SO); //SPI
sTune tuner = sTune(&Input, &Output, tuner.ZN_PID, tuner.directIP, tuner.printOFF);
PID myPID(&input, &output, &setpoint, kp, ki, kd, P_ON_M, DIRECT);

void setup() {
pinMode(drdyPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
while (!Serial) delay(10);
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, 1, 3);
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.SetSampleTime(outputSpan * 0.2);
debounce = 0; // switch to SSR optimum cycle mode
setpoint = Setpoint, output = outputStep, kp = Kp, ki = Ki, kd = Kd;
myPID.SetMode(AUTOMATIC); // the PID is turned on
output = outputStep;
Output = output;
myPID.SetTunings(kp, ki, kd); // update PID with the new tunings
break;

case tuner.runPid: // active once per sample after tunings
Input = module.readCelsius();
input = Input;
myPID.Compute();
Output = output;
tuner.plotter(Input, optimumOutput, Setpoint, 1, 3);
break;
}
}
77 changes: 77 additions & 0 deletions examples/MAX6675_PTC_SSR/sTune_QuickPID/sTune_QuickPID.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/***************************************************************************
sTune QuickPID Example
This sketch does on-the-fly tunning and PID SSR control of a
PTC heater. Tunning parameters are quickly determined and
applied during the temperature ramp-up to setpoint. Open
the serial plotter to view the graphical results.
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 drdyPin = 5;
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;
const uint16_t samples = 500;
const float inputSpan = 200;
const float outputSpan = 1000;
float outputStart = 0;
float outputStep = 30;
float tempLimit = 75;
uint8_t debounce = 1;

// variables
float Input, Output, Setpoint = 50, 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(drdyPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
while (!Serial) delay(10);
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, 1, 3);
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 * 1000 * 0.2);
debounce = 0; // switch to SSR optimum cycle mode
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, 1, 3);
break;
}
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sTune",
"version": "2.3.0",
"version": "2.3.1",
"description": "Open loop PID autotuner using a novel s-curve inflection point test method. Tuning parameters are determined in about ½Tau on a first-order system with time delay. Full 5Tau testing and multiple serial output options are provided.",
"keywords": "PID, autotune, autotuner, tuner, tune, stune, inflection, step",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=sTune
version=2.3.0
version=2.3.1
author=David Lloyd
maintainer=David Lloyd <dlloydev@testcor.ca>
sentence=Open loop PID autotuner using a novel s-curve inflection point test method.
Expand Down
2 changes: 1 addition & 1 deletion src/sTune.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************************
sTune Library for Arduino - Version 2.3.0
sTune Library for Arduino - Version 2.3.1
by dlloydev https://github.com/Dlloydev/sTune
Licensed under the MIT License.
Expand Down

0 comments on commit d26fd3d

Please sign in to comment.