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 6.1KB

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