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