Origin firmware.
Dependencies: PID QEI SB1602E mbed-rtos mbed
Fork of PreHeater by
Diff: main.cpp
- Revision:
- 3:9af1bd67c5f8
- Parent:
- 2:387240c58110
- Child:
- 4:143b93e499a3
--- a/main.cpp Sat Jun 13 06:34:11 2015 +0000 +++ b/main.cpp Sun Jun 14 02:44:14 2015 +0000 @@ -5,89 +5,108 @@ #include "math.h" #include "SB1602E.h" -#define ROTATE_PER_REVOLUTIONS 24 //QEI 1 rotate by count -#define THR 560 /PTH pull up register value -#define THB 3380 //PTH B number -#define THCR 10000 //25C PTH register -#define OV_LL 0.0 //PID calcurate output value 0.0 = 0% -#define OV_HL 1.0 //PID calcurate output value 1.0 = 100% -#define SV_LL 0.0 //PID setpoint % value for lo limit -#define SV_HL 100.0 //PID setpoint % value for high limit +/* PID constant initialize Kc, Ti, Td, interval */ #define P 1.0 //propotional band #define I 0.2 //Integral #define D 0.1 //Devide #define RATE 0.1 //update time sec -#define Bias 0.2 //control output bias -#define InitialSP 50.0 // PID initial setpoint -#define RangeSPL 30.0 //calcurate celcius range -#define RangeSPH 120.0 //same above - -//Kc, Ti, Td, interval PID TIC(P, I, D, RATE); //GPIO initilaize AnalogIn THAI(dp4); PwmOut out(dp1); DigitalOut led1(dp14),led2(dp28); -DigitalIn Run(dp17,PullUp); +DigitalIn Run(dp17,PullDown); -float temp_sv_input; +/*Power on first setpoint temperature */ +#define InitialSP 50.0 // PID initial setpoint +float temp_sv_input = InitialSP; + double temp_pv,temp_cal; char *init_massage = "Welcome!"; -//init_massage = 'Hello'; -//Rotary encode pin, pinmode and sppecification instance + +/*Rotary encode pin, pinmode and sppecification instance */ +#define ROTATE_PER_REVOLUTIONS 24 //QEI 1 rotate by count QEI wheel(dp11, PullUp, dp13, PullUp,NC, ROTATE_PER_REVOLUTIONS, QEI::X2_ENCODING); -//LCD I2C pin asign + +/*LCD I2C pin initialize */ SB1602E lcd(dp5, dp27, init_massage); // SDA, SCL - -void TempCal_thread(void const *args) { +void TempCal_thread(void const *args) +{ while (true) { - //input for change to 0 to 100% range by 50C to 120C - temp_sv_input = wheel.getPulses() * 0.5 + RangeSPL; - if (temp_sv_input <= RangeSPL){ - temp_sv_input = RangeSPL; - } - else if (temp_sv_input >= RangeSPH){ - temp_sv_input = RangeSPH; - } - temp_cal = THAI.read(); -//six order polynomial - temp_pv =-0.7964*pow(temp_cal,6.0) - 2.5431*pow(temp_cal,5.0) +63.605*pow(temp_cal,4.0) - 274.1*pow(temp_cal,3.0) + 522.57*pow(temp_cal,2.0) - 539.26*temp_cal + 405.76; + /*input for change to 0 to 100% range by 30C to 120C */ +#define RangeSPL 30.0 //calcurate celcius range +#define RangeSPH 120.0 //same above + /*Temperature setpoint low high range */ + temp_sv_input = wheel.getPulses() * 0.5 + RangeSPL; + if (temp_sv_input <= RangeSPL) { + temp_sv_input = RangeSPL; + } else if (temp_sv_input >= RangeSPH) { + temp_sv_input = RangeSPH; + } + temp_cal = THAI.read(); + + + /*six order polynomial calculation value + Thermister pull up resiter 560R + Thermister B value 3380K + Thermister Resistance 10K ohm at 25C + */ + temp_pv =-0.7964*pow(temp_cal,6.0) - 2.5431*pow(temp_cal,5.0) +63.605*pow(temp_cal,4.0) - 274.1*pow(temp_cal,3.0) + 522.57*pow(temp_cal,2.0) - 539.26*temp_cal + 405.76; Thread::wait(500); -//Insert LCD code - lcd.printf(0, "Temp SP %.1f\n", temp_sv_input); - lcd.printf(1, "Temp PV %.1f\n", temp_pv); - } - } - -int main(){ -Thread thread(TempCal_thread); - while (1){ - //Analog input from 50.0C to 120.0C - TIC.setInputLimits(SV_LL, SV_HL); - //Pwm output from 0.0 to 1.0 - TIC.setOutputLimits(OV_LL, OV_HL); - //If there's a bias. - TIC.setBias(Bias); - TIC.setMode(Run); - //We want the process variable to be 1.7V - TIC.setSetPoint(temp_sv_input); - - if (Run == 1){ - out = OV_LL; - } - else if (Run == 0){ - //Update the process variable. - TIC.setProcessValue(temp_cal); - //Set the new output. - out = TIC.compute(); - } - //Wait for another loop calculation. - Thread::wait(RATE); - } - } \ No newline at end of file + /*LCD Display section */ + lcd.printf(0, "Temp SP %.1f\n", temp_sv_input); + lcd.printf(1, "Temp PV %.1f\n", temp_pv); + } +} + + + +int main() +{ + /* call Tmeperature calculate section */ + Thread thread(TempCal_thread); + //LCD contrast set from 00 to 3f 64resolution defult set is 32step +#define LCDCont 0x32 //LCD contrast set from 00 to 3f 64resolution defult set is 32step + lcd.contrast(LCDCont); + + + while (1) { + /*Analog input from 30.0C to 120.0C by 0% to 100% */ +#define SV_LL 0.0 //PID setpoint % value for lo limit +#define SV_HL 100.0 //PID setpoint % value for high limit + /* TIC PID setpoit limit set */ + TIC.setInputLimits(SV_LL, SV_HL); + + /* Output from 0.0 to 1.0*/ +#define OV_LL 0.0 //PID calcurate output value 0.0 = 0% +#define OV_HL 1.0 //PID calcurate output value 1.0 = 100% + /* TIC PID control output limit set */ + TIC.setOutputLimits(OV_LL, OV_HL); + + /* TIC PID control output bias */ +#define Bias 0.2 //control output bias + TIC.setBias(Bias); + /* TIC PID control mode set 0=Stop 1=Auto*/ + TIC.setMode(Run); + /* TIC PID setpoint temperature read */ + TIC.setSetPoint(temp_sv_input); + + /* TIC PID output control */ + if (Run == 0) { + out = OV_LL; + } else if (Run == 1) { + /* TIC PID read process value */ + TIC.setProcessValue(temp_cal); + //PID calculate output. + out = TIC.compute(); + } + //Wait for another loop calculation. + Thread::wait(RATE); + } +} \ No newline at end of file