Browse Source

Split up prepare_move by type

- For better encapsulation add inlines for each prepare_move type
- Move controllerFan vars inside the function as statics
- Some formatting cleanup
- Rename `ClearToSend` as `ok_to_send`
Scott Lahteine 10 years ago
parent
commit
9e95ceb3fc
2 changed files with 102 additions and 87 deletions
  1. 1
    1
      Marlin/Marlin.h
  2. 101
    86
      Marlin/Marlin_main.cpp

+ 1
- 1
Marlin/Marlin.h View File

205
 void disable_all_steppers();
205
 void disable_all_steppers();
206
 
206
 
207
 void FlushSerialRequestResend();
207
 void FlushSerialRequestResend();
208
-void ClearToSend();
208
+void ok_to_send();
209
 
209
 
210
 void get_coordinates();
210
 void get_coordinates();
211
 #ifdef DELTA
211
 #ifdef DELTA

+ 101
- 86
Marlin/Marlin_main.cpp View File

1657
 
1657
 
1658
 #ifdef FWRETRACT
1658
 #ifdef FWRETRACT
1659
 
1659
 
1660
-  void retract(bool retracting, bool swapretract = false) {
1660
+  void retract(bool retracting, bool swapping=false) {
1661
 
1661
 
1662
     if (retracting == retracted[active_extruder]) return;
1662
     if (retracting == retracted[active_extruder]) return;
1663
 
1663
 
1668
     if (retracting) {
1668
     if (retracting) {
1669
 
1669
 
1670
       feedrate = retract_feedrate * 60;
1670
       feedrate = retract_feedrate * 60;
1671
-      current_position[E_AXIS] += (swapretract ? retract_length_swap : retract_length) / volumetric_multiplier[active_extruder];
1671
+      current_position[E_AXIS] += (swapping ? retract_length_swap : retract_length) / volumetric_multiplier[active_extruder];
1672
       plan_set_e_position(current_position[E_AXIS]);
1672
       plan_set_e_position(current_position[E_AXIS]);
1673
       prepare_move();
1673
       prepare_move();
1674
 
1674
 
1695
       }
1695
       }
1696
 
1696
 
1697
       feedrate = retract_recover_feedrate * 60;
1697
       feedrate = retract_recover_feedrate * 60;
1698
-      float move_e = swapretract ? retract_length_swap + retract_recover_length_swap : retract_length + retract_recover_length;
1698
+      float move_e = swapping ? retract_length_swap + retract_recover_length_swap : retract_length + retract_recover_length;
1699
       current_position[E_AXIS] -= move_e / volumetric_multiplier[active_extruder];
1699
       current_position[E_AXIS] -= move_e / volumetric_multiplier[active_extruder];
1700
       plan_set_e_position(current_position[E_AXIS]);
1700
       plan_set_e_position(current_position[E_AXIS]);
1701
       prepare_move();
1701
       prepare_move();
1770
     #endif //FWRETRACT
1770
     #endif //FWRETRACT
1771
 
1771
 
1772
     prepare_move();
1772
     prepare_move();
1773
-    //ClearToSend();
1773
+    //ok_to_send();
1774
   }
1774
   }
1775
 }
1775
 }
1776
 
1776
 
4292
       destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
4292
       destination[X_AXIS] = delta[X_AXIS]/axis_scaling[X_AXIS];
4293
       destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
4293
       destination[Y_AXIS] = delta[Y_AXIS]/axis_scaling[Y_AXIS];
4294
       prepare_move();
4294
       prepare_move();
4295
-      //ClearToSend();
4295
+      //ok_to_send();
4296
       return true;
4296
       return true;
4297
     }
4297
     }
4298
     return false;
4298
     return false;
5515
     SERIAL_ECHOLNPGM("\"");
5515
     SERIAL_ECHOLNPGM("\"");
5516
   }
5516
   }
5517
 
5517
 
5518
-  ClearToSend();
5518
+  ok_to_send();
5519
 }
5519
 }
5520
 
5520
 
5521
 void FlushSerialRequestResend() {
5521
 void FlushSerialRequestResend() {
5523
   MYSERIAL.flush();
5523
   MYSERIAL.flush();
5524
   SERIAL_PROTOCOLPGM(MSG_RESEND);
5524
   SERIAL_PROTOCOLPGM(MSG_RESEND);
5525
   SERIAL_PROTOCOLLN(gcode_LastN + 1);
5525
   SERIAL_PROTOCOLLN(gcode_LastN + 1);
5526
-  ClearToSend();
5526
+  ok_to_send();
5527
 }
5527
 }
5528
 
5528
 
