Created
March 30, 2016 15:20
-
-
Save jalexandre0/d8c0222c685d6f5c007de8006e30d400 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PID_v1.h> | |
#include <PID_AutoTune_v0.h> | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
//Pins | |
#define RelayPin 6 | |
#define ONE_WIRE_BUS 2 | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature sensors(&oneWire); | |
double aTuneStep=2500, aTuneNoise=1, aTuneStartValue=2500; | |
double input, output, setpoint=35, kp=0, ki=0, kd=0; | |
int windowSize = 5000 ; | |
unsigned long windowStartTime, now; | |
int heat ; | |
byte ATuneModeRemember=2; | |
unsigned int aTuneLookBack=20; | |
boolean tuning = true; | |
int serialTime; | |
PID myPID(&input, &output, &setpoint,kp,ki,kd, DIRECT); | |
PID_ATune aTune(&input, &output); | |
void setup() | |
{ | |
//Setup the pid | |
myPID.SetMode(AUTOMATIC); | |
if(tuning) | |
{ | |
tuning=false; | |
changeAutoTune(); | |
tuning=true; | |
} | |
serialTime = 0; | |
Serial.begin(9600); | |
sensors.begin(); | |
windowStartTime - millis(); | |
myPID.SetOutputLimits(0, windowSize); | |
myPID.SetMode(AUTOMATIC) ; | |
pinMode(RelayPin, OUTPUT); | |
} | |
void loop() | |
{ | |
now = millis(); | |
sensors.requestTemperatures(); | |
input = sensors.getTempCByIndex(0); | |
if(tuning) | |
{ | |
byte val = (aTune.Runtime()); | |
if (val!=0) | |
{ | |
tuning = false; | |
} | |
if(!tuning) | |
{ | |
kp = aTune.GetKp(); | |
ki = aTune.GetKi(); | |
kd = aTune.GetKd(); | |
myPID.SetTunings(kp,ki,kd); | |
AutoTuneHelper(false); | |
} | |
} | |
else myPID.Compute(); | |
if(now - windowStartTime>windowSize) | |
{ | |
windowStartTime += windowSize; | |
} | |
if(output > now - windowStartTime) { | |
digitalWrite(RelayPin,HIGH); | |
heat = 1 ; | |
} | |
else { | |
digitalWrite(RelayPin,LOW); | |
heat = 0 ; | |
} | |
SerialSend(); | |
} | |
void changeAutoTune() | |
{ | |
if(!tuning) | |
{ | |
output=aTuneStartValue; | |
aTune.SetNoiseBand(aTuneNoise); | |
aTune.SetOutputStep(aTuneStep); | |
aTune.SetLookbackSec((int)aTuneLookBack); | |
AutoTuneHelper(true); | |
tuning = true; | |
} | |
else | |
{ | |
aTune.Cancel(); | |
tuning = false; | |
AutoTuneHelper(false); | |
} | |
} | |
void AutoTuneHelper(boolean start) | |
{ | |
if(start) | |
ATuneModeRemember = myPID.GetMode(); | |
else | |
myPID.SetMode(ATuneModeRemember); | |
} | |
void SerialSend() | |
{ | |
// unsigned long now = millis(); | |
Serial.print("setpoint: ");Serial.print(setpoint); Serial.print(" "); | |
Serial.print("input: ");Serial.print(input); Serial.print(" "); | |
Serial.print("output: ");Serial.print(output); Serial.print(" "); | |
Serial.print("kp: ");Serial.print(myPID.GetKp());Serial.print(" "); | |
Serial.print("ki: ");Serial.print(myPID.GetKi());Serial.print(" "); | |
Serial.print("kd: ");Serial.print(myPID.GetKd());Serial.print(" "); | |
Serial.print("tuning: ");Serial.print(tuning);Serial.println(""); | |
// Serial.print("time: ");Serial.print(now);Serial.println(); | |
} | |
void SerialReceive() | |
{ | |
if(Serial.available()) | |
{ | |
char b = Serial.read(); | |
Serial.flush(); | |
if((b=='1' && !tuning) || (b!='1' && tuning))changeAutoTune(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment