Browse Source

Apply sq macro throughout

Scott Lahteine 8 years ago
parent
commit
9f9fe043ba
5 changed files with 21 additions and 21 deletions
  1. 8
    9
      Marlin/Marlin_main.cpp
  2. 1
    0
      Marlin/macros.h
  3. 8
    8
      Marlin/planner.cpp
  4. 3
    3
      Marlin/planner.h
  5. 1
    1
      Marlin/ultralcd.cpp

+ 8
- 9
Marlin/Marlin_main.cpp View File

3596
          * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
3596
          * so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
3597
          */
3597
          */
3598
 
3598
 
3599
-        int abl2 = auto_bed_leveling_grid_points * auto_bed_leveling_grid_points;
3599
+        int abl2 = sq(auto_bed_leveling_grid_points);
3600
 
3600
 
3601
         double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations
3601
         double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations
3602
                eqnBVector[abl2],     // "B" vector of Z points
3602
                eqnBVector[abl2],     // "B" vector of Z points
3629
 
3629
 
3630
           #if ENABLED(DELTA)
3630
           #if ENABLED(DELTA)
3631
             // Avoid probing the corners (outside the round or hexagon print surface) on a delta printer.
3631
             // Avoid probing the corners (outside the round or hexagon print surface) on a delta printer.
3632
-            float distance_from_center = sqrt(xProbe * xProbe + yProbe * yProbe);
3632
+            float distance_from_center = HYPOT(xProbe, yProbe);
3633
             if (distance_from_center > DELTA_PROBEABLE_RADIUS) continue;
3633
             if (distance_from_center > DELTA_PROBEABLE_RADIUS) continue;
3634
           #endif //DELTA
3634
           #endif //DELTA
3635
 
3635
 
4252
         return;
4252
         return;
4253
       }
4253
       }
4254
     #else
4254
     #else
4255
-      if (sqrt(X_probe_location * X_probe_location + Y_probe_location * Y_probe_location) > DELTA_PROBEABLE_RADIUS) {
4255
+      if (HYPOT(X_probe_location, Y_probe_location) > DELTA_PROBEABLE_RADIUS) {
4256
         SERIAL_PROTOCOLLNPGM("? (X,Y) location outside of probeable radius.");
4256
         SERIAL_PROTOCOLLNPGM("? (X,Y) location outside of probeable radius.");
4257
         return;
4257
         return;
4258
       }
4258
       }
4342
           #else
4342
           #else
4343
             // If we have gone out too far, we can do a simple fix and scale the numbers
4343
             // If we have gone out too far, we can do a simple fix and scale the numbers
4344
             // back in closer to the origin.
4344
             // back in closer to the origin.
