Browse Source

Improved M900

Scott Lahteine 4 years ago
parent
commit
6b5347ee39
2 changed files with 75 additions and 69 deletions
  1. 73
    67
      Marlin/src/gcode/feature/advance/M900.cpp
  2. 2
    2
      Marlin/src/module/configuration_store.cpp

+ 73
- 67
Marlin/src/gcode/feature/advance/M900.cpp View File

@@ -29,11 +29,10 @@
29 29
 #include "../../../module/stepper.h"
30 30
 
31 31
 #if ENABLED(EXTRA_LIN_ADVANCE_K)
32
-  float saved_extruder_advance_K[EXTRUDERS];
32
+  float other_extruder_advance_K[EXTRUDERS];
33 33
   uint8_t lin_adv_slot = 0;
34 34
 #endif
35 35
 
36
-
37 36
 /**
38 37
  * M900: Get or Set Linear Advance K-factor
39 38
  *  T<tool>     Which tool to address
@@ -43,100 +42,107 @@
43 42
  */
44 43
 void GcodeSuite::M900() {
45 44
 
45
+  auto echo_value_oor = [] (const char ltr, const bool ten=true) {
46
+    SERIAL_CHAR('?'); SERIAL_CHAR(ltr);
47
+    SERIAL_ECHOPGM(" value out of range");
48
+    if (ten) SERIAL_ECHOPGM(" (0-10)");
49
+    SERIAL_ECHOLNPGM(".");
50
+  }
51
+
52
+  if (!parser.seen_any()) {
53
+
54
+    #if ENABLED(EXTRA_LIN_ADVANCE_K)
55
+
56
+      #if EXTRUDERS < 2
57
+        SERIAL_ECHOLNPAIR("Advance S", int(new_slot), " K", kref, "(S", int(!new_slot), " K", lref, ")");
58
+      #else
59
+        LOOP_L_N(i, EXTRUDERS) {
60
+          const bool slot = TEST(lin_adv_slot, i);
61
+          SERIAL_ECHOLNPAIR("Advance T", int(i), " S", int(slot), " K", planner.extruder_advance_K[i],
62
+                            "(S", int(!slot), " K", other_extruder_advance_K[i], ")");
63
+          SERIAL_EOL();
64
+        }
65
+      #endif
66
+
67
+    #else
68
+
69
+      SERIAL_ECHO_START();
70
+      #if EXTRUDERS < 2
71
+        SERIAL_ECHOLNPAIR("Advance K=", planner.extruder_advance_K[0]);
72
+      #else
73
+        SERIAL_ECHOPGM("Advance K");
74
+        LOOP_L_N(i, EXTRUDERS) {
75
+          SERIAL_CHAR(' ', '0' + i, ':');
76
+          SERIAL_ECHO(planner.extruder_advance_K[i]);
77
+        }
78
+        SERIAL_EOL();
79
+      #endif
80
+
81
+    #endif
82
+
83
+    return;
84
+  }
85
+
46 86
   #if EXTRUDERS < 2
47 87
     constexpr uint8_t tool_index = 0;
48 88
   #else
49 89
     const uint8_t tool_index = parser.intval('T', active_extruder);
50 90
     if (tool_index >= EXTRUDERS) {
51
-      SERIAL_ECHOLNPGM("?T value out of range.");
91
+      echo_value_oor('T', false);
52 92
       return;
53 93
     }
54 94
   #endif
55 95
 
96
+  float &kref = planner.extruder_advance_K[tool_index],
97
+        &lref = other_extruder_advance_K[tool_index];
98
+  const float oldK = kref, oldOther = lref;
99
+  float newK = oldK;
100
+
56 101
   #if ENABLED(EXTRA_LIN_ADVANCE_K)
57 102
 
58
-    bool ext_slot = TEST(lin_adv_slot, tool_index);
59
-
60
-    if (parser.seenval('S')) {
61
-      const bool slot = parser.value_bool();
62
-      if (ext_slot != slot) {
63
-        ext_slot = slot;
64
-        SET_BIT_TO(lin_adv_slot, tool_index, slot);
65
-        planner.synchronize();
66
-        const float temp = planner.extruder_advance_K[tool_index];
67
-        planner.extruder_advance_K[tool_index] = saved_extruder_advance_K[tool_index];
68
-        saved_extruder_advance_K[tool_index] = temp;
69
-      }
103
+    const bool old_slot = TEST(lin_adv_slot, tool_index), // The tool's current slot (0 or 1)
104
+               new_slot = parser.boolval('S', old_slot);  // The passed slot (default = current)
105
+
106
+    // If a new slot is being selected swap the current and
107
+    // saved K values. Do here so K/L will apply correctly.
108
+    if (new_slot != old_slot) {                       // Not the same slot?
109
+      SET_BIT_TO(lin_adv_slot, tool_index, new_slot); // Update the slot for the tool
110
+      newK = oldOther;                                // Get new K value from backup
111
+      lref = oldK;                                    // Save K to backup
70 112
     }
71 113
 
114
+    // Set the main K value. Apply if the main slot is active.
72 115
     if (parser.seenval('K')) {
73 116
       const float newK = parser.value_float();
74
-      if (WITHIN(newK, 0, 10)) {
75
-        if (ext_slot)
76
-          saved_extruder_advance_K[tool_index] = newK;
77
-        else {
78
-          planner.synchronize();
79
-          planner.extruder_advance_K[tool_index] = newK;
80
-        }
81
-      }
82
-      else
83
-        SERIAL_ECHOLNPGM("?K value out of range (0-10).");
117
+      if (!WITHIN(newK, 0, 10)) echo_value_oor('K');
118
+      else if (new_slot)        lref = newK;          // S1 Knn
119
+      else                      newK = newK;          // S0 Knn
84 120
     }
85 121
 
122
+    // Set the extra K value. Apply if the extra slot is active.
86 123
     if (parser.seenval('L')) {
87 124
       const float newL = parser.value_float();
88
-      if (WITHIN(newL, 0, 10)) {
89
-        if (!ext_slot)
90
-          saved_extruder_advance_K[tool_index] = newL;
91
-        else {
92
-          planner.synchronize();
93
-          planner.extruder_advance_K[tool_index] = newL;
94
-        }
95
-      }
96
-      else
97
-        SERIAL_ECHOLNPGM("?L value out of range (0-10).");
98
-    }
99
-
100
-    if (!parser.seen_any()) {
101
-      #if EXTRUDERS < 2
102
-        SERIAL_ECHOLNPAIR("Advance S", ext_slot, " K", planner.extruder_advance_K[0],
103
-                          "(Slot ", 1 - ext_slot, " K", saved_extruder_advance_K[0], ")");
104
-      #else
105
-        LOOP_L_N(i, EXTRUDERS) {
106
-          const int slot = (int)TEST(lin_adv_slot, i);
107
-          SERIAL_ECHOLNPAIR("Advance T", int(i), " S", slot, " K", planner.extruder_advance_K[i],
108
-                            "(Slot ", 1 - slot, " K", saved_extruder_advance_K[i], ")");
109
-          SERIAL_EOL();
110
-        }
111
-      #endif
125
+      if (!WITHIN(newL, 0, 10)) echo_value_oor('L');
126
+      else if (!new_slot)       lref = newL;          // S0 Lnn
127
+      else                      newK = newL;          // S1 Lnn
112 128
     }
113 129
 
114 130
   #else
115 131
 
116 132
     if (parser.seenval('K')) {
117 133
       const float newK = parser.value_float();
118
-      if (WITHIN(newK, 0, 10)) {
119
-        planner.synchronize();
120
-        planner.extruder_advance_K[tool_index] = newK;
121
-      }
134
+      if (WITHIN(newK, 0, 10))
135
+        newK = newK;
122 136
       else
123
-        SERIAL_ECHOLNPGM("?K value out of range (0-10).");
124
-    }
125
-    else {
126
-      SERIAL_ECHO_START();
127
-      #if EXTRUDERS < 2
128
-        SERIAL_ECHOLNPAIR("Advance K=", planner.extruder_advance_K[0]);
129
-      #else
130
-        SERIAL_ECHOPGM("Advance K");
131
-        LOOP_L_N(i, EXTRUDERS) {
132
-          SERIAL_CHAR(' '); SERIAL_ECHO(int(i));
133
-          SERIAL_CHAR('='); SERIAL_ECHO(planner.extruder_advance_K[i]);
134
-        }
135
-        SERIAL_EOL();
136
-      #endif
137
+        echo_value_oor('K');
137 138
     }
138 139
 
139 140
   #endif
141
+
142
+  if (newK != oldK) {
143
+    planner.synchronize();
144
+    kref = newK;
145
+  }
140 146
 }
141 147
 
142 148
 #endif // LIN_ADVANCE

+ 2
- 2
Marlin/src/module/configuration_store.cpp View File

@@ -101,7 +101,7 @@
101 101
 #endif
102 102
 
103 103
 #if ENABLED(EXTRA_LIN_ADVANCE_K)
104
-  extern float saved_extruder_advance_K[EXTRUDERS];
104
+  extern float other_extruder_advance_K[EXTRUDERS];
105 105
 #endif
106 106
 
107 107
 #if EXTRUDERS > 1
@@ -2717,7 +2717,7 @@ void MarlinSettings::reset() {
2717 2717
     LOOP_L_N(i, EXTRUDERS) {
2718 2718
       planner.extruder_advance_K[i] = LIN_ADVANCE_K;
2719 2719
       #if ENABLED(EXTRA_LIN_ADVANCE_K)
2720
-        saved_extruder_advance_K[i] = LIN_ADVANCE_K;
2720
+        other_extruder_advance_K[i] = LIN_ADVANCE_K;
2721 2721
       #endif
2722 2722
     }
2723 2723
   #endif

Loading…
Cancel
Save