2016-05-25 17:37:40 +02:00
|
|
|
# Printer stepper support
|
|
|
|
#
|
2019-06-13 05:06:41 +02:00
|
|
|
# Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-06-22 00:48:33 +02:00
|
|
|
import math, logging, collections
|
2018-07-13 17:24:36 +02:00
|
|
|
import homing
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2018-06-22 17:44:25 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Stepper enable pins
|
|
|
|
######################################################################
|
|
|
|
|
2018-01-10 20:49:00 +01: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)
|
|
|
|
|
2019-06-13 05:06:41 +02:00
|
|
|
class StepperMultiEnablePin:
|
|
|
|
def __init__(self, enable_list):
|
|
|
|
self.enable_list = enable_list
|
|
|
|
def set_enable(self, print_time, enable):
|
|
|
|
for en in self.enable_list:
|
|
|
|
en.set_enable(print_time, enable)
|
|
|
|
|
|
|
|
def lookup_enable_pin(ppins, pin_list):
|
|
|
|
if pin_list is None:
|
2018-01-10 20:49:00 +01:00
|
|
|
return StepperEnablePin(None, 9999)
|
2019-06-13 05:06:41 +02:00
|
|
|
enable_list = []
|
|
|
|
for pin in pin_list.split(','):
|
|
|
|
pin_params = ppins.lookup_pin(pin, can_invert=True,
|
|
|
|
share_type='stepper_enable')
|
|
|
|
enable = pin_params.get('class')
|
|
|
|
if enable is None:
|
|
|
|
mcu_enable = pin_params['chip'].setup_pin('digital_out', pin_params)
|
|
|
|
mcu_enable.setup_max_duration(0.)
|
|
|
|
pin_params['class'] = enable = StepperEnablePin(mcu_enable)
|
|
|
|
enable_list.append(enable)
|
|
|
|
if len(enable_list) == 1:
|
|
|
|
return enable_list[0]
|
|
|
|
return StepperMultiEnablePin(enable_list)
|
2018-01-10 20:49:00 +01:00
|
|
|
|
2018-06-22 17:44:25 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Steppers
|
|
|
|
######################################################################
|
|
|
|
|
2017-11-07 00:37:35 +01:00
|
|
|
# Code storing the definitions for a stepper motor
|
2016-05-25 17:37:40 +02:00
|
|
|
class PrinterStepper:
|
2018-06-21 19:47:39 +02:00
|
|
|
def __init__(self, config):
|
|
|
|
printer = config.get_printer()
|
2018-01-20 04:22:17 +01:00
|
|
|
self.name = config.get_name()
|
2017-11-07 18:12:28 +01:00
|
|
|
self.need_motor_enable = True
|
|
|
|
# Stepper definition
|
2018-04-04 18:07:41 +02:00
|
|
|
ppins = printer.lookup_object('pins')
|
2018-07-26 16:11:56 +02:00
|
|
|
step_pin = config.get('step_pin')
|
|
|
|
self.mcu_stepper = mcu_stepper = ppins.setup_pin('stepper', step_pin)
|
2018-07-26 15:44:45 +02:00
|
|
|
dir_pin = config.get('dir_pin')
|
|
|
|
dir_pin_params = ppins.lookup_pin(dir_pin, can_invert=True)
|
2018-07-26 16:11:56 +02:00
|
|
|
mcu_stepper.setup_dir_pin(dir_pin_params)
|
2018-06-21 20:38:39 +02:00
|
|
|
step_dist = config.getfloat('step_distance', above=0.)
|
2018-07-26 16:11:56 +02:00
|
|
|
mcu_stepper.setup_step_distance(step_dist)
|
2019-11-10 17:55:53 +01:00
|
|
|
# Enable pin handling
|
|
|
|
stepper_enable = printer.try_load_module(config, 'stepper_enable')
|
2019-11-12 17:41:41 +01:00
|
|
|
stepper_enable.register_stepper(self)
|
2019-10-28 02:05:57 +01:00
|
|
|
mcu_stepper.add_active_callback(self._stepper_active)
|
2018-04-04 18:07:41 +02:00
|
|
|
self.enable = lookup_enable_pin(ppins, config.get('enable_pin', None))
|
2018-05-24 15:42:33 +02:00
|
|
|
# Register STEPPER_BUZZ command
|
2018-07-26 17:15:51 +02:00
|
|
|
force_move = printer.try_load_module(config, 'force_move')
|
|
|
|
force_move.register_stepper(self)
|
2018-06-21 20:38:39 +02:00
|
|
|
# Wrappers
|
2018-07-26 16:11:56 +02:00
|
|
|
self.setup_itersolve = mcu_stepper.setup_itersolve
|
2019-10-28 02:05:57 +01:00
|
|
|
self.generate_steps = mcu_stepper.generate_steps
|
|
|
|
self.set_trapq = mcu_stepper.set_trapq
|
2018-07-26 16:11:56 +02:00
|
|
|
self.set_stepper_kinematics = mcu_stepper.set_stepper_kinematics
|
|
|
|
self.calc_position_from_coord = mcu_stepper.calc_position_from_coord
|
|
|
|
self.set_position = mcu_stepper.set_position
|
|
|
|
self.get_commanded_position = mcu_stepper.get_commanded_position
|
2018-10-09 02:50:45 +02:00
|
|
|
self.set_commanded_position = mcu_stepper.set_commanded_position
|
|
|
|
self.get_mcu_position = mcu_stepper.get_mcu_position
|
2018-07-26 16:11:56 +02:00
|
|
|
self.get_step_dist = mcu_stepper.get_step_dist
|
2019-06-07 00:14:45 +02:00
|
|
|
self.is_dir_inverted = mcu_stepper.is_dir_inverted
|
2018-06-21 20:33:55 +02:00
|
|
|
def get_name(self, short=False):
|
|
|
|
if short and self.name.startswith('stepper_'):
|
|
|
|
return self.name[8:]
|
|
|
|
return self.name
|
2018-06-22 00:15:23 +02:00
|
|
|
def add_to_endstop(self, mcu_endstop):
|
|
|
|
mcu_endstop.add_stepper(self.mcu_stepper)
|
2017-07-24 19:54:46 +02: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
|
2018-06-21 20:38:39 +02:00
|
|
|
step_dist = self.get_step_dist()
|
2017-07-24 19:54:46 +02:00
|
|
|
last_step_time = self._dist_to_time(
|
2018-06-21 20:38:39 +02:00
|
|
|
step_dist, max_halt_velocity, max_accel)
|
2017-07-24 19:54:46 +02:00
|
|
|
second_last_step_time = self._dist_to_time(
|
2018-06-21 20:38:39 +02:00
|
|
|
2. * step_dist, max_halt_velocity, max_accel)
|
2017-07-24 19:54:46 +02:00
|
|
|
min_stop_interval = second_last_step_time - last_step_time
|
2017-08-21 17:25:26 +02:00
|
|
|
self.mcu_stepper.setup_min_stop_interval(min_stop_interval)
|
2019-10-28 02:05:57 +01:00
|
|
|
def _stepper_active(self, active_time):
|
|
|
|
self.motor_enable(active_time, 1)
|
2017-09-12 18:47:40 +02:00
|
|
|
def motor_enable(self, print_time, enable=0):
|
2018-01-10 20:49:00 +01:00
|
|
|
if self.need_motor_enable != (not enable):
|
|
|
|
self.enable.set_enable(print_time, enable)
|
2017-07-24 19:54:46 +02:00
|
|
|
self.need_motor_enable = not enable
|
2019-10-28 02:05:57 +01:00
|
|
|
if not enable:
|
|
|
|
# Enable stepper on future stepper movement
|
|
|
|
self.mcu_stepper.add_active_callback(self._stepper_active)
|
2018-06-21 23:58:18 +02:00
|
|
|
def is_motor_enabled(self):
|
|
|
|
return not self.need_motor_enable
|
2017-07-24 19:54:46 +02:00
|
|
|
|
2018-06-22 17:44:25 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Stepper controlled rails
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# A motor control "rail" with one (or more) steppers and one (or more)
|
|
|
|
# endstops.
|
|
|
|
class PrinterRail:
|
2018-06-21 19:47:39 +02:00
|
|
|
def __init__(self, config, need_position_minmax=True,
|
2018-05-19 01:34:17 +02:00
|
|
|
default_position_endstop=None):
|
2018-06-22 17:44:25 +02:00
|
|
|
# Primary stepper
|
|
|
|
stepper = PrinterStepper(config)
|
|
|
|
self.steppers = [stepper]
|
|
|
|
self.name = stepper.get_name(short=True)
|
|
|
|
self.get_commanded_position = stepper.get_commanded_position
|
|
|
|
self.is_motor_enabled = stepper.is_motor_enabled
|
|
|
|
# Primary endstop and its position
|
2018-07-16 16:06:30 +02:00
|
|
|
printer = config.get_printer()
|
|
|
|
ppins = printer.lookup_object('pins')
|
2018-06-22 17:44:25 +02:00
|
|
|
mcu_endstop = ppins.setup_pin('endstop', config.get('endstop_pin'))
|
|
|
|
self.endstops = [(mcu_endstop, self.name)]
|
|
|
|
stepper.add_to_endstop(mcu_endstop)
|
2018-09-27 01:29:58 +02:00
|
|
|
if hasattr(mcu_endstop, "get_position_endstop"):
|
|
|
|
self.position_endstop = mcu_endstop.get_position_endstop()
|
|
|
|
elif default_position_endstop is None:
|
2017-11-02 02:21:37 +01:00
|
|
|
self.position_endstop = config.getfloat('position_endstop')
|
|
|
|
else:
|
|
|
|
self.position_endstop = config.getfloat(
|
2018-05-19 01:34:17 +02:00
|
|
|
'position_endstop', default_position_endstop)
|
2018-07-16 16:06:30 +02:00
|
|
|
query_endstops = printer.try_load_module(config, 'query_endstops')
|
|
|
|
query_endstops.register_endstop(mcu_endstop, self.name)
|
2017-11-07 00:37:35 +01:00
|
|
|
# Axis range
|
2018-05-19 01:34:17 +02:00
|
|
|
if need_position_minmax:
|
|
|
|
self.position_min = config.getfloat('position_min', 0.)
|
|
|
|
self.position_max = config.getfloat(
|
|
|
|
'position_max', above=self.position_min)
|
|
|
|
else:
|
|
|
|
self.position_min = 0.
|
|
|
|
self.position_max = self.position_endstop
|
|
|
|
if (self.position_endstop < self.position_min
|
|
|
|
or self.position_endstop > self.position_max):
|
|
|
|
raise config.error(
|
|
|
|
"position_endstop in section '%s' must be between"
|
|
|
|
" position_min and position_max" % config.get_name())
|
2017-11-07 00:37:35 +01:00
|
|
|
# Homing mechanics
|
2017-04-11 17:37:09 +02:00
|
|
|
self.homing_speed = config.getfloat('homing_speed', 5.0, above=0.)
|
2018-10-08 18:54:33 +02:00
|
|
|
self.second_homing_speed = config.getfloat(
|
|
|
|
'second_homing_speed', self.homing_speed/2., above=0.)
|
2017-11-07 00:37:35 +01:00
|
|
|
self.homing_retract_dist = config.getfloat(
|
2018-03-06 07:59:13 +01:00
|
|
|
'homing_retract_dist', 5., minval=0.)
|
2018-07-26 16:11:56 +02:00
|
|
|
self.homing_positive_dir = config.getboolean(
|
|
|
|
'homing_positive_dir', None)
|
2017-07-24 20:12:17 +02:00
|
|
|
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 04:22:17 +01:00
|
|
|
config.get_name(),))
|
2018-06-22 17:44:25 +02:00
|
|
|
def get_range(self):
|
|
|
|
return self.position_min, self.position_max
|
|
|
|
def get_homing_info(self):
|
|
|
|
homing_info = collections.namedtuple('homing_info', [
|
2018-10-08 18:54:33 +02:00
|
|
|
'speed', 'position_endstop', 'retract_dist', 'positive_dir',
|
|
|
|
'second_homing_speed'])(
|
2018-06-22 17:44:25 +02:00
|
|
|
self.homing_speed, self.position_endstop,
|
2018-10-08 18:54:33 +02:00
|
|
|
self.homing_retract_dist, self.homing_positive_dir,
|
|
|
|
self.second_homing_speed)
|
2018-06-22 17:44:25 +02:00
|
|
|
return homing_info
|
|
|
|
def get_steppers(self):
|
|
|
|
return list(self.steppers)
|
|
|
|
def get_endstops(self):
|
|
|
|
return list(self.endstops)
|
|
|
|
def add_extra_stepper(self, config):
|
|
|
|
stepper = PrinterStepper(config)
|
|
|
|
self.steppers.append(stepper)
|
|
|
|
mcu_endstop = self.endstops[0][0]
|
|
|
|
endstop_pin = config.get('endstop_pin', None)
|
|
|
|
if endstop_pin is not None:
|
2018-07-16 16:06:30 +02:00
|
|
|
printer = config.get_printer()
|
|
|
|
ppins = printer.lookup_object('pins')
|
2018-06-22 17:44:25 +02:00
|
|
|
mcu_endstop = ppins.setup_pin('endstop', endstop_pin)
|
2018-07-16 16:06:30 +02:00
|
|
|
name = stepper.get_name(short=True)
|
|
|
|
self.endstops.append((mcu_endstop, name))
|
|
|
|
query_endstops = printer.try_load_module(config, 'query_endstops')
|
|
|
|
query_endstops.register_endstop(mcu_endstop, name)
|
2018-06-22 17:44:25 +02:00
|
|
|
stepper.add_to_endstop(mcu_endstop)
|
|
|
|
def add_to_endstop(self, mcu_endstop):
|
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.add_to_endstop(mcu_endstop)
|
2018-07-13 17:24:36 +02:00
|
|
|
def setup_itersolve(self, alloc_func, *params):
|
2018-06-22 17:44:25 +02:00
|
|
|
for stepper in self.steppers:
|
2018-07-13 17:24:36 +02:00
|
|
|
stepper.setup_itersolve(alloc_func, *params)
|
2019-10-28 02:05:57 +01:00
|
|
|
def generate_steps(self, flush_time):
|
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.generate_steps(flush_time)
|
|
|
|
def set_trapq(self, trapq):
|
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.set_trapq(trapq)
|
2017-11-07 19:10:08 +01:00
|
|
|
def set_max_jerk(self, max_halt_velocity, max_accel):
|
2018-06-22 17:44:25 +02:00
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.set_max_jerk(max_halt_velocity, max_accel)
|
2018-10-09 02:50:45 +02:00
|
|
|
def set_commanded_position(self, pos):
|
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.set_commanded_position(pos)
|
|
|
|
def set_position(self, coord):
|
2018-06-22 17:44:25 +02:00
|
|
|
for stepper in self.steppers:
|
2018-10-09 02:50:45 +02:00
|
|
|
stepper.set_position(coord)
|
2017-11-07 19:10:08 +01:00
|
|
|
def motor_enable(self, print_time, enable=0):
|
2018-06-22 17:44:25 +02:00
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.motor_enable(print_time, enable)
|
2017-11-07 19:10:08 +01:00
|
|
|
|
2018-06-22 17:44:25 +02:00
|
|
|
# Wrapper for dual stepper motor support
|
|
|
|
def LookupMultiRail(config):
|
|
|
|
rail = PrinterRail(config)
|
|
|
|
for i in range(1, 99):
|
|
|
|
if not config.has_section(config.get_name() + str(i)):
|
|
|
|
break
|
|
|
|
rail.add_extra_stepper(config.getsection(config.get_name() + str(i)))
|
|
|
|
return rail
|