comparison main.c @ 427:7c2efec9cd51

fix stupid bugs - lack of PSTR, MIN not MAX
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Oct 2012 19:21:11 +0800
parents b3b85cd4f09b
children 66d5e15f40d0
comparison
equal deleted inserted replaced
426:b3b85cd4f09b 427:7c2efec9cd51
36 #define VALUE_BROKEN 0x07D1 // 125.0625 36 #define VALUE_BROKEN 0x07D1 // 125.0625
37 37
38 // ranges are in decicelcius 38 // ranges are in decicelcius
39 #define FRIDGE_AIR_MIN_RANGE 30 39 #define FRIDGE_AIR_MIN_RANGE 30
40 #define FRIDGE_AIR_MAX_RANGE 30 40 #define FRIDGE_AIR_MAX_RANGE 30
41 #define OVERSHOOT_SCALE 1 41 #define OVERSHOOT_SCALE 0.5
42 42
43 #define BAUD 19200 43 #define BAUD 19200
44 #define UBRR ((F_CPU)/8/(BAUD)-1) 44 #define UBRR ((F_CPU)/8/(BAUD)-1)
45 45
46 #define PORT_LED PORTC 46 #define PORT_LED PORTC
372 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs); 372 fprintf_P(crc_stdout, PSTR("wake=%hhu\n"), wake_secs);
373 fprintf_P(crc_stdout, PSTR("fridge=%.1f\n"), fridge_setpoint/10.0); 373 fprintf_P(crc_stdout, PSTR("fridge=%.1f\n"), fridge_setpoint/10.0);
374 fprintf_P(crc_stdout, PSTR("fridge_diff=%.1f\n"), fridge_difference/10.0); 374 fprintf_P(crc_stdout, PSTR("fridge_diff=%.1f\n"), fridge_difference/10.0);
375 fprintf_P(crc_stdout, PSTR("fridge_delay=%hu\n"), fridge_delay); 375 fprintf_P(crc_stdout, PSTR("fridge_delay=%hu\n"), fridge_delay);
376 fprintf_P(crc_stdout, PSTR("fridge_status=%hhu\n"), is_fridge_on()); 376 fprintf_P(crc_stdout, PSTR("fridge_status=%hhu\n"), is_fridge_on());
377 fprintf_P(crc_stdout, PSTR("fridge_last_on=%lu\n"), fridge_on_clock.ticks);
378 fprintf_P(crc_stdout, PSTR("fridge_last_off=%lu\n"), fridge_off_clock.ticks);
379 fprintf_P(crc_stdout, PSTR("last_fridge=%hu\n"), last_fridge);
380 fprintf_P(crc_stdout, PSTR("last_wort=%hu\n"), last_wort);
377 fprintf_P(crc_stdout, PSTR("tick_secs=%d\n"), TICK); 381 fprintf_P(crc_stdout, PSTR("tick_secs=%d\n"), TICK);
378 fprintf_P(crc_stdout, PSTR("tick_wake=%d\n"), SLEEP_COMPARE); 382 fprintf_P(crc_stdout, PSTR("tick_wake=%d\n"), SLEEP_COMPARE);
379 fprintf_P(crc_stdout, PSTR("maxsens=%hhu\n"), MAX_SENSORS); 383 fprintf_P(crc_stdout, PSTR("maxsens=%hhu\n"), MAX_SENSORS);
380 fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS); 384 fprintf_P(crc_stdout, PSTR("totalmeas=%hu\n"), TOTAL_MEASUREMENTS);
381 fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors); 385 fprintf_P(crc_stdout, PSTR("sensors=%hhu\n"), n_sensors);
840 // the fridge min only applies when the wort is in the desired range. 844 // the fridge min only applies when the wort is in the desired range.
841 int16_t fridge_min = fridge_setpoint - FRIDGE_AIR_MIN_RANGE; 845 int16_t fridge_min = fridge_setpoint - FRIDGE_AIR_MIN_RANGE;
842 int16_t fridge_max = fridge_setpoint + FRIDGE_AIR_MAX_RANGE; 846 int16_t fridge_max = fridge_setpoint + FRIDGE_AIR_MAX_RANGE;
843 847
844 uint8_t fridge_on = PORT_FRIDGE & _BV(PIN_FRIDGE); 848 uint8_t fridge_on = PORT_FRIDGE & _BV(PIN_FRIDGE);
845 printf_P(PSTR("last_wort %hd (%hd, %hd), last_fridge %hd (%hd, %hd), setpoint %hd, diff %hd, fridge_on %d\n"), 849 printf_P(PSTR("last_wort %hd (%hd, %hd), last_fridge %hd (%hd, %hd), setpoint %hd, diff %hd, fridge_on %hu\n"),
846 last_wort, wort_min, wort_max, 850 last_wort, wort_min, wort_max,
847 fridge_setpoint, fridge_min, fridge_max, 851 fridge_setpoint, fridge_min, fridge_max,
848 fridge_difference, fridge_on); 852 fridge_difference, fridge_on);
849 853
850 if (off_time < fridge_delay) 854 if (off_time < fridge_delay)
859 { 863 {
860 bool turn_off = false; 864 bool turn_off = false;
861 uint16_t on_time = now.ticks - fridge_on_clock.ticks; 865 uint16_t on_time = now.ticks - fridge_on_clock.ticks;
862 866
863 // *10 for decicelcius 867 // *10 for decicelcius
864 uint16_t overshoot = OVERSHOOT_SCALE * 10.0f * MAX(3600, on_time) / 3600.0; 868 uint16_t overshoot = OVERSHOOT_SCALE * 10.0f * MIN(3600, on_time) / 3600.0;
869
870 printf_P(PSTR("on_time %hu, overshoot %hu\n"), on_time, overshoot);
865 871
866 // wort has cooled enough. will probably cool a bit more by itself 872 // wort has cooled enough. will probably cool a bit more by itself
867 if (wort_valid && (last_wort-overshoot) <= fridge_setpoint) 873 if (wort_valid && (last_wort-overshoot) <= fridge_setpoint)
868 { 874 {
869 printf_P("wort has cooled enough, overshoot %hu\n", overshoot); 875 printf_P(PSTR("wort has cooled enough, overshoot %hu on_time %hu\n"), overshoot, on_time);
870 turn_off = true; 876 turn_off = true;
871 } 877 }
872 878
873 // fridge is much cooler than wort 879 // fridge is much cooler than wort
874 if ((last_wort < wort_max || !wort_valid) && 880 if ((last_wort < wort_max || !wort_valid) &&
875 fridge_valid && last_fridge < fridge_min) 881 fridge_valid && last_fridge < fridge_min)
876 { 882 {
877 printf_P("fridge is too cold\n"); 883 printf_P(PSTR("fridge is too cold\n"));
878 turn_off = true; 884 turn_off = true;
879 } 885 }
880 886
881 if (turn_off) 887 if (turn_off)
882 { 888 {
890 { 896 {
891 bool turn_on = false; 897 bool turn_on = false;
892 898
893 if (wort_valid && last_wort >= wort_max) 899 if (wort_valid && last_wort >= wort_max)
894 { 900 {
895 printf_P("wort is too hot\n"); 901 printf_P(PSTR("wort is too hot\n"));
896 turn_on = true; 902 turn_on = true;
897 } 903 }
898 904
899 if ((fridge_valid && last_fridge > fridge_max)) 905 if ((fridge_valid && last_fridge > fridge_max))
900 { 906 {
904 910
905 911
906 if ((last_wort > wort_min || !wort_valid) && 912 if ((last_wort > wort_min || !wort_valid) &&
907 (fridge_valid && last_fridge > fridge_setpoint)) 913 (fridge_valid && last_fridge > fridge_setpoint))
908 { 914 {
909 printf_P("fridge is too hot\n"); 915 printf_P(PSTR("fridge is too hot\n"));
910 turn_on = true; 916 turn_on = true;
911 } 917 }
912 918
913 if (turn_on) 919 if (turn_on)
914 { 920 {