5529
-void ClearToSend() {
5529
+void ok_to_send() {
5530
   refresh_cmd_timeout();
5530
   refresh_cmd_timeout();
5531
   #ifdef SDSUPPORT
5531
   #ifdef SDSUPPORT
5532
     if (fromsd[cmd_queue_index_r]) return;
5532
     if (fromsd[cmd_queue_index_r]) return;
5755
 
5755
 
5756
 #endif // PREVENT_DANGEROUS_EXTRUDE
5756
 #endif // PREVENT_DANGEROUS_EXTRUDE
5757
 
5757
 
5758
-void prepare_move() {
5759
-  clamp_to_software_endstops(destination);
5760
-  refresh_cmd_timeout();
5761
-
5762
-  #ifdef PREVENT_DANGEROUS_EXTRUDE
5763
-    (void)prevent_dangerous_extrude(current_position[E_AXIS], destination[E_AXIS]);
5764
-  #endif
5765
-
5766
-  #ifdef SCARA //for now same as delta-code
5767
-
5768
-    float difference[NUM_AXIS];
5769
-    for (int8_t i = 0; i < NUM_AXIS; i++) difference[i] = destination[i] - current_position[i];
5770
-
5771
-    float cartesian_mm = sqrt(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS]));
5772
-    if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
5773
-    if (cartesian_mm < 0.000001) { return; }
5774
-    float seconds = 6000 * cartesian_mm / feedrate / feedrate_multiplier;
5775
-    int steps = max(1, int(scara_segments_per_second * seconds));
5776
-
5777
-    //SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
5778
-    //SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
5779
-    //SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
5780
-
5781
-    for (int s = 1; s <= steps; s++) {
5782
-      float fraction = float(s) / float(steps);
5783
-      for (int8_t i = 0; i < NUM_AXIS; i++) destination[i] = current_position[i] + difference[i] * fraction;
5784
-  
5785
-      calculate_delta(destination);
5786
-      //SERIAL_ECHOPGM("destination[X_AXIS]="); SERIAL_ECHOLN(destination[X_AXIS]);
5787
-      //SERIAL_ECHOPGM("destination[Y_AXIS]="); SERIAL_ECHOLN(destination[Y_AXIS]);
5788
-      //SERIAL_ECHOPGM("destination[Z_AXIS]="); SERIAL_ECHOLN(destination[Z_AXIS]);
5789
-      //SERIAL_ECHOPGM("delta[X_AXIS]="); SERIAL_ECHOLN(delta[X_AXIS]);
5790
-      //SERIAL_ECHOPGM("delta[Y_AXIS]="); SERIAL_ECHOLN(delta[Y_AXIS]);
5791
-      //SERIAL_ECHOPGM("delta[Z_AXIS]="); SERIAL_ECHOLN(delta[Z_AXIS]);
5792
-
5793
-      plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], destination[E_AXIS], feedrate/60*feedrate_multiplier/100.0, active_extruder);
5794
-    }
5795
-
5796
-  #endif // SCARA
5797
-  
5798
-  #ifdef DELTA
5758
+#if defined(DELTA) || defined(SCARA)
5799
 
5759
 
5760
+  inline bool prepare_move_delta() {
5800
     float difference[NUM_AXIS];
5761
     float difference[NUM_AXIS];
5801
     for (int8_t i=0; i < NUM_AXIS; i++) difference[i] = destination[i] - current_position[i];
5762
     for (int8_t i=0; i < NUM_AXIS; i++) difference[i] = destination[i] - current_position[i];
5802
 
5763
 
5803
     float cartesian_mm = sqrt(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS]));
5764
     float cartesian_mm = sqrt(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS]));
5804
     if (cartesian_mm < 0.000001) cartesian_mm = abs(difference[E_AXIS]);
5765
     if (cartesian_mm < 0.000001) cartesian_mm = abs(difference[E_AXIS]);
5805
-    if (cartesian_mm < 0.000001) return;
5766
+    if (cartesian_mm < 0.000001) return false;
5806
     float seconds = 6000 * cartesian_mm / feedrate / feedrate_multiplier;
5767
     float seconds = 6000 * cartesian_mm / feedrate / feedrate_multiplier;
5807
     int steps = max(1, int(delta_segments_per_second * seconds));
5768
     int steps = max(1, int(delta_segments_per_second * seconds));
5808
 
5769
 
5811
     // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
5772
     // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
5812
 
5773
 
