My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin_main.cpp 76KB

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