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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. vector_3::vector_3() : x(0), y(0), z(0) { }
  45. vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
  46. vector_3 vector_3::cross(vector_3 left, vector_3 right) {
  47. return vector_3(left.y * right.z - left.z * right.y,
  48. left.z * right.x - left.x * right.z,
  49. left.x * right.y - left.y * right.x);
  50. }
  51. vector_3 vector_3::operator+(vector_3 v) { return vector_3((x + v.x), (y + v.y), (z + v.z)); }
  52. vector_3 vector_3::operator-(vector_3 v) { return vector_3((x - v.x), (y - v.y), (z - v.z)); }
  53. vector_3 vector_3::get_normal() {
  54. vector_3 normalized = vector_3(x, y, z);
  55. normalized.normalize();
  56. return normalized;
  57. }
  58. float vector_3::get_length() { return SQRT(sq(x) + sq(y) + sq(z)); }
  59. void vector_3::normalize() {
  60. const float inv_length = 1.0 / get_length();
  61. x *= inv_length;
  62. y *= inv_length;
  63. z *= inv_length;
  64. }
  65. void vector_3::apply_rotation(matrix_3x3 matrix) {
  66. const float resultX = x * matrix.matrix[3 * 0 + 0] + y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0],
  67. resultY = x * matrix.matrix[3 * 0 + 1] + y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1],
  68. resultZ = x * matrix.matrix[3 * 0 + 2] + y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
  69. x = resultX;
  70. y = resultY;
  71. z = resultZ;
  72. }
  73. void vector_3::debug(const char * const title) {
  74. serialprintPGM(title);
  75. SERIAL_PROTOCOLPGM(" x: ");
  76. SERIAL_PROTOCOL_F(x, 6);
  77. SERIAL_PROTOCOLPGM(" y: ");
  78. SERIAL_PROTOCOL_F(y, 6);
  79. SERIAL_PROTOCOLPGM(" z: ");
  80. SERIAL_PROTOCOL_F(z, 6);
  81. SERIAL_EOL();
  82. }
  83. void apply_rotation_xyz(matrix_3x3 matrix, float &x, float &y, float &z) {
  84. vector_3 vector = vector_3(x, y, z);
  85. vector.apply_rotation(matrix);
  86. x = vector.x;
  87. y = vector.y;
  88. z = vector.z;
  89. }
  90. matrix_3x3 matrix_3x3::create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2) {
  91. //row_0.debug(PSTR("row_0"));
  92. //row_1.debug(PSTR("row_1"));
  93. //row_2.debug(PSTR("row_2"));
  94. matrix_3x3 new_matrix;
  95. new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
  96. new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
  97. new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
  98. //new_matrix.debug(PSTR("new_matrix"));
  99. return new_matrix;
  100. }
  101. void matrix_3x3::set_to_identity() {
  102. matrix[0] = 1; matrix[1] = 0; matrix[2] = 0;
  103. matrix[3] = 0; matrix[4] = 1; matrix[5] = 0;
  104. matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
  105. }
  106. matrix_3x3 matrix_3x3::create_look_at(vector_3 target) {
  107. vector_3 z_row = target.get_normal();
  108. vector_3 x_row = vector_3(1, 0, -target.x / target.z).get_normal();
  109. vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
  110. // x_row.debug(PSTR("x_row"));
  111. // y_row.debug(PSTR("y_row"));
  112. // z_row.debug(PSTR("z_row"));
  113. // create the matrix already correctly transposed
  114. matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
  115. // rot.debug(PSTR("rot"));
  116. return rot;
  117. }
  118. matrix_3x3 matrix_3x3::transpose(matrix_3x3 original) {
  119. matrix_3x3 new_matrix;
  120. new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
  121. new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
  122. new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
  123. return new_matrix;
  124. }
  125. void matrix_3x3::debug(const char * const title) {
  126. if (title != NULL) {
  127. serialprintPGM(title);
  128. SERIAL_EOL();
  129. }
  130. uint8_t count = 0;
  131. for (uint8_t i = 0; i < 3; i++) {
  132. for (uint8_t j = 0; j < 3; j++) {
  133. if (matrix[count] >= 0.0) SERIAL_PROTOCOLCHAR('+');
  134. SERIAL_PROTOCOL_F(matrix[count], 6);
  135. SERIAL_PROTOCOLCHAR(' ');
  136. count++;
  137. }
  138. SERIAL_EOL();
  139. }
  140. }
  141. #endif // HAS_ABL