ConfigurationStore.cpp 14 KB

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