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.

powerloss.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #pragma once
  23. /**
  24. * feature/powerloss.h - Resume an SD print after power-loss
  25. */
  26. #include "../sd/cardreader.h"
  27. #include "../gcode/gcode.h"
  28. #include "../inc/MarlinConfig.h"
  29. #if ENABLED(GCODE_REPEAT_MARKERS)
  30. #include "../feature/repeat.h"
  31. #endif
  32. #if ENABLED(MIXING_EXTRUDER)
  33. #include "../feature/mixing.h"
  34. #endif
  35. #if !defined(POWER_LOSS_STATE) && PIN_EXISTS(POWER_LOSS)
  36. #define POWER_LOSS_STATE HIGH
  37. #endif
  38. #ifndef POWER_LOSS_ZRAISE
  39. #define POWER_LOSS_ZRAISE 2
  40. #endif
  41. //#define DEBUG_POWER_LOSS_RECOVERY
  42. //#define SAVE_EACH_CMD_MODE
  43. //#define SAVE_INFO_INTERVAL_MS 0
  44. typedef struct {
  45. uint8_t valid_head;
  46. // Machine state
  47. xyze_pos_t current_position;
  48. uint16_t feedrate;
  49. float zraise;
  50. // Repeat information
  51. #if ENABLED(GCODE_REPEAT_MARKERS)
  52. Repeat stored_repeat;
  53. #endif
  54. #if HAS_HOME_OFFSET
  55. xyz_pos_t home_offset;
  56. #endif
  57. #if HAS_POSITION_SHIFT
  58. xyz_pos_t position_shift;
  59. #endif
  60. #if HAS_MULTI_EXTRUDER
  61. uint8_t active_extruder;
  62. #endif
  63. #if DISABLED(NO_VOLUMETRICS)
  64. float filament_size[EXTRUDERS];
  65. #endif
  66. #if HAS_HOTEND
  67. celsius_t target_temperature[HOTENDS];
  68. #endif
  69. #if HAS_HEATED_BED
  70. celsius_t target_temperature_bed;
  71. #endif
  72. #if HAS_FAN
  73. uint8_t fan_speed[FAN_COUNT];
  74. #endif
  75. #if HAS_LEVELING
  76. float fade;
  77. #endif
  78. #if ENABLED(FWRETRACT)
  79. float retract[EXTRUDERS], retract_hop;
  80. #endif
  81. // Mixing extruder and gradient
  82. #if ENABLED(MIXING_EXTRUDER)
  83. //uint_fast8_t selected_vtool;
  84. //mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  85. #if ENABLED(GRADIENT_MIX)
  86. gradient_t gradient;
  87. #endif
  88. #endif
  89. // SD Filename and position
  90. char sd_filename[MAXPATHNAMELENGTH];
  91. volatile uint32_t sdpos;
  92. // Job elapsed time
  93. millis_t print_job_elapsed;
  94. // Relative axis modes
  95. uint8_t axis_relative;
  96. // Misc. Marlin flags
  97. struct {
  98. bool raised:1; // Raised before saved
  99. bool dryrun:1; // M111 S8
  100. bool allow_cold_extrusion:1; // M302 P1
  101. #if HAS_LEVELING
  102. bool leveling:1; // M420 S
  103. #endif
  104. #if DISABLED(NO_VOLUMETRICS)
  105. bool volumetric_enabled:1; // M200 S D
  106. #endif
  107. } flag;
  108. uint8_t valid_foot;
  109. bool valid() { return valid_head && valid_head == valid_foot; }
  110. } job_recovery_info_t;
  111. class PrintJobRecovery {
  112. public:
  113. static const char filename[5];
  114. static SdFile file;
  115. static job_recovery_info_t info;
  116. static uint8_t queue_index_r; //!< Queue index of the active command
  117. static uint32_t cmd_sdpos, //!< SD position of the next command
  118. sdpos[BUFSIZE]; //!< SD positions of queued commands
  119. #if HAS_DWIN_E3V2_BASIC
  120. static bool dwin_flag;
  121. #endif
  122. static void init();
  123. static void prepare();
  124. static void setup() {
  125. #if PIN_EXISTS(POWER_LOSS)
  126. #if ENABLED(POWER_LOSS_PULLUP)
  127. SET_INPUT_PULLUP(POWER_LOSS_PIN);
  128. #elif ENABLED(POWER_LOSS_PULLDOWN)
  129. SET_INPUT_PULLDOWN(POWER_LOSS_PIN);
  130. #else
  131. SET_INPUT(POWER_LOSS_PIN);
  132. #endif
  133. #endif
  134. }
  135. // Track each command's file offsets
  136. static uint32_t command_sdpos() { return sdpos[queue_index_r]; }
  137. static void commit_sdpos(const uint8_t index_w) { sdpos[index_w] = cmd_sdpos; }
  138. static bool enabled;
  139. static void enable(const bool onoff);
  140. static void changed();
  141. static bool exists() { return card.jobRecoverFileExists(); }
  142. static void open(const bool read) { card.openJobRecoveryFile(read); }
  143. static void close() { file.close(); }
  144. static bool check();
  145. static void resume();
  146. static void purge();
  147. static void cancel() { purge(); }
  148. static void load();
  149. static void save(const bool force=ENABLED(SAVE_EACH_CMD_MODE), const float zraise=POWER_LOSS_ZRAISE, const bool raised=false);
  150. #if PIN_EXISTS(POWER_LOSS)
  151. static void outage() {
  152. static constexpr uint8_t OUTAGE_THRESHOLD = 3;
  153. static uint8_t outage_counter = 0;
  154. if (enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) {
  155. outage_counter++;
  156. if (outage_counter >= OUTAGE_THRESHOLD) _outage();
  157. }
  158. else
  159. outage_counter = 0;
  160. }
  161. #endif
  162. // The referenced file exists
  163. static bool interrupted_file_exists() { return card.fileExists(info.sd_filename); }
  164. static bool valid() { return info.valid() && interrupted_file_exists(); }
  165. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  166. static void debug(FSTR_P const prefix);
  167. #else
  168. static void debug(FSTR_P const) {}
  169. #endif
  170. private:
  171. static void write();
  172. #if ENABLED(BACKUP_POWER_SUPPLY)
  173. static void retract_and_lift(const_float_t zraise);
  174. #endif
  175. #if PIN_EXISTS(POWER_LOSS) || ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  176. friend class GcodeSuite;
  177. static void _outage(TERN_(DEBUG_POWER_LOSS_RECOVERY, const bool simulated=false));
  178. #endif
  179. };
  180. extern PrintJobRecovery recovery;