Skip to content

Commit

Permalink
New leveling rotation computation, watchdog handling
Browse files Browse the repository at this point in the history
  • Loading branch information
repetier committed Mar 24, 2016
1 parent 5a7a8f1 commit 6e81706
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 57 deletions.
62 changes: 44 additions & 18 deletions src/ArduinoAVR/Repetier/BedLeveling.cpp
Expand Up @@ -112,14 +112,7 @@ the bed it self. For deltas you can enable distortion correction to follow the b

#if FEATURE_AUTOLEVEL && FEATURE_Z_PROBE

class Plane {
public:
// f(x, y) = ax + by + c
float a,b,c;
float z(float x,float y) {
return a * x + y * b + c;
}
};

class PlaneBuilder {
float sum_xx,sum_xy,sum_yy,sum_x,sum_y,sum_xz,sum_yz,sum_z,n;
public:
Expand Down Expand Up @@ -230,13 +223,14 @@ bool measureAutolevelPlane(Plane &plane) {
#else
#error Unknown bed leveling method
#endif
builder.createPlane(plane);
builder.createPlane(plane,false);
return true;
}

void correctAutolevel(GCode *code,Plane &plane) {
#if BED_CORRECTION_METHOD == 0 // rotation matrix
Printer::buildTransformationMatrix(plane.z(EEPROM::zProbeX1(),EEPROM::zProbeY1()),plane.z(EEPROM::zProbeX2(),EEPROM::zProbeY2()),plane.z(EEPROM::zProbeX3(),EEPROM::zProbeY3()));
//Printer::buildTransformationMatrix(plane.z(EEPROM::zProbeX1(),EEPROM::zProbeY1()),plane.z(EEPROM::zProbeX2(),EEPROM::zProbeY2()),plane.z(EEPROM::zProbeX3(),EEPROM::zProbeY3()));
Printer::buildTransformationMatrix(plane);
#elif BED_CORRECTION_METHOD == 1 // motorized correction
#if !defined(NUM_MOTOR_DRIVERS) || NUM_MOTOR_DRIVERS < 2
#error You need to define 2 motors for motorized bed correction
Expand Down Expand Up @@ -549,7 +543,7 @@ float Printer::bendingCorrectionAt(float x, float y) {
builder.addPoint(EEPROM::zProbeX2(),EEPROM::zProbeY2(),EEPROM::bendingCorrectionB());
builder.addPoint(EEPROM::zProbeX3(),EEPROM::zProbeY3(),EEPROM::bendingCorrectionC());
Plane plane;
builder.createPlane(plane);
builder.createPlane(plane,true);
return plane.z(x,y);
}

Expand Down Expand Up @@ -616,13 +610,44 @@ void Printer::resetTransformationMatrix(bool silent) {
Com::printInfoFLN(Com::tAutolevelReset);
}

void Printer::buildTransformationMatrix(Plane &plane) {
float z0 = plane.z(0,0);
float az = z0-plane.z(1,0); // ax = 1, ay = 0
float bz = z0-plane.z(0,1); // bx = 0, by = 1
// First z direction
autolevelTransformation[6] = -az;
autolevelTransformation[7] = -bz;
autolevelTransformation[8] = 1;
float len = sqrt(az * az + bz * bz + 1);
autolevelTransformation[6] /= len;
autolevelTransformation[7] /= len;
autolevelTransformation[8] /= len;
autolevelTransformation[0] = 1;
autolevelTransformation[1] = 0;
autolevelTransformation[2] = -autolevelTransformation[6]/autolevelTransformation[8];
len = sqrt(autolevelTransformation[0] * autolevelTransformation[0] + autolevelTransformation[1] * autolevelTransformation[1] + autolevelTransformation[2] * autolevelTransformation[2]);
autolevelTransformation[0] /= len;
autolevelTransformation[1] /= len;
autolevelTransformation[2] /= len;
// cross(z,x) y,z)
autolevelTransformation[3] = autolevelTransformation[7] * autolevelTransformation[2] - autolevelTransformation[8] * autolevelTransformation[1];
autolevelTransformation[4] = autolevelTransformation[8] * autolevelTransformation[0] - autolevelTransformation[6] * autolevelTransformation[2];
autolevelTransformation[5] = autolevelTransformation[6] * autolevelTransformation[1] - autolevelTransformation[7] * autolevelTransformation[0];
len = sqrt(autolevelTransformation[3] * autolevelTransformation[3] + autolevelTransformation[4] * autolevelTransformation[4] + autolevelTransformation[5] * autolevelTransformation[5]);
autolevelTransformation[3] /= len;
autolevelTransformation[4] /= len;
autolevelTransformation[5] /= len;

Com::printArrayFLN(Com::tTransformationMatrix,autolevelTransformation, 9, 6);
}
/*
void Printer::buildTransformationMatrix(float h1,float h2,float h3) {
float ax = EEPROM::zProbeX2()-EEPROM::zProbeX1();
float ay = EEPROM::zProbeY2()-EEPROM::zProbeY1();
float az = h1-h2;
float bx = EEPROM::zProbeX3()-EEPROM::zProbeX1();
float by = EEPROM::zProbeY3()-EEPROM::zProbeY1();
float bz = h1-h3;
float ax = EEPROM::zProbeX2() - EEPROM::zProbeX1();
float ay = EEPROM::zProbeY2() - EEPROM::zProbeY1();
float az = h1 - h2;
float bx = EEPROM::zProbeX3() - EEPROM::zProbeX1();
float by = EEPROM::zProbeY3() - EEPROM::zProbeY1();
float bz = h1 - h3;
// First z direction
autolevelTransformation[6] = ay * bz - az * by;
autolevelTransformation[7] = az * bx - ax * bz;
Expand All @@ -638,7 +663,7 @@ void Printer::buildTransformationMatrix(float h1,float h2,float h3) {
// cross(y,z)
autolevelTransformation[0] = autolevelTransformation[4] * autolevelTransformation[8] - autolevelTransformation[5] * autolevelTransformation[7];
autolevelTransformation[1] = autolevelTransformation[5] * autolevelTransformation[6];// - autolevelTransformation[3] * autolevelTransformation[8];
autolevelTransformation[2] = /*autolevelTransformation[3] * autolevelTransformation[7]*/ - autolevelTransformation[4] * autolevelTransformation[6];
autolevelTransformation[2] = autolevelTransformation[3] * autolevelTransformation[7] - autolevelTransformation[4] * autolevelTransformation[6];
len = sqrt(autolevelTransformation[0] * autolevelTransformation[0] + autolevelTransformation[1] * autolevelTransformation[1] + autolevelTransformation[2] * autolevelTransformation[2]);
autolevelTransformation[0] /= len;
autolevelTransformation[1] /= len;
Expand All @@ -648,4 +673,5 @@ void Printer::buildTransformationMatrix(float h1,float h2,float h3) {
autolevelTransformation[5] /= len;
Com::printArrayFLN(Com::tTransformationMatrix,autolevelTransformation, 9, 6);
}
*/
#endif
5 changes: 5 additions & 0 deletions src/ArduinoAVR/Repetier/Commands.cpp
Expand Up @@ -2008,6 +2008,11 @@ void Commands::processMCode(GCode *com) {
case 281: // Trigger watchdog
#if FEATURE_WATCHDOG
{
if(com->hasX()) {
HAL::stopWatchdog();
Com::printFLN(PSTR("Watchdog disabled"));
break;
}
Com::printInfoFLN(PSTR("Triggering watchdog. If activated, the printer will reset."));
Printer::kill(false);
HAL::delayMilliseconds(200); // write output, make sure heaters are off for safety
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoAVR/Repetier/Extruder.cpp
Expand Up @@ -673,7 +673,7 @@ void Extruder::selectExtruderById(uint8_t extruderId)
void Extruder::setTemperatureForExtruder(float temperatureInCelsius, uint8_t extr, bool beep, bool wait)
{
#if NUM_EXTRUDER > 0
#if MIXING_EXTRUDER
#if MIXING_EXTRUDER || SHARED_EXTRUDER_HEATER
extr = 0; // map any virtual extruder number to 0
#endif // MIXING_EXTRUDER
bool alloffs = true;
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoAVR/Repetier/HAL.h
Expand Up @@ -713,7 +713,7 @@ class HAL
WDTCSR = (1<<WDCE) | (1<<WDE); // wdt FIX for arduino mega boards
WDTCSR = (1<<WDIE) | (1<<WDP3);
#else
wdt_enable(WDTO_1S);
wdt_enable(WDTO_4S);
#endif
};
inline static void stopWatchdog()
Expand Down
8 changes: 4 additions & 4 deletions src/ArduinoAVR/Repetier/Printer.cpp
Expand Up @@ -145,10 +145,10 @@ float Printer::backlashY;
float Printer::backlashZ;
uint8_t Printer::backlashDir;
#endif
float Printer::memoryX;
float Printer::memoryY;
float Printer::memoryZ;
float Printer::memoryE;
float Printer::memoryX = IGNORE_COORDINATE;
float Printer::memoryY = IGNORE_COORDINATE;
float Printer::memoryZ = IGNORE_COORDINATE;
float Printer::memoryE = IGNORE_COORDINATE;
float Printer::memoryF = -1;
#if GANTRY && !defined(FAST_COREXYZ)
int8_t Printer::motorX;
Expand Down
11 changes: 10 additions & 1 deletion src/ArduinoAVR/Repetier/Printer.h
Expand Up @@ -115,6 +115,14 @@ union wizardVar
#define towerBMinSteps Printer::yMinSteps
#define towerCMinSteps Printer::zMinSteps

class Plane {
public:
// f(x, y) = ax + by + c
float a,b,c;
float z(float x,float y) {
return a * x + y * b + c;
}
};
#if DISTORTION_CORRECTION
class Distortion
{
Expand Down Expand Up @@ -1111,7 +1119,8 @@ class Printer
static void transformToPrinter(float x,float y,float z,float &transX,float &transY,float &transZ);
static void transformFromPrinter(float x,float y,float z,float &transX,float &transY,float &transZ);
static void resetTransformationMatrix(bool silent);
static void buildTransformationMatrix(float h1,float h2,float h3);
//static void buildTransformationMatrix(float h1,float h2,float h3);
static void buildTransformationMatrix(Plane &plane);
#endif
#if DISTORTION_CORRECTION
static bool measureDistortion(void);
Expand Down
16 changes: 16 additions & 0 deletions src/ArduinoAVR/Repetier/Repetier.h
Expand Up @@ -197,6 +197,22 @@ usage or for searching for memory induced errors. Switch it off for production,

#include "Configuration.h"

#ifndef SHARED_EXTRUDER_HEATER
#define SHARED_EXTRUDER_HEATER 0
#endif
#if SHARED_EXTRUDER_HEATER || MIXING_EXTRUDER
#undef EXT1_HEATER_PIN
#undef EXT2_HEATER_PIN
#undef EXT3_HEATER_PIN
#undef EXT4_HEATER_PIN
#undef EXT5_HEATER_PIN
#define EXT1_HEATER_PIN -1
#define EXT2_HEATER_PIN -1
#define EXT3_HEATER_PIN -1
#define EXT4_HEATER_PIN -1
#define EXT5_HEATER_PIN -1
#endif

#ifndef MAX_JERK_DISTANCE
#define MAX_JERK_DISTANCE 0.6
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoAVR/Repetier/Repetier.ino
Expand Up @@ -117,7 +117,7 @@ Custom M Codes
- M233 X<AdvanceK> Y<AdvanceL> - Set temporary advance K-value to X and linear term advanceL to Y
- M251 Measure Z steps from homing stop (Delta printers). S0 - Reset, S1 - Print, S2 - Store to Z length (also EEPROM if enabled)
- M280 S<mode> - Set ditto printing mode. mode: 0 = off, 1 = 1 extra extruder, 2 = 2 extra extruder, 3 = 3 extra extruders
- M281 Test if watchdog is running and working.
- M281 Test if watchdog is running and working. Use M281 X0 to disable watchdog on AVR boards. Sometimes needed for boards with old bootloaders to allow reflashing.
- M300 S<Frequency> P<DurationMillis> play frequency
- M302 S<0 or 1> - allow cold extrusion. Without S parameter it will allow. S1 will disallow.
- M303 P<extruder/bed> S<printTemerature> X0 R<Repetitions>- Autodetect pid values. Use P<NUM_EXTRUDER> for heated bed. X0 saves result in EEPROM. R is number of cycles.
Expand Down
3 changes: 3 additions & 0 deletions src/ArduinoAVR/Repetier/SDCard.cpp
Expand Up @@ -78,9 +78,11 @@ void SDCard::initsd()
if(READ(SDCARDDETECT) != SDCARDDETECTINVERTED)
return;
#endif
HAL::pingWatchdog();
HAL::delayMilliseconds(50); // wait for stabilization of contacts, bootup ...
fat.begin(SDSS, SPI_FULL_SPEED); // dummy init of SD_CARD
HAL::delayMilliseconds(50); // wait for init end
HAL::pingWatchdog();
/*if(dir[0].isOpen())
dir[0].close();*/
if(!fat.begin(SDSS, SPI_FULL_SPEED))
Expand All @@ -91,6 +93,7 @@ void SDCard::initsd()
}
sdactive = true;
Printer::setMenuMode(MENU_MODE_SD_MOUNTED, true);
HAL::pingWatchdog();

fat.chdir();
if(selectFile("init.g", true))
Expand Down
1 change: 0 additions & 1 deletion src/ArduinoAVR/Repetier/motion.cpp
Expand Up @@ -2359,7 +2359,6 @@ int32_t PrintLine::bresenhamStep() // Version for delta printer
if(curd != NULL)
{
if(curd->checkEndstops(cur,(cur->isCheckEndstops()))) { // should stop move
Com::printFLN(PSTR("STop move endstop"));
cur->stepsRemaining = 0;
curd = NULL;
// eat up all following segments with moveID
Expand Down
3 changes: 1 addition & 2 deletions src/ArduinoAVR/Repetier/ui.h
Expand Up @@ -1856,7 +1856,7 @@ void uiCheckSlowKeys(uint16_t &action) {}
#define SPI_CHAN 0
#undef SDSUPPORT
#define SDSUPPORT 0 // sd card does not work reliable due to spi charing
#define SDSUPPORT 0 // sd card does not work reliable due to spi sharing
*/

#undef BEEPER_PIN
Expand All @@ -1883,7 +1883,6 @@ void uiCheckSlowKeys(uint16_t &action) {}
// PWM1 Pin
#define BLUE_STATUS_LED 5


#else
#error No predefined Viki 2 mapping for your board available
#endif
Expand Down
62 changes: 44 additions & 18 deletions src/ArduinoDUE/Repetier/BedLeveling.cpp
Expand Up @@ -112,14 +112,7 @@ the bed it self. For deltas you can enable distortion correction to follow the b

#if FEATURE_AUTOLEVEL && FEATURE_Z_PROBE

class Plane {
public:
// f(x, y) = ax + by + c
float a,b,c;
float z(float x,float y) {
return a * x + y * b + c;
}
};

class PlaneBuilder {
float sum_xx,sum_xy,sum_yy,sum_x,sum_y,sum_xz,sum_yz,sum_z,n;
public:
Expand Down Expand Up @@ -230,13 +223,14 @@ bool measureAutolevelPlane(Plane &plane) {
#else
#error Unknown bed leveling method
#endif
builder.createPlane(plane);
builder.createPlane(plane,false);
return true;
}

void correctAutolevel(GCode *code,Plane &plane) {
#if BED_CORRECTION_METHOD == 0 // rotation matrix
Printer::buildTransformationMatrix(plane.z(EEPROM::zProbeX1(),EEPROM::zProbeY1()),plane.z(EEPROM::zProbeX2(),EEPROM::zProbeY2()),plane.z(EEPROM::zProbeX3(),EEPROM::zProbeY3()));
//Printer::buildTransformationMatrix(plane.z(EEPROM::zProbeX1(),EEPROM::zProbeY1()),plane.z(EEPROM::zProbeX2(),EEPROM::zProbeY2()),plane.z(EEPROM::zProbeX3(),EEPROM::zProbeY3()));
Printer::buildTransformationMatrix(plane);
#elif BED_CORRECTION_METHOD == 1 // motorized correction
#if !defined(NUM_MOTOR_DRIVERS) || NUM_MOTOR_DRIVERS < 2
#error You need to define 2 motors for motorized bed correction
Expand Down Expand Up @@ -549,7 +543,7 @@ float Printer::bendingCorrectionAt(float x, float y) {
builder.addPoint(EEPROM::zProbeX2(),EEPROM::zProbeY2(),EEPROM::bendingCorrectionB());
builder.addPoint(EEPROM::zProbeX3(),EEPROM::zProbeY3(),EEPROM::bendingCorrectionC());
Plane plane;
builder.createPlane(plane);
builder.createPlane(plane,true);
return plane.z(x,y);
}

Expand Down Expand Up @@ -616,13 +610,44 @@ void Printer::resetTransformationMatrix(bool silent) {
Com::printInfoFLN(Com::tAutolevelReset);
}

void Printer::buildTransformationMatrix(Plane &plane) {
float z0 = plane.z(0,0);
float az = z0-plane.z(1,0); // ax = 1, ay = 0
float bz = z0-plane.z(0,1); // bx = 0, by = 1
// First z direction
autolevelTransformation[6] = -az;
autolevelTransformation[7] = -bz;
autolevelTransformation[8] = 1;
float len = sqrt(az * az + bz * bz + 1);
autolevelTransformation[6] /= len;
autolevelTransformation[7] /= len;
autolevelTransformation[8] /= len;
autolevelTransformation[0] = 1;
autolevelTransformation[1] = 0;
autolevelTransformation[2] = -autolevelTransformation[6]/autolevelTransformation[8];
len = sqrt(autolevelTransformation[0] * autolevelTransformation[0] + autolevelTransformation[1] * autolevelTransformation[1] + autolevelTransformation[2] * autolevelTransformation[2]);
autolevelTransformation[0] /= len;
autolevelTransformation[1] /= len;
autolevelTransformation[2] /= len;
// cross(z,x) y,z)
autolevelTransformation[3] = autolevelTransformation[7] * autolevelTransformation[2] - autolevelTransformation[8] * autolevelTransformation[1];
autolevelTransformation[4] = autolevelTransformation[8] * autolevelTransformation[0] - autolevelTransformation[6] * autolevelTransformation[2];
autolevelTransformation[5] = autolevelTransformation[6] * autolevelTransformation[1] - autolevelTransformation[7] * autolevelTransformation[0];
len = sqrt(autolevelTransformation[3] * autolevelTransformation[3] + autolevelTransformation[4] * autolevelTransformation[4] + autolevelTransformation[5] * autolevelTransformation[5]);
autolevelTransformation[3] /= len;
autolevelTransformation[4] /= len;
autolevelTransformation[5] /= len;

Com::printArrayFLN(Com::tTransformationMatrix,autolevelTransformation, 9, 6);
}
/*
void Printer::buildTransformationMatrix(float h1,float h2,float h3) {
float ax = EEPROM::zProbeX2()-EEPROM::zProbeX1();
float ay = EEPROM::zProbeY2()-EEPROM::zProbeY1();
float az = h1-h2;
float bx = EEPROM::zProbeX3()-EEPROM::zProbeX1();
float by = EEPROM::zProbeY3()-EEPROM::zProbeY1();
float bz = h1-h3;
float ax = EEPROM::zProbeX2() - EEPROM::zProbeX1();
float ay = EEPROM::zProbeY2() - EEPROM::zProbeY1();
float az = h1 - h2;
float bx = EEPROM::zProbeX3() - EEPROM::zProbeX1();
float by = EEPROM::zProbeY3() - EEPROM::zProbeY1();
float bz = h1 - h3;
// First z direction
autolevelTransformation[6] = ay * bz - az * by;
autolevelTransformation[7] = az * bx - ax * bz;
Expand All @@ -638,7 +663,7 @@ void Printer::buildTransformationMatrix(float h1,float h2,float h3) {
// cross(y,z)
autolevelTransformation[0] = autolevelTransformation[4] * autolevelTransformation[8] - autolevelTransformation[5] * autolevelTransformation[7];
autolevelTransformation[1] = autolevelTransformation[5] * autolevelTransformation[6];// - autolevelTransformation[3] * autolevelTransformation[8];
autolevelTransformation[2] = /*autolevelTransformation[3] * autolevelTransformation[7]*/ - autolevelTransformation[4] * autolevelTransformation[6];
autolevelTransformation[2] = autolevelTransformation[3] * autolevelTransformation[7] - autolevelTransformation[4] * autolevelTransformation[6];
len = sqrt(autolevelTransformation[0] * autolevelTransformation[0] + autolevelTransformation[1] * autolevelTransformation[1] + autolevelTransformation[2] * autolevelTransformation[2]);
autolevelTransformation[0] /= len;
autolevelTransformation[1] /= len;
Expand All @@ -648,4 +673,5 @@ void Printer::buildTransformationMatrix(float h1,float h2,float h3) {
autolevelTransformation[5] /= len;
Com::printArrayFLN(Com::tTransformationMatrix,autolevelTransformation, 9, 6);
}
*/
#endif
5 changes: 5 additions & 0 deletions src/ArduinoDUE/Repetier/Commands.cpp
Expand Up @@ -2008,6 +2008,11 @@ void Commands::processMCode(GCode *com) {
case 281: // Trigger watchdog
#if FEATURE_WATCHDOG
{
if(com->hasX()) {
HAL::stopWatchdog();
Com::printFLN(PSTR("Watchdog disabled"));
break;
}
Com::printInfoFLN(PSTR("Triggering watchdog. If activated, the printer will reset."));
Printer::kill(false);
HAL::delayMilliseconds(200); // write output, make sure heaters are off for safety
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoDUE/Repetier/Extruder.cpp
Expand Up @@ -673,7 +673,7 @@ void Extruder::selectExtruderById(uint8_t extruderId)
void Extruder::setTemperatureForExtruder(float temperatureInCelsius, uint8_t extr, bool beep, bool wait)
{
#if NUM_EXTRUDER > 0
#if MIXING_EXTRUDER
#if MIXING_EXTRUDER || SHARED_EXTRUDER_HEATER
extr = 0; // map any virtual extruder number to 0
#endif // MIXING_EXTRUDER
bool alloffs = true;
Expand Down
8 changes: 4 additions & 4 deletions src/ArduinoDUE/Repetier/Printer.cpp
Expand Up @@ -145,10 +145,10 @@ float Printer::backlashY;
float Printer::backlashZ;
uint8_t Printer::backlashDir;
#endif
float Printer::memoryX;
float Printer::memoryY;
float Printer::memoryZ;
float Printer::memoryE;
float Printer::memoryX = IGNORE_COORDINATE;
float Printer::memoryY = IGNORE_COORDINATE;
float Printer::memoryZ = IGNORE_COORDINATE;
float Printer::memoryE = IGNORE_COORDINATE;
float Printer::memoryF = -1;
#if GANTRY && !defined(FAST_COREXYZ)
int8_t Printer::motorX;
Expand Down

0 comments on commit 6e81706

Please sign in to comment.