la10compat.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "la10compat.h"
  2. #include "Marlin.h"
  3. #include <float.h>
  4. static LA10C_MODE la10c_mode = LA10C_UNKNOWN; // Current LA compatibility mode
  5. static float la10c_orig_jerk = 0; // Unadjusted/saved e-jerk
  6. LA10C_MODE la10c_mode_get()
  7. {
  8. return la10c_mode;
  9. }
  10. void la10c_mode_change(LA10C_MODE mode)
  11. {
  12. if(mode == la10c_mode) return;
  13. // always restore to the last unadjusted E-jerk value
  14. if(la10c_orig_jerk)
  15. cs.max_jerk[E_AXIS] = la10c_orig_jerk;
  16. SERIAL_ECHOPGM("LA10C: Linear Advance mode: ");
  17. switch(mode)
  18. {
  19. case LA10C_UNKNOWN: SERIAL_ECHOLNPGM("UNKNOWN"); break;
  20. case LA10C_LA15: SERIAL_ECHOLNPGM("1.5"); break;
  21. case LA10C_LA10: SERIAL_ECHOLNPGM("1.0"); break;
  22. }
  23. la10c_mode = mode;
  24. // adjust the E-jerk if needed
  25. cs.max_jerk[E_AXIS] = la10c_jerk(cs.max_jerk[E_AXIS]);
  26. }
  27. // Approximate a LA10 value to a LA15 equivalent.
  28. static float la10c_convert(float k)
  29. {
  30. float new_K = k * 0.004 - 0.05;
  31. return new_K < 0? 0:
  32. new_K > (LA_K_MAX - FLT_EPSILON)? (LA_K_MAX - FLT_EPSILON):
  33. new_K;
  34. }
  35. float la10c_value(float k)
  36. {
  37. if(la10c_mode == LA10C_UNKNOWN)
  38. {
  39. // do not autodetect until a valid value is seen
  40. if(k == 0)
  41. return 0;
  42. else if(k < 0)
  43. return -1;
  44. la10c_mode_change(k < LA_LA10_MIN? LA10C_LA15: LA10C_LA10);
  45. }
  46. if(la10c_mode == LA10C_LA15)
  47. return (k >= 0 && k < LA_K_MAX? k: -1);
  48. else
  49. return (k >= 0? la10c_convert(k): -1);
  50. }
  51. float la10c_jerk(float j)
  52. {
  53. la10c_orig_jerk = j;
  54. if(la10c_mode != LA10C_LA10)
  55. return j;
  56. // check for a compatible range of values prior to convert (be sure that
  57. // a higher E-jerk would still be compatible wrt the E accell range)
  58. if(j < 4.5 && cs.max_acceleration_units_per_sq_second_normal[E_AXIS] < 2000)
  59. return j;
  60. // bring low E-jerk values into equivalent LA 1.5 values by
  61. // flattening the response in the (0.3-4.5) range using a piecewise
  62. // function. Is it truly worth to preserve the difference between
  63. // 1.5/2.5 E-jerk for LA1.0? Probably not, but we try nonetheless.
  64. j = j < 0.3? j * 11.5:
  65. j < 4.5? j * 0.25 + 3.375:
  66. j;
  67. SERIAL_ECHOPGM("LA10C: Adjusted E-Jerk: ");
  68. SERIAL_ECHOLN(j);
  69. return j;
  70. }