PIDHeater82

Dependencies:   PID mbed millis ttmath

Fork of PIDHeater by FRDM-K64F Code Share

PIDHeater.cpp

Committer:
unix_guru
Date:
2016-01-27
Revision:
1:b852d582ad0e
Parent:
0:8b77aea74642

File content as of revision 1:b852d582ad0e:

#include "PIDHeater.h"

PID controller(DEFAULT_Kp , DEFAULT_Ki , DEFAULT_Kd , 0.1);       // Initialize PID controller  -- Kc, Ti, Td, interval
 
 
PIDHeater::PIDHeater(PinName sensorPin, PinName pwmoutPin): thermistor(sensorPin), driver(pwmoutPin) {

    heaterOn = false;
    controller.setInputLimits(0, 250);      // MIN/MAX Temperature values 
    controller.setOutputLimits(0.0, 1.0);   // Pwm output from 0.0 to 1.0
//  controller.setMode(AUTO);
    controller.setSetPoint(22);             // Set initial Temperature to 22 degrees celcius
 }
 
void PIDHeater::configureRange(float minTemperature, float maxTemperature) {
    if (minTemperature < maxTemperature) {
        minTemp = minTemperature;
        maxTemp = maxTemperature;
        controller.setInputLimits(minTemperature, maxTemperature);      // MIN/MAX Temperature values    
    }
}
 
float PIDHeater::getTemperature() {
#define THERMISTORNOMINAL 100000      // 100k 
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25   
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 4700    

// This is the workhorse routine that calculates the temperature
// using the Steinhart-Hart equation for thermistors
// https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation

    float temperature, resistance;
    float steinhart;
    int a;
    
    a = thermistor.read_u16();       // Read 16bit Analog value
//    pc.printf("Raw Analog Value for Thermistor = %d\r\n",a);
  
    /* Calculate the resistance of the thermistor from analog votage read. */
    resistance = (float) SERIESRESISTOR / ((65536.0 / a) - 1);
//    pc.printf("Resistance for Thermistor = %f\r\n",resistance);
   
    steinhart = resistance / THERMISTORNOMINAL;         // (R/Ro)
    steinhart = log(steinhart);                         // ln(R/Ro)
    steinhart /= BCOEFFICIENT;                          // 1/B * ln(R/Ro)
    steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15);   // + (1/To)
    steinhart = 1.0 / steinhart;                        // Invert
    temperature = steinhart - 273.15;                   // convert to C

    return temperature;    

}
 
bool PIDHeater::isHeaterOn() {
    return heaterOn;
}
 
void PIDHeater::run() {
    getTemperature();
    driver = controller.compute();
}
  
 
void PIDHeater::setTemperature(float temp) {
    if (temp >= minTemp && temp <= maxTemp) {
        desiredTemp = temp;
        controller.setSetPoint(desiredTemp);             // Set desired temperature in celcius
    }
}