Bläddra i källkod

Merge pull request #1037 from filipmu/Filament-Sensor

Support for a filament diameter sensor
Erik van der Zalm 10 år sedan
förälder
incheckning
058e446531
8 ändrade filer med 316 tillägg och 7 borttagningar
  1. 30
    1
      Marlin/Configuration.h
  2. 10
    0
      Marlin/Marlin.h
  3. 84
    0
      Marlin/Marlin_main.cpp
  4. 16
    0
      Marlin/pins.h
  5. 47
    0
      Marlin/planner.cpp
  6. 117
    6
      Marlin/temperature.cpp
  7. 8
    0
      Marlin/temperature.h
  8. 4
    0
      README.md

+ 30
- 1
Marlin/Configuration.h Visa fil

193
                                   // is more then PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max.
193
                                   // is more then PID_FUNCTIONAL_RANGE then the PID will be shut off and the heater will be set to min/max.
194
   #define PID_INTEGRAL_DRIVE_MAX 255  //limit for the integral term
194
   #define PID_INTEGRAL_DRIVE_MAX 255  //limit for the integral term
195
   #define K1 0.95 //smoothing factor within the PID
195
   #define K1 0.95 //smoothing factor within the PID
196
-  #define PID_dT ((OVERSAMPLENR * 8.0)/(F_CPU / 64.0 / 256.0)) //sampling period of the temperature routine
196
+  #define PID_dT ((OVERSAMPLENR * 10.0)/(F_CPU / 64.0 / 256.0)) //sampling period of the temperature routine
197
 
197
 
198
 // If you are using a pre-configured hotend then you can use one of the value sets by uncommenting it
198
 // If you are using a pre-configured hotend then you can use one of the value sets by uncommenting it
199
 // Ultimaker
199
 // Ultimaker
766
 //#define SERVO_ENDSTOPS {-1, -1, 0} // Servo index for X, Y, Z. Disable with -1
766
 //#define SERVO_ENDSTOPS {-1, -1, 0} // Servo index for X, Y, Z. Disable with -1
767
 //#define SERVO_ENDSTOP_ANGLES {0,0, 0,0, 70,0} // X,Y,Z Axis Extend and Retract angles
767
 //#define SERVO_ENDSTOP_ANGLES {0,0, 0,0, 70,0} // X,Y,Z Axis Extend and Retract angles
768
 
768
 
769
+/**********************************************************************\
770
+ * Support for a filament diameter sensor
771
+ * Also allows adjustment of diameter at print time (vs  at slicing)
772
+ * Single extruder only at this point (extruder 0)
773
+ * 
774
+ * Motherboards
775
+ * 34 - RAMPS1.4 - uses Analog input 5 on the AUX2 connector 
776
+ * 81 - Printrboard - Uses Analog input 2 on the Aux 2 connector
777
+ * 301 - Rambo  - uses Analog input 3
778
+ * Note may require analog pins to be defined for different motherboards
779
+ **********************************************************************/
780
+#define FILAMENT_SENSOR
781
+#define FILAMENT_SENSOR_EXTRUDER_NUM	0  //The number of the extruder that has the filament sensor (0,1,2)
782
+#define MEASUREMENT_DELAY_CM			14  //measurement delay in cm.  This is the distance from filament sensor to middle of barrel
783
+
784
+#define DEFAULT_NOMINAL_FILAMENT_DIA  3.0  //Enter the diameter (in mm) of the filament generally used (3.0 mm or 1.75 mm) - this is then used in the slicer software.  Used for sensor reading validation
785
+#define MEASURED_UPPER_LIMIT          3.30  //upper limit factor used for sensor reading validation in mm
786
+#define MEASURED_LOWER_LIMIT          1.90  //lower limit factor for sensor reading validation in mm
787
+#define MAX_MEASUREMENT_DELAY			20  //delay buffer size in bytes (1 byte = 1cm)- limits maximum measurement delay allowable (must be larger than MEASUREMENT_DELAY_CM  and lower number saves RAM)
788
+
789
+//defines used in the code
790
+#define DEFAULT_MEASURED_FILAMENT_DIA  DEFAULT_NOMINAL_FILAMENT_DIA  //set measured to nominal initially 
791
+
792
+
793
+
794
+
795
+
796
+
797
+
769
 #include "Configuration_adv.h"
