ConfigurationStore.cpp 13 KB

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