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 73KB

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