ConfigurationStore.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #include "Marlin.h"
  2. #include "planner.h"
  3. #include "temperature.h"
  4. #include "ultralcd.h"
  5. #include "ConfigurationStore.h"
  6. #include "Configuration_prusa.h"
  7. #ifdef MESH_BED_LEVELING
  8. #include "mesh_bed_leveling.h"
  9. #endif
  10. void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size)
  11. {
  12. do
  13. {
  14. eeprom_write_byte((unsigned char*)pos, *value);
  15. pos++;
  16. value++;
  17. }while(--size);
  18. }
  19. #define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value))
  20. void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size)
  21. {
  22. do
  23. {
  24. *value = eeprom_read_byte((unsigned char*)pos);
  25. pos++;
  26. value++;
  27. }while(--size);
  28. }
  29. #define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value))
  30. //======================================================================================
  31. #define EEPROM_OFFSET 20
  32. // IMPORTANT: Whenever there are changes made to the variables stored in EEPROM
  33. // in the functions below, also increment the version number. This makes sure that
  34. // the default values are used whenever there is a change to the data, to prevent
  35. // wrong data being written to the variables.
  36. // ALSO: always make sure the variables in the Store and retrieve sections are in the same order.
  37. #ifdef SNMM
  38. #define EEPROM_VERSION "M1"
  39. #else
  40. #define EEPROM_VERSION "V1"
  41. #endif
  42. #ifdef EEPROM_SETTINGS
  43. void Config_StoreSettings()
  44. {
  45. char ver[4]= "000";
  46. int i=EEPROM_OFFSET;
  47. EEPROM_WRITE_VAR(i,ver); // invalidate data first
  48. EEPROM_WRITE_VAR(i,axis_steps_per_unit);
  49. EEPROM_WRITE_VAR(i,max_feedrate);
  50. EEPROM_WRITE_VAR(i,max_acceleration_units_per_sq_second);
  51. EEPROM_WRITE_VAR(i,acceleration);
  52. EEPROM_WRITE_VAR(i,retract_acceleration);
  53. EEPROM_WRITE_VAR(i,minimumfeedrate);
  54. EEPROM_WRITE_VAR(i,mintravelfeedrate);
  55. EEPROM_WRITE_VAR(i,minsegmenttime);
  56. EEPROM_WRITE_VAR(i,max_jerk[X_AXIS]);
  57. EEPROM_WRITE_VAR(i,max_jerk[Y_AXIS]);
  58. EEPROM_WRITE_VAR(i,max_jerk[Z_AXIS]);
  59. EEPROM_WRITE_VAR(i,max_jerk[E_AXIS]);
  60. EEPROM_WRITE_VAR(i,add_homing);
  61. #ifndef ULTIPANEL
  62. int plaPreheatHotendTemp = PLA_PREHEAT_HOTEND_TEMP, plaPreheatHPBTemp = PLA_PREHEAT_HPB_TEMP, plaPreheatFanSpeed = PLA_PREHEAT_FAN_SPEED;
  63. int absPreheatHotendTemp = ABS_PREHEAT_HOTEND_TEMP, absPreheatHPBTemp = ABS_PREHEAT_HPB_TEMP, absPreheatFanSpeed = ABS_PREHEAT_FAN_SPEED;
  64. #endif
  65. /* EEPROM_WRITE_VAR(i,plaPreheatHotendTemp);
  66. EEPROM_WRITE_VAR(i,plaPreheatHPBTemp);
  67. EEPROM_WRITE_VAR(i,plaPreheatFanSpeed);
  68. EEPROM_WRITE_VAR(i,absPreheatHotendTemp);
  69. EEPROM_WRITE_VAR(i,absPreheatHPBTemp);
  70. EEPROM_WRITE_VAR(i,absPreheatFanSpeed);
  71. */
  72. EEPROM_WRITE_VAR(i,zprobe_zoffset);
  73. #ifdef PIDTEMP
  74. EEPROM_WRITE_VAR(i,Kp);
  75. EEPROM_WRITE_VAR(i,Ki);
  76. EEPROM_WRITE_VAR(i,Kd);
  77. #else
  78. float dummy = 3000.0f;
  79. EEPROM_WRITE_VAR(i,dummy);
  80. dummy = 0.0f;
  81. EEPROM_WRITE_VAR(i,dummy);
  82. EEPROM_WRITE_VAR(i,dummy);
  83. #endif
  84. #ifdef PIDTEMPBED
  85. EEPROM_WRITE_VAR(i, bedKp);
  86. EEPROM_WRITE_VAR(i, bedKi);
  87. EEPROM_WRITE_VAR(i, bedKd);
  88. #endif
  89. #ifndef DOGLCD
  90. int lcd_contrast = 32;
  91. #endif
  92. EEPROM_WRITE_VAR(i,lcd_contrast);
  93. #ifdef FWRETRACT
  94. EEPROM_WRITE_VAR(i,autoretract_enabled);
  95. EEPROM_WRITE_VAR(i,retract_length);
  96. #if EXTRUDERS > 1
  97. EEPROM_WRITE_VAR(i,retract_length_swap);
  98. #endif
  99. EEPROM_WRITE_VAR(i,retract_feedrate);
  100. EEPROM_WRITE_VAR(i,retract_zlift);
  101. EEPROM_WRITE_VAR(i,retract_recover_length);
  102. #if EXTRUDERS > 1
  103. EEPROM_WRITE_VAR(i,retract_recover_length_swap);
  104. #endif
  105. EEPROM_WRITE_VAR(i,retract_recover_feedrate);
  106. #endif
  107. // Save filament sizes
  108. EEPROM_WRITE_VAR(i, volumetric_enabled);
  109. EEPROM_WRITE_VAR(i, filament_size[0]);
  110. #if EXTRUDERS > 1
  111. EEPROM_WRITE_VAR(i, filament_size[1]);
  112. #if EXTRUDERS > 2
  113. EEPROM_WRITE_VAR(i, filament_size[2]);
  114. #endif
  115. #endif
  116. /*MYSERIAL.print("Top address used:\n");
  117. MYSERIAL.print(i);
  118. MYSERIAL.print("\n");
  119. */
  120. char ver2[4]=EEPROM_VERSION;
  121. i=EEPROM_OFFSET;
  122. EEPROM_WRITE_VAR(i,ver2); // validate data
  123. SERIAL_ECHO_START;
  124. SERIAL_ECHOLNPGM("Settings Stored");
  125. }
  126. #endif //EEPROM_SETTINGS
  127. #ifndef DISABLE_M503
  128. void Config_PrintSettings()
  129. { // Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown
  130. SERIAL_ECHO_START;
  131. SERIAL_ECHOLNPGM("Steps per unit:");
  132. SERIAL_ECHO_START;
  133. SERIAL_ECHOPAIR(" M92 X",axis_steps_per_unit[X_AXIS]);
  134. SERIAL_ECHOPAIR(" Y",axis_steps_per_unit[Y_AXIS]);
  135. SERIAL_ECHOPAIR(" Z",axis_steps_per_unit[Z_AXIS]);
  136. SERIAL_ECHOPAIR(" E",axis_steps_per_unit[E_AXIS]);
  137. SERIAL_ECHOLN("");
  138. SERIAL_ECHO_START;
  139. SERIAL_ECHOLNPGM("Maximum feedrates (mm/s):");
  140. SERIAL_ECHO_START;
  141. SERIAL_ECHOPAIR(" M203 X", max_feedrate[X_AXIS]);
  142. SERIAL_ECHOPAIR(" Y", max_feedrate[Y_AXIS]);
  143. SERIAL_ECHOPAIR(" Z", max_feedrate[Z_AXIS]);
  144. SERIAL_ECHOPAIR(" E", max_feedrate[E_AXIS]);
  145. SERIAL_ECHOLN("");
  146. SERIAL_ECHO_START;
  147. SERIAL_ECHOLNPGM("Maximum Acceleration (mm/s2):");
  148. SERIAL_ECHO_START;
  149. SERIAL_ECHOPAIR(" M201 X" ,max_acceleration_units_per_sq_second[X_AXIS] );
  150. SERIAL_ECHOPAIR(" Y" , max_acceleration_units_per_sq_second[Y_AXIS] );
  151. SERIAL_ECHOPAIR(" Z" ,max_acceleration_units_per_sq_second[Z_AXIS] );
  152. SERIAL_ECHOPAIR(" E" ,max_acceleration_units_per_sq_second[E_AXIS]);
  153. SERIAL_ECHOLN("");
  154. SERIAL_ECHO_START;
  155. SERIAL_ECHOLNPGM("Acceleration: S=acceleration, T=retract acceleration");
  156. SERIAL_ECHO_START;
  157. SERIAL_ECHOPAIR(" M204 S",acceleration );
  158. SERIAL_ECHOPAIR(" T" ,retract_acceleration);
  159. SERIAL_ECHOLN("");
  160. SERIAL_ECHO_START;
  161. SERIAL_ECHOLNPGM("Advanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum XY jerk (mm/s), Z=maximum Z jerk (mm/s), E=maximum E jerk (mm/s)");
  162. SERIAL_ECHO_START;
  163. SERIAL_ECHOPAIR(" M205 S",minimumfeedrate );
  164. SERIAL_ECHOPAIR(" T" ,mintravelfeedrate );
  165. SERIAL_ECHOPAIR(" B" ,minsegmenttime );
  166. SERIAL_ECHOPAIR(" X" ,max_jerk[X_AXIS] );
  167. SERIAL_ECHOPAIR(" Y" ,max_jerk[Y_AXIS] );
  168. SERIAL_ECHOPAIR(" Z" ,max_jerk[Z_AXIS] );
  169. SERIAL_ECHOPAIR(" E" ,max_jerk[E_AXIS] );
  170. SERIAL_ECHOLN("");
  171. SERIAL_ECHO_START;
  172. SERIAL_ECHOLNPGM("Home offset (mm):");
  173. SERIAL_ECHO_START;
  174. SERIAL_ECHOPAIR(" M206 X",add_homing[X_AXIS] );
  175. SERIAL_ECHOPAIR(" Y" ,add_homing[Y_AXIS] );
  176. SERIAL_ECHOPAIR(" Z" ,add_homing[Z_AXIS] );
  177. SERIAL_ECHOLN("");
  178. #ifdef PIDTEMP
  179. SERIAL_ECHO_START;
  180. SERIAL_ECHOLNPGM("PID settings:");
  181. SERIAL_ECHO_START;
  182. SERIAL_ECHOPAIR(" M301 P",Kp);
  183. SERIAL_ECHOPAIR(" I" ,unscalePID_i(Ki));
  184. SERIAL_ECHOPAIR(" D" ,unscalePID_d(Kd));
  185. SERIAL_ECHOLN("");
  186. #endif
  187. #ifdef PIDTEMPBED
  188. SERIAL_ECHO_START;
  189. SERIAL_ECHOLNPGM("PID heatbed settings:");
  190. SERIAL_ECHO_START;
  191. SERIAL_ECHOPAIR(" M304 P", bedKp);
  192. SERIAL_ECHOPAIR(" I", unscalePID_i(bedKi));
  193. SERIAL_ECHOPAIR(" D", unscalePID_d(bedKd));
  194. SERIAL_ECHOLN("");
  195. #endif
  196. #ifdef FWRETRACT
  197. SERIAL_ECHO_START;
  198. SERIAL_ECHOLNPGM("Retract: S=Length (mm) F:Speed (mm/m) Z: ZLift (mm)");
  199. SERIAL_ECHO_START;
  200. SERIAL_ECHOPAIR(" M207 S",retract_length);
  201. SERIAL_ECHOPAIR(" F" ,retract_feedrate*60);
  202. SERIAL_ECHOPAIR(" Z" ,retract_zlift);
  203. SERIAL_ECHOLN("");
  204. SERIAL_ECHO_START;
  205. SERIAL_ECHOLNPGM("Recover: S=Extra length (mm) F:Speed (mm/m)");
  206. SERIAL_ECHO_START;
  207. SERIAL_ECHOPAIR(" M208 S",retract_recover_length);
  208. SERIAL_ECHOPAIR(" F", retract_recover_feedrate*60);
  209. SERIAL_ECHOLN("");
  210. SERIAL_ECHO_START;
  211. SERIAL_ECHOLNPGM("Auto-Retract: S=0 to disable, 1 to interpret extrude-only moves as retracts or recoveries");
  212. SERIAL_ECHO_START;
  213. SERIAL_ECHOPAIR(" M209 S", (unsigned long)(autoretract_enabled ? 1 : 0));
  214. SERIAL_ECHOLN("");
  215. #if EXTRUDERS > 1
  216. SERIAL_ECHO_START;
  217. SERIAL_ECHOLNPGM("Multi-extruder settings:");
  218. SERIAL_ECHO_START;
  219. SERIAL_ECHOPAIR(" Swap retract length (mm): ", retract_length_swap);
  220. SERIAL_ECHOLN("");
  221. SERIAL_ECHO_START;
  222. SERIAL_ECHOPAIR(" Swap rec. addl. length (mm): ", retract_recover_length_swap);
  223. SERIAL_ECHOLN("");
  224. #endif
  225. SERIAL_ECHO_START;
  226. if (volumetric_enabled) {
  227. SERIAL_ECHOLNPGM("Filament settings:");
  228. SERIAL_ECHO_START;
  229. SERIAL_ECHOPAIR(" M200 D", filament_size[0]);
  230. SERIAL_ECHOLN("");
  231. #if EXTRUDERS > 1
  232. SERIAL_ECHO_START;
  233. SERIAL_ECHOPAIR(" M200 T1 D", filament_size[1]);
  234. SERIAL_ECHOLN("");
  235. #if EXTRUDERS > 2
  236. SERIAL_ECHO_START;
  237. SERIAL_ECHOPAIR(" M200 T2 D", filament_size[2]);
  238. SERIAL_ECHOLN("");
  239. #endif
  240. #endif
  241. } else {
  242. SERIAL_ECHOLNPGM("Filament settings: Disabled");
  243. }
  244. #endif
  245. }
  246. #endif
  247. #ifdef EEPROM_SETTINGS
  248. bool Config_RetrieveSettings()
  249. {
  250. int i=EEPROM_OFFSET;
  251. bool previous_settings_retrieved = true;
  252. char stored_ver[4];
  253. char ver[4]=EEPROM_VERSION;
  254. EEPROM_READ_VAR(i,stored_ver); //read stored version
  255. // SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << stored_ver << "]");
  256. if (strncmp(ver,stored_ver,3) == 0)
  257. {
  258. // version number match
  259. EEPROM_READ_VAR(i,axis_steps_per_unit);
  260. EEPROM_READ_VAR(i,max_feedrate);
  261. EEPROM_READ_VAR(i,max_acceleration_units_per_sq_second);
  262. // 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)
  263. reset_acceleration_rates();
  264. EEPROM_READ_VAR(i,acceleration);
  265. EEPROM_READ_VAR(i,retract_acceleration);
  266. EEPROM_READ_VAR(i,minimumfeedrate);
  267. EEPROM_READ_VAR(i,mintravelfeedrate);
  268. EEPROM_READ_VAR(i,minsegmenttime);
  269. EEPROM_READ_VAR(i,max_jerk[X_AXIS]);
  270. EEPROM_READ_VAR(i,max_jerk[Y_AXIS]);
  271. EEPROM_READ_VAR(i,max_jerk[Z_AXIS]);
  272. EEPROM_READ_VAR(i,max_jerk[E_AXIS]);
  273. EEPROM_READ_VAR(i,add_homing);
  274. #ifndef ULTIPANEL
  275. int plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed;
  276. int absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed;
  277. #endif
  278. /*
  279. EEPROM_READ_VAR(i,plaPreheatHotendTemp);
  280. EEPROM_READ_VAR(i,plaPreheatHPBTemp);
  281. EEPROM_READ_VAR(i,plaPreheatFanSpeed);
  282. EEPROM_READ_VAR(i,absPreheatHotendTemp);
  283. EEPROM_READ_VAR(i,absPreheatHPBTemp);
  284. EEPROM_READ_VAR(i,absPreheatFanSpeed);
  285. */
  286. EEPROM_READ_VAR(i,zprobe_zoffset);
  287. #ifndef PIDTEMP
  288. float Kp,Ki,Kd;
  289. #endif
  290. // do not need to scale PID values as the values in EEPROM are already scaled
  291. EEPROM_READ_VAR(i,Kp);
  292. EEPROM_READ_VAR(i,Ki);
  293. EEPROM_READ_VAR(i,Kd);
  294. #ifdef PIDTEMPBED
  295. EEPROM_READ_VAR(i, bedKp);
  296. EEPROM_READ_VAR(i, bedKi);
  297. EEPROM_READ_VAR(i, bedKd);
  298. #endif
  299. #ifndef DOGLCD
  300. int lcd_contrast;
  301. #endif
  302. EEPROM_READ_VAR(i,lcd_contrast);
  303. #ifdef FWRETRACT
  304. EEPROM_READ_VAR(i,autoretract_enabled);
  305. EEPROM_READ_VAR(i,retract_length);
  306. #if EXTRUDERS > 1
  307. EEPROM_READ_VAR(i,retract_length_swap);
  308. #endif
  309. EEPROM_READ_VAR(i,retract_feedrate);
  310. EEPROM_READ_VAR(i,retract_zlift);
  311. EEPROM_READ_VAR(i,retract_recover_length);
  312. #if EXTRUDERS > 1
  313. EEPROM_READ_VAR(i,retract_recover_length_swap);
  314. #endif
  315. EEPROM_READ_VAR(i,retract_recover_feedrate);
  316. #endif
  317. EEPROM_READ_VAR(i, volumetric_enabled);
  318. EEPROM_READ_VAR(i, filament_size[0]);
  319. #if EXTRUDERS > 1
  320. EEPROM_READ_VAR(i, filament_size[1]);
  321. #if EXTRUDERS > 2
  322. EEPROM_READ_VAR(i, filament_size[2]);
  323. #endif
  324. #endif
  325. calculate_volumetric_multipliers();
  326. // Call updatePID (similar to when we have processed M301)
  327. updatePID();
  328. SERIAL_ECHO_START;
  329. SERIAL_ECHOLNPGM("Stored settings retrieved");
  330. }
  331. else
  332. {
  333. Config_ResetDefault();
  334. //Return false to inform user that eeprom version was changed and firmware is using default hardcoded settings now.
  335. //In case that storing to eeprom was not used yet, do not inform user that hardcoded settings are used.
  336. if (eeprom_read_byte((uint8_t *)EEPROM_OFFSET) != 0xFF ||
  337. eeprom_read_byte((uint8_t *)EEPROM_OFFSET + 1) != 0xFF ||
  338. eeprom_read_byte((uint8_t *)EEPROM_OFFSET + 2) != 0xFF) {
  339. previous_settings_retrieved = false;
  340. }
  341. }
  342. #ifdef EEPROM_CHITCHAT
  343. Config_PrintSettings();
  344. #endif
  345. return previous_settings_retrieved;
  346. }
  347. #endif
  348. void Config_ResetDefault()
  349. {
  350. float tmp1[]=DEFAULT_AXIS_STEPS_PER_UNIT;
  351. float tmp2[]=DEFAULT_MAX_FEEDRATE;
  352. long tmp3[]=DEFAULT_MAX_ACCELERATION;
  353. for (short i=0;i<4;i++)
  354. {
  355. axis_steps_per_unit[i]=tmp1[i];
  356. max_feedrate[i]=tmp2[i];
  357. max_acceleration_units_per_sq_second[i]=tmp3[i];
  358. }
  359. // steps per sq second need to be updated to agree with the units per sq second
  360. reset_acceleration_rates();
  361. acceleration=DEFAULT_ACCELERATION;
  362. retract_acceleration=DEFAULT_RETRACT_ACCELERATION;
  363. minimumfeedrate=DEFAULT_MINIMUMFEEDRATE;
  364. minsegmenttime=DEFAULT_MINSEGMENTTIME;
  365. mintravelfeedrate=DEFAULT_MINTRAVELFEEDRATE;
  366. max_jerk[X_AXIS] = DEFAULT_XJERK;
  367. max_jerk[Y_AXIS] = DEFAULT_YJERK;
  368. max_jerk[Z_AXIS] = DEFAULT_ZJERK;
  369. max_jerk[E_AXIS] = DEFAULT_EJERK;
  370. add_homing[X_AXIS] = add_homing[Y_AXIS] = add_homing[Z_AXIS] = 0;
  371. #ifdef ENABLE_AUTO_BED_LEVELING
  372. zprobe_zoffset = -Z_PROBE_OFFSET_FROM_EXTRUDER;
  373. #endif
  374. #ifdef DOGLCD
  375. lcd_contrast = DEFAULT_LCD_CONTRAST;
  376. #endif
  377. #ifdef PIDTEMP
  378. Kp = DEFAULT_Kp;
  379. Ki = scalePID_i(DEFAULT_Ki);
  380. Kd = scalePID_d(DEFAULT_Kd);
  381. // call updatePID (similar to when we have processed M301)
  382. updatePID();
  383. #ifdef PID_ADD_EXTRUSION_RATE
  384. Kc = DEFAULT_Kc;
  385. #endif//PID_ADD_EXTRUSION_RATE
  386. #endif//PIDTEMP
  387. #ifdef FWRETRACT
  388. autoretract_enabled = false;
  389. retract_length = RETRACT_LENGTH;
  390. #if EXTRUDERS > 1
  391. retract_length_swap = RETRACT_LENGTH_SWAP;
  392. #endif
  393. retract_feedrate = RETRACT_FEEDRATE;
  394. retract_zlift = RETRACT_ZLIFT;
  395. retract_recover_length = RETRACT_RECOVER_LENGTH;
  396. #if EXTRUDERS > 1
  397. retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  398. #endif
  399. retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  400. #endif
  401. volumetric_enabled = false;
  402. filament_size[0] = DEFAULT_NOMINAL_FILAMENT_DIA;
  403. #if EXTRUDERS > 1
  404. filament_size[1] = DEFAULT_NOMINAL_FILAMENT_DIA;
  405. #if EXTRUDERS > 2
  406. filament_size[2] = DEFAULT_NOMINAL_FILAMENT_DIA;
  407. #endif
  408. #endif
  409. calculate_volumetric_multipliers();
  410. SERIAL_ECHO_START;
  411. SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded");
  412. }