798
 #include "Configuration_adv.h"
770
 #include "thermistortables.h"
799
 #include "thermistortables.h"
771
 
800
 

+ 10
- 0
Marlin/Marlin.h Visa fil

236
 extern unsigned char fanSpeedSoftPwm;
236
 extern unsigned char fanSpeedSoftPwm;
237
 #endif
237
 #endif
238
 
238
 
239
+#ifdef FILAMENT_SENSOR
240
+  extern float filament_width_nominal;  //holds the theoretical filament diameter ie., 3.00 or 1.75 
241
+  extern bool filament_sensor;  //indicates that filament sensor readings should control extrusion  
242
+  extern float filament_width_meas; //holds the filament diameter as accurately measured 
243
+  extern signed char measurement_delay[];  //ring buffer to delay measurement
244
+  extern int delay_index1, delay_index2;  //index into ring buffer
245
+  extern float delay_dist; //delay distance counter
246
+  extern int meas_delay_cm; //delay distance
247
+#endif
248
+
239
 #ifdef FWRETRACT
249
 #ifdef FWRETRACT
240
 extern bool autoretract_enabled;
250
 extern bool autoretract_enabled;
241
 extern bool retracted[EXTRUDERS];
251
 extern bool retracted[EXTRUDERS];

+ 84
- 0
Marlin/Marlin_main.cpp Visa fil

159
 // M400 - Finish all moves
159
 // M400 - Finish all moves
160
 // M401 - Lower z-probe if present
160
 // M401 - Lower z-probe if present
161
 // M402 - Raise z-probe if present
161
 // M402 - Raise z-probe if present
162
+// M404 - N<dia in mm> Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
163
+// M405 - Turn on Filament Sensor extrusion control.  Optional D<delay in cm> to set delay in centimeters between sensor and extruder 
164
+// M406 - Turn off Filament Sensor extrusion control 
165
+// M407 - Displays measured filament diameter 
162
 // M500 - stores parameters in EEPROM
166
 // M500 - stores parameters in EEPROM
163
 // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
167
 // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
164
 // M502 - reverts to the default "factory settings".  You still need to store them in EEPROM afterwards if you want to.
168
 // M502 - reverts to the default "factory settings".  You still need to store them in EEPROM afterwards if you want to.
313
 
317
 
314
 bool cancel_heatup = false ;
318
 bool cancel_heatup = false ;
315
 
319
 
320
+#ifdef FILAMENT_SENSOR
321
+  //Variables for Filament Sensor input 
322
+  float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA;  //Set nominal filament width, can be changed with M404 
323
+  bool filament_sensor=false;  //M405 turns on filament_sensor control, M406 turns it off 
324
+  float filament_width_meas=DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter 
325
+  signed char measurement_delay[MAX_MEASUREMENT_DELAY+1];  //ring buffer to delay measurement  store extruder factor after subtracting 100 
326
+  int delay_index1=0;  //index into ring buffer
327
+  int delay_index2=-1;  //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized
328
+  float delay_dist=0; //delay distance counter  
329
+  int meas_delay_cm = MEASUREMENT_DELAY_CM;  //distance delay setting
330
+#endif
331
+
316
 //===========================================================================
332
 //===========================================================================
317
 //=============================Private Variables=============================
333
 //=============================Private Variables=============================
318
 //===========================================================================
334
 //===========================================================================
510
   #endif
526
   #endif
511
 }
527
 }
512
 
528
 
529
+
513
 void setup()
530
 void setup()
514
 {
531
 {
515
   setup_killpin();
532
   setup_killpin();
559
   st_init();    // Initialize stepper, this enables interrupts!
576
   st_init();    // Initialize stepper, this enables interrupts!
560
   setup_photpin();
577
   setup_photpin();
561
   servo_init();
578
   servo_init();
579
+  
562
 
580
 
563
   lcd_init();
581
   lcd_init();
564
   _delay_ms(1000);	// wait 1sec to display the splash screen
582
   _delay_ms(1000);	// wait 1sec to display the splash screen
2780
         } else {
2798
         } else {
2781
           //reserved for setting filament diameter via UFID or filament measuring device
2799
           //reserved for setting filament diameter via UFID or filament measuring device
2782
           break;
2800
           break;
2801
+        
2802
+          
2783
         }
2803
         }
