FRDM-K64F Code Share / Mbed 2 deprecated PIDHeater

Dependencies:   PID millis mbed

Files at this revision

API Documentation at this revision

Comitter:
unix_guru
Date:
Mon Jan 25 22:38:30 2016 +0000
Child:
1:b852d582ad0e
Commit message:
Extruder/Heated Bed controller for the FRDM-K64F

Changed in this revision

PID.lib Show annotated file Show diff for this revision Revisions of this file
PIDHeater.cpp Show annotated file Show diff for this revision Revisions of this file
PIDHeater.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PID.lib	Mon Jan 25 22:38:30 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/FRDM-K64F-Code-Share/code/PID/#117e0c36eb22
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PIDHeater.cpp	Mon Jan 25 22:38:30 2016 +0000
@@ -0,0 +1,75 @@
+#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(currentTemp, desiredTemp);
+}
+  
+ 
+void PIDHeater::setTemperature(float temp) {
+    if (temp >= minTemp && temp <= maxTemp) {
+        desiredTemp = temp;
+        controller.setSetPoint(desiredTemp);             // Set desired temperature in celcius
+    }
+}
+ 
+           
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PIDHeater.h	Mon Jan 25 22:38:30 2016 +0000
@@ -0,0 +1,43 @@
+#ifndef PID_HEATER_H
+#define PID_HEATER_H
+ 
+#include "mbed.h"
+ 
+#include "PID.h"
+// These values were borrowed from Marlin Firmware defaults
+#define  DEFAULT_Kp 17.52
+#define  DEFAULT_Ki 0.62
+#define  DEFAULT_Kd 123.43
+
+  
+/** A simple class for temperature control of a heater element using PID
+ * a heater driven via PWM digital output and one analog temperature sensor.
+ *
+ * Author(s): Michael Ball  unix_guru@hotmail.com
+ *
+ */
+class PIDHeater{
+    public:
+        /** Constructor receives pin names of the temperature sensor and
+         * the DigitalOut pin to which the heater driver is connected. */
+        PIDHeater(PinName sensorPin, PinName digitalOutPin);
+        /** Set the temperature range of the sensor. */
+        void configureRange(float minTemperature, float maxTemperature);
+        /** Read the current room temperature from the sensor. */
+        float getTemperature();
+        /** Test if the heater in turned ON. */
+        bool isHeaterOn();
+        /** Set the desired room temperature. */
+        void setTemperature(float temp);
+        /** Attach run process to a timer */
+        void run();
+        
+    private:
+        AnalogIn thermistor;
+        DigitalOut driver;
+        Ticker ticker;
+        bool heaterOn;
+        float currentTemp, desiredTemp, minTemp, maxTemp, hysteresis, pwmValue;
+};
+ 
+#endif // HEATER_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jan 25 22:38:30 2016 +0000
@@ -0,0 +1,39 @@
+/** A demo application to show how to mangage and control a heater element 
+ * through a PID loop using a Thermistor input and PWM output.
+ * For more information on PID control 
+ *
+ * Author(s): Michael Ball  unix_guru@hotmail.com
+ *
+ */
+
+#include "mbed.h"
+
+#include "PIDHeater.h"                  // PIDHeater simply takes two variables
+                                        // The Thermistor input and the Heater output.
+                                        // 
+Serial pc(USBTX, USBRX);
+Ticker ticker;                        
+
+#define RATE 0.1                        // Check temp every 100ms
+PIDHeater extruder(A3, PTC3);           // Thermistor on A3 heater on PTC3
+
+void run_PID_loop() {                        // Periodically test temperature and set output
+    extruder.run();
+}    
+
+int main(){
+ 
+  extruder.configureRange(0,250);       // Set MIN/MAX temperature in degrees Celcius.
+  extruder.setTemperature(25);         // Set target temperature to 25 degrees Celcius.
+  
+  ticker.attach(&run_PID_loop,RATE);    // Start PID process running at 100ms rate.
+
+  while(1){
+   
+     pc.printf("Extruder Temperature is %f\r\n", extruder.getTemperature());
+     
+     wait(1);  
+
+  }
+ 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jan 25 22:38:30 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6f327212ef96
\ No newline at end of file