CÓDIGO

Este es el código que utilizaremos para realizar las pruebas.



#include

const int heatPin = 9;

const long period = 1000; // >750

doublekp = 0.0;

double kd = 0.0;

double ki = 0.0;

double setTemp = 0.0;

double measuredTemp = 0.0;

doubleoutputPower = 0.0;

long lastSampleTime = 0;

//TMP36 Pin Variables

int temperaturePin = 0; //the analog pin the TMP36's Vout(sense) pin is connected to

//the resolution is 10 mV / degree centigrade

//(500 mV offset) to make negative temperatures an option

PID myPID(&measuredTemp, &outputPower, &setTemp, kp, ki, kd, DIRECT); //

void setup() {

pinMode(heatPin, OUTPUT);

Serial.begin(9600); //Start the serial connection with the computer

Serial.println("t30 - sets the temperature to 30");

Serial.println("k50 20 10 - sets kp, ki and kd respectively");

myPID.SetSampleTime(1000);

myPID.SetMode(AUTOMATIC);

}

void loop() {

checkForSerialCommands();

long now = millis();

if (now > lastSampleTime + period) {

lastSampleTime = now;

double temperature = getVoltage(temperaturePin);

temperature = (temperature - .5) * 100;

delay(period-10);

measuredTemp = temperature;

myPID.Compute();

analogWrite(heatPin, outputPower);

Serial.print(measuredTemp);

Serial.print(", ");

Serial.print(setTemp);

Serial.print(", ");

Serial.println(outputPower);

}

}

void checkForSerialCommands() {

if (Serial.available()) {

char command = Serial.read();

if (command == 't') {

setTemp = Serial.parseFloat();

Serial.print("Set Temp=");

Serial.println(setTemp);

}

if (command == 'k') {

kp = Serial.parseFloat();

ki = Serial.parseFloat();

kd = Serial.parseFloat();

myPID.SetTunings(kp, ki, kd);

Serial.print("Set Constants kp=");

Serial.print(kp);

Serial.print(" ki=");

Serial.print(ki);

Serial.print(" kd=");

Serial.println(kd);

}

}

}

float getVoltage(int pin){

return (analogRead(pin) * .004882814);

}