2784
         tmp_extruder = active_extruder;
2804
         tmp_extruder = active_extruder;
2785
         if(code_seen('T')) {
2805
         if(code_seen('T')) {
3342
     }
3362
     }
3343
     break;
3363
     break;
3344
 #endif
3364
 #endif
3365
+
3366
+#ifdef FILAMENT_SENSOR
3367
+case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or display nominal filament width 
3368
+    {
3369
+    #if (FILWIDTH_PIN > -1) 
3370
+    if(code_seen('N')) filament_width_nominal=code_value();
3371
+    else{
3372
+    SERIAL_PROTOCOLPGM("Filament dia (nominal mm):"); 
3373
+    SERIAL_PROTOCOLLN(filament_width_nominal); 
3374
+    }
3375
+    #endif
3376
+    }
3377
+    break; 
3378
+    
3379
+    case 405:  //M405 Turn on filament sensor for control 
3380
+    {
3381
+    
3382
+    
3383
+    if(code_seen('D')) meas_delay_cm=code_value();
3384
+       
3385
+       if(meas_delay_cm> MAX_MEASUREMENT_DELAY)
3386
+       	meas_delay_cm = MAX_MEASUREMENT_DELAY;
3387
+    
3388
+       if(delay_index2 == -1)  //initialize the ring buffer if it has not been done since startup
3389
+    	   {
3390
+    	   int temp_ratio = widthFil_to_size_ratio(); 
3391
+       	    
3392
+       	    for (delay_index1=0; delay_index1<(MAX_MEASUREMENT_DELAY+1); ++delay_index1 ){
3393
+       	              measurement_delay[delay_index1]=temp_ratio-100;  //subtract 100 to scale within a signed byte
3394
+       	        }
3395
+       	    delay_index1=0;
3396
+       	    delay_index2=0;	
3397
+    	   }
3398
+    
3399
+    filament_sensor = true ; 
3400
+    
3401
+    //SERIAL_PROTOCOLPGM("Filament dia (measured mm):"); 
3402
+    //SERIAL_PROTOCOL(filament_width_meas); 
3403
+    //SERIAL_PROTOCOLPGM("Extrusion ratio(%):"); 
3404
+    //SERIAL_PROTOCOL(extrudemultiply); 
3405
+    } 
3406
+    break; 
3407
+    
3408
+    case 406:  //M406 Turn off filament sensor for control 
3409
+    {      
3410
+    filament_sensor = false ; 
3411
+    } 
3412
+    break; 
3413
+  
3414
+    case 407:   //M407 Display measured filament diameter 
3415
+    { 
3416
+     
3417
+    
3418
+    
3419
+    SERIAL_PROTOCOLPGM("Filament dia (measured mm):"); 
3420
+    SERIAL_PROTOCOLLN(filament_width_meas);   
3421
+    } 
3422
+    break; 
3423
+    #endif
3424
+    
3425
+
3426
+
3427
+
3428
+
3345
     case 500: // M500 Store settings in EEPROM
3429
     case 500: // M500 Store settings in EEPROM