5813
     for (int s = 1; s <= steps; s++) {
5774
     for (int s = 1; s <= steps; s++) {
5775
+
5814
       float fraction = float(s) / float(steps);
5776
       float fraction = float(s) / float(steps);
5815
-      for (int8_t i = 0; i < NUM_AXIS; i++) destination[i] = current_position[i] + difference[i] * fraction;
5777
+
5778
+      for (int8_t i = 0; i < NUM_AXIS; i++)
5779
+        destination[i] = current_position[i] + difference[i] * fraction;
5780
+
5816
       calculate_delta(destination);
5781
       calculate_delta(destination);
5782
+
5817
       #ifdef ENABLE_AUTO_BED_LEVELING
5783
       #ifdef ENABLE_AUTO_BED_LEVELING
5818
         adjust_delta(destination);
5784
         adjust_delta(destination);
5819
       #endif
5785
       #endif
5786
+
5787
+      //SERIAL_ECHOPGM("destination[X_AXIS]="); SERIAL_ECHOLN(destination[X_AXIS]);
5788
+      //SERIAL_ECHOPGM("destination[Y_AXIS]="); SERIAL_ECHOLN(destination[Y_AXIS]);
5789
+      //SERIAL_ECHOPGM("destination[Z_AXIS]="); SERIAL_ECHOLN(destination[Z_AXIS]);
5790
+      //SERIAL_ECHOPGM("delta[X_AXIS]="); SERIAL_ECHOLN(delta[X_AXIS]);
5791
+      //SERIAL_ECHOPGM("delta[Y_AXIS]="); SERIAL_ECHOLN(delta[Y_AXIS]);
5792
+      //SERIAL_ECHOPGM("delta[Z_AXIS]="); SERIAL_ECHOLN(delta[Z_AXIS]);
5793
+
5820
       plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], destination[E_AXIS], feedrate/60*feedrate_multiplier/100.0, active_extruder);
5794
       plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], destination[E_AXIS], feedrate/60*feedrate_multiplier/100.0, active_extruder);
5821
     }
5795
     }
5796
+    return true;
5797
+  }
5822
 
5798
 
5823
-  #endif // DELTA
5799
+#endif // DELTA || SCARA
5824
 
5800
 
5825
-  #ifdef DUAL_X_CARRIAGE
5801
+#ifdef SCARA
5802
+  inline bool prepare_move_scara() { return prepare_move_delta(); }
5803
+#endif
5804
+
5805
+#ifdef DUAL_X_CARRIAGE
5806
+
5807
+  inline bool prepare_move_dual_x_carriage() {
5826
     if (active_extruder_parked) {
5808
     if (active_extruder_parked) {
5827
       if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
5809
       if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
5828
         // move duplicate extruder into correct duplication position.
5810
         // move duplicate extruder into correct duplication position.
5843
             set_current_to_destination();
5825
             set_current_to_destination();
5844
             NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
5826
             NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
5845
             delayed_move_time = millis();
5827
             delayed_move_time = millis();
5846
-            return;
5828
+            return false;
5847
           }
5829
           }
5848
         }
5830
         }
5849
         delayed_move_time = 0;
5831
         delayed_move_time = 0;
5854
         active_extruder_parked = false;
5836
         active_extruder_parked = false;
5855
       }
5837
       }
5856
     }
5838
     }
5857
-  #endif // DUAL_X_CARRIAGE
5839
+    return true;
5840
+  }
5858
 
5841
 
5859
-  #if !defined(DELTA) && !defined(SCARA)
5842
+#endif // DUAL_X_CARRIAGE
5843
+
5844
+#if !defined(DELTA) && !defined(SCARA)
5845
+
5846
+  inline bool prepare_move_cartesian() {
5860
     // Do not use feedrate_multiplier for E or Z only moves
5847
     // Do not use feedrate_multiplier for E or Z only moves
5861
     if (current_position[X_AXIS] == destination[X_AXIS] && current_position[Y_AXIS] == destination[Y_AXIS]) {
5848
     if (current_position[X_AXIS] == destination[X_AXIS] && current_position[Y_AXIS] == destination[Y_AXIS]) {
5862
       line_to_destination();
5849
       line_to_destination();
5864
     else {
5851
     else {
5865
       #ifdef MESH_BED_LEVELING
5852
       #ifdef MESH_BED_LEVELING
5866
         mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], (feedrate/60)*(feedrate_multiplier/100.0), active_extruder);
5853
         mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], (feedrate/60)*(feedrate_multiplier/100.0), active_extruder);
5867
-        return;
5854
+        return false;
5868
       #else
5855
       #else
5869
         line_to_destination(feedrate * feedrate_multiplier / 100.0);
5856
         line_to_destination(feedrate * feedrate_multiplier / 100.0);
5870
-      #endif  // MESH_BED_LEVELING
5857
+      #endif
5871
     }
5858
     }
