Prusa MINI Firmware overview
FilamentWidthSensor Class Reference

#include <filwidth.h>

Collaboration diagram for FilamentWidthSensor:

Public Member Functions

 FilamentWidthSensor ()
 

Static Public Member Functions

static void init ()
 
static void enable (const bool ena)
 
static void set_delay_cm (const uint8_t cm)
 
static int8_t sample_to_size_ratio ()
 
static void accumulate (const uint16_t adc)
 
static float raw_to_mm (const uint16_t v)
 
static float raw_to_mm ()
 
static void reading_ready ()
 
static void update_measured_mm ()
 
static void advance_e (const float &e_move)
 
static void update_volumetric ()
 

Static Public Attributes

static constexpr int MMD_CM = MAX_MEASUREMENT_DELAY + 1
 
static constexpr int MMD_MM = MMD_CM * 10
 
static bool enabled
 
static uint32_t accum
 
static uint16_t raw
 
static float nominal_mm
 
static float measured_mm
 
static float e_count
 
static float delay_dist
 
static uint8_t meas_delay_cm
 
static int8_t ratios [MMD_CM]
 
static int8_t index_r
 
static int8_t index_w
 

Detailed Description

Marlin 3D Printer Firmware Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]

Based on Sprinter and grbl. Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Constructor & Destructor Documentation

◆ FilamentWidthSensor()

FilamentWidthSensor::FilamentWidthSensor ( )
40 { init(); }
Here is the call graph for this function:

Member Function Documentation

◆ init()

static void FilamentWidthSensor::init ( )
static
Here is the caller graph for this function:

◆ enable()

static void FilamentWidthSensor::enable ( const bool  ena)
static
43 { enabled = ena; }

◆ set_delay_cm()

static void FilamentWidthSensor::set_delay_cm ( const uint8_t  cm)
static
45  {
46  meas_delay_cm = _MIN(cm, MAX_MEASUREMENT_DELAY);
47  }

◆ sample_to_size_ratio()

static int8_t FilamentWidthSensor::sample_to_size_ratio ( )
static

Convert Filament Width (mm) to an extrusion 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.

57  {
58  return ABS(nominal_mm - measured_mm) <= FILWIDTH_ERROR_MARGIN
59  ? int(100.0f * nominal_mm / measured_mm) - 100 : 0;
60  }
Here is the caller graph for this function:

◆ accumulate()

static void FilamentWidthSensor::accumulate ( const uint16_t  adc)
static
63  {
64  if (adc > 102) // Ignore ADC under 0.5 volts
65  accum += (uint32_t(adc) << 7) - (accum >> 7);
66  }
Here is the caller graph for this function:

◆ raw_to_mm() [1/2]

static float FilamentWidthSensor::raw_to_mm ( const uint16_t  v)
static
69 { return v * 5.0f * RECIPROCAL(16383.0f); }

◆ raw_to_mm() [2/2]

