2018-01-20 06:09:43 +01:00
|
|
|
# Support for servos
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017,2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
|
|
|
SERVO_SIGNAL_PERIOD = 0.020
|
|
|
|
PIN_MIN_TIME = 0.100
|
|
|
|
|
|
|
|
class PrinterServo:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
2018-04-04 18:07:41 +02:00
|
|
|
ppins = self.printer.lookup_object('pins')
|
|
|
|
self.mcu_servo = ppins.setup_pin('pwm', config.get('pin'))
|
2018-01-20 06:09:43 +01:00
|
|
|
self.mcu_servo.setup_max_duration(0.)
|
|
|
|
self.mcu_servo.setup_cycle_time(SERVO_SIGNAL_PERIOD)
|
|
|
|
self.min_width = config.getfloat(
|
|
|
|
'minimum_pulse_width', .001, above=0., below=SERVO_SIGNAL_PERIOD)
|
|
|
|
self.max_width = config.getfloat(
|
|
|
|
'maximum_pulse_width', .002
|
|
|
|
, above=self.min_width, below=SERVO_SIGNAL_PERIOD)
|
|
|
|
self.max_angle = config.getfloat('maximum_servo_angle', 180.)
|
|
|
|
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.
|
2018-11-14 15:38:09 +01:00
|
|
|
self.enable = config.getboolean('enable', True)
|
|
|
|
self.last_enable = not self.enable
|
2018-05-20 18:40:10 +02:00
|
|
|
servo_name = config.get_name().split()[1]
|
2018-01-20 06:09:43 +01:00
|
|
|
self.gcode = self.printer.lookup_object('gcode')
|
2018-05-20 18:40:10 +02:00
|
|
|
self.gcode.register_mux_command("SET_SERVO", "SERVO", servo_name,
|
|
|
|
self.cmd_SET_SERVO,
|
|
|
|
desc=self.cmd_SET_SERVO_help)
|
2018-10-09 21:07:47 +02:00
|
|
|
# Check to see if an initial angle or pulse width is
|
|
|
|
# configured and set it as required
|
2018-10-05 20:35:38 +02:00
|
|
|
self.initial_pwm_value = None
|
2018-10-09 21:07:47 +02:00
|
|
|
initial_angle = config.getfloat('initial_angle', None,
|
|
|
|
minval=0., maxval=360.)
|
|
|
|
if initial_angle is not None:
|
2018-10-05 20:35:38 +02:00
|
|
|
self.initial_pwm_value = self._get_pwm_from_angle(initial_angle)
|
|
|
|
else:
|
2018-10-09 21:07:47 +02:00
|
|
|
initial_pulse_width = config.getfloat('initial_pulse_width', None,
|
|
|
|
minval=self.min_width,
|
|
|
|
maxval=self.max_width)
|
|
|
|
if initial_pulse_width is not None:
|
|
|
|
self.initial_pwm_value = self._get_pwm_from_pulse_width(
|
|
|
|
initial_pulse_width)
|
2019-01-08 16:55:18 +01:00
|
|
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
|
|
|
def handle_ready(self):
|
|
|
|
if self.initial_pwm_value is not None:
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
print_time = toolhead.get_last_move_time()
|
|
|
|
self._set_pwm(print_time, self.initial_pwm_value)
|
2018-10-05 20:35:38 +02:00
|
|
|
def _set_pwm(self, print_time, value):
|
2018-11-14 15:38:09 +01:00
|
|
|
if value == self.last_value and self.enable == self.last_enable:
|
2018-01-20 06:09:43 +01:00
|
|
|
return
|
|
|
|
print_time = max(print_time, self.last_value_time + PIN_MIN_TIME)
|
2018-11-14 15:38:09 +01:00
|
|
|
if self.enable:
|
|
|
|
self.mcu_servo.set_pwm(print_time, value)
|
|
|
|
else:
|
|
|
|
self.mcu_servo.set_pwm(print_time, 0)
|
2018-01-20 06:09:43 +01:00
|
|
|
self.last_value = value
|
2018-11-14 15:38:09 +01:00
|
|
|
self.last_enable = self.enable
|
2018-01-20 06:09:43 +01:00
|
|
|
self.last_value_time = print_time
|
2018-10-05 20:35:38 +02:00
|
|
|
def _get_pwm_from_angle(self, angle):
|
2018-01-20 06:09:43 +01:00
|
|
|
angle = max(0., min(self.max_angle, angle))
|
|
|
|
width = self.min_width + angle * self.angle_to_width
|
2018-10-05 20:35:38 +02:00
|
|
|
return width * self.width_to_value
|
|
|
|
def _get_pwm_from_pulse_width(self, width):
|
2018-01-20 06:09:43 +01:00
|
|
|
width = max(self.min_width, min(self.max_width, width))
|
2018-10-05 20:35:38 +02:00
|
|
|
return width * self.width_to_value
|
2018-01-20 06:09:43 +01:00
|
|
|
cmd_SET_SERVO_help = "Set servo angle"
|
|
|
|
def cmd_SET_SERVO(self, params):
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
2018-11-14 15:38:09 +01:00
|
|
|
if 'ENABLE' in params:
|
|
|
|
value = self.gcode.get_int('ENABLE', params)
|
|
|
|
self.enable = value != 0
|
2018-01-20 06:09:43 +01:00
|
|
|
if 'WIDTH' in params:
|
2018-10-05 20:35:38 +02:00
|
|
|
self._set_pwm(print_time, self._get_pwm_from_pulse_width(
|
|
|
|
self.gcode.get_float('WIDTH', params)))
|
2018-01-20 06:09:43 +01:00
|
|
|
else:
|
2018-11-14 15:38:09 +01:00
|
|
|
if 'ANGLE' in params:
|
|
|
|
self._set_pwm(print_time, self._get_pwm_from_angle(
|
|
|
|
self.gcode.get_float('ANGLE', params)))
|
|
|
|
else:
|
|
|
|
self._set_pwm(print_time, self.last_value)
|
2018-10-09 21:07:47 +02:00
|
|
|
|
2018-02-03 18:53:11 +01:00
|
|
|
def load_config_prefix(config):
|
2018-01-20 06:09:43 +01:00
|
|
|
return PrinterServo(config)
|