2019-02-09 02:11:05 +01:00
|
|
|
# Support for a manual controlled stepper
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2020-06-12 15:55:57 +02:00
|
|
|
import stepper, homing, chelper
|
|
|
|
from . import force_move
|
2019-02-09 02:11:05 +01:00
|
|
|
|
|
|
|
ENDSTOP_SAMPLE_TIME = .000015
|
|
|
|
ENDSTOP_SAMPLE_COUNT = 4
|
|
|
|
|
|
|
|
class ManualStepper:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
if config.get('endstop_pin', None) is not None:
|
|
|
|
self.can_home = True
|
2019-11-12 18:49:21 +01:00
|
|
|
self.rail = stepper.PrinterRail(
|
2019-02-09 02:11:05 +01:00
|
|
|
config, need_position_minmax=False, default_position_endstop=0.)
|
2019-11-12 18:49:21 +01:00
|
|
|
self.steppers = self.rail.get_steppers()
|
2019-02-09 02:11:05 +01:00
|
|
|
else:
|
|
|
|
self.can_home = False
|
2019-11-12 18:49:21 +01:00
|
|
|
self.rail = stepper.PrinterStepper(config)
|
|
|
|
self.steppers = [self.rail]
|
2019-03-03 19:23:45 +01:00
|
|
|
self.velocity = config.getfloat('velocity', 5., above=0.)
|
|
|
|
self.accel = config.getfloat('accel', 0., minval=0.)
|
2019-02-09 02:11:05 +01:00
|
|
|
self.next_cmd_time = 0.
|
|
|
|
# Setup iterative solver
|
|
|
|
ffi_main, ffi_lib = chelper.get_ffi()
|
2019-10-29 16:34:52 +01:00
|
|
|
self.trapq = ffi_main.gc(ffi_lib.trapq_alloc(), ffi_lib.trapq_free)
|
2019-10-29 17:44:39 +01:00
|
|
|
self.trapq_append = ffi_lib.trapq_append
|
2019-10-29 16:34:52 +01:00
|
|
|
self.trapq_free_moves = ffi_lib.trapq_free_moves
|
2019-11-12 18:49:21 +01:00
|
|
|
self.rail.setup_itersolve('cartesian_stepper_alloc', 'x')
|
|
|
|
self.rail.set_trapq(self.trapq)
|
|
|
|
self.rail.set_max_jerk(9999999.9, 9999999.9)
|
2019-02-09 02:11:05 +01:00
|
|
|
# Register commands
|
|
|
|
stepper_name = config.get_name().split()[1]
|
2020-04-25 06:29:15 +02:00
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
gcode.register_mux_command('MANUAL_STEPPER', "STEPPER",
|
|
|
|
stepper_name, self.cmd_MANUAL_STEPPER,
|
|
|
|
desc=self.cmd_MANUAL_STEPPER_help)
|
2019-02-09 02:11:05 +01:00
|
|
|
def sync_print_time(self):
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
print_time = toolhead.get_last_move_time()
|
|
|
|
if self.next_cmd_time > print_time:
|
|
|
|
toolhead.dwell(self.next_cmd_time - print_time)
|
|
|
|
else:
|
|
|
|
self.next_cmd_time = print_time
|
|
|
|
def do_enable(self, enable):
|
|
|
|
self.sync_print_time()
|
2019-11-12 18:49:21 +01:00
|
|
|
stepper_enable = self.printer.lookup_object('stepper_enable')
|
|
|
|
if enable:
|
|
|
|
for s in self.steppers:
|
2019-11-12 19:55:50 +01:00
|
|
|
se = stepper_enable.lookup_enable(s.get_name())
|
|
|
|
se.motor_enable(self.next_cmd_time)
|
2019-11-12 18:49:21 +01:00
|
|
|
else:
|
|
|
|
for s in self.steppers:
|
2019-11-12 19:55:50 +01:00
|
|
|
se = stepper_enable.lookup_enable(s.get_name())
|
|
|
|
se.motor_disable(self.next_cmd_time)
|
2019-02-09 02:11:05 +01:00
|
|
|
self.sync_print_time()
|
|
|
|
def do_set_position(self, setpos):
|
2019-11-12 18:49:21 +01:00
|
|
|
self.rail.set_position([setpos, 0., 0.])
|
2020-03-21 10:54:01 +01:00
|
|
|
def do_move(self, movepos, speed, accel, sync=True):
|
2019-02-09 02:11:05 +01:00
|
|
|
self.sync_print_time()
|
2019-11-12 18:49:21 +01:00
|
|
|
cp = self.rail.get_commanded_position()
|
2019-02-09 02:11:05 +01:00
|
|
|
dist = movepos - cp
|
2019-11-06 14:41:50 +01:00
|
|
|
axis_r, accel_t, cruise_t, cruise_v = force_move.calc_move_time(
|
2019-03-03 19:23:45 +01:00
|
|
|
dist, speed, accel)
|
2019-10-29 17:44:39 +01:00
|
|
|
self.trapq_append(self.trapq, self.next_cmd_time,
|
|
|
|
accel_t, cruise_t, accel_t,
|
2019-11-06 14:41:50 +01:00
|
|
|
cp, 0., 0., axis_r, 0., 0.,
|
2019-10-29 17:44:39 +01:00
|
|
|
0., cruise_v, accel)
|
2019-11-24 16:59:13 +01:00
|
|
|
self.next_cmd_time = self.next_cmd_time + accel_t + cruise_t + accel_t
|
2019-11-12 18:49:21 +01:00
|
|
|
self.rail.generate_steps(self.next_cmd_time)
|
2019-11-24 16:59:13 +01:00
|
|
|
self.trapq_free_moves(self.trapq, self.next_cmd_time + 99999.9)
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
toolhead.note_kinematic_activity(self.next_cmd_time)
|
2020-03-21 10:54:01 +01:00
|
|
|
if sync:
|
|
|
|
self.sync_print_time()
|
2020-02-19 19:34:06 +01:00
|
|
|
def do_homing_move(self, movepos, speed, accel, triggered, check_trigger):
|
2019-02-09 02:11:05 +01:00
|
|
|
if not self.can_home:
|
2020-04-25 06:29:15 +02:00
|
|
|
raise self.printer.command_error(
|
|
|
|
"No endstop for this manual stepper")
|
2020-03-21 02:11:36 +01:00
|
|
|
# Notify start of homing/probing move
|
|
|
|
endstops = self.rail.get_endstops()
|
|
|
|
self.printer.send_event("homing:homing_move_begin",
|
|
|
|
[es for es, name in endstops])
|
2019-02-09 02:11:05 +01:00
|
|
|
# Start endstop checking
|
|
|
|
self.sync_print_time()
|
2020-02-12 19:03:42 +01:00
|
|
|
endstops = self.rail.get_endstops()
|
2019-02-09 02:11:05 +01:00
|
|
|
for mcu_endstop, name in endstops:
|
|
|
|
min_step_dist = min([s.get_step_dist()
|
|
|
|
for s in mcu_endstop.get_steppers()])
|
|
|
|
mcu_endstop.home_start(
|
|
|
|
self.next_cmd_time, ENDSTOP_SAMPLE_TIME, ENDSTOP_SAMPLE_COUNT,
|
|
|
|
min_step_dist / speed, triggered=triggered)
|
|
|
|
# Issue move
|
2019-03-03 19:23:45 +01:00
|
|
|
self.do_move(movepos, speed, accel)
|
2019-02-09 02:11:05 +01:00
|
|
|
# Wait for endstops to trigger
|
|
|
|
error = None
|
|
|
|
for mcu_endstop, name in endstops:
|
2020-02-19 18:07:30 +01:00
|
|
|
did_trigger = mcu_endstop.home_wait(self.next_cmd_time)
|
2020-02-19 19:34:06 +01:00
|
|
|
if not did_trigger and check_trigger and error is None:
|
2020-02-19 18:07:30 +01:00
|
|
|
error = "Failed to home %s: Timeout during homing" % (name,)
|
2020-03-21 02:11:36 +01:00
|
|
|
# Signal homing/probing move complete
|
|
|
|
try:
|
|
|
|
self.printer.send_event("homing:homing_move_end",
|
|
|
|
[es for es, name in endstops])
|
|
|
|
except CommandError as e:
|
|
|
|
if error is None:
|
|
|
|
error = str(e)
|
2019-02-09 02:11:05 +01:00
|
|
|
self.sync_print_time()
|
|
|
|
if error is not None:
|
2019-06-03 19:01:57 +02:00
|
|
|
raise homing.CommandError(error)
|
2019-02-09 02:11:05 +01:00
|
|
|
cmd_MANUAL_STEPPER_help = "Command a manually configured stepper"
|
2020-04-25 06:29:15 +02:00
|
|
|
def cmd_MANUAL_STEPPER(self, gcmd):
|
|
|
|
enable = gcmd.get_int('ENABLE', None)
|
|
|
|
if enable is not None:
|
|
|
|
self.do_enable(enable)
|
|
|
|
setpos = gcmd.get_float('SET_POSITION', None)
|
|
|
|
if setpos is not None:
|
2019-02-09 02:11:05 +01:00
|
|
|
self.do_set_position(setpos)
|
2020-04-25 06:29:15 +02:00
|
|
|
speed = gcmd.get_float('SPEED', self.velocity, above=0.)
|
|
|
|
accel = gcmd.get_float('ACCEL', self.accel, minval=0.)
|
|
|
|
homing_move = gcmd.get_int('STOP_ON_ENDSTOP', 0)
|
2019-02-09 02:11:05 +01:00
|
|
|
if homing_move:
|
2020-04-25 06:29:15 +02:00
|
|
|
movepos = gcmd.get_float('MOVE')
|
2020-02-19 19:34:06 +01:00
|
|
|
self.do_homing_move(movepos, speed, accel,
|
|
|
|
homing_move > 0, abs(homing_move) == 1)
|
2020-04-25 06:29:15 +02:00
|
|
|
elif gcmd.get_float('MOVE', None) is not None:
|
|
|
|
movepos = gcmd.get_float('MOVE')
|
|
|
|
sync = gcmd.get_int('SYNC', 1)
|
2020-03-21 10:54:01 +01:00
|
|
|
self.do_move(movepos, speed, accel, sync)
|
2020-04-25 06:29:15 +02:00
|
|
|
elif gcmd.get_int('SYNC', 0):
|
2020-03-21 10:54:01 +01:00
|
|
|
self.sync_print_time()
|
2019-02-09 02:11:05 +01:00
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return ManualStepper(config)
|