Mercurial > templog
comparison main.c @ 135:10d35b4370ef
make overshoot a parameter
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 15 Oct 2012 21:51:19 +0800 |
parents | 405785632c78 |
children | 08adc1fa02e8 |
comparison
equal
deleted
inserted
replaced
134:405785632c78 | 135:10d35b4370ef |
---|---|
33 #define SLEEP_COMPARE (32*TICK-1) | 33 #define SLEEP_COMPARE (32*TICK-1) |
34 | 34 |
35 #define VALUE_NOSENSOR 0x07D0 // 125 degrees | 35 #define VALUE_NOSENSOR 0x07D0 // 125 degrees |
36 #define VALUE_BROKEN 0x07D1 // 125.0625 | 36 #define VALUE_BROKEN 0x07D1 // 125.0625 |
37 | 37 |
38 // ranges are in decicelcius | 38 #define OVERSHOOT_MAX_DIV 1800.0 // 30 mins |
39 #define OVERSHOOT_DELAY 1800 // 30 mins | |
40 #define OVERSHOOT_FACTOR 1 | |
41 #define WORT_INVALID_TIME 900 // 15 mins | 39 #define WORT_INVALID_TIME 900 // 15 mins |
42 // fridge min/max are only used if the wort sensor is invalid | 40 // fridge min/max are only used if the wort sensor is invalid |
43 #define FRIDGE_AIR_MIN_RANGE 40 // 4º | 41 #define FRIDGE_AIR_MIN_RANGE 40 // 4º |
44 #define FRIDGE_AIR_MAX_RANGE 40 // 4º | 42 #define FRIDGE_AIR_MAX_RANGE 40 // 4º |
45 | 43 |
87 // decidegrees | 85 // decidegrees |
88 static int16_t fridge_setpoint = 180; // 18.0ºC | 86 static int16_t fridge_setpoint = 180; // 18.0ºC |
89 static int16_t fridge_difference = 3; // 0.3ºC | 87 static int16_t fridge_difference = 3; // 0.3ºC |
90 static uint16_t fridge_delay = 600; // seconds | 88 static uint16_t fridge_delay = 600; // seconds |
91 | 89 |
90 static uint16_t overshoot_delay = 720; // 12 mins | |
91 static uint8_t overshoot_factor = 10; // 1.0ºC | |
92 | |
92 // ---- Atomic guards required accessing these variables | 93 // ---- Atomic guards required accessing these variables |
93 // clock_epoch in seconds | 94 // clock_epoch in seconds |
94 static uint32_t clock_epoch; | 95 static uint32_t clock_epoch; |
95 static uint16_t comms_count; | 96 static uint16_t comms_count; |
96 static uint16_t measure_count; | 97 static uint16_t measure_count; |
162 uint8_t wake_secs; | 163 uint8_t wake_secs; |
163 | 164 |
164 int16_t fridge_setpoint; // decidegrees | 165 int16_t fridge_setpoint; // decidegrees |
165 uint8_t fridge_difference; // decidegrees | 166 uint8_t fridge_difference; // decidegrees |
166 uint16_t fridge_delay; | 167 uint16_t fridge_delay; |
168 | |
169 uint16_t overshoot_delay; | |
170 uint8_t overshoot_factor; // decidegrees | |
167 | 171 |
168 #if 0 | 172 #if 0 |
169 static uint8_t wort_id[ID_LEN]; | 173 static uint8_t wort_id[ID_LEN]; |
170 static uint8_t fridge_id[ID_LEN]; | 174 static uint8_t fridge_id[ID_LEN]; |
171 #endif | 175 #endif |
375 fprintf_P(crc_stdout, PSTR("comms=%hu\n"), comms_wake); | 379 fprintf_P(crc_stdout, PSTR("comms=%hu\n"), comms_wake); |
376 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs); | 380 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs); |
377 fprintf_P(crc_stdout, PSTR("fridge=%.1f\n"), fridge_setpoint/10.0); | 381 fprintf_P(crc_stdout, PSTR("fridge=%.1f\n"), fridge_setpoint/10.0); |
378 fprintf_P(crc_stdout, PSTR("fridge_diff=%.1f\n"), fridge_difference/10.0); | 382 fprintf_P(crc_stdout, PSTR("fridge_diff=%.1f\n"), fridge_difference/10.0); |
379 fprintf_P(crc_stdout, PSTR("fridge_delay=%hu\n"), fridge_delay); | 383 fprintf_P(crc_stdout, PSTR("fridge_delay=%hu\n"), fridge_delay); |
384 fprintf_P(crc_stdout, PSTR("overshoot_factor=%.1f\n"), overshoot_factor/10.0); | |
385 fprintf_P(crc_stdout, PSTR("overshoot_delay=%hu\n"), overshoot_delay); | |
380 fprintf_P(crc_stdout, PSTR("fridge_status=%hhu\n"), is_fridge_on()); | 386 fprintf_P(crc_stdout, PSTR("fridge_status=%hhu\n"), is_fridge_on()); |
381 fprintf_P(crc_stdout, PSTR("fridge_last_on=%lu\n"), fridge_on_clock.ticks); | 387 fprintf_P(crc_stdout, PSTR("fridge_last_on=%lu\n"), fridge_on_clock.ticks); |
382 fprintf_P(crc_stdout, PSTR("fridge_last_off=%lu\n"), fridge_off_clock.ticks); | 388 fprintf_P(crc_stdout, PSTR("fridge_last_off=%lu\n"), fridge_off_clock.ticks); |
383 fprintf_P(crc_stdout, PSTR("last_fridge=%hu\n"), last_fridge); | 389 fprintf_P(crc_stdout, PSTR("last_fridge=%hu\n"), last_fridge); |
384 fprintf_P(crc_stdout, PSTR("last_wort=%hu\n"), last_wort); | 390 fprintf_P(crc_stdout, PSTR("last_wort=%hu\n"), last_wort); |
506 eeprom_read(comms_wake, comms_wake); | 512 eeprom_read(comms_wake, comms_wake); |
507 eeprom_read(wake_secs, wake_secs); | 513 eeprom_read(wake_secs, wake_secs); |
508 eeprom_read(fridge_setpoint, fridge_setpoint); | 514 eeprom_read(fridge_setpoint, fridge_setpoint); |
509 eeprom_read(fridge_difference, fridge_difference); | 515 eeprom_read(fridge_difference, fridge_difference); |
510 eeprom_read(fridge_delay, fridge_delay); | 516 eeprom_read(fridge_delay, fridge_delay); |
517 eeprom_read(overshoot_delay, overshoot_delay); | |
518 eeprom_read(overshoot_factor, overshoot_factor); | |
511 } | 519 } |
512 } | 520 } |
513 | 521 |
514 static void | 522 static void |
515 cmd_get_params() | 523 cmd_get_params() |
519 printf_P(PSTR("wake %hhu\n"), wake_secs); | 527 printf_P(PSTR("wake %hhu\n"), wake_secs); |
520 printf_P(PSTR("tick %d\n"), TICK); | 528 printf_P(PSTR("tick %d\n"), TICK); |
521 printf_P(PSTR("fridge %.1fº\n"), fridge_setpoint / 10.0f); | 529 printf_P(PSTR("fridge %.1fº\n"), fridge_setpoint / 10.0f); |
522 printf_P(PSTR("fridge difference %.1fº\n"), fridge_difference / 10.0f); | 530 printf_P(PSTR("fridge difference %.1fº\n"), fridge_difference / 10.0f); |
523 printf_P(PSTR("fridge_delay %hu\n"), fridge_delay); | 531 printf_P(PSTR("fridge_delay %hu\n"), fridge_delay); |
532 printf_P(PSTR("overshoot factor %.1fº\n"), overshoot_factor / 10.0f); | |
533 printf_P(PSTR("overshoot delay %hu\n"), overshoot_delay); | |
524 printf_P(PSTR("sensors %hhu (%hhu)\n"), | 534 printf_P(PSTR("sensors %hhu (%hhu)\n"), |
525 n_sensors, MAX_SENSORS); | 535 n_sensors, MAX_SENSORS); |
526 printf_P(PSTR("meas %hu (%hu)\n"), | 536 printf_P(PSTR("meas %hu (%hu)\n"), |
527 max_measurements, TOTAL_MEASUREMENTS); | 537 max_measurements, TOTAL_MEASUREMENTS); |
528 } | 538 } |
573 eeprom_write(comms_wake, comms_wake); | 583 eeprom_write(comms_wake, comms_wake); |
574 eeprom_write(wake_secs, wake_secs); | 584 eeprom_write(wake_secs, wake_secs); |
575 eeprom_write(fridge_setpoint, fridge_setpoint); | 585 eeprom_write(fridge_setpoint, fridge_setpoint); |
576 eeprom_write(fridge_difference, fridge_difference); | 586 eeprom_write(fridge_difference, fridge_difference); |
577 eeprom_write(fridge_delay, fridge_delay); | 587 eeprom_write(fridge_delay, fridge_delay); |
588 eeprom_write(overshoot_delay, overshoot_delay); | |
589 eeprom_write(overshoot_factor, overshoot_factor); | |
578 magic = EXPECT_MAGIC; | 590 magic = EXPECT_MAGIC; |
579 eeprom_write(magic, magic); | 591 eeprom_write(magic, magic); |
580 } | 592 } |
581 | 593 |
582 return true; | 594 return true; |
651 eeprom_write(fridge_delay, fridge_delay); | 663 eeprom_write(fridge_delay, fridge_delay); |
652 } | 664 } |
653 } | 665 } |
654 printf_P(PSTR("new fridge delay %hu\n"), fridge_delay); | 666 printf_P(PSTR("new fridge delay %hu\n"), fridge_delay); |
655 } | 667 } |
668 | |
669 static void | |
670 cmd_set_overshoot_factor(char *params) | |
671 { | |
672 float new_f = atof(params); | |
673 if (new_f <= 0 || new_f > 20) | |
674 { | |
675 printf_P(PSTR("Bad overshoot factor %f\n"), new_f); | |
676 return; | |
677 } | |
678 | |
679 uint8_t old = overshoot_delay; | |
680 | |
681 overshoot_delay = new_f * 10; | |
682 bool written = set_initial_eeprom(); | |
683 if (!written) | |
684 { | |
685 if (old != overshoot_factor) | |
686 { | |
687 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
688 { | |
689 eeprom_write(overshoot_factor, overshoot_factor); | |
690 } | |
691 } | |
692 } | |
693 printf_P(PSTR("old factor %.1fº new factor %.1fº\n"), | |
694 old / 10.0f, overshoot_factor / 10.0f); | |
695 } | |
656 | 696 |
697 static void | |
698 cmd_set_overshoot_delay(char *params) | |
699 { | |
700 uint16_t new_delay = atoi(params); | |
701 if (new_delay < 5) | |
702 { | |
703 printf_P(PSTR("Bad overshoot delay %d\n"), new_delay); | |
704 return; | |
705 } | |
706 | |
707 overshoot_delay = new_delay; | |
708 bool written = set_initial_eeprom(); | |
709 if (!written) | |
710 { | |
711 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) | |
712 { | |
713 eeprom_write(overshoot_delay, overshoot_delay); | |
714 } | |
715 } | |
716 printf_P(PSTR("new overshoot delay %hu\n"), overshoot_delay); | |
717 } | |
718 | |
657 static void | 719 static void |
658 cmd_awake() | 720 cmd_awake() |
659 { | 721 { |
660 stay_awake = 1; | 722 stay_awake = 1; |
661 printf_P(PSTR("awake\n")); | 723 printf_P(PSTR("awake\n")); |
705 cmd_set_fridge_difference(&readbuf[12]); | 767 cmd_set_fridge_difference(&readbuf[12]); |
706 } | 768 } |
707 else if (strncmp_P(readbuf, PSTR("fridge_delay "), 13) == 0) | 769 else if (strncmp_P(readbuf, PSTR("fridge_delay "), 13) == 0) |
708 { | 770 { |
709 cmd_set_fridge_delay(&readbuf[13]); | 771 cmd_set_fridge_delay(&readbuf[13]); |
772 } | |
773 else if (strncmp_P(readbuf, PSTR("overshoot_delay "), 16) == 0) | |
774 { | |
775 cmd_set_overshoot_delay(&readbuf[16]); | |
776 } | |
777 else if (strncmp_P(readbuf, PSTR("overshoot_factor "), 17) == 0) | |
778 { | |
779 cmd_set_overshoot_factor(&readbuf[17]); | |
710 } | 780 } |
711 else if (strcmp_P(readbuf, PSTR("reset")) == 0) | 781 else if (strcmp_P(readbuf, PSTR("reset")) == 0) |
712 { | 782 { |
713 cmd_reset(); | 783 cmd_reset(); |
714 } | 784 } |
889 { | 959 { |
890 bool turn_off = false; | 960 bool turn_off = false; |
891 uint16_t on_time = now.ticks - fridge_on_clock.ticks; | 961 uint16_t on_time = now.ticks - fridge_on_clock.ticks; |
892 | 962 |
893 uint16_t overshoot = 0; | 963 uint16_t overshoot = 0; |
894 if (on_time > 1800) | 964 if (on_time > overshoot_delay) |
895 { | 965 { |
896 // *10.0f for decicelcius | 966 overshoot = overshoot_factor * MIN(OVERSHOOT_MAX_DIV, on_time) / OVERSHOOT_MAX_DIV; |
897 overshoot = OVERSHOOT_FACTOR * 10.0f * MIN(3600, on_time) / 3600.0; | |
898 } | 967 } |
899 | 968 |
900 printf_P(PSTR("on_time %hu, overshoot %hu\n"), on_time, overshoot); | 969 printf_P(PSTR("on_time %hu, overshoot %hu\n"), on_time, overshoot); |
901 | 970 |
902 // wort has cooled enough. will probably cool a bit more by itself | 971 // wort has cooled enough. will probably cool a bit more by itself |