aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/homing.py
blob: 7ec103b034e1db9082fccf07e0e9673dbc5c8bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Code for state tracking during homing operations
#
# Copyright (C) 2016  Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging

class Homing:
    def __init__(self, toolhead, changed_axes):
        self.toolhead = toolhead
        self.changed_axes = changed_axes
        self.verify_retract = True
    def set_no_verify_retract(self):
        self.verify_retract = False
    def set_axes(self, axes):
        self.changed_axes = axes
    def get_axes(self):
        return self.changed_axes
    def _fill_coord(self, coord):
        # Fill in any None entries in 'coord' with current toolhead position
        thcoord = list(self.toolhead.get_position())
        for i in range(len(coord)):
            if coord[i] is not None:
                thcoord[i] = coord[i]
        return thcoord
    def retract(self, newpos, speed):
        self.toolhead.move(self._fill_coord(newpos), speed)
    def home(self, forcepos, movepos, steppers, speed, second_home=False):
        # Alter kinematics class to think printer is at forcepos
        self.toolhead.set_position(self._fill_coord(forcepos))
        # Start homing and issue move
        print_time = self.toolhead.get_last_move_time()
        endstops = []
        for s in steppers:
            es = s.enable_endstop_checking(print_time, s.step_dist / speed)
            endstops.append((s, es, s.mcu_stepper.get_mcu_position()))
        self.toolhead.move(self._fill_coord(movepos), speed)
        move_end_print_time = self.toolhead.get_last_move_time()
        self.toolhead.reset_print_time()
        for s, es, last_pos in endstops:
            es.home_finalize(es.get_mcu().print_to_mcu_time(move_end_print_time))
        # Wait for endstops to trigger
        for s, es, last_pos in endstops:
            try:
                es.home_wait()
            except es.error as e:
                raise EndstopError("Failed to home stepper %s: %s" % (
                    s.name, str(e)))
            post_home_pos = s.mcu_stepper.get_mcu_position()
            if second_home and self.verify_retract and last_pos == post_home_pos:
                raise EndstopError("Endstop %s still triggered after retract" % (
                    s.name,))
    def set_homed_position(self, pos):
        self.toolhead.set_position(self._fill_coord(pos))

class EndstopError(Exception):
    pass

def EndstopMoveError(pos, msg="Move out of range"):
    return EndstopError("%s: %.3f %.3f %.3f [%.3f]" % (
            msg, pos[0], pos[1], pos[2], pos[3]))