My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Marlin_main.cpp 90KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779
  1. /* -*- c++ -*- */
  2. /*
  3. Reprap firmware based on Sprinter and grbl.
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include "Marlin.h"
  24. #include "ultralcd.h"
  25. #include "planner.h"
  26. #include "stepper.h"
  27. #include "temperature.h"
  28. #include "motion_control.h"
  29. #include "cardreader.h"
  30. #include "watchdog.h"
  31. #include "ConfigurationStore.h"
  32. #include "language.h"
  33. #include "pins_arduino.h"
  34. #if NUM_SERVOS > 0
  35. #include "Servo.h"
  36. #endif
  37. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  38. #include <SPI.h>
  39. #endif
  40. #define VERSION_STRING "1.0.0"
  41. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  42. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  43. //Implemented Codes
  44. //-------------------
  45. // G0 -> G1
  46. // G1 - Coordinated Movement X Y Z E
  47. // G2 - CW ARC
  48. // G3 - CCW ARC
  49. // G4 - Dwell S<seconds> or P<milliseconds>
  50. // G10 - retract filament according to settings of M207
  51. // G11 - retract recover filament according to settings of M208
  52. // G28 - Home all Axis
  53. // G90 - Use Absolute Coordinates
  54. // G91 - Use Relative Coordinates
  55. // G92 - Set current position to cordinates given
  56. // M Codes
  57. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  58. // M1 - Same as M0
  59. // M17 - Enable/Power all stepper motors
  60. // M18 - Disable all stepper motors; same as M84
  61. // M20 - List SD card
  62. // M21 - Init SD card
  63. // M22 - Release SD card
  64. // M23 - Select SD file (M23 filename.g)
  65. // M24 - Start/resume SD print
  66. // M25 - Pause SD print
  67. // M26 - Set SD position in bytes (M26 S12345)
  68. // M27 - Report SD print status
  69. // M28 - Start SD write (M28 filename.g)
  70. // M29 - Stop SD write
  71. // M30 - Delete file from SD (M30 filename.g)
  72. // M31 - Output time since last M109 or SD card start to serial
  73. // M32 - Select file and start SD print (Can be used when printing from SD card)
  74. // M42 - Change pin status via gcode Use M42 Px Sy to set pin x to value y, when omitting Px the onboard led will be used.
  75. // M80 - Turn on Power Supply
  76. // M81 - Turn off Power Supply
  77. // M82 - Set E codes absolute (default)
  78. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  79. // M84 - Disable steppers until next move,
  80. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  81. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  82. // M92 - Set axis_steps_per_unit - same syntax as G92
  83. // M104 - Set extruder target temp
  84. // M105 - Read current temp
  85. // M106 - Fan on
  86. // M107 - Fan off
  87. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  88. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  89. // M114 - Output current position to serial port
  90. // M115 - Capabilities string
  91. // M117 - display message
  92. // M119 - Output Endstop status to serial port
  93. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  94. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  95. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  96. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  97. // M140 - Set bed target temp
  98. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  99. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  100. // M200 - Set filament diameter
  101. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  102. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  103. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  104. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  105. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
  106. // M206 - set additional homeing offset
  107. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  108. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  109. // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  110. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  111. // M220 S<factor in percent>- set speed factor override percentage
  112. // M221 S<factor in percent>- set extrude factor override percentage
  113. // M240 - Trigger a camera to take a photograph
  114. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  115. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  116. // M300 - Play beepsound S<frequency Hz> P<duration ms>
  117. // M301 - Set PID parameters P I and D
  118. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  119. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  120. // M304 - Set bed PID parameters P I and D
  121. // M400 - Finish all moves
  122. // M500 - stores paramters in EEPROM
  123. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  124. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  125. // M503 - print the current settings (from memory not from eeprom)
  126. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  127. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  128. // M666 - set delta endstop adjustemnt
  129. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  130. // M907 - Set digital trimpot motor current using axis codes.
  131. // M908 - Control digital trimpot directly.
  132. // M350 - Set microstepping mode.
  133. // M351 - Toggle MS1 MS2 pins directly.
  134. // M928 - Start SD logging (M928 filename.g) - ended by M29
  135. // M999 - Restart after being stopped by error
  136. //Stepper Movement Variables
  137. //===========================================================================
  138. //=============================imported variables============================
  139. //===========================================================================
  140. //===========================================================================
  141. //=============================public variables=============================
  142. //===========================================================================
  143. #ifdef SDSUPPORT
  144. CardReader card;
  145. #endif
  146. float homing_feedrate[] = HOMING_FEEDRATE;
  147. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  148. int feedmultiply=100; //100->1 200->2
  149. int saved_feedmultiply;
  150. int extrudemultiply=100; //100->1 200->2
  151. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  152. float add_homeing[3]={0,0,0};
  153. #ifdef DELTA
  154. float endstop_adj[3]={0,0,0};
  155. #endif
  156. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  157. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  158. // Extruder offset
  159. #if EXTRUDERS > 1
  160. #ifndef DUAL_X_CARRIAGE
  161. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  162. #else
  163. #define NUM_EXTRUDER_OFFSETS 3 // supports offsets in XYZ plane
  164. #endif
  165. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  166. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  167. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  168. #endif
  169. };
  170. #endif
  171. uint8_t active_extruder = 0;
  172. int fanSpeed=0;
  173. #ifdef SERVO_ENDSTOPS
  174. int servo_endstops[] = SERVO_ENDSTOPS;
  175. int servo_endstop_angles[] = SERVO_ENDSTOP_ANGLES;
  176. #endif
  177. #ifdef BARICUDA
  178. int ValvePressure=0;
  179. int EtoPPressure=0;
  180. #endif
  181. #ifdef FWRETRACT
  182. bool autoretract_enabled=true;
  183. bool retracted=false;
  184. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  185. float retract_recover_length=0, retract_recover_feedrate=8*60;
  186. #endif
  187. #ifdef ULTIPANEL
  188. bool powersupply = true;
  189. #endif
  190. #ifdef DELTA
  191. float delta[3] = {0.0, 0.0, 0.0};
  192. #endif
  193. //===========================================================================
  194. //=============================private variables=============================
  195. //===========================================================================
  196. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  197. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  198. static float offset[3] = {0.0, 0.0, 0.0};
  199. static bool home_all_axis = true;
  200. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  201. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  202. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  203. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  204. static bool fromsd[BUFSIZE];
  205. static int bufindr = 0;
  206. static int bufindw = 0;
  207. static int buflen = 0;
  208. //static int i = 0;
  209. static char serial_char;
  210. static int serial_count = 0;
  211. static boolean comment_mode = false;
  212. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  213. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  214. //static float tt = 0;
  215. //static float bt = 0;
  216. //Inactivity shutdown variables
  217. static unsigned long previous_millis_cmd = 0;
  218. static unsigned long max_inactive_time = 0;
  219. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  220. unsigned long starttime=0;
  221. unsigned long stoptime=0;
  222. static uint8_t tmp_extruder;
  223. bool Stopped=false;
  224. #if NUM_SERVOS > 0
  225. Servo servos[NUM_SERVOS];
  226. #endif
  227. bool CooldownNoWait = true;
  228. bool target_direction;
  229. //===========================================================================
  230. //=============================ROUTINES=============================
  231. //===========================================================================
  232. void get_arc_coordinates();
  233. bool setTargetedHotend(int code);
  234. void serial_echopair_P(const char *s_P, float v)
  235. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  236. void serial_echopair_P(const char *s_P, double v)
  237. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  238. void serial_echopair_P(const char *s_P, unsigned long v)
  239. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  240. extern "C"{
  241. extern unsigned int __bss_end;
  242. extern unsigned int __heap_start;
  243. extern void *__brkval;
  244. int freeMemory() {
  245. int free_memory;
  246. if((int)__brkval == 0)
  247. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  248. else
  249. free_memory = ((int)&free_memory) - ((int)__brkval);
  250. return free_memory;
  251. }
  252. }
  253. //adds an command to the main command buffer
  254. //thats really done in a non-safe way.
  255. //needs overworking someday
  256. void enquecommand(const char *cmd)
  257. {
  258. if(buflen < BUFSIZE)
  259. {
  260. //this is dangerous if a mixing of serial and this happsens
  261. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  262. SERIAL_ECHO_START;
  263. SERIAL_ECHOPGM("enqueing \"");
  264. SERIAL_ECHO(cmdbuffer[bufindw]);
  265. SERIAL_ECHOLNPGM("\"");
  266. bufindw= (bufindw + 1)%BUFSIZE;
  267. buflen += 1;
  268. }
  269. }
  270. void enquecommand_P(const char *cmd)
  271. {
  272. if(buflen < BUFSIZE)
  273. {
  274. //this is dangerous if a mixing of serial and this happsens
  275. strcpy_P(&(cmdbuffer[bufindw][0]),cmd);
  276. SERIAL_ECHO_START;
  277. SERIAL_ECHOPGM("enqueing \"");
  278. SERIAL_ECHO(cmdbuffer[bufindw]);
  279. SERIAL_ECHOLNPGM("\"");
  280. bufindw= (bufindw + 1)%BUFSIZE;
  281. buflen += 1;
  282. }
  283. }
  284. void setup_killpin()
  285. {
  286. #if defined(KILL_PIN) && KILL_PIN > -1
  287. pinMode(KILL_PIN,INPUT);
  288. WRITE(KILL_PIN,HIGH);
  289. #endif
  290. }
  291. void setup_photpin()
  292. {
  293. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  294. SET_OUTPUT(PHOTOGRAPH_PIN);
  295. WRITE(PHOTOGRAPH_PIN, LOW);
  296. #endif
  297. }
  298. void setup_powerhold()
  299. {
  300. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  301. SET_OUTPUT(SUICIDE_PIN);
  302. WRITE(SUICIDE_PIN, HIGH);
  303. #endif
  304. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  305. SET_OUTPUT(PS_ON_PIN);
  306. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  307. #endif
  308. }
  309. void suicide()
  310. {
  311. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  312. SET_OUTPUT(SUICIDE_PIN);
  313. WRITE(SUICIDE_PIN, LOW);
  314. #endif
  315. }
  316. void servo_init()
  317. {
  318. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  319. servos[0].attach(SERVO0_PIN);
  320. #endif
  321. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  322. servos[1].attach(SERVO1_PIN);
  323. #endif
  324. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  325. servos[2].attach(SERVO2_PIN);
  326. #endif
  327. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  328. servos[3].attach(SERVO3_PIN);
  329. #endif
  330. #if (NUM_SERVOS >= 5)
  331. #error "TODO: enter initalisation code for more servos"
  332. #endif
  333. // Set position of Servo Endstops that are defined
  334. #ifdef SERVO_ENDSTOPS
  335. for(int8_t i = 0; i < 3; i++)
  336. {
  337. if(servo_endstops[i] > -1) {
  338. servos[servo_endstops[i]].write(servo_endstop_angles[i * 2 + 1]);
  339. }
  340. }
  341. #endif
  342. }
  343. void setup()
  344. {
  345. setup_killpin();
  346. setup_powerhold();
  347. MYSERIAL.begin(BAUDRATE);
  348. SERIAL_PROTOCOLLNPGM("start");
  349. SERIAL_ECHO_START;
  350. // Check startup - does nothing if bootloader sets MCUSR to 0
  351. byte mcu = MCUSR;
  352. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  353. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  354. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  355. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  356. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  357. MCUSR=0;
  358. SERIAL_ECHOPGM(MSG_MARLIN);
  359. SERIAL_ECHOLNPGM(VERSION_STRING);
  360. #ifdef STRING_VERSION_CONFIG_H
  361. #ifdef STRING_CONFIG_H_AUTHOR
  362. SERIAL_ECHO_START;
  363. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  364. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  365. SERIAL_ECHOPGM(MSG_AUTHOR);
  366. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  367. SERIAL_ECHOPGM("Compiled: ");
  368. SERIAL_ECHOLNPGM(__DATE__);
  369. #endif
  370. #endif
  371. SERIAL_ECHO_START;
  372. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  373. SERIAL_ECHO(freeMemory());
  374. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  375. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  376. for(int8_t i = 0; i < BUFSIZE; i++)
  377. {
  378. fromsd[i] = false;
  379. }
  380. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  381. Config_RetrieveSettings();
  382. tp_init(); // Initialize temperature loop
  383. plan_init(); // Initialize planner;
  384. watchdog_init();
  385. st_init(); // Initialize stepper, this enables interrupts!
  386. setup_photpin();
  387. servo_init();
  388. lcd_init();
  389. _delay_ms(1000); // wait 1sec to display the splash screen
  390. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  391. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  392. #endif
  393. }
  394. void loop()
  395. {
  396. if(buflen < (BUFSIZE-1))
  397. get_command();
  398. #ifdef SDSUPPORT
  399. card.checkautostart(false);
  400. #endif
  401. if(buflen)
  402. {
  403. #ifdef SDSUPPORT
  404. if(card.saving)
  405. {
  406. if(strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL)
  407. {
  408. card.write_command(cmdbuffer[bufindr]);
  409. if(card.logging)
  410. {
  411. process_commands();
  412. }
  413. else
  414. {
  415. SERIAL_PROTOCOLLNPGM(MSG_OK);
  416. }
  417. }
  418. else
  419. {
  420. card.closefile();
  421. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  422. }
  423. }
  424. else
  425. {
  426. process_commands();
  427. }
  428. #else
  429. process_commands();
  430. #endif //SDSUPPORT
  431. buflen = (buflen-1);
  432. bufindr = (bufindr + 1)%BUFSIZE;
  433. }
  434. //check heater every n milliseconds
  435. manage_heater();
  436. manage_inactivity();
  437. checkHitEndstops();
  438. lcd_update();
  439. }
  440. void get_command()
  441. {
  442. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  443. serial_char = MYSERIAL.read();
  444. if(serial_char == '\n' ||
  445. serial_char == '\r' ||
  446. (serial_char == ':' && comment_mode == false) ||
  447. serial_count >= (MAX_CMD_SIZE - 1) )
  448. {
  449. if(!serial_count) { //if empty line
  450. comment_mode = false; //for new command
  451. return;
  452. }
  453. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  454. if(!comment_mode){
  455. comment_mode = false; //for new command
  456. fromsd[bufindw] = false;
  457. if(strchr(cmdbuffer[bufindw], 'N') != NULL)
  458. {
  459. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  460. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  461. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
  462. SERIAL_ERROR_START;
  463. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  464. SERIAL_ERRORLN(gcode_LastN);
  465. //Serial.println(gcode_N);
  466. FlushSerialRequestResend();
  467. serial_count = 0;
  468. return;
  469. }
  470. if(strchr(cmdbuffer[bufindw], '*') != NULL)
  471. {
  472. byte checksum = 0;
  473. byte count = 0;
  474. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  475. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  476. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  477. SERIAL_ERROR_START;
  478. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  479. SERIAL_ERRORLN(gcode_LastN);
  480. FlushSerialRequestResend();
  481. serial_count = 0;
  482. return;
  483. }
  484. //if no errors, continue parsing
  485. }
  486. else
  487. {
  488. SERIAL_ERROR_START;
  489. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  490. SERIAL_ERRORLN(gcode_LastN);
  491. FlushSerialRequestResend();
  492. serial_count = 0;
  493. return;
  494. }
  495. gcode_LastN = gcode_N;
  496. //if no errors, continue parsing
  497. }
  498. else // if we don't receive 'N' but still see '*'
  499. {
  500. if((strchr(cmdbuffer[bufindw], '*') != NULL))
  501. {
  502. SERIAL_ERROR_START;
  503. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  504. SERIAL_ERRORLN(gcode_LastN);
  505. serial_count = 0;
  506. return;
  507. }
  508. }
  509. if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
  510. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  511. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  512. case 0:
  513. case 1:
  514. case 2:
  515. case 3:
  516. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  517. #ifdef SDSUPPORT
  518. if(card.saving)
  519. break;
  520. #endif //SDSUPPORT
  521. SERIAL_PROTOCOLLNPGM(MSG_OK);
  522. }
  523. else {
  524. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  525. LCD_MESSAGEPGM(MSG_STOPPED);
  526. }
  527. break;
  528. default:
  529. break;
  530. }
  531. }
  532. bufindw = (bufindw + 1)%BUFSIZE;
  533. buflen += 1;
  534. }
  535. serial_count = 0; //clear buffer
  536. }
  537. else
  538. {
  539. if(serial_char == ';') comment_mode = true;
  540. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  541. }
  542. }
  543. #ifdef SDSUPPORT
  544. if(!card.sdprinting || serial_count!=0){
  545. return;
  546. }
  547. while( !card.eof() && buflen < BUFSIZE) {
  548. int16_t n=card.get();
  549. serial_char = (char)n;
  550. if(serial_char == '\n' ||
  551. serial_char == '\r' ||
  552. (serial_char == ':' && comment_mode == false) ||
  553. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  554. {
  555. if(card.eof()){
  556. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  557. stoptime=millis();
  558. char time[30];
  559. unsigned long t=(stoptime-starttime)/1000;
  560. int hours, minutes;
  561. minutes=(t/60)%60;
  562. hours=t/60/60;
  563. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  564. SERIAL_ECHO_START;
  565. SERIAL_ECHOLN(time);
  566. lcd_setstatus(time);
  567. card.printingHasFinished();
  568. card.checkautostart(true);
  569. }
  570. if(!serial_count)
  571. {
  572. comment_mode = false; //for new command
  573. return; //if empty line
  574. }
  575. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  576. // if(!comment_mode){
  577. fromsd[bufindw] = true;
  578. buflen += 1;
  579. bufindw = (bufindw + 1)%BUFSIZE;
  580. // }
  581. comment_mode = false; //for new command
  582. serial_count = 0; //clear buffer
  583. }
  584. else
  585. {
  586. if(serial_char == ';') comment_mode = true;
  587. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  588. }
  589. }
  590. #endif //SDSUPPORT
  591. }
  592. float code_value()
  593. {
  594. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  595. }
  596. long code_value_long()
  597. {
  598. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  599. }
  600. bool code_seen(char code)
  601. {
  602. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  603. return (strchr_pointer != NULL); //Return True if a character was found
  604. }
  605. #define DEFINE_PGM_READ_ANY(type, reader) \
  606. static inline type pgm_read_any(const type *p) \
  607. { return pgm_read_##reader##_near(p); }
  608. DEFINE_PGM_READ_ANY(float, float);
  609. DEFINE_PGM_READ_ANY(signed char, byte);
  610. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  611. static const PROGMEM type array##_P[3] = \
  612. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  613. static inline type array(int axis) \
  614. { return pgm_read_any(&array##_P[axis]); }
  615. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  616. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  617. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  618. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  619. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  620. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  621. #ifdef DUAL_X_CARRIAGE
  622. #if EXTRUDERS == 1 || defined(COREXY) \
  623. || !defined(X2_ENABLE_PIN) || !defined(X2_STEP_PIN) || !defined(X2_DIR_PIN) \
  624. || !defined(X2_HOME_POS) || !defined(X2_MIN_POS) || !defined(X2_MAX_POS) \
  625. || !defined(X_MAX_PIN) || X_MAX_PIN < 0
  626. #error "Missing or invalid definitions for DUAL_X_CARRIAGE mode."
  627. #endif
  628. #if X_HOME_DIR != -1 || X2_HOME_DIR != 1
  629. #error "Please use canonical x-carriage assignment" // the x-carriages are defined by their homing directions
  630. #endif
  631. #define DXC_FULL_CONTROL_MODE 0
  632. #define DXC_AUTO_PARK_MODE 1
  633. #define DXC_DUPLICATION_MODE 2
  634. static int dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  635. static float x_home_pos(int extruder) {
  636. if (extruder == 0)
  637. return base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  638. else
  639. // In dual carriage mode the extruder offset provides an override of the
  640. // second X-carriage offset when homed - otherwise X2_HOME_POS is used.
  641. // This allow soft recalibration of the second extruder offset position without firmware reflash
  642. // (through the M218 command).
  643. return (extruder_offset[X_AXIS][1] > 0) ? extruder_offset[X_AXIS][1] : X2_HOME_POS;
  644. }
  645. static int x_home_dir(int extruder) {
  646. return (extruder == 0) ? X_HOME_DIR : X2_HOME_DIR;
  647. }
  648. static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1
  649. static bool active_extruder_parked = false; // used in mode 1 & 2
  650. static float raised_parked_position[NUM_AXIS]; // used in mode 1
  651. static unsigned long delayed_move_time = 0; // used in mode 1
  652. static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  653. static float duplicate_extruder_temp_offset = 0; // used in mode 2
  654. bool extruder_duplication_enabled = false; // used in mode 2
  655. #endif //DUAL_X_CARRIAGE
  656. static void axis_is_at_home(int axis) {
  657. #ifdef DUAL_X_CARRIAGE
  658. if (axis == X_AXIS) {
  659. if (active_extruder != 0) {
  660. current_position[X_AXIS] = x_home_pos(active_extruder);
  661. min_pos[X_AXIS] = X2_MIN_POS;
  662. max_pos[X_AXIS] = max(extruder_offset[X_AXIS][1], X2_MAX_POS);
  663. return;
  664. }
  665. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
  666. current_position[X_AXIS] = base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  667. min_pos[X_AXIS] = base_min_pos(X_AXIS) + add_homeing[X_AXIS];
  668. max_pos[X_AXIS] = min(base_max_pos(X_AXIS) + add_homeing[X_AXIS],
  669. max(extruder_offset[X_AXIS][1], X2_MAX_POS) - duplicate_extruder_x_offset);
  670. return;
  671. }
  672. }
  673. #endif
  674. current_position[axis] = base_home_pos(axis) + add_homeing[axis];
  675. min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
  676. max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
  677. }
  678. static void homeaxis(int axis) {
  679. #define HOMEAXIS_DO(LETTER) \
  680. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  681. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  682. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  683. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  684. 0) {
  685. int axis_home_dir = home_dir(axis);
  686. #ifdef DUAL_X_CARRIAGE
  687. if (axis == X_AXIS)
  688. axis_home_dir = x_home_dir(active_extruder);
  689. #endif
  690. // Engage Servo endstop if enabled
  691. #ifdef SERVO_ENDSTOPS
  692. if (SERVO_ENDSTOPS[axis] > -1) {
  693. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2]);
  694. }
  695. #endif
  696. current_position[axis] = 0;
  697. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  698. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  699. feedrate = homing_feedrate[axis];
  700. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  701. st_synchronize();
  702. current_position[axis] = 0;
  703. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  704. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  705. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  706. st_synchronize();
  707. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  708. #ifdef DELTA
  709. feedrate = homing_feedrate[axis]/10;
  710. #else
  711. feedrate = homing_feedrate[axis]/2 ;
  712. #endif
  713. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  714. st_synchronize();
  715. #ifdef DELTA
  716. // retrace by the amount specified in endstop_adj
  717. if (endstop_adj[axis] * axis_home_dir < 0) {
  718. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  719. destination[axis] = endstop_adj[axis];
  720. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  721. st_synchronize();
  722. }
  723. #endif
  724. axis_is_at_home(axis);
  725. destination[axis] = current_position[axis];
  726. feedrate = 0.0;
  727. endstops_hit_on_purpose();
  728. // Retract Servo endstop if enabled
  729. #ifdef SERVO_ENDSTOPS
  730. if (SERVO_ENDSTOPS[axis] > -1) {
  731. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2 + 1]);
  732. }
  733. #endif
  734. }
  735. }
  736. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  737. void process_commands()
  738. {
  739. unsigned long codenum; //throw away variable
  740. char *starpos = NULL;
  741. if(code_seen('G'))
  742. {
  743. switch((int)code_value())
  744. {
  745. case 0: // G0 -> G1
  746. case 1: // G1
  747. if(Stopped == false) {
  748. get_coordinates(); // For X Y Z E F
  749. prepare_move();
  750. //ClearToSend();
  751. return;
  752. }
  753. //break;
  754. case 2: // G2 - CW ARC
  755. if(Stopped == false) {
  756. get_arc_coordinates();
  757. prepare_arc_move(true);
  758. return;
  759. }
  760. case 3: // G3 - CCW ARC
  761. if(Stopped == false) {
  762. get_arc_coordinates();
  763. prepare_arc_move(false);
  764. return;
  765. }
  766. case 4: // G4 dwell
  767. LCD_MESSAGEPGM(MSG_DWELL);
  768. codenum = 0;
  769. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  770. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  771. st_synchronize();
  772. codenum += millis(); // keep track of when we started waiting
  773. previous_millis_cmd = millis();
  774. while(millis() < codenum ){
  775. manage_heater();
  776. manage_inactivity();
  777. lcd_update();
  778. }
  779. break;
  780. #ifdef FWRETRACT
  781. case 10: // G10 retract
  782. if(!retracted)
  783. {
  784. destination[X_AXIS]=current_position[X_AXIS];
  785. destination[Y_AXIS]=current_position[Y_AXIS];
  786. destination[Z_AXIS]=current_position[Z_AXIS];
  787. current_position[Z_AXIS]+=-retract_zlift;
  788. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  789. feedrate=retract_feedrate;
  790. retracted=true;
  791. prepare_move();
  792. }
  793. break;
  794. case 11: // G10 retract_recover
  795. if(!retracted)
  796. {
  797. destination[X_AXIS]=current_position[X_AXIS];
  798. destination[Y_AXIS]=current_position[Y_AXIS];
  799. destination[Z_AXIS]=current_position[Z_AXIS];
  800. current_position[Z_AXIS]+=retract_zlift;
  801. current_position[E_AXIS]+=-retract_recover_length;
  802. feedrate=retract_recover_feedrate;
  803. retracted=false;
  804. prepare_move();
  805. }
  806. break;
  807. #endif //FWRETRACT
  808. case 28: //G28 Home all Axis one at a time
  809. saved_feedrate = feedrate;
  810. saved_feedmultiply = feedmultiply;
  811. feedmultiply = 100;
  812. previous_millis_cmd = millis();
  813. enable_endstops(true);
  814. for(int8_t i=0; i < NUM_AXIS; i++) {
  815. destination[i] = current_position[i];
  816. }
  817. feedrate = 0.0;
  818. #ifdef DELTA
  819. // A delta can only safely home all axis at the same time
  820. // all axis have to home at the same time
  821. // Move all carriages up together until the first endstop is hit.
  822. current_position[X_AXIS] = 0;
  823. current_position[Y_AXIS] = 0;
  824. current_position[Z_AXIS] = 0;
  825. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  826. destination[X_AXIS] = 3 * Z_MAX_LENGTH;
  827. destination[Y_AXIS] = 3 * Z_MAX_LENGTH;
  828. destination[Z_AXIS] = 3 * Z_MAX_LENGTH;
  829. feedrate = 1.732 * homing_feedrate[X_AXIS];
  830. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  831. st_synchronize();
  832. endstops_hit_on_purpose();
  833. current_position[X_AXIS] = destination[X_AXIS];
  834. current_position[Y_AXIS] = destination[Y_AXIS];
  835. current_position[Z_AXIS] = destination[Z_AXIS];
  836. // take care of back off and rehome now we are all at the top
  837. HOMEAXIS(X);
  838. HOMEAXIS(Y);
  839. HOMEAXIS(Z);
  840. calculate_delta(current_position);
  841. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  842. #else // NOT DELTA
  843. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  844. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  845. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  846. HOMEAXIS(Z);
  847. }
  848. #endif
  849. #ifdef QUICK_HOME
  850. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  851. {
  852. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  853. #ifndef DUAL_X_CARRIAGE
  854. int x_axis_home_dir = home_dir(X_AXIS);
  855. #else
  856. int x_axis_home_dir = x_home_dir(active_extruder);
  857. extruder_duplication_enabled = false;
  858. #endif
  859. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  860. destination[X_AXIS] = 1.5 * max_length(X_AXIS) * x_axis_home_dir;destination[Y_AXIS] = 1.5 * max_length(Y_AXIS) * home_dir(Y_AXIS);
  861. feedrate = homing_feedrate[X_AXIS];
  862. if(homing_feedrate[Y_AXIS]<feedrate)
  863. feedrate =homing_feedrate[Y_AXIS];
  864. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  865. st_synchronize();
  866. axis_is_at_home(X_AXIS);
  867. axis_is_at_home(Y_AXIS);
  868. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  869. destination[X_AXIS] = current_position[X_AXIS];
  870. destination[Y_AXIS] = current_position[Y_AXIS];
  871. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  872. feedrate = 0.0;
  873. st_synchronize();
  874. endstops_hit_on_purpose();
  875. current_position[X_AXIS] = destination[X_AXIS];
  876. current_position[Y_AXIS] = destination[Y_AXIS];
  877. current_position[Z_AXIS] = destination[Z_AXIS];
  878. }
  879. #endif
  880. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  881. {
  882. #ifdef DUAL_X_CARRIAGE
  883. int tmp_extruder = active_extruder;
  884. extruder_duplication_enabled = false;
  885. active_extruder = !active_extruder;
  886. HOMEAXIS(X);
  887. inactive_extruder_x_pos = current_position[X_AXIS];
  888. active_extruder = tmp_extruder;
  889. HOMEAXIS(X);
  890. // reset state used by the different modes
  891. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  892. delayed_move_time = 0;
  893. active_extruder_parked = true;
  894. #else
  895. HOMEAXIS(X);
  896. #endif
  897. }
  898. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  899. HOMEAXIS(Y);
  900. }
  901. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  902. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  903. HOMEAXIS(Z);
  904. }
  905. #endif
  906. if(code_seen(axis_codes[X_AXIS]))
  907. {
  908. if(code_value_long() != 0) {
  909. current_position[X_AXIS]=code_value()+add_homeing[0];
  910. }
  911. }
  912. if(code_seen(axis_codes[Y_AXIS])) {
  913. if(code_value_long() != 0) {
  914. current_position[Y_AXIS]=code_value()+add_homeing[1];
  915. }
  916. }
  917. if(code_seen(axis_codes[Z_AXIS])) {
  918. if(code_value_long() != 0) {
  919. current_position[Z_AXIS]=code_value()+add_homeing[2];
  920. }
  921. }
  922. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  923. #endif // else DELTA
  924. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  925. enable_endstops(false);
  926. #endif
  927. feedrate = saved_feedrate;
  928. feedmultiply = saved_feedmultiply;
  929. previous_millis_cmd = millis();
  930. endstops_hit_on_purpose();
  931. break;
  932. case 90: // G90
  933. relative_mode = false;
  934. break;
  935. case 91: // G91
  936. relative_mode = true;
  937. break;
  938. case 92: // G92
  939. if(!code_seen(axis_codes[E_AXIS]))
  940. st_synchronize();
  941. for(int8_t i=0; i < NUM_AXIS; i++) {
  942. if(code_seen(axis_codes[i])) {
  943. if(i == E_AXIS) {
  944. current_position[i] = code_value();
  945. plan_set_e_position(current_position[E_AXIS]);
  946. }
  947. else {
  948. current_position[i] = code_value()+add_homeing[i];
  949. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  950. }
  951. }
  952. }
  953. break;
  954. }
  955. }
  956. else if(code_seen('M'))
  957. {
  958. switch( (int)code_value() )
  959. {
  960. #ifdef ULTIPANEL
  961. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  962. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  963. {
  964. LCD_MESSAGEPGM(MSG_USERWAIT);
  965. codenum = 0;
  966. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  967. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  968. st_synchronize();
  969. previous_millis_cmd = millis();
  970. if (codenum > 0){
  971. codenum += millis(); // keep track of when we started waiting
  972. while(millis() < codenum && !lcd_clicked()){
  973. manage_heater();
  974. manage_inactivity();
  975. lcd_update();
  976. }
  977. }else{
  978. while(!lcd_clicked()){
  979. manage_heater();
  980. manage_inactivity();
  981. lcd_update();
  982. }
  983. }
  984. LCD_MESSAGEPGM(MSG_RESUMING);
  985. }
  986. break;
  987. #endif
  988. case 17:
  989. LCD_MESSAGEPGM(MSG_NO_MOVE);
  990. enable_x();
  991. enable_y();
  992. enable_z();
  993. enable_e0();
  994. enable_e1();
  995. enable_e2();
  996. break;
  997. #ifdef SDSUPPORT
  998. case 20: // M20 - list SD card
  999. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  1000. card.ls();
  1001. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  1002. break;
  1003. case 21: // M21 - init SD card
  1004. card.initsd();
  1005. break;
  1006. case 22: //M22 - release SD card
  1007. card.release();
  1008. break;
  1009. case 23: //M23 - Select file
  1010. starpos = (strchr(strchr_pointer + 4,'*'));
  1011. if(starpos!=NULL)
  1012. *(starpos-1)='\0';
  1013. card.openFile(strchr_pointer + 4,true);
  1014. break;
  1015. case 24: //M24 - Start SD print
  1016. card.startFileprint();
  1017. starttime=millis();
  1018. break;
  1019. case 25: //M25 - Pause SD print
  1020. card.pauseSDPrint();
  1021. break;
  1022. case 26: //M26 - Set SD index
  1023. if(card.cardOK && code_seen('S')) {
  1024. card.setIndex(code_value_long());
  1025. }
  1026. break;
  1027. case 27: //M27 - Get SD status
  1028. card.getStatus();
  1029. break;
  1030. case 28: //M28 - Start SD write
  1031. starpos = (strchr(strchr_pointer + 4,'*'));
  1032. if(starpos != NULL){
  1033. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1034. strchr_pointer = strchr(npos,' ') + 1;
  1035. *(starpos-1) = '\0';
  1036. }
  1037. card.openFile(strchr_pointer+4,false);
  1038. break;
  1039. case 29: //M29 - Stop SD write
  1040. //processed in write to file routine above
  1041. //card,saving = false;
  1042. break;
  1043. case 30: //M30 <filename> Delete File
  1044. if (card.cardOK){
  1045. card.closefile();
  1046. starpos = (strchr(strchr_pointer + 4,'*'));
  1047. if(starpos != NULL){
  1048. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1049. strchr_pointer = strchr(npos,' ') + 1;
  1050. *(starpos-1) = '\0';
  1051. }
  1052. card.removeFile(strchr_pointer + 4);
  1053. }
  1054. break;
  1055. case 32: //M32 - Select file and start SD print
  1056. if(card.sdprinting) {
  1057. st_synchronize();
  1058. card.closefile();
  1059. card.sdprinting = false;
  1060. }
  1061. starpos = (strchr(strchr_pointer + 4,'*'));
  1062. if(starpos!=NULL)
  1063. *(starpos-1)='\0';
  1064. card.openFile(strchr_pointer + 4,true);
  1065. card.startFileprint();
  1066. starttime=millis();
  1067. break;
  1068. case 928: //M928 - Start SD write
  1069. starpos = (strchr(strchr_pointer + 5,'*'));
  1070. if(starpos != NULL){
  1071. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1072. strchr_pointer = strchr(npos,' ') + 1;
  1073. *(starpos-1) = '\0';
  1074. }
  1075. card.openLogFile(strchr_pointer+5);
  1076. break;
  1077. #endif //SDSUPPORT
  1078. case 31: //M31 take time since the start of the SD print or an M109 command
  1079. {
  1080. stoptime=millis();
  1081. char time[30];
  1082. unsigned long t=(stoptime-starttime)/1000;
  1083. int sec,min;
  1084. min=t/60;
  1085. sec=t%60;
  1086. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  1087. SERIAL_ECHO_START;
  1088. SERIAL_ECHOLN(time);
  1089. lcd_setstatus(time);
  1090. autotempShutdown();
  1091. }
  1092. break;
  1093. case 42: //M42 -Change pin status via gcode
  1094. if (code_seen('S'))
  1095. {
  1096. int pin_status = code_value();
  1097. int pin_number = LED_PIN;
  1098. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  1099. pin_number = code_value();
  1100. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  1101. {
  1102. if (sensitive_pins[i] == pin_number)
  1103. {
  1104. pin_number = -1;
  1105. break;
  1106. }
  1107. }
  1108. #if defined(FAN_PIN) && FAN_PIN > -1
  1109. if (pin_number == FAN_PIN)
  1110. fanSpeed = pin_status;
  1111. #endif
  1112. if (pin_number > -1)
  1113. {
  1114. pinMode(pin_number, OUTPUT);
  1115. digitalWrite(pin_number, pin_status);
  1116. analogWrite(pin_number, pin_status);
  1117. }
  1118. }
  1119. break;
  1120. case 104: // M104
  1121. if(setTargetedHotend(104)){
  1122. break;
  1123. }
  1124. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  1125. #ifdef DUAL_X_CARRIAGE
  1126. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1127. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1128. #endif
  1129. setWatch();
  1130. break;
  1131. case 140: // M140 set bed temp
  1132. if (code_seen('S')) setTargetBed(code_value());
  1133. break;
  1134. case 105 : // M105
  1135. if(setTargetedHotend(105)){
  1136. break;
  1137. }
  1138. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1139. SERIAL_PROTOCOLPGM("ok T:");
  1140. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  1141. SERIAL_PROTOCOLPGM(" /");
  1142. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  1143. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1144. SERIAL_PROTOCOLPGM(" B:");
  1145. SERIAL_PROTOCOL_F(degBed(),1);
  1146. SERIAL_PROTOCOLPGM(" /");
  1147. SERIAL_PROTOCOL_F(degTargetBed(),1);
  1148. #endif //TEMP_BED_PIN
  1149. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  1150. SERIAL_PROTOCOLPGM(" T");
  1151. SERIAL_PROTOCOL(cur_extruder);
  1152. SERIAL_PROTOCOLPGM(":");
  1153. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  1154. SERIAL_PROTOCOLPGM(" /");
  1155. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  1156. }
  1157. #else
  1158. SERIAL_ERROR_START;
  1159. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  1160. #endif
  1161. SERIAL_PROTOCOLPGM(" @:");
  1162. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  1163. SERIAL_PROTOCOLPGM(" B@:");
  1164. SERIAL_PROTOCOL(getHeaterPower(-1));
  1165. SERIAL_PROTOCOLLN("");
  1166. return;
  1167. break;
  1168. case 109:
  1169. {// M109 - Wait for extruder heater to reach target.
  1170. if(setTargetedHotend(109)){
  1171. break;
  1172. }
  1173. LCD_MESSAGEPGM(MSG_HEATING);
  1174. #ifdef AUTOTEMP
  1175. autotemp_enabled=false;
  1176. #endif
  1177. if (code_seen('S')) {
  1178. setTargetHotend(code_value(), tmp_extruder);
  1179. #ifdef DUAL_X_CARRIAGE
  1180. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1181. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1182. #endif
  1183. CooldownNoWait = true;
  1184. } else if (code_seen('R')) {
  1185. setTargetHotend(code_value(), tmp_extruder);
  1186. #ifdef DUAL_X_CARRIAGE
  1187. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1188. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1189. #endif
  1190. CooldownNoWait = false;
  1191. }
  1192. #ifdef AUTOTEMP
  1193. if (code_seen('S')) autotemp_min=code_value();
  1194. if (code_seen('B')) autotemp_max=code_value();
  1195. if (code_seen('F'))
  1196. {
  1197. autotemp_factor=code_value();
  1198. autotemp_enabled=true;
  1199. }
  1200. #endif
  1201. setWatch();
  1202. codenum = millis();
  1203. /* See if we are heating up or cooling down */
  1204. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  1205. #ifdef TEMP_RESIDENCY_TIME
  1206. long residencyStart;
  1207. residencyStart = -1;
  1208. /* continue to loop until we have reached the target temp
  1209. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  1210. while((residencyStart == -1) ||
  1211. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  1212. #else
  1213. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  1214. #endif //TEMP_RESIDENCY_TIME
  1215. if( (millis() - codenum) > 1000UL )
  1216. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  1217. SERIAL_PROTOCOLPGM("T:");
  1218. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  1219. SERIAL_PROTOCOLPGM(" E:");
  1220. SERIAL_PROTOCOL((int)tmp_extruder);
  1221. #ifdef TEMP_RESIDENCY_TIME
  1222. SERIAL_PROTOCOLPGM(" W:");
  1223. if(residencyStart > -1)
  1224. {
  1225. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  1226. SERIAL_PROTOCOLLN( codenum );
  1227. }
  1228. else
  1229. {
  1230. SERIAL_PROTOCOLLN( "?" );
  1231. }
  1232. #else
  1233. SERIAL_PROTOCOLLN("");
  1234. #endif
  1235. codenum = millis();
  1236. }
  1237. manage_heater();
  1238. manage_inactivity();
  1239. lcd_update();
  1240. #ifdef TEMP_RESIDENCY_TIME
  1241. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  1242. or when current temp falls outside the hysteresis after target temp was reached */
  1243. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  1244. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  1245. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  1246. {
  1247. residencyStart = millis();
  1248. }
  1249. #endif //TEMP_RESIDENCY_TIME
  1250. }
  1251. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  1252. starttime=millis();
  1253. previous_millis_cmd = millis();
  1254. }
  1255. break;
  1256. case 190: // M190 - Wait for bed heater to reach target.
  1257. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1258. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1259. if (code_seen('S')) {
  1260. setTargetBed(code_value());
  1261. CooldownNoWait = true;
  1262. } else if (code_seen('R')) {
  1263. setTargetBed(code_value());
  1264. CooldownNoWait = false;
  1265. }
  1266. codenum = millis();
  1267. target_direction = isHeatingBed(); // true if heating, false if cooling
  1268. while ( target_direction ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  1269. {
  1270. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  1271. {
  1272. float tt=degHotend(active_extruder);
  1273. SERIAL_PROTOCOLPGM("T:");
  1274. SERIAL_PROTOCOL(tt);
  1275. SERIAL_PROTOCOLPGM(" E:");
  1276. SERIAL_PROTOCOL((int)active_extruder);
  1277. SERIAL_PROTOCOLPGM(" B:");
  1278. SERIAL_PROTOCOL_F(degBed(),1);
  1279. SERIAL_PROTOCOLLN("");
  1280. codenum = millis();
  1281. }
  1282. manage_heater();
  1283. manage_inactivity();
  1284. lcd_update();
  1285. }
  1286. LCD_MESSAGEPGM(MSG_BED_DONE);
  1287. previous_millis_cmd = millis();
  1288. #endif
  1289. break;
  1290. #if defined(FAN_PIN) && FAN_PIN > -1
  1291. case 106: //M106 Fan On
  1292. if (code_seen('S')){
  1293. fanSpeed=constrain(code_value(),0,255);
  1294. }
  1295. else {
  1296. fanSpeed=255;
  1297. }
  1298. break;
  1299. case 107: //M107 Fan Off
  1300. fanSpeed = 0;
  1301. break;
  1302. #endif //FAN_PIN
  1303. #ifdef BARICUDA
  1304. // PWM for HEATER_1_PIN
  1305. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1306. case 126: //M126 valve open
  1307. if (code_seen('S')){
  1308. ValvePressure=constrain(code_value(),0,255);
  1309. }
  1310. else {
  1311. ValvePressure=255;
  1312. }
  1313. break;
  1314. case 127: //M127 valve closed
  1315. ValvePressure = 0;
  1316. break;
  1317. #endif //HEATER_1_PIN
  1318. // PWM for HEATER_2_PIN
  1319. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1320. case 128: //M128 valve open
  1321. if (code_seen('S')){
  1322. EtoPPressure=constrain(code_value(),0,255);
  1323. }
  1324. else {
  1325. EtoPPressure=255;
  1326. }
  1327. break;
  1328. case 129: //M129 valve closed
  1329. EtoPPressure = 0;
  1330. break;
  1331. #endif //HEATER_2_PIN
  1332. #endif
  1333. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  1334. case 80: // M80 - Turn on Power Supply
  1335. SET_OUTPUT(PS_ON_PIN); //GND
  1336. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  1337. #ifdef ULTIPANEL
  1338. powersupply = true;
  1339. LCD_MESSAGEPGM(WELCOME_MSG);
  1340. lcd_update();
  1341. #endif
  1342. break;
  1343. #endif
  1344. case 81: // M81 - Turn off Power Supply
  1345. disable_heater();
  1346. st_synchronize();
  1347. disable_e0();
  1348. disable_e1();
  1349. disable_e2();
  1350. finishAndDisableSteppers();
  1351. fanSpeed = 0;
  1352. delay(1000); // Wait a little before to switch off
  1353. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  1354. st_synchronize();
  1355. suicide();
  1356. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  1357. SET_OUTPUT(PS_ON_PIN);
  1358. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  1359. #endif
  1360. #ifdef ULTIPANEL
  1361. powersupply = false;
  1362. LCD_MESSAGEPGM(MACHINE_NAME" "MSG_OFF".");
  1363. lcd_update();
  1364. #endif
  1365. break;
  1366. case 82:
  1367. axis_relative_modes[3] = false;
  1368. break;
  1369. case 83:
  1370. axis_relative_modes[3] = true;
  1371. break;
  1372. case 18: //compatibility
  1373. case 84: // M84
  1374. if(code_seen('S')){
  1375. stepper_inactive_time = code_value() * 1000;
  1376. }
  1377. else
  1378. {
  1379. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1380. if(all_axis)
  1381. {
  1382. st_synchronize();
  1383. disable_e0();
  1384. disable_e1();
  1385. disable_e2();
  1386. finishAndDisableSteppers();
  1387. }
  1388. else
  1389. {
  1390. st_synchronize();
  1391. if(code_seen('X')) disable_x();
  1392. if(code_seen('Y')) disable_y();
  1393. if(code_seen('Z')) disable_z();
  1394. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1395. if(code_seen('E')) {
  1396. disable_e0();
  1397. disable_e1();
  1398. disable_e2();
  1399. }
  1400. #endif
  1401. }
  1402. }
  1403. break;
  1404. case 85: // M85
  1405. code_seen('S');
  1406. max_inactive_time = code_value() * 1000;
  1407. break;
  1408. case 92: // M92
  1409. for(int8_t i=0; i < NUM_AXIS; i++)
  1410. {
  1411. if(code_seen(axis_codes[i]))
  1412. {
  1413. if(i == 3) { // E
  1414. float value = code_value();
  1415. if(value < 20.0) {
  1416. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1417. max_e_jerk *= factor;
  1418. max_feedrate[i] *= factor;
  1419. axis_steps_per_sqr_second[i] *= factor;
  1420. }
  1421. axis_steps_per_unit[i] = value;
  1422. }
  1423. else {
  1424. axis_steps_per_unit[i] = code_value();
  1425. }
  1426. }
  1427. }
  1428. break;
  1429. case 115: // M115
  1430. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  1431. break;
  1432. case 117: // M117 display message
  1433. starpos = (strchr(strchr_pointer + 5,'*'));
  1434. if(starpos!=NULL)
  1435. *(starpos-1)='\0';
  1436. lcd_setstatus(strchr_pointer + 5);
  1437. break;
  1438. case 114: // M114
  1439. SERIAL_PROTOCOLPGM("X:");
  1440. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1441. SERIAL_PROTOCOLPGM("Y:");
  1442. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1443. SERIAL_PROTOCOLPGM("Z:");
  1444. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1445. SERIAL_PROTOCOLPGM("E:");
  1446. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1447. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1448. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1449. SERIAL_PROTOCOLPGM("Y:");
  1450. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1451. SERIAL_PROTOCOLPGM("Z:");
  1452. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1453. SERIAL_PROTOCOLLN("");
  1454. break;
  1455. case 120: // M120
  1456. enable_endstops(false) ;
  1457. break;
  1458. case 121: // M121
  1459. enable_endstops(true) ;
  1460. break;
  1461. case 119: // M119
  1462. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  1463. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  1464. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1465. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1466. #endif
  1467. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  1468. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1469. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1470. #endif
  1471. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  1472. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1473. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1474. #endif
  1475. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  1476. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1477. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1478. #endif
  1479. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  1480. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1481. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1482. #endif
  1483. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  1484. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1485. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1486. #endif
  1487. break;
  1488. //TODO: update for all axis, use for loop
  1489. case 201: // M201
  1490. for(int8_t i=0; i < NUM_AXIS; i++)
  1491. {
  1492. if(code_seen(axis_codes[i]))
  1493. {
  1494. max_acceleration_units_per_sq_second[i] = code_value();
  1495. }
  1496. }
  1497. // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
  1498. reset_acceleration_rates();
  1499. break;
  1500. #if 0 // Not used for Sprinter/grbl gen6
  1501. case 202: // M202
  1502. for(int8_t i=0; i < NUM_AXIS; i++) {
  1503. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1504. }
  1505. break;
  1506. #endif
  1507. case 203: // M203 max feedrate mm/sec
  1508. for(int8_t i=0; i < NUM_AXIS; i++) {
  1509. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1510. }
  1511. break;
  1512. case 204: // M204 acclereration S normal moves T filmanent only moves
  1513. {
  1514. if(code_seen('S')) acceleration = code_value() ;
  1515. if(code_seen('T')) retract_acceleration = code_value() ;
  1516. }
  1517. break;
  1518. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  1519. {
  1520. if(code_seen('S')) minimumfeedrate = code_value();
  1521. if(code_seen('T')) mintravelfeedrate = code_value();
  1522. if(code_seen('B')) minsegmenttime = code_value() ;
  1523. if(code_seen('X')) max_xy_jerk = code_value() ;
  1524. if(code_seen('Z')) max_z_jerk = code_value() ;
  1525. if(code_seen('E')) max_e_jerk = code_value() ;
  1526. }
  1527. break;
  1528. case 206: // M206 additional homeing offset
  1529. for(int8_t i=0; i < 3; i++)
  1530. {
  1531. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1532. }
  1533. break;
  1534. #ifdef DELTA
  1535. case 666: // M666 set delta endstop adjustemnt
  1536. for(int8_t i=0; i < 3; i++)
  1537. {
  1538. if(code_seen(axis_codes[i])) endstop_adj[i] = code_value();
  1539. }
  1540. break;
  1541. #endif
  1542. #ifdef FWRETRACT
  1543. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1544. {
  1545. if(code_seen('S'))
  1546. {
  1547. retract_length = code_value() ;
  1548. }
  1549. if(code_seen('F'))
  1550. {
  1551. retract_feedrate = code_value() ;
  1552. }
  1553. if(code_seen('Z'))
  1554. {
  1555. retract_zlift = code_value() ;
  1556. }
  1557. }break;
  1558. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1559. {
  1560. if(code_seen('S'))
  1561. {
  1562. retract_recover_length = code_value() ;
  1563. }
  1564. if(code_seen('F'))
  1565. {
  1566. retract_recover_feedrate = code_value() ;
  1567. }
  1568. }break;
  1569. case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  1570. {
  1571. if(code_seen('S'))
  1572. {
  1573. int t= code_value() ;
  1574. switch(t)
  1575. {
  1576. case 0: autoretract_enabled=false;retracted=false;break;
  1577. case 1: autoretract_enabled=true;retracted=false;break;
  1578. default:
  1579. SERIAL_ECHO_START;
  1580. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1581. SERIAL_ECHO(cmdbuffer[bufindr]);
  1582. SERIAL_ECHOLNPGM("\"");
  1583. }
  1584. }
  1585. }break;
  1586. #endif // FWRETRACT
  1587. #if EXTRUDERS > 1
  1588. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  1589. {
  1590. if(setTargetedHotend(218)){
  1591. break;
  1592. }
  1593. if(code_seen('X'))
  1594. {
  1595. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  1596. }
  1597. if(code_seen('Y'))
  1598. {
  1599. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  1600. }
  1601. #ifdef DUAL_X_CARRIAGE
  1602. if(code_seen('Z'))
  1603. {
  1604. extruder_offset[Z_AXIS][tmp_extruder] = code_value();
  1605. }
  1606. #endif
  1607. SERIAL_ECHO_START;
  1608. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  1609. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  1610. {
  1611. SERIAL_ECHO(" ");
  1612. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  1613. SERIAL_ECHO(",");
  1614. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  1615. #ifdef DUAL_X_CARRIAGE
  1616. SERIAL_ECHO(",");
  1617. SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
  1618. #endif
  1619. }
  1620. SERIAL_ECHOLN("");
  1621. }break;
  1622. #endif
  1623. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1624. {
  1625. if(code_seen('S'))
  1626. {
  1627. feedmultiply = code_value() ;
  1628. }
  1629. }
  1630. break;
  1631. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1632. {
  1633. if(code_seen('S'))
  1634. {
  1635. extrudemultiply = code_value() ;
  1636. }
  1637. }
  1638. break;
  1639. #if NUM_SERVOS > 0
  1640. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  1641. {
  1642. int servo_index = -1;
  1643. int servo_position = 0;
  1644. if (code_seen('P'))
  1645. servo_index = code_value();
  1646. if (code_seen('S')) {
  1647. servo_position = code_value();
  1648. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  1649. servos[servo_index].write(servo_position);
  1650. }
  1651. else {
  1652. SERIAL_ECHO_START;
  1653. SERIAL_ECHO("Servo ");
  1654. SERIAL_ECHO(servo_index);
  1655. SERIAL_ECHOLN(" out of range");
  1656. }
  1657. }
  1658. else if (servo_index >= 0) {
  1659. SERIAL_PROTOCOL(MSG_OK);
  1660. SERIAL_PROTOCOL(" Servo ");
  1661. SERIAL_PROTOCOL(servo_index);
  1662. SERIAL_PROTOCOL(": ");
  1663. SERIAL_PROTOCOL(servos[servo_index].read());
  1664. SERIAL_PROTOCOLLN("");
  1665. }
  1666. }
  1667. break;
  1668. #endif // NUM_SERVOS > 0
  1669. #if LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) )
  1670. case 300: // M300
  1671. {
  1672. int beepS = code_seen('S') ? code_value() : 110;
  1673. int beepP = code_seen('P') ? code_value() : 1000;
  1674. if (beepS > 0)
  1675. {
  1676. #if BEEPER > 0
  1677. tone(BEEPER, beepS);
  1678. delay(beepP);
  1679. noTone(BEEPER);
  1680. #elif defined(ULTRALCD)
  1681. lcd_buzz(beepS, beepP);
  1682. #endif
  1683. }
  1684. else
  1685. {
  1686. delay(beepP);
  1687. }
  1688. }
  1689. break;
  1690. #endif // M300
  1691. #ifdef PIDTEMP
  1692. case 301: // M301
  1693. {
  1694. if(code_seen('P')) Kp = code_value();
  1695. if(code_seen('I')) Ki = scalePID_i(code_value());
  1696. if(code_seen('D')) Kd = scalePID_d(code_value());
  1697. #ifdef PID_ADD_EXTRUSION_RATE
  1698. if(code_seen('C')) Kc = code_value();
  1699. #endif
  1700. updatePID();
  1701. SERIAL_PROTOCOL(MSG_OK);
  1702. SERIAL_PROTOCOL(" p:");
  1703. SERIAL_PROTOCOL(Kp);
  1704. SERIAL_PROTOCOL(" i:");
  1705. SERIAL_PROTOCOL(unscalePID_i(Ki));
  1706. SERIAL_PROTOCOL(" d:");
  1707. SERIAL_PROTOCOL(unscalePID_d(Kd));
  1708. #ifdef PID_ADD_EXTRUSION_RATE
  1709. SERIAL_PROTOCOL(" c:");
  1710. //Kc does not have scaling applied above, or in resetting defaults
  1711. SERIAL_PROTOCOL(Kc);
  1712. #endif
  1713. SERIAL_PROTOCOLLN("");
  1714. }
  1715. break;
  1716. #endif //PIDTEMP
  1717. #ifdef PIDTEMPBED
  1718. case 304: // M304
  1719. {
  1720. if(code_seen('P')) bedKp = code_value();
  1721. if(code_seen('I')) bedKi = scalePID_i(code_value());
  1722. if(code_seen('D')) bedKd = scalePID_d(code_value());
  1723. updatePID();
  1724. SERIAL_PROTOCOL(MSG_OK);
  1725. SERIAL_PROTOCOL(" p:");
  1726. SERIAL_PROTOCOL(bedKp);
  1727. SERIAL_PROTOCOL(" i:");
  1728. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  1729. SERIAL_PROTOCOL(" d:");
  1730. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  1731. SERIAL_PROTOCOLLN("");
  1732. }
  1733. break;
  1734. #endif //PIDTEMP
  1735. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1736. {
  1737. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  1738. const uint8_t NUM_PULSES=16;
  1739. const float PULSE_LENGTH=0.01524;
  1740. for(int i=0; i < NUM_PULSES; i++) {
  1741. WRITE(PHOTOGRAPH_PIN, HIGH);
  1742. _delay_ms(PULSE_LENGTH);
  1743. WRITE(PHOTOGRAPH_PIN, LOW);
  1744. _delay_ms(PULSE_LENGTH);
  1745. }
  1746. delay(7.33);
  1747. for(int i=0; i < NUM_PULSES; i++) {
  1748. WRITE(PHOTOGRAPH_PIN, HIGH);
  1749. _delay_ms(PULSE_LENGTH);
  1750. WRITE(PHOTOGRAPH_PIN, LOW);
  1751. _delay_ms(PULSE_LENGTH);
  1752. }
  1753. #endif
  1754. }
  1755. break;
  1756. #ifdef DOGLCD
  1757. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  1758. {
  1759. if (code_seen('C')) {
  1760. lcd_setcontrast( ((int)code_value())&63 );
  1761. }
  1762. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  1763. SERIAL_PROTOCOL(lcd_contrast);
  1764. SERIAL_PROTOCOLLN("");
  1765. }
  1766. break;
  1767. #endif
  1768. #ifdef PREVENT_DANGEROUS_EXTRUDE
  1769. case 302: // allow cold extrudes, or set the minimum extrude temperature
  1770. {
  1771. float temp = .0;
  1772. if (code_seen('S')) temp=code_value();
  1773. set_extrude_min_temp(temp);
  1774. }
  1775. break;
  1776. #endif
  1777. case 303: // M303 PID autotune
  1778. {
  1779. float temp = 150.0;
  1780. int e=0;
  1781. int c=5;
  1782. if (code_seen('E')) e=code_value();
  1783. if (e<0)
  1784. temp=70;
  1785. if (code_seen('S')) temp=code_value();
  1786. if (code_seen('C')) c=code_value();
  1787. PID_autotune(temp, e, c);
  1788. }
  1789. break;
  1790. case 400: // M400 finish all moves
  1791. {
  1792. st_synchronize();
  1793. }
  1794. break;
  1795. case 500: // M500 Store settings in EEPROM
  1796. {
  1797. Config_StoreSettings();
  1798. }
  1799. break;
  1800. case 501: // M501 Read settings from EEPROM
  1801. {
  1802. Config_RetrieveSettings();
  1803. }
  1804. break;
  1805. case 502: // M502 Revert to default settings
  1806. {
  1807. Config_ResetDefault();
  1808. }
  1809. break;
  1810. case 503: // M503 print settings currently in memory
  1811. {
  1812. Config_PrintSettings();
  1813. }
  1814. break;
  1815. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  1816. case 540:
  1817. {
  1818. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  1819. }
  1820. break;
  1821. #endif
  1822. #ifdef FILAMENTCHANGEENABLE
  1823. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  1824. {
  1825. float target[4];
  1826. float lastpos[4];
  1827. target[X_AXIS]=current_position[X_AXIS];
  1828. target[Y_AXIS]=current_position[Y_AXIS];
  1829. target[Z_AXIS]=current_position[Z_AXIS];
  1830. target[E_AXIS]=current_position[E_AXIS];
  1831. lastpos[X_AXIS]=current_position[X_AXIS];
  1832. lastpos[Y_AXIS]=current_position[Y_AXIS];
  1833. lastpos[Z_AXIS]=current_position[Z_AXIS];
  1834. lastpos[E_AXIS]=current_position[E_AXIS];
  1835. //retract by E
  1836. if(code_seen('E'))
  1837. {
  1838. target[E_AXIS]+= code_value();
  1839. }
  1840. else
  1841. {
  1842. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  1843. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  1844. #endif
  1845. }
  1846. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1847. //lift Z
  1848. if(code_seen('Z'))
  1849. {
  1850. target[Z_AXIS]+= code_value();
  1851. }
  1852. else
  1853. {
  1854. #ifdef FILAMENTCHANGE_ZADD
  1855. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  1856. #endif
  1857. }
  1858. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1859. //move xy
  1860. if(code_seen('X'))
  1861. {
  1862. target[X_AXIS]+= code_value();
  1863. }
  1864. else
  1865. {
  1866. #ifdef FILAMENTCHANGE_XPOS
  1867. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  1868. #endif
  1869. }
  1870. if(code_seen('Y'))
  1871. {
  1872. target[Y_AXIS]= code_value();
  1873. }
  1874. else
  1875. {
  1876. #ifdef FILAMENTCHANGE_YPOS
  1877. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  1878. #endif
  1879. }
  1880. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1881. if(code_seen('L'))
  1882. {
  1883. target[E_AXIS]+= code_value();
  1884. }
  1885. else
  1886. {
  1887. #ifdef FILAMENTCHANGE_FINALRETRACT
  1888. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  1889. #endif
  1890. }
  1891. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  1892. //finish moves
  1893. st_synchronize();
  1894. //disable extruder steppers so filament can be removed
  1895. disable_e0();
  1896. disable_e1();
  1897. disable_e2();
  1898. delay(100);
  1899. LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  1900. uint8_t cnt=0;
  1901. while(!lcd_clicked()){
  1902. cnt++;
  1903. manage_heater();
  1904. manage_inactivity();
  1905. lcd_update();
  1906. if(cnt==0)
  1907. {
  1908. #if BEEPER > 0
  1909. SET_OUTPUT(BEEPER);
  1910. WRITE(BEEPER,HIGH);
  1911. delay(3);
  1912. WRITE(BEEPER,LOW);
  1913. delay(3);
  1914. #else
  1915. lcd_buzz(1000/6,100);
  1916. #endif
  1917. }
  1918. }
  1919. //return to normal
  1920. if(code_seen('L'))
  1921. {
  1922. target[E_AXIS]+= -code_value();
  1923. }
  1924. else
  1925. {
  1926. #ifdef FILAMENTCHANGE_FINALRETRACT
  1927. target[E_AXIS]+=(-1)*FILAMENTCHANGE_FINALRETRACT ;
  1928. #endif
  1929. }
  1930. current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  1931. plan_set_e_position(current_position[E_AXIS]);
  1932. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //should do nothing
  1933. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move xy back
  1934. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move z back
  1935. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], lastpos[E_AXIS], feedrate/60, active_extruder); //final untretract
  1936. }
  1937. break;
  1938. #endif //FILAMENTCHANGEENABLE
  1939. #ifdef DUAL_X_CARRIAGE
  1940. case 605: // Set dual x-carriage movement mode:
  1941. // M605 S0: Full control mode. The slicer has full control over x-carriage movement
  1942. // M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement
  1943. // M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn
  1944. // millimeters x-offset and an optional differential hotend temperature of
  1945. // mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate
  1946. // the first with a spacing of 100mm in the x direction and 2 degrees hotter.
  1947. //
  1948. // Note: the X axis should be homed after changing dual x-carriage mode.
  1949. {
  1950. st_synchronize();
  1951. if (code_seen('S'))
  1952. dual_x_carriage_mode = code_value();
  1953. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  1954. {
  1955. if (code_seen('X'))
  1956. duplicate_extruder_x_offset = max(code_value(),X2_MIN_POS - x_home_pos(0));
  1957. if (code_seen('R'))
  1958. duplicate_extruder_temp_offset = code_value();
  1959. SERIAL_ECHO_START;
  1960. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  1961. SERIAL_ECHO(" ");
  1962. SERIAL_ECHO(extruder_offset[X_AXIS][0]);
  1963. SERIAL_ECHO(",");
  1964. SERIAL_ECHO(extruder_offset[Y_AXIS][0]);
  1965. SERIAL_ECHO(" ");
  1966. SERIAL_ECHO(duplicate_extruder_x_offset);
  1967. SERIAL_ECHO(",");
  1968. SERIAL_ECHOLN(extruder_offset[Y_AXIS][1]);
  1969. }
  1970. else if (dual_x_carriage_mode != DXC_FULL_CONTROL_MODE && dual_x_carriage_mode != DXC_AUTO_PARK_MODE)
  1971. {
  1972. dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  1973. }
  1974. active_extruder_parked = false;
  1975. extruder_duplication_enabled = false;
  1976. delayed_move_time = 0;
  1977. }
  1978. break;
  1979. #endif //DUAL_X_CARRIAGE
  1980. case 907: // M907 Set digital trimpot motor current using axis codes.
  1981. {
  1982. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1983. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  1984. if(code_seen('B')) digipot_current(4,code_value());
  1985. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  1986. #endif
  1987. }
  1988. break;
  1989. case 908: // M908 Control digital trimpot directly.
  1990. {
  1991. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1992. uint8_t channel,current;
  1993. if(code_seen('P')) channel=code_value();
  1994. if(code_seen('S')) current=code_value();
  1995. digitalPotWrite(channel, current);
  1996. #endif
  1997. }
  1998. break;
  1999. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  2000. {
  2001. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  2002. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  2003. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  2004. if(code_seen('B')) microstep_mode(4,code_value());
  2005. microstep_readings();
  2006. #endif
  2007. }
  2008. break;
  2009. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  2010. {
  2011. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  2012. if(code_seen('S')) switch((int)code_value())
  2013. {
  2014. case 1:
  2015. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  2016. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  2017. break;
  2018. case 2:
  2019. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  2020. if(code_seen('B')) microstep_ms(4,-1,code_value());
  2021. break;
  2022. }
  2023. microstep_readings();
  2024. #endif
  2025. }
  2026. break;
  2027. case 999: // M999: Restart after being stopped
  2028. Stopped = false;
  2029. lcd_reset_alert_level();
  2030. gcode_LastN = Stopped_gcode_LastN;
  2031. FlushSerialRequestResend();
  2032. break;
  2033. }
  2034. }
  2035. else if(code_seen('T'))
  2036. {
  2037. tmp_extruder = code_value();
  2038. if(tmp_extruder >= EXTRUDERS) {
  2039. SERIAL_ECHO_START;
  2040. SERIAL_ECHO("T");
  2041. SERIAL_ECHO(tmp_extruder);
  2042. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  2043. }
  2044. else {
  2045. boolean make_move = false;
  2046. if(code_seen('F')) {
  2047. make_move = true;
  2048. next_feedrate = code_value();
  2049. if(next_feedrate > 0.0) {
  2050. feedrate = next_feedrate;
  2051. }
  2052. }
  2053. #if EXTRUDERS > 1
  2054. if(tmp_extruder != active_extruder) {
  2055. // Save current position to return to after applying extruder offset
  2056. memcpy(destination, current_position, sizeof(destination));
  2057. #ifdef DUAL_X_CARRIAGE
  2058. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE && Stopped == false &&
  2059. (delayed_move_time != 0 || current_position[X_AXIS] != x_home_pos(active_extruder)))
  2060. {
  2061. // Park old head: 1) raise 2) move to park position 3) lower
  2062. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  2063. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2064. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  2065. current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
  2066. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
  2067. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2068. st_synchronize();
  2069. }
  2070. // apply Y & Z extruder offset (x offset is already used in determining home pos)
  2071. current_position[Y_AXIS] = current_position[Y_AXIS] -
  2072. extruder_offset[Y_AXIS][active_extruder] +
  2073. extruder_offset[Y_AXIS][tmp_extruder];
  2074. current_position[Z_AXIS] = current_position[Z_AXIS] -
  2075. extruder_offset[Z_AXIS][active_extruder] +
  2076. extruder_offset[Z_AXIS][tmp_extruder];
  2077. active_extruder = tmp_extruder;
  2078. // This function resets the max/min values - the current position may be overwritten below.
  2079. axis_is_at_home(X_AXIS);
  2080. if (dual_x_carriage_mode == DXC_FULL_CONTROL_MODE)
  2081. {
  2082. current_position[X_AXIS] = inactive_extruder_x_pos;
  2083. inactive_extruder_x_pos = destination[X_AXIS];
  2084. }
  2085. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  2086. {
  2087. active_extruder_parked = (active_extruder == 0); // this triggers the second extruder to move into the duplication position
  2088. if (active_extruder == 0 || active_extruder_parked)
  2089. current_position[X_AXIS] = inactive_extruder_x_pos;
  2090. else
  2091. current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset;
  2092. inactive_extruder_x_pos = destination[X_AXIS];
  2093. extruder_duplication_enabled = false;
  2094. }
  2095. else
  2096. {
  2097. // record raised toolhead position for use by unpark
  2098. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  2099. raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT;
  2100. active_extruder_parked = true;
  2101. delayed_move_time = 0;
  2102. }
  2103. #else
  2104. // Offset extruder (only by XY)
  2105. int i;
  2106. for(i = 0; i < 2; i++) {
  2107. current_position[i] = current_position[i] -
  2108. extruder_offset[i][active_extruder] +
  2109. extruder_offset[i][tmp_extruder];
  2110. }
  2111. // Set the new active extruder and position
  2112. active_extruder = tmp_extruder;
  2113. #endif //else DUAL_X_CARRIAGE
  2114. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2115. // Move to the old position if 'F' was in the parameters
  2116. if(make_move && Stopped == false) {
  2117. prepare_move();
  2118. }
  2119. }
  2120. #endif
  2121. SERIAL_ECHO_START;
  2122. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  2123. SERIAL_PROTOCOLLN((int)active_extruder);
  2124. }
  2125. }
  2126. else
  2127. {
  2128. SERIAL_ECHO_START;
  2129. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  2130. SERIAL_ECHO(cmdbuffer[bufindr]);
  2131. SERIAL_ECHOLNPGM("\"");
  2132. }
  2133. ClearToSend();
  2134. }
  2135. void FlushSerialRequestResend()
  2136. {
  2137. //char cmdbuffer[bufindr][100]="Resend:";
  2138. MYSERIAL.flush();
  2139. SERIAL_PROTOCOLPGM(MSG_RESEND);
  2140. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  2141. ClearToSend();
  2142. }
  2143. void ClearToSend()
  2144. {
  2145. previous_millis_cmd = millis();
  2146. #ifdef SDSUPPORT
  2147. if(fromsd[bufindr])
  2148. return;
  2149. #endif //SDSUPPORT
  2150. SERIAL_PROTOCOLLNPGM(MSG_OK);
  2151. }
  2152. void get_coordinates()
  2153. {
  2154. bool seen[4]={false,false,false,false};
  2155. for(int8_t i=0; i < NUM_AXIS; i++) {
  2156. if(code_seen(axis_codes[i]))
  2157. {
  2158. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  2159. seen[i]=true;
  2160. }
  2161. else destination[i] = current_position[i]; //Are these else lines really needed?
  2162. }
  2163. if(code_seen('F')) {
  2164. next_feedrate = code_value();
  2165. if(next_feedrate > 0.0) feedrate = next_feedrate;
  2166. }
  2167. #ifdef FWRETRACT
  2168. if(autoretract_enabled)
  2169. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  2170. {
  2171. float echange=destination[E_AXIS]-current_position[E_AXIS];
  2172. if(echange<-MIN_RETRACT) //retract
  2173. {
  2174. if(!retracted)
  2175. {
  2176. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  2177. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  2178. float correctede=-echange-retract_length;
  2179. //to generate the additional steps, not the destination is changed, but inversely the current position
  2180. current_position[E_AXIS]+=-correctede;
  2181. feedrate=retract_feedrate;
  2182. retracted=true;
  2183. }
  2184. }
  2185. else
  2186. if(echange>MIN_RETRACT) //retract_recover
  2187. {
  2188. if(retracted)
  2189. {
  2190. //current_position[Z_AXIS]+=-retract_zlift;
  2191. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  2192. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  2193. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  2194. feedrate=retract_recover_feedrate;
  2195. retracted=false;
  2196. }
  2197. }
  2198. }
  2199. #endif //FWRETRACT
  2200. }
  2201. void get_arc_coordinates()
  2202. {
  2203. #ifdef SF_ARC_FIX
  2204. bool relative_mode_backup = relative_mode;
  2205. relative_mode = true;
  2206. #endif
  2207. get_coordinates();
  2208. #ifdef SF_ARC_FIX
  2209. relative_mode=relative_mode_backup;
  2210. #endif
  2211. if(code_seen('I')) {
  2212. offset[0] = code_value();
  2213. }
  2214. else {
  2215. offset[0] = 0.0;
  2216. }
  2217. if(code_seen('J')) {
  2218. offset[1] = code_value();
  2219. }
  2220. else {
  2221. offset[1] = 0.0;
  2222. }
  2223. }
  2224. void clamp_to_software_endstops(float target[3])
  2225. {
  2226. if (min_software_endstops) {
  2227. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  2228. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  2229. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  2230. }
  2231. if (max_software_endstops) {
  2232. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  2233. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  2234. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  2235. }
  2236. }
  2237. #ifdef DELTA
  2238. void calculate_delta(float cartesian[3])
  2239. {
  2240. delta[X_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2241. - sq(DELTA_TOWER1_X-cartesian[X_AXIS])
  2242. - sq(DELTA_TOWER1_Y-cartesian[Y_AXIS])
  2243. ) + cartesian[Z_AXIS];
  2244. delta[Y_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2245. - sq(DELTA_TOWER2_X-cartesian[X_AXIS])
  2246. - sq(DELTA_TOWER2_Y-cartesian[Y_AXIS])
  2247. ) + cartesian[Z_AXIS];
  2248. delta[Z_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2249. - sq(DELTA_TOWER3_X-cartesian[X_AXIS])
  2250. - sq(DELTA_TOWER3_Y-cartesian[Y_AXIS])
  2251. ) + cartesian[Z_AXIS];
  2252. /*
  2253. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  2254. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  2255. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  2256. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  2257. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  2258. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  2259. */
  2260. }
  2261. #endif
  2262. void prepare_move()
  2263. {
  2264. clamp_to_software_endstops(destination);
  2265. previous_millis_cmd = millis();
  2266. #ifdef DELTA
  2267. float difference[NUM_AXIS];
  2268. for (int8_t i=0; i < NUM_AXIS; i++) {
  2269. difference[i] = destination[i] - current_position[i];
  2270. }
  2271. float cartesian_mm = sqrt(sq(difference[X_AXIS]) +
  2272. sq(difference[Y_AXIS]) +
  2273. sq(difference[Z_AXIS]));
  2274. if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
  2275. if (cartesian_mm < 0.000001) { return; }
  2276. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  2277. int steps = max(1, int(DELTA_SEGMENTS_PER_SECOND * seconds));
  2278. // SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  2279. // SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  2280. // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  2281. for (int s = 1; s <= steps; s++) {
  2282. float fraction = float(s) / float(steps);
  2283. for(int8_t i=0; i < NUM_AXIS; i++) {
  2284. destination[i] = current_position[i] + difference[i] * fraction;
  2285. }
  2286. calculate_delta(destination);
  2287. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  2288. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  2289. active_extruder);
  2290. }
  2291. #else
  2292. #ifdef DUAL_X_CARRIAGE
  2293. if (active_extruder_parked)
  2294. {
  2295. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0)
  2296. {
  2297. // move duplicate extruder into correct duplication position.
  2298. plan_set_position(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2299. plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset, current_position[Y_AXIS], current_position[Z_AXIS],
  2300. current_position[E_AXIS], max_feedrate[X_AXIS], 1);
  2301. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2302. st_synchronize();
  2303. extruder_duplication_enabled = true;
  2304. active_extruder_parked = false;
  2305. }
  2306. else if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE) // handle unparking of head
  2307. {
  2308. if (current_position[E_AXIS] == destination[E_AXIS])
  2309. {
  2310. // this is a travel move - skit it but keep track of current position (so that it can later
  2311. // be used as start of first non-travel move)
  2312. if (delayed_move_time != 0xFFFFFFFFUL)
  2313. {
  2314. memcpy(current_position, destination, sizeof(current_position));
  2315. if (destination[Z_AXIS] > raised_parked_position[Z_AXIS])
  2316. raised_parked_position[Z_AXIS] = destination[Z_AXIS];
  2317. delayed_move_time = millis();
  2318. return;
  2319. }
  2320. }
  2321. delayed_move_time = 0;
  2322. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  2323. plan_buffer_line(raised_parked_position[X_AXIS], raised_parked_position[Y_AXIS], raised_parked_position[Z_AXIS], current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2324. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], raised_parked_position[Z_AXIS],
  2325. current_position[E_AXIS], min(max_feedrate[X_AXIS],max_feedrate[Y_AXIS]), active_extruder);
  2326. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  2327. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2328. active_extruder_parked = false;
  2329. }
  2330. }
  2331. #endif //DUAL_X_CARRIAGE
  2332. // Do not use feedmultiply for E or Z only moves
  2333. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  2334. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  2335. }
  2336. else {
  2337. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  2338. }
  2339. #endif //else DELTA
  2340. for(int8_t i=0; i < NUM_AXIS; i++) {
  2341. current_position[i] = destination[i];
  2342. }
  2343. }
  2344. void prepare_arc_move(char isclockwise) {
  2345. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  2346. // Trace the arc
  2347. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  2348. // As far as the parser is concerned, the position is now == target. In reality the
  2349. // motion control system might still be processing the action and the real tool position
  2350. // in any intermediate location.
  2351. for(int8_t i=0; i < NUM_AXIS; i++) {
  2352. current_position[i] = destination[i];
  2353. }
  2354. previous_millis_cmd = millis();
  2355. }
  2356. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  2357. #if defined(FAN_PIN)
  2358. #if CONTROLLERFAN_PIN == FAN_PIN
  2359. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  2360. #endif
  2361. #endif
  2362. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  2363. unsigned long lastMotorCheck = 0;
  2364. void controllerFan()
  2365. {
  2366. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  2367. {
  2368. lastMotorCheck = millis();
  2369. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  2370. #if EXTRUDERS > 2
  2371. || !READ(E2_ENABLE_PIN)
  2372. #endif
  2373. #if EXTRUDER > 1
  2374. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  2375. || !READ(X2_ENABLE_PIN)
  2376. #endif
  2377. || !READ(E1_ENABLE_PIN)
  2378. #endif
  2379. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  2380. {
  2381. lastMotor = millis(); //... set time to NOW so the fan will turn on
  2382. }
  2383. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  2384. {
  2385. digitalWrite(CONTROLLERFAN_PIN, 0);
  2386. analogWrite(CONTROLLERFAN_PIN, 0);
  2387. }
  2388. else
  2389. {
  2390. // allows digital or PWM fan output to be used (see M42 handling)
  2391. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  2392. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  2393. }
  2394. }
  2395. }
  2396. #endif
  2397. void manage_inactivity()
  2398. {
  2399. if( (millis() - previous_millis_cmd) > max_inactive_time )
  2400. if(max_inactive_time)
  2401. kill();
  2402. if(stepper_inactive_time) {
  2403. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  2404. {
  2405. if(blocks_queued() == false) {
  2406. disable_x();
  2407. disable_y();
  2408. disable_z();
  2409. disable_e0();
  2410. disable_e1();
  2411. disable_e2();
  2412. }
  2413. }
  2414. }
  2415. #if defined(KILL_PIN) && KILL_PIN > -1
  2416. if( 0 == READ(KILL_PIN) )
  2417. kill();
  2418. #endif
  2419. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  2420. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  2421. #endif
  2422. #ifdef EXTRUDER_RUNOUT_PREVENT
  2423. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  2424. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  2425. {
  2426. bool oldstatus=READ(E0_ENABLE_PIN);
  2427. enable_e0();
  2428. float oldepos=current_position[E_AXIS];
  2429. float oldedes=destination[E_AXIS];
  2430. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  2431. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  2432. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  2433. current_position[E_AXIS]=oldepos;
  2434. destination[E_AXIS]=oldedes;
  2435. plan_set_e_position(oldepos);
  2436. previous_millis_cmd=millis();
  2437. st_synchronize();
  2438. WRITE(E0_ENABLE_PIN,oldstatus);
  2439. }
  2440. #endif
  2441. #if defined(DUAL_X_CARRIAGE)
  2442. // handle delayed move timeout
  2443. if (delayed_move_time != 0 && (millis() - delayed_move_time) > 1000 && Stopped == false)
  2444. {
  2445. // travel moves have been received so enact them
  2446. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  2447. memcpy(destination,current_position,sizeof(destination));
  2448. prepare_move();
  2449. }
  2450. #endif
  2451. check_axes_activity();
  2452. }
  2453. void kill()
  2454. {
  2455. cli(); // Stop interrupts
  2456. disable_heater();
  2457. disable_x();
  2458. disable_y();
  2459. disable_z();
  2460. disable_e0();
  2461. disable_e1();
  2462. disable_e2();
  2463. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  2464. pinMode(PS_ON_PIN,INPUT);
  2465. #endif
  2466. SERIAL_ERROR_START;
  2467. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  2468. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  2469. suicide();
  2470. while(1) { /* Intentionally left empty */ } // Wait for reset
  2471. }
  2472. void Stop()
  2473. {
  2474. disable_heater();
  2475. if(Stopped == false) {
  2476. Stopped = true;
  2477. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  2478. SERIAL_ERROR_START;
  2479. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  2480. LCD_MESSAGEPGM(MSG_STOPPED);
  2481. }
  2482. }
  2483. bool IsStopped() { return Stopped; };
  2484. #ifdef FAST_PWM_FAN
  2485. void setPwmFrequency(uint8_t pin, int val)
  2486. {
  2487. val &= 0x07;
  2488. switch(digitalPinToTimer(pin))
  2489. {
  2490. #if defined(TCCR0A)
  2491. case TIMER0A:
  2492. case TIMER0B:
  2493. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  2494. // TCCR0B |= val;
  2495. break;
  2496. #endif
  2497. #if defined(TCCR1A)
  2498. case TIMER1A:
  2499. case TIMER1B:
  2500. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2501. // TCCR1B |= val;
  2502. break;
  2503. #endif
  2504. #if defined(TCCR2)
  2505. case TIMER2:
  2506. case TIMER2:
  2507. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2508. TCCR2 |= val;
  2509. break;
  2510. #endif
  2511. #if defined(TCCR2A)
  2512. case TIMER2A:
  2513. case TIMER2B:
  2514. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  2515. TCCR2B |= val;
  2516. break;
  2517. #endif
  2518. #if defined(TCCR3A)
  2519. case TIMER3A:
  2520. case TIMER3B:
  2521. case TIMER3C:
  2522. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  2523. TCCR3B |= val;
  2524. break;
  2525. #endif
  2526. #if defined(TCCR4A)
  2527. case TIMER4A:
  2528. case TIMER4B:
  2529. case TIMER4C:
  2530. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  2531. TCCR4B |= val;
  2532. break;
  2533. #endif
  2534. #if defined(TCCR5A)
  2535. case TIMER5A:
  2536. case TIMER5B:
  2537. case TIMER5C:
  2538. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  2539. TCCR5B |= val;
  2540. break;
  2541. #endif
  2542. }
  2543. }
  2544. #endif //FAST_PWM_FAN
  2545. bool setTargetedHotend(int code){
  2546. tmp_extruder = active_extruder;
  2547. if(code_seen('T')) {
  2548. tmp_extruder = code_value();
  2549. if(tmp_extruder >= EXTRUDERS) {
  2550. SERIAL_ECHO_START;
  2551. switch(code){
  2552. case 104:
  2553. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  2554. break;
  2555. case 105:
  2556. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  2557. break;
  2558. case 109:
  2559. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  2560. break;
  2561. case 218:
  2562. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  2563. break;
  2564. }
  2565. SERIAL_ECHOLN(tmp_extruder);
  2566. return true;
  2567. }
  2568. }
  2569. return false;
  2570. }