![](/media/cache/profiles/090a24f78cd36ddf8f89779fc5ab22a8.jpg.50x50_q85.png)
Origin firmware.
Dependencies: PID QEI SB1602E mbed-rtos mbed
Fork of PreHeater by
main.cpp@6:704706b288b4, 2015-07-03 (annotated)
- Committer:
- kazu_zamasu
- Date:
- Fri Jul 03 11:43:09 2015 +0000
- Revision:
- 6:704706b288b4
- Parent:
- 5:bfbc802f4958
- Child:
- 9:3e9f4d44b4b5
Rev5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kazu_zamasu | 4:143b93e499a3 | 1 | /*The MIT License (MIT) |
kazu_zamasu | 4:143b93e499a3 | 2 | |
kazu_zamasu | 4:143b93e499a3 | 3 | Copyright (c) <2015> <Kazumichi Aoki> |
kazu_zamasu | 4:143b93e499a3 | 4 | |
kazu_zamasu | 4:143b93e499a3 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
kazu_zamasu | 4:143b93e499a3 | 6 | of this software and associated documentation files (the "Software"), to deal |
kazu_zamasu | 4:143b93e499a3 | 7 | in the Software without restriction, including without limitation the rights |
kazu_zamasu | 4:143b93e499a3 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kazu_zamasu | 4:143b93e499a3 | 9 | copies of the Software, and to permit persons to whom the Software is |
kazu_zamasu | 4:143b93e499a3 | 10 | furnished to do so, subject to the following conditions: |
kazu_zamasu | 4:143b93e499a3 | 11 | |
kazu_zamasu | 4:143b93e499a3 | 12 | The above copyright notice and this permission notice shall be included in |
kazu_zamasu | 4:143b93e499a3 | 13 | all copies or substantial portions of the Software. |
kazu_zamasu | 4:143b93e499a3 | 14 | |
kazu_zamasu | 4:143b93e499a3 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kazu_zamasu | 4:143b93e499a3 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kazu_zamasu | 4:143b93e499a3 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kazu_zamasu | 4:143b93e499a3 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kazu_zamasu | 4:143b93e499a3 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kazu_zamasu | 4:143b93e499a3 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
kazu_zamasu | 4:143b93e499a3 | 21 | THE SOFTWARE. |
kazu_zamasu | 4:143b93e499a3 | 22 | */ |
kazu_zamasu | 4:143b93e499a3 | 23 | |
kazu_zamasu | 4:143b93e499a3 | 24 | |
kazu_zamasu | 5:bfbc802f4958 | 25 | |
kazu_zamasu | 0:b1d44d6f9adf | 26 | #include "mbed.h" |
kazu_zamasu | 0:b1d44d6f9adf | 27 | #include "PID.h" |
kazu_zamasu | 0:b1d44d6f9adf | 28 | #include "math.h" |
kazu_zamasu | 2:387240c58110 | 29 | #include "SB1602E.h" |
kazu_zamasu | 5:bfbc802f4958 | 30 | #include "QEI.h" |
kazu_zamasu | 5:bfbc802f4958 | 31 | #include "rtos.h" |
kazu_zamasu | 1:f974481c37b6 | 32 | //GPIO initilaize |
kazu_zamasu | 0:b1d44d6f9adf | 33 | AnalogIn THAI(dp4); |
kazu_zamasu | 0:b1d44d6f9adf | 34 | PwmOut out(dp1); |
kazu_zamasu | 5:bfbc802f4958 | 35 | DigitalOut ledR(dp28),ledG(dp26),ledB(dp14); |
kazu_zamasu | 5:bfbc802f4958 | 36 | InterruptIn RunPB(dp13); |
kazu_zamasu | 5:bfbc802f4958 | 37 | Serial pc(dp16, dp15); // tx, rx |
kazu_zamasu | 2:387240c58110 | 38 | |
kazu_zamasu | 3:9af1bd67c5f8 | 39 | |
kazu_zamasu | 5:bfbc802f4958 | 40 | /* PID constant initialize Kc, Ti, Td, interval */ |
kazu_zamasu | 5:bfbc802f4958 | 41 | #define P 0.1 //propotional band |
kazu_zamasu | 5:bfbc802f4958 | 42 | #define I 1.5 //Integral |
kazu_zamasu | 5:bfbc802f4958 | 43 | #define D 1.0 //Devide |
kazu_zamasu | 5:bfbc802f4958 | 44 | #define RATE 0.1 //update time sec |
kazu_zamasu | 5:bfbc802f4958 | 45 | #define InitialSP 30.0 // Boot Setpoint initial temeprature |
kazu_zamasu | 5:bfbc802f4958 | 46 | PID TIC(P, I, D, RATE); |
kazu_zamasu | 3:9af1bd67c5f8 | 47 | |
kazu_zamasu | 3:9af1bd67c5f8 | 48 | /*LCD I2C pin initialize */ |
kazu_zamasu | 5:bfbc802f4958 | 49 | char *init_massage = "Hello!"; |
kazu_zamasu | 2:387240c58110 | 50 | SB1602E lcd(dp5, dp27, init_massage); // SDA, SCL |
kazu_zamasu | 5:bfbc802f4958 | 51 | /*QEI initialize */ |
kazu_zamasu | 5:bfbc802f4958 | 52 | #define ROTATE_PER_REVOLUTIONS 24 |
kazu_zamasu | 5:bfbc802f4958 | 53 | QEI wheel(dp17, dp18, NC, ROTATE_PER_REVOLUTIONS, QEI::X2_ENCODING); |
kazu_zamasu | 1:f974481c37b6 | 54 | |
kazu_zamasu | 5:bfbc802f4958 | 55 | //Initial |
kazu_zamasu | 5:bfbc802f4958 | 56 | float temp_sv_input = InitialSP, waittime,OV_LL,ticout,qei,Bias; |
kazu_zamasu | 5:bfbc802f4958 | 57 | bool Run(false); |
kazu_zamasu | 5:bfbc802f4958 | 58 | double temp_pv,temp_cal; |
kazu_zamasu | 5:bfbc802f4958 | 59 | int rtostime; |
kazu_zamasu | 2:387240c58110 | 60 | |
kazu_zamasu | 5:bfbc802f4958 | 61 | /*PB control*/ |
kazu_zamasu | 6:704706b288b4 | 62 | void runmode() |
kazu_zamasu | 6:704706b288b4 | 63 | { |
kazu_zamasu | 5:bfbc802f4958 | 64 | Run = !Run; |
kazu_zamasu | 5:bfbc802f4958 | 65 | } |
kazu_zamasu | 5:bfbc802f4958 | 66 | |
kazu_zamasu | 6:704706b288b4 | 67 | void cal_temp(void const *argument) |
kazu_zamasu | 6:704706b288b4 | 68 | { |
kazu_zamasu | 6:704706b288b4 | 69 | while(1) { |
kazu_zamasu | 6:704706b288b4 | 70 | /*input for change to 0 to 100% range by 30C to 120C */ |
kazu_zamasu | 3:9af1bd67c5f8 | 71 | /*Temperature setpoint low high range */ |
kazu_zamasu | 5:bfbc802f4958 | 72 | #define RangeSPL 30.0 //Celcius low side temperature |
kazu_zamasu | 5:bfbc802f4958 | 73 | temp_sv_input = wheel.getPulses() / 20.0 + RangeSPL; |
kazu_zamasu | 3:9af1bd67c5f8 | 74 | if (temp_sv_input <= RangeSPL) { |
kazu_zamasu | 3:9af1bd67c5f8 | 75 | temp_sv_input = RangeSPL; |
kazu_zamasu | 5:bfbc802f4958 | 76 | #define RangeSPH 100.0 //Celcius high side temperature |
kazu_zamasu | 3:9af1bd67c5f8 | 77 | } else if (temp_sv_input >= RangeSPH) { |
kazu_zamasu | 3:9af1bd67c5f8 | 78 | temp_sv_input = RangeSPH; |
kazu_zamasu | 3:9af1bd67c5f8 | 79 | } |
kazu_zamasu | 6:704706b288b4 | 80 | |
kazu_zamasu | 3:9af1bd67c5f8 | 81 | /*six order polynomial calculation value |
kazu_zamasu | 3:9af1bd67c5f8 | 82 | Thermister pull up resiter 560R |
kazu_zamasu | 3:9af1bd67c5f8 | 83 | Thermister B value 3380K |
kazu_zamasu | 3:9af1bd67c5f8 | 84 | Thermister Resistance 10K ohm at 25C |
kazu_zamasu | 5:bfbc802f4958 | 85 | This NTC is NCP18XH103F03RB muRata |
kazu_zamasu | 3:9af1bd67c5f8 | 86 | */ |
kazu_zamasu | 5:bfbc802f4958 | 87 | temp_cal = THAI.read() * 3.3; |
kazu_zamasu | 3:9af1bd67c5f8 | 88 | 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; |
kazu_zamasu | 1:f974481c37b6 | 89 | |
kazu_zamasu | 3:9af1bd67c5f8 | 90 | /*LCD Display section */ |
kazu_zamasu | 6:704706b288b4 | 91 | /*LCD Display section */ |
kazu_zamasu | 6:704706b288b4 | 92 | lcd.printf(0, "SP %.1f", temp_sv_input); |
kazu_zamasu | 6:704706b288b4 | 93 | lcd.printf(0, "PV %.1f\n", temp_pv); |
kazu_zamasu | 6:704706b288b4 | 94 | lcd.printf(1, "OUT %.2f", out.read() * 100); |
kazu_zamasu | 6:704706b288b4 | 95 | lcd.printf(1, "PB %s\n", Run); |
kazu_zamasu | 5:bfbc802f4958 | 96 | /*Tenperature indicater */ |
kazu_zamasu | 5:bfbc802f4958 | 97 | /* 1.5C high temperature */ |
kazu_zamasu | 6:704706b288b4 | 98 | if (Run == true) { |
kazu_zamasu | 6:704706b288b4 | 99 | if ((temp_pv - temp_sv_input) >= 1.5) { |
kazu_zamasu | 6:704706b288b4 | 100 | ledR = 0; |
kazu_zamasu | 6:704706b288b4 | 101 | ledG = 1; |
kazu_zamasu | 6:704706b288b4 | 102 | ledB = 1; |
kazu_zamasu | 6:704706b288b4 | 103 | /* 1.5C low temperature */ |
kazu_zamasu | 6:704706b288b4 | 104 | } else if ((temp_sv_input - temp_pv ) >= 1.5 ) { |
kazu_zamasu | 6:704706b288b4 | 105 | ledR = 1; |
kazu_zamasu | 6:704706b288b4 | 106 | ledG = 1; |
kazu_zamasu | 6:704706b288b4 | 107 | ledB = 0; |
kazu_zamasu | 6:704706b288b4 | 108 | } else { |
kazu_zamasu | 6:704706b288b4 | 109 | /* control green */ |
kazu_zamasu | 6:704706b288b4 | 110 | ledR = 1; |
kazu_zamasu | 6:704706b288b4 | 111 | ledG = 0; |
kazu_zamasu | 6:704706b288b4 | 112 | ledB = 1; |
kazu_zamasu | 6:704706b288b4 | 113 | } |
kazu_zamasu | 5:bfbc802f4958 | 114 | } else { |
kazu_zamasu | 5:bfbc802f4958 | 115 | ledR = 1; |
kazu_zamasu | 5:bfbc802f4958 | 116 | ledG = 1; |
kazu_zamasu | 5:bfbc802f4958 | 117 | ledB = 1; |
kazu_zamasu | 5:bfbc802f4958 | 118 | } |
kazu_zamasu | 6:704706b288b4 | 119 | Thread::wait(rtostime * 2); |
kazu_zamasu | 6:704706b288b4 | 120 | } |
kazu_zamasu | 5:bfbc802f4958 | 121 | } |
kazu_zamasu | 3:9af1bd67c5f8 | 122 | |
kazu_zamasu | 3:9af1bd67c5f8 | 123 | |
kazu_zamasu | 3:9af1bd67c5f8 | 124 | int main() |
kazu_zamasu | 3:9af1bd67c5f8 | 125 | { |
kazu_zamasu | 5:bfbc802f4958 | 126 | RunPB.mode(PullDown); |
kazu_zamasu | 3:9af1bd67c5f8 | 127 | #define LCDCont 0x32 //LCD contrast set from 00 to 3f 64resolution defult set is 32step |
kazu_zamasu | 3:9af1bd67c5f8 | 128 | lcd.contrast(LCDCont); |
kazu_zamasu | 5:bfbc802f4958 | 129 | out.period(0.02); |
kazu_zamasu | 5:bfbc802f4958 | 130 | RunPB.rise(&runmode); |
kazu_zamasu | 5:bfbc802f4958 | 131 | Thread thread(cal_temp); |
kazu_zamasu | 5:bfbc802f4958 | 132 | rtostime = RATE * 1000; |
kazu_zamasu | 6:704706b288b4 | 133 | while (1) { |
kazu_zamasu | 5:bfbc802f4958 | 134 | /*PID control*/ |
kazu_zamasu | 3:9af1bd67c5f8 | 135 | #define SV_LL 0.0 //PID setpoint % value for lo limit |
kazu_zamasu | 3:9af1bd67c5f8 | 136 | #define SV_HL 100.0 //PID setpoint % value for high limit |
kazu_zamasu | 6:704706b288b4 | 137 | TIC.setInputLimits(SV_LL, SV_HL); |
kazu_zamasu | 3:9af1bd67c5f8 | 138 | #define OV_LL 0.0 //PID calcurate output value 0.0 = 0% |
kazu_zamasu | 5:bfbc802f4958 | 139 | #define OV_HL 100.0 //PID calcurate output value 1.0 = 100% |
kazu_zamasu | 6:704706b288b4 | 140 | if (Run == true) { |
kazu_zamasu | 5:bfbc802f4958 | 141 | TIC.setOutputLimits(OV_LL, OV_HL); |
kazu_zamasu | 5:bfbc802f4958 | 142 | TIC.setSetPoint(temp_sv_input); |
kazu_zamasu | 5:bfbc802f4958 | 143 | TIC.setProcessValue(temp_pv); |
kazu_zamasu | 5:bfbc802f4958 | 144 | TIC.setBias(Bias); //control output bias |
kazu_zamasu | 5:bfbc802f4958 | 145 | TIC.setMode(1); |
kazu_zamasu | 5:bfbc802f4958 | 146 | out = TIC.compute() /100; |
kazu_zamasu | 5:bfbc802f4958 | 147 | TIC.setInterval(RATE); |
kazu_zamasu | 6:704706b288b4 | 148 | } else if (Run == false) { |
kazu_zamasu | 6:704706b288b4 | 149 | TIC.setMode(0); |
kazu_zamasu | 6:704706b288b4 | 150 | TIC.reset(); |
kazu_zamasu | 6:704706b288b4 | 151 | out = 0.0; |
kazu_zamasu | 6:704706b288b4 | 152 | } |
kazu_zamasu | 6:704706b288b4 | 153 | Thread::wait(rtostime); |
kazu_zamasu | 3:9af1bd67c5f8 | 154 | |
kazu_zamasu | 6:704706b288b4 | 155 | } |
kazu_zamasu | 3:9af1bd67c5f8 | 156 | } |