作成途中ですよーだ
Dependencies: PID QEI mbed-rtos mbed PreHeater SB1602E
main.cpp@4:143b93e499a3, 2015-06-14 (annotated)
- Committer:
- kazu_zamasu
- Date:
- Sun Jun 14 07:04:07 2015 +0000
- Revision:
- 4:143b93e499a3
- Parent:
- 3:9af1bd67c5f8
- Child:
- 5:bfbc802f4958
Fix line 129 Pv read source.
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 | 0:b1d44d6f9adf | 25 | #include "mbed.h" |
kazu_zamasu | 0:b1d44d6f9adf | 26 | #include "PID.h" |
kazu_zamasu | 0:b1d44d6f9adf | 27 | #include "QEI.h" |
kazu_zamasu | 0:b1d44d6f9adf | 28 | #include "rtos.h" |
kazu_zamasu | 0:b1d44d6f9adf | 29 | #include "math.h" |
kazu_zamasu | 2:387240c58110 | 30 | #include "SB1602E.h" |
kazu_zamasu | 0:b1d44d6f9adf | 31 | |
kazu_zamasu | 3:9af1bd67c5f8 | 32 | /* PID constant initialize Kc, Ti, Td, interval */ |
kazu_zamasu | 1:f974481c37b6 | 33 | #define P 1.0 //propotional band |
kazu_zamasu | 1:f974481c37b6 | 34 | #define I 0.2 //Integral |
kazu_zamasu | 1:f974481c37b6 | 35 | #define D 0.1 //Devide |
kazu_zamasu | 1:f974481c37b6 | 36 | #define RATE 0.1 //update time sec |
kazu_zamasu | 0:b1d44d6f9adf | 37 | PID TIC(P, I, D, RATE); |
kazu_zamasu | 0:b1d44d6f9adf | 38 | |
kazu_zamasu | 1:f974481c37b6 | 39 | //GPIO initilaize |
kazu_zamasu | 0:b1d44d6f9adf | 40 | AnalogIn THAI(dp4); |
kazu_zamasu | 0:b1d44d6f9adf | 41 | PwmOut out(dp1); |
kazu_zamasu | 0:b1d44d6f9adf | 42 | DigitalOut led1(dp14),led2(dp28); |
kazu_zamasu | 3:9af1bd67c5f8 | 43 | DigitalIn Run(dp17,PullDown); |
kazu_zamasu | 1:f974481c37b6 | 44 | |
kazu_zamasu | 3:9af1bd67c5f8 | 45 | /*Power on first setpoint temperature */ |
kazu_zamasu | 3:9af1bd67c5f8 | 46 | #define InitialSP 50.0 // PID initial setpoint |
kazu_zamasu | 3:9af1bd67c5f8 | 47 | float temp_sv_input = InitialSP; |
kazu_zamasu | 3:9af1bd67c5f8 | 48 | |
kazu_zamasu | 2:387240c58110 | 49 | double temp_pv,temp_cal; |
kazu_zamasu | 2:387240c58110 | 50 | char *init_massage = "Welcome!"; |
kazu_zamasu | 2:387240c58110 | 51 | |
kazu_zamasu | 3:9af1bd67c5f8 | 52 | |
kazu_zamasu | 4:143b93e499a3 | 53 | /*Rotary encode pin, pinmode and specification initialize */ |
kazu_zamasu | 3:9af1bd67c5f8 | 54 | #define ROTATE_PER_REVOLUTIONS 24 //QEI 1 rotate by count |
kazu_zamasu | 1:f974481c37b6 | 55 | QEI wheel(dp11, PullUp, dp13, PullUp,NC, ROTATE_PER_REVOLUTIONS, QEI::X2_ENCODING); |
kazu_zamasu | 3:9af1bd67c5f8 | 56 | |
kazu_zamasu | 3:9af1bd67c5f8 | 57 | /*LCD I2C pin initialize */ |
kazu_zamasu | 2:387240c58110 | 58 | SB1602E lcd(dp5, dp27, init_massage); // SDA, SCL |
kazu_zamasu | 1:f974481c37b6 | 59 | |
kazu_zamasu | 2:387240c58110 | 60 | |
kazu_zamasu | 3:9af1bd67c5f8 | 61 | void TempCal_thread(void const *args) |
kazu_zamasu | 3:9af1bd67c5f8 | 62 | { |
kazu_zamasu | 0:b1d44d6f9adf | 63 | while (true) { |
kazu_zamasu | 3:9af1bd67c5f8 | 64 | /*input for change to 0 to 100% range by 30C to 120C */ |
kazu_zamasu | 3:9af1bd67c5f8 | 65 | #define RangeSPL 30.0 //calcurate celcius range |
kazu_zamasu | 3:9af1bd67c5f8 | 66 | #define RangeSPH 120.0 //same above |
kazu_zamasu | 3:9af1bd67c5f8 | 67 | /*Temperature setpoint low high range */ |
kazu_zamasu | 3:9af1bd67c5f8 | 68 | temp_sv_input = wheel.getPulses() * 0.5 + RangeSPL; |
kazu_zamasu | 3:9af1bd67c5f8 | 69 | if (temp_sv_input <= RangeSPL) { |
kazu_zamasu | 3:9af1bd67c5f8 | 70 | temp_sv_input = RangeSPL; |
kazu_zamasu | 3:9af1bd67c5f8 | 71 | } else if (temp_sv_input >= RangeSPH) { |
kazu_zamasu | 3:9af1bd67c5f8 | 72 | temp_sv_input = RangeSPH; |
kazu_zamasu | 3:9af1bd67c5f8 | 73 | } |
kazu_zamasu | 3:9af1bd67c5f8 | 74 | temp_cal = THAI.read(); |
kazu_zamasu | 3:9af1bd67c5f8 | 75 | |
kazu_zamasu | 3:9af1bd67c5f8 | 76 | |
kazu_zamasu | 3:9af1bd67c5f8 | 77 | /*six order polynomial calculation value |
kazu_zamasu | 3:9af1bd67c5f8 | 78 | Thermister pull up resiter 560R |
kazu_zamasu | 3:9af1bd67c5f8 | 79 | Thermister B value 3380K |
kazu_zamasu | 3:9af1bd67c5f8 | 80 | Thermister Resistance 10K ohm at 25C |
kazu_zamasu | 3:9af1bd67c5f8 | 81 | */ |
kazu_zamasu | 3:9af1bd67c5f8 | 82 | 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 | 83 | Thread::wait(500); |
kazu_zamasu | 0:b1d44d6f9adf | 84 | |
kazu_zamasu | 1:f974481c37b6 | 85 | |
kazu_zamasu | 3:9af1bd67c5f8 | 86 | /*LCD Display section */ |
kazu_zamasu | 3:9af1bd67c5f8 | 87 | lcd.printf(0, "Temp SP %.1f\n", temp_sv_input); |
kazu_zamasu | 3:9af1bd67c5f8 | 88 | lcd.printf(1, "Temp PV %.1f\n", temp_pv); |
kazu_zamasu | 3:9af1bd67c5f8 | 89 | } |
kazu_zamasu | 3:9af1bd67c5f8 | 90 | } |
kazu_zamasu | 3:9af1bd67c5f8 | 91 | |
kazu_zamasu | 3:9af1bd67c5f8 | 92 | |
kazu_zamasu | 3:9af1bd67c5f8 | 93 | |
kazu_zamasu | 3:9af1bd67c5f8 | 94 | int main() |
kazu_zamasu | 3:9af1bd67c5f8 | 95 | { |
kazu_zamasu | 3:9af1bd67c5f8 | 96 | /* call Tmeperature calculate section */ |
kazu_zamasu | 3:9af1bd67c5f8 | 97 | Thread thread(TempCal_thread); |
kazu_zamasu | 3:9af1bd67c5f8 | 98 | //LCD contrast set from 00 to 3f 64resolution defult set is 32step |
kazu_zamasu | 3:9af1bd67c5f8 | 99 | #define LCDCont 0x32 //LCD contrast set from 00 to 3f 64resolution defult set is 32step |
kazu_zamasu | 3:9af1bd67c5f8 | 100 | lcd.contrast(LCDCont); |
kazu_zamasu | 3:9af1bd67c5f8 | 101 | |
kazu_zamasu | 3:9af1bd67c5f8 | 102 | |
kazu_zamasu | 3:9af1bd67c5f8 | 103 | while (1) { |
kazu_zamasu | 3:9af1bd67c5f8 | 104 | /*Analog input from 30.0C to 120.0C by 0% to 100% */ |
kazu_zamasu | 3:9af1bd67c5f8 | 105 | #define SV_LL 0.0 //PID setpoint % value for lo limit |
kazu_zamasu | 3:9af1bd67c5f8 | 106 | #define SV_HL 100.0 //PID setpoint % value for high limit |
kazu_zamasu | 3:9af1bd67c5f8 | 107 | /* TIC PID setpoit limit set */ |
kazu_zamasu | 3:9af1bd67c5f8 | 108 | TIC.setInputLimits(SV_LL, SV_HL); |
kazu_zamasu | 3:9af1bd67c5f8 | 109 | |
kazu_zamasu | 3:9af1bd67c5f8 | 110 | /* Output from 0.0 to 1.0*/ |
kazu_zamasu | 3:9af1bd67c5f8 | 111 | #define OV_LL 0.0 //PID calcurate output value 0.0 = 0% |
kazu_zamasu | 3:9af1bd67c5f8 | 112 | #define OV_HL 1.0 //PID calcurate output value 1.0 = 100% |
kazu_zamasu | 3:9af1bd67c5f8 | 113 | /* TIC PID control output limit set */ |
kazu_zamasu | 3:9af1bd67c5f8 | 114 | TIC.setOutputLimits(OV_LL, OV_HL); |
kazu_zamasu | 3:9af1bd67c5f8 | 115 | |
kazu_zamasu | 3:9af1bd67c5f8 | 116 | /* TIC PID control output bias */ |
kazu_zamasu | 3:9af1bd67c5f8 | 117 | #define Bias 0.2 //control output bias |
kazu_zamasu | 3:9af1bd67c5f8 | 118 | TIC.setBias(Bias); |
kazu_zamasu | 3:9af1bd67c5f8 | 119 | /* TIC PID control mode set 0=Stop 1=Auto*/ |
kazu_zamasu | 3:9af1bd67c5f8 | 120 | TIC.setMode(Run); |
kazu_zamasu | 3:9af1bd67c5f8 | 121 | /* TIC PID setpoint temperature read */ |
kazu_zamasu | 3:9af1bd67c5f8 | 122 | TIC.setSetPoint(temp_sv_input); |
kazu_zamasu | 3:9af1bd67c5f8 | 123 | |
kazu_zamasu | 3:9af1bd67c5f8 | 124 | /* TIC PID output control */ |
kazu_zamasu | 3:9af1bd67c5f8 | 125 | if (Run == 0) { |
kazu_zamasu | 3:9af1bd67c5f8 | 126 | out = OV_LL; |
kazu_zamasu | 3:9af1bd67c5f8 | 127 | } else if (Run == 1) { |
kazu_zamasu | 3:9af1bd67c5f8 | 128 | /* TIC PID read process value */ |
kazu_zamasu | 4:143b93e499a3 | 129 | TIC.setProcessValue(temp_pv); |
kazu_zamasu | 3:9af1bd67c5f8 | 130 | //PID calculate output. |
kazu_zamasu | 3:9af1bd67c5f8 | 131 | out = TIC.compute(); |
kazu_zamasu | 3:9af1bd67c5f8 | 132 | } |
kazu_zamasu | 3:9af1bd67c5f8 | 133 | //Wait for another loop calculation. |
kazu_zamasu | 3:9af1bd67c5f8 | 134 | Thread::wait(RATE); |
kazu_zamasu | 3:9af1bd67c5f8 | 135 | } |
kazu_zamasu | 3:9af1bd67c5f8 | 136 | } |