pd pi pid controller coil temperature controlling

Homepage

  1. include "mbed.h"
  2. include "millis.h"
  3. include "PID.h"
  1. include "pindef.h"
  2. include "TextLCD.h"
  3. include "Serial.h"
  1. include <string>
  1. define LOG(args...) std::printf(args)

DigitalIn mybutton1(D5); DigitalIn mybutton2(D6); DigitalIn mybutton3(D7);

DigitalOut myled(D13);

float val1=0; float val2=0; float val3=0; float val4=0; float w; int i,j,k,l; TextLCD lcd(D8, D9, D10, D11, D12, D13); TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type) : _rs(rs), _e(e), _d(d4, d5, d6, d7), _type(type) {

_e = 1; _rs = 0; command mode

wait(0.015); Wait 15ms to ensure powered up

send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) for (int i=0; i<3; i++) { writeByte(0x3); wait(0.00164); this command takes 1.64ms, so wait for it } writeByte(0x2); 4-bit mode wait(0.000040f); most instructions take 40us

writeCommand(0x28); Function set 001 BW N F - - writeCommand(0x0C); writeCommand(0x6); Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes cls(); }

float getTemperature();

Serial pc(USBTX, USBRX); Ticker PrintTicker; Send process results to Console once per second Ticker ticker; Set up the millis() ticker.

  1. define DEFAULT_Kp 1
  2. define DEFAULT_Ki 0.002
  3. define DEFAULT_Kd 20
  1. define AUTOMATIC 1
  2. define MANUAL 0
  3. define DIRECT 0
  4. define REVERSE 1
  5. define thermistor A0 FRDM-K64F Analog input pin A3 - Adjust to your particular board
  6. define driver D3 FRDM-K64F PWM output pin PTC3 - Adjust to your particular board

AnalogIn Thermistor(thermistor); Read temperature value from thermistor on A0 PwmOut Driver(driver); PWM drive FET heater on PTC3 values are 0-1.0 For 0-100%

float Input, Output, Setpoint; PID controller(&Input, &Output, &Setpoint, DEFAULT_Kp , DEFAULT_Ki , DEFAULT_Kd , DIRECT);

  1. define RATE 1.0 Print rate Once per second

void PrintValues() { Routine to print out results to console pc.printf("Input Output Setpoint Kp Ki Kd time\r\n"); pc.printf("%f, %f, %f, %f, %f, %f, %d \r\n", Input, Output, Setpoint, controller.GetKp() , controller.GetKi() , controller.GetKd() , millis() );

}

int main(){ wait_ms(100); display sign-on message on LCD lcd.cls(); clear screen, cursor positioned at col 0, row 0 i.e. top row. lcd.printf("Welcome to \n"); lcd.printf("indore\n"); wait(2.0); delay for 2 seconds so that we can see the sign-on message. lcd.cls();

startMillis(); Initialize timer.

pc.baud(9600); pc.printf("\r\nThermistor PID Test - Build " DATE " " TIME "\r\n");

PrintTicker.attach(&PrintValues,RATE); Start PID process running at 100ms rate.

Setpoint = 80; Set target temperature in degrees Celcius. controller.SetMode(AUTOMATIC); Turn PID controller on.

while(1){

lcd.printf("temp= \n"); lcd.printf(" svits indore \n"); wait(2.0); lcd.cls(); if (mybutton1 == 0) { Button is pressed lcd.printf("PI CONTROLLER\n"); wait(1.0); lcd.printf("I/p O/p Sp Ki t\r\n"); lcd.printf("%f, %f, %f, %f, %d \r\n", Input, Output, Setpoint, controller.GetKi(),millis() ); wait(2.0); lcd.cls(); }

if (mybutton2== 0) { Button is pressed lcd.printf("PD CONTROLLER\n"); wait(1.0); lcd.printf("I/p O/p Sp Kd t\r\n"); lcd.printf("%f, %f, %f, %f, %d \r\n", Input, Output, Setpoint, controller.GetKd(),millis() ); wait(2.0); lcd.cls(); }

if (mybutton3 == 0) { Button is pressed lcd.printf("PID CONTROLLER\n");

wait(1.0); lcd.printf("I/p O/p Sp Kp t\r\n"); lcd.printf("%f, %f, %f, %f, %d \r\n", Input, Output, Setpoint, controller.GetKp(),millis() ); wait(2.0); lcd.cls(); }

Input = getTemperature(); Actual temperature in Degrees Celcius

controller.Compute(); Process PID loop.

Driver = Output/1000; Sent PWM value scaled 0 - 1.0 as mbed requires

}

}

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 getTemperature() {

  1. define THERMISTORNOMINAL 100000 100k temp. for nominal resistance (almost always 25 C)
  2. define TEMPERATURENOMINAL 25 The beta coefficient of the thermistor (usually 3000-4000)
  3. define BCOEFFICIENT 3950 the value of the 'other' resistor
  4. 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 temperature = 260 - temperature; convert to C temperature = a;

lcd.printf("Temp %f\r\n", temperature);

return temperature;

}


All wikipages