My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Marlin_main.cpp 71KB

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