static float FilamentWidthSensor::raw_to_mm ( )
static
70 { return raw_to_mm(raw); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reading_ready()

static void FilamentWidthSensor::reading_ready ( )
static
74 { raw = accum >> 10; }
Here is the caller graph for this function:

◆ update_measured_mm()

static void FilamentWidthSensor::update_measured_mm ( )
static
77 { measured_mm = raw_to_mm(); }
Here is the call graph for this function:

◆ advance_e()

static void FilamentWidthSensor::advance_e ( const float &  e_move)
static
80  {
81 
82  // Increment counters with the E distance
83  e_count += e_move;
84  delay_dist += e_move;
85 
86  // Only get new measurements on forward E movement
87  if (!UNEAR_ZERO(e_count)) {
88 
89  // Loop the delay distance counter (modulus by the mm length)
90  while (delay_dist >= MMD_MM) delay_dist -= MMD_MM;
91 
92  // Convert into an index (cm) into the measurement array
93  index_r = int8_t(delay_dist * 0.1f);
94 
95  // If the ring buffer is not full...
96  if (index_r != index_w) {
97  e_count = 0; // Reset the E movement counter
98  const int8_t meas_sample = sample_to_size_ratio();
99  do {
100  if (++index_w >= MMD_CM) index_w = 0; // The next unused slot
101  ratios[index_w] = meas_sample; // Store the measurement
102  } while (index_r != index_w); // More slots to fill?
103  }
104  }
105  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_volumetric()

static void FilamentWidthSensor::update_volumetric ( )
static
108  {
109  if (enabled) {
110  int8_t read_index = index_r - meas_delay_cm;
111  if (read_index < 0) read_index += MMD_CM; // Loop around buffer if needed
112  LIMIT(read_index, 0, MAX_MEASUREMENT_DELAY);
113  planner.apply_filament_width_sensor(ratios[read_index]);
114  }
115  }
Here is the caller graph for this function:

Member Data Documentation

◆ MMD_CM

constexpr int FilamentWidthSensor::MMD_CM = MAX_MEASUREMENT_DELAY + 1
staticconstexpr

◆ MMD_MM

constexpr int FilamentWidthSensor::MMD_MM = MMD_CM * 10
staticconstexpr

◆ enabled

bool FilamentWidthSensor::enabled
static

◆ accum

uint32_t FilamentWidthSensor::accum
static

◆ raw

uint16_t FilamentWidthSensor::raw
static

◆ nominal_mm

float FilamentWidthSensor::nominal_mm
static

◆ measured_mm

float FilamentWidthSensor::measured_mm
static

◆ e_count

float FilamentWidthSensor::e_count
static

◆ delay_dist

float FilamentWidthSensor::delay_dist
static

◆ meas_delay_cm

uint8_t FilamentWidthSensor::meas_delay_cm
static

◆ ratios

int8_t FilamentWidthSensor::ratios[MMD_CM]
static

◆ index_r

int8_t FilamentWidthSensor::index_r
static

◆ index_w

int8_t FilamentWidthSensor::index_w
static
FilamentWidthSensor::index_w
static int8_t index_w
Definition: filwidth.h:37
FilamentWidthSensor::raw_to_mm
static float raw_to_mm()
Definition: filwidth.h:70
FilamentWidthSensor::accum
static uint32_t accum
Definition: filwidth.h:31
RECIPROCAL
#define RECIPROCAL(x)
Definition: macros.h:273
UNEAR_ZERO
#define UNEAR_ZERO(x)
Definition: macros.h:269
_MIN
#define _MIN(V...)
Definition: macros.h:333
FilamentWidthSensor::meas_delay_cm
static uint8_t meas_delay_cm
Definition: filwidth.h:36
LIMIT
#define LIMIT(v, n1, n2)
Definition: macros.h:139
FilamentWidthSensor::index_r
static int8_t index_r
Definition: filwidth.h:37
ABS
#define ABS(a)
Definition: macros.h:266
FilamentWidthSensor::enabled
static bool enabled
Definition: filwidth.h:30
FilamentWidthSensor::nominal_mm
static float nominal_mm
Definition: filwidth.h:33
FilamentWidthSensor::MMD_CM
static constexpr int MMD_CM
Definition: filwidth.h:29
FilamentWidthSensor::sample_to_size_ratio
static int8_t sample_to_size_ratio()
Definition: filwidth.h:57
FilamentWidthSensor::e_count
static float e_count
Definition: filwidth.h:33
FilamentWidthSensor::MMD_MM
static constexpr int MMD_MM
Definition: filwidth.h:29
FilamentWidthSensor::init
static void init()
createSpeedLookupTable.int
int
Definition: createSpeedLookupTable.py:15
FilamentWidthSensor::delay_dist
static float delay_dist
Definition: filwidth.h:33
FilamentWidthSensor::raw
static uint16_t raw
Definition: filwidth.h:32
FilamentWidthSensor::ratios
static int8_t ratios[MMD_CM]
Definition: filwidth.h:37
FilamentWidthSensor::measured_mm
static float measured_mm
Definition: filwidth.h:33
planner
Planner planner
Definition: planner.cpp:111