Browse Source

Optimize delta kinematics

Co-Authored-By: ejtagle <ejtagle@hotmail.com>
Scott Lahteine 6 years ago
parent
commit
2992112da0
2 changed files with 21 additions and 21 deletions
  1. 17
    17
      Marlin/src/module/delta.cpp
  2. 4
    4
      Marlin/src/module/delta.h

+ 17
- 17
Marlin/src/module/delta.cpp View File

155
  *
155
  *
156
  * The result is stored in the cartes[] array.
156
  * The result is stored in the cartes[] array.
157
  */
157
  */
158
-void forward_kinematics_DELTA(float z1, float z2, float z3) {
158
+void forward_kinematics_DELTA(const float &z1, const float &z2, const float &z3) {
159
   // Create a vector in old coordinates along x axis of new coordinate
159
   // Create a vector in old coordinates along x axis of new coordinate
160
-  float p12[3] = { delta_tower[B_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[B_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z2 - z1 };
160
+  const float p12[3] = { delta_tower[B_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[B_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z2 - z1 };
161
 
161
 
162
-  // Get the Magnitude of vector.
163
-  float d = SQRT( sq(p12[0]) + sq(p12[1]) + sq(p12[2]) );
162
+  // Get the reciprocal of Magnitude of vector.
163
+  const float d2 = sq(p12[0]) + sq(p12[1]) + sq(p12[2]), inv_d = RSQRT(d2);
164
 
164
 
165
-  // Create unit vector by dividing by magnitude.
166
-  float ex[3] = { p12[0] / d, p12[1] / d, p12[2] / d };
165
+  // Create unit vector by multiplying by the inverse of the magnitude.
166
+  const float ex[3] = { p12[0] * inv_d, p12[1] * inv_d, p12[2] * inv_d };
167
 
167
 
168
   // Get the vector from the origin of the new system to the third point.
168
   // Get the vector from the origin of the new system to the third point.
169
-  float p13[3] = { delta_tower[C_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[C_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z3 - z1 };
169
+  const float p13[3] = { delta_tower[C_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[C_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z3 - z1 };
170
 
170
 
171
   // Use the dot product to find the component of this vector on the X axis.
171
   // Use the dot product to find the component of this vector on the X axis.
172
-  float i = ex[0] * p13[0] + ex[1] * p13[1] + ex[2] * p13[2];
172
+  const float i = ex[0] * p13[0] + ex[1] * p13[1] + ex[2] * p13[2];
173
 
173
 
174
   // Create a vector along the x axis that represents the x component of p13.
174
   // Create a vector along the x axis that represents the x component of p13.
175
-  float iex[3] = { ex[0] * i, ex[1] * i, ex[2] * i };
175
+  const float iex[3] = { ex[0] * i, ex[1] * i, ex[2] * i };
176
 
176
 
177
   // Subtract the X component from the original vector leaving only Y. We use the
177
   // Subtract the X component from the original vector leaving only Y. We use the
178
   // variable that will be the unit vector after we scale it.
178
   // variable that will be the unit vector after we scale it.
179
   float ey[3] = { p13[0] - iex[0], p13[1] - iex[1], p13[2] - iex[2] };
179
   float ey[3] = { p13[0] - iex[0], p13[1] - iex[1], p13[2] - iex[2] };
180
 
180
 
181
-  // The magnitude of Y component
182
-  float j = SQRT( sq(ey[0]) + sq(ey[1]) + sq(ey[2]) );
181
+  // The magnitude and the inverse of the magnitude of Y component
182
+  const float j2 = sq(ey[0]) + sq(ey[1]) + sq(ey[2]), inv_j = RSQRT(j2);
183
 
183
 
184
   // Convert to a unit vector
184
   // Convert to a unit vector
185
-  ey[0] /= j; ey[1] /= j;  ey[2] /= j;
185
+  ey[0] *= inv_j; ey[1] *= inv_j; ey[2] *= inv_j;
186
 
186
 
187
   // The cross product of the unit x and y is the unit z
187
   // The cross product of the unit x and y is the unit z
188
   // float[] ez = vectorCrossProd(ex, ey);
188
   // float[] ez = vectorCrossProd(ex, ey);
189
-  float ez[3] = {
189
+  const float ez[3] = {
190
     ex[1] * ey[2] - ex[2] * ey[1],
190
     ex[1] * ey[2] - ex[2] * ey[1],
191
     ex[2] * ey[0] - ex[0] * ey[2],
191
     ex[2] * ey[0] - ex[0] * ey[2],
192
     ex[0] * ey[1] - ex[1] * ey[0]
192
     ex[0] * ey[1] - ex[1] * ey[0]
194
 
194
 
195
   // We now have the d, i and j values defined in Wikipedia.
195
   // We now have the d, i and j values defined in Wikipedia.
196
   // Plug them into the equations defined in Wikipedia for Xnew, Ynew and Znew
196
   // Plug them into the equations defined in Wikipedia for Xnew, Ynew and Znew
197
-  float Xnew = (delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[B_AXIS] + sq(d)) / (d * 2),
198
-        Ynew = ((delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[C_AXIS] + HYPOT2(i, j)) / 2 - i * Xnew) / j,
199
-        Znew = SQRT(delta_diagonal_rod_2_tower[A_AXIS] - HYPOT2(Xnew, Ynew));
197
+  const float Xnew = (delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[B_AXIS] + d2) * inv_d * 0.5,
198
+              Ynew = ((delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[C_AXIS] + sq(i) + j2) * 0.5 - i * Xnew) * inv_j,
199
+              Znew = SQRT(delta_diagonal_rod_2_tower[A_AXIS] - HYPOT2(Xnew, Ynew));
200
 
200
 
201
   // Start from the origin of the old coordinates and add vectors in the
201
   // Start from the origin of the old coordinates and add vectors in the
202
   // old coords that represent the Xnew, Ynew and Znew to find the point
202
   // old coords that represent the Xnew, Ynew and Znew to find the point
203
   // in the old system.
203
   // in the old system.
204
   cartes[X_AXIS] = delta_tower[A_AXIS][X_AXIS] + ex[0] * Xnew + ey[0] * Ynew - ez[0] * Znew;
204
   cartes[X_AXIS] = delta_tower[A_AXIS][X_AXIS] + ex[0] * Xnew + ey[0] * Ynew - ez[0] * Znew;
205
   cartes[Y_AXIS] = delta_tower[A_AXIS][Y_AXIS] + ex[1] * Xnew + ey[1] * Ynew - ez[1] * Znew;
205
   cartes[Y_AXIS] = delta_tower[A_AXIS][Y_AXIS] + ex[1] * Xnew + ey[1] * Ynew - ez[1] * Znew;
206
-  cartes[Z_AXIS] =             z1 + ex[2] * Xnew + ey[2] * Ynew - ez[2] * Znew;
206
+  cartes[Z_AXIS] =                          z1 + ex[2] * Xnew + ey[2] * Ynew - ez[2] * Znew;
207
 }
207
 }
208
 
208
 
209
 #if ENABLED(SENSORLESS_HOMING)
209
 #if ENABLED(SENSORLESS_HOMING)

+ 4
- 4
Marlin/src/module/delta.h View File

65
  */
65
  */
66
 
66
 
67
 // Macro to obtain the Z position of an individual tower
67
 // Macro to obtain the Z position of an individual tower
68
-#define DELTA_Z(V,T) V[Z_AXIS] + SQRT(   \
68
+#define DELTA_Z(V,T) V[Z_AXIS] + SQRT(    \
69
   delta_diagonal_rod_2_tower[T] - HYPOT2( \
69
   delta_diagonal_rod_2_tower[T] - HYPOT2( \
70
       delta_tower[T][X_AXIS] - V[X_AXIS], \
70
       delta_tower[T][X_AXIS] - V[X_AXIS], \
71
       delta_tower[T][Y_AXIS] - V[Y_AXIS]  \
71
       delta_tower[T][Y_AXIS] - V[Y_AXIS]  \
72
     )                                     \
72
     )                                     \
73
   )
73
   )
74
 
74
 
75
-#define DELTA_IK(V) do {        \
75
+#define DELTA_IK(V) do {              \
76
   delta[A_AXIS] = DELTA_Z(V, A_AXIS); \
76
   delta[A_AXIS] = DELTA_Z(V, A_AXIS); \
77
   delta[B_AXIS] = DELTA_Z(V, B_AXIS); \
77
   delta[B_AXIS] = DELTA_Z(V, B_AXIS); \
78
   delta[C_AXIS] = DELTA_Z(V, C_AXIS); \
78
   delta[C_AXIS] = DELTA_Z(V, C_AXIS); \
111
  *
111
  *
112
  * The result is stored in the cartes[] array.
112
  * The result is stored in the cartes[] array.
113
  */
113
  */
114
-void forward_kinematics_DELTA(float z1, float z2, float z3);
114
+void forward_kinematics_DELTA(const float &z1, const float &z2, const float &z3);
115
 
115
 
116
-FORCE_INLINE void forward_kinematics_DELTA(float point[ABC]) {
116
+FORCE_INLINE void forward_kinematics_DELTA(const float (&point)[ABC]) {
117
   forward_kinematics_DELTA(point[A_AXIS], point[B_AXIS], point[C_AXIS]);
117
   forward_kinematics_DELTA(point[A_AXIS], point[B_AXIS], point[C_AXIS]);
118
 }
118
 }
119
 
119
 

Loading…
Cancel
Save