heat.c 8.46 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <unistd.h>
#include "hvc.h"
#include "heat.h"
#include "pump.h"
#include "printx.h"

#define TASK_NUMBER 32
#define T_INIT_THRESH 40
#define T_STEP_THRESH 80
#define T_HOLD_THRESH 88
#define T_ECO_MODE_THRESH 50
#define INIT_HEAT_TIME 15
#define INIT_WAIT_TIME 12
#define STEP_HEAT_TIME 2
#define STEP_WAIT_TIME 6
#define HOLD_HEAT_TIME 1
#define HOLD_WAIT_TIME 8
#define HOLD_PUMP_HEAT_TIME 3
#define HOLD_PUMP_WAIT_TIME 5


//Globals
time_t tTask;
time_t tNow;
float tAct;
bool task[TASK_NUMBER];
bool stop=false;
bool hold_heat=true;
bool eco_mode=false;
bool stopHeat=false;
bool heat_ok=false;

void initProcessHeat(void)
{
	task[0]=true;
	for (int i_initH=1;i_initH<TASK_NUMBER;i_initH++)
		task[i_initH]=false;
}

void actTemp(float temp)
{
	tAct = temp;
}

void stopAutoHeat(void)
{
	stopHeat = true;
}

void setHeatOk(void)
{
	heat_ok=true;
}

void resetHeatOk(void)
{
	heat_ok=false;
}

bool isHeatOk(void)
{
	heat_ok = (tAct > T_HOLD_THRESH);
	return heat_ok;
}

