main/task_driver.c

changeset 103
1885d0c75c48
parent 102
96e30a3a3980
child 108
9bfd85878edc
--- a/main/task_driver.c	Mon Jun 21 19:04:10 2021 +0200
+++ b/main/task_driver.c	Tue Jun 22 22:21:09 2021 +0200
@@ -64,7 +64,7 @@
 
 /**
  * @brief Calculate and set PWM value.
- * @param percent Then power percentage, 0..100
+ * @param percent The power percentage, 0..100
  */
 void MLT_PWM(int percent);
 
@@ -116,7 +116,6 @@
 }
 
 
-//int oldval = 200;
 void MLT_PWM(int percent) {
     int		val;
     static int	oldval = -1;
@@ -133,10 +132,16 @@
 	val = 0;
     }
 
+    /*
+     * If the Hendi is on, the lowest setting is 500 Watt. So, if we need less then
+     * 10% power, turn it off, just like the manual knob.
+     */
+    MLT((val >= 10) ? 1:0);
+
     if (val != oldval) {
     	ESP_LOGI(TAG, "MLT_PWM(%d) val=%d %.0f watt", percent, val, (percent / 100.0) * equipment.MLT_watt);
-    	ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 1024 - val);
-    	ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
+    	ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 1024 - val);
+    	ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
     }
     oldval = val;
 }
@@ -184,6 +189,7 @@
 {
     TickType_t		wait_ticks, last_tick, now_tick;
     bool		rc;
+    int			SafeCount = 0;
     unsigned long	now, RealTime, w_StartTime = 0;
 
     ESP_LOGI(TAG, "Start drivers");
@@ -200,8 +206,8 @@
 
     // Prepare and then apply the LEDC PWM timer configuration
     ledc_timer_config_t ledc_timer = {
-	.speed_mode = LEDC_LOW_SPEED_MODE,		///< Use low speed
-	.timer_num = LEDC_TIMER_1,
+	.speed_mode = LEDC_HIGH_SPEED_MODE,		///< Use high speed timer
+	.timer_num = LEDC_TIMER_0,			///< Timer 0
 	.duty_resolution = LEDC_TIMER_10_BIT,		///< 10 bits resolution
 	.freq_hz = 100,					///< 100 Hz
 	.clk_cfg = LEDC_AUTO_CLK			///< Auto select PWM clock
@@ -212,10 +218,10 @@
 	.channel    = LEDC_CHANNEL_0,
 	.duty       = 1024,				///< Default 0% (inverted value)
 	.gpio_num   = PWM_MLT,				///< MLT pin
-	.speed_mode = LEDC_LOW_SPEED_MODE,
+	.speed_mode = LEDC_HIGH_SPEED_MODE,
 	.hpoint     = 0,
 	.intr_type  = LEDC_INTR_DISABLE,
-	.timer_sel  = LEDC_TIMER_1
+	.timer_sel  = LEDC_TIMER_0			///< Timer 0
     };
     ledc_channel_config(&pwm_channel);
 
@@ -235,9 +241,6 @@
     driver_state->hlt_and_mlt = false;
     driver_state->pump_gpio = SSR_PUMP;
     driver_state->pump_run = 0;
-    driver_state->pwm_gpio = PWM_MLT;
-    driver_state->pwm_mlt = false;
-    driver_state->pwm_nohlt = 10;	/* Conservative safety value. */
 
     PID(&Input, &Output, &Setpoint, 200, 2.0, 1.5, PID_DIRECT);
 
@@ -318,33 +321,45 @@
 	    int PWMout = (int)((Output * 100) / 255.0);
 
 	    if ((PID_GetMode() == PID_AUTOMATIC) && (MLT_Mode == MLT_MODE_PID)) {
+		/* Mash power limited */
 		if (PWMout > equipment.MashPower)
 		    PWMout = equipment.MashPower;
 	    }
 
 	    /*
-	     * Hendi minimum power is 500 Watt, this is 14%.
-	     * So, we turn the cooker on around 10% power.
+	     * Do not send power values < 10%.
 	     */
 	    if (PWMout >= 10) {	// Hendi minimum power is 500 Watt, this is 10%
 		if ((((PWMout / 100.0) * equipment.MLT_watt) + equipment.HLT_watt) > equipment.Max_watt) {
 		    if (HLT_pin) {
-		    	ESP_LOGI(TAG, "Power %f  %d", ((PWMout / 100.0) * equipment.MLT_watt) + equipment.HLT_watt, equipment.Max_watt);
-		    	ESP_LOGI(TAG, "Immediate HLT panic shutdown");
+		    	ESP_LOGI(TAG, "Current %.0f Watt above %d limit, shutdown HLT",
+					((PWMout / 100.0) * equipment.MLT_watt) + equipment.HLT_watt, equipment.Max_watt);
 		    	HLT_Output = 0;
 		    	HLT(0);		// As soon as possible before the Hendi increases power.
 		    }
-		} else {
-		    AllowHLT();		// TODO: delay this one loop.
+		    SafeCount = (int)(15000 / equipment.SampleTime) + 1; // About 15 seconds release time
+		} else if (rc) {
+		    if (SafeCount > 0) {
+			SafeCount--;
+		    } else {
+			AllowHLT();
+		    }
 		}
 		MLT_PWM(PWMout);
-                MLT(1);
 	    } else {
 		MLT_PWM(0);
-		MLT(0);
-		AllowHLT();
+		if (rc) {
+		    if (SafeCount > 0) {
+                    	SafeCount--;
+        	    } else {
+		    	AllowHLT();
+		    }
+		}
 	    }
 	} else if ((int)((Output / 255.0) * RealTime) > (now - w_StartTime)) {
+	    /*
+	     * Use On/Off kettles using time slices.
+	     */
 	    MLT(1);
 	    if ((equipment.SSR2 == SSR2_HLT_SHARE) || (equipment.SSR2 == SSR2_ON_IDLE)) {
 	    	HLT(0);

mercurial