Skip to content

Commit

Permalink
⚡️ Fix noisy ADC - 16x oversampling with 12-bit ADC (MarlinFirmware#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
tombrazier authored and LCh-77 committed Apr 1, 2022
1 parent 5c00015 commit 700ebf3
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 118 deletions.
1 change: 1 addition & 0 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ typedef float feedRate_t;
// celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
// For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
//
typedef uint16_t raw_adc_t;
typedef int16_t celsius_t;
typedef float celsius_float_t;

Expand Down
22 changes: 11 additions & 11 deletions Marlin/src/feature/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ Joystick joystick;
void Joystick::report() {
SERIAL_ECHOPGM("Joystick");
#if HAS_JOY_ADC_X
SERIAL_ECHOPGM_P(SP_X_STR, JOY_X(x.raw));
SERIAL_ECHOPGM_P(SP_X_STR, JOY_X(x.getraw()));
#endif
#if HAS_JOY_ADC_Y
SERIAL_ECHOPGM_P(SP_Y_STR, JOY_Y(y.raw));
SERIAL_ECHOPGM_P(SP_Y_STR, JOY_Y(y.getraw()));
#endif
#if HAS_JOY_ADC_Z
SERIAL_ECHOPGM_P(SP_Z_STR, JOY_Z(z.raw));
SERIAL_ECHOPGM_P(SP_Z_STR, JOY_Z(z.getraw()));
#endif
#if HAS_JOY_ADC_EN
SERIAL_ECHO_TERNARY(READ(JOY_EN_PIN), " EN=", "HIGH (dis", "LOW (en", "abled)");
Expand All @@ -91,29 +91,29 @@ Joystick joystick;
if (READ(JOY_EN_PIN)) return;
#endif

auto _normalize_joy = [](float &axis_jog, const int16_t raw, const int16_t (&joy_limits)[4]) {
auto _normalize_joy = [](float &axis_jog, const raw_adc_t raw, const raw_adc_t (&joy_limits)[4]) {
if (WITHIN(raw, joy_limits[0], joy_limits[3])) {
// within limits, check deadzone
if (raw > joy_limits[2])
axis_jog = (raw - joy_limits[2]) / float(joy_limits[3] - joy_limits[2]);
else if (raw < joy_limits[1])
axis_jog = (raw - joy_limits[1]) / float(joy_limits[1] - joy_limits[0]); // negative value
axis_jog = int16_t(raw - joy_limits[1]) / float(joy_limits[1] - joy_limits[0]); // negative value
// Map normal to jog value via quadratic relationship
axis_jog = SIGN(axis_jog) * sq(axis_jog);
}
};

#if HAS_JOY_ADC_X
static constexpr int16_t joy_x_limits[4] = JOY_X_LIMITS;
_normalize_joy(norm_jog.x, JOY_X(x.raw), joy_x_limits);
static constexpr raw_adc_t joy_x_limits[4] = JOY_X_LIMITS;
_normalize_joy(norm_jog.x, JOY_X(x.getraw()), joy_x_limits);
#endif
#if HAS_JOY_ADC_Y
static constexpr int16_t joy_y_limits[4] = JOY_Y_LIMITS;
_normalize_joy(norm_jog.y, JOY_Y(y.raw), joy_y_limits);
static constexpr raw_adc_t joy_y_limits[4] = JOY_Y_LIMITS;
_normalize_joy(norm_jog.y, JOY_Y(y.getraw()), joy_y_limits);
#endif
#if HAS_JOY_ADC_Z
static constexpr int16_t joy_z_limits[4] = JOY_Z_LIMITS;
_normalize_joy(norm_jog.z, JOY_Z(z.raw), joy_z_limits);
static constexpr raw_adc_t joy_z_limits[4] = JOY_Z_LIMITS;
_normalize_joy(norm_jog.z, JOY_Z(z.getraw()), joy_z_limits);
#endif
}

Expand Down
14 changes: 7 additions & 7 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ void MarlinUI::init() {
#if HAS_ADC_BUTTONS

typedef struct {
uint16_t ADCKeyValueMin, ADCKeyValueMax;
raw_adc_t ADCKeyValueMin, ADCKeyValueMax;
uint8_t ADCKeyNo;
} _stADCKeypadTable_;

Expand All @@ -1203,10 +1203,10 @@ void MarlinUI::init() {
#endif

// Calculate the ADC value for the voltage divider with specified pull-down resistor value
#define ADC_BUTTON_VALUE(r) int(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
#define ADC_BUTTON_VALUE(r) raw_adc_t(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))

static constexpr uint16_t adc_button_tolerance = HAL_ADC_RANGE * 25 / 1024,
adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
static constexpr raw_adc_t adc_button_tolerance = HAL_ADC_RANGE * 25 / 1024,
adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
// VALUE_MIN, VALUE_MAX, KEY
{ adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1 }, // F1
Expand All @@ -1226,13 +1226,13 @@ void MarlinUI::init() {

uint8_t get_ADC_keyValue() {
if (thermalManager.ADCKey_count >= 16) {
const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw;
const raw_adc_t currentkpADCValue = thermalManager.current_ADCKey_raw;
thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
thermalManager.ADCKey_count = 0;
if (currentkpADCValue < adc_other_button)
LOOP_L_N(i, ADC_KEY_NUM) {
const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
const raw_adc_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
}
}
Expand Down
Loading

0 comments on commit 700ebf3

Please sign in to comment.