Take anycubic's approach to implement power outage support. SD card wearing can be prevented by disabling it via POWER_OUTAGE_TEST flag.

This commit is contained in:
Knutwurst
2020-06-11 15:36:23 +02:00
parent a52dc3c7e9
commit d090049594
7 changed files with 134 additions and 32 deletions

View File

@@ -40,6 +40,7 @@
#define KNUTWURST_MEGAS
#define KNUTWURST_TMC
//#define POWER_OUTAGE_TEST
//===========================================================================
//============================= Getting Started =============================

View File

@@ -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("<Z Down 0.1>");
break;
case 8: // Page 3
HARDWARE_SERIAL_PROTOCOLLNPGM("<Z Up 0.02>");
HARDWARE_SERIAL_PROTOCOLLNPGM("<Z Up 0.02>");
@@ -569,7 +592,6 @@ void AnycubicTouchscreenClass::Ls()
HARDWARE_SERIAL_PROTOCOLLNPGM("<Z Down 0.01>");
break;
case 12: // Page 4
HARDWARE_SERIAL_PROTOCOLLNPGM("<PID Tune Hotend>");
HARDWARE_SERIAL_PROTOCOLLNPGM("<PID Tune Hotend>");
@@ -581,7 +603,6 @@ void AnycubicTouchscreenClass::Ls()
HARDWARE_SERIAL_PROTOCOLLNPGM("<Load FW Defaults>");
break;
case 16: // Page 5
HARDWARE_SERIAL_PROTOCOLLNPGM("<Disable Fil. Sensor>");
HARDWARE_SERIAL_PROTOCOLLNPGM("<Disable Fil. Sensor>");
@@ -591,7 +612,6 @@ void AnycubicTouchscreenClass::Ls()
HARDWARE_SERIAL_PROTOCOLLNPGM("<Exit>");
break;
/*
case 12: // Fourth Page
HARDWARE_SERIAL_PROTOCOLLNPGM("<Z Up 0.1>");
@@ -651,13 +671,13 @@ 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]))
{
@@ -666,7 +686,6 @@ void AnycubicTouchscreenClass::Ls()
}
buffer[fileNameLen] = '\0';
if (card.flag.filenameIsDir)
{
HARDWARE_SERIAL_PROTOCOLPGM("/");
@@ -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

View File

@@ -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)

View File

@@ -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();

View File

@@ -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"
//

View File

@@ -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

View File

@@ -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; }