My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

joystick.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * joystick.cpp - joystick input / jogging
  24. */
  25. #include "../inc/MarlinConfigPre.h"
  26. #if ENABLED(JOYSTICK)
  27. #include "joystick.h"
  28. #include "../inc/MarlinConfig.h" // for pins
  29. #include "../module/planner.h"
  30. #include "../module/temperature.h"
  31. Joystick joystick;
  32. #if ENABLED(EXTENSIBLE_UI)
  33. #include "../lcd/extensible_ui/ui_api.h"
  34. #endif
  35. #if HAS_JOY_ADC_X
  36. temp_info_t Joystick::x; // = { 0 }
  37. #if ENABLED(INVERT_JOY_X)
  38. #define JOY_X(N) (16383 - (N))
  39. #else
  40. #define JOY_X(N) (N)
  41. #endif
  42. #endif
  43. #if HAS_JOY_ADC_Y
  44. temp_info_t Joystick::y; // = { 0 }
  45. #if ENABLED(INVERT_JOY_Y)
  46. #define JOY_Y(N) (16383 - (N))
  47. #else
  48. #define JOY_Y(N) (N)
  49. #endif
  50. #endif
  51. #if HAS_JOY_ADC_Z
  52. temp_info_t Joystick::z; // = { 0 }
  53. #if ENABLED(INVERT_JOY_Z)
  54. #define JOY_Z(N) (16383 - (N))
  55. #else
  56. #define JOY_Z(N) (N)
  57. #endif
  58. #endif
  59. #if ENABLED(JOYSTICK_DEBUG)
  60. void Joystick::report() {
  61. SERIAL_ECHOPGM("Joystick");
  62. #if HAS_JOY_ADC_X
  63. SERIAL_ECHOPAIR_P(SP_X_STR, JOY_X(x.raw));
  64. #endif
  65. #if HAS_JOY_ADC_Y
  66. SERIAL_ECHOPAIR_P(SP_Y_STR, JOY_Y(y.raw));
  67. #endif
  68. #if HAS_JOY_ADC_Z
  69. SERIAL_ECHOPAIR_P(SP_Z_STR, JOY_Z(z.raw));
  70. #endif
  71. #if HAS_JOY_ADC_EN
  72. SERIAL_ECHO_TERNARY(READ(JOY_EN_PIN), " EN=", "HIGH (dis", "LOW (en", "abled)");
  73. #endif
  74. SERIAL_EOL();
  75. }
  76. #endif
  77. #if HAS_JOY_ADC_X || HAS_JOY_ADC_Y || HAS_JOY_ADC_Z
  78. void Joystick::calculate(xyz_float_t &norm_jog) {
  79. // Do nothing if enable pin (active-low) is not LOW
  80. #if HAS_JOY_ADC_EN
  81. if (READ(JOY_EN_PIN)) return;
  82. #endif
  83. auto _normalize_joy = [](float &axis_jog, const int16_t raw, const int16_t (&joy_limits)[4]) {
  84. if (WITHIN(raw, joy_limits[0], joy_limits[3])) {
  85. // within limits, check deadzone
  86. if (raw > joy_limits[2])
  87. axis_jog = (raw - joy_limits[2]) / float(joy_limits[3] - joy_limits[2]);
  88. else if (raw < joy_limits[1])
  89. axis_jog = (raw - joy_limits[1]) / float(joy_limits[1] - joy_limits[0]); // negative value
  90. // Map normal to jog value via quadratic relationship
  91. axis_jog = SIGN(axis_jog) * sq(axis_jog);
  92. }
  93. };
  94. #if HAS_JOY_ADC_X
  95. static constexpr int16_t joy_x_limits[4] = JOY_X_LIMITS;
  96. _normalize_joy(norm_jog.x, JOY_X(x.raw), joy_x_limits);
  97. #endif
  98. #if HAS_JOY_ADC_Y
  99. static constexpr int16_t joy_y_limits[4] = JOY_Y_LIMITS;
  100. _normalize_joy(norm_jog.y, JOY_Y(y.raw), joy_y_limits);
  101. #endif
  102. #if HAS_JOY_ADC_Z
  103. static constexpr int16_t joy_z_limits[4] = JOY_Z_LIMITS;
  104. _normalize_joy(norm_jog.z, JOY_Z(z.raw), joy_z_limits);
  105. #endif
  106. }
  107. #endif
  108. #if ENABLED(POLL_JOG)
  109. void Joystick::inject_jog_moves() {
  110. // Recursion barrier
  111. static bool injecting_now; // = false;
  112. if (injecting_now) return;
  113. static constexpr int QUEUE_DEPTH = 5; // Insert up to this many movements
  114. static constexpr float target_lag = 0.25f, // Aim for 1/4 second lag
  115. seg_time = target_lag / QUEUE_DEPTH; // 0.05 seconds, short segments inserted every 1/20th of a second
  116. static constexpr millis_t timer_limit_ms = millis_t(seg_time * 500); // 25 ms minimum delay between insertions
  117. // The planner can merge/collapse small moves, so the movement queue is unreliable to control the lag
  118. static millis_t next_run = 0;
  119. if (PENDING(millis(), next_run)) return;
  120. next_run = millis() + timer_limit_ms;
  121. // Only inject a command if the planner has fewer than 5 moves and there are no unparsed commands
  122. if (planner.movesplanned() >= QUEUE_DEPTH || queue.has_commands_queued())
  123. return;
  124. // Normalized jog values are 0 for no movement and -1 or +1 for as max feedrate (nonlinear relationship)
  125. // Jog are initialized to zero and handling input can update values but doesn't have to
  126. // You could use a two-axis joystick and a one-axis keypad and they might work together
  127. xyz_float_t norm_jog{0};
  128. // Use ADC values and defined limits. The active zone is normalized: -1..0 (dead) 0..1
  129. #if HAS_JOY_ADC_X || HAS_JOY_ADC_Y || HAS_JOY_ADC_Z
  130. joystick.calculate(norm_jog);
  131. #endif
  132. // Other non-joystick poll-based jogging could be implemented here
  133. // with "jogging" encapsulated as a more general class.
  134. #if ENABLED(EXTENSIBLE_UI)
  135. ExtUI::_joystick_update(norm_jog);
  136. #endif
  137. // norm_jog values of [-1 .. 1] maps linearly to [-feedrate .. feedrate]
  138. xyz_float_t move_dist{0};
  139. float hypot2 = 0;
  140. LOOP_XYZ(i) if (norm_jog[i]) {
  141. move_dist[i] = seg_time * norm_jog[i] *
  142. #if ENABLED(EXTENSIBLE_UI)
  143. manual_feedrate_mm_s[i];
  144. #else
  145. planner.settings.max_feedrate_mm_s[i];
  146. #endif
  147. hypot2 += sq(move_dist[i]);
  148. }
  149. if (!UNEAR_ZERO(hypot2)) {
  150. current_position += move_dist;
  151. apply_motion_limits(current_position);
  152. const float length = sqrt(hypot2);
  153. injecting_now = true;
  154. planner.buffer_line(current_position, length / seg_time, active_extruder, length);
  155. injecting_now = false;
  156. }
  157. }
  158. #endif // POLL_JOG
  159. #endif // JOYSTICK