measure_temp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Xytronic LF-1600
  3. * Temperature measurement routines
  4. *
  5. * Copyright (c) 2015-2017 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "measure_temp.h"
  22. #include "measure.h"
  23. #include "timer.h"
  24. #include "scale.h"
  25. #include "controller_temp.h"
  26. #include "controller_current.h"
  27. #include "debug_uart.h"
  28. #include "menu.h"
  29. #include "settings.h"
  30. #include <string.h>
  31. struct meastemp_context {
  32. fixpt_t prev_feedback;
  33. };
  34. static struct meastemp_context meastemp;
  35. void meastemp_adjust_set(fixpt_t adjustment)
  36. {
  37. #if CONF_ADJ
  38. measure_adjust_set(MEAS_CHAN_TEMP, adjustment);
  39. get_settings()->temp_adj = adjustment;
  40. store_settings();
  41. menu_request_display_update();
  42. #endif
  43. }
  44. fixpt_t meastemp_adjust_get(void)
  45. {
  46. return measure_adjust_get(MEAS_CHAN_TEMP);
  47. }
  48. void meastemp_result_handler(fixpt_t measured_phys_value,
  49. enum measure_plausibility plaus);
  50. void meastemp_result_handler(fixpt_t measured_phys_value,
  51. enum measure_plausibility plaus)
  52. {
  53. /* Set/reset emergency status. */
  54. if (plaus == MEAS_PLAUSIBLE)
  55. contrtemp_set_emerg(false);
  56. else if (plaus == MEAS_PLAUS_TIMEOUT)
  57. contrtemp_set_emerg(true);
  58. /* Set the controller feedback. */
  59. if (plaus == MEAS_PLAUSIBLE) {
  60. meastemp.prev_feedback = measured_phys_value;
  61. contrcurr_set_restricted(
  62. measured_phys_value < float_to_fixpt(CONTRCURR_RESTRICT_TOTEMP)
  63. );
  64. contrtemp_set_feedback(measured_phys_value);
  65. } else if (plaus == MEAS_NOT_PLAUSIBLE) {
  66. /* Just run the controller with the previous feedback. */
  67. contrtemp_set_feedback(meastemp.prev_feedback);
  68. }
  69. }
  70. extern
  71. const struct measure_config __flash meastemp_config;
  72. const struct measure_config __flash meastemp_config = {
  73. .name = "mt",
  74. .mux = MEAS_MUX_ADC1,
  75. .did = MEAS_DID_ADC1,
  76. .ps = MEAS_PS_64,
  77. .ref = MEAS_REF_AREF,
  78. .averaging_timeout_ms = 50,
  79. #ifdef HW_LEGACY
  80. .scale_raw_lo = 209,
  81. .scale_raw_hi = 410,
  82. .scale_phys_lo = FLOAT_TO_FIXPT(CELSIUS(150)),
  83. .scale_phys_hi = FLOAT_TO_FIXPT(CELSIUS(480)),
  84. #endif
  85. #ifdef HW_SMD
  86. .scale_raw_lo = 210,
  87. .scale_raw_hi = 411,
  88. .scale_phys_lo = FLOAT_TO_FIXPT(CELSIUS(150)),
  89. .scale_phys_hi = FLOAT_TO_FIXPT(CELSIUS(480)),
  90. #endif
  91. .plaus_neglim = FLOAT_TO_FIXPT(CELSIUS(-20)),
  92. .plaus_poslim = FLOAT_TO_FIXPT(CELSIUS(500)),
  93. .plaus_timeout_ms = 3000,
  94. };
  95. void meastemp_init(void)
  96. {
  97. #if CONF_ADJ
  98. measure_adjust_set(MEAS_CHAN_TEMP, get_settings()->temp_adj);
  99. #endif
  100. }