From d0900495947d65c360d1efd80e894e3d5710a838 Mon Sep 17 00:00:00 2001 From: Knutwurst <36196269+knutwurst@users.noreply.github.com> Date: Thu, 11 Jun 2020 15:36:23 +0200 Subject: [PATCH] Take anycubic's approach to implement power outage support. SD card wearing can be prevented by disabling it via POWER_OUTAGE_TEST flag. --- Marlin/Configuration.h | 1 + Marlin/src/lcd/anycubic_touchscreen.cpp | 101 ++++++++++++++------- Marlin/src/module/configuration_store.cpp | 44 +++++++++ Marlin/src/module/configuration_store.h | 9 ++ Marlin/src/pins/ramps/pins_TRIGORILLA_14.h | 5 + Marlin/src/sd/cardreader.cpp | 5 + Marlin/src/sd/cardreader.h | 1 + 7 files changed, 134 insertions(+), 32 deletions(-) diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h index 540ed9cf..a4910300 100755 --- a/Marlin/Configuration.h +++ b/Marlin/Configuration.h @@ -40,6 +40,7 @@ #define KNUTWURST_MEGAS #define KNUTWURST_TMC +//#define POWER_OUTAGE_TEST //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/src/lcd/anycubic_touchscreen.cpp b/Marlin/src/lcd/anycubic_touchscreen.cpp index a74c7993..718bd63b 100755 --- a/Marlin/src/lcd/anycubic_touchscreen.cpp +++ b/Marlin/src/lcd/anycubic_touchscreen.cpp @@ -38,13 +38,34 @@ #include "../module/stepper.h" #include "../module/temperature.h" #include "../sd/cardreader.h" +#include "../module/configuration_store.h" #ifdef ANYCUBIC_TOUCHSCREEN #include "anycubic_touchscreen.h" #include "HardwareSerial.h" +int Temp_Buf_Extuder_Temperature = 0; +int Temp_Buf_Bed_Temperature = 0; + char _conv[8]; + +unsigned char ResumingFlag = 0; + +#if defined(POWER_OUTAGE_TEST) +int PowerInt = 6; +unsigned char PowerTestFlag = false; +#endif + +void setup_OutageTestPin() +{ +#if defined(POWER_OUTAGE_TEST) + pinMode(OUTAGETEST_PIN, INPUT); + pinMode(OUTAGECON_PIN, OUTPUT); + WRITE(OUTAGECON_PIN, LOW); +#endif +} + char *itostr2(const uint8_t &x) { //sprintf(conv,"%5.1f",x); @@ -128,6 +149,9 @@ void AnycubicTouchscreenClass::Setup() buzzer.tone(250, 554); // C#5 buzzer.tone(500, 831); // G#5 #endif + + +setup_OutageTestPin(); } void AnycubicTouchscreenClass::WriteOutageEEPromData() @@ -557,7 +581,6 @@ void AnycubicTouchscreenClass::Ls() HARDWARE_SERIAL_PROTOCOLLNPGM(""); break; - case 8: // Page 3 HARDWARE_SERIAL_PROTOCOLLNPGM(""); HARDWARE_SERIAL_PROTOCOLLNPGM(""); @@ -569,7 +592,6 @@ void AnycubicTouchscreenClass::Ls() HARDWARE_SERIAL_PROTOCOLLNPGM(""); break; - case 12: // Page 4 HARDWARE_SERIAL_PROTOCOLLNPGM(""); HARDWARE_SERIAL_PROTOCOLLNPGM(""); @@ -581,7 +603,6 @@ void AnycubicTouchscreenClass::Ls() HARDWARE_SERIAL_PROTOCOLLNPGM(""); break; - case 16: // Page 5 HARDWARE_SERIAL_PROTOCOLLNPGM(""); HARDWARE_SERIAL_PROTOCOLLNPGM(""); @@ -591,8 +612,7 @@ void AnycubicTouchscreenClass::Ls() HARDWARE_SERIAL_PROTOCOLLNPGM(""); break; - -/* + /* case 12: // Fourth Page HARDWARE_SERIAL_PROTOCOLLNPGM(""); HARDWARE_SERIAL_PROTOCOLLNPGM(""); @@ -651,21 +671,20 @@ void AnycubicTouchscreenClass::Ls() { card.selectFileByIndex(cnt - 1); - // Bugfix for non-printable special characters // which are now replaced by underscores. int fileNameLen = strlen(card.longFilename); char buffer[fileNameLen]; - for (unsigned char i = 0; i < fileNameLen; i++){ + for (unsigned char i = 0; i < fileNameLen; i++) + { buffer[i] = card.longFilename[i]; - if (!isPrintable(buffer[i])) + if (!isPrintable(buffer[i])) { buffer[i] = '_'; } } buffer[fileNameLen] = '\0'; - if (card.flag.filenameIsDir) { @@ -874,9 +893,9 @@ void AnycubicTouchscreenClass::StateHandler() void AnycubicTouchscreenClass::FilamentRunout() { - if(FilamentSensorEnabled == true) + if (FilamentSensorEnabled == true) { - #if ENABLED(ANYCUBIC_FILAMENT_RUNOUT_SENSOR) +#if ENABLED(ANYCUBIC_FILAMENT_RUNOUT_SENSOR) FilamentTestStatus = READ(19) & 0xff; if (FilamentTestStatus > FilamentTestLastStatus) @@ -888,9 +907,9 @@ void AnycubicTouchscreenClass::FilamentRunout() // since this is inside a loop, only set delay time once if (FilamentSetMillis) { - #ifdef ANYCUBIC_TFT_DEBUG +#ifdef ANYCUBIC_TFT_DEBUG SERIAL_ECHOLNPGM("DEBUG: Set filament trigger time"); - #endif +#endif // set the delayed timestamp to 3000ms later fil_delay = fil_ms + 3000UL; // this doesn't need to run until the filament is recovered again @@ -903,26 +922,26 @@ void AnycubicTouchscreenClass::FilamentRunout() { if (!IsParked) { - #ifdef ANYCUBIC_TFT_DEBUG +#ifdef ANYCUBIC_TFT_DEBUG SERIAL_ECHOLNPGM("DEBUG: 3000ms delay done"); - #endif +#endif if (card.isPrinting()) { ai3m_pause_state = 3; ; // set runout pause flag - #ifdef ANYCUBIC_TFT_DEBUG +#ifdef ANYCUBIC_TFT_DEBUG SERIAL_ECHOPAIR(" DEBUG: AI3M Pause State: ", ai3m_pause_state); SERIAL_EOL(); - #endif +#endif PausePrint(); } else if (!card.isPrinting()) { HARDWARE_SERIAL_PROTOCOLPGM("J15"); //J15 FILAMENT LACK HARDWARE_SERIAL_ENTER(); - #ifdef ANYCUBIC_TFT_DEBUG +#ifdef ANYCUBIC_TFT_DEBUG SERIAL_ECHOLNPGM("TFT Serial Debug: Filament runout... J15"); - #endif +#endif FilamentTestLastStatus = FilamentTestStatus; } } @@ -933,11 +952,11 @@ void AnycubicTouchscreenClass::FilamentRunout() { FilamentSetMillis = true; // set the timestamps on the next loop again FilamentTestLastStatus = FilamentTestStatus; - #ifdef ANYCUBIC_TFT_DEBUG +#ifdef ANYCUBIC_TFT_DEBUG SERIAL_ECHOLNPGM("TFT Serial Debug: Filament runout recovered"); - #endif +#endif } - #endif +#endif } } @@ -1203,16 +1222,18 @@ void AnycubicTouchscreenClass::GetCommandFromTFT() #endif break; case 15: // A15 RESUMING FROM OUTAGE - //if((!planner.movesplanned())&&(!TFTresumingflag)) - // { - // if(card.isMounted()) - // FlagResumFromOutage=true; - // ResumingFlag=1; - // card.startFileprint(); - // starttime=millis(); - // HARDWARE_SERIAL_SUCC_START; - // } - //HARDWARE_SERIAL_ENTER(); + if ((!planner.movesplanned()) && (TFTstate != ANYCUBIC_TFT_STATE_SDPAUSE)) + { + if (card.isFileOpen()) + FlagResumFromOutage = true; + + ResumingFlag = 1; + card.startFileprint(); + starttime = millis(); + HARDWARE_SERIAL_SUCC_START; + } + HARDWARE_SERIAL_ENTER(); + break; case 16: // A16 set hotend temp { @@ -1547,5 +1568,21 @@ void AnycubicTouchscreenClass::BedHeatingDone() #endif } +void PowerKill() +{ +#ifdef POWER_OUTAGE_TEST + Temp_Buf_Extuder_Temperature = thermalManager.degTargetHotend(0); + Temp_Buf_Bed_Temperature = thermalManager.degTargetBed(); + if (PowerTestFlag == true) + { + thermalManager.disable_all_heaters(); + OutageSave(); + PowerTestFlag = false; + thermalManager.setTargetHotend(Temp_Buf_Extuder_Temperature, 0); + thermalManager.setTargetBed(Temp_Buf_Bed_Temperature); + } +#endif +} + AnycubicTouchscreenClass AnycubicTouchscreen; #endif diff --git a/Marlin/src/module/configuration_store.cpp b/Marlin/src/module/configuration_store.cpp index 2ecc9299..8ba2ffc3 100755 --- a/Marlin/src/module/configuration_store.cpp +++ b/Marlin/src/module/configuration_store.cpp @@ -56,6 +56,8 @@ #include "../gcode/gcode.h" #include "../MarlinCore.h" +#include "../sd/cardreader.h" + #if EITHER(EEPROM_SETTINGS, SD_FIRMWARE_UPDATE) #include "../HAL/shared/eeprom_api.h" #endif @@ -522,6 +524,9 @@ void MarlinSettings::postprocess() { #define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }while(0) #define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0) + #define EEPROM_WRITE_VAR(pos, value) persistentStore.write_data(pos, (uint8_t*)&value, sizeof(value)) + #define EEPROM_READ_VAR(pos, value) persistentStore.read_data(pos, (uint8_t*)&value, sizeof(value)) + #if ENABLED(DEBUG_EEPROM_READWRITE) #define _FIELD_TEST(FIELD) \ EEPROM_ASSERT( \ @@ -3682,6 +3687,45 @@ void MarlinSettings::reset() { #endif } + +#ifdef POWER_OUTAGE_TEST +float last_position[4] = { 0.0,0.0,0.0,0.0 }; +long last_sd_position[1] = { 0 }; + +void OutageSave() +{ + char ver[4] = "000"; + int j = 20; + EEPROM_WRITE_VAR(j,ver); + last_sd_position[0] = card.GetLastSDpos(); + last_position[0] = current_position[E_AXIS]; + last_position[1] = current_position[Z_AXIS]; + last_position[2] = current_position[Y_AXIS]; + last_position[3] = current_position[X_AXIS]; + + EEPROM_WRITE_VAR(j,last_sd_position[0]); + EEPROM_WRITE_VAR(j,last_position[0]); //E + EEPROM_WRITE_VAR(j,last_position[1]); //Z + EEPROM_WRITE_VAR(j,last_position[2]); //Y + EEPROM_WRITE_VAR(j,last_position[3]); //X +} + + +void OutageRead() +{ + int i = 20; + char stored_ver[4]; + char ver[4] = EEPROM_VERSION; + EEPROM_READ_VAR(i,stored_ver); + EEPROM_READ_VAR(i,last_sd_position[0]); + EEPROM_READ_VAR(i,last_position[0]); //E + EEPROM_READ_VAR(i,last_position[1]); //Z + EEPROM_READ_VAR(i,last_position[2]); //Y + EEPROM_READ_VAR(i,last_position[3]); //X +} + +#endif + #endif // !DISABLE_M503 #pragma pack(pop) diff --git a/Marlin/src/module/configuration_store.h b/Marlin/src/module/configuration_store.h index f2e84c96..258bf0e4 100755 --- a/Marlin/src/module/configuration_store.h +++ b/Marlin/src/module/configuration_store.h @@ -27,6 +27,15 @@ #include "../HAL/shared/eeprom_api.h" #endif +#ifdef POWER_OUTAGE_TEST + +static bool RestartFlag = false; +void OutageSave(); +void OutageRead(); +extern float last_position[4]; +extern long last_sd_position[1]; +#endif + class MarlinSettings { public: static uint16_t datasize(); diff --git a/Marlin/src/pins/ramps/pins_TRIGORILLA_14.h b/Marlin/src/pins/ramps/pins_TRIGORILLA_14.h index d38dd5a7..dee521fc 100755 --- a/Marlin/src/pins/ramps/pins_TRIGORILLA_14.h +++ b/Marlin/src/pins/ramps/pins_TRIGORILLA_14.h @@ -90,6 +90,11 @@ #define FAN2_PIN TG_FAN2_PIN #define ORIG_E0_AUTO_FAN_PIN TG_FAN2_PIN // Used in Anycubic Kossel example config +#ifdef POWER_OUTAGE_TEST + #define OUTAGETEST_PIN 79 + #define OUTAGECON_PIN 58 +#endif + #include "pins_RAMPS.h" // diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 80e94b88..e1702419 100755 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -34,6 +34,11 @@ #include "../gcode/queue.h" #include "../module/configuration_store.h" +#if defined(POWER_OUTAGE_TEST) +extern unsigned char PowerTestFlag; +extern char seekdataflag; +#endif + #if ENABLED(EMERGENCY_PARSER) #include "../feature/e_parser.h" #endif diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h index 955a8b69..0dd52de2 100755 --- a/Marlin/src/sd/cardreader.h +++ b/Marlin/src/sd/cardreader.h @@ -153,6 +153,7 @@ public: static inline int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); } static inline int16_t read(void* buf, uint16_t nbyte) { return file.isOpen() ? file.read(buf, nbyte) : -1; } static inline int16_t write(void* buf, uint16_t nbyte) { return file.isOpen() ? file.write(buf, nbyte) : -1; } + static inline long GetLastSDpos() { return sdpos; }; static Sd2Card& getSd2Card() { return sd2card; }