Преглед на файлове

Improvements and bug fixes in sensor delay buffer for filament sensor code

Improvement to avoid reinitializing delay buffer with every print. Fixed
issues in buffer indexing and memory out of bounds due to floating point
imprecision.  Simplified the code by avoiding conversion to standard
diameter and 1cu mm extrusion, which caused complications in determining
mm extruded.
Filip Mulier преди 10 години
родител
ревизия
62db9848d3
променени са 4 файла, в които са добавени 58 реда и са изтрити 34 реда
  1. 2
    2
      Marlin/Marlin.h
  2. 20
    18
      Marlin/Marlin_main.cpp
  3. 22
    6
      Marlin/planner.cpp
  4. 14
    8
      Marlin/temperature.cpp

+ 2
- 2
Marlin/Marlin.h Целия файл

@@ -230,8 +230,8 @@ extern unsigned char fanSpeedSoftPwm;
230 230
 #endif
231 231
 
232 232
 #ifdef FILAMENT_SENSOR
233
-  extern volatile float filament_width_nominal;  //holds the theoretical filament diameter ie., 3.00 or 1.75 
234
-  extern volatile bool filament_sensor;  //indicates that filament sensor readings should control extrusion  
233
+  extern float filament_width_nominal;  //holds the theoretical filament diameter ie., 3.00 or 1.75 
234
+  extern bool filament_sensor;  //indicates that filament sensor readings should control extrusion  
235 235
   extern float filament_width_meas; //holds the filament diameter as accurately measured 
236 236
   extern signed char measurement_delay[];  //ring buffer to delay measurement
237 237
   extern int delay_index1, delay_index2;  //index into ring buffer

+ 20
- 18
Marlin/Marlin_main.cpp Целия файл

@@ -302,12 +302,12 @@ bool cancel_heatup = false ;
302 302
 
303 303
 #ifdef FILAMENT_SENSOR
304 304
   //Variables for Filament Sensor input 
305
-  volatile float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA;  //Set nominal filament width, can be changed with M404 
306
-  volatile bool filament_sensor=false;  //M405 turns on filament_sensor control, M406 turns it off 
305
+  float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA;  //Set nominal filament width, can be changed with M404 
306
+  bool filament_sensor=false;  //M405 turns on filament_sensor control, M406 turns it off 
307 307
   float filament_width_meas=DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter 
308 308
   signed char measurement_delay[MAX_MEASUREMENT_DELAY+1];  //ring buffer to delay measurement  store extruder factor after subtracting 100 
309
-  int delay_index1=0;
310
-  int delay_index2=0;  //index into ring buffer
309
+  int delay_index1=0;  //index into ring buffer
310
+  int delay_index2=-1;  //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
311 311
   float delay_dist=0; //delay distance counter  
312 312
   int meas_delay_cm = MEASUREMENT_DELAY_CM;  //distance delay setting
313 313
 #endif
@@ -504,6 +504,7 @@ void servo_init()
504 504
   #endif
505 505
 }
506 506
 
507
+
507 508
 void setup()
508 509
 {
509 510
   setup_killpin();
@@ -553,6 +554,7 @@ void setup()
553 554
   st_init();    // Initialize stepper, this enables interrupts!
554 555
   setup_photpin();
555 556
   servo_init();
557
+  
556 558
 
557 559
   lcd_init();
558 560
   _delay_ms(1000);	// wait 1sec to display the splash screen
@@ -2333,12 +2335,8 @@ void process_commands()
2333 2335
           }
2334 2336
         } else {
2335 2337
           //reserved for setting filament diameter via UFID or filament measuring device
2336
-        	if(active_extruder == FILAMENT_SENSOR_EXTRUDER_NUM){
2337
-        		radius = analog2widthFil() * 0.5;
2338
-        		area = M_PI * pow(radius, 2);
2339
-        	}else{
2340
-        		area = 1.0;
2341
-        	}
2338
+          break;
2339
+        
2342 2340
           
2343 2341
         }
2344 2342
         tmp_extruder = active_extruder;
@@ -2816,15 +2814,19 @@ case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp
2816 2814
        if(meas_delay_cm> MAX_MEASUREMENT_DELAY)
2817 2815
        	meas_delay_cm = MAX_MEASUREMENT_DELAY;
2818 2816
     
2817
+       if(delay_index2 == -1)  //initialize the ring buffer if it has not been done since startup
2818
+    	   {
2819
+    	   int temp_ratio = widthFil_to_size_ratio(); 
2820
+       	    
2821
+       	    for (delay_index1=0; delay_index1<(MAX_MEASUREMENT_DELAY+1); ++delay_index1 ){
2822
+       	              measurement_delay[delay_index1]=temp_ratio-100;  //subtract 100 to scale within a signed byte
2823
+       	        }
2824
+       	    delay_index1=0;
2825
+       	    delay_index2=0;	
2826
+    	   }
2827
+    
2819 2828
     filament_sensor = true ; 
2820
-    int temp_ratio = widthFil_to_size_ratio(); 
2821 2829
     
2822
-    for (delay_index1=0; delay_index1<(MAX_MEASUREMENT_DELAY+1); ++delay_index1 ){
2823
-              measurement_delay[delay_index1]=temp_ratio-100;  //subtract 100 to scale within a signed byte
2824
-        }
2825
-    delay_index1=0;
2826
-    delay_index2=0;
2827
-              
2828 2830
     //SERIAL_PROTOCOLPGM("Filament dia (measured mm):"); 
2829 2831
     //SERIAL_PROTOCOL(filament_width_meas); 
