M114.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #include "../gcode.h"
  24. #include "../../module/motion.h"
  25. #include "../../module/stepper.h"
  26. #if ENABLED(M114_DETAIL)
  27. void report_all_axis_pos(const xyze_pos_t &pos, const uint8_t n=LOGICAL_AXES, const uint8_t precision=3) {
  28. for (uint8_t a = 0; a < n; ++a) {
  29. SERIAL_ECHOPGM_P((PGM_P)pgm_read_ptr(&SP_AXIS_LBL[a]));
  30. if (pos[a] >= 0) SERIAL_CHAR(' ');
  31. SERIAL_ECHO(p_float_t(pos[a], precision));
  32. }
  33. SERIAL_EOL();
  34. }
  35. inline void report_linear_axis_pos(const xyze_pos_t &pos) { report_all_axis_pos(pos, XYZ); }
  36. void report_linear_axis_pos(const xyz_pos_t &pos, const uint8_t precision=3) {
  37. LOOP_NUM_AXES(a) SERIAL_ECHO(FPSTR(pgm_read_ptr(&SP_AXIS_LBL[a])), p_float_t(pos[a], precision));
  38. SERIAL_EOL();
  39. }
  40. void report_current_position_detail() {
  41. // Position as sent by G-code
  42. SERIAL_ECHOPGM("\nLogical:");
  43. report_linear_axis_pos(current_position.asLogical());
  44. // Cartesian position in native machine space
  45. SERIAL_ECHOPGM("Raw: ");
  46. report_linear_axis_pos(current_position);
  47. xyze_pos_t leveled = current_position;
  48. #if HAS_LEVELING
  49. // Current position with leveling applied
  50. SERIAL_ECHOPGM("Leveled:");
  51. planner.apply_leveling(leveled);
  52. report_linear_axis_pos(leveled);
  53. // Test planner un-leveling. This should match the Raw result.
  54. SERIAL_ECHOPGM("UnLevel:");
  55. xyze_pos_t unleveled = leveled;
  56. planner.unapply_leveling(unleveled);
  57. report_linear_axis_pos(unleveled);
  58. #endif
  59. #if IS_KINEMATIC
  60. // Kinematics applied to the leveled position
  61. SERIAL_ECHOPGM(TERN(POLAR, "Polar", TERN(IS_SCARA, "Scara", "Delta")) "K: " );
  62. inverse_kinematics(leveled); // writes delta[]
  63. report_linear_axis_pos(delta);
  64. #endif
  65. planner.synchronize();
  66. SERIAL_ECHOPGM("Stepper:");
  67. LOOP_LOGICAL_AXES(i) {
  68. SERIAL_ECHOPGM_P((PGM_P)pgm_read_ptr(&SP_AXIS_LBL[i]), stepper.position((AxisEnum)i));
  69. }
  70. SERIAL_EOL();
  71. #if IS_SCARA
  72. const xy_float_t deg = {
  73. planner.get_axis_position_degrees(A_AXIS),
  74. planner.get_axis_position_degrees(B_AXIS)
  75. };
  76. SERIAL_ECHOPGM("Degrees:");
  77. report_all_axis_pos(deg, 2);
  78. #endif
  79. SERIAL_ECHOPGM("FromStp:");
  80. get_cartesian_from_steppers(); // writes 'cartes' (with forward kinematics)
  81. xyze_pos_t from_steppers = LOGICAL_AXIS_ARRAY(
  82. planner.get_axis_position_mm(E_AXIS),
  83. cartes.x, cartes.y, cartes.z,
  84. planner.get_axis_position_mm(I_AXIS),
  85. planner.get_axis_position_mm(J_AXIS),
  86. planner.get_axis_position_mm(K_AXIS),
  87. planner.get_axis_position_mm(U_AXIS),
  88. planner.get_axis_position_mm(V_AXIS),
  89. planner.get_axis_position_mm(W_AXIS)
  90. );
  91. report_all_axis_pos(from_steppers);
  92. const xyze_float_t diff = from_steppers - leveled;
  93. SERIAL_ECHOPGM("Diff: ");
  94. report_all_axis_pos(diff);
  95. TERN_(FULL_REPORT_TO_HOST_FEATURE, report_current_grblstate_moving());
  96. }
  97. #endif // M114_DETAIL
  98. /**
  99. * M114: Report the current position to host.
  100. * Since steppers are moving, the count positions are
  101. * projected by using planner calculations.
  102. * D - Report more detail. This syncs the planner. (Requires M114_DETAIL)
  103. * E - Report E stepper position (Requires M114_DETAIL)
  104. * R - Report the realtime position instead of projected.
  105. */
  106. void GcodeSuite::M114() {
  107. #if ENABLED(M114_DETAIL)
  108. if (parser.seen_test('D')) {
  109. IF_DISABLED(M114_LEGACY, planner.synchronize());
  110. report_current_position();
  111. report_current_position_detail();
  112. return;
  113. }
  114. #if HAS_EXTRUDERS
  115. if (parser.seen_test('E')) {
  116. SERIAL_ECHOLNPGM("Count E:", stepper.position(E_AXIS));
  117. return;
  118. }
  119. #endif
  120. #endif
  121. TERN_(M114_REALTIME, if (parser.seen_test('R')) return report_real_position());
  122. TERN_(M114_LEGACY, planner.synchronize());
  123. report_current_position_projected();
  124. TERN_(FULL_REPORT_TO_HOST_FEATURE, report_current_grblstate_moving());
  125. }