dd
Dependencies: BufferedSerial FastAnalogIn FastPWM mbed SHT75
controlt.cpp@1:5c42ec7f1aeb, 2016-02-22 (annotated)
- Committer:
- sbh9428
- Date:
- Mon Feb 22 06:03:11 2016 +0000
- Revision:
- 1:5c42ec7f1aeb
- Parent:
- 0:9bfc4aea91e2
- Child:
- 2:4c51394fb35b
PID?;
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 | 0:9bfc4aea91e2 | 15 | control_t::control_t(temp_sensor_t* _temp_sensor, peltier_t* _peltier) |
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 | 0:9bfc4aea91e2 | 26 | } |
sbh9428 | 0:9bfc4aea91e2 | 27 | |
sbh9428 | 0:9bfc4aea91e2 | 28 | control_t::~control_t() { |
sbh9428 | 0:9bfc4aea91e2 | 29 | // TODO Auto-generated destructor stub |
sbh9428 | 0:9bfc4aea91e2 | 30 | } |
sbh9428 | 0:9bfc4aea91e2 | 31 | |
sbh9428 | 0:9bfc4aea91e2 | 32 | float control_t::get_temp() |
sbh9428 | 0:9bfc4aea91e2 | 33 | { |
sbh9428 | 0:9bfc4aea91e2 | 34 | return temp_sensor->get_temp(); |
sbh9428 | 0:9bfc4aea91e2 | 35 | } |
sbh9428 | 0:9bfc4aea91e2 | 36 | |
sbh9428 | 0:9bfc4aea91e2 | 37 | void control_t::control_PWM(float PWM) |
sbh9428 | 0:9bfc4aea91e2 | 38 | { |
sbh9428 | 0:9bfc4aea91e2 | 39 | peltier->set_PWM(PWM); |
sbh9428 | 0:9bfc4aea91e2 | 40 | } |
sbh9428 | 0:9bfc4aea91e2 | 41 | |
sbh9428 | 0:9bfc4aea91e2 | 42 | void control_t::control_temp() |
sbh9428 | 0:9bfc4aea91e2 | 43 | { |
sbh9428 | 1:5c42ec7f1aeb | 44 | PWM_value+=calc_P(); |
sbh9428 | 0:9bfc4aea91e2 | 45 | |
sbh9428 | 1:5c42ec7f1aeb | 46 | PWM_value+=calc_I(); |
sbh9428 | 0:9bfc4aea91e2 | 47 | |
sbh9428 | 1:5c42ec7f1aeb | 48 | PWM_value+=calc_D(); |
sbh9428 | 1:5c42ec7f1aeb | 49 | |
sbh9428 | 1:5c42ec7f1aeb | 50 | if(PWM_value>1) |
sbh9428 | 1:5c42ec7f1aeb | 51 | { |
sbh9428 | 1:5c42ec7f1aeb | 52 | PWM_value=1; |
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 | peltier->set_PWM(PWM_value); |
sbh9428 | 0:9bfc4aea91e2 | 59 | } |
sbh9428 | 0:9bfc4aea91e2 | 60 | |
sbh9428 | 0:9bfc4aea91e2 | 61 | void control_t::set_mode(int _mode) |
sbh9428 | 0:9bfc4aea91e2 | 62 | { |
sbh9428 | 0:9bfc4aea91e2 | 63 | mode=_mode; |
sbh9428 | 1:5c42ec7f1aeb | 64 | } |
sbh9428 | 1:5c42ec7f1aeb | 65 | |
sbh9428 | 1:5c42ec7f1aeb | 66 | void control_t::set_PWM_value(float _PWM_value) |
sbh9428 | 1:5c42ec7f1aeb | 67 | { |
sbh9428 | 1:5c42ec7f1aeb | 68 | PWM_value=_PWM_value; |
sbh9428 | 1:5c42ec7f1aeb | 69 | } |
sbh9428 | 1:5c42ec7f1aeb | 70 | |
sbh9428 | 1:5c42ec7f1aeb | 71 | void control_t::set_target_temp(float _target_temp) |
sbh9428 | 1:5c42ec7f1aeb | 72 | { |
sbh9428 | 1:5c42ec7f1aeb | 73 | target_temp=_target_temp; |
sbh9428 | 1:5c42ec7f1aeb | 74 | } |
sbh9428 | 1:5c42ec7f1aeb | 75 | |
sbh9428 | 1:5c42ec7f1aeb | 76 | void control_t::set_P_value(float _P_value) |
sbh9428 | 1:5c42ec7f1aeb | 77 | { |
sbh9428 | 1:5c42ec7f1aeb | 78 | P_value=_P_value; |
sbh9428 | 1:5c42ec7f1aeb | 79 | } |
sbh9428 | 1:5c42ec7f1aeb | 80 | |
sbh9428 | 1:5c42ec7f1aeb | 81 | void control_t::set_I_value(float _I_value) |
sbh9428 | 1:5c42ec7f1aeb | 82 | { |
sbh9428 | 1:5c42ec7f1aeb | 83 | I_value=_I_value; |
sbh9428 | 1:5c42ec7f1aeb | 84 | } |
sbh9428 | 1:5c42ec7f1aeb | 85 | |
sbh9428 | 1:5c42ec7f1aeb | 86 | void control_t::set_D_value(float _D_value) |
sbh9428 | 1:5c42ec7f1aeb | 87 | { |
sbh9428 | 1:5c42ec7f1aeb | 88 | D_value=_D_value; |
sbh9428 | 1:5c42ec7f1aeb | 89 | } |
sbh9428 | 1:5c42ec7f1aeb | 90 | |
sbh9428 | 1:5c42ec7f1aeb | 91 | void control_t::refresh_PWM() |
sbh9428 | 1:5c42ec7f1aeb | 92 | { |
sbh9428 | 1:5c42ec7f1aeb | 93 | write_log(); |
sbh9428 | 1:5c42ec7f1aeb | 94 | if(mode==0) |
sbh9428 | 1:5c42ec7f1aeb | 95 | { |
sbh9428 | 1:5c42ec7f1aeb | 96 | PWM_value=0; |
sbh9428 | 1:5c42ec7f1aeb | 97 | control_PWM(0); |
sbh9428 | 1:5c42ec7f1aeb | 98 | } |
sbh9428 | 1:5c42ec7f1aeb | 99 | else if(mode==1) |
sbh9428 | 1:5c42ec7f1aeb | 100 | { |
sbh9428 | 1:5c42ec7f1aeb | 101 | } |
sbh9428 | 1:5c42ec7f1aeb | 102 | else if(mode==2) |
sbh9428 | 1:5c42ec7f1aeb | 103 | { |
sbh9428 | 1:5c42ec7f1aeb | 104 | control_temp(); |
sbh9428 | 1:5c42ec7f1aeb | 105 | } |
sbh9428 | 1:5c42ec7f1aeb | 106 | } |
sbh9428 | 1:5c42ec7f1aeb | 107 | |
sbh9428 | 1:5c42ec7f1aeb | 108 | int control_t::get_mode() |
sbh9428 | 1:5c42ec7f1aeb | 109 | { |
sbh9428 | 1:5c42ec7f1aeb | 110 | return mode; |
sbh9428 | 1:5c42ec7f1aeb | 111 | } |
sbh9428 | 1:5c42ec7f1aeb | 112 | float control_t::get_target_temp() |
sbh9428 | 1:5c42ec7f1aeb | 113 | { |
sbh9428 | 1:5c42ec7f1aeb | 114 | return target_temp; |
sbh9428 | 1:5c42ec7f1aeb | 115 | } |
sbh9428 | 1:5c42ec7f1aeb | 116 | float control_t::get_P_value() |
sbh9428 | 1:5c42ec7f1aeb | 117 | { |
sbh9428 | 1:5c42ec7f1aeb | 118 | return P_value; |
sbh9428 | 1:5c42ec7f1aeb | 119 | } |
sbh9428 | 1:5c42ec7f1aeb | 120 | float control_t::get_I_value() |
sbh9428 | 1:5c42ec7f1aeb | 121 | { |
sbh9428 | 1:5c42ec7f1aeb | 122 | return I_value; |
sbh9428 | 1:5c42ec7f1aeb | 123 | } |
sbh9428 | 1:5c42ec7f1aeb | 124 | float control_t::get_D_value() |
sbh9428 | 1:5c42ec7f1aeb | 125 | { |
sbh9428 | 1:5c42ec7f1aeb | 126 | return D_value; |
sbh9428 | 1:5c42ec7f1aeb | 127 | } |
sbh9428 | 1:5c42ec7f1aeb | 128 | float control_t::get_PWM_value() |
sbh9428 | 1:5c42ec7f1aeb | 129 | { |
sbh9428 | 1:5c42ec7f1aeb | 130 | return PWM_value; |
sbh9428 | 1:5c42ec7f1aeb | 131 | } |
sbh9428 | 1:5c42ec7f1aeb | 132 | |
sbh9428 | 1:5c42ec7f1aeb | 133 | float control_t::calc_P() |
sbh9428 | 1:5c42ec7f1aeb | 134 | { |
sbh9428 | 1:5c42ec7f1aeb | 135 | return (target_temp-temp_sensor->get_temp())*P_value; |
sbh9428 | 1:5c42ec7f1aeb | 136 | } |
sbh9428 | 1:5c42ec7f1aeb | 137 | |
sbh9428 | 1:5c42ec7f1aeb | 138 | float control_t::calc_I() |
sbh9428 | 1:5c42ec7f1aeb | 139 | { |
sbh9428 | 1:5c42ec7f1aeb | 140 | float data=0; |
sbh9428 | 1:5c42ec7f1aeb | 141 | for(int i=0;i<10;i++) |
sbh9428 | 1:5c42ec7f1aeb | 142 | { |
sbh9428 | 1:5c42ec7f1aeb | 143 | data+=temp_log[i]; |
sbh9428 | 1:5c42ec7f1aeb | 144 | } |
sbh9428 | 1:5c42ec7f1aeb | 145 | return data*I_value; |
sbh9428 | 1:5c42ec7f1aeb | 146 | } |
sbh9428 | 1:5c42ec7f1aeb | 147 | |
sbh9428 | 1:5c42ec7f1aeb | 148 | float control_t::calc_D() |
sbh9428 | 1:5c42ec7f1aeb | 149 | { |
sbh9428 | 1:5c42ec7f1aeb | 150 | return -(temp_log[log_count]-temp_log[(log_count-1)%10])*D_value; |
sbh9428 | 1:5c42ec7f1aeb | 151 | } |
sbh9428 | 1:5c42ec7f1aeb | 152 | |
sbh9428 | 1:5c42ec7f1aeb | 153 | void control_t::write_log() |
sbh9428 | 1:5c42ec7f1aeb | 154 | { |
sbh9428 | 1:5c42ec7f1aeb | 155 | log_count++; |
sbh9428 | 1:5c42ec7f1aeb | 156 | log_count=log_count%10; |
sbh9428 | 1:5c42ec7f1aeb | 157 | temp_log[log_count]=temp_sensor->get_temp(); |
sbh9428 | 0:9bfc4aea91e2 | 158 | } |