Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSerial FastAnalogIn FastPWM mbed SHT75
Revision 1:5c42ec7f1aeb, committed 2016-02-22
- Comitter:
- sbh9428
- Date:
- Mon Feb 22 06:03:11 2016 +0000
- Parent:
- 0:9bfc4aea91e2
- Child:
- 2:4c51394fb35b
- Commit message:
- PID?;
Changed in this revision
--- a/commandt.cpp Fri Feb 19 07:07:26 2016 +0000 +++ b/commandt.cpp Mon Feb 22 06:03:11 2016 +0000 @@ -14,6 +14,7 @@ command_t::command_t(BufferedSerial* _pc, control_t* _control) { + time=0; count=0; control=_control; pc=_pc; @@ -40,23 +41,60 @@ { case 'm': control->set_mode(data[2]-'0'); + printf("set mode %d\n", data[2]-'0'); break; - case 'p': + case 'r': control->control_PWM(asci_to_bin(data+2)); - printf("set WPM duty ratio %f\n", asci_to_bin(data+2)); + control->set_PWM_value(asci_to_bin(data+2)); + printf("set PWM duty ratio %f1.5\n", asci_to_bin(data+2)); break; case 't': - + control->set_target_temp(asci_to_bin(data+2)); + printf("set target temp %f\n", asci_to_bin(data+2)); + break; + case 'p': + control->set_P_value(asci_to_bin(data+2)); + printf("set P value %f\n", asci_to_bin(data+2)); + break; + case 'i': + control->set_I_value(asci_to_bin(data+2)); + printf("set I value %f\n", asci_to_bin(data+2)); + break; + case 'd': + control->set_D_value(asci_to_bin(data+2)); + printf("set D value %f\n", asci_to_bin(data+2)); break; } break; case 'g': + switch(data[1]) + { + case 'm': + printf("mode is %d\n", control->get_mode()); + break; + case 'r': + printf("duty ratio is %f\n", control->get_PWM_value()); + break; + case 't': + printf("target temp is %f\n", control->get_target_temp()); + break; + case 'p': + printf("P value is %f\n", control->get_P_value()); + break; + case 'i': + printf("I value is %f\n", control->get_I_value()); + break; + case 'd': + printf("D value is %f\n", control->get_P_value()); + break; + } break; dafault: pc->printf("command error"); } pc->printf("\n"); + count=0; } void command_t::get_data() @@ -77,8 +115,9 @@ void command_t::refreshPWM() { - printf("temperature is %f\n", control->get_temp()); - + printf(",%2.2f\n", control->get_temp()); + time=time+3; + control->refresh_PWM(); } double command_t::asci_to_bin(int *start)
--- a/commandt.h Fri Feb 19 07:07:26 2016 +0000 +++ b/commandt.h Mon Feb 22 06:03:11 2016 +0000 @@ -24,7 +24,8 @@ virtual ~command_t(); private: int count; - int data[10]; + int data[30]; + int time; BufferedSerial* pc; control_t *control;
--- a/controlt.cpp Fri Feb 19 07:07:26 2016 +0000 +++ b/controlt.cpp Mon Feb 22 06:03:11 2016 +0000 @@ -14,6 +14,13 @@ control_t::control_t(temp_sensor_t* _temp_sensor, peltier_t* _peltier) { + log_count=0; + + target_temp=15; + P_value=0; + I_value=0; + D_value=0; + temp_sensor=_temp_sensor; peltier=_peltier; } @@ -34,11 +41,118 @@ void control_t::control_temp() { + PWM_value+=calc_P(); + PWM_value+=calc_I(); + PWM_value+=calc_D(); + + if(PWM_value>1) + { + PWM_value=1; + } + if(PWM_value<-1) + { + PWM_value=-1; + } + peltier->set_PWM(PWM_value); } void control_t::set_mode(int _mode) { mode=_mode; +} + +void control_t::set_PWM_value(float _PWM_value) +{ + PWM_value=_PWM_value; +} + +void control_t::set_target_temp(float _target_temp) +{ + target_temp=_target_temp; +} + +void control_t::set_P_value(float _P_value) +{ + P_value=_P_value; +} + +void control_t::set_I_value(float _I_value) +{ + I_value=_I_value; +} + +void control_t::set_D_value(float _D_value) +{ + D_value=_D_value; +} + +void control_t::refresh_PWM() +{ + write_log(); + if(mode==0) + { + PWM_value=0; + control_PWM(0); + } + else if(mode==1) + { + } + else if(mode==2) + { + control_temp(); + } +} + +int control_t::get_mode() +{ + return mode; +} +float control_t::get_target_temp() +{ + return target_temp; +} +float control_t::get_P_value() +{ + return P_value; +} +float control_t::get_I_value() +{ + return I_value; +} +float control_t::get_D_value() +{ + return D_value; +} +float control_t::get_PWM_value() +{ + return PWM_value; +} + +float control_t::calc_P() +{ + return (target_temp-temp_sensor->get_temp())*P_value; +} + +float control_t::calc_I() +{ + float data=0; + for(int i=0;i<10;i++) + { + data+=temp_log[i]; + } + return data*I_value; +} + +float control_t::calc_D() +{ + return -(temp_log[log_count]-temp_log[(log_count-1)%10])*D_value; +} + +void control_t::write_log() +{ + log_count++; + log_count=log_count%10; + temp_log[log_count]=temp_sensor->get_temp(); } \ No newline at end of file
--- a/controlt.h Fri Feb 19 07:07:26 2016 +0000 +++ b/controlt.h Mon Feb 22 06:03:11 2016 +0000 @@ -14,14 +14,53 @@ class control_t { public: float get_temp(); - void control_PWM(float PWM); + void control_PWM(float _PWM); void control_temp(); + void set_mode(int _mode); - + void set_target_temp(float _target_temp); + void set_PWM_value(float _PWM_value); + void set_P_value(float _P_value); + void set_I_value(float _I_value); + void set_D_value(float _D_value); + + void refresh_PWM(); + + int get_mode(); + float get_target_temp(); + float get_P_value(); + float get_I_value(); + float get_D_value(); + float get_PWM_value(); + + float calc_P(); + float calc_I(); + float calc_D(); + + void write_log(); + control_t(); control_t(temp_sensor_t* _temp_sensor, peltier_t* _peltier); virtual ~control_t(); private: + float P_value; + float I_value; + float D_value; + + float PWM_value; + + float target_temp; + + float dif; + + float temp_log[10]; + int log_count; + + float table[201]; + int table_count; + int table_min; + int table_max; + temp_sensor_t* temp_sensor; peltier_t* peltier;
--- a/main.cpp Fri Feb 19 07:07:26 2016 +0000 +++ b/main.cpp Mon Feb 22 06:03:11 2016 +0000 @@ -11,15 +11,17 @@ Ticker controltick; -FastAnalogIn temp_sensor_pin(p20); +AnalogIn temp_sensor_pin(p19); temp_sensor_t temp_sensor(&temp_sensor_pin); -FastPWM PWM_pin(p21); +FastPWM PWM_pin(p23); DigitalOut PWM_dir(p22); + + peltier_t peltier(&PWM_pin, &PWM_dir); @@ -40,11 +42,12 @@ void peltier_control(); int main() { + pc.printf("temperature start \n"); - controltick.attach(&peltier_control, 1); + controltick.attach(&peltier_control, 10); - + wait(10); while(1) { @@ -58,5 +61,5 @@ void peltier_control() { - command.refreshPWM(); + command.refreshPWM(); } \ No newline at end of file
--- a/tempsensort.cpp Fri Feb 19 07:07:26 2016 +0000 +++ b/tempsensort.cpp Mon Feb 22 06:03:11 2016 +0000 @@ -12,10 +12,9 @@ } -temp_sensor_t::temp_sensor_t(FastAnalogIn* _temperature_sensor) +temp_sensor_t::temp_sensor_t(AnalogIn* _temperature_sensor) { temperature_sensor=_temperature_sensor; - temperature_sensor->enable(1); } @@ -25,5 +24,5 @@ float temp_sensor_t::get_temp() { - return temperature_sensor->read()*165-40; + return (double)temperature_sensor->read_u16()/1024/64*165-40; }
--- a/tempsensort.h Fri Feb 19 07:07:26 2016 +0000 +++ b/tempsensort.h Mon Feb 22 06:03:11 2016 +0000 @@ -15,10 +15,10 @@ float get_temp(); temp_sensor_t(); - temp_sensor_t(FastAnalogIn* _temperature_sensor); + temp_sensor_t(AnalogIn* _temperature_sensor); virtual ~temp_sensor_t(); private: - FastAnalogIn* temperature_sensor; + AnalogIn* temperature_sensor; }; #endif /* TEMPSENSOTT_H_ */