2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Multi-processor safe interface to micro-controller
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Copyright (C) 2016,2017  Kevin O'Connor <kevin@koconnor.net>
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# This file may be distributed under the terms of the GNU GPLv3 license.
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import sys, os, zlib, logging, math
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import serialhdl, pins, chelper
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-29 18:27:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								class error(Exception):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    pass
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def parse_pin_extras(pin, can_pullup=False):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    pullup = invert = 0
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if can_pullup and pin.startswith('^'):
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-08 09:29:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        pullup = 1
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        pin = pin[1:].strip()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if pin.startswith('!'):
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-08 09:29:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        invert = 1
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        pin = pin[1:].strip()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return pin, pullup, invert
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								STEPCOMPRESS_ERROR_RET = -989898989
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class MCU_stepper:
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, mcu, step_pin, dir_pin):
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu = mcu
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._oid = mcu.create_oid(self)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._step_pin, pullup, self._invert_step = parse_pin_extras(step_pin)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._dir_pin, pullup, self._invert_dir = parse_pin_extras(dir_pin)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._commanded_pos = 0
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 12:31:03 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._step_dist = self._inv_step_dist = 1.
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:54:32 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._velocity_factor = self._accel_factor = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 11:22:01 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_position_offset = 0
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = self._min_stop_interval = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._reset_cmd = self._get_position_cmd = None
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._ffi_lib = self._stepqueue = None
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.print_to_mcu_time = mcu.print_to_mcu_time
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_min_stop_interval(self, min_stop_interval):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._min_stop_interval = min_stop_interval
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 12:31:03 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_step_distance(self, step_dist):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._step_dist = step_dist
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._inv_step_dist = 1. / step_dist
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def build_config(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = self._mcu.get_mcu_freq()
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 12:31:03 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._velocity_factor = 1. / (self._mcu_freq * self._step_dist)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:54:32 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._accel_factor = 1. / (self._mcu_freq**2 * self._step_dist)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        max_error = self._mcu.get_max_stepper_error()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        min_stop_interval = max(0., self._min_stop_interval - max_error)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.add_config_cmd(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "config_stepper oid=%d step_pin=%s dir_pin=%s"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            " min_stop_interval=TICKS(%.9f) invert_step=%d" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._oid, self._step_pin, self._dir_pin,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                min_stop_interval, self._invert_step))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.register_stepper(self)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        step_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "queue_step oid=%c interval=%u count=%hu add=%hi")
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        dir_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "set_next_step_dir oid=%c dir=%c")
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._reset_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "reset_step_clock oid=%c clock=%u")
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._get_position_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:00:15 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            "stepper_get_position oid=%c")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ffi_main, self._ffi_lib = chelper.get_ffi()
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        max_error = int(max_error * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc(
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            max_error, step_cmd.msgid, dir_cmd.msgid,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._invert_dir, self._oid),
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                                      self._ffi_lib.stepcompress_free)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def get_oid(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self._oid
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-19 11:34:42 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_position(self, pos):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:20:54 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if pos >= 0.:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            steppos = int(pos * self._inv_step_dist + 0.5)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            steppos = int(pos * self._inv_step_dist - 0.5)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_position_offset += self._commanded_pos - steppos
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._commanded_pos = steppos
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def get_commanded_position(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:20:54 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return self._commanded_pos * self._step_dist
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 11:22:01 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def get_mcu_position(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return self._commanded_pos + self._mcu_position_offset
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-30 17:02:28 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def note_homing_start(self, homing_clock):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._ffi_lib.stepcompress_set_homing(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._stepqueue, homing_clock)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-30 17:02:28 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def note_homing_finalized(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._ffi_lib.stepcompress_set_homing(self._stepqueue, 0)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._ffi_lib.stepcompress_reset(self._stepqueue, 0)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:00:15 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def note_homing_triggered(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        params = self._mcu.serial.send_with_response(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._get_position_cmd.encode(self._oid),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            'stepper_position', self._oid)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        pos = params['pos']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if self._invert_dir:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            pos = -pos
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_position_offset = pos - self._commanded_pos
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-14 13:10:14 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def reset_step_clock(self, mcu_time):
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-10 12:44:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        clock = int(mcu_time * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._ffi_lib.stepcompress_reset(self._stepqueue, clock)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        data = (self._reset_cmd.msgid, self._oid, clock & 0xffffffff)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._ffi_lib.stepcompress_queue_msg(
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._stepqueue, data, len(data))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-10 12:44:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def step(self, mcu_time, sdir):
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 16:42:25 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        clock = mcu_time * self._mcu_freq
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._ffi_lib.stepcompress_push(self._stepqueue, clock, sdir)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-19 11:34:42 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if sdir:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._commanded_pos += 1
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-19 11:34:42 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._commanded_pos -= 1
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 12:51:52 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def step_const(self, mcu_time, start_pos, dist, start_v, accel):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 12:31:03 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        inv_step_dist = self._inv_step_dist
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        step_offset = self._commanded_pos - start_pos * inv_step_dist
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:54:32 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        count = self._ffi_lib.stepcompress_push_const(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._stepqueue, mcu_time * self._mcu_freq, step_offset,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            dist * inv_step_dist, start_v * self._velocity_factor,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            accel * self._accel_factor)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if count == STEPCOMPRESS_ERROR_RET:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._commanded_pos += count
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def step_delta(self, mcu_time, dist, start_v, accel
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                   , height_base, startxy_d, arm_d, movez_r):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 21:59:28 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        inv_step_dist = self._inv_step_dist
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        height = self._commanded_pos - height_base * inv_step_dist
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 10:49:14 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        count = self._ffi_lib.stepcompress_push_delta(
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._stepqueue, mcu_time * self._mcu_freq, dist * inv_step_dist,
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 21:59:28 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            start_v * self._velocity_factor, accel * self._accel_factor,
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            height, startxy_d * inv_step_dist, arm_d * inv_step_dist, movez_r)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if count == STEPCOMPRESS_ERROR_RET:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._commanded_pos += count
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class MCU_endstop:
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    error = error
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    RETRY_QUERY = 1.000
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, mcu, pin):
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu = mcu
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._oid = mcu.create_oid(self)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._steppers = []
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._pin, self._pullup, self._invert = parse_pin_extras(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            pin, can_pullup=True)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._cmd_queue = mcu.alloc_command_queue()
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._home_cmd = self._query_cmd = None
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._homing = False
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._min_query_time = self._mcu_freq = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._next_query_clock = self._home_timeout_clock = 0
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._retry_query_ticks = 0
							 | 
						
					
						
							
								
									
										
										
										
											2016-10-10 23:41:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._last_state = {}
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        mcu.add_init_callback(self._init_callback)
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 16:42:25 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.print_to_mcu_time = mcu.print_to_mcu_time
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def add_stepper(self, stepper):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._steppers.append(stepper)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def build_config(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = self._mcu.get_mcu_freq()
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu.add_config_cmd(
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            "config_end_stop oid=%d pin=%s pull_up=%d stepper_count=%d" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._oid, self._pin, self._pullup, len(self._steppers)))
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._retry_query_ticks = int(self._mcu_freq * self.RETRY_QUERY)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._home_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "end_stop_home oid=%c clock=%u rest_ticks=%u pin_value=%c")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._query_cmd = self._mcu.lookup_command("end_stop_query oid=%c")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.register_msg(self._handle_end_stop_state, "end_stop_state"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                               , self._oid)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _init_callback(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        set_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "end_stop_set_stepper oid=%c pos=%c stepper_oid=%c")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for i, s in enumerate(self._steppers):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            msg = set_cmd.encode(self._oid, i, s.get_oid())
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._mcu.send(msg, cq=self._cmd_queue)
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def home_start(self, mcu_time, rest_time):
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 16:42:25 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        clock = int(mcu_time * self._mcu_freq)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        rest_ticks = int(rest_time * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._homing = True
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._min_query_time = self._mcu.monotonic()
							 | 
						
					
						
							
								
									
										
										
										
											2016-10-10 23:41:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._next_query_clock = clock + self._retry_query_ticks
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        msg = self._home_cmd.encode(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._oid, clock, rest_ticks, 1 ^ self._invert)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for s in self._steppers:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            s.note_homing_start(clock)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def home_finalize(self, mcu_time):
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for s in self._steppers:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            s.note_homing_finalized()
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._home_timeout_clock = int(mcu_time * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def home_wait(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        eventtime = self._mcu.monotonic()
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        while self._check_busy(eventtime):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            eventtime = self._mcu.pause(eventtime + 0.1)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def _handle_end_stop_state(self, params):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        logging.debug("end_stop_state %s" % (params,))
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._last_state = params
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _check_busy(self, eventtime):
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        # Check if need to send an end_stop_query command
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-23 16:18:08 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._mcu.is_fileoutput():
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return False
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        last_sent_time = self._last_state.get('#sent_time', -1.)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if last_sent_time >= self._min_query_time:
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 11:22:01 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if not self._homing:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return False
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if not self._last_state.get('homing', 0):
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                for s in self._steppers:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    s.note_homing_triggered()
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 11:22:01 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                self._homing = False
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                return False
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if (self._mcu.serial.get_clock(last_sent_time)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                > self._home_timeout_clock):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                # Timeout - disable endstop checking
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                msg = self._home_cmd.encode(self._oid, 0, 0, 0)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._mcu.send(msg, reqclock=0, cq=self._cmd_queue)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-29 18:27:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                raise error("Timeout during endstop homing")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._mcu.is_shutdown:
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-29 18:27:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            raise error("MCU is shutdown")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 19:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        last_clock, last_clock_time = self._mcu.get_last_clock()
							 | 
						
					
						
							
								
									
										
										
										
											2016-10-10 23:41:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if last_clock >= self._next_query_clock:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._next_query_clock = last_clock + self._retry_query_ticks
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            msg = self._query_cmd.encode(self._oid)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._mcu.send(msg, cq=self._cmd_queue)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return True
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-17 17:24:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def query_endstop(self, mcu_time):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        clock = int(mcu_time * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._homing = False
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._min_query_time = self._mcu.monotonic()
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-17 17:24:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._next_query_clock = clock
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def query_endstop_wait(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        eventtime = self._mcu.monotonic()
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        while self._check_busy(eventtime):
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            eventtime = self._mcu.pause(eventtime + 0.1)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return self._last_state.get('pin', self._invert) ^ self._invert
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class MCU_digital_out:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, mcu, pin, max_duration):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu = mcu
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._oid = mcu.create_oid(self)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        pin, pullup, self._invert = parse_pin_extras(pin)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._last_clock = 0
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._last_value = None
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._cmd_queue = mcu.alloc_command_queue()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        mcu.add_config_cmd(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "config_digital_out oid=%d pin=%s default_value=%d"
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 19:55:56 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            " max_duration=TICKS(%f)" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._oid, pin, self._invert, max_duration))
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._set_cmd = None
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 16:42:25 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.print_to_mcu_time = mcu.print_to_mcu_time
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def build_config(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = self._mcu.get_mcu_freq()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._set_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "schedule_digital_out oid=%c clock=%u value=%c")
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 16:42:25 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_digital(self, mcu_time, value):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        clock = int(mcu_time * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        msg = self._set_cmd.encode(self._oid, clock, value ^ self._invert)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.send(msg, minclock=self._last_clock, reqclock=clock
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                      , cq=self._cmd_queue)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._last_clock = clock
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._last_value = value
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def get_last_setting(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self._last_value
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_pwm(self, mcu_time, value):
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        dval = 0
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 11:24:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if value >= 0.5:
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            dval = 1
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 16:42:25 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.set_digital(mcu_time, dval)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class MCU_pwm:
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 11:24:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    PWM_MAX = 255.
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 19:55:56 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, mcu, pin, cycle_time, hard_cycle_ticks, max_duration):
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu = mcu
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._hard_cycle_ticks = hard_cycle_ticks
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._oid = mcu.create_oid(self)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-09 00:44:25 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        pin, pullup, self._invert = parse_pin_extras(pin)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._last_clock = 0
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._cmd_queue = mcu.alloc_command_queue()
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 19:55:56 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if hard_cycle_ticks:
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            mcu.add_config_cmd(
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-09 00:44:25 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                "config_pwm_out oid=%d pin=%s cycle_ticks=%d default_value=%d"
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 19:55:56 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                " max_duration=TICKS(%f)" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    self._oid, pin, hard_cycle_ticks, self._invert,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    max_duration))
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            mcu.add_config_cmd(
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 19:55:56 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                "config_soft_pwm_out oid=%d pin=%s cycle_ticks=TICKS(%f)"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                " default_value=%d max_duration=TICKS(%f)" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    self._oid, pin, cycle_time, self._invert, max_duration))
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._set_cmd = None
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.print_to_mcu_time = mcu.print_to_mcu_time
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def build_config(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = self._mcu.get_mcu_freq()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if self._hard_cycle_ticks:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._set_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                "schedule_pwm_out oid=%c clock=%u value=%c")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._set_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                "schedule_soft_pwm_out oid=%c clock=%u value=%c")
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_pwm(self, mcu_time, value):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        clock = int(mcu_time * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-09 00:44:25 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._invert:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            value = 1. - value
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 11:24:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        value = int(value * self.PWM_MAX + 0.5)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        msg = self._set_cmd.encode(self._oid, clock, value)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.send(msg, minclock=self._last_clock, reqclock=clock
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                      , cq=self._cmd_queue)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._last_clock = clock
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class MCU_adc:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, mcu, pin):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu = mcu
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._oid = mcu.create_oid(self)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._min_sample = self._max_sample = 0.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._sample_time = self._report_time = 0.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._sample_count = 0
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._report_clock = 0
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._callback = None
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._inv_max_adc = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._cmd_queue = mcu.alloc_command_queue()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        mcu.add_config_cmd("config_analog_in oid=%d pin=%s" % (self._oid, pin))
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._query_cmd = None
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-22 12:37:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        mcu.add_init_callback(self._init_callback)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._query_cmd = None
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def build_config(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = self._mcu.get_mcu_freq()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._query_cmd = self._mcu.lookup_command(
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "query_analog_in oid=%c clock=%u sample_ticks=%u sample_count=%c"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            " rest_ticks=%u min_value=%hu max_value=%hu")
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_minmax(self, sample_time, sample_count, minval=0., maxval=1.):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._sample_time = sample_time
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._sample_count = sample_count
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._min_sample = minval
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._max_sample = maxval
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-22 12:37:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _init_callback(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if not self._sample_count:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 19:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        last_clock, last_clock_time = self._mcu.get_last_clock()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        clock = last_clock + int(self._mcu_freq * (1.0 + self._oid * 0.01)) # XXX
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        sample_ticks = int(self._sample_time * self._mcu_freq)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        mcu_adc_max = self._mcu.serial.msgparser.get_constant_float("ADC_MAX")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        max_adc = self._sample_count * mcu_adc_max
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._inv_max_adc = 1.0 / max_adc
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-16 14:38:35 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._report_clock = int(self._report_time * self._mcu_freq)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.register_msg(self._handle_analog_in_state, "analog_in_state"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                               , self._oid)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        min_sample = int(self._min_sample * max_adc)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        max_sample = min(0xffff, int(math.ceil(self._max_sample * max_adc)))
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        msg = self._query_cmd.encode(
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._oid, clock, sample_ticks, self._sample_count
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            , self._report_clock, min_sample, max_sample)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def _handle_analog_in_state(self, params):
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        last_value = params['value'] * self._inv_max_adc
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        next_clock = self._mcu.serial.translate_clock(params['next_clock'])
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        last_read_time = (next_clock - self._report_clock) / self._mcu_freq
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if self._callback is not None:
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._callback(last_read_time, last_value)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-22 12:37:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def set_adc_callback(self, report_time, callback):
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._report_time = report_time
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-22 12:37:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._callback = callback
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class MCU:
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    error = error
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 19:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    COMM_TIMEOUT = 3.5
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, printer, config):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._printer = printer
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Serial port
							 | 
						
					
						
							
								
									
										
										
										
											2017-01-02 12:22:43 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        baud = config.getint('baud', 250000)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 22:26:10 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._serialport = config.get('serial', '/dev/ttyS0')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.serial = serialhdl.SerialReader(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            printer.reactor, self._serialport, baud)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.is_shutdown = False
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 21:42:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._shutdown_msg = ""
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-23 16:18:08 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._is_fileoutput = False
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 19:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._timeout_timer = printer.reactor.register_timer(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.timeout_handler)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        rmethods = {m: m for m in ['arduino', 'command', 'rpi_usb']}
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-02 23:18:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._restart_method = config.getchoice(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            'restart_method', rmethods, 'arduino')
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Config building
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-02 23:18:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._config_error = config.error
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._emergency_stop_cmd = self._reset_cmd = None
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._oids = []
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._config_cmds = []
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._config_crc = None
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-22 12:37:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._init_callbacks = []
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 23:05:01 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._pin_map = config.get('pin_map', None)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._custom = config.get('custom', '')
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Move command queuing
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ffi_main, self._ffi_lib = chelper.get_ffi()
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-11 11:37:09 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._max_stepper_error = config.getfloat(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            'max_stepper_error', 0.000025, minval=0.)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._steppers = []
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._steppersync = None
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Print time to clock epoch calculations
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:07:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._print_start_time = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Stats
							 | 
						
					
						
							
								
									
										
										
										
											2017-01-09 23:08:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._stats_sumsq_base = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_tick_avg = 0.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_tick_stddev = 0.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def handle_mcu_stats(self, params):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        count = params['count']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        tick_sum = params['sum']
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        c = 1.0 / (count * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_tick_avg = tick_sum * c
							 | 
						
					
						
							
								
									
										
										
										
											2017-01-09 23:08:23 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        tick_sumsq = params['sumsq'] * self._stats_sumsq_base
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._mcu_tick_stddev = c * math.sqrt(count*tick_sumsq - tick_sum**2)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def handle_shutdown(self, params):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if self.is_shutdown:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.is_shutdown = True
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 21:42:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._shutdown_msg = params['#msg']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        logging.info("%s: %s" % (params['#name'], self._shutdown_msg))
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-14 10:06:11 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        pst = self._print_start_time
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        logging.info("Clock last synchronized at %.6f (%d)" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            pst, int(pst * self._mcu_freq)))
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.serial.dump_debug()
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 21:42:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._printer.note_shutdown(self._shutdown_msg)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # Connection phase
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _check_restart(self, reason):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if self._printer.get_startup_state() == 'firmware_restart':
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        logging.info("Attempting automated firmware restart: %s" % (reason,))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._printer.request_exit('firmware_restart')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._printer.reactor.pause(self._printer.reactor.monotonic() + 2.000)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        raise error("Attempt firmware restart failed")
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def connect(self):
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-27 17:45:58 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if not self._is_fileoutput:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if (self._restart_method == 'rpi_usb'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                and not os.path.exists(self._serialport)):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                # Try toggling usb power
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._check_restart("enable power")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-27 17:45:58 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self.serial.connect()
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 19:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._printer.reactor.update_timer(
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                self._timeout_timer, self.monotonic() + self.COMM_TIMEOUT)
							 | 
						
					
						
							
								
									
										
										
										
											2017-01-09 23:50:13 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._mcu_freq = self.serial.msgparser.get_constant_float('CLOCK_FREQ')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._stats_sumsq_base = self.serial.msgparser.get_constant_float(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            'STATS_SUMSQ_BASE')
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 19:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._emergency_stop_cmd = self.lookup_command("emergency_stop")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-02 23:18:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        try:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._reset_cmd = self.lookup_command("reset")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        except self.serial.msgparser.error, e:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            pass
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.register_msg(self.handle_shutdown, 'shutdown')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.register_msg(self.handle_shutdown, 'is_shutdown')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.register_msg(self.handle_mcu_stats, 'stats')
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._build_config()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._send_config()
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def connect_file(self, debugoutput, dictionary, pace=False):
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-23 16:18:08 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._is_fileoutput = True
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.serial.connect_file(debugoutput, dictionary)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if not pace:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            def dummy_set_print_start_time(eventtime):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                pass
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            def dummy_get_print_buffer_time(eventtime, last_move_end):
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                return 1.250
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.set_print_start_time = dummy_set_print_start_time
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.get_print_buffer_time = dummy_get_print_buffer_time
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 19:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def timeout_handler(self, eventtime):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        last_clock, last_clock_time = self.serial.get_last_clock()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        timeout = last_clock_time + self.COMM_TIMEOUT
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if eventtime < timeout:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return timeout
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        logging.info("Timeout with firmware (eventtime=%f last_status=%f)" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            eventtime, last_clock_time))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._printer.note_mcu_error("Lost communication with firmware")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self._printer.reactor.NEVER
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def disconnect(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.serial.disconnect()
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 01:58:45 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._steppersync is not None:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._ffi_lib.steppersync_free(self._steppersync)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 01:58:45 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._steppersync = None
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def stats(self, eventtime):
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return "%s mcu_task_avg=%.06f mcu_task_stddev=%.06f" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.serial.stats(eventtime),
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self._mcu_tick_avg, self._mcu_tick_stddev)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def force_shutdown(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.send(self._emergency_stop_cmd.encode())
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 22:26:10 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def microcontroller_restart(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-02 23:18:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        reactor = self._printer.reactor
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._restart_method == 'rpi_usb':
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            logging.info("Attempting a microcontroller reset via rpi usb power")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.disconnect()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            chelper.run_hub_ctrl(0)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            reactor.pause(reactor.monotonic() + 2.000)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            chelper.run_hub_ctrl(1)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-02 23:18:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._restart_method == 'command':
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            last_clock, last_clock_time = self.serial.get_last_clock()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            eventtime = reactor.monotonic()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if (self._reset_cmd is None
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                or eventtime > last_clock_time + self.COMM_TIMEOUT):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                logging.info("Unable to issue reset command")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            # Attempt reset via command
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            logging.info("Attempting a microcontroller reset command")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.send(self._reset_cmd.encode())
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            reactor.pause(reactor.monotonic() + 0.015)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.disconnect()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Attempt reset via arduino mechanism
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 22:26:10 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        logging.info("Attempting a microcontroller reset")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.disconnect()
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-02 23:18:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        serialhdl.arduino_reset(self._serialport, reactor)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-23 16:18:08 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def is_fileoutput(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self._is_fileoutput
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # Configuration phase
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def _add_custom(self):
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for line in self._custom.split('\n'):
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            line = line.strip()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            cpos = line.find('#')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if cpos >= 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                line = line[:cpos].strip()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if not line:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                continue
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            self.add_config_cmd(line)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _build_config(self):
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Build config commands
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for oid in self._oids:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            oid.build_config()
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._add_custom()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._config_cmds.insert(0, "allocate_oids count=%d" % (
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            len(self._oids),))
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Resolve pin names
							 | 
						
					
						
							
								
									
										
										
										
											2017-01-09 23:50:13 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        mcu = self.serial.msgparser.get_constant('MCU')
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 23:05:01 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        pnames = pins.get_pin_map(mcu, self._pin_map)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 15:05:26 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        updated_cmds = []
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for cmd in self._config_cmds:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            try:
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 19:55:56 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                updated_cmds.append(pins.update_command(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    cmd, self._mcu_freq, pnames))
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 15:05:26 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            except:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-02 23:18:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                raise self._config_error("Unable to translate pin name: %s" % (
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 15:05:26 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                    cmd,))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._config_cmds = updated_cmds
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Calculate config CRC
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._config_crc = zlib.crc32('\n'.join(self._config_cmds)) & 0xffffffff
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.add_config_cmd("finalize_config crc=%d" % (self._config_crc,))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def _send_config(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        msg = self.create_command("get_config")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._is_fileoutput:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            config_params = {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                'is_config': 0, 'move_count': 500, 'crc': self._config_crc}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            config_params = self.serial.send_with_response(msg, 'config')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if not config_params['is_config']:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if self._restart_method == 'rpi_usb':
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                # Only configure mcu after usb power reset
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self._check_restart("full reset before config")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            # Send config commands
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 22:26:10 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            logging.info("Sending printer configuration...")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            for c in self._config_cmds:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self.send(self.create_command(c))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if not self._is_fileoutput:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                config_params = self.serial.send_with_response(msg, 'config')
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 21:42:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                if not config_params['is_config']:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    if self.is_shutdown:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                        raise error("Firmware error during config: %s" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                            self._shutdown_msg,))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    raise error("Unable to configure printer")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 14:29:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif self._printer.get_startup_state() == 'firmware_restart':
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Failed automated reset of micro-controller")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._config_crc != config_params['crc']:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._check_restart("CRC mismatch")
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 14:30:45 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            raise error("Printer CRC does not match config")
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-24 12:33:56 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        move_count = config_params['move_count']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        logging.info("Configured (%d moves)" % (move_count,))
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        stepqueues = tuple(s._stepqueue for s in self._steppers)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._steppersync = self._ffi_lib.steppersync_alloc(
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-24 12:33:56 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self.serial.serialqueue, stepqueues, len(stepqueues), move_count)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-22 12:37:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for cb in self._init_callbacks:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            cb()
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # Config creation helpers
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def create_oid(self, oid):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._oids.append(oid)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return len(self._oids) - 1
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def add_config_cmd(self, cmd):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._config_cmds.append(cmd)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-22 12:37:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def add_init_callback(self, callback):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._init_callbacks.append(callback)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def register_msg(self, cb, msg, oid=None):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.serial.register_callback(cb, msg, oid)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def register_stepper(self, stepper):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self._steppers.append(stepper)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def alloc_command_queue(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self.serial.alloc_command_queue()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def lookup_command(self, msgformat):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self.serial.msgparser.lookup_command(msgformat)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def create_command(self, msg):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self.serial.msgparser.create_command(msg)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # Wrappers for mcu object creation
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def create_stepper(self, step_pin, dir_pin):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return MCU_stepper(self, step_pin, dir_pin)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def create_endstop(self, pin):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return MCU_endstop(self, pin)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def create_digital_out(self, pin, max_duration=2.):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return MCU_digital_out(self, pin, max_duration)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-08 20:00:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def create_pwm(self, pin, cycle_time, hard_cycle_ticks=0, max_duration=2.):
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if hard_cycle_ticks < 0:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return MCU_digital_out(self, pin, max_duration)
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 19:55:56 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return MCU_pwm(self, pin, cycle_time, hard_cycle_ticks, max_duration)
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def create_adc(self, pin):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return MCU_adc(self, pin)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # Clock syncing
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def set_print_start_time(self, eventtime):
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-30 12:51:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        clock = self.serial.get_clock(eventtime)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-14 10:06:11 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        logging.debug("Synchronizing mcu clock at %.6f to %d" % (
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-30 12:51:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            eventtime, clock))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        est_mcu_time = clock / self._mcu_freq
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:07:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self._print_start_time = est_mcu_time
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def get_print_buffer_time(self, eventtime, print_time):
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self.is_shutdown:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:07:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        mcu_time = print_time + self._print_start_time
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        est_mcu_time = self.serial.get_clock(eventtime) / self._mcu_freq
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:07:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return mcu_time - est_mcu_time
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def print_to_mcu_time(self, print_time):
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:07:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return print_time + self._print_start_time
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def get_mcu_freq(self):
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return self._mcu_freq
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def get_last_clock(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self.serial.get_last_clock()
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def get_max_stepper_error(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self._max_stepper_error
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # Move command queuing
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def send(self, cmd, minclock=0, reqclock=0, cq=None):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.serial.send(cmd, minclock, reqclock, cq=cq)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def flush_moves(self, print_time):
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-28 22:17:49 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self._steppersync is None:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:07:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        mcu_time = print_time + self._print_start_time
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-25 12:09:37 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        clock = int(mcu_time * self._mcu_freq)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ret = self._ffi_lib.steppersync_flush(self._steppersync, clock)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if ret:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise error("Internal error in stepcompress")
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def pause(self, waketime):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self._printer.reactor.pause(waketime)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def monotonic(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return self._printer.reactor.monotonic()
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-30 01:58:45 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __del__(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.disconnect()
							 |