void* processHeat(void* arg)
{
	while(!stopHeat)
	{
		tNow = clock();

		//Chaufffe initiale
		if (task[0])
		{
			printx(INFO, HEAT, "Entering in task 0\n");
			tTask = clock();

			if (tAct < T_INIT_THRESH)
			{
				setHeatOn();
				printx(INFO, HEAT, "Début chauffe initiale 0\n");
				task[0] = false;
				task[1] = true;
			}

			else
			{
				task[0] = false;
				task[6] = true;
			}
		}

		else if (task[1])
		{
			printx(INFO, HEAT, "Entering in task 1\n");
			tTask = clock();
			task[1] = false;
			task[2] = true;
		}

		else if (task[2])
		{
			printx(INFO, HEAT, "Entering in task 2\n");
			printx(INFO, HEAT, "Time heating : %d (%d - %d) / %d\n", (tNow - tTask) / CLOCKS_PER_SEC, tNow, tTask, CLOCKS_PER_SEC);
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > INIT_HEAT_TIME )
			{
				setHeatOff();
				printx(INFO, HEAT, "Fin chauffe initiale 1\n");
				task[2] = false;
				task[3] = true;
			}
		}

		else if (task[3])
		{
			printx(INFO, HEAT, "Entering in task 3\n");
			tTask = clock();
			task[3] = false;
			task[4] = true;
		}

		else if (task[4])
		{
			printx(INFO, HEAT, "Entering in task 4\n");
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > INIT_WAIT_TIME )
			{
				printx(INFO, HEAT, "Fin attente %d sec 2\n", INIT_WAIT_TIME);
				task[4] = false;
				task[5] = true;
			}
		}


		//Chauffe par palier
		else if (task[5])
		{
			printx(INFO, HEAT, "Entering in task 5\n");
			tTask = clock();
			task[5] = false;
			task[6] = true;
		}


		else if (task[6])
		{
			printx(INFO, HEAT, "Entering in task 6\n");
			if (tAct < T_STEP_THRESH)
			{
				printx(INFO, HEAT, "Début Chauffe par palier 3\n");
				setHeatOn();
				task[6] = false;
				task[7] = true;
			}

			else
			{
				task[6] = false;
				task[11] = true; //Numéro de tache début "Maintien au chaud"
			}
		}

		else if (task[7])
		{
			printx(INFO, HEAT, "Entering in task 7\n");
			tTask = clock();
			task[7] = false;
			task[8] = true;
		}

		else if (task[8])
		{
			printx(INFO, HEAT, "Entering in task 8\n");
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > STEP_HEAT_TIME )
			{
				printx(INFO, HEAT, "Fin de chauffe palier intermédiaire 4\n");
				setHeatOff();
				task[8] = false;
				task[9] = true;
			}
		}

		else if (task[9])
		{
			printx(INFO, HEAT, "Entering in task 9\n");
			tTask = clock();
			task[9] = false;
			task[10] = true;
		}

		else if (task[10])
		{
			printx(INFO, HEAT, "Entering in task 10\n");
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > STEP_WAIT_TIME )
			{
				printx(INFO, HEAT, "Fin d'attente palier intermédiaire 5\n");

				if ( tAct < T_STEP_THRESH )
				{
					task[10] = false;
					task[5] = true;
				}

				else
				{
					task[10] = false;
					task[11] = true;
				}
			}
		}


		// Maintien au chaud
		else if (task[11])
		{
			printx(INFO, HEAT, "Entering in task 11\n");
			tTask = clock();

			if (eco_mode)
            {
                task[11] = false;
                task[24] = true; //Numéro tache Mode éco
            }

            else
            {
                task[11] = false;
                task[12] = true;
            }
		}

		else if (task[12])
		{
			printx(INFO, HEAT, "Entering in task 12\n");
			printx(INFO, HEAT, "Début Maintien au chaud 6\n");
			setHeatOk();
			task[12]=false;
			task[13]=true;
		}

		else if (task[13])
		{
			printx(INFO, HEAT, "Entering in task 13\n");
			tTask = clock();
			task[13] = false;
			task[14] = true;
		}

		else if (task[14])
		{
			printx(INFO, HEAT, "Entering in task 14\n");
			if(hold_heat)
			{
				if( (tAct < T_HOLD_THRESH))
				{
					if (!isPumpOn())
					{
						printx(INFO, HEAT, "Début Chauffe maintien sans pompe 7\n");
						setHeatOn();
						task[14] = false;
						task[15] = true;
					}

					else
					{
						printx(INFO, HEAT, "Début Chauffe maintien avec pompe 7'\n");
						setHeatOn();
						task[14]=false;
						task[19]=true; //Numéro de tache "T<Thold && pumpitup"
					}
				}

			}

			else
			{
				printx(INFO, HEAT, "Maintien au chaud annulé 7 ' ' Go etape de fin\n");
				task[14]=false;
				task[23]=true; //Sinon go étape d'attente fin
			}
		}

		else if (task[15])
		{
			printx(INFO, HEAT, "Entering in task 15\n");
			tTask = clock();
			task[15] = false;
			task[16] = true;
		}

			//Maintien au chaud sans pompage
		else if (task[16])
		{
			printx(INFO, HEAT, "Entering in task 16\n");
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_HEAT_TIME )
			{
				printx(INFO, HEAT, "Fin chauffe maintien sans pompe 8\n");
				setHeatOff();
				task[16] = false;
				task[17] = true;
			}
		}

		else if (task[17])
		{
			printx(INFO, HEAT, "Entering in task 17\n");
			tTask = clock();
			task[17] = false;
			task[18] = true;
		}

		else if (task[18])
		{
			printx(INFO, HEAT, "Entering in task 18\n");
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_WAIT_TIME )
			{
				if ( tAct < T_HOLD_THRESH)
				{
					printx(INFO, HEAT, "Fin d'attente chauffe intermédiaire maintien 9\n");
					task[18] = false;
					task[11] = true; //Retour au début de "Maintien au chaud"
				}
			}
		}

			//Maintien au chaud avec pompage
		else if (task[19])
		{
			printx(INFO, HEAT, "Entering in task 19\n");
			tTask = clock();
			task[19] = false;
			task[20] = true;
		}

		else if (task[20])
		{
			printx(INFO, HEAT, "Entering in task 20\n");
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_PUMP_HEAT_TIME )
			{
				printx(INFO, HEAT, "Fin chauffe maintien avec pompe 10\n");
				setHeatOff();
				task[20] = false;
				task[21] = true;
			}
		}

		else if (task[21])
		{
			printx(INFO, HEAT, "Entering in task 21\n");
			tTask = clock();
			task[21] = false;
			task[22] = true;
		}

		else if (task[22])
		{
			printx(INFO, HEAT, "Entering in task 22\n");
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_PUMP_WAIT_TIME )
			{
				if ( tAct < T_HOLD_THRESH)
				{
					printx(INFO, HEAT, "Fin d'attente chauffe intermédiaire maintien avec pompe 11\n");
					task[22] = false;
					task[5] = true; //Retour au début de "Chauffe par palier" (shuntera si temperature OK (t>T_HOLD_THRESH))
				}
			}
		}

			//Fin de maintien au chaud (demandé par l'extérieur) et reboucle quand hold_heat est remis
		else if(task[23])
		{
			printx(INFO, HEAT, "Entering in task 23\n");
			setHeatOff();
			resetHeatOk();

			if(hold_heat)
			{
				task[23]=false;
				task[0]=true; //Retour au début
			}
		}


		//Mode économie d'énergie
		else if(task[24])
		{
		    printx(INFO, HEAT, "Entering in task 24\n");
		    printx(INFO, HEAT, "Début Mode Eco maintien 24\n");

			if (eco_mode)
            {
                if (tAct < T_ECO_MODE_THRESH)
                {
                    printx(INFO, HEAT, "Debut chauffe maintien Eco Mode 25\n");
                    setHeatOn();
                    task[24] = false;
                    task[25] = true;
                }
            }

            else
            {
                task[24] = false;
                task[5] = true; //Retour "Chauffe par palier"
            }
		}

		else if (task[25])
		{
			tTask = clock();
			task[25] = false;
			task[26] = true;
		}

        else if (task[26])
		{
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_HEAT_TIME )
			{
				printx(INFO, HEAT, "Fin chauffe maintien Eco mode 27\n");
				setHeatOff();
				task[26] = false;
				task[27] = true;
			}
		}

		else if (task[27])
		{
			tTask = clock();
			task[27] = false;
			task[28] = true;
		}

		else if (task[28])
		{
			if( ((tNow - tTask) / CLOCKS_PER_SEC) > HOLD_WAIT_TIME )
			{
					printx(INFO, HEAT, "Fin d'attente maintien Eco mode 29\n");
					task[28] = false;
					task[24] = true;
            }
        }

		usleep(20000);
    }


	return NULL;
}