2830 2832
     //SERIAL_PROTOCOLPGM("Extrusion ratio(%):"); 
@@ -2841,7 +2843,7 @@ case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp
2841 2843
     case 407:   //M407 Display measured filament diameter 
2842 2844
     { 
2843 2845
      
2844
-    filament_width_meas = code_value(); 
2846
+    
2845 2847
     
2846 2848
     SERIAL_PROTOCOLPGM("Filament dia (measured mm):"); 
2847 2849
     SERIAL_PROTOCOLLN(filament_width_meas);   

+ 22
- 6
Marlin/planner.cpp Целия файл

@@ -743,15 +743,25 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
743 743
 
744 744
 #ifdef FILAMENT_SENSOR
745 745
   //FMM update ring buffer used for delay with filament measurements
746
-    
747
-    if(filament_sensor && (extruder==0))  //only for extruder 0
746
+  
747
+  
748
+    if((extruder==FILAMENT_SENSOR_EXTRUDER_NUM) && (delay_index2 > -1))  //only for extruder with filament sensor and if ring buffer is initialized
748 749
   	  {
749 750
     delay_dist = delay_dist + delta_mm[E_AXIS];  //increment counter with next move in e axis
750
-    if (delay_dist> (10*(MAX_MEASUREMENT_DELAY+1)))  //check if counter is over max buffer size in mm
751
+  
752
+    while (delay_dist >= (10*(MAX_MEASUREMENT_DELAY+1)))  //check if counter is over max buffer size in mm
751 753
       	  delay_dist = delay_dist - 10*(MAX_MEASUREMENT_DELAY+1);  //loop around the buffer
752
-      if(delay_dist<0)
754
+    while (delay_dist<0)
753 755
     	  delay_dist = delay_dist + 10*(MAX_MEASUREMENT_DELAY+1); //loop around the buffer
754
-    delay_index1=delay_dist/10;  //calculate index
756
+      
757
+    delay_index1=delay_dist/10.0;  //calculate index
758
+    
759
+    //ensure the number is within range of the array after converting from floating point
760
+    if(delay_index1<0)
761
+    	delay_index1=0;
762
+    else if (delay_index1>MAX_MEASUREMENT_DELAY)
763
+    	delay_index1=MAX_MEASUREMENT_DELAY;
764
+    	
755 765
     if(delay_index1 != delay_index2)  //moved index
756 766
   	  {
757 767
     	meas_sample=widthFil_to_size_ratio()-100;  //subtract off 100 to reduce magnitude - to store in a signed char
@@ -761,10 +771,16 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
761 771
   	  delay_index2 = delay_index2 + 1;
762 772
   	if(delay_index2>MAX_MEASUREMENT_DELAY)
763 773
   			  delay_index2=delay_index2-(MAX_MEASUREMENT_DELAY+1);  //loop around buffer when incrementing
774
+  	  if(delay_index2<0)
775
+  		delay_index2=0;
776
+  	  else if (delay_index2>MAX_MEASUREMENT_DELAY)
777
+  		delay_index2=MAX_MEASUREMENT_DELAY;  
778
+  	  
764 779
   	  measurement_delay[delay_index2]=meas_sample;
765 780
   	  }
781
+    	
766 782
     
767
-  	  } 
783
+  	  }
768 784
 #endif
769 785
 
770 786
 

+ 14
- 8
Marlin/temperature.cpp Целия файл

@@ -617,9 +617,16 @@ void manage_heater()
617 617
 			  meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1);  //loop around buffer if needed
618 618
 		  
619 619
 		  //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
620
-		  //then adjust as a factor to the Standard Diameter (has an area of 1mm squared)
621 620
 		  //then square it to get an area
622
-		  volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/filament_width_nominal*STANDARD_DIA/100.0,2);
621
+		  
622
+		  if(meas_shift_index<0)
623
+			  meas_shift_index=0;
624
+		  else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
625
+			  meas_shift_index=MAX_MEASUREMENT_DELAY;
626
+		  
627
+		     volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
628
+		     if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
629
+		    	 volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
623 630
 	}
624 631
 #endif
625 632
 }
@@ -715,6 +722,9 @@ static void updateTemperaturesFromRawValues()
715 722
     #ifdef TEMP_SENSOR_1_AS_REDUNDANT
716 723
       redundant_temperature = analog2temp(redundant_temperature_raw, 1);
717 724
     #endif
725
+    #ifdef FILAMENT_SENSOR  && (FILWIDTH_PIN > -1)    //check if a sensor is supported 
726
+      filament_width_meas = analog2widthFil();
727
+    #endif  
718 728
     //Reset the watchdog after we know we have a temperature measurement.
719 729
     watchdog_reset();
720 730
 
@@ -731,15 +741,11 @@ return current_raw_filwidth/16383.0*5.0;
731 741
 //return current_raw_filwidth; 
732 742
 } 
733 743
  
734
-// For converting raw Filament Width to an volumetric ratio 
744
+// For converting raw Filament Width to a ratio 
735 745
 int widthFil_to_size_ratio() { 
736 746
  
737 747
 float temp; 
738
-   
739
-#if (FILWIDTH_PIN > -1)    //check if a sensor is supported 
740
-filament_width_meas=current_raw_filwidth/16383.0*5.0; 
741
-#endif   
742
- 
748
+      
743 749
 temp=filament_width_meas;
744 750
 if(filament_width_meas<MEASURED_LOWER_LIMIT)
745 751
 	temp=filament_width_nominal;  //assume sensor cut out

Loading…
Отказ
Запис