1514 /* |
1514 /* |
1515 * Set both PID's to their input values. |
1515 * Set both PID's to their input values. |
1516 */ |
1516 */ |
1517 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE; |
1517 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE; |
1518 if (unit->mode == UNITMODE_FRIDGE) { |
1518 if (unit->mode == UNITMODE_FRIDGE) { |
1519 unit->PID_cool->SetP = unit->fridge_set; |
1519 unit->PID_cool->SetP = unit->fridge_set + unit->PID_cool->idleRange; |
1520 unit->PID_heat->SetP = unit->fridge_set; |
1520 unit->PID_heat->SetP = unit->fridge_set - unit->PID_heat->idleRange; |
1521 unit->PID_cool->Input = unit->PID_heat->Input = unit->air_temperature / 1000.0; |
1521 unit->PID_cool->Input = unit->PID_heat->Input = unit->air_temperature / 1000.0; |
1522 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_BOO; |
1522 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_BOO; |
1523 } else if (unit->mode == UNITMODE_BEER) { |
1523 } else if (unit->mode == UNITMODE_BEER) { |
1524 unit->PID_cool->SetP = unit->beer_set; |
1524 unit->PID_cool->SetP = unit->beer_set; |
1525 unit->PID_heat->SetP = unit->beer_set; |
1525 unit->PID_heat->SetP = unit->beer_set; |
1526 unit->PID_cool->Input = unit->PID_heat->Input = unit->beer_temperature / 1000.0; |
1526 unit->PID_cool->Input = unit->PID_heat->Input = unit->beer_temperature / 1000.0; |
1527 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_BOO; |
1527 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_AUTO; |
1528 } else if (unit->mode == UNITMODE_PROFILE) { |
1528 } else if (unit->mode == UNITMODE_PROFILE) { |
1529 double usetemp; |
1529 double usetemp; |
1530 unit->PID_cool->SetP = unit->prof_target_hi; |
1530 unit->PID_cool->SetP = unit->prof_target_hi; |
1531 unit->PID_heat->SetP = unit->prof_target_lo; |
1531 unit->PID_heat->SetP = unit->prof_target_lo; |
1532 /* |
1532 /* |
1536 ((100 - unit->prof_fridge_mode) * (unit->beer_temperature / 1000.0))) / 100.0; |
1536 ((100 - unit->prof_fridge_mode) * (unit->beer_temperature / 1000.0))) / 100.0; |
1537 if (debug) |
1537 if (debug) |
1538 fprintf(stdout, " fridge_mode=%d measured=%.3f %.3f => %.3f\n", unit->prof_fridge_mode, |
1538 fprintf(stdout, " fridge_mode=%d measured=%.3f %.3f => %.3f\n", unit->prof_fridge_mode, |
1539 unit->air_temperature / 1000.0, unit->beer_temperature / 1000.0, usetemp); |
1539 unit->air_temperature / 1000.0, unit->beer_temperature / 1000.0, usetemp); |
1540 unit->PID_cool->Input = unit->PID_heat->Input = usetemp; |
1540 unit->PID_cool->Input = unit->PID_heat->Input = usetemp; |
1541 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_BOO; |
1541 unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_AUTO; |
1542 } |
1542 } |
1543 |
1543 |
1544 /* |
1544 /* |
1545 * PID controller compute |
1545 * PID controller compute |
1546 */ |
1546 */ |
1549 |
1549 |
1550 /* |
1550 /* |
1551 * Logging |
1551 * Logging |
1552 */ |
1552 */ |
1553 if (unit->heater_address) { |
1553 if (unit->heater_address) { |
|
1554 /* |
|
1555 * Prevent extreme heating |
|
1556 */ |
|
1557 if ((unit->mode == UNITMODE_BEER) && ((unit->air_temperature / 1000.0) > (unit->PID_heat->Input + 5.0))) { |
|
1558 unit->PID_heat->OutP = 0.0; |
|
1559 } |
1554 if (debug) |
1560 if (debug) |
1555 fprintf(stdout, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n", |
1561 fprintf(stdout, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n", |
1556 unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP); |
1562 unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP); |
1557 if (/* ((unit->PID_heat->OutP >= 2) && unit->heater_address) || */ (seconds == 60) /* || unit->heater_state */) { |
1563 if (seconds == 60) { |
1558 syslog(LOG_NOTICE, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f", |
1564 syslog(LOG_NOTICE, "Heat: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f", |
1559 unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP); |
1565 unit->PID_heat->SetP, unit->PID_heat->Input, unit->PID_heat->iState, unit->PID_heat->Err, unit->PID_heat->OutP); |
1560 } |
1566 } |
1561 } |
1567 } |
1562 if (unit->cooler_address) { |
1568 if (unit->cooler_address) { |
|
1569 /* |
|
1570 * Prevent extreme cooling |
|
1571 */ |
|
1572 if ((unit->mode == UNITMODE_BEER) && ((unit->air_temperature / 1000.0) < (unit->PID_cool->Input - 5.0))) { |
|
1573 unit->PID_cool->OutP = 0.0; |
|
1574 } |
1563 if (debug) |
1575 if (debug) |
1564 fprintf(stdout, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n", |
1576 fprintf(stdout, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f\n", |
1565 unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP); |
1577 unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP); |
1566 if (/* ((unit->PID_cool->OutP >= 2) && unit->cooler_address) || */ (seconds == 60) /* || unit->cooler_state*/ ) { |
1578 if (seconds == 60) { |
1567 syslog(LOG_NOTICE, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f", |
1579 syslog(LOG_NOTICE, "Cool: sp=%.2f Input=%.2f iState=%.2f Err=%.2f Out=%.2f", |
1568 unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP); |
1580 unit->PID_cool->SetP, unit->PID_cool->Input, unit->PID_cool->iState, unit->PID_cool->Err, unit->PID_cool->OutP); |
1569 } |
1581 } |
1570 } |
1582 } |
1571 |
1583 |
1572 /* |
1584 /* |
1573 * Deadlock |
1585 * Deadlock |
1574 */ |
1586 */ |
1575 if (unit->PID_cool->OutP && unit->PID_heat->OutP) { |
1587 if (unit->PID_cool->OutP && unit->PID_heat->OutP) { |
1576 syslog(LOG_NOTICE, "Heat and Cool lockdown"); |
1588 syslog(LOG_NOTICE, "Heat and Cool lockdown"); |
1577 unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0; |
1589 if (unit->PID_cool->OutP > unit->PID_heat->OutP) |
|
1590 unit->PID_heat->OutP = 0.0; |
|
1591 else |
|
1592 unit->PID_cool->OutP = 0.0; |
|
1593 // unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0; |
1578 } |
1594 } |
1579 |
1595 |
1580 if (unit->heater_address && ! unit->cooler_state) { |
1596 if (unit->heater_address && ! unit->cooler_state) { |
1581 if (unit->PID_heat->OutP >= 2) { |
1597 if (unit->PID_heat->OutP >= 50) { |
1582 if (unit->heater_wait < unit->heater_delay) { |
1598 if (unit->heater_wait < unit->heater_delay) { |
1583 unit->heater_wait++; |
1599 unit->heater_wait++; |
1584 } else { |
1600 } else { |
1585 int power = round(unit->PID_heat->OutP); |
1601 int power = round(unit->PID_heat->OutP); |
1586 if (unit->heater_state != power) { |
1602 if (unit->heater_state != power) { |
1628 if (unit->door_state) |
1644 if (unit->door_state) |
1629 device_out(unit->cooler_address, unit->cooler_state); |
1645 device_out(unit->cooler_address, unit->cooler_state); |
1630 else |
1646 else |
1631 device_out(unit->cooler_address, 0); |
1647 device_out(unit->cooler_address, 0); |
1632 } |
1648 } |
|
1649 if (debug) |
|
1650 fprintf(stdout, "Final: PIDheat=%.2f PWRheat=%d PIDcool=%.2f PWRcool=%d\n", |
|
1651 unit->PID_heat->OutP, unit->heater_state, unit->PID_cool->OutP, unit->cooler_state); |
1633 |
1652 |
1634 if ((unit->heater_address || unit->cooler_address) && unit->fan_address) { |
1653 if ((unit->heater_address || unit->cooler_address) && unit->fan_address) { |
1635 /* |
1654 /* |
1636 * If there is a heater or cooler and we have a fan, turn fan on if |
1655 * If there is a heater or cooler and we have a fan, turn fan on if |
1637 * cooling or heating. The Fan has a start/stop delay. |
1656 * cooling or heating. The Fan has a start/stop delay. |