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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. This library is free software; you can redistribute it and/or
  26. modify it under the terms of the GNU Lesser General Public
  27. License as published by the Free Software Foundation; either
  28. version 2.1 of the License, or (at your option) any later version.
  29. This library is distributed in the hope that it will be useful,
  30. but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. Lesser General Public License for more details.
  33. You should have received a copy of the GNU Lesser General Public
  34. License along with this library; if not, write to the Free Software
  35. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  36. */
  37. #include <math.h>
  38. #include "Marlin.h"
  39. #if HAS_ABL
  40. #include "vector_3.h"
  41. vector_3::vector_3() : x(0), y(0), z(0) { }
  42. vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
  43. vector_3 vector_3::cross(vector_3 left, vector_3 right) {
  44. return vector_3(left.y * right.z - left.z * right.y,
  45. left.z * right.x - left.x * right.z,
  46. left.x * right.y - left.y * right.x);
  47. }
  48. vector_3 vector_3::operator+(vector_3 v) { return vector_3((x + v.x), (y + v.y), (z + v.z)); }
  49. vector_3 vector_3::operator-(vector_3 v) { return vector_3((x - v.x), (y - v.y), (z - v.z)); }
  50. vector_3 vector_3::get_normal() {
  51. vector_3 normalized = vector_3(x, y, z);
  52. normalized.normalize();
  53. return normalized;
  54. }
  55. float vector_3::get_length() { return sqrt(sq(x) + sq(y) + sq(z)); }
  56. void vector_3::normalize() {
  57. const float inv_length = 1.0 / get_length();
  58. x *= inv_length;
  59. y *= inv_length;
  60. z *= inv_length;
  61. }
  62. void vector_3::apply_rotation(matrix_3x3 matrix) {
  63. const float resultX = x * matrix.matrix[3 * 0 + 0] + y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0],
  64. resultY = x * matrix.matrix[3 * 0 + 1] + y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1],
  65. resultZ = x * matrix.matrix[3 * 0 + 2] + y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
  66. x = resultX;
  67. y = resultY;
  68. z = resultZ;
  69. }
  70. void vector_3::debug(const char * const title) {
  71. serialprintPGM(title);
  72. SERIAL_PROTOCOLPGM(" x: ");
  73. SERIAL_PROTOCOL_F(x, 6);
  74. SERIAL_PROTOCOLPGM(" y: ");
  75. SERIAL_PROTOCOL_F(y, 6);
  76. SERIAL_PROTOCOLPGM(" z: ");
  77. SERIAL_PROTOCOL_F(z, 6);
  78. SERIAL_EOL();
  79. }
  80. void apply_rotation_xyz(matrix_3x3 matrix, float &x, float &y, float &z) {
  81. vector_3 vector = vector_3(x, y, z);
  82. vector.apply_rotation(matrix);
  83. x = vector.x;
  84. y = vector.y;
  85. z = vector.z;
  86. }
  87. matrix_3x3 matrix_3x3::create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2) {
  88. //row_0.debug(PSTR("row_0"));
  89. //row_1.debug(PSTR("row_1"));
  90. //row_2.debug(PSTR("row_2"));
  91. matrix_3x3 new_matrix;
  92. new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
  93. new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
  94. new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
  95. //new_matrix.debug(PSTR("new_matrix"));
  96. return new_matrix;
  97. }
  98. void matrix_3x3::set_to_identity() {
  99. matrix[0] = 1; matrix[1] = 0; matrix[2] = 0;
  100. matrix[3] = 0; matrix[4] = 1; matrix[5] = 0;
  101. matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
  102. }
  103. matrix_3x3 matrix_3x3::create_look_at(vector_3 target) {
  104. vector_3 z_row = target.get_normal();
  105. vector_3 x_row = vector_3(1, 0, -target.x / target.z).get_normal();
  106. vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
  107. // x_row.debug(PSTR("x_row"));
  108. // y_row.debug(PSTR("y_row"));
  109. // z_row.debug(PSTR("z_row"));
  110. // create the matrix already correctly transposed
  111. matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
  112. // rot.debug(PSTR("rot"));
  113. return rot;
  114. }
  115. matrix_3x3 matrix_3x3::transpose(matrix_3x3 original) {
  116. matrix_3x3 new_matrix;
  117. new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
  118. new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
  119. new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
  120. return new_matrix;
  121. }
  122. void matrix_3x3::debug(const char * const title) {
  123. serialprintPGM(title);
  124. uint8_t count = 0;
  125. for (uint8_t i = 0; i < 3; i++) {
  126. for (uint8_t j = 0; j < 3; j++) {
  127. if (matrix[count] >= 0.0) SERIAL_PROTOCOLCHAR('+');
  128. SERIAL_PROTOCOL_F(matrix[count], 6);
  129. SERIAL_PROTOCOLCHAR(' ');
  130. count++;
  131. }
  132. SERIAL_EOL();
  133. }
  134. }
  135. #endif // HAS_ABL