dd
Dependencies: BufferedSerial FastAnalogIn FastPWM mbed SHT75
Revision 3:72644690e2e6, committed 2016-03-25
- Comitter:
- sbh9428
- Date:
- Fri Mar 25 00:10:39 2016 +0000
- Parent:
- 2:4c51394fb35b
- Child:
- 4:7ca449fca19b
- Commit message:
- 00;
Changed in this revision
--- a/commandt.cpp Wed Mar 02 16:33:23 2016 +0000 +++ b/commandt.cpp Fri Mar 25 00:10:39 2016 +0000 @@ -64,6 +64,12 @@ control->set_D_value(asci_to_bin(data+2)); printf("set D value %f\n", asci_to_bin(data+2)); break; + case 'e': + control->set_period((int)asci_to_bin(data+2)); + printf("set period %d\n", (int)asci_to_bin(data+2)); + break; + default: + pc->printf("command error"); } break; case 'g': @@ -87,6 +93,14 @@ case 'd': printf("D value is %f\n", control->get_P_value()); break; + case 'e': + printf("Period is %d\n", control->get_period()); + break; + case 'a': + control->print_table(); + break; + default: + pc->printf("command error"); } break;
--- a/controlt.cpp Wed Mar 02 16:33:23 2016 +0000 +++ b/controlt.cpp Fri Mar 25 00:10:39 2016 +0000 @@ -92,6 +92,11 @@ D_value=_D_value; } +void control_t::set_period(int _period) +{ + period=_period; +} + void control_t::refresh_PWM() { write_log(); @@ -111,6 +116,10 @@ { build_table(); } + else if(mode==4) + { + follow_table(); + } } int control_t::get_mode() @@ -138,6 +147,11 @@ return PWM_value; } +int control_t::get_period() +{ + return period; +} + int control_t::get_table_count() { return table_count; @@ -163,6 +177,11 @@ return -(temp_log[log_count]-temp_log[(log_count-1)%10])*D_value; } +int control_t::get_table_check() +{ + return table_check; +} + void control_t::write_log() { log_count++; @@ -173,16 +192,18 @@ void control_t::build_table() { + table_check=1; if(table_mode==0) { peltier->set_PWM(-1); table_count++; - if(table_count>50) + pc->printf("set initial temp %d/30", table_count); + if(table_count>29) { table_mode=1; table_count=0; } - pc->printf("set initial temp %d/50 PWM is %f\n", table_count); + } else { @@ -190,8 +211,13 @@ table_count++; peltier->set_PWM((float)-1+0.005*table_count); if(table_count>=200) - mode=0; - pc->printf("build table %d/200, temp is %2.2f", table_count, temp_sensor->get_temp()); + { + table_count=0; + mode=0; + table_min=table[0]; + table_max=table[199]; + } + pc->printf("build table %d/200, PWM, temp is %1.3f", table_count,-1+0.005*table_count); } } @@ -200,6 +226,46 @@ int i; for(i=0;i<201;i++) { - pc->printf("PWM: %3.1f, temp is %2.2f\n", (-1+0.005*i), table[i]); + pc->printf("%d/200 PWM: %1.3f, temp:%2.2f\n",i, (-1+0.005*i), table[i]); + wait_us(2000); + } +} + +float control_t::find_table(float _temp) +{ + int i; + + for (i=0; i<200;i++) + { + if (table[i+1]>_temp) + return -1+0.005*i; } -} \ No newline at end of file + return 0; +} + +void control_t::follow_table() +{ + if(table_check!=1) + { + mode=0; + pc->printf("table not built\n"); + } + else if(target_temp>table_max|target_temp<table_min) + { + mode=0; + pc->printf("target temp out of range\n"); + } + else if(period<=step) + { + pc->printf("table follow end\n"); + step=0; + mode=2; + } + else + { + PWM_value=find_table(target_temp/period*step+get_temp()/period*(period-step)); + peltier->set_PWM(PWM_value); + printf("remain step, temp: %d",period-step); + step++; + } +} \ No newline at end of file
--- a/controlt.h Wed Mar 02 16:33:23 2016 +0000 +++ b/controlt.h Fri Mar 25 00:10:39 2016 +0000 @@ -24,6 +24,7 @@ void set_P_value(float _P_value); void set_I_value(float _I_value); void set_D_value(float _D_value); + void set_period(int _period); void refresh_PWM(); @@ -33,6 +34,8 @@ float get_I_value(); float get_D_value(); float get_PWM_value(); + int get_period(); + int get_table_check(); int get_table_count(); float calc_P(); @@ -42,6 +45,7 @@ void write_log(); void print_table(); + void follow_table(); control_t(); control_t(temp_sensor_t* _temp_sensor, peltier_t* _peltier,BufferedSerial* _pc); @@ -64,8 +68,15 @@ float table[201]; int table_count; int table_mode; + int table_check; + float table_min; + float table_max; + + int period; + int step; void build_table(); + float find_table(float _temp); temp_sensor_t* temp_sensor; peltier_t* peltier;
--- a/main.cpp Wed Mar 02 16:33:23 2016 +0000 +++ b/main.cpp Fri Mar 25 00:10:39 2016 +0000 @@ -45,7 +45,7 @@ pc.printf("temperature start \n"); - controltick.attach(&peltier_control, 5); + controltick.attach(&peltier_control, 3);// sampling time wait(10);