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.5KB

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