Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

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