Skip to content

Commit a082053

Browse files
committed
New sTune (autotune) examples
1 parent e1f98ed commit a082053

6 files changed

Lines changed: 146 additions & 7 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2021 David Lloyd <dlloydev@testcor.ca>
1+
Copyright 2022 David Lloyd <dlloydev@testcor.ca>
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of
44
this software and associated documentation files (the "Software"), to deal in
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/********************************************************************
2+
PID_v1 Autotune Example (using Temperature Control Lab)
3+
http://apmonitor.com/pdc/index.php/Main/ArduinoTemperatureControl
4+
********************************************************************/
5+
6+
#include <sTune.h>
7+
#include <PID_v1.h>
8+
9+
const uint8_t inputPin = 0;
10+
const uint8_t outputPin = 3;
11+
12+
//user settings
13+
const uint32_t testTimeSec = 300;
14+
const float outputStart = 0;
15+
const float outputStep = 20;
16+
const uint16_t samples = 500;
17+
const uint32_t settleTimeSec = 10;
18+
19+
//input constants
20+
const float mvResolution = 3300 / 1024.0f;
21+
const float bias = 50;
22+
23+
double Input = 0, Output = 0, Setpoint = 30; //myPID
24+
float input = 0, output = 0, kp = 0, ki = 0, kd = 0; //tuner
25+
26+
PID myPID(&Input, &Output, &Setpoint, 0, 0, 0, DIRECT);
27+
28+
sTune tuner = sTune(&input, &output, tuner.zieglerNicholsPID, tuner.directIP, tuner.printALL);
29+
/* zieglerNicholsPI directIP serialOFF
30+
zieglerNicholsPID direct5T printALL
31+
tyreusLuybenPI reverseIP printSUMMARY
32+
tyreusLuybenPID reverse5T printDEBUG
33+
cianconeMarlinPI printPIDTUNER
34+
cianconeMarlinPID serialPLOTTER
35+
amigofPID
36+
pessenIntegralPID
37+
someOvershootPID
38+
noOvershootPID
39+
*/
40+
void setup() {
41+
analogReference(EXTERNAL);
42+
Serial.begin(115200);
43+
analogWrite(outputPin, outputStart);
44+
tuner.Configure(outputStart, outputStep, testTimeSec, settleTimeSec, samples);
45+
}
46+
47+
void loop() {
48+
49+
switch (tuner.Run()) {
50+
case tuner.inOut:
51+
input = (analogRead(inputPin) / mvResolution) - bias;
52+
analogWrite(outputPin, output);
53+
break;
54+
55+
case tuner.tunings:
56+
tuner.SetAutoTunings(&kp, &ki, &kd);
57+
myPID.SetMode(AUTOMATIC);
58+
myPID.SetSampleTime((testTimeSec * 1000) / samples);
59+
myPID.SetTunings(kp, ki, kd);
60+
break;
61+
62+
case tuner.runPid:
63+
Input = (analogRead(inputPin) / mvResolution) - bias;
64+
myPID.Compute();
65+
analogWrite(outputPin, Output);
66+
break;
67+
}
68+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/********************************************************************
2+
QuickPID Autotune Example (using Temperature Control Lab)
3+
http://apmonitor.com/pdc/index.php/Main/ArduinoTemperatureControl
4+
********************************************************************/
5+
6+
#include <sTune.h>
7+
#include <QuickPID.h>
8+
9+
const uint8_t inputPin = 0;
10+
const uint8_t outputPin = 3;
11+
12+
//user settings
13+
const uint32_t testTimeSec = 300;
14+
const float outputStart = 0;
15+
const float outputStep = 20;
16+
const uint16_t samples = 500;
17+
const uint32_t settleTimeSec = 10;
18+
19+
//input constants
20+
const float mvResolution = 3300 / 1024.0f;
21+
const float bias = 50;
22+
23+
float Input = 0, Output = 0, Setpoint = 30, Kp = 0, Ki = 0, Kd = 0;
24+
25+
QuickPID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd,
26+
myPID.pMode::pOnError,
27+
myPID.dMode::dOnMeas,
28+
myPID.iAwMode::iAwClamp,
29+
myPID.Action::reverse);
30+
31+
sTune tuner = sTune(&Input, &Output, tuner.zieglerNicholsPID, tuner.directIP, tuner.printALL);
32+
/* zieglerNicholsPI directIP serialOFF
33+
zieglerNicholsPID direct5T printALL
34+
tyreusLuybenPI reverseIP printSUMMARY
35+
tyreusLuybenPID reverse5T printDEBUG
36+
cianconeMarlinPI printPIDTUNER
37+
cianconeMarlinPID serialPLOTTER
38+
amigofPID
39+
pessenIntegralPID
40+
someOvershootPID
41+
noOvershootPID
42+
*/
43+
void setup() {
44+
analogReference(EXTERNAL);
45+
Serial.begin(115200);
46+
analogWrite(outputPin, outputStart);
47+
tuner.Configure(outputStart, outputStep, testTimeSec, settleTimeSec, samples);
48+
}
49+
50+
void loop() {
51+
52+
switch (tuner.Run()) {
53+
case tuner.inOut:
54+
Input = (analogRead(inputPin) / mvResolution) - bias;
55+
analogWrite(outputPin, Output);
56+
break;
57+
58+
case tuner.tunings:
59+
tuner.SetAutoTunings(&Kp, &Ki, &Kd);
60+
myPID.SetMode(myPID.Control::automatic);
61+
myPID.SetSampleTimeUs((testTimeSec * 1000000) / samples);
62+
myPID.SetTunings(Kp, Ki, Kd);
63+
break;
64+
65+
case tuner.runPid:
66+
Input = (analogRead(inputPin) / mvResolution) - bias;
67+
myPID.Compute();
68+
analogWrite(outputPin, Output);
69+
break;
70+
}
71+
}

library.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "QuickPID",
3-
"version": "3.0.4",
4-
"description": "A fast PID controller with Integral anti-windup, TIMER mode and multiple options for Proportional, Derivative and anti-windup modes of operation. Also includes analogWrite compatibility for ESP32 and ESP32-S2.",
5-
"keywords": "PID, controller, signal",
3+
"version": "3.0.5",
4+
"description": "A fast PID controller with Integral anti-windup, timer mode and multiple options for Proportional, Derivative and anti-windup modes of operation. Also includes analogWrite compatibility for ESP32 and ESP32-S2.",
5+
"keywords": "PID, controller, signal, autotune, tuner, stune",
66
"repository":
77
{
88
"type": "git",
@@ -20,7 +20,7 @@
2020
"license": "MIT",
2121
"homepage": "https://github.com/Dlloydev/QuickPID",
2222
"dependencies": {
23-
"QuickPID": "~3.0.4"
23+
"QuickPID": "~3.0.5"
2424
},
2525
"frameworks": "arduino",
2626
"platforms": "*"

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=QuickPID
2-
version=3.0.4
2+
version=3.0.5
33
author=David Lloyd
44
maintainer=David Lloyd <dlloydev@testcor.ca>
55
sentence=A fast PID controller with Integral anti-windup, timer mode and multiple options for Proportional, Derivative and anti-windup modes of operation.

src/QuickPID.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************************
2-
QuickPID Library for Arduino - Version 3.0.4
2+
QuickPID Library for Arduino - Version 3.0.5
33
by dlloydev https://github.com/Dlloydev/QuickPID
44
Based on the Arduino PID_v1 Library. Licensed under the MIT License.
55
**********************************************************************************/

0 commit comments

Comments
 (0)