Repetier-Firmware  0.91
src/ArduinoAVR/Repetier/Printer.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Repetier-Firmware.
00003 
00004     Repetier-Firmware is free software: you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation, either version 3 of the License, or
00007     (at your option) any later version.
00008 
00009     Repetier-Firmware is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with Repetier-Firmware.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017     This firmware is a nearly complete rewrite of the sprinter firmware
00018     by kliment (https://github.com/kliment/Sprinter)
00019     which based on Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
00020 */
00021 
00022 #ifndef PRINTER_H_INCLUDED
00023 #define PRINTER_H_INCLUDED
00024 
00025 
00026 #define PRINTER_FLAG0_STEPPER_DISABLED      1
00027 #define PRINTER_FLAG0_SEPERATE_EXTRUDER_INT 2
00028 #define PRINTER_FLAG0_TEMPSENSOR_DEFECT     4
00029 #define PRINTER_FLAG0_FORCE_CHECKSUM        8
00030 #define PRINTER_FLAG0_MANUAL_MOVE_MODE      16
00031 #define PRINTER_FLAG0_AUTOLEVEL_ACTIVE      32
00032 #define PRINTER_FLAG0_ZPROBEING             64
00033 #define PRINTER_FLAG0_LARGE_MACHINE         128
00034 #define PRINTER_FLAG1_HOMED                 1
00035 #define PRINTER_FLAG1_AUTOMOUNT             2
00036 #define PRINTER_FLAG1_ANIMATION             4
00037 class Printer
00038 {
00039 public:
00040 #if defined(USE_ADVANCE)
00041     static volatile int extruderStepsNeeded; 
00042     static uint8_t minExtruderSpeed;            
00043     static uint8_t maxExtruderSpeed;            
00044     //static uint8_t extruderAccelerateDelay;     ///< delay between 2 speec increases
00045     static int advanceStepsSet;
00046 #ifdef ENABLE_QUADRATIC_ADVANCE
00047     static long advanceExecuted;             
00048 #endif
00049 #endif
00050     static uint8_t menuMode;
00051     static float axisStepsPerMM[];
00052     static float invAxisStepsPerMM[];
00053     static float maxFeedrate[];
00054     static float homingFeedrate[];
00055     static float maxAccelerationMMPerSquareSecond[];
00056     static float maxTravelAccelerationMMPerSquareSecond[];
00057     static unsigned long maxPrintAccelerationStepsPerSquareSecond[];
00058     static unsigned long maxTravelAccelerationStepsPerSquareSecond[];
00059     static uint8_t relativeCoordinateMode;    
00060     static uint8_t relativeExtruderCoordinateMode;  
00061 
00062     static uint8_t unitIsInches;
00063 
00064     static uint8_t debugLevel;
00065     static uint8_t flag0,flag1; // 1 = stepper disabled, 2 = use external extruder interrupt, 4 = temp Sensor defect, 8 = homed
00066     static uint8_t stepsPerTimerCall;
00067     static unsigned long interval;    
00068     static unsigned long timer;              
00069     static unsigned long stepNumber;         
00070     static float coordinateOffset[3];
00071     static long currentPositionSteps[4];     
00072     static float currentPosition[3];
00073     static long destinationSteps[4];         
00074 #if NONLINEAR_SYSTEM
00075     static long currentDeltaPositionSteps[4];
00076     static long maxDeltaPositionSteps;
00077     static long deltaDiagonalStepsSquared;
00078     static float deltaDiagonalStepsSquaredF;
00079     static long deltaAPosXSteps;
00080     static long deltaAPosYSteps;
00081     static long deltaBPosXSteps;
00082     static long deltaBPosYSteps;
00083     static long deltaCPosXSteps;
00084     static long deltaCPosYSteps;
00085 #endif
00086 #if FEATURE_Z_PROBE || MAX_HARDWARE_ENDSTOP_Z || DRIVE_SYSTEM==3
00087     static long stepsRemainingAtZHit;
00088 #endif
00089 #if DRIVE_SYSTEM==3
00090     static long stepsRemainingAtXHit;
00091     static long stepsRemainingAtYHit;
00092 #endif
00093 #ifdef SOFTWARE_LEVELING
00094     static long levelingP1[3];
00095     static long levelingP2[3];
00096     static long levelingP3[3];
00097 #endif
00098 #if FEATURE_AUTOLEVEL
00099     static float autolevelTransformation[9]; 
00100 #endif
00101     static float minimumSpeed;               
00102     static float minimumZSpeed;              
00103     static long xMaxSteps;                   
00104     static long yMaxSteps;                   
00105     static long zMaxSteps;                   
00106     static long xMinSteps;                   
00107     static long yMinSteps;                   
00108     static long zMinSteps;                   
00109     static float xLength;
00110     static float xMin;
00111     static float yLength;
00112     static float yMin;
00113     static float zLength;
00114     static float zMin;
00115     static float feedrate;                   
00116     static int feedrateMultiply;             
00117     static unsigned int extrudeMultiply;     
00118     static float maxJerk;                    
00119 #if DRIVE_SYSTEM!=3
00120     static float maxZJerk;                   
00121 #endif
00122     static float offsetX;                     
00123     static float offsetY;                     
00124     static unsigned int vMaxReached;         
00125     static unsigned long msecondsPrinting;            
00126     static float filamentPrinted;            
00127     static uint8_t wasLastHalfstepping;         
00128 #if ENABLE_BACKLASH_COMPENSATION
00129     static float backlashX;
00130     static float backlashY;
00131     static float backlashZ;
00132     static uint8_t backlashDir;
00133 #endif
00134 #ifdef DEBUG_STEPCOUNT
00135     static long totalStepsRemaining;
00136 #endif
00137 #if FEATURE_MEMORY_POSITION
00138     static long memoryX;
00139     static long memoryY;
00140     static long memoryZ;
00141     static long memoryE;
00142 #endif
00143 #ifdef XY_GANTRY
00144     static char motorX;
00145     static char motorY;
00146 #endif
00147     static inline void setMenuMode(uint8_t mode,bool on) {
00148         if(on)
00149             menuMode |= mode;
00150         else
00151             menuMode &= ~mode;
00152     }
00153     static inline bool debugEcho()
00154     {
00155         return ((debugLevel & 1)!=0);
00156     }
00157     static inline bool debugInfo()
00158     {
00159         return ((debugLevel & 2)!=0);
00160     }
00161     static inline bool debugErrors()
00162     {
00163         return ((debugLevel & 4)!=0);
00164     }
00165     static inline bool debugDryrun()
00166     {
00167         return ((debugLevel & 8)!=0);
00168     }
00169     static inline bool debugCommunication()
00170     {
00171         return ((debugLevel & 16)!=0);
00172     }
00173     static inline bool debugNoMoves() {
00174         return ((debugLevel & 32)!=0);
00175     }
00176 
00178     static inline void disableXStepper()
00179     {
00180 #if (X_ENABLE_PIN > -1)
00181         WRITE(X_ENABLE_PIN,!X_ENABLE_ON);
00182 #endif
00183 #if FEATURE_TWO_XSTEPPER && (X2_ENABLE_PIN > -1)
00184         WRITE(X2_ENABLE_PIN,!X_ENABLE_ON);
00185 #endif
00186     }
00188     static inline void disableYStepper()
00189     {
00190 #if (Y_ENABLE_PIN > -1)
00191         WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON);
00192 #endif
00193 #if FEATURE_TWO_YSTEPPER && (Y2_ENABLE_PIN > -1)
00194         WRITE(Y2_ENABLE_PIN,!Y_ENABLE_ON);
00195 #endif
00196     }
00198     static inline void disableZStepper()
00199     {
00200 #if (Z_ENABLE_PIN > -1)
00201         WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON);
00202 #endif
00203 #if FEATURE_TWO_ZSTEPPER && (Z2_ENABLE_PIN > -1)
00204         WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON);
00205 #endif
00206     }
00208     static inline void  enableXStepper()
00209     {
00210 #if (X_ENABLE_PIN > -1)
00211         WRITE(X_ENABLE_PIN, X_ENABLE_ON);
00212 #endif
00213 #if FEATURE_TWO_XSTEPPER && (X2_ENABLE_PIN > -1)
00214         WRITE(X2_ENABLE_PIN,X_ENABLE_ON);
00215 #endif
00216     }
00218     static inline void  enableYStepper()
00219     {
00220 #if (Y_ENABLE_PIN > -1)
00221         WRITE(Y_ENABLE_PIN, Y_ENABLE_ON);
00222 #endif
00223 #if FEATURE_TWO_YSTEPPER && (Y2_ENABLE_PIN > -1)
00224         WRITE(Y2_ENABLE_PIN,Y_ENABLE_ON);
00225 #endif
00226     }
00228     static inline void  enableZStepper()
00229     {
00230 #if (Z_ENABLE_PIN > -1)
00231         WRITE(Z_ENABLE_PIN, Z_ENABLE_ON);
00232 #endif
00233 #if FEATURE_TWO_ZSTEPPER && (Z2_ENABLE_PIN > -1)
00234         WRITE(Z2_ENABLE_PIN,Z_ENABLE_ON);
00235 #endif
00236     }
00237     static inline void setXDirection(bool positive)
00238     {
00239         if(positive)
00240         {
00241             WRITE(X_DIR_PIN,!INVERT_X_DIR);
00242 #if FEATURE_TWO_XSTEPPER
00243             WRITE(X2_DIR_PIN,!INVERT_X_DIR);
00244 #endif
00245         }
00246         else
00247         {
00248             WRITE(X_DIR_PIN,INVERT_X_DIR);
00249 #if FEATURE_TWO_XSTEPPER
00250             WRITE(X2_DIR_PIN,INVERT_X_DIR);
00251 #endif
00252         }
00253     }
00254     static inline void setYDirection(bool positive)
00255     {
00256         if(positive)
00257         {
00258             WRITE(Y_DIR_PIN,!INVERT_Y_DIR);
00259 #if FEATURE_TWO_YSTEPPER
00260             WRITE(Y2_DIR_PIN,!INVERT_Y_DIR);
00261 #endif
00262         }
00263         else
00264         {
00265             WRITE(Y_DIR_PIN,INVERT_Y_DIR);
00266 #if FEATURE_TWO_YSTEPPER
00267             WRITE(Y2_DIR_PIN,INVERT_Y_DIR);
00268 #endif
00269         }
00270     }
00271     static inline void setZDirection(bool positive)
00272     {
00273         if(positive)
00274         {
00275             WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
00276 #if FEATURE_TWO_ZSTEPPER
00277             WRITE(Z2_DIR_PIN,!INVERT_Z_DIR);
00278 #endif
00279         }
00280         else
00281         {
00282             WRITE(Z_DIR_PIN,INVERT_Z_DIR);
00283 #if FEATURE_TWO_ZSTEPPER
00284             WRITE(Z2_DIR_PIN,INVERT_Z_DIR);
00285 #endif
00286         }
00287     }
00288     static inline uint8_t isLargeMachine()
00289     {
00290         return flag0 & PRINTER_FLAG0_LARGE_MACHINE;
00291     }
00292     static inline void setLargeMachine(uint8_t b)
00293     {
00294         flag0 = (b ? flag0 | PRINTER_FLAG0_LARGE_MACHINE : flag0 & ~PRINTER_FLAG0_LARGE_MACHINE);
00295     }
00296     static inline uint8_t isAdvanceActivated()
00297     {
00298         return flag0 & PRINTER_FLAG0_SEPERATE_EXTRUDER_INT;
00299     }
00300     static inline void setAdvanceActivated(uint8_t b)
00301     {
00302         flag0 = (b ? flag0 | PRINTER_FLAG0_SEPERATE_EXTRUDER_INT : flag0 & ~PRINTER_FLAG0_SEPERATE_EXTRUDER_INT);
00303     }
00304     static inline uint8_t isHomed()
00305     {
00306         return flag1 & PRINTER_FLAG1_HOMED;
00307     }
00308     static inline void setHomed(uint8_t b)
00309     {
00310         flag1 = (b ? flag1 | PRINTER_FLAG1_HOMED : flag1 & ~PRINTER_FLAG1_HOMED);
00311     }
00312     static inline uint8_t isAutomount()
00313     {
00314         return flag1 & PRINTER_FLAG1_AUTOMOUNT;
00315     }
00316     static inline void setAutomount(uint8_t b)
00317     {
00318         flag1 = (b ? flag1 | PRINTER_FLAG1_AUTOMOUNT : flag1 & ~PRINTER_FLAG1_AUTOMOUNT);
00319     }
00320     static inline uint8_t isAnimation()
00321     {
00322         return flag1 & PRINTER_FLAG1_ANIMATION;
00323     }
00324     static inline void setAnimation(uint8_t b)
00325     {
00326         flag1 = (b ? flag1 | PRINTER_FLAG1_ANIMATION : flag1 & ~PRINTER_FLAG1_ANIMATION);
00327     }
00328     static inline void toggleAnimation() {
00329         setAnimation(!isAnimation());
00330     }
00331     static inline float convertToMM(float x)
00332     {
00333         return (unitIsInches ? x*25.4 : x);
00334     }
00335     static inline bool isXMinEndstopHit()
00336     {
00337 #if X_MIN_PIN>-1 && MIN_HARDWARE_ENDSTOP_X
00338         return READ(X_MIN_PIN) != ENDSTOP_X_MIN_INVERTING;
00339 #else
00340         return false;
00341 #endif
00342     }
00343     static inline bool isYMinEndstopHit()
00344     {
00345 #if Y_MIN_PIN>-1 && MIN_HARDWARE_ENDSTOP_Y
00346         return READ(Y_MIN_PIN) != ENDSTOP_Y_MIN_INVERTING;
00347 #else
00348         return false;
00349 #endif
00350     }
00351     static inline bool isZMinEndstopHit()
00352     {
00353 #if Z_MIN_PIN>-1 && MIN_HARDWARE_ENDSTOP_Z
00354         return READ(Z_MIN_PIN) != ENDSTOP_Z_MIN_INVERTING;
00355 #else
00356         return false;
00357 #endif
00358     }
00359     static inline bool isXMaxEndstopHit()
00360     {
00361 #if X_MAX_PIN>-1 && MAX_HARDWARE_ENDSTOP_X
00362         return READ(X_MAX_PIN) != ENDSTOP_X_MAX_INVERTING;
00363 #else
00364         return false;
00365 #endif
00366     }
00367     static inline bool isYMaxEndstopHit()
00368     {
00369 #if Y_MAX_PIN>-1 && MAX_HARDWARE_ENDSTOP_Y
00370         return READ(Y_MAX_PIN) != ENDSTOP_Y_MAX_INVERTING;
00371 #else
00372         return false;
00373 #endif
00374     }
00375     static inline bool isZMaxEndstopHit()
00376     {
00377 #if Z_MAX_PIN>-1 && MAX_HARDWARE_ENDSTOP_Z
00378         return READ(Z_MAX_PIN) != ENDSTOP_Z_MAX_INVERTING;
00379 #else
00380         return false;
00381 #endif
00382     }
00383     static inline bool areAllSteppersDisabled()
00384     {
00385         return flag0 & PRINTER_FLAG0_STEPPER_DISABLED;
00386     }
00387     static inline void setAllSteppersDiabled()
00388     {
00389         flag0 |= PRINTER_FLAG0_STEPPER_DISABLED;
00390     }
00391     static inline void unsetAllSteppersDisabled()
00392     {
00393         flag0 &= ~PRINTER_FLAG0_STEPPER_DISABLED;
00394     }
00395     static inline bool isAnyTempsensorDefect()
00396     {
00397         return (flag0 & PRINTER_FLAG0_TEMPSENSOR_DEFECT);
00398     }
00399     static inline bool isManualMoveMode()
00400     {
00401         return (flag0 & PRINTER_FLAG0_MANUAL_MOVE_MODE);
00402     }
00403     static inline void setManualMoveMode(bool on)
00404     {
00405         flag0 = (on ? flag0 | PRINTER_FLAG0_MANUAL_MOVE_MODE : flag0 & ~PRINTER_FLAG0_MANUAL_MOVE_MODE);
00406     }
00407     static inline bool isAutolevelActive()
00408     {
00409         return (flag0 & PRINTER_FLAG0_AUTOLEVEL_ACTIVE)!=0;
00410     }
00411     static void setAutolevelActive(bool on);
00412     static inline void setZProbingActive(bool on)
00413     {
00414         flag0 = (on ? flag0 | PRINTER_FLAG0_ZPROBEING : flag0 & ~PRINTER_FLAG0_ZPROBEING);
00415     }
00416     static inline bool isZProbingActive()
00417     {
00418         return (flag0 & PRINTER_FLAG0_ZPROBEING);
00419     }
00420     static inline bool isZProbeHit()
00421     {
00422 #if FEATURE_Z_PROBE
00423         return (Z_PROBE_ON_HIGH ? READ(Z_PROBE_PIN) : !READ(Z_PROBE_PIN));
00424 #else
00425         return false;
00426 #endif
00427     }
00428     static inline void executeXYGantrySteps()
00429     {
00430 #if defined(XY_GANTRY)
00431         if(motorX <= -2)
00432         {
00433             ANALYZER_ON(ANALYZER_CH2);
00434             WRITE(X_STEP_PIN,HIGH);
00435 #if FEATURE_TWO_XSTEPPER
00436             WRITE(X2_STEP_PIN,HIGH);
00437 #endif
00438             motorX += 2;
00439         }
00440         else if(motorX >= 2)
00441         {
00442             ANALYZER_ON(ANALYZER_CH2);
00443             WRITE(X_STEP_PIN,HIGH);
00444 #if FEATURE_TWO_XSTEPPER
00445             WRITE(X2_STEP_PIN,HIGH);
00446 #endif
00447             motorX -= 2;
00448         }
00449         if(motorY <= -2)
00450         {
00451             ANALYZER_ON(ANALYZER_CH3);
00452             WRITE(Y_STEP_PIN,HIGH);
00453 #if FEATURE_TWO_YSTEPPER
00454             WRITE(Y2_STEP_PIN,HIGH);
00455 #endif
00456             motorY += 2;
00457         }
00458         else if(motorY >= 2)
00459         {
00460             ANALYZER_ON(ANALYZER_CH3);
00461             WRITE(Y_STEP_PIN,HIGH);
00462 #if FEATURE_TWO_YSTEPPER
00463             WRITE(Y2_STEP_PIN,HIGH);
00464 #endif
00465             motorY -= 2;
00466         }
00467 #endif
00468     }
00469     static inline void endXYZSteps()
00470     {
00471         WRITE(X_STEP_PIN,LOW);
00472         WRITE(Y_STEP_PIN,LOW);
00473         WRITE(Z_STEP_PIN,LOW);
00474 #if FEATURE_TWO_XSTEPPER
00475         WRITE(X2_STEP_PIN,LOW);
00476 #endif
00477 #if FEATURE_TWO_YSTEPPER
00478         WRITE(Y2_STEP_PIN,LOW);
00479 #endif
00480 #if FEATURE_TWO_ZSTEPPER
00481         WRITE(Z2_STEP_PIN,LOW);
00482 #endif
00483         ANALYZER_OFF(ANALYZER_CH1);
00484         ANALYZER_OFF(ANALYZER_CH2);
00485         ANALYZER_OFF(ANALYZER_CH3);
00486         ANALYZER_OFF(ANALYZER_CH6);
00487         ANALYZER_OFF(ANALYZER_CH7);
00488     }
00489     static inline unsigned int updateStepsPerTimerCall(unsigned int vbase)
00490     {
00491         if(vbase>STEP_DOUBLER_FREQUENCY)
00492         {
00493 #if ALLOW_QUADSTEPPING
00494             if(vbase>STEP_DOUBLER_FREQUENCY*2)
00495             {
00496                 Printer::stepsPerTimerCall = 4;
00497                 return vbase>>2;
00498             }
00499             else
00500             {
00501                 Printer::stepsPerTimerCall = 2;
00502                 return vbase>>1;
00503             }
00504 #else
00505             Printer::stepsPerTimerCall = 2;
00506             return vbase>>1;
00507 #endif
00508         }
00509         else
00510         {
00511             Printer::stepsPerTimerCall = 1;
00512         }
00513         return vbase;
00514     }
00515     static inline void disableAllowedStepper()
00516     {
00517 #ifdef XY_GANTRY
00518         if(DISABLE_X && DISABLE_Y)
00519         {
00520             disableXStepper();
00521             disableYStepper();
00522         }
00523 #else
00524         if(DISABLE_X) disableXStepper();
00525         if(DISABLE_Y) disableYStepper();
00526 #endif
00527         if(DISABLE_Z) disableZStepper();
00528     }
00529     static inline float realXPosition()
00530     {
00531         return currentPosition[0];
00532     }
00533 
00534     static inline float realYPosition()
00535     {
00536         return currentPosition[1];
00537     }
00538 
00539     static inline float realZPosition()
00540     {
00541         return currentPosition[2];
00542     }
00543     static inline void realPosition(float &xp,float &yp,float &zp)
00544     {
00545         xp = currentPosition[X_AXIS];
00546         yp = currentPosition[Y_AXIS];
00547         zp = currentPosition[Z_AXIS];
00548     }
00549     static inline void insertStepperHighDelay() {
00550 #if STEPPER_HIGH_DELAY>0
00551         HAL::delayMicroseconds(STEPPER_HIGH_DELAY);
00552 #endif
00553     }
00554     static void constrainDestinationCoords();
00555     static void updateDerivedParameter();
00556     static void updateCurrentPosition();
00557     static void kill(uint8_t only_steppers);
00558     static void updateAdvanceFlags();
00559     static void setup();
00560     static void defaultLoopActions();
00561     static uint8_t setDestinationStepsFromGCode(GCode *com);
00562     static void moveTo(float x,float y,float z,float e,float f);
00563     static void moveToReal(float x,float y,float z,float e,float f);
00564     static void homeAxis(bool xaxis,bool yaxis,bool zaxis); 
00565     static void setOrigin(float xOff,float yOff,float zOff);
00566     static inline int getFanSpeed() {
00567         return (int)pwm_pos[NUM_EXTRUDER+2];
00568     }
00569 #if DRIVE_SYSTEM==3
00570     static inline void setDeltaPositions(long xaxis, long yaxis, long zaxis)
00571     {
00572         currentDeltaPositionSteps[0] = xaxis;
00573         currentDeltaPositionSteps[1] = yaxis;
00574         currentDeltaPositionSteps[2] = zaxis;
00575     }
00576     static void deltaMoveToTopEndstops(float feedrate);
00577 #endif
00578 #if MAX_HARDWARE_ENDSTOP_Z
00579     static float runZMaxProbe();
00580 #endif
00581 #if FEATURE_Z_PROBE
00582     static float runZProbe(bool first,bool last);
00583     static void waitForZProbeStart();
00584 #if FEATURE_AUTOLEVEL
00585     static void transformToPrinter(float x,float y,float z,float &transX,float &transY,float &transZ);
00586     static void transformFromPrinter(float x,float y,float z,float &transX,float &transY,float &transZ);
00587     static void resetTransformationMatrix(bool silent);
00588     static void buildTransformationMatrix(float h1,float h2,float h3);
00589 #endif
00590 #endif
00591 #if FEATURE_MEMORY_POSITION
00592     static void MemoryPosition();
00593     static void GoToMemoryPosition(bool x,bool y,bool z,bool e,float feed);
00594 #endif
00595 private:
00596     static void homeXAxis();
00597     static void homeYAxis();
00598     static void homeZAxis();
00599 };
00600 
00601 #endif // PRINTER_H_INCLUDED
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Defines