Browse Source

Add infrastructure for MMU parametrization after comm start

For now, only the Extra loading distance is being sent, but the infrastructure can be easily extended for other registers as well.
D.R.racer 2 years ago
parent
commit
b0466ae20f
3 changed files with 41 additions and 7 deletions
  1. 3 2
      Firmware/mmu2.cpp
  2. 27 4
      Firmware/mmu2_protocol_logic.cpp
  3. 11 1
      Firmware/mmu2_protocol_logic.h

+ 3 - 2
Firmware/mmu2.cpp

@@ -39,7 +39,8 @@ static constexpr float MMU2_LOAD_TO_NOZZLE_LENGTH = 87.0F + 5.0F;
 // - ToolChange shall not try to push filament into the very tip of the nozzle
 // to have some space for additional G-code to tune the extruded filament length
 // in the profile
-static constexpr float MMU2_TOOL_CHANGE_LOAD_LENGTH = 5.0F;//30.0F;
+// Beware - this value is sent to the MMU upon line up, it is written into its 8bit register 0x0b
+static constexpr uint8_t MMU2_TOOL_CHANGE_LOAD_LENGTH = 5; // mm
 
 static constexpr float MMU2_LOAD_TO_NOZZLE_FEED_RATE = 20.0F; // mm/s
 static constexpr float MMU2_UNLOAD_TO_FINDA_FEED_RATE = 120.0F; // mm/s
@@ -102,7 +103,7 @@ MMU2 mmu2;
 
 MMU2::MMU2()
     : is_mmu_error_monitor_active(false)
-    , logic(&mmu2Serial)
+    , logic(&mmu2Serial, MMU2_TOOL_CHANGE_LOAD_LENGTH)
     , extruder(MMU2_NO_TOOL)
     , tool_change_extruder(MMU2_NO_TOOL)
     , resume_position()

+ 27 - 4
Firmware/mmu2_protocol_logic.cpp

@@ -19,6 +19,10 @@ const uint8_t ProtocolLogic::regs16Addrs[ProtocolLogic::regs16Count] PROGMEM = {
     0x1a, // Pulley position [mm]
 };
 
+const uint8_t ProtocolLogic::initRegs8Addrs[ProtocolLogic::initRegs8Count] PROGMEM = {
+    0x0b, // extra load distance
+};
+
 void ProtocolLogic::CheckAndReportAsyncEvents() {
     // even when waiting for a query period, we need to report a change in filament sensor's state
     // - it is vital for a precise synchronization of moves of the printer and the MMU
@@ -65,6 +69,21 @@ ProtocolLogic::ScopeState __attribute__((noinline)) ProtocolLogic::ProcessRead16
     return ScopeState::Reading16bitRegisters;
 }
 
+void ProtocolLogic::StartWritingInitRegisters() {
+    regIndex = 0;
+    SendWriteRegister(pgm_read_byte(initRegs8Addrs + regIndex), initRegs8[regIndex], ScopeState::WritingInitRegisters);
+}
+
+bool __attribute__((noinline)) ProtocolLogic::ProcessWritingInitRegister(){
+    ++regIndex;
+    if(regIndex >= initRegs8Count){
+        return true;
+    } else {
+        SendWriteRegister(pgm_read_byte(initRegs8Addrs + regIndex), initRegs8[regIndex], ScopeState::WritingInitRegisters);
+    }
+    return false;
+}
+
 void ProtocolLogic::SendAndUpdateFilamentSensor() {
     SendMsg(RequestMsg(RequestMsgCodes::FilamentSensor, lastFSensor = (uint8_t)WhereIsFilament()));
     scopeState = ScopeState::FilamentSensorStateSent;
@@ -262,9 +281,12 @@ StepStatus ProtocolLogic::StartSeqStep() {
             SendVersion(3);
         } else {
             mmuFwVersionBuild = rsp.paramValue; // just register the build number
-            // Start General Interrogation after line up.
-            // For now we just send the state of the filament sensor, but we may request
-            // data point states from the MMU as well. TBD in the future, especially with another protocol
+            // Start General Interrogation after line up - initial parametrization is started
+            StartWritingInitRegisters();
+        }
+        return Processing;
+    case ScopeState::WritingInitRegisters:
+        if( ProcessWritingInitRegister() ){
             SendAndUpdateFilamentSensor();
         }
         return Processing;
@@ -465,7 +487,7 @@ StepStatus ProtocolLogic::IdleStep() {
     return Finished;
 }
 
-ProtocolLogic::ProtocolLogic(MMU2Serial *uart)
+ProtocolLogic::ProtocolLogic(MMU2Serial *uart, uint8_t extraLoadDistance)
     : currentScope(Scope::Stopped)
     , scopeState(ScopeState::Ready)
     , plannedRq(RequestMsgCodes::unknown, 0)
@@ -481,6 +503,7 @@ ProtocolLogic::ProtocolLogic(MMU2Serial *uart)
     , lastFSensor((uint8_t)WhereIsFilament())
     , regs8 { 0, 0, 0 }
     , regs16 { 0, 0 }
+    , initRegs8 { extraLoadDistance }
     , regIndex(0)
     , mmuFwVersion { 0, 0, 0 }
 {}

+ 11 - 1
Firmware/mmu2_protocol_logic.h

@@ -71,7 +71,7 @@ public:
 /// Logic layer of the MMU vs. printer communication protocol
 class ProtocolLogic {
 public:
-    ProtocolLogic(MMU2Serial *uart);
+    ProtocolLogic(MMU2Serial *uart, uint8_t extraLoadDistance);
 
     /// Start/Enable communication with the MMU
     void Start();
@@ -190,6 +190,7 @@ private:
         FilamentSensorStateSent,
         Reading8bitRegisters,
         Reading16bitRegisters,
+        WritingInitRegisters,
         ButtonSent,
         ReadRegisterSent, // standalone requests for reading registers - from higher layers
         WriteRegisterSent,
@@ -222,6 +223,9 @@ private:
     void ProcessRead8bitRegister();
     void StartReading16bitRegisters();
     ScopeState ProcessRead16bitRegister(ProtocolLogic::ScopeState stateAtEnd);
+    void StartWritingInitRegisters();
+    /// @returns true when all registers have been written into the MMU
+    bool ProcessWritingInitRegister();
     void SendAndUpdateFilamentSensor();
     void SendButton(uint8_t btn);
     void SendVersion(uint8_t stage);
@@ -306,6 +310,12 @@ private:
     static const uint8_t regs16Addrs[regs16Count] PROGMEM;
     uint16_t regs16[regs16Count];
 
+    // 8bit init values to be sent to the MMU after line up
+    static constexpr uint8_t initRegs8Count = 1;
+    static_assert(initRegs8Count > 0); // code is not ready for empty lists of registers
+    static const uint8_t initRegs8Addrs[initRegs8Count] PROGMEM;
+    uint8_t initRegs8[initRegs8Count];
+
     uint8_t regIndex;
 
     uint8_t mmuFwVersion[3];