4345
-            while (sqrt(X_current * X_current + Y_current * Y_current) > DELTA_PROBEABLE_RADIUS) {
4345
+            while (HYPOT(X_current, Y_current) > DELTA_PROBEABLE_RADIUS) {
4346
               X_current /= 1.25;
4346
               X_current /= 1.25;
4347
               Y_current /= 1.25;
4347
               Y_current /= 1.25;
4348
               if (verbose_level > 3) {
4348
               if (verbose_level > 3) {
4378
        * data points we have so far
4378
        * data points we have so far
4379
        */
4379
        */
4380
       sum = 0.0;
4380
       sum = 0.0;
4381
-      for (uint8_t j = 0; j <= n; j++) {
4382
-        float ss = sample_set[j] - mean;
4383
-        sum += ss * ss;
4384
-      }
4381
+      for (uint8_t j = 0; j <= n; j++)
4382
+        sum += sq(sample_set[j] - mean);
4383
+
4385
       sigma = sqrt(sum / (n + 1));
4384
       sigma = sqrt(sum / (n + 1));
4386
       if (verbose_level > 0) {
4385
       if (verbose_level > 0) {
4387
         if (verbose_level > 1) {
4386
         if (verbose_level > 1) {
8139
      * This is important when there are successive arc motions.
8138
      * This is important when there are successive arc motions.
8140
      */
8139
      */
8141
     // Vector rotation matrix values
8140
     // Vector rotation matrix values
8142
-    float cos_T = 1 - 0.5 * theta_per_segment * theta_per_segment; // Small angle approximation
8141
+    float cos_T = 1 - 0.5 * sq(theta_per_segment); // Small angle approximation
8143
     float sin_T = theta_per_segment;
8142
     float sin_T = theta_per_segment;
8144
 
8143
 
8145
     float arc_target[NUM_AXIS];
8144
     float arc_target[NUM_AXIS];

+ 1
- 0
Marlin/macros.h View File

36
 // Macros for maths shortcuts
36
 // Macros for maths shortcuts
37
 #define RADIANS(d) ((d)*M_PI/180.0)
37
 #define RADIANS(d) ((d)*M_PI/180.0)
38
 #define DEGREES(r) ((r)*180.0/M_PI)
38
 #define DEGREES(r) ((r)*180.0/M_PI)
39
+#define HYPOT(x,y) sqrt(sq(x)+sq(y))
39
 
40
 
40
 // Macros to contrain values
41
 // Macros to contrain values
41
 #define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
42
 #define NOLESS(v,n) do{ if (v < n) v = n; }while(0)

+ 8
- 8
Marlin/planner.cpp View File

171
   }
171
   }
172
 
172
 
173
   #if ENABLED(ADVANCE)
173
   #if ENABLED(ADVANCE)
174
-    volatile long initial_advance = block->advance * entry_factor * entry_factor;
175
-    volatile long final_advance = block->advance * exit_factor * exit_factor;
174
+    volatile long initial_advance = block->advance * sq(entry_factor);
175
+    volatile long final_advance = block->advance * sq(exit_factor);
176
   #endif // ADVANCE
176
   #endif // ADVANCE
177
 
177
 
178
   // block->accelerate_until = accelerate_steps;
178
   // block->accelerate_until = accelerate_steps;
815
   else {
815
   else {
816
     block->millimeters = sqrt(
816
     block->millimeters = sqrt(
817
       #if ENABLED(COREXY)
817
       #if ENABLED(COREXY)
818
-        square(delta_mm[X_HEAD]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_AXIS])
818
+        sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_AXIS])
819
       #elif ENABLED(COREXZ)
819
       #elif ENABLED(COREXZ)
820
-        square(delta_mm[X_HEAD]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_HEAD])
820
+        sq(delta_mm[X_HEAD]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_HEAD])
821
       #elif ENABLED(COREYZ)
821
       #elif ENABLED(COREYZ)
822
-        square(delta_mm[X_AXIS]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_HEAD])
822
+        sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_HEAD]) + sq(delta_mm[Z_HEAD])
823
       #else
823
       #else
824
-        square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_AXIS])
824
+        sq(delta_mm[X_AXIS]) + sq(delta_mm[Y_AXIS]) + sq(delta_mm[Z_AXIS])
825
       #endif
825
       #endif
826
     );
826
     );
827
   }
827
   }
1030
           dsy = current_speed[Y_AXIS] - previous_speed[Y_AXIS],
1030
           dsy = current_speed[Y_AXIS] - previous_speed[Y_AXIS],
1031
           dsz = fabs(csz - previous_speed[Z_AXIS]),
1031
           dsz = fabs(csz - previous_speed[Z_AXIS]),
1032
           dse = fabs(cse - previous_speed[E_AXIS]),
1032
           dse = fabs(cse - previous_speed[E_AXIS]),
1033
-          jerk = sqrt(dsx * dsx + dsy * dsy);
1033
+          jerk = HYPOT(dsx, dsy);
1034
 
1034
 
1035
     //    if ((fabs(previous_speed[X_AXIS]) > 0.0001) || (fabs(previous_speed[Y_AXIS]) > 0.0001)) {
1035
     //    if ((fabs(previous_speed[X_AXIS]) > 0.0001) || (fabs(previous_speed[Y_AXIS]) > 0.0001)) {
1036
     vmax_junction = block->nominal_speed;
1036
     vmax_junction = block->nominal_speed;
1086
     }
1086
     }
1087
     else {
1087
     else {
1088
       long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_steps_per_s2);
1088
       long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_steps_per_s2);
1089
-      float advance = ((STEPS_PER_CUBIC_MM_E) * (EXTRUDER_ADVANCE_K)) * (cse * cse * (EXTRUSION_AREA) * (EXTRUSION_AREA)) * 256;
1089
+      float advance = ((STEPS_PER_CUBIC_MM_E) * (EXTRUDER_ADVANCE_K)) * HYPOT(cse, EXTRUSION_AREA) * 256;
1090
       block->advance = advance;
