/firmware/src/shared/Heater.hh

http://github.com/makerbot/G3Firmware · C++ Header · 125 lines · 42 code · 21 blank · 62 comment · 0 complexity · 8c19ea55f8d503dd3a24be866ca16f4d MD5 · raw file

  1. /*
  2. * Copyright 2010 by Adam Mayer <adam@makerbot.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>
  16. */
  17. #ifndef HEATER_H
  18. #define HEATER_H
  19. #include "TemperatureSensor.hh"
  20. #include "HeatingElement.hh"
  21. #include "Pin.hh"
  22. #include "PID.hh"
  23. #include "Types.hh"
  24. #include "Timeout.hh"
  25. /// A heater object uses a #TemperatureSensor to control a #HeatingElement.
  26. /// It is primarily a #PID controller, but enters a bypass mode when the setpoint
  27. /// temperature is much higher than the current temperature. The #manage_temperature()
  28. /// function must be called periodically for it to be able to maintain accurate
  29. /// temperature control.
  30. /// \ingroup SoftwareLibraries
  31. class Heater
  32. {
  33. private:
  34. TemperatureSensor& sensor; ///< Sensor used to measure input temperature
  35. HeatingElement& element; ///< Heating element used to produce an output
  36. Timeout next_pid_timeout; ///< Timeout timer for PID loop (should be slower
  37. ///< or the same speed as sensor timeout)
  38. Timeout next_sense_timeout; ///< Timeout timer for sensor measurement
  39. micros_t sample_interval_micros; ///< Interval that the temperature sensor should
  40. ///< be updated at.
  41. // TODO: Delete this.
  42. int current_temperature; ///< Last known temperature reading
  43. uint16_t eeprom_base; ///< Base address to read EEPROM configuration from
  44. PID pid; ///< PID controller instance
  45. bool bypassing_PID; ///< True if the heater is in full on
  46. bool fail_state; ///< True if the heater has detected a hardware
  47. ///< failure and is shut down.
  48. uint8_t fail_count; ///< Count of the number of hardware failures that
  49. ///< have been reported by #getTemperature().
  50. ///< If this goes over #SENSOR_MAX_BAD_READINGS,
  51. ///< then the heater will go into a fail state.
  52. /// This is the interval between PID calculations. It doesn't make sense for
  53. /// this to be fast (<1 sec) because of the long system delay between heater
  54. /// and sensor.
  55. const static micros_t UPDATE_INTERVAL_MICROS = 500L * 1000L;
  56. /// Put the heater into a failure state, ensuring that the heating element is
  57. /// disabled.
  58. void fail();
  59. public:
  60. /// Instantiate a new heater object.
  61. /// \param[in] sensor #TemperatureSensor element to use as an input
  62. /// \param[in] element #HeatingElement to use as an output
  63. /// \param[in] sample_interval_micros Interval to sample the temperature sensor,
  64. /// in microseconds.
  65. /// \param[in] eeprom_base EEPROM address where the PID settings are stored.
  66. Heater(TemperatureSensor& sensor,
  67. HeatingElement& element,
  68. const micros_t sample_interval_micros,
  69. const uint16_t eeprom_base);
  70. /// Get the current sensor temperature
  71. /// \return Current sensor temperature, in degrees Celcius
  72. int get_current_temperature();
  73. /// Get the setpoint temperature
  74. /// \return Setpoint temperature, in degrees Celcius
  75. int get_set_temperature();
  76. /// Set the target output temperature
  77. /// \param temp New target temperature, in degrees Celcius.
  78. void set_target_temperature(int temp);
  79. /// Check if the heater is within the specified band
  80. /// \return True if the heater temperature is within #TARGET_HYSTERESIS degrees
  81. /// of the setpoint temperature.
  82. bool has_reached_target_temperature();
  83. /// Check if the heater is in a failure state
  84. /// \return true if the heater has failed.
  85. bool has_failed();
  86. /// Run the heater management loop. This must be called periodically,
  87. /// at a higher frequency than #sample_interval_micros.
  88. void manage_temperature();
  89. /// Change the setpoint temperature
  90. /// \param value New setpoint temperature, in degrees Celcius.
  91. void set_output(uint8_t value);
  92. /// Reset the heater to a to board-on state
  93. void reset();
  94. /// Get the current PID error term
  95. /// \return E term from the PID controller
  96. int getPIDErrorTerm();
  97. /// Get the current PID delta term
  98. /// \return D term from the PID controller
  99. int getPIDDeltaTerm();
  100. /// Get the last PID output
  101. /// \return last output from the PID controller
  102. int getPIDLastOutput();
  103. };
  104. #endif // HEATER_H