gcode: Move SET_SERVO command from gcode.py to chipmisc.py
Now that commands can be registered dynamically, move the code for SET_SERVO from gcode.py to the PrinterServo() class in chipmisc.py. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
b5a41d0dd1
commit
bc9cbc8133
|
@ -41,6 +41,7 @@ SERVO_SIGNAL_PERIOD = 0.020
|
|||
|
||||
class PrinterServo:
|
||||
def __init__(self, printer, config):
|
||||
self.printer = printer
|
||||
self.mcu_servo = pins.setup_pin(printer, 'pwm', config.get('pin'))
|
||||
self.mcu_servo.setup_max_duration(0.)
|
||||
self.mcu_servo.setup_cycle_time(SERVO_SIGNAL_PERIOD)
|
||||
|
@ -53,6 +54,9 @@ class PrinterServo:
|
|||
self.angle_to_width = (self.max_width - self.min_width) / self.max_angle
|
||||
self.width_to_value = 1. / SERVO_SIGNAL_PERIOD
|
||||
self.last_value = self.last_value_time = 0.
|
||||
self.gcode = printer.objects['gcode']
|
||||
self.gcode.register_command("SET_SERVO", self.cmd_SET_SERVO,
|
||||
desc=self.cmd_SET_SERVO_help)
|
||||
def set_pwm(self, print_time, value):
|
||||
if value == self.last_value:
|
||||
return
|
||||
|
@ -60,7 +64,6 @@ class PrinterServo:
|
|||
self.mcu_servo.set_pwm(print_time, value)
|
||||
self.last_value = value
|
||||
self.last_value_time = print_time
|
||||
# External commands
|
||||
def set_angle(self, print_time, angle):
|
||||
angle = max(0., min(self.max_angle, angle))
|
||||
width = self.min_width + angle * self.angle_to_width
|
||||
|
@ -68,9 +71,20 @@ class PrinterServo:
|
|||
def set_pulse_width(self, print_time, width):
|
||||
width = max(self.min_width, min(self.max_width, width))
|
||||
self.set_pwm(print_time, width * self.width_to_value)
|
||||
|
||||
def get_printer_servo(printer, name):
|
||||
return printer.objects.get('servo ' + name)
|
||||
cmd_SET_SERVO_help = "Set servo angle"
|
||||
def cmd_SET_SERVO(self, params):
|
||||
servo_name = self.gcode.get_str('SERVO', params)
|
||||
servo = self.printer.objects.get('servo ' + servo_name)
|
||||
if servo is not self:
|
||||
if servo is None:
|
||||
raise self.gcode.error("Servo not configured")
|
||||
return servo.cmd_SET_SERVO(params)
|
||||
print_time = self.printer.objects['toolhead'].get_last_move_time()
|
||||
if 'WIDTH' in params:
|
||||
self.set_pulse_width(print_time,
|
||||
self.gcode.get_float('WIDTH', params))
|
||||
else:
|
||||
self.set_angle(print_time, self.gcode.get_float('ANGLE', params))
|
||||
|
||||
|
||||
######################################################################
|
||||
|
|
|
@ -4,13 +4,14 @@
|
|||
#
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
import os, re, logging, collections
|
||||
import homing, extruder, chipmisc
|
||||
import homing, extruder
|
||||
|
||||
class error(Exception):
|
||||
pass
|
||||
|
||||
# Parse and handle G-Code commands
|
||||
class GCodeParser:
|
||||
error = error
|
||||
RETRY_TIME = 0.100
|
||||
def __init__(self, printer, fd):
|
||||
self.printer = printer
|
||||
|
@ -48,6 +49,9 @@ class GCodeParser:
|
|||
self.homing_add = [0.0, 0.0, 0.0, 0.0]
|
||||
self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3}
|
||||
def register_command(self, cmd, func, when_not_ready=False, desc=None):
|
||||
if not (len(cmd) >= 2 and not cmd[0].isupper() and cmd[1].isdigit()):
|
||||
origfunc = func
|
||||
func = lambda params: origfunc(self.get_extended_params(params))
|
||||
self.ready_gcode_handlers[cmd] = func
|
||||
if when_not_ready:
|
||||
self.base_gcode_handlers[cmd] = func
|
||||
|
@ -305,7 +309,7 @@ class GCodeParser:
|
|||
'G1', 'G4', 'G20', 'G28', 'G90', 'G91', 'G92',
|
||||
'M82', 'M83', 'M18', 'M105', 'M104', 'M109', 'M112', 'M114', 'M115',
|
||||
'M140', 'M190', 'M106', 'M107', 'M206', 'M400',
|
||||
'IGNORE', 'QUERY_ENDSTOPS', 'PID_TUNE', 'SET_SERVO',
|
||||
'IGNORE', 'QUERY_ENDSTOPS', 'PID_TUNE',
|
||||
'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP']
|
||||
cmd_G1_aliases = ['G0']
|
||||
def cmd_G1(self, params):
|
||||
|
@ -467,18 +471,6 @@ class GCodeParser:
|
|||
temp = self.get_float('S', params)
|
||||
heater.start_auto_tune(temp)
|
||||
self.bg_temp(heater)
|
||||
cmd_SET_SERVO_help = "Set servo angle"
|
||||
def cmd_SET_SERVO(self, params):
|
||||
params = self.get_extended_params(params)
|
||||
name = self.get_str('SERVO', params)
|
||||
s = chipmisc.get_printer_servo(self.printer, name)
|
||||
if s is None:
|
||||
raise error("Servo not configured")
|
||||
print_time = self.toolhead.get_last_move_time()
|
||||
if 'WIDTH' in params:
|
||||
s.set_pulse_width(print_time, self.get_float('WIDTH', params))
|
||||
return
|
||||
s.set_angle(print_time, self.get_float('ANGLE', params))
|
||||
def prep_restart(self):
|
||||
if self.is_printer_ready:
|
||||
self.respond_info("Preparing to restart...")
|
||||
|
|
|
@ -130,8 +130,8 @@ class Printer:
|
|||
if bglogger is not None:
|
||||
bglogger.set_rollover_info("config", None)
|
||||
self.reactor = reactor.Reactor()
|
||||
self.objects = {}
|
||||
self.gcode = gcode.GCodeParser(self, input_fd)
|
||||
self.objects = {'gcode': self.gcode}
|
||||
self.stats_timer = self.reactor.register_timer(self._stats)
|
||||
self.connect_timer = self.reactor.register_timer(
|
||||
self._connect, self.reactor.NOW)
|
||||
|
|
Loading…
Reference in New Issue