My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

power.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * power.cpp - power control
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if ENABLED(AUTO_POWER_CONTROL)
  27. #include "power.h"
  28. #include "../module/temperature.h"
  29. #include "../module/stepper/indirection.h"
  30. #include "../MarlinCore.h"
  31. #if defined(PSU_POWERUP_GCODE) || defined(PSU_POWEROFF_GCODE)
  32. #include "../gcode/gcode.h"
  33. #endif
  34. #if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
  35. #include "controllerfan.h"
  36. #endif
  37. Power powerManager;
  38. millis_t Power::lastPowerOn;
  39. bool Power::is_power_needed() {
  40. #if ENABLED(AUTO_POWER_FANS)
  41. FANS_LOOP(i) if (thermalManager.fan_speed[i]) return true;
  42. #endif
  43. #if ENABLED(AUTO_POWER_E_FANS)
  44. HOTEND_LOOP() if (thermalManager.autofan_speed[e]) return true;
  45. #endif
  46. #if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
  47. if (controllerFan.state()) return true;
  48. #endif
  49. if (TERN0(AUTO_POWER_CHAMBER_FAN, thermalManager.chamberfan_speed))
  50. return true;
  51. // If any of the drivers or the bed are enabled...
  52. if (X_ENABLE_READ() == X_ENABLE_ON || Y_ENABLE_READ() == Y_ENABLE_ON || Z_ENABLE_READ() == Z_ENABLE_ON
  53. #if HAS_X2_ENABLE
  54. || X2_ENABLE_READ() == X_ENABLE_ON
  55. #endif
  56. #if HAS_Y2_ENABLE
  57. || Y2_ENABLE_READ() == Y_ENABLE_ON
  58. #endif
  59. #if HAS_Z2_ENABLE
  60. || Z2_ENABLE_READ() == Z_ENABLE_ON
  61. #endif
  62. #if E_STEPPERS
  63. #define _OR_ENABLED_E(N) || E##N##_ENABLE_READ() == E_ENABLE_ON
  64. REPEAT(E_STEPPERS, _OR_ENABLED_E)
  65. #endif
  66. ) return true;
  67. HOTEND_LOOP() if (thermalManager.degTargetHotend(e) > 0 || thermalManager.temp_hotend[e].soft_pwm_amount > 0) return true;
  68. if (TERN0(HAS_HEATED_BED, thermalManager.degTargetBed() > 0 || thermalManager.temp_bed.soft_pwm_amount > 0)) return true;
  69. #if HAS_HOTEND && AUTO_POWER_E_TEMP
  70. HOTEND_LOOP() if (thermalManager.degHotend(e) >= AUTO_POWER_E_TEMP) return true;
  71. #endif
  72. #if HAS_HEATED_CHAMBER && AUTO_POWER_CHAMBER_TEMP
  73. if (thermalManager.degChamber() >= AUTO_POWER_CHAMBER_TEMP) return true;
  74. #endif
  75. return false;
  76. }
  77. void Power::check() {
  78. static millis_t nextPowerCheck = 0;
  79. millis_t ms = millis();
  80. if (ELAPSED(ms, nextPowerCheck)) {
  81. nextPowerCheck = ms + 2500UL;
  82. if (is_power_needed())
  83. power_on();
  84. else if (!lastPowerOn || ELAPSED(ms, lastPowerOn + SEC_TO_MS(POWER_TIMEOUT)))
  85. power_off();
  86. }
  87. }
  88. void Power::power_on() {
  89. lastPowerOn = millis();
  90. if (!powersupply_on) {
  91. PSU_PIN_ON();
  92. safe_delay(PSU_POWERUP_DELAY);
  93. restore_stepper_drivers();
  94. TERN_(HAS_TRINAMIC_CONFIG, safe_delay(PSU_POWERUP_DELAY));
  95. #ifdef PSU_POWERUP_GCODE
  96. GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWERUP_GCODE));
  97. #endif
  98. }
  99. }
  100. void Power::power_off() {
  101. if (powersupply_on) {
  102. #ifdef PSU_POWEROFF_GCODE
  103. GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWEROFF_GCODE));
  104. #endif
  105. PSU_PIN_OFF();
  106. }
  107. }
  108. void Power::power_off_soon() {
  109. #if POWER_OFF_DELAY
  110. lastPowerOn = millis() - SEC_TO_MS(POWER_TIMEOUT) + SEC_TO_MS(POWER_OFF_DELAY);
  111. #else
  112. power_off();
  113. #endif
  114. }
  115. #endif // AUTO_POWER_CONTROL