Browse Source

Store Mix factors as reciprocals and multiply

Scott Lahteine 8 years ago
parent
commit
f663220e87
3 changed files with 18 additions and 17 deletions
  1. 15
    16
      Marlin/Marlin_main.cpp
  2. 2
    0
      Marlin/macros.h
  3. 1
    1
      Marlin/planner.cpp

+ 15
- 16
Marlin/Marlin_main.cpp View File

590
 #endif
590
 #endif
591
 
591
 
592
 #if ENABLED(MIXING_EXTRUDER)
592
 #if ENABLED(MIXING_EXTRUDER)
593
-  float mixing_factor[MIXING_STEPPERS];
593
+  float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0.
594
   #if MIXING_VIRTUAL_TOOLS > 1
594
   #if MIXING_VIRTUAL_TOOLS > 1
595
     float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
595
     float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
596
   #endif
596
   #endif
2650
 
2650
 
2651
   void normalize_mix() {
2651
   void normalize_mix() {
2652
     float mix_total = 0.0;
2652
     float mix_total = 0.0;
2653
-    for (int i = 0; i < MIXING_STEPPERS; i++) {
2654
-      float v = mixing_factor[i];
2655
-      if (v < 0) v = mixing_factor[i] = 0;
2656
-      mix_total += v;
2657
-    }
2653
+    for (int i = 0; i < MIXING_STEPPERS; i++) mix_total += RECIPROCAL(mixing_factor[i]);
2658
     // Scale all values if they don't add up to ~1.0
2654
     // Scale all values if they don't add up to ~1.0
2659
-    if (mix_total < 0.9999 || mix_total > 1.0001) {
2655
+    if (!NEAR(mix_total, 1.0)) {
2660
       SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling.");
2656
       SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling.");
2661
-      float mix_scale = 1.0 / mix_total;
2662
-      for (int i = 0; i < MIXING_STEPPERS; i++)
2663
-        mixing_factor[i] *= mix_scale;
2657
+      for (int i = 0; i < MIXING_STEPPERS; i++) mixing_factor[i] *= mix_total;
2664
     }
2658
     }
2665
   }
2659
   }
2666
 
2660
 
2670
     // The total "must" be 1.0 (but it will be normalized)
2664
     // The total "must" be 1.0 (but it will be normalized)
2671
     void gcode_get_mix() {
2665
     void gcode_get_mix() {
2672
       const char* mixing_codes = "ABCDHI";
2666
       const char* mixing_codes = "ABCDHI";
2673
-      for (int i = 0; i < MIXING_STEPPERS; i++)
2674
-        mixing_factor[i] = code_seen(mixing_codes[i]) ? code_value_float() : 0;
2675
-
2667
+      for (int i = 0; i < MIXING_STEPPERS; i++) {
2668
+        float v = code_seen(mixing_codes[i]) ? code_value_float() : 0.0;
2669
+        NOLESS(v, 0.0);
2670
+        mixing_factor[i] = RECIPROCAL(v);
2671
+      }
2676
       normalize_mix();
2672
       normalize_mix();
2677
     }
2673
     }
2678
   #endif
2674
   #endif
7142
    */
7138
    */
7143
   inline void gcode_M163() {
7139
   inline void gcode_M163() {
7144
     int mix_index = code_seen('S') ? code_value_int() : 0;
7140
     int mix_index = code_seen('S') ? code_value_int() : 0;
7145
-    float mix_value = code_seen('P') ? code_value_float() : 0.0;
7146
-    if (mix_index < MIXING_STEPPERS) mixing_factor[mix_index] = mix_value;
7141
+    if (mix_index < MIXING_STEPPERS) {
7142
+      float mix_value = code_seen('P') ? code_value_float() : 0.0;
7143
+      NOLESS(mix_value, 0.0);
7144
+      mixing_factor[mix_index] = RECIPROCAL(mix_value);
7145
+    }
7147
   }
7146
   }
7148
 
7147
 
7149
   #if MIXING_VIRTUAL_TOOLS > 1
7148
   #if MIXING_VIRTUAL_TOOLS > 1
9881
   #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
9880
   #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
9882
     // Initialize mixing to 100% color 1
9881
     // Initialize mixing to 100% color 1
9883
     for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
9882
     for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
9884
-      mixing_factor[i] = (i == 0) ? 1 : 0;
9883
+      mixing_factor[i] = (i == 0) ? 1.0 : 0.0;
9885
     for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++)
9884
     for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++)
9886
       for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
9885
       for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
9887
         mixing_virtual_tool_mix[t][i] = mixing_factor[i];
9886
         mixing_virtual_tool_mix[t][i] = mixing_factor[i];

+ 2
- 0
Marlin/macros.h View File

133
 #define NEAR_ZERO(x) ((x) > -0.000001 && (x) < 0.000001)
133
 #define NEAR_ZERO(x) ((x) > -0.000001 && (x) < 0.000001)
134
 #define NEAR(x,y) NEAR_ZERO((x)-(y))
134
 #define NEAR(x,y) NEAR_ZERO((x)-(y))
135
 
135
 
136
+#define RECIPROCAL(x) (NEAR_ZERO(x) ? 0.0 : 1.0 / (x))
137
+
136
 #endif //__MACROS_H
138
 #endif //__MACROS_H

+ 1
- 1
Marlin/planner.cpp View File

750
   // For a mixing extruder, get a magnified step_event_count for each
750
   // For a mixing extruder, get a magnified step_event_count for each
751
   #if ENABLED(MIXING_EXTRUDER)
751
   #if ENABLED(MIXING_EXTRUDER)
752
     for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
752
     for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
753
-      block->mix_event_count[i] = UNEAR_ZERO(mixing_factor[i]) ? 0 : block->step_event_count / mixing_factor[i];
753
+      block->mix_event_count[i] = mixing_factor[i] * block->step_event_count;
754
   #endif
754
   #endif
755
 
755
 
756
   #if FAN_COUNT > 0
756
   #if FAN_COUNT > 0

Loading…
Cancel
Save