3346
     {
3430
     {
3347
         Config_StoreSettings();
3431
         Config_StoreSettings();

+ 16
- 0
Marlin/pins.h Visa fil

628
     #define E1_DIR_PIN         34
628
     #define E1_DIR_PIN         34
629
     #define E1_ENABLE_PIN      30
629
     #define E1_ENABLE_PIN      30
630
 
630
 
631
+#if MOTHERBOARD == 34  //FMM added for Filament Extruder
632
+#ifdef FILAMENT_SENSOR
633
+	  //define analog pin for the filament width sensor input
634
+	  //Use the RAMPS 1.4 Analog input 5 on the AUX2 connector
635
+      #define FILWIDTH_PIN        5
636
+#endif
637
+#endif
638
+
639
+
631
     #if MOTHERBOARD == 68
640
     #if MOTHERBOARD == 68
632
       #define E2_STEP_PIN        23
641
       #define E2_STEP_PIN        23
633
       #define E2_DIR_PIN         25
642
       #define E2_DIR_PIN         25
1762
   #define Z_STOP_PIN         36
1771
   #define Z_STOP_PIN         36
1763
   #define TEMP_0_PIN          1  // Extruder / Analog pin numbering
1772
   #define TEMP_0_PIN          1  // Extruder / Analog pin numbering
1764
   #define TEMP_BED_PIN        0  // Bed / Analog pin numbering
1773
   #define TEMP_BED_PIN        0  // Bed / Analog pin numbering
1774
+  #ifdef FILAMENT_SENSOR
1775
+   #define FILWIDTH_PIN        2
1776
+  #endif //FILAMENT_SENSOR
1765
 #endif
1777
 #endif
1766
 
1778
 
1767
 #define TEMP_1_PIN         -1
1779
 #define TEMP_1_PIN         -1
2396
   #endif
2408
   #endif
2397
 #endif //ULTRA_LCD
2409
 #endif //ULTRA_LCD
2398
 
2410
 
2411
+#ifdef FILAMENT_SENSOR
2412
+  //Filip added pin for Filament sensor analog input 
2413
+  #define FILWIDTH_PIN        3
2414
+#endif //FILAMENT_SENSOR
2399
 
2415
 
2400
 #endif
2416
 #endif
2401
 
2417
 

+ 47
- 0
Marlin/planner.cpp Visa fil

119
 static long y_segment_time[3]={MAX_FREQ_TIME + 1,0,0};
119
 static long y_segment_time[3]={MAX_FREQ_TIME + 1,0,0};
120
 #endif
120
 #endif
121
 
121
 
122
+#ifdef FILAMENT_SENSOR
123
+ static char meas_sample; //temporary variable to hold filament measurement sample
124
+#endif
125
+
122
 // Returns the index of the next block in the ring buffer
126
 // Returns the index of the next block in the ring buffer
123
 // NOTE: Removed modulo (%) operator, which uses an expensive divide and multiplication.
127
 // NOTE: Removed modulo (%) operator, which uses an expensive divide and multiplication.
124
 static int8_t next_block_index(int8_t block_index) {
128
 static int8_t next_block_index(int8_t block_index) {
762
   block->nominal_speed = block->millimeters * inverse_second; // (mm/sec) Always > 0
766
   block->nominal_speed = block->millimeters * inverse_second; // (mm/sec) Always > 0
763
   block->nominal_rate = ceil(block->step_event_count * inverse_second); // (step/sec) Always > 0
767
   block->nominal_rate = ceil(block->step_event_count * inverse_second); // (step/sec) Always > 0
764
 
768
 
769
+#ifdef FILAMENT_SENSOR
770
+  //FMM update ring buffer used for delay with filament measurements
771
+  
772
+  
773
+    if((extruder==FILAMENT_SENSOR_EXTRUDER_NUM) && (delay_index2 > -1))  //only for extruder with filament sensor and if ring buffer is initialized
774
+  	  {
775
+    delay_dist = delay_dist + delta_mm[E_AXIS];  //increment counter with next move in e axis
776
+  
777
+    while (delay_dist >= (10*(MAX_MEASUREMENT_DELAY+1)))  //check if counter is over max buffer size in mm
778
+      	  delay_dist = delay_dist - 10*(MAX_MEASUREMENT_DELAY+1);  //loop around the buffer
779
+    while (delay_dist<0)
780
+    	  delay_dist = delay_dist + 10*(MAX_MEASUREMENT_DELAY+1); //loop around the buffer
781
+      
782
+    delay_index1=delay_dist/10.0;  //calculate index
783
+    
784
+    //ensure the number is within range of the array after converting from floating point
785
+    if(delay_index1<0)
786
+    	delay_index1=0;
787
+    else if (delay_index1>MAX_MEASUREMENT_DELAY)
788
+    	delay_index1=MAX_MEASUREMENT_DELAY;
789
+    	
790
+    if(delay_index1 != delay_index2)  //moved index
791
+  	  {
792
+    	meas_sample=widthFil_to_size_ratio()-100;  //subtract off 100 to reduce magnitude - to store in a signed char
793
+  	  }
794
+    while( delay_index1 != delay_index2)
795
+  	  {
796
+  	  delay_index2 = delay_index2 + 1;
797
+  	if(delay_index2>MAX_MEASUREMENT_DELAY)
798
+  			  delay_index2=delay_index2-(MAX_MEASUREMENT_DELAY+1);  //loop around buffer when incrementing
799
+  	  if(delay_index2<0)
800
+  		delay_index2=0;
801
+  	  else if (delay_index2>MAX_MEASUREMENT_DELAY)
802
+  		delay_index2=MAX_MEASUREMENT_DELAY;  
803
+  	  
804
+  	  measurement_delay[delay_index2]=meas_sample;
805
+  	  }
806
+    	
807
+    
808
+  	  }
809
+#endif
810
+
811
+
765
   // Calculate and limit speed in mm/sec for each axis
812
   // Calculate and limit speed in mm/sec for each axis
766
   float current_speed[4];
813
   float current_speed[4];
767
   float speed_factor = 1.0; //factor <=1 do decrease speed
814
   float speed_factor = 1.0; //factor <=1 do decrease speed

+ 117
- 6
Marlin/temperature.cpp Visa fil

74
 #ifdef BABYSTEPPING
74
 #ifdef BABYSTEPPING
75
   volatile int babystepsTodo[3]={0,0,0};
75
   volatile int babystepsTodo[3]={0,0,0};
76
 #endif
76
 #endif
77
-  
77
+
78
+#ifdef FILAMENT_SENSOR
79
+  int current_raw_filwidth = 0;  //Holds measured filament diameter - one extruder only
80
+#endif  
78
 //===========================================================================
81
 //===========================================================================
79
 //=============================private variables============================
82
 //=============================private variables============================
80
 //===========================================================================
83
 //===========================================================================
161
 #define SOFT_PWM_SCALE 0
164
 #define SOFT_PWM_SCALE 0
162
 #endif
165
 #endif
163
 
166
 
167
+#ifdef FILAMENT_SENSOR
168
+  static int meas_shift_index;  //used to point to a delayed sample in buffer for filament width sensor
169
+#endif
164
 //===========================================================================
170
 //===========================================================================
165
 //=============================   functions      ============================
171
 //=============================   functions      ============================
166
 //===========================================================================
172
 //===========================================================================
604
       }
610
       }
605
     #endif
611
     #endif
606
   #endif
612
   #endif
613
+  
614
+//code for controlling the extruder rate based on the width sensor 
615
+#ifdef FILAMENT_SENSOR
616
+  if(filament_sensor) 
617
+	{
618
+	meas_shift_index=delay_index1-meas_delay_cm;
619
+		  if(meas_shift_index<0)
620
+			  meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1);  //loop around buffer if needed
621
+		  
622
+		  //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
623
+		  //then square it to get an area
624
+		  
625
+		  if(meas_shift_index<0)
626
+			  meas_shift_index=0;
627
+		  else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
628
+			  meas_shift_index=MAX_MEASUREMENT_DELAY;
629
+		  
630
+		     volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
631
+		     if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
632
+		    	 volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
633
+	}
634
+#endif
607
 }
