|
@@ -74,7 +74,10 @@ unsigned char soft_pwm_bed;
|
74
|
74
|
#ifdef BABYSTEPPING
|
75
|
75
|
volatile int babystepsTodo[3]={0,0,0};
|
76
|
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
|
82
|
//=============================private variables============================
|
80
|
83
|
//===========================================================================
|
|
@@ -161,6 +164,9 @@ unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
|
161
|
164
|
#define SOFT_PWM_SCALE 0
|
162
|
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
|
171
|
//============================= functions ============================
|
166
|
172
|
//===========================================================================
|
|
@@ -604,6 +610,28 @@ void manage_heater()
|
604
|
610
|
}
|
605
|
611
|
#endif
|
606
|
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
|
637
|
#define PGM_RD_W(x) (short)pgm_read_word(&x)
|
|
@@ -697,6 +725,9 @@ static void updateTemperaturesFromRawValues()
|
697
|
725
|
#ifdef TEMP_SENSOR_1_AS_REDUNDANT
|
698
|
726
|
redundant_temperature = analog2temp(redundant_temperature_raw, 1);
|
699
|
727
|
#endif
|
|
728
|
+ #ifdef FILAMENT_SENSOR && (FILWIDTH_PIN > -1) //check if a sensor is supported
|
|
729
|
+ filament_width_meas = analog2widthFil();
|
|
730
|
+ #endif
|
700
|
731
|
//Reset the watchdog after we know we have a temperature measurement.
|
701
|
732
|
watchdog_reset();
|
702
|
733
|
|
|
@@ -705,6 +736,36 @@ static void updateTemperaturesFromRawValues()
|
705
|
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
|
769
|
void tp_init()
|
709
|
770
|
{
|
710
|
771
|
#if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
|
|
@@ -804,6 +865,17 @@ void tp_init()
|
804
|
865
|
#endif
|
805
|
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
|
879
|
// Use timer0 for temperature measurement
|
808
|
880
|
// Interleave temperature interrupt with millies interrupt
|
809
|
881
|
OCR0B = 128;
|
|
@@ -1116,7 +1188,7 @@ ISR(TIMER0_COMPB_vect)
|
1116
|
1188
|
static unsigned long raw_temp_1_value = 0;
|
1117
|
1189
|
static unsigned long raw_temp_2_value = 0;
|
1118
|
1190
|
static unsigned long raw_temp_bed_value = 0;
|
1119
|
|
- static unsigned char temp_state = 8;
|
|
1191
|
+ static unsigned char temp_state = 10;
|
1120
|
1192
|
static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
|
1121
|
1193
|
static unsigned char soft_pwm_0;
|
1122
|
1194
|
#if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
|
|
@@ -1129,6 +1201,10 @@ ISR(TIMER0_COMPB_vect)
|
1129
|
1201
|
static unsigned char soft_pwm_b;
|
1130
|
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
|
1208
|
if(pwm_count == 0){
|
1133
|
1209
|
soft_pwm_0 = soft_pwm[0];
|
1134
|
1210
|
if(soft_pwm_0 > 0) {
|
|
@@ -1255,10 +1331,39 @@ ISR(TIMER0_COMPB_vect)
|
1255
|
1331
|
#if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
|
1256
|
1332
|
raw_temp_2_value += ADC;
|
1257
|
1333
|
#endif
|
1258
|
|
- temp_state = 0;
|
1259
|
|
- temp_count++;
|
|
1334
|
+ temp_state = 8;//change so that Filament Width is also measured
|
|
1335
|
+
|
1260
|
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
|
1367
|
temp_state = 0;
|
1263
|
1368
|
break;
|
1264
|
1369
|
// default:
|
|
@@ -1267,7 +1372,7 @@ ISR(TIMER0_COMPB_vect)
|
1267
|
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
|
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,6 +1388,12 @@ ISR(TIMER0_COMPB_vect)
|
1283
|
1388
|
#endif
|
1284
|
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
|
1398
|
temp_meas_ready = true;
|
1288
|
1399
|
temp_count = 0;
|