Browse Source

Fix FILAMENT_WIDTH_SENSOR measurement

Only measure and store filament width when E is going forward.
Scott Lahteine 8 years ago
parent
commit
865dcf3fb4
4 changed files with 31 additions and 24 deletions
  1. 1
    2
      Marlin/Marlin.h
  2. 6
    7
      Marlin/Marlin_main.cpp
  3. 23
    14
      Marlin/planner.cpp
  4. 1
    1
      Marlin/temperature.cpp

+ 1
- 2
Marlin/Marlin.h View File

@@ -341,8 +341,7 @@ extern bool axis_homed[3]; // axis[n].is_homed
341 341
   extern bool filament_sensor;  //indicates that filament sensor readings should control extrusion
342 342
   extern float filament_width_meas; //holds the filament diameter as accurately measured
343 343
   extern int8_t measurement_delay[];  //ring buffer to delay measurement
344
-  extern int delay_index1, delay_index2;  //ring buffer index. used by planner, temperature, and main code
345
-  extern float delay_dist; //delay distance counter
344
+  extern int filwidth_delay_index1, filwidth_delay_index2;  //ring buffer index. used by planner, temperature, and main code
346 345
   extern int meas_delay_cm; //delay distance
347 346
 #endif
348 347
 

+ 6
- 7
Marlin/Marlin_main.cpp View File

@@ -411,9 +411,8 @@ static uint8_t target_extruder;
411 411
   bool filament_sensor = false;  //M405 turns on filament_sensor control, M406 turns it off
412 412
   float filament_width_meas = DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter
413 413
   int8_t measurement_delay[MAX_MEASUREMENT_DELAY + 1]; //ring buffer to delay measurement  store extruder factor after subtracting 100
414
-  int delay_index1 = 0;  //index into ring buffer
415
-  int delay_index2 = -1;  //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
416
-  float delay_dist = 0; //delay distance counter
414
+  int filwidth_delay_index1 = 0;  //index into ring buffer
415
+  int filwidth_delay_index2 = -1;  //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
417 416
   int meas_delay_cm = MEASUREMENT_DELAY_CM;  //distance delay setting
418 417
 #endif
419 418
 
@@ -5428,13 +5427,13 @@ inline void gcode_M400() { st_synchronize(); }
5428 5427
     if (code_seen('D')) meas_delay_cm = code_value();
5429 5428
     NOMORE(meas_delay_cm, MAX_MEASUREMENT_DELAY);
5430 5429
 
5431
-    if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup
5430
+    if (filwidth_delay_index2 == -1) { // Initialize the ring buffer if not done since startup
5432 5431
       int temp_ratio = widthFil_to_size_ratio();
5433 5432
 
5434
-      for (delay_index1 = 0; delay_index1 < (int)COUNT(measurement_delay); ++delay_index1)
5435
-        measurement_delay[delay_index1] = temp_ratio - 100;  //subtract 100 to scale within a signed byte
5433
+      for (uint8_t i = 0; i < COUNT(measurement_delay); ++i)
5434
+        measurement_delay[i] = temp_ratio - 100;  // Subtract 100 to scale within a signed byte
5436 5435
 
5437
-      delay_index1 = delay_index2 = 0;
5436
+      filwidth_delay_index1 = filwidth_delay_index2 = 0;
5438 5437
     }
5439 5438
 
5440 5439
     filament_sensor = true;

+ 23
- 14
Marlin/planner.cpp View File

@@ -852,25 +852,34 @@ float junction_deviation = 0.1;
852 852
   block->nominal_rate = ceil(block->step_event_count * inverse_second); // (step/sec) Always > 0
853 853
 
854 854
   #if ENABLED(FILAMENT_WIDTH_SENSOR)
855
+    static float filwidth_e_count = 0, filwidth_delay_dist = 0;
856
+
855 857
     //FMM update ring buffer used for delay with filament measurements
856
-    if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && delay_index2 > -1) {  //only for extruder with filament sensor and if ring buffer is initialized
858
+    if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && filwidth_delay_index2 >= 0) {  //only for extruder with filament sensor and if ring buffer is initialized
859
+
860
+      const int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10;
861
+
862
+      // increment counters with next move in e axis
863
+      filwidth_e_count += delta_mm[E_AXIS];
864
+      filwidth_delay_dist += delta_mm[E_AXIS];
857 865
 
858
-      const int MMD = MAX_MEASUREMENT_DELAY + 1, MMD10 = MMD * 10;
866
+      // Only get new measurements on forward E movement
867
+      if (filwidth_e_count > 0.0001) {
859 868
 
860
-      delay_dist += delta_mm[E_AXIS];  // increment counter with next move in e axis
861
-      while (delay_dist >= MMD10) delay_dist -= MMD10; // loop around the buffer
862
-      while (delay_dist < 0) delay_dist += MMD10;
869
+        // Loop the delay distance counter (modulus by the mm length)
870
+        while (filwidth_delay_dist >= MMD_MM) filwidth_delay_dist -= MMD_MM;
863 871
 
864
-      delay_index1 = delay_dist / 10.0;  // calculate index
865
-      delay_index1 = constrain(delay_index1, 0, MAX_MEASUREMENT_DELAY); // (already constrained above)
872
+        // Convert into an index into the measurement array
873
+        filwidth_delay_index1 = (int)(filwidth_delay_dist / 10.0 + 0.0001);
866 874
 
867
-      if (delay_index1 != delay_index2) { // moved index
868
-        int8_t meas_sample = widthFil_to_size_ratio() - 100;  // Subtract 100 to reduce magnitude - to store in a signed char
869
-        while (delay_index1 != delay_index2) {
870
-          // Increment and loop around buffer
871
-          if (++delay_index2 >= MMD) delay_index2 -= MMD;
872
-          delay_index2 = constrain(delay_index2, 0, MAX_MEASUREMENT_DELAY);
873
-          measurement_delay[delay_index2] = meas_sample;
875
+        // If the index has changed (must have gone forward)...
876
+        if (filwidth_delay_index1 != filwidth_delay_index2) {
877
+          filwidth_e_count = 0; // Reset the E movement counter
878
+          int8_t meas_sample = widthFil_to_size_ratio() - 100; // Subtract 100 to reduce magnitude - to store in a signed char
879
+          do {
880
+            filwidth_delay_index2 = (filwidth_delay_index2 + 1) % MMD_CM; // The next unused slot
881
+            measurement_delay[filwidth_delay_index2] = meas_sample;       // Store the measurement
882
+          } while (filwidth_delay_index1 != filwidth_delay_index2);       // More slots to fill?
874 883
         }
875 884
       }
876 885
     }

+ 1
- 1
Marlin/temperature.cpp View File

@@ -705,7 +705,7 @@ void manage_heater() {
705 705
   // Control the extruder rate based on the width sensor
706 706
   #if ENABLED(FILAMENT_WIDTH_SENSOR)
707 707
     if (filament_sensor) {
708
-      meas_shift_index = delay_index1 - meas_delay_cm;
708
+      meas_shift_index = filwidth_delay_index1 - meas_delay_cm;
709 709
       if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1;  //loop around buffer if needed
710 710
 
711 711
       // Get the delayed info and add 100 to reconstitute to a percent of

Loading…
Cancel
Save