635
 }
608
 
636
 
609
 #define PGM_RD_W(x)   (short)pgm_read_word(&x)
637
 #define PGM_RD_W(x)   (short)pgm_read_word(&x)
697
     #ifdef TEMP_SENSOR_1_AS_REDUNDANT
725
     #ifdef TEMP_SENSOR_1_AS_REDUNDANT
698
       redundant_temperature = analog2temp(redundant_temperature_raw, 1);
726
       redundant_temperature = analog2temp(redundant_temperature_raw, 1);
699
     #endif
727
     #endif
728
+    #ifdef FILAMENT_SENSOR  && (FILWIDTH_PIN > -1)    //check if a sensor is supported 
729
+      filament_width_meas = analog2widthFil();
730
+    #endif  
700
     //Reset the watchdog after we know we have a temperature measurement.
731
     //Reset the watchdog after we know we have a temperature measurement.
701
     watchdog_reset();
732
     watchdog_reset();
702
 
733
 
705
     CRITICAL_SECTION_END;
736
     CRITICAL_SECTION_END;
706
 }
737
 }
707
 
738
 
739
+
740
+// For converting raw Filament Width to milimeters 
741
+#ifdef FILAMENT_SENSOR
742
+float analog2widthFil() { 
743
+return current_raw_filwidth/16383.0*5.0; 
744
+//return current_raw_filwidth; 
745
+} 
746
+ 
747
+// For converting raw Filament Width to a ratio 
748
+int widthFil_to_size_ratio() { 
749
+ 
750
+float temp; 
751
+      
752
+temp=filament_width_meas;
753
+if(filament_width_meas<MEASURED_LOWER_LIMIT)
754
+	temp=filament_width_nominal;  //assume sensor cut out
755
+else if (filament_width_meas>MEASURED_UPPER_LIMIT)
756
+	temp= MEASURED_UPPER_LIMIT;
757
+
758
+
759
+return(filament_width_nominal/temp*100); 
760
+
761
+
762
+} 
763
+#endif
764
+
765
+
766
+
767
+
768
+
708
 void tp_init()
