Improved M900
This commit is contained in:
		| @@ -29,11 +29,10 @@ | |||||||
| #include "../../../module/stepper.h" | #include "../../../module/stepper.h" | ||||||
|  |  | ||||||
| #if ENABLED(EXTRA_LIN_ADVANCE_K) | #if ENABLED(EXTRA_LIN_ADVANCE_K) | ||||||
|   float saved_extruder_advance_K[EXTRUDERS]; |   float other_extruder_advance_K[EXTRUDERS]; | ||||||
|   uint8_t lin_adv_slot = 0; |   uint8_t lin_adv_slot = 0; | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * M900: Get or Set Linear Advance K-factor |  * M900: Get or Set Linear Advance K-factor | ||||||
|  *  T<tool>     Which tool to address |  *  T<tool>     Which tool to address | ||||||
| @@ -43,100 +42,107 @@ | |||||||
|  */ |  */ | ||||||
| void GcodeSuite::M900() { | void GcodeSuite::M900() { | ||||||
|  |  | ||||||
|   #if EXTRUDERS < 2 |   auto echo_value_oor = [] (const char ltr, const bool ten=true) { | ||||||
|     constexpr uint8_t tool_index = 0; |     SERIAL_CHAR('?'); SERIAL_CHAR(ltr); | ||||||
|   #else |     SERIAL_ECHOPGM(" value out of range"); | ||||||
|     const uint8_t tool_index = parser.intval('T', active_extruder); |     if (ten) SERIAL_ECHOPGM(" (0-10)"); | ||||||
|     if (tool_index >= EXTRUDERS) { |     SERIAL_ECHOLNPGM("."); | ||||||
|       SERIAL_ECHOLNPGM("?T value out of range."); |  | ||||||
|       return; |  | ||||||
|     } |  | ||||||
|   #endif |  | ||||||
|  |  | ||||||
|   #if ENABLED(EXTRA_LIN_ADVANCE_K) |  | ||||||
|  |  | ||||||
|     bool ext_slot = TEST(lin_adv_slot, tool_index); |  | ||||||
|  |  | ||||||
|     if (parser.seenval('S')) { |  | ||||||
|       const bool slot = parser.value_bool(); |  | ||||||
|       if (ext_slot != slot) { |  | ||||||
|         ext_slot = slot; |  | ||||||
|         SET_BIT_TO(lin_adv_slot, tool_index, slot); |  | ||||||
|         planner.synchronize(); |  | ||||||
|         const float temp = planner.extruder_advance_K[tool_index]; |  | ||||||
|         planner.extruder_advance_K[tool_index] = saved_extruder_advance_K[tool_index]; |  | ||||||
|         saved_extruder_advance_K[tool_index] = temp; |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     if (parser.seenval('K')) { |  | ||||||
|       const float newK = parser.value_float(); |  | ||||||
|       if (WITHIN(newK, 0, 10)) { |  | ||||||
|         if (ext_slot) |  | ||||||
|           saved_extruder_advance_K[tool_index] = newK; |  | ||||||
|         else { |  | ||||||
|           planner.synchronize(); |  | ||||||
|           planner.extruder_advance_K[tool_index] = newK; |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
|       else |  | ||||||
|         SERIAL_ECHOLNPGM("?K value out of range (0-10)."); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     if (parser.seenval('L')) { |  | ||||||
|       const float newL = parser.value_float(); |  | ||||||
|       if (WITHIN(newL, 0, 10)) { |  | ||||||
|         if (!ext_slot) |  | ||||||
|           saved_extruder_advance_K[tool_index] = newL; |  | ||||||
|         else { |  | ||||||
|           planner.synchronize(); |  | ||||||
|           planner.extruder_advance_K[tool_index] = newL; |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
|       else |  | ||||||
|         SERIAL_ECHOLNPGM("?L value out of range (0-10)."); |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   if (!parser.seen_any()) { |   if (!parser.seen_any()) { | ||||||
|  |  | ||||||
|  |     #if ENABLED(EXTRA_LIN_ADVANCE_K) | ||||||
|  |  | ||||||
|       #if EXTRUDERS < 2 |       #if EXTRUDERS < 2 | ||||||
|         SERIAL_ECHOLNPAIR("Advance S", ext_slot, " K", planner.extruder_advance_K[0], |         SERIAL_ECHOLNPAIR("Advance S", int(new_slot), " K", kref, "(S", int(!new_slot), " K", lref, ")"); | ||||||
|                           "(Slot ", 1 - ext_slot, " K", saved_extruder_advance_K[0], ")"); |  | ||||||
|       #else |       #else | ||||||
|         LOOP_L_N(i, EXTRUDERS) { |         LOOP_L_N(i, EXTRUDERS) { | ||||||
|           const int slot = (int)TEST(lin_adv_slot, i); |           const bool slot = TEST(lin_adv_slot, i); | ||||||
|           SERIAL_ECHOLNPAIR("Advance T", int(i), " S", slot, " K", planner.extruder_advance_K[i], |           SERIAL_ECHOLNPAIR("Advance T", int(i), " S", int(slot), " K", planner.extruder_advance_K[i], | ||||||
|                             "(Slot ", 1 - slot, " K", saved_extruder_advance_K[i], ")"); |                             "(S", int(!slot), " K", other_extruder_advance_K[i], ")"); | ||||||
|           SERIAL_EOL(); |           SERIAL_EOL(); | ||||||
|         } |         } | ||||||
|       #endif |       #endif | ||||||
|     } |  | ||||||
|  |  | ||||||
|     #else |     #else | ||||||
|  |  | ||||||
|     if (parser.seenval('K')) { |  | ||||||
|       const float newK = parser.value_float(); |  | ||||||
|       if (WITHIN(newK, 0, 10)) { |  | ||||||
|         planner.synchronize(); |  | ||||||
|         planner.extruder_advance_K[tool_index] = newK; |  | ||||||
|       } |  | ||||||
|       else |  | ||||||
|         SERIAL_ECHOLNPGM("?K value out of range (0-10)."); |  | ||||||
|     } |  | ||||||
|     else { |  | ||||||
|       SERIAL_ECHO_START(); |       SERIAL_ECHO_START(); | ||||||
|       #if EXTRUDERS < 2 |       #if EXTRUDERS < 2 | ||||||
|         SERIAL_ECHOLNPAIR("Advance K=", planner.extruder_advance_K[0]); |         SERIAL_ECHOLNPAIR("Advance K=", planner.extruder_advance_K[0]); | ||||||
|       #else |       #else | ||||||
|         SERIAL_ECHOPGM("Advance K"); |         SERIAL_ECHOPGM("Advance K"); | ||||||
|         LOOP_L_N(i, EXTRUDERS) { |         LOOP_L_N(i, EXTRUDERS) { | ||||||
|           SERIAL_CHAR(' '); SERIAL_ECHO(int(i)); |           SERIAL_CHAR(' ', '0' + i, ':'); | ||||||
|           SERIAL_CHAR('='); SERIAL_ECHO(planner.extruder_advance_K[i]); |           SERIAL_ECHO(planner.extruder_advance_K[i]); | ||||||
|         } |         } | ||||||
|         SERIAL_EOL(); |         SERIAL_EOL(); | ||||||
|       #endif |       #endif | ||||||
|  |  | ||||||
|  |     #endif | ||||||
|  |  | ||||||
|  |     return; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   #if EXTRUDERS < 2 | ||||||
|  |     constexpr uint8_t tool_index = 0; | ||||||
|  |   #else | ||||||
|  |     const uint8_t tool_index = parser.intval('T', active_extruder); | ||||||
|  |     if (tool_index >= EXTRUDERS) { | ||||||
|  |       echo_value_oor('T', false); | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |   #endif | ||||||
|  |  | ||||||
|  |   float &kref = planner.extruder_advance_K[tool_index], | ||||||
|  |         &lref = other_extruder_advance_K[tool_index]; | ||||||
|  |   const float oldK = kref, oldOther = lref; | ||||||
|  |   float newK = oldK; | ||||||
|  |  | ||||||
|  |   #if ENABLED(EXTRA_LIN_ADVANCE_K) | ||||||
|  |  | ||||||
|  |     const bool old_slot = TEST(lin_adv_slot, tool_index), // The tool's current slot (0 or 1) | ||||||
|  |                new_slot = parser.boolval('S', old_slot);  // The passed slot (default = current) | ||||||
|  |  | ||||||
|  |     // If a new slot is being selected swap the current and | ||||||
|  |     // saved K values. Do here so K/L will apply correctly. | ||||||
|  |     if (new_slot != old_slot) {                       // Not the same slot? | ||||||
|  |       SET_BIT_TO(lin_adv_slot, tool_index, new_slot); // Update the slot for the tool | ||||||
|  |       newK = oldOther;                                // Get new K value from backup | ||||||
|  |       lref = oldK;                                    // Save K to backup | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Set the main K value. Apply if the main slot is active. | ||||||
|  |     if (parser.seenval('K')) { | ||||||
|  |       const float newK = parser.value_float(); | ||||||
|  |       if (!WITHIN(newK, 0, 10)) echo_value_oor('K'); | ||||||
|  |       else if (new_slot)        lref = newK;          // S1 Knn | ||||||
|  |       else                      newK = newK;          // S0 Knn | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Set the extra K value. Apply if the extra slot is active. | ||||||
|  |     if (parser.seenval('L')) { | ||||||
|  |       const float newL = parser.value_float(); | ||||||
|  |       if (!WITHIN(newL, 0, 10)) echo_value_oor('L'); | ||||||
|  |       else if (!new_slot)       lref = newL;          // S0 Lnn | ||||||
|  |       else                      newK = newL;          // S1 Lnn | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |   #else | ||||||
|  |  | ||||||
|  |     if (parser.seenval('K')) { | ||||||
|  |       const float newK = parser.value_float(); | ||||||
|  |       if (WITHIN(newK, 0, 10)) | ||||||
|  |         newK = newK; | ||||||
|  |       else | ||||||
|  |         echo_value_oor('K'); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   #endif |   #endif | ||||||
|  |  | ||||||
|  |   if (newK != oldK) { | ||||||
|  |     planner.synchronize(); | ||||||
|  |     kref = newK; | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| #endif // LIN_ADVANCE | #endif // LIN_ADVANCE | ||||||
|   | |||||||
| @@ -101,7 +101,7 @@ | |||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #if ENABLED(EXTRA_LIN_ADVANCE_K) | #if ENABLED(EXTRA_LIN_ADVANCE_K) | ||||||
|   extern float saved_extruder_advance_K[EXTRUDERS]; |   extern float other_extruder_advance_K[EXTRUDERS]; | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #if EXTRUDERS > 1 | #if EXTRUDERS > 1 | ||||||
| @@ -2717,7 +2717,7 @@ void MarlinSettings::reset() { | |||||||
|     LOOP_L_N(i, EXTRUDERS) { |     LOOP_L_N(i, EXTRUDERS) { | ||||||
|       planner.extruder_advance_K[i] = LIN_ADVANCE_K; |       planner.extruder_advance_K[i] = LIN_ADVANCE_K; | ||||||
|       #if ENABLED(EXTRA_LIN_ADVANCE_K) |       #if ENABLED(EXTRA_LIN_ADVANCE_K) | ||||||
|         saved_extruder_advance_K[i] = LIN_ADVANCE_K; |         other_extruder_advance_K[i] = LIN_ADVANCE_K; | ||||||
|       #endif |       #endif | ||||||
|     } |     } | ||||||
|   #endif |   #endif | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user