1090
       block->advance = advance;
1091
       block->advance_rate = acc_dist ? advance / (float)acc_dist : 0;
1091
       block->advance_rate = acc_dist ? advance / (float)acc_dist : 0;
1092
     }
1092
     }

+ 3
- 3
Marlin/planner.h View File

290
      */
290
      */
291
     static float estimate_acceleration_distance(float initial_rate, float target_rate, float accel) {
291
     static float estimate_acceleration_distance(float initial_rate, float target_rate, float accel) {
292
       if (accel == 0) return 0; // accel was 0, set acceleration distance to 0
292
       if (accel == 0) return 0; // accel was 0, set acceleration distance to 0
293
-      return (target_rate * target_rate - initial_rate * initial_rate) / (accel * 2);
293
+      return (sq(target_rate) - sq(initial_rate)) / (accel * 2);
294
     }
294
     }
295
 
295
 
296
     /**
296
     /**
303
      */
303
      */
304
     static float intersection_distance(float initial_rate, float final_rate, float accel, float distance) {
304
     static float intersection_distance(float initial_rate, float final_rate, float accel, float distance) {
305
       if (accel == 0) return 0; // accel was 0, set intersection distance to 0
305
       if (accel == 0) return 0; // accel was 0, set intersection distance to 0
306
-      return (accel * 2 * distance - initial_rate * initial_rate + final_rate * final_rate) / (accel * 4);
306
+      return (accel * 2 * distance - sq(initial_rate) + sq(final_rate)) / (accel * 4);
307
     }
307
     }
308
 
308
 
309
     /**
309
     /**
312
      * 'distance'.
312
      * 'distance'.
313
      */
313
      */
314
     static float max_allowable_speed(float accel, float target_velocity, float distance) {
314
     static float max_allowable_speed(float accel, float target_velocity, float distance) {
315
-      return sqrt(target_velocity * target_velocity - 2 * accel * distance);
315
+      return sqrt(sq(target_velocity) - 2 * accel * distance);
316
     }
316
     }
317
 
317
 
318
     static void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor);
318
     static void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor);

+ 1
- 1
Marlin/ultralcd.cpp View File

1356
   }
1356
   }
1357
   #if ENABLED(DELTA)
1357
   #if ENABLED(DELTA)
1358
     static float delta_clip_radius_2 =  (DELTA_PRINTABLE_RADIUS) * (DELTA_PRINTABLE_RADIUS);
1358
     static float delta_clip_radius_2 =  (DELTA_PRINTABLE_RADIUS) * (DELTA_PRINTABLE_RADIUS);
1359
-    static int delta_clip( float a ) { return sqrt(delta_clip_radius_2 - a*a); }
1359
+    static int delta_clip( float a ) { return sqrt(delta_clip_radius_2 - sq(a)); }
1360
     static void lcd_move_x() { int clip = delta_clip(current_position[Y_AXIS]); _lcd_move_xyz(PSTR(MSG_MOVE_X), X_AXIS, max(sw_endstop_min[X_AXIS], -clip), min(sw_endstop_max[X_AXIS], clip)); }
1360
     static void lcd_move_x() { int clip = delta_clip(current_position[Y_AXIS]); _lcd_move_xyz(PSTR(MSG_MOVE_X), X_AXIS, max(sw_endstop_min[X_AXIS], -clip), min(sw_endstop_max[X_AXIS], clip)); }
1361
     static void lcd_move_y() { int clip = delta_clip(current_position[X_AXIS]); _lcd_move_xyz(PSTR(MSG_MOVE_Y), Y_AXIS, max(sw_endstop_min[Y_AXIS], -clip), min(sw_endstop_max[Y_AXIS], clip)); }
1361
     static void lcd_move_y() { int clip = delta_clip(current_position[X_AXIS]); _lcd_move_xyz(PSTR(MSG_MOVE_Y), Y_AXIS, max(sw_endstop_min[Y_AXIS], -clip), min(sw_endstop_max[Y_AXIS], clip)); }
1362
   #else
1362
   #else

Loading…
Cancel
Save