769
 void tp_init()
709
 {
770
 {
710
 #if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
771
 #if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
804
     #endif
865
     #endif
805
   #endif
866
   #endif
806
   
867
   
868
+  //Added for Filament Sensor 
869
+  #ifdef FILAMENT_SENSOR
870
+   #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1) 
871
+	#if FILWIDTH_PIN < 8 
872
+       	   DIDR0 |= 1<<FILWIDTH_PIN;  
873
+	#else 
874
+       	   DIDR2 |= 1<<(FILWIDTH_PIN - 8);  
875
+	#endif 
876
+   #endif
877
+  #endif
878
+  
807
   // Use timer0 for temperature measurement
879
   // Use timer0 for temperature measurement
808
   // Interleave temperature interrupt with millies interrupt
880
   // Interleave temperature interrupt with millies interrupt
809
   OCR0B = 128;
881
   OCR0B = 128;
1116
   static unsigned long raw_temp_1_value = 0;
1188
   static unsigned long raw_temp_1_value = 0;
1117
   static unsigned long raw_temp_2_value = 0;
1189
   static unsigned long raw_temp_2_value = 0;
1118
   static unsigned long raw_temp_bed_value = 0;
1190
   static unsigned long raw_temp_bed_value = 0;
1119
-  static unsigned char temp_state = 8;
1191
+  static unsigned char temp_state = 10;
1120
   static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
1192
   static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
1121
   static unsigned char soft_pwm_0;
1193
   static unsigned char soft_pwm_0;
1122
   #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
1194
   #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
1129
   static unsigned char soft_pwm_b;
1201
   static unsigned char soft_pwm_b;
1130
   #endif
1202
   #endif
1131
   
1203
   
1204
+  #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
1205
+   static unsigned long raw_filwidth_value = 0;  //added for filament width sensor
1206
+  #endif
1207
+  
1132
   if(pwm_count == 0){
1208
   if(pwm_count == 0){
1133
     soft_pwm_0 = soft_pwm[0];
1209
     soft_pwm_0 = soft_pwm[0];
1134
     if(soft_pwm_0 > 0) { 
1210
     if(soft_pwm_0 > 0) { 
1255
       #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
1331
       #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
1256
         raw_temp_2_value += ADC;
1332
         raw_temp_2_value += ADC;
1257
       #endif
1333
       #endif
1258
-      temp_state = 0;
1259
-      temp_count++;
1334
+      temp_state = 8;//change so that Filament Width is also measured
1335
+      
1260
       break;
1336
       break;
1261
-    case 8: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
1337
+    case 8: //Prepare FILWIDTH 
1338
+     #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1) 
1339
+      #if FILWIDTH_PIN>7 
1340
+         ADCSRB = 1<<MUX5;
1341
+      #else
1342
+         ADCSRB = 0; 
1343
+      #endif 
1344
+      ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07)); 
1345
+      ADCSRA |= 1<<ADSC; // Start conversion 
1346
+     #endif 
1347
+     lcd_buttons_update();       
1348
+     temp_state = 9; 
1349
+     break; 
1350
+    case 9:   //Measure FILWIDTH 
1351
+     #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1) 
1352
+     //raw_filwidth_value += ADC;  //remove to use an IIR filter approach 
1353
+      if(ADC>102)  //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
1354
+        {
1355
+    	raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7);  //multipliy raw_filwidth_value by 127/128
1356
+        
1357
+        raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7);  //add new ADC reading 
1358
+        }
1359
+     #endif 
1360
+     temp_state = 0;   
1361
+      
1362
+     temp_count++;
1363
+     break;      
1364
+      
1365
+      
1366
+    case 10: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
1262
       temp_state = 0;
1367
       temp_state = 0;
1263
       break;
1368
       break;
1264
 //    default:
1369
 //    default:
1267
 //      break;
1372
 //      break;
1268
   }
1373
   }
