endstop: Support oversampling of the endstop
Some printers can show occasional noise on the endstop pin. Support sampling the endstop pin multiple times to attempt to filter out this noise. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
776d8f9f79
commit
ece1f71c64
|
@ -137,6 +137,8 @@ class MCU_endstop:
|
||||||
self._pin = pin_params['pin']
|
self._pin = pin_params['pin']
|
||||||
self._pullup = pin_params['pullup']
|
self._pullup = pin_params['pullup']
|
||||||
self._invert = pin_params['invert']
|
self._invert = pin_params['invert']
|
||||||
|
self._oversample_count = 0
|
||||||
|
self._oversample_time = 0.
|
||||||
self._cmd_queue = mcu.alloc_command_queue()
|
self._cmd_queue = mcu.alloc_command_queue()
|
||||||
self._oid = self._home_cmd = self._query_cmd = None
|
self._oid = self._home_cmd = self._query_cmd = None
|
||||||
self._homing = False
|
self._homing = False
|
||||||
|
@ -144,6 +146,9 @@ class MCU_endstop:
|
||||||
self._last_state = {}
|
self._last_state = {}
|
||||||
def get_mcu(self):
|
def get_mcu(self):
|
||||||
return self._mcu
|
return self._mcu
|
||||||
|
def setup_oversample(self, oversample_count, oversample_time):
|
||||||
|
self._oversample_count = oversample_count
|
||||||
|
self._oversample_time = oversample_time
|
||||||
def add_stepper(self, stepper):
|
def add_stepper(self, stepper):
|
||||||
self._steppers.append(stepper)
|
self._steppers.append(stepper)
|
||||||
def build_config(self):
|
def build_config(self):
|
||||||
|
@ -155,6 +160,10 @@ class MCU_endstop:
|
||||||
self._mcu.add_config_cmd(
|
self._mcu.add_config_cmd(
|
||||||
"end_stop_set_stepper oid=%d pos=%d stepper_oid=%d" % (
|
"end_stop_set_stepper oid=%d pos=%d stepper_oid=%d" % (
|
||||||
self._oid, i, s.get_oid()), is_init=True)
|
self._oid, i, s.get_oid()), is_init=True)
|
||||||
|
self._mcu.add_config_cmd(
|
||||||
|
"end_stop_set_oversample oid=%d sample_ticks=%d sample_count=%d" % (
|
||||||
|
self._oid, self._mcu.seconds_to_clock(self._oversample_time),
|
||||||
|
self._oversample_count), is_init=True)
|
||||||
self._home_cmd = self._mcu.lookup_command(
|
self._home_cmd = self._mcu.lookup_command(
|
||||||
"end_stop_home oid=%c clock=%u rest_ticks=%u pin_value=%c")
|
"end_stop_home oid=%c clock=%u rest_ticks=%u pin_value=%c")
|
||||||
self._query_cmd = self._mcu.lookup_command("end_stop_query oid=%c")
|
self._query_cmd = self._mcu.lookup_command("end_stop_query oid=%c")
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
import math, logging
|
import math, logging
|
||||||
import homing, pins
|
import homing, pins
|
||||||
|
|
||||||
|
ENDSTOP_OVERSAMPLE_COUNT = 4
|
||||||
|
ENDSTOP_OVERSAMPLE_TIME = .000015
|
||||||
|
|
||||||
class PrinterStepper:
|
class PrinterStepper:
|
||||||
def __init__(self, printer, config, name):
|
def __init__(self, printer, config, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -52,6 +55,8 @@ class PrinterHomingStepper(PrinterStepper):
|
||||||
|
|
||||||
self.mcu_endstop = pins.setup_pin(
|
self.mcu_endstop = pins.setup_pin(
|
||||||
printer, 'endstop', config.get('endstop_pin'))
|
printer, 'endstop', config.get('endstop_pin'))
|
||||||
|
self.mcu_endstop.setup_oversample(
|
||||||
|
ENDSTOP_OVERSAMPLE_COUNT, ENDSTOP_OVERSAMPLE_TIME)
|
||||||
self.mcu_endstop.add_stepper(self.mcu_stepper)
|
self.mcu_endstop.add_stepper(self.mcu_stepper)
|
||||||
self.position_min = config.getfloat('position_min', 0.)
|
self.position_min = config.getfloat('position_min', 0.)
|
||||||
self.position_max = config.getfloat(
|
self.position_max = config.getfloat(
|
||||||
|
|
|
@ -13,9 +13,9 @@
|
||||||
|
|
||||||
struct end_stop {
|
struct end_stop {
|
||||||
struct timer time;
|
struct timer time;
|
||||||
uint32_t rest_time;
|
uint32_t rest_time, sample_time;
|
||||||
struct gpio_in pin;
|
struct gpio_in pin;
|
||||||
uint8_t flags, stepper_count;
|
uint8_t flags, stepper_count, sample_count, trigger_count;
|
||||||
struct stepper *steppers[0];
|
struct stepper *steppers[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -34,6 +34,8 @@ stop_steppers(struct end_stop *e)
|
||||||
sched_wake_task(&endstop_wake);
|
sched_wake_task(&endstop_wake);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint_fast8_t end_stop_oversample_event(struct timer *t);
|
||||||
|
|
||||||
// Timer callback for an end stop
|
// Timer callback for an end stop
|
||||||
static uint_fast8_t
|
static uint_fast8_t
|
||||||
end_stop_event(struct timer *t)
|
end_stop_event(struct timer *t)
|
||||||
|
@ -45,9 +47,33 @@ end_stop_event(struct timer *t)
|
||||||
e->time.waketime += e->rest_time;
|
e->time.waketime += e->rest_time;
|
||||||
return SF_RESCHEDULE;
|
return SF_RESCHEDULE;
|
||||||
}
|
}
|
||||||
|
e->time.func = end_stop_oversample_event;
|
||||||
|
return end_stop_oversample_event(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timer callback for an end stop this is sampling extra times
|
||||||
|
static uint_fast8_t
|
||||||
|
end_stop_oversample_event(struct timer *t)
|
||||||
|
{
|
||||||
|
struct end_stop *e = container_of(t, struct end_stop, time);
|
||||||
|
uint8_t val = gpio_in_read(e->pin);
|
||||||
|
if ((val ? ~e->flags : e->flags) & ESF_PIN_HIGH) {
|
||||||
|
// No longer matching - reschedule for the next attempt
|
||||||
|
e->time.func = end_stop_event;
|
||||||
|
uint8_t past_triggers = e->sample_count - e->trigger_count;
|
||||||
|
e->time.waketime += e->rest_time - past_triggers * e->sample_time;
|
||||||
|
e->trigger_count = e->sample_count;
|
||||||
|
return SF_RESCHEDULE;
|
||||||
|
}
|
||||||
|
uint8_t count = e->trigger_count - 1;
|
||||||
|
if (!count) {
|
||||||
stop_steppers(e);
|
stop_steppers(e);
|
||||||
return SF_DONE;
|
return SF_DONE;
|
||||||
}
|
}
|
||||||
|
e->trigger_count = count;
|
||||||
|
e->time.waketime += e->sample_time;
|
||||||
|
return SF_RESCHEDULE;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
command_config_end_stop(uint32_t *args)
|
command_config_end_stop(uint32_t *args)
|
||||||
|
@ -56,9 +82,9 @@ command_config_end_stop(uint32_t *args)
|
||||||
struct end_stop *e = oid_alloc(
|
struct end_stop *e = oid_alloc(
|
||||||
args[0], command_config_end_stop
|
args[0], command_config_end_stop
|
||||||
, sizeof(*e) + sizeof(e->steppers[0]) * stepper_count);
|
, sizeof(*e) + sizeof(e->steppers[0]) * stepper_count);
|
||||||
e->time.func = end_stop_event;
|
|
||||||
e->pin = gpio_in_setup(args[1], args[2]);
|
e->pin = gpio_in_setup(args[1], args[2]);
|
||||||
e->stepper_count = stepper_count;
|
e->stepper_count = stepper_count;
|
||||||
|
e->sample_count = 1;
|
||||||
}
|
}
|
||||||
DECL_COMMAND(command_config_end_stop,
|
DECL_COMMAND(command_config_end_stop,
|
||||||
"config_end_stop oid=%c pin=%c pull_up=%c stepper_count=%c");
|
"config_end_stop oid=%c pin=%c pull_up=%c stepper_count=%c");
|
||||||
|
@ -75,6 +101,18 @@ command_end_stop_set_stepper(uint32_t *args)
|
||||||
DECL_COMMAND(command_end_stop_set_stepper,
|
DECL_COMMAND(command_end_stop_set_stepper,
|
||||||
"end_stop_set_stepper oid=%c pos=%c stepper_oid=%c");
|
"end_stop_set_stepper oid=%c pos=%c stepper_oid=%c");
|
||||||
|
|
||||||
|
void
|
||||||
|
command_end_stop_set_oversample(uint32_t *args)
|
||||||
|
{
|
||||||
|
struct end_stop *e = oid_lookup(args[0], command_config_end_stop);
|
||||||
|
e->sample_time = args[1];
|
||||||
|
e->sample_count = args[2];
|
||||||
|
if (!e->sample_count)
|
||||||
|
e->sample_count = 1;
|
||||||
|
}
|
||||||
|
DECL_COMMAND(command_end_stop_set_oversample,
|
||||||
|
"end_stop_set_oversample oid=%c sample_ticks=%u sample_count=%c");
|
||||||
|
|
||||||
// Home an axis
|
// Home an axis
|
||||||
void
|
void
|
||||||
command_end_stop_home(uint32_t *args)
|
command_end_stop_home(uint32_t *args)
|
||||||
|
@ -88,6 +126,8 @@ command_end_stop_home(uint32_t *args)
|
||||||
e->flags = 0;
|
e->flags = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
e->time.func = end_stop_event;
|
||||||
|
e->trigger_count = e->sample_count;
|
||||||
e->flags = ESF_HOMING | (args[3] ? ESF_PIN_HIGH : 0);
|
e->flags = ESF_HOMING | (args[3] ? ESF_PIN_HIGH : 0);
|
||||||
sched_add_timer(&e->time);
|
sched_add_timer(&e->time);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue