Filament Width Sensor singleton (#15191)

This commit is contained in:
Scott Lahteine
2019-09-10 18:48:58 -05:00
committed by GitHub
parent fe6ba4fd70
commit 75927e17dd
9 changed files with 147 additions and 146 deletions

View File

@ -1328,14 +1328,14 @@ void Planner::check_axes_activity() {
* into a volumetric multiplier. Conversion differs when using
* linear extrusion vs volumetric extrusion.
*/
void Planner::calculate_volumetric_for_width_sensor(const int8_t encoded_ratio) {
void Planner::apply_filament_width_sensor(const int8_t encoded_ratio) {
// Reconstitute the nominal/measured ratio
const float nom_meas_ratio = 1 + 0.01f * encoded_ratio,
ratio_2 = sq(nom_meas_ratio);
volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = parser.volumetric_enabled
? ratio_2 / CIRCLE_AREA(filament_width_nominal * 0.5f) // Volumetric uses a true volumetric multiplier
: ratio_2; // Linear squares the ratio, which scales the volume
? ratio_2 / CIRCLE_AREA(filwidth.nominal_mm * 0.5f) // Volumetric uses a true volumetric multiplier
: ratio_2; // Linear squares the ratio, which scales the volume
refresh_e_factor(FILAMENT_SENSOR_EXTRUDER_NUM);
}
@ -2069,37 +2069,8 @@ bool Planner::_populate_block(block_t * const block, bool split_move,
block->nominal_rate = CEIL(block->step_event_count * inverse_secs); // (step/sec) Always > 0
#if ENABLED(FILAMENT_WIDTH_SENSOR)
static float filwidth_e_count = 0, filwidth_delay_dist = 0;
//FMM update ring buffer used for delay with filament measurements
if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && filwidth_delay_index[1] >= 0) { //only for extruder with filament sensor and if ring buffer is initialized
constexpr int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10;
// increment counters with next move in e axis
filwidth_e_count += delta_mm[E_AXIS];
filwidth_delay_dist += delta_mm[E_AXIS];
// Only get new measurements on forward E movement
if (!UNEAR_ZERO(filwidth_e_count)) {
// Loop the delay distance counter (modulus by the mm length)
while (filwidth_delay_dist >= MMD_MM) filwidth_delay_dist -= MMD_MM;
// Convert into an index into the measurement array
filwidth_delay_index[0] = int8_t(filwidth_delay_dist * 0.1f);
// If the index has changed (must have gone forward)...
if (filwidth_delay_index[0] != filwidth_delay_index[1]) {
filwidth_e_count = 0; // Reset the E movement counter
const int8_t meas_sample = thermalManager.widthFil_to_size_ratio();
do {
filwidth_delay_index[1] = (filwidth_delay_index[1] + 1) % MMD_CM; // The next unused slot
measurement_delay[filwidth_delay_index[1]] = meas_sample; // Store the measurement
} while (filwidth_delay_index[0] != filwidth_delay_index[1]); // More slots to fill?
}
}
}
if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM) // Only for extruder with filament sensor
filwidth.advance_e(delta_mm[E_AXIS]);
#endif
// Calculate and limit speed in mm/sec for each axis

View File

@ -375,7 +375,14 @@ class Planner {
static void calculate_volumetric_multipliers();
#if ENABLED(FILAMENT_WIDTH_SENSOR)
void calculate_volumetric_for_width_sensor(const int8_t encoded_ratio);
void apply_filament_width_sensor(const int8_t encoded_ratio);
static inline float volumetric_percent(const bool vol) {
return 100.0f * (vol
? volumetric_area_nominal / volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]
: volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]
);
}
#endif
#if DISABLED(NO_VOLUMETRICS)

View File

@ -301,10 +301,6 @@ volatile bool Temperature::temp_meas_ready = false;
millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
int8_t Temperature::meas_shift_index; // Index of a delayed sample in buffer
#endif
#if HAS_AUTO_FAN
millis_t Temperature::next_auto_fan_check_ms = 0;
#endif
@ -314,10 +310,6 @@ volatile bool Temperature::temp_meas_ready = false;
Temperature::soft_pwm_count_fan[FAN_COUNT];
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
uint16_t Temperature::current_raw_filwidth = 0; // Measured filament diameter - one extruder only
#endif
#if ENABLED(PROBING_HEATERS_OFF)
bool Temperature::paused;
#endif
@ -1082,16 +1074,11 @@ void Temperature::manage_heater() {
#if ENABLED(FILAMENT_WIDTH_SENSOR)
/**
* Filament Width Sensor dynamically sets the volumetric multiplier
* based on a delayed measurement of the filament diameter.
* Dynamically set the volumetric multiplier based
* on the delayed Filament Width measurement.
*/
if (filament_sensor) {
meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
LIMIT(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
planner.calculate_volumetric_for_width_sensor(measurement_delay[meas_shift_index]);
}
#endif // FILAMENT_WIDTH_SENSOR
filwidth.update_volumetric();
#endif
#if HAS_HEATED_BED
@ -1526,7 +1513,7 @@ void Temperature::updateTemperaturesFromRawValues() {
redundant_temperature = analog_to_celsius_hotend(redundant_temperature_raw, 1);
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
filament_width_meas = analog_to_mm_fil_width();
filwidth.update_measured_mm();
#endif
#if ENABLED(USE_WATCHDOG)
@ -1537,30 +1524,6 @@ void Temperature::updateTemperaturesFromRawValues() {
temp_meas_ready = false;
}
#if ENABLED(FILAMENT_WIDTH_SENSOR)
// Convert raw Filament Width to millimeters
float Temperature::analog_to_mm_fil_width() {
return current_raw_filwidth * 5.0f * (1.0f / 16383.0f);
}
/**
* Convert Filament Width (mm) to a simple ratio
* and reduce to an 8 bit value.
*
* A nominal width of 1.75 and measured width of 1.73
* gives (100 * 1.75 / 1.73) for a ratio of 101 and
* a return value of 1.
*/
int8_t Temperature::widthFil_to_size_ratio() {
if (ABS(filament_width_nominal - filament_width_meas) <= FILWIDTH_ERROR_MARGIN)
return int(100.0f * filament_width_nominal / filament_width_meas) - 100;
return 0;
}
#endif
#if MAX6675_SEPARATE_SPI
SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
#endif
@ -2241,10 +2204,6 @@ void Temperature::set_current_temp_raw() {
temp_meas_ready = true;
}
#if ENABLED(FILAMENT_WIDTH_SENSOR)
uint32_t raw_filwidth_value; // = 0
#endif
void Temperature::readings_ready() {
// Update the raw values if they've been read. Else we could be updating them during reading.
@ -2252,7 +2211,7 @@ void Temperature::readings_ready() {
// Filament Sensor - can be read any time since IIR filtering is used
#if ENABLED(FILAMENT_WIDTH_SENSOR)
current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
filwidth.reading_ready();
#endif
#if HOTENDS
@ -2781,10 +2740,8 @@ void Temperature::isr() {
case Measure_FILWIDTH:
if (!HAL_ADC_READY())
next_sensor_state = adc_sensor_state; // redo this state
else if (HAL_READ_ADC() > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
raw_filwidth_value -= raw_filwidth_value >> 7; // Subtract 1/128th of the raw_filwidth_value
raw_filwidth_value += uint32_t(HAL_READ_ADC()) << 7; // Add new ADC reading, scaled by 128
}
else
filwidth.accumulate(HAL_READ_ADC());
break;
#endif

View File

@ -392,18 +392,10 @@ class Temperature {
static millis_t preheat_end_time[HOTENDS];
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
static int8_t meas_shift_index; // Index of a delayed sample in buffer
#endif
#if HAS_AUTO_FAN
static millis_t next_auto_fan_check_ms;
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
static uint16_t current_raw_filwidth; // Measured filament diameter - one extruder only
#endif
#if ENABLED(PROBING_HEATERS_OFF)
static bool paused;
#endif
@ -570,12 +562,6 @@ class Temperature {
#define is_preheating(n) (false)
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
static float analog_to_mm_fil_width(); // Convert raw Filament Width to millimeters
static int8_t widthFil_to_size_ratio(); // Convert Filament Width (mm) to an extrusion ratio
#endif
//high level conversion routines, for use outside of temperature.cpp
//inline so that there is no performance decrease.
//deg=degreeCelsius