2019-11-10 17:55:53 +01:00
|
|
|
# Support for enable pins on stepper motor drivers
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2019-11-12 17:41:41 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
DISABLE_STALL_TIME = 0.100
|
2019-11-10 17:55:53 +01:00
|
|
|
|
2019-11-12 18:49:21 +01:00
|
|
|
# Tracking of shared stepper enable pins
|
|
|
|
class StepperEnablePin:
|
2019-11-13 00:34:32 +01:00
|
|
|
def __init__(self, mcu_enable, enable_count):
|
2019-11-12 18:49:21 +01:00
|
|
|
self.mcu_enable = mcu_enable
|
|
|
|
self.enable_count = enable_count
|
2019-11-13 00:34:32 +01:00
|
|
|
self.is_dedicated = True
|
2019-11-12 18:49:21 +01:00
|
|
|
def set_enable(self, print_time):
|
|
|
|
if not self.enable_count:
|
|
|
|
self.mcu_enable.set_digital(print_time, 1)
|
|
|
|
self.enable_count += 1
|
|
|
|
def set_disable(self, print_time):
|
|
|
|
self.enable_count -= 1
|
|
|
|
if not self.enable_count:
|
|
|
|
self.mcu_enable.set_digital(print_time, 0)
|
|
|
|
|
|
|
|
# Enable line tracking for each stepper motor
|
|
|
|
class EnableTracking:
|
|
|
|
def __init__(self, printer, stepper, pin):
|
|
|
|
self.stepper = stepper
|
2019-11-13 00:18:32 +01:00
|
|
|
self.callbacks = []
|
2019-11-12 19:55:50 +01:00
|
|
|
self.is_enabled = False
|
2019-11-12 18:49:21 +01:00
|
|
|
self.stepper.add_active_callback(self.motor_enable)
|
2019-11-13 00:34:32 +01:00
|
|
|
if pin is None:
|
|
|
|
# No enable line (stepper always enabled)
|
|
|
|
self.enable = StepperEnablePin(None, 9999)
|
|
|
|
self.enable.is_dedicated = False
|
|
|
|
return
|
|
|
|
ppins = printer.lookup_object('pins')
|
|
|
|
pin_params = ppins.lookup_pin(pin, can_invert=True,
|
|
|
|
share_type='stepper_enable')
|
|
|
|
self.enable = pin_params.get('class')
|
|
|
|
if self.enable is not None:
|
|
|
|
# Shared enable line
|
|
|
|
self.enable.is_dedicated = False
|
|
|
|
return
|
|
|
|
mcu_enable = pin_params['chip'].setup_pin('digital_out', pin_params)
|
|
|
|
mcu_enable.setup_max_duration(0.)
|
|
|
|
self.enable = pin_params['class'] = StepperEnablePin(mcu_enable, 0)
|
2019-11-13 00:18:32 +01:00
|
|
|
def register_state_callback(self, callback):
|
|
|
|
self.callbacks.append(callback)
|
2019-11-12 18:49:21 +01:00
|
|
|
def motor_enable(self, print_time):
|
2019-11-12 19:55:50 +01:00
|
|
|
if not self.is_enabled:
|
2019-11-13 00:18:32 +01:00
|
|
|
for cb in self.callbacks:
|
|
|
|
cb(print_time, True)
|
2019-11-12 18:49:21 +01:00
|
|
|
self.enable.set_enable(print_time)
|
2019-11-12 19:55:50 +01:00
|
|
|
self.is_enabled = True
|
2019-11-12 18:49:21 +01:00
|
|
|
def motor_disable(self, print_time):
|
2019-11-12 19:55:50 +01:00
|
|
|
if self.is_enabled:
|
2019-11-12 18:49:21 +01:00
|
|
|
# Enable stepper on future stepper movement
|
2019-11-13 00:18:32 +01:00
|
|
|
for cb in self.callbacks:
|
|
|
|
cb(print_time, False)
|
2019-11-12 18:49:21 +01:00
|
|
|
self.enable.set_disable(print_time)
|
2019-11-12 19:55:50 +01:00
|
|
|
self.is_enabled = False
|
2019-11-12 18:49:21 +01:00
|
|
|
self.stepper.add_active_callback(self.motor_enable)
|
2019-11-12 19:55:50 +01:00
|
|
|
def is_motor_enabled(self):
|
|
|
|
return self.is_enabled
|
2019-11-13 00:18:32 +01:00
|
|
|
def has_dedicated_enable(self):
|
|
|
|
return self.enable.is_dedicated
|
2019-11-12 18:49:21 +01:00
|
|
|
|
2019-11-13 00:34:32 +01:00
|
|
|
# Global stepper enable line tracking
|
2019-11-12 18:49:21 +01:00
|
|
|
class PrinterStepperEnable:
|
2019-11-10 17:55:53 +01:00
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
2019-11-12 18:49:21 +01:00
|
|
|
self.enable_lines = {}
|
2019-11-12 17:41:41 +01:00
|
|
|
self.printer.register_event_handler("gcode:request_restart",
|
|
|
|
self._handle_request_restart)
|
2019-11-10 17:55:53 +01:00
|
|
|
# Register M18/M84 commands
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
gcode.register_command("M18", self.cmd_M18)
|
|
|
|
gcode.register_command("M84", self.cmd_M18)
|
2019-11-12 18:49:21 +01:00
|
|
|
def register_stepper(self, stepper, pin):
|
|
|
|
name = stepper.get_name()
|
|
|
|
self.enable_lines[name] = EnableTracking(self.printer, stepper, pin)
|
2019-11-12 17:41:41 +01:00
|
|
|
def motor_off(self):
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
toolhead.dwell(DISABLE_STALL_TIME)
|
|
|
|
print_time = toolhead.get_last_move_time()
|
2019-11-12 18:49:21 +01:00
|
|
|
for el in self.enable_lines.values():
|
|
|
|
el.motor_disable(print_time)
|
2019-11-12 17:41:41 +01:00
|
|
|
self.printer.send_event("stepper_enable:motor_off", print_time)
|
|
|
|
toolhead.dwell(DISABLE_STALL_TIME)
|
|
|
|
logging.debug('; Max time of %f', print_time)
|
|
|
|
def _handle_request_restart(self, print_time):
|
|
|
|
self.motor_off()
|
2019-11-10 17:55:53 +01:00
|
|
|
def cmd_M18(self, params):
|
|
|
|
# Turn off motors
|
2019-11-12 17:41:41 +01:00
|
|
|
self.motor_off()
|
2019-11-12 19:55:50 +01:00
|
|
|
def lookup_enable(self, name):
|
2019-11-15 00:05:32 +01:00
|
|
|
if name not in self.enable_lines:
|
|
|
|
raise self.printer.config_error("Unknown stepper '%s'" % (name,))
|
2019-11-12 19:55:50 +01:00
|
|
|
return self.enable_lines[name]
|
2019-11-10 17:55:53 +01:00
|
|
|
|
|
|
|
def load_config(config):
|
2019-11-12 18:49:21 +01:00
|
|
|
return PrinterStepperEnable(config)
|