2016-05-25 18:37:40 +03:00
|
|
|
# Printer stepper support
|
|
|
|
#
|
2018-01-10 22:49:00 +03:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 18:37:40 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import math, logging
|
2018-04-04 19:07:41 +03:00
|
|
|
import homing
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2018-01-10 22:49:00 +03:00
|
|
|
# Tracking of shared stepper enable pins
|
|
|
|
class StepperEnablePin:
|
|
|
|
def __init__(self, mcu_enable, enable_count=0):
|
|
|
|
self.mcu_enable = mcu_enable
|
|
|
|
self.enable_count = enable_count
|
|
|
|
def set_enable(self, print_time, enable):
|
|
|
|
if enable:
|
|
|
|
if not self.enable_count:
|
|
|
|
self.mcu_enable.set_digital(print_time, 1)
|
|
|
|
self.enable_count += 1
|
|
|
|
else:
|
|
|
|
self.enable_count -= 1
|
|
|
|
if not self.enable_count:
|
|
|
|
self.mcu_enable.set_digital(print_time, 0)
|
|
|
|
|
2018-04-04 19:07:41 +03:00
|
|
|
def lookup_enable_pin(ppins, pin):
|
2018-01-10 22:49:00 +03:00
|
|
|
if pin is None:
|
|
|
|
return StepperEnablePin(None, 9999)
|
2018-04-04 19:07:41 +03:00
|
|
|
pin_params = ppins.lookup_pin('digital_out', pin, 'stepper_enable')
|
2018-01-10 22:49:00 +03:00
|
|
|
enable = pin_params.get('class')
|
|
|
|
if enable is None:
|
|
|
|
mcu_enable = pin_params['chip'].setup_pin(pin_params)
|
|
|
|
mcu_enable.setup_max_duration(0.)
|
|
|
|
pin_params['class'] = enable = StepperEnablePin(mcu_enable)
|
|
|
|
return enable
|
|
|
|
|
2017-11-07 02:37:35 +03:00
|
|
|
# Code storing the definitions for a stepper motor
|
2016-05-25 18:37:40 +03:00
|
|
|
class PrinterStepper:
|
2017-10-29 17:58:04 +03:00
|
|
|
def __init__(self, printer, config):
|
2018-01-20 06:22:17 +03:00
|
|
|
self.name = config.get_name()
|
2017-10-29 17:58:04 +03:00
|
|
|
if self.name.startswith('stepper_'):
|
|
|
|
self.name = self.name[8:]
|
2017-11-07 20:12:28 +03:00
|
|
|
self.need_motor_enable = True
|
|
|
|
# Stepper definition
|
2018-04-04 19:07:41 +03:00
|
|
|
ppins = printer.lookup_object('pins')
|
|
|
|
self.mcu_stepper = ppins.setup_pin('stepper', config.get('step_pin'))
|
|
|
|
dir_pin_params = ppins.lookup_pin('digital_out', config.get('dir_pin'))
|
2017-08-21 18:25:26 +03:00
|
|
|
self.mcu_stepper.setup_dir_pin(dir_pin_params)
|
2017-11-07 20:12:28 +03:00
|
|
|
self.step_dist = config.getfloat('step_distance', above=0.)
|
2017-08-21 18:25:26 +03:00
|
|
|
self.mcu_stepper.setup_step_distance(self.step_dist)
|
2018-02-17 21:39:37 +03:00
|
|
|
self.step = self.mcu_stepper.step
|
2017-11-07 20:12:28 +03:00
|
|
|
self.step_const = self.mcu_stepper.step_const
|
|
|
|
self.step_delta = self.mcu_stepper.step_delta
|
2018-04-04 19:07:41 +03:00
|
|
|
self.enable = lookup_enable_pin(ppins, config.get('enable_pin', None))
|
2017-07-24 20:54:46 +03:00
|
|
|
def _dist_to_time(self, dist, start_velocity, accel):
|
|
|
|
# Calculate the time it takes to travel a distance with constant accel
|
|
|
|
time_offset = start_velocity / accel
|
|
|
|
return math.sqrt(2. * dist / accel + time_offset**2) - time_offset
|
|
|
|
def set_max_jerk(self, max_halt_velocity, max_accel):
|
|
|
|
# Calculate the firmware's maximum halt interval time
|
|
|
|
last_step_time = self._dist_to_time(
|
|
|
|
self.step_dist, max_halt_velocity, max_accel)
|
|
|
|
second_last_step_time = self._dist_to_time(
|
|
|
|
2. * self.step_dist, max_halt_velocity, max_accel)
|
|
|
|
min_stop_interval = second_last_step_time - last_step_time
|
2017-08-21 18:25:26 +03:00
|
|
|
self.mcu_stepper.setup_min_stop_interval(min_stop_interval)
|
2017-11-07 20:29:51 +03:00
|
|
|
def set_position(self, pos):
|
|
|
|
self.mcu_stepper.set_position(pos)
|
2017-09-12 19:47:40 +03:00
|
|
|
def motor_enable(self, print_time, enable=0):
|
2018-01-10 22:49:00 +03:00
|
|
|
if self.need_motor_enable != (not enable):
|
|
|
|
self.enable.set_enable(print_time, enable)
|
2017-07-24 20:54:46 +03:00
|
|
|
self.need_motor_enable = not enable
|
|
|
|
|
2017-11-07 02:37:35 +03:00
|
|
|
# Support for stepper controlled linear axis with an endstop
|
2017-07-24 20:54:46 +03:00
|
|
|
class PrinterHomingStepper(PrinterStepper):
|
2017-11-02 04:21:37 +03:00
|
|
|
def __init__(self, printer, config, default_position=None):
|
2017-10-29 17:58:04 +03:00
|
|
|
PrinterStepper.__init__(self, printer, config)
|
2017-11-07 02:37:35 +03:00
|
|
|
# Endstop and its position
|
2018-04-04 19:07:41 +03:00
|
|
|
ppins = printer.lookup_object('pins')
|
|
|
|
self.mcu_endstop = ppins.setup_pin('endstop', config.get('endstop_pin'))
|
2017-07-24 20:54:46 +03:00
|
|
|
self.mcu_endstop.add_stepper(self.mcu_stepper)
|
2017-11-02 04:21:37 +03:00
|
|
|
if default_position is None:
|
|
|
|
self.position_endstop = config.getfloat('position_endstop')
|
|
|
|
else:
|
|
|
|
self.position_endstop = config.getfloat(
|
|
|
|
'position_endstop', default_position)
|
2017-11-07 02:37:35 +03:00
|
|
|
# Axis range
|
2017-07-24 20:54:46 +03:00
|
|
|
self.position_min = config.getfloat('position_min', 0.)
|
|
|
|
self.position_max = config.getfloat(
|
|
|
|
'position_max', 0., above=self.position_min)
|
2017-11-07 02:37:35 +03:00
|
|
|
# Homing mechanics
|
2017-04-11 18:37:09 +03:00
|
|
|
self.homing_speed = config.getfloat('homing_speed', 5.0, above=0.)
|
2017-11-07 02:37:35 +03:00
|
|
|
self.homing_retract_dist = config.getfloat(
|
2018-03-06 09:59:13 +03:00
|
|
|
'homing_retract_dist', 5., minval=0.)
|
2017-07-24 21:12:17 +03:00
|
|
|
self.homing_positive_dir = config.getboolean('homing_positive_dir', None)
|
|
|
|
if self.homing_positive_dir is None:
|
|
|
|
axis_len = self.position_max - self.position_min
|
|
|
|
if self.position_endstop <= self.position_min + axis_len / 4.:
|
|
|
|
self.homing_positive_dir = False
|
|
|
|
elif self.position_endstop >= self.position_max - axis_len / 4.:
|
|
|
|
self.homing_positive_dir = True
|
|
|
|
else:
|
|
|
|
raise config.error(
|
|
|
|
"Unable to infer homing_positive_dir in section '%s'" % (
|
2018-01-20 06:22:17 +03:00
|
|
|
config.get_name(),))
|
2017-11-07 02:37:35 +03:00
|
|
|
# Endstop stepper phase position tracking
|
2017-04-11 18:37:09 +03:00
|
|
|
self.homing_stepper_phases = config.getint(
|
|
|
|
'homing_stepper_phases', None, minval=0)
|
|
|
|
endstop_accuracy = config.getfloat(
|
|
|
|
'homing_endstop_accuracy', None, above=0.)
|
|
|
|
self.homing_endstop_accuracy = self.homing_endstop_phase = None
|
2016-07-27 05:06:14 +03:00
|
|
|
if self.homing_stepper_phases:
|
2017-04-11 18:37:09 +03:00
|
|
|
self.homing_endstop_phase = config.getint(
|
|
|
|
'homing_endstop_phase', None, minval=0
|
|
|
|
, maxval=self.homing_stepper_phases-1)
|
2017-12-08 01:35:19 +03:00
|
|
|
if (self.homing_endstop_phase is not None
|
|
|
|
and config.getboolean('homing_endstop_align_zero', False)):
|
2017-10-04 02:32:08 +03:00
|
|
|
# Adjust the endstop position so 0.0 is always at a full step
|
|
|
|
micro_steps = self.homing_stepper_phases // 4
|
|
|
|
phase_offset = (
|
|
|
|
((self.homing_endstop_phase + micro_steps // 2) % micro_steps)
|
|
|
|
- micro_steps // 2) * self.step_dist
|
|
|
|
full_step = micro_steps * self.step_dist
|
|
|
|
es_pos = (int(self.position_endstop / full_step + .5) * full_step
|
|
|
|
+ phase_offset)
|
|
|
|
if es_pos != self.position_endstop:
|
|
|
|
logging.info("Changing %s endstop position to %.3f"
|
|
|
|
" (from %.3f)", self.name, es_pos,
|
|
|
|
self.position_endstop)
|
|
|
|
self.position_endstop = es_pos
|
2016-07-27 05:06:14 +03:00
|
|
|
if endstop_accuracy is None:
|
|
|
|
self.homing_endstop_accuracy = self.homing_stepper_phases//2 - 1
|
|
|
|
elif self.homing_endstop_phase is not None:
|
|
|
|
self.homing_endstop_accuracy = int(math.ceil(
|
2017-11-07 19:51:27 +03:00
|
|
|
endstop_accuracy * .5 / self.step_dist))
|
2016-07-27 05:06:14 +03:00
|
|
|
else:
|
|
|
|
self.homing_endstop_accuracy = int(math.ceil(
|
2017-11-07 19:51:27 +03:00
|
|
|
endstop_accuracy / self.step_dist))
|
2017-06-10 06:39:46 +03:00
|
|
|
if self.homing_endstop_accuracy >= self.homing_stepper_phases // 2:
|
2016-07-27 05:06:14 +03:00
|
|
|
logging.info("Endstop for %s is not accurate enough for stepper"
|
2017-09-27 18:43:14 +03:00
|
|
|
" phase adjustment", name)
|
2016-07-27 05:06:14 +03:00
|
|
|
self.homing_stepper_phases = None
|
2017-09-13 19:06:08 +03:00
|
|
|
if self.mcu_endstop.get_mcu().is_fileoutput():
|
2017-03-31 21:38:09 +03:00
|
|
|
self.homing_endstop_accuracy = self.homing_stepper_phases
|
2017-11-07 20:29:51 +03:00
|
|
|
def get_endstops(self):
|
2017-12-06 05:51:44 +03:00
|
|
|
return [(self.mcu_endstop, self.name)]
|
2016-10-13 17:04:30 +03:00
|
|
|
def get_homed_offset(self):
|
2016-11-14 21:40:35 +03:00
|
|
|
if not self.homing_stepper_phases or self.need_motor_enable:
|
2017-11-07 02:03:10 +03:00
|
|
|
return 0.
|
2016-12-09 02:12:20 +03:00
|
|
|
pos = self.mcu_stepper.get_mcu_position()
|
2016-07-27 05:06:14 +03:00
|
|
|
pos %= self.homing_stepper_phases
|
|
|
|
if self.homing_endstop_phase is None:
|
2017-09-27 18:43:14 +03:00
|
|
|
logging.info("Setting %s endstop phase to %d", self.name, pos)
|
2016-07-27 05:06:14 +03:00
|
|
|
self.homing_endstop_phase = pos
|
2017-11-07 02:03:10 +03:00
|
|
|
return 0.
|
2016-07-27 05:06:14 +03:00
|
|
|
delta = (pos - self.homing_endstop_phase) % self.homing_stepper_phases
|
|
|
|
if delta >= self.homing_stepper_phases - self.homing_endstop_accuracy:
|
|
|
|
delta -= self.homing_stepper_phases
|
|
|
|
elif delta > self.homing_endstop_accuracy:
|
2016-11-18 22:35:31 +03:00
|
|
|
raise homing.EndstopError(
|
|
|
|
"Endstop %s incorrect phase (got %d vs %d)" % (
|
|
|
|
self.name, pos, self.homing_endstop_phase))
|
2017-04-05 02:20:54 +03:00
|
|
|
return delta * self.step_dist
|
2017-11-07 21:10:08 +03:00
|
|
|
|
|
|
|
# Wrapper for dual stepper motor support
|
|
|
|
class PrinterMultiStepper(PrinterHomingStepper):
|
|
|
|
def __init__(self, printer, config):
|
|
|
|
PrinterHomingStepper.__init__(self, printer, config)
|
|
|
|
self.endstops = PrinterHomingStepper.get_endstops(self)
|
|
|
|
self.extras = []
|
|
|
|
self.all_step_const = [self.step_const]
|
|
|
|
for i in range(1, 99):
|
2018-01-20 06:22:17 +03:00
|
|
|
if not config.has_section(config.get_name() + str(i)):
|
2017-11-07 21:10:08 +03:00
|
|
|
break
|
2018-01-20 06:22:17 +03:00
|
|
|
extraconfig = config.getsection(config.get_name() + str(i))
|
2017-11-07 21:10:08 +03:00
|
|
|
extra = PrinterStepper(printer, extraconfig)
|
|
|
|
self.extras.append(extra)
|
|
|
|
self.all_step_const.append(extra.step_const)
|
|
|
|
extraendstop = extraconfig.get('endstop_pin', None)
|
|
|
|
if extraendstop is not None:
|
2018-04-04 19:07:41 +03:00
|
|
|
ppins = printer.lookup_object('pins')
|
|
|
|
mcu_endstop = ppins.setup_pin('endstop', extraendstop)
|
2017-11-07 21:10:08 +03:00
|
|
|
mcu_endstop.add_stepper(extra.mcu_stepper)
|
2017-12-06 05:51:44 +03:00
|
|
|
self.endstops.append((mcu_endstop, extra.name))
|
2017-11-07 21:10:08 +03:00
|
|
|
else:
|
|
|
|
self.mcu_endstop.add_stepper(extra.mcu_stepper)
|
|
|
|
self.step_const = self.step_multi_const
|
|
|
|
def step_multi_const(self, print_time, start_pos, dist, start_v, accel):
|
|
|
|
for step_const in self.all_step_const:
|
|
|
|
step_const(print_time, start_pos, dist, start_v, accel)
|
|
|
|
def set_max_jerk(self, max_halt_velocity, max_accel):
|
|
|
|
PrinterHomingStepper.set_max_jerk(self, max_halt_velocity, max_accel)
|
|
|
|
for extra in self.extras:
|
|
|
|
extra.set_max_jerk(max_halt_velocity, max_accel)
|
|
|
|
def set_position(self, pos):
|
|
|
|
PrinterHomingStepper.set_position(self, pos)
|
|
|
|
for extra in self.extras:
|
|
|
|
extra.set_position(pos)
|
|
|
|
def motor_enable(self, print_time, enable=0):
|
|
|
|
PrinterHomingStepper.motor_enable(self, print_time, enable)
|
|
|
|
for extra in self.extras:
|
|
|
|
extra.motor_enable(print_time, enable)
|
|
|
|
def get_endstops(self):
|
|
|
|
return self.endstops
|
|
|
|
|
|
|
|
def LookupMultiHomingStepper(printer, config):
|
2018-01-20 06:22:17 +03:00
|
|
|
if not config.has_section(config.get_name() + '1'):
|
2017-11-07 21:10:08 +03:00
|
|
|
return PrinterHomingStepper(printer, config)
|
|
|
|
return PrinterMultiStepper(printer, config)
|