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.

vector_3.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * 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. const xyz_float_t &lv = left, &rv = right;
  49. return vector_3(lv.y * rv.z - lv.z * rv.y, // YZ cross
  50. lv.z * rv.x - lv.x * rv.z, // ZX cross
  51. lv.x * rv.y - lv.y * rv.x); // XY cross
  52. }
  53. vector_3 vector_3::get_normal() const {
  54. vector_3 normalized = *this;
  55. normalized.normalize();
  56. return normalized;
  57. }
  58. void vector_3::normalize() {
  59. *this *= RSQRT(sq(x) + sq(y) + sq(z));
  60. }
  61. // Apply a rotation to the matrix
  62. void vector_3::apply_rotation(const matrix_3x3 &matrix) {
  63. const float _x = x, _y = y, _z = z;
  64. *this = { matrix.vectors[0][0] * _x + matrix.vectors[1][0] * _y + matrix.vectors[2][0] * _z,
  65. matrix.vectors[0][1] * _x + matrix.vectors[1][1] * _y + matrix.vectors[2][1] * _z,
  66. matrix.vectors[0][2] * _x + matrix.vectors[1][2] * _y + matrix.vectors[2][2] * _z };
  67. }
  68. extern const char SP_X_STR[], SP_Y_STR[], SP_Z_STR[];
  69. void vector_3::debug(PGM_P const title) {
  70. serialprintPGM(title);
  71. SERIAL_ECHOPAIR_F_P(SP_X_STR, x, 6);
  72. SERIAL_ECHOPAIR_F_P(SP_Y_STR, y, 6);
  73. SERIAL_ECHOLNPAIR_F_P(SP_Z_STR, z, 6);
  74. }
  75. /**
  76. * matrix_3x3
  77. */
  78. void apply_rotation_xyz(const matrix_3x3 &matrix, float &_x, float &_y, float &_z) {
  79. vector_3 vec = vector_3(_x, _y, _z); vec.apply_rotation(matrix);
  80. _x = vec.x; _y = vec.y; _z = vec.z;
  81. }
  82. // Reset to identity. No rotate or translate.
  83. void matrix_3x3::set_to_identity() {
  84. LOOP_L_N(i, 3)
  85. LOOP_L_N(j, 3)
  86. vectors[i][j] = float(i == j);
  87. }
  88. // Create a matrix from 3 vector_3 inputs
  89. matrix_3x3 matrix_3x3::create_from_rows(const vector_3 &row_0, const vector_3 &row_1, const vector_3 &row_2) {
  90. //row_0.debug(PSTR("row_0"));
  91. //row_1.debug(PSTR("row_1"));
  92. //row_2.debug(PSTR("row_2"));
  93. matrix_3x3 new_matrix;
  94. new_matrix.vectors[0] = row_0;
  95. new_matrix.vectors[1] = row_1;
  96. new_matrix.vectors[2] = row_2;
  97. //new_matrix.debug(PSTR("new_matrix"));
  98. return new_matrix;
  99. }
  100. // Create a matrix rotated to point towards a target
  101. matrix_3x3 matrix_3x3::create_look_at(const vector_3 &target) {
  102. const vector_3 z_row = target.get_normal(),
  103. x_row = vector_3(1, 0, -target.x / target.z).get_normal(),
  104. y_row = vector_3::cross(z_row, x_row).get_normal();
  105. // x_row.debug(PSTR("x_row"));
  106. // y_row.debug(PSTR("y_row"));
  107. // z_row.debug(PSTR("z_row"));
  108. // create the matrix already correctly transposed
  109. matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
  110. // rot.debug(PSTR("rot"));
  111. return rot;
  112. }
  113. // Get a transposed copy of the matrix
  114. matrix_3x3 matrix_3x3::transpose(const matrix_3x3 &original) {
  115. matrix_3x3 new_matrix;
  116. LOOP_L_N(i, 3)
  117. LOOP_L_N(j, 3)
  118. new_matrix.vectors[i][j] = original.vectors[j][i];
  119. return new_matrix;
  120. }
  121. void matrix_3x3::debug(PGM_P const title) {
  122. if (title != nullptr) {
  123. serialprintPGM(title);
  124. SERIAL_EOL();
  125. }
  126. LOOP_L_N(i, 3) {
  127. LOOP_L_N(j, 3) {
  128. if (vectors[i][j] >= 0.0) SERIAL_CHAR('+');
  129. SERIAL_ECHO_F(vectors[i][j], 6);
  130. SERIAL_CHAR(' ');
  131. }
  132. SERIAL_EOL();
  133. }
  134. }
  135. #endif // HAS_ABL_OR_UBL