dd

Dependencies:   BufferedSerial FastAnalogIn FastPWM mbed SHT75

Revision:
3:72644690e2e6
Parent:
2:4c51394fb35b
Child:
4:7ca449fca19b
--- 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