My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

vector_3.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. vector_3.cpp - Vector library for bed leveling
  3. Copyright (c) 2012 Lars Brubaker. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include <math.h>
  17. #include "Marlin.h"
  18. #if ENABLED(ENABLE_AUTO_BED_LEVELING)
  19. #include "vector_3.h"
  20. vector_3::vector_3() : x(0), y(0), z(0) { }
  21. vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
  22. vector_3 vector_3::cross(vector_3 left, vector_3 right) {
  23. return vector_3(left.y * right.z - left.z * right.y,
  24. left.z * right.x - left.x * right.z,
  25. left.x * right.y - left.y * right.x);
  26. }
  27. vector_3 vector_3::operator+(vector_3 v) { return vector_3((x + v.x), (y + v.y), (z + v.z)); }
  28. vector_3 vector_3::operator-(vector_3 v) { return vector_3((x - v.x), (y - v.y), (z - v.z)); }
  29. vector_3 vector_3::get_normal() {
  30. vector_3 normalized = vector_3(x, y, z);
  31. normalized.normalize();
  32. return normalized;
  33. }
  34. float vector_3::get_length() { return sqrt((x * x) + (y * y) + (z * z)); }
  35. void vector_3::normalize() {
  36. float length = get_length();
  37. x /= length;
  38. y /= length;
  39. z /= length;
  40. }
  41. void vector_3::apply_rotation(matrix_3x3 matrix) {
  42. float resultX = x * matrix.matrix[3*0+0] + y * matrix.matrix[3*1+0] + z * matrix.matrix[3*2+0];
  43. float resultY = x * matrix.matrix[3*0+1] + y * matrix.matrix[3*1+1] + z * matrix.matrix[3*2+1];
  44. float resultZ = x * matrix.matrix[3*0+2] + y * matrix.matrix[3*1+2] + z * matrix.matrix[3*2+2];
  45. x = resultX;
  46. y = resultY;
  47. z = resultZ;
  48. }
  49. void vector_3::debug(const char title[]) {
  50. SERIAL_PROTOCOL(title);
  51. SERIAL_PROTOCOLPGM(" x: ");
  52. SERIAL_PROTOCOL_F(x, 6);
  53. SERIAL_PROTOCOLPGM(" y: ");
  54. SERIAL_PROTOCOL_F(y, 6);
  55. SERIAL_PROTOCOLPGM(" z: ");
  56. SERIAL_PROTOCOL_F(z, 6);
  57. SERIAL_EOL;
  58. }
  59. void apply_rotation_xyz(matrix_3x3 matrix, float &x, float& y, float& z) {
  60. vector_3 vector = vector_3(x, y, z);
  61. vector.apply_rotation(matrix);
  62. x = vector.x;
  63. y = vector.y;
  64. z = vector.z;
  65. }
  66. matrix_3x3 matrix_3x3::create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2) {
  67. //row_0.debug("row_0");
  68. //row_1.debug("row_1");
  69. //row_2.debug("row_2");
  70. matrix_3x3 new_matrix;
  71. new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
  72. new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
  73. new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
  74. //new_matrix.debug("new_matrix");
  75. return new_matrix;
  76. }
  77. void matrix_3x3::set_to_identity() {
  78. matrix[0] = 1; matrix[1] = 0; matrix[2] = 0;
  79. matrix[3] = 0; matrix[4] = 1; matrix[5] = 0;
  80. matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
  81. }
  82. matrix_3x3 matrix_3x3::create_look_at(vector_3 target) {
  83. vector_3 z_row = target.get_normal();
  84. vector_3 x_row = vector_3(1, 0, -target.x/target.z).get_normal();
  85. vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
  86. // x_row.debug("x_row");
  87. // y_row.debug("y_row");
  88. // z_row.debug("z_row");
  89. // create the matrix already correctly transposed
  90. matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
  91. // rot.debug("rot");
  92. return rot;
  93. }
  94. matrix_3x3 matrix_3x3::transpose(matrix_3x3 original) {
  95. matrix_3x3 new_matrix;
  96. new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
  97. new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
  98. new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
  99. return new_matrix;
  100. }
  101. void matrix_3x3::debug(const char title[]) {
  102. SERIAL_PROTOCOLLN(title);
  103. int count = 0;
  104. for(int i=0; i<3; i++) {
  105. for(int j=0; j<3; j++) {
  106. if (matrix[count] >= 0.0) SERIAL_PROTOCOLCHAR('+');
  107. SERIAL_PROTOCOL_F(matrix[count], 6);
  108. SERIAL_PROTOCOLCHAR(' ');
  109. count++;
  110. }
  111. SERIAL_EOL;
  112. }
  113. }
  114. #endif // ENABLE_AUTO_BED_LEVELING