1269
     
1374
     
1270
-  if(temp_count >= OVERSAMPLENR) // 8 * 16 * 1/(16000000/64/256)  = 131ms.
1375
+  if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256)  = 164ms.
1271
   {
1376
   {
1272
     if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
1377
     if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
1273
     {
1378
     {
1283
 #endif
1388
 #endif
1284
       current_temperature_bed_raw = raw_temp_bed_value;
1389
       current_temperature_bed_raw = raw_temp_bed_value;
1285
     }
1390
     }
1391
+
1392
+//Add similar code for Filament Sensor - can be read any time since IIR filtering is used 
1393
+#if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
1394
+  current_raw_filwidth = raw_filwidth_value>>10;  //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach 
1395
+#endif
1396
+    
1286
     
1397
     
1287
     temp_meas_ready = true;
1398
     temp_meas_ready = true;
1288
     temp_count = 0;
1399
     temp_count = 0;

+ 8
- 0
Marlin/temperature.h Visa fil

31
 void tp_init();  //initialize the heating
31
 void tp_init();  //initialize the heating
32
 void manage_heater(); //it is critical that this is called periodically.
32
 void manage_heater(); //it is critical that this is called periodically.
33
 
33
 
34
+#ifdef FILAMENT_SENSOR
35
+// For converting raw Filament Width to milimeters 
36
+ float analog2widthFil(); 
37
+ 
38
+// For converting raw Filament Width to an extrusion ratio 
39
+ int widthFil_to_size_ratio();
40
+#endif
41
+
34
 // low level conversion routines
42
 // low level conversion routines
35
 // do not use these routines and variables outside of temperature.cpp
43
 // do not use these routines and variables outside of temperature.cpp
36
 extern int target_temperature[EXTRUDERS];  
44
 extern int target_temperature[EXTRUDERS];  

+ 4
- 0
README.md Visa fil

233
 *  M400 - Finish all moves
233
 *  M400 - Finish all moves
234
 *  M401 - Lower z-probe if present
234
 *  M401 - Lower z-probe if present
235
 *  M402 - Raise z-probe if present
235
 *  M402 - Raise z-probe if present
236
+*  M404 - N<dia in mm> Enter the nominal filament width (3mm, 1.75mm ) or will display nominal filament width without parameters
237
+*  M405 - Turn on Filament Sensor extrusion control.  Optional D<delay in cm> to set delay in centimeters between sensor and extruder
238
+*  M406 - Turn off Filament Sensor extrusion control
239
+*  M407 - Displays measured filament diameter
236
 *  M500 - stores paramters in EEPROM
240
 *  M500 - stores paramters in EEPROM
237
 *  M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
241
 *  M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
238
 *  M502 - reverts to the default "factory settings".  You still need to store them in EEPROM afterwards if you want to.
242
 *  M502 - reverts to the default "factory settings".  You still need to store them in EEPROM afterwards if you want to.

Laddar…
Avbryt
Spara