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