![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dd
Dependencies: BufferedSerial FastAnalogIn FastPWM mbed SHT75
controlt.cpp@6:c2fb5c188e8a, 2016-03-25 (annotated)
- Committer:
- sbh9428
- Date:
- Fri Mar 25 09:28:09 2016 +0000
- Revision:
- 6:c2fb5c188e8a
- Parent:
- 5:8e3b5ccf7207
- Child:
- 7:851659cf185c
fin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sbh9428 | 0:9bfc4aea91e2 | 1 | /* |
sbh9428 | 0:9bfc4aea91e2 | 2 | * controlt.cpp |
sbh9428 | 0:9bfc4aea91e2 | 3 | * |
sbh9428 | 0:9bfc4aea91e2 | 4 | * Created on: 2016. 2. 19. |
sbh9428 | 0:9bfc4aea91e2 | 5 | * Author: sbh9428 |
sbh9428 | 0:9bfc4aea91e2 | 6 | */ |
sbh9428 | 0:9bfc4aea91e2 | 7 | |
sbh9428 | 0:9bfc4aea91e2 | 8 | #include "controlt.h" |
sbh9428 | 0:9bfc4aea91e2 | 9 | |
sbh9428 | 0:9bfc4aea91e2 | 10 | control_t::control_t() { |
sbh9428 | 0:9bfc4aea91e2 | 11 | // TODO Auto-generated constructor stub |
sbh9428 | 0:9bfc4aea91e2 | 12 | |
sbh9428 | 0:9bfc4aea91e2 | 13 | } |
sbh9428 | 0:9bfc4aea91e2 | 14 | |
sbh9428 | 2:4c51394fb35b | 15 | control_t::control_t(temp_sensor_t* _temp_sensor, peltier_t* _peltier,BufferedSerial *_pc) |
sbh9428 | 0:9bfc4aea91e2 | 16 | { |
sbh9428 | 1:5c42ec7f1aeb | 17 | log_count=0; |
sbh9428 | 6:c2fb5c188e8a | 18 | time=0; |
sbh9428 | 1:5c42ec7f1aeb | 19 | |
sbh9428 | 1:5c42ec7f1aeb | 20 | target_temp=15; |
sbh9428 | 1:5c42ec7f1aeb | 21 | P_value=0; |
sbh9428 | 1:5c42ec7f1aeb | 22 | I_value=0; |
sbh9428 | 1:5c42ec7f1aeb | 23 | D_value=0; |
sbh9428 | 1:5c42ec7f1aeb | 24 | |
sbh9428 | 0:9bfc4aea91e2 | 25 | temp_sensor=_temp_sensor; |
sbh9428 | 0:9bfc4aea91e2 | 26 | peltier=_peltier; |
sbh9428 | 2:4c51394fb35b | 27 | pc=_pc; |
sbh9428 | 2:4c51394fb35b | 28 | |
sbh9428 | 2:4c51394fb35b | 29 | table_count=0; |
sbh9428 | 2:4c51394fb35b | 30 | table_mode=0; |
sbh9428 | 0:9bfc4aea91e2 | 31 | } |
sbh9428 | 0:9bfc4aea91e2 | 32 | |
sbh9428 | 0:9bfc4aea91e2 | 33 | control_t::~control_t() { |
sbh9428 | 0:9bfc4aea91e2 | 34 | // TODO Auto-generated destructor stub |
sbh9428 | 0:9bfc4aea91e2 | 35 | } |
sbh9428 | 0:9bfc4aea91e2 | 36 | |
sbh9428 | 0:9bfc4aea91e2 | 37 | float control_t::get_temp() |
sbh9428 | 0:9bfc4aea91e2 | 38 | { |
sbh9428 | 0:9bfc4aea91e2 | 39 | return temp_sensor->get_temp(); |
sbh9428 | 0:9bfc4aea91e2 | 40 | } |
sbh9428 | 0:9bfc4aea91e2 | 41 | |
sbh9428 | 0:9bfc4aea91e2 | 42 | void control_t::control_PWM(float PWM) |
sbh9428 | 0:9bfc4aea91e2 | 43 | { |
sbh9428 | 0:9bfc4aea91e2 | 44 | peltier->set_PWM(PWM); |
sbh9428 | 0:9bfc4aea91e2 | 45 | } |
sbh9428 | 0:9bfc4aea91e2 | 46 | |
sbh9428 | 0:9bfc4aea91e2 | 47 | void control_t::control_temp() |
sbh9428 | 0:9bfc4aea91e2 | 48 | { |
sbh9428 | 1:5c42ec7f1aeb | 49 | PWM_value+=calc_P(); |
sbh9428 | 0:9bfc4aea91e2 | 50 | |
sbh9428 | 1:5c42ec7f1aeb | 51 | PWM_value+=calc_I(); |
sbh9428 | 0:9bfc4aea91e2 | 52 | |
sbh9428 | 1:5c42ec7f1aeb | 53 | PWM_value+=calc_D(); |
sbh9428 | 1:5c42ec7f1aeb | 54 | |
sbh9428 | 1:5c42ec7f1aeb | 55 | if(PWM_value>1) |
sbh9428 | 1:5c42ec7f1aeb | 56 | { |
sbh9428 | 1:5c42ec7f1aeb | 57 | PWM_value=1; |
sbh9428 | 1:5c42ec7f1aeb | 58 | } |
sbh9428 | 1:5c42ec7f1aeb | 59 | if(PWM_value<-1) |
sbh9428 | 1:5c42ec7f1aeb | 60 | { |
sbh9428 | 1:5c42ec7f1aeb | 61 | PWM_value=-1; |
sbh9428 | 1:5c42ec7f1aeb | 62 | } |
sbh9428 | 1:5c42ec7f1aeb | 63 | peltier->set_PWM(PWM_value); |
sbh9428 | 0:9bfc4aea91e2 | 64 | } |
sbh9428 | 0:9bfc4aea91e2 | 65 | |
sbh9428 | 0:9bfc4aea91e2 | 66 | void control_t::set_mode(int _mode) |
sbh9428 | 0:9bfc4aea91e2 | 67 | { |
sbh9428 | 0:9bfc4aea91e2 | 68 | mode=_mode; |
sbh9428 | 4:7ca449fca19b | 69 | if(mode==4) |
sbh9428 | 6:c2fb5c188e8a | 70 | { |
sbh9428 | 6:c2fb5c188e8a | 71 | start_temp=temp_sensor->get_temp(); |
sbh9428 | 6:c2fb5c188e8a | 72 | time=0; |
sbh9428 | 6:c2fb5c188e8a | 73 | } |
sbh9428 | 1:5c42ec7f1aeb | 74 | } |
sbh9428 | 1:5c42ec7f1aeb | 75 | |
sbh9428 | 1:5c42ec7f1aeb | 76 | void control_t::set_PWM_value(float _PWM_value) |
sbh9428 | 1:5c42ec7f1aeb | 77 | { |
sbh9428 | 1:5c42ec7f1aeb | 78 | PWM_value=_PWM_value; |
sbh9428 | 1:5c42ec7f1aeb | 79 | } |
sbh9428 | 1:5c42ec7f1aeb | 80 | |
sbh9428 | 1:5c42ec7f1aeb | 81 | void control_t::set_target_temp(float _target_temp) |
sbh9428 | 1:5c42ec7f1aeb | 82 | { |
sbh9428 | 1:5c42ec7f1aeb | 83 | target_temp=_target_temp; |
sbh9428 | 1:5c42ec7f1aeb | 84 | } |
sbh9428 | 1:5c42ec7f1aeb | 85 | |
sbh9428 | 1:5c42ec7f1aeb | 86 | void control_t::set_P_value(float _P_value) |
sbh9428 | 1:5c42ec7f1aeb | 87 | { |
sbh9428 | 1:5c42ec7f1aeb | 88 | P_value=_P_value; |
sbh9428 | 1:5c42ec7f1aeb | 89 | } |
sbh9428 | 1:5c42ec7f1aeb | 90 | |
sbh9428 | 1:5c42ec7f1aeb | 91 | void control_t::set_I_value(float _I_value) |
sbh9428 | 1:5c42ec7f1aeb | 92 | { |
sbh9428 | 1:5c42ec7f1aeb | 93 | I_value=_I_value; |
sbh9428 | 1:5c42ec7f1aeb | 94 | } |
sbh9428 | 1:5c42ec7f1aeb | 95 | |
sbh9428 | 1:5c42ec7f1aeb | 96 | void control_t::set_D_value(float _D_value) |
sbh9428 | 1:5c42ec7f1aeb | 97 | { |
sbh9428 | 1:5c42ec7f1aeb | 98 | D_value=_D_value; |
sbh9428 | 1:5c42ec7f1aeb | 99 | } |
sbh9428 | 1:5c42ec7f1aeb | 100 | |
sbh9428 | 3:72644690e2e6 | 101 | void control_t::set_period(int _period) |
sbh9428 | 3:72644690e2e6 | 102 | { |
sbh9428 | 3:72644690e2e6 | 103 | period=_period; |
sbh9428 | 3:72644690e2e6 | 104 | } |
sbh9428 | 3:72644690e2e6 | 105 | |
sbh9428 | 4:7ca449fca19b | 106 | void control_t::set_start_temp(float _start_temp) |
sbh9428 | 4:7ca449fca19b | 107 | { |
sbh9428 | 4:7ca449fca19b | 108 | start_temp=_start_temp; |
sbh9428 | 4:7ca449fca19b | 109 | } |
sbh9428 | 4:7ca449fca19b | 110 | |
sbh9428 | 1:5c42ec7f1aeb | 111 | void control_t::refresh_PWM() |
sbh9428 | 1:5c42ec7f1aeb | 112 | { |
sbh9428 | 1:5c42ec7f1aeb | 113 | write_log(); |
sbh9428 | 6:c2fb5c188e8a | 114 | time+=3; |
sbh9428 | 6:c2fb5c188e8a | 115 | printf("\n%f, %f, %f, %1.4f, %2.2f, %d, %d, %d, %2.2f, %2.2f", P_value, I_value, D_value, PWM_value, start_temp, period, time, mode, target_temp, temp_sensor->get_temp()); |
sbh9428 | 1:5c42ec7f1aeb | 116 | if(mode==0) |
sbh9428 | 1:5c42ec7f1aeb | 117 | { |
sbh9428 | 1:5c42ec7f1aeb | 118 | PWM_value=0; |
sbh9428 | 1:5c42ec7f1aeb | 119 | control_PWM(0); |
sbh9428 | 1:5c42ec7f1aeb | 120 | } |
sbh9428 | 1:5c42ec7f1aeb | 121 | else if(mode==1) |
sbh9428 | 1:5c42ec7f1aeb | 122 | { |
sbh9428 | 1:5c42ec7f1aeb | 123 | } |
sbh9428 | 1:5c42ec7f1aeb | 124 | else if(mode==2) |
sbh9428 | 1:5c42ec7f1aeb | 125 | { |
sbh9428 | 1:5c42ec7f1aeb | 126 | control_temp(); |
sbh9428 | 1:5c42ec7f1aeb | 127 | } |
sbh9428 | 2:4c51394fb35b | 128 | else if(mode==3) |
sbh9428 | 2:4c51394fb35b | 129 | { |
sbh9428 | 2:4c51394fb35b | 130 | build_table(); |
sbh9428 | 2:4c51394fb35b | 131 | } |
sbh9428 | 3:72644690e2e6 | 132 | else if(mode==4) |
sbh9428 | 3:72644690e2e6 | 133 | { |
sbh9428 | 3:72644690e2e6 | 134 | follow_table(); |
sbh9428 | 3:72644690e2e6 | 135 | } |
sbh9428 | 1:5c42ec7f1aeb | 136 | } |
sbh9428 | 1:5c42ec7f1aeb | 137 | |
sbh9428 | 1:5c42ec7f1aeb | 138 | int control_t::get_mode() |
sbh9428 | 1:5c42ec7f1aeb | 139 | { |
sbh9428 | 1:5c42ec7f1aeb | 140 | return mode; |
sbh9428 | 1:5c42ec7f1aeb | 141 | } |
sbh9428 | 1:5c42ec7f1aeb | 142 | float control_t::get_target_temp() |
sbh9428 | 1:5c42ec7f1aeb | 143 | { |
sbh9428 | 1:5c42ec7f1aeb | 144 | return target_temp; |
sbh9428 | 1:5c42ec7f1aeb | 145 | } |
sbh9428 | 1:5c42ec7f1aeb | 146 | float control_t::get_P_value() |
sbh9428 | 1:5c42ec7f1aeb | 147 | { |
sbh9428 | 1:5c42ec7f1aeb | 148 | return P_value; |
sbh9428 | 1:5c42ec7f1aeb | 149 | } |
sbh9428 | 1:5c42ec7f1aeb | 150 | float control_t::get_I_value() |
sbh9428 | 1:5c42ec7f1aeb | 151 | { |
sbh9428 | 1:5c42ec7f1aeb | 152 | return I_value; |
sbh9428 | 1:5c42ec7f1aeb | 153 | } |
sbh9428 | 1:5c42ec7f1aeb | 154 | float control_t::get_D_value() |
sbh9428 | 1:5c42ec7f1aeb | 155 | { |
sbh9428 | 1:5c42ec7f1aeb | 156 | return D_value; |
sbh9428 | 1:5c42ec7f1aeb | 157 | } |
sbh9428 | 1:5c42ec7f1aeb | 158 | float control_t::get_PWM_value() |
sbh9428 | 1:5c42ec7f1aeb | 159 | { |
sbh9428 | 1:5c42ec7f1aeb | 160 | return PWM_value; |
sbh9428 | 1:5c42ec7f1aeb | 161 | } |
sbh9428 | 1:5c42ec7f1aeb | 162 | |
sbh9428 | 3:72644690e2e6 | 163 | int control_t::get_period() |
sbh9428 | 3:72644690e2e6 | 164 | { |
sbh9428 | 3:72644690e2e6 | 165 | return period; |
sbh9428 | 3:72644690e2e6 | 166 | } |
sbh9428 | 3:72644690e2e6 | 167 | |
sbh9428 | 2:4c51394fb35b | 168 | int control_t::get_table_count() |
sbh9428 | 2:4c51394fb35b | 169 | { |
sbh9428 | 2:4c51394fb35b | 170 | return table_count; |
sbh9428 | 2:4c51394fb35b | 171 | } |
sbh9428 | 2:4c51394fb35b | 172 | |
sbh9428 | 1:5c42ec7f1aeb | 173 | float control_t::calc_P() |
sbh9428 | 1:5c42ec7f1aeb | 174 | { |
sbh9428 | 1:5c42ec7f1aeb | 175 | return (target_temp-temp_sensor->get_temp())*P_value; |
sbh9428 | 1:5c42ec7f1aeb | 176 | } |
sbh9428 | 1:5c42ec7f1aeb | 177 | |
sbh9428 | 1:5c42ec7f1aeb | 178 | float control_t::calc_I() |
sbh9428 | 1:5c42ec7f1aeb | 179 | { |
sbh9428 | 1:5c42ec7f1aeb | 180 | float data=0; |
sbh9428 | 1:5c42ec7f1aeb | 181 | for(int i=0;i<10;i++) |
sbh9428 | 1:5c42ec7f1aeb | 182 | { |
sbh9428 | 2:4c51394fb35b | 183 | data+=PWM_log[i]; |
sbh9428 | 1:5c42ec7f1aeb | 184 | } |
sbh9428 | 1:5c42ec7f1aeb | 185 | return data*I_value; |
sbh9428 | 1:5c42ec7f1aeb | 186 | } |
sbh9428 | 1:5c42ec7f1aeb | 187 | |
sbh9428 | 1:5c42ec7f1aeb | 188 | float control_t::calc_D() |
sbh9428 | 1:5c42ec7f1aeb | 189 | { |
sbh9428 | 1:5c42ec7f1aeb | 190 | return -(temp_log[log_count]-temp_log[(log_count-1)%10])*D_value; |
sbh9428 | 1:5c42ec7f1aeb | 191 | } |
sbh9428 | 1:5c42ec7f1aeb | 192 | |
sbh9428 | 3:72644690e2e6 | 193 | int control_t::get_table_check() |
sbh9428 | 3:72644690e2e6 | 194 | { |
sbh9428 | 3:72644690e2e6 | 195 | return table_check; |
sbh9428 | 3:72644690e2e6 | 196 | } |
sbh9428 | 3:72644690e2e6 | 197 | |
sbh9428 | 1:5c42ec7f1aeb | 198 | void control_t::write_log() |
sbh9428 | 1:5c42ec7f1aeb | 199 | { |
sbh9428 | 1:5c42ec7f1aeb | 200 | log_count++; |
sbh9428 | 1:5c42ec7f1aeb | 201 | log_count=log_count%10; |
sbh9428 | 1:5c42ec7f1aeb | 202 | temp_log[log_count]=temp_sensor->get_temp(); |
sbh9428 | 2:4c51394fb35b | 203 | PWM_log[log_count]=target_temp-temp_sensor->get_temp(); |
sbh9428 | 2:4c51394fb35b | 204 | } |
sbh9428 | 2:4c51394fb35b | 205 | |
sbh9428 | 2:4c51394fb35b | 206 | void control_t::build_table() |
sbh9428 | 2:4c51394fb35b | 207 | { |
sbh9428 | 3:72644690e2e6 | 208 | table_check=1; |
sbh9428 | 2:4c51394fb35b | 209 | if(table_mode==0) |
sbh9428 | 2:4c51394fb35b | 210 | { |
sbh9428 | 2:4c51394fb35b | 211 | peltier->set_PWM(-1); |
sbh9428 | 2:4c51394fb35b | 212 | table_count++; |
sbh9428 | 6:c2fb5c188e8a | 213 | pc->printf(",set initial temp %d/30", table_count); |
sbh9428 | 3:72644690e2e6 | 214 | if(table_count>29) |
sbh9428 | 2:4c51394fb35b | 215 | { |
sbh9428 | 2:4c51394fb35b | 216 | table_mode=1; |
sbh9428 | 2:4c51394fb35b | 217 | table_count=0; |
sbh9428 | 6:c2fb5c188e8a | 218 | time=0; |
sbh9428 | 2:4c51394fb35b | 219 | } |
sbh9428 | 3:72644690e2e6 | 220 | |
sbh9428 | 2:4c51394fb35b | 221 | } |
sbh9428 | 2:4c51394fb35b | 222 | else |
sbh9428 | 2:4c51394fb35b | 223 | { |
sbh9428 | 2:4c51394fb35b | 224 | table[table_count]=temp_sensor->get_temp(); |
sbh9428 | 2:4c51394fb35b | 225 | table_count++; |
sbh9428 | 5:8e3b5ccf7207 | 226 | PWM_value=(float)-1+0.005*table_count; |
sbh9428 | 5:8e3b5ccf7207 | 227 | peltier->set_PWM(PWM_value); |
sbh9428 | 2:4c51394fb35b | 228 | if(table_count>=200) |
sbh9428 | 3:72644690e2e6 | 229 | { |
sbh9428 | 3:72644690e2e6 | 230 | table_count=0; |
sbh9428 | 3:72644690e2e6 | 231 | mode=0; |
sbh9428 | 3:72644690e2e6 | 232 | table_min=table[0]; |
sbh9428 | 3:72644690e2e6 | 233 | table_max=table[199]; |
sbh9428 | 3:72644690e2e6 | 234 | } |
sbh9428 | 6:c2fb5c188e8a | 235 | pc->printf(",build table %d/200", table_count); |
sbh9428 | 2:4c51394fb35b | 236 | } |
sbh9428 | 2:4c51394fb35b | 237 | } |
sbh9428 | 2:4c51394fb35b | 238 | |
sbh9428 | 2:4c51394fb35b | 239 | void control_t::print_table() |
sbh9428 | 2:4c51394fb35b | 240 | { |
sbh9428 | 2:4c51394fb35b | 241 | int i; |
sbh9428 | 2:4c51394fb35b | 242 | for(i=0;i<201;i++) |
sbh9428 | 2:4c51394fb35b | 243 | { |
sbh9428 | 6:c2fb5c188e8a | 244 | pc->printf(",%d/200 PWM: %1.3f, temp:%2.2f",i, (-1+0.005*i), table[i]); |
sbh9428 | 3:72644690e2e6 | 245 | wait_us(2000); |
sbh9428 | 3:72644690e2e6 | 246 | } |
sbh9428 | 3:72644690e2e6 | 247 | } |
sbh9428 | 3:72644690e2e6 | 248 | |
sbh9428 | 3:72644690e2e6 | 249 | float control_t::find_table(float _temp) |
sbh9428 | 3:72644690e2e6 | 250 | { |
sbh9428 | 3:72644690e2e6 | 251 | int i; |
sbh9428 | 3:72644690e2e6 | 252 | |
sbh9428 | 3:72644690e2e6 | 253 | for (i=0; i<200;i++) |
sbh9428 | 3:72644690e2e6 | 254 | { |
sbh9428 | 3:72644690e2e6 | 255 | if (table[i+1]>_temp) |
sbh9428 | 3:72644690e2e6 | 256 | return -1+0.005*i; |
sbh9428 | 2:4c51394fb35b | 257 | } |
sbh9428 | 3:72644690e2e6 | 258 | return 0; |
sbh9428 | 3:72644690e2e6 | 259 | } |
sbh9428 | 3:72644690e2e6 | 260 | |
sbh9428 | 3:72644690e2e6 | 261 | void control_t::follow_table() |
sbh9428 | 3:72644690e2e6 | 262 | { |
sbh9428 | 3:72644690e2e6 | 263 | if(table_check!=1) |
sbh9428 | 3:72644690e2e6 | 264 | { |
sbh9428 | 3:72644690e2e6 | 265 | mode=0; |
sbh9428 | 6:c2fb5c188e8a | 266 | pc->printf(",table not built\n"); |
sbh9428 | 3:72644690e2e6 | 267 | } |
sbh9428 | 3:72644690e2e6 | 268 | else if(target_temp>table_max|target_temp<table_min) |
sbh9428 | 3:72644690e2e6 | 269 | { |
sbh9428 | 3:72644690e2e6 | 270 | mode=0; |
sbh9428 | 6:c2fb5c188e8a | 271 | pc->printf(",target temp out of range"); |
sbh9428 | 3:72644690e2e6 | 272 | } |
sbh9428 | 3:72644690e2e6 | 273 | else if(period<=step) |
sbh9428 | 3:72644690e2e6 | 274 | { |
sbh9428 | 6:c2fb5c188e8a | 275 | pc->printf(",table follow end"); |
sbh9428 | 3:72644690e2e6 | 276 | step=0; |
sbh9428 | 3:72644690e2e6 | 277 | mode=2; |
sbh9428 | 3:72644690e2e6 | 278 | } |
sbh9428 | 3:72644690e2e6 | 279 | else |
sbh9428 | 3:72644690e2e6 | 280 | { |
sbh9428 | 4:7ca449fca19b | 281 | PWM_value=find_table(target_temp/period*step+start_temp/period*(period-step)); |
sbh9428 | 3:72644690e2e6 | 282 | peltier->set_PWM(PWM_value); |
sbh9428 | 6:c2fb5c188e8a | 283 | printf(",remain step: %d",period-step); |
sbh9428 | 3:72644690e2e6 | 284 | step++; |
sbh9428 | 3:72644690e2e6 | 285 | } |
sbh9428 | 3:72644690e2e6 | 286 | } |