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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * vector_3.cpp - Vector library for bed leveling
  24. * Copyright (c) 2012 Lars Brubaker. All right reserved.
  25. *
  26. * This library is free software; you can redistribute it and/or
  27. * modify it under the terms of the GNU Lesser General Public
  28. * License as published by the Free Software Foundation; either
  29. * version 2.1 of the License, or (at your option) any later version.
  30. *
  31. * This library is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. * Lesser General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Lesser General Public
  37. * License along with this library; if not, write to the Free Software
  38. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  39. */
  40. #include "../inc/MarlinConfig.h"
  41. #if ABL_PLANAR || ENABLED(AUTO_BED_LEVELING_UBL)
  42. #include "vector_3.h"
  43. #include <math.h>
  44. /**
  45. * vector_3
  46. */
  47. vector_3 vector_3::cross(const vector_3 &left, const vector_3 &right) {
  48. return vector_3(left.y * right.z - left.z * right.y, // YZ cross
  49. left.z * right.x - left.x * right.z, // ZX cross
  50. left.x * right.y - left.y * right.x); // XY cross
  51. }
  52. vector_3 vector_3::get_normal() const {
  53. vector_3 normalized = *this;
  54. normalized.normalize();
  55. return normalized;
  56. }
  57. float vector_3::magnitude() const { return SQRT(sq(x) + sq(y) + sq(z)); }
  58. void vector_3::normalize() { *this *= RSQRT(sq(x) + sq(y) + sq(z)); }
  59. // Apply a rotation to the matrix
  60. void vector_3::apply_rotation(const matrix_3x3 &matrix) {
  61. const float _x = x, _y = y, _z = z;
  62. *this = { matrix.vectors[0].x * _x + matrix.vectors[1].x * _y + matrix.vectors[2].x * _z,
  63. matrix.vectors[0].y * _x + matrix.vectors[1].y * _y + matrix.vectors[2].y * _z,
  64. matrix.vectors[0].z * _x + matrix.vectors[1].z * _y + matrix.vectors[2].z * _z };
  65. }
  66. void vector_3::debug(FSTR_P const title) {
  67. SERIAL_ECHOF(title);
  68. SERIAL_ECHOPAIR_F_P(SP_X_STR, x, 6);
  69. SERIAL_ECHOPAIR_F_P(SP_Y_STR, y, 6);
  70. SERIAL_ECHOLNPAIR_F_P(SP_Z_STR, z, 6);
  71. }
  72. /**
  73. * matrix_3x3
  74. */
  75. void matrix_3x3::apply_rotation_xyz(float &_x, float &_y, float &_z) {
  76. vector_3 vec = vector_3(_x, _y, _z); vec.apply_rotation(*this);
  77. _x = vec.x; _y = vec.y; _z = vec.z;
  78. }
  79. // Reset to identity. No rotate or translate.
  80. void matrix_3x3::set_to_identity() {
  81. LOOP_L_N(i, 3)
  82. LOOP_L_N(j, 3)
  83. vectors[i][j] = float(i == j);
  84. }
  85. // Create a matrix from 3 vector_3 inputs
  86. matrix_3x3 matrix_3x3::create_from_rows(const vector_3 &row_0, const vector_3 &row_1, const vector_3 &row_2) {
  87. //row_0.debug(F("row_0"));
  88. //row_1.debug(F("row_1"));
  89. //row_2.debug(F("row_2"));
  90. matrix_3x3 new_matrix;
  91. new_matrix.vectors[0] = row_0;
  92. new_matrix.vectors[1] = row_1;
  93. new_matrix.vectors[2] = row_2;
  94. //new_matrix.debug(F("new_matrix"));
  95. return new_matrix;
  96. }
  97. // Create a matrix rotated to point towards a target
  98. matrix_3x3 matrix_3x3::create_look_at(const vector_3 &target) {
  99. const vector_3 z_row = target.get_normal(),
  100. x_row = vector_3(1, 0, -target.x / target.z).get_normal(),
  101. y_row = vector_3::cross(z_row, x_row).get_normal();
  102. // x_row.debug(F("x_row"));
  103. // y_row.debug(F("y_row"));
  104. // z_row.debug(F("z_row"));
  105. // create the matrix already correctly transposed
  106. matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
  107. // rot.debug(F("rot"));
  108. return rot;
  109. }
  110. // Get a transposed copy of the matrix
  111. matrix_3x3 matrix_3x3::transpose(const matrix_3x3 &original) {
  112. matrix_3x3 new_matrix;
  113. LOOP_L_N(i, 3)
  114. LOOP_L_N(j, 3)
  115. new_matrix.vectors[i][j] = original.vectors[j][i];
  116. return new_matrix;
  117. }
  118. void matrix_3x3::debug(FSTR_P const title) {
  119. if (title) SERIAL_ECHOLNF(title);
  120. LOOP_L_N(i, 3) {
  121. LOOP_L_N(j, 3) {
  122. serial_offset(vectors[i][j], 2);
  123. SERIAL_CHAR(' ');
  124. }
  125. SERIAL_EOL();
  126. }
  127. }
  128. #endif // HAS_ABL_OR_UBL