thermferm/thermferm.c

changeset 317
18dd6eadba31
parent 316
73cd31dc6ce1
child 318
aad7789a40f2
equal deleted inserted replaced
316:73cd31dc6ce1 317:18dd6eadba31
1309 * PID controller per unit, each second 1309 * PID controller per unit, each second
1310 */ 1310 */
1311 for (unit = Config.units; unit; unit = unit->next) { 1311 for (unit = Config.units; unit; unit = unit->next) {
1312 if ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE)) { 1312 if ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE)) {
1313 // double pTerm, dTerm, iTerm; 1313 // double pTerm, dTerm, iTerm;
1314 int usePid = TRUE;
1314 1315
1315 sp = unit->beer_set; 1316 sp = unit->beer_set;
1316 pv = unit->beer_temperature / 1000.0; 1317 pv = unit->beer_temperature / 1000.0;
1317 if (unit->mode == UNITMODE_FRIDGE) { 1318 if (unit->mode == UNITMODE_FRIDGE) {
1318 sp = unit->fridge_set; 1319 sp = unit->fridge_set;
1319 pv = unit->air_temperature / 1000.0; 1320 pv = unit->air_temperature / 1000.0;
1321 usePid = FALSE;
1320 } else if (unit->mode == UNITMODE_PROFILE) { 1322 } else if (unit->mode == UNITMODE_PROFILE) {
1321 sp = unit->prof_target; 1323 sp = unit->prof_target;
1322 } 1324 }
1323 1325
1324 /*
1325 * PID controller compute
1326 */
1327 P_err = sp - pv; 1326 P_err = sp - pv;
1328 if (P_err < unit->idle_rangeH && P_err > unit->idle_rangeL) { 1327 if (P_err < unit->idle_rangeH && P_err > unit->idle_rangeL) {
1329 P_err = 0.0; 1328 P_err = 0.0;
1330 } 1329 }
1331 1330
1332 pid = (pid_var *)malloc(sizeof(pid_var)); 1331 if (usePid) {
1333 pid->dState = unit->PID_dState; 1332 /*
1334 pid->iState = unit->PID_iState; 1333 * PID controller compute
1335 pid->iMax = 100.0; 1334 */
1336 pid->iMin = -100.0; 1335 pid = (pid_var *)malloc(sizeof(pid_var));
1337 pid->pGain = unit->PID_Kp; 1336 pid->dState = unit->PID_dState;
1338 pid->iGain = unit->PID_Ki; 1337 pid->iState = unit->PID_iState;
1339 pid->dGain = unit->PID_Kd; 1338 pid->iMax = 100.0;
1340 1339 pid->iMin = -100.0;
1341 Out = UpdatePID(pid, P_err, pv); 1340 pid->pGain = unit->PID_Kp;
1342 1341 pid->iGain = unit->PID_Ki;
1343 if (Out > 100.0) 1342 pid->dGain = unit->PID_Kd;
1344 Out = 100.0; 1343
1345 if (Out < -100.0) 1344 Out = UpdatePID(pid, P_err, pv);
1346 Out = -100.0; 1345
1347 1346 if (Out > 100.0)
1348 if (debug) 1347 Out = 100.0;
1349 fprintf(stdout, "sp=%.2f pv=%.2f dState=%.2f P_err=%.2f iState=%.2f Out=%.2f\n", 1348 if (Out < -100.0)
1349 Out = -100.0;
1350
1351 if (debug)
1352 fprintf(stdout, "sp=%.2f pv=%.2f dState=%.2f P_err=%.2f iState=%.2f Out=%.2f\n",
1350 sp, pv, unit->PID_dState, P_err, unit->PID_iState, Out); 1353 sp, pv, unit->PID_dState, P_err, unit->PID_iState, Out);
1351 if ((Out >= 1) || (Out <= -1) || (seconds == 60) || unit->heater_state || unit->cooler_state) { 1354 if ((Out >= 1) || (Out <= -1) || (seconds == 60) || unit->heater_state || unit->cooler_state) {
1352 syslog(LOG_NOTICE, "sp=%.2f pv=%.2f P_err=%.2f dState=%.2f iState=%.2f Out=%.2f", 1355 syslog(LOG_NOTICE, "sp=%.2f pv=%.2f P_err=%.2f dState=%.2f iState=%.2f Out=%.2f",
1353 sp, pv, P_err, unit->PID_dState, unit->PID_iState, Out); 1356 sp, pv, P_err, unit->PID_dState, unit->PID_iState, Out);
1357 }
1358
1359 unit->PID_iState = pid->iState;
1360 unit->PID_dState = pid->dState;
1361 free(pid);
1362 pid = NULL;
1363 } else {
1364 /*
1365 * Simple temperature control
1366 */
1367 if (P_err < 0) {
1368 Out = 100.0;
1369 } else if (P_err > 0) {
1370 Out = -100.0;
1371 } else {
1372 Out = 0.0;
1373 }
1374 if ((Out >= 1) || (Out <= -1) || (seconds == 60) || unit->heater_state || unit->cooler_state) {
1375 syslog(LOG_NOTICE, "sp=%.2f pv=%.2f P_err=%.2f Out=%.2f",
1376 sp, pv, P_err, Out);
1377 }
1354 } 1378 }
1355
1356 unit->PID_iState = pid->iState;
1357 unit->PID_dState = pid->dState;
1358 1379
1359 // pTerm = unit->PID_Kp * P_err; 1380 // pTerm = unit->PID_Kp * P_err;
1360 1381
1361 /* 1382 /*
1362 * Calculate the intergral state with appropriate limiting 1383 * Calculate the intergral state with appropriate limiting

mercurial