5872
-  #endif // !(DELTA || SCARA)
5859
+    return true;
5860
+  }
5861
+
5862
+#endif // !DELTA && !SCARA
5863
+
5864
+/**
5865
+ * Prepare a single move and get ready for the next one
5866
+ */
5867
+void prepare_move() {
5868
+  clamp_to_software_endstops(destination);
5869
+  refresh_cmd_timeout();
5870
+
5871
+  #ifdef PREVENT_DANGEROUS_EXTRUDE
5872
+    prevent_dangerous_extrude(current_position[E_AXIS], destination[E_AXIS]);
5873
+  #endif
5874
+
5875
+  #ifdef SCARA
5876
+    if (!prepare_move_scara()) return;
5877
+  #elif defined(DELTA)
5878
+    if (!prepare_move_delta()) return;
5879
+  #endif
5880
+
5881
+  #ifdef DUAL_X_CARRIAGE
5882
+    if (!prepare_move_dual_x_carriage()) return;
5883
+  #endif
5884
+
5885
+  #if !defined(DELTA) && !defined(SCARA)
5886
+    if (!prepare_move_cartesian()) return;
5887
+  #endif
5873
 
5888
 
5874
   set_current_to_destination();
5889
   set_current_to_destination();
5875
 }
5890
 }
5889
 
5904
 
5890
 #if HAS_CONTROLLERFAN
5905
 #if HAS_CONTROLLERFAN
5891
 
5906
 
5892
-millis_t lastMotor = 0; // Last time a motor was turned on
5893
-millis_t lastMotorCheck = 0; // Last time the state was checked
5894
-
5895
-void controllerFan() {
5896
-  millis_t ms = millis();
5897
-  if (ms >= lastMotorCheck + 2500) { // Not a time critical function, so we only check every 2500ms
5898
-    lastMotorCheck = ms;
5899
-    if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || soft_pwm_bed > 0
5900
-      || E0_ENABLE_READ == E_ENABLE_ON // If any of the drivers are enabled...
5901
-      #if EXTRUDERS > 1
5902
-        || E1_ENABLE_READ == E_ENABLE_ON
5903
-        #if HAS_X2_ENABLE
5904
-          || X2_ENABLE_READ == X_ENABLE_ON
5905
-        #endif
5906
-        #if EXTRUDERS > 2
5907
-          || E2_ENABLE_READ == E_ENABLE_ON
5908
-          #if EXTRUDERS > 3
5909
-            || E3_ENABLE_READ == E_ENABLE_ON
5907
+  void controllerFan() {
5908
+    static millis_t lastMotor = 0;      // Last time a motor was turned on
5909
+    static millis_t lastMotorCheck = 0; // Last time the state was checked
5910
+    millis_t ms = millis();
5911
+    if (ms >= lastMotorCheck + 2500) { // Not a time critical function, so we only check every 2500ms
5912
+      lastMotorCheck = ms;
5913
+      if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || soft_pwm_bed > 0
5914
+        || E0_ENABLE_READ == E_ENABLE_ON // If any of the drivers are enabled...
5915
+        #if EXTRUDERS > 1
5916
+          || E1_ENABLE_READ == E_ENABLE_ON
5917
+          #if HAS_X2_ENABLE
5918
+            || X2_ENABLE_READ == X_ENABLE_ON
5919
+          #endif
5920
+          #if EXTRUDERS > 2
5921
+            || E2_ENABLE_READ == E_ENABLE_ON
5922
+            #if EXTRUDERS > 3
5923
+              || E3_ENABLE_READ == E_ENABLE_ON
5924
+            #endif
5910
           #endif
5925
           #endif
5911
         #endif
5926
         #endif
5912
-      #endif
5913
-    ) {
5914
-      lastMotor = ms; //... set time to NOW so the fan will turn on
5927
+      ) {
5928
+        lastMotor = ms; //... set time to NOW so the fan will turn on
5929
+      }
5930
+      uint8_t speed = (lastMotor == 0 || ms >= lastMotor + (CONTROLLERFAN_SECS * 1000UL)) ? 0 : CONTROLLERFAN_SPEED;
5931
+      // allows digital or PWM fan output to be used (see M42 handling)
5932
+      digitalWrite(CONTROLLERFAN_PIN, speed);
5933
+      analogWrite(CONTROLLERFAN_PIN, speed);
5915
     }
5934
     }
5916
-    uint8_t speed = (lastMotor == 0 || ms >= lastMotor + (CONTROLLERFAN_SECS * 1000UL)) ? 0 : CONTROLLERFAN_SPEED;
5917
-    // allows digital or PWM fan output to be used (see M42 handling)
5918
-    digitalWrite(CONTROLLERFAN_PIN, speed);
5919
-    analogWrite(CONTROLLERFAN_PIN, speed);
5920
   }
5935
   }
5921
-}
5922
-#endif
5936
+
5937
+#endif // HAS_CONTROLLERFAN
5923
 
5938
 
5924
 #ifdef SCARA
5939
 #ifdef SCARA
5925
 void calculate_SCARA_forward_Transform(float f_scara[3])
5940
 void calculate_SCARA_forward_Transform(float f_scara[3])

Loading…
Cancel
Save