My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Marlin_main.cpp 80KB

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