servo: Set initial value via mcu_servo.setup_start_value()

Using the setup_start_value() method avoids the PWM output line
transitioning to an intermediate state prior to setting the initial
value.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-07-16 14:17:04 -04:00
parent 428a8d4881
commit db05938a5f
2 changed files with 28 additions and 39 deletions

View File

@ -1245,15 +1245,13 @@
# The maximum pulse width time (in seconds). This should correspond # The maximum pulse width time (in seconds). This should correspond
# with an angle of maximum_servo_angle. The default is 0.002 # with an angle of maximum_servo_angle. The default is 0.002
# seconds. # seconds.
#initial_angle: 70 #initial_angle:
# Initial angle to set the servo to when the mcu resets. Must be between # Initial angle (in degrees) to set the servo to. The default is to
# 0 and maximum_servo_angle This parameter is optional. If both # not send any signal at startup.
# initial_angle and initial_pulse_width are set initial_angle will be used. #initial_pulse_width:
#initial_pulse_width: 0.0015 # Initial pulse width time (in seconds) to set the servo to. (This
# Initial pulse width time (in seconds) to set the servo to when # is only valid if initial_angle is not set.) The default is to not
# the mcu resets. Must be between minimum_pulse_width and maximum_pulse_width. # send any signal at startup.
# This parameter is optional. If both initial_angle and initial_pulse_width
# are set initial_angle will be used
# Neopixel (aka WS2812) LED support (one may define any number of # Neopixel (aka WS2812) LED support (one may define any number of
# sections with a "neopixel" prefix). One may set the LED color via # sections with a "neopixel" prefix). One may set the LED color via

View File

@ -1,6 +1,6 @@
# Support for servos # Support for servos
# #
# Copyright (C) 2017,2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2017-2020 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
@ -10,44 +10,35 @@ PIN_MIN_TIME = 0.100
class PrinterServo: class PrinterServo:
def __init__(self, config): def __init__(self, config):
self.printer = config.get_printer() self.printer = config.get_printer()
ppins = self.printer.lookup_object('pins') self.min_width = config.getfloat('minimum_pulse_width', .001,
self.mcu_servo = ppins.setup_pin('pwm', config.get('pin')) above=0., below=SERVO_SIGNAL_PERIOD)
self.mcu_servo.setup_max_duration(0.) self.max_width = config.getfloat('maximum_pulse_width', .002,
self.mcu_servo.setup_cycle_time(SERVO_SIGNAL_PERIOD) above=self.min_width,
self.min_width = config.getfloat( below=SERVO_SIGNAL_PERIOD)
'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.max_angle = config.getfloat('maximum_servo_angle', 180.)
self.angle_to_width = (self.max_width - self.min_width) / self.max_angle self.angle_to_width = (self.max_width - self.min_width) / self.max_angle
self.width_to_value = 1. / SERVO_SIGNAL_PERIOD self.width_to_value = 1. / SERVO_SIGNAL_PERIOD
self.last_value = self.last_value_time = 0. self.last_value = self.last_value_time = 0.
initial_pwm = 0.
iangle = config.getfloat('initial_angle', None, minval=0., maxval=360.)
if iangle is not None:
initial_pwm = self._get_pwm_from_angle(iangle)
else:
iwidth = config.getfloat('initial_pulse_width', 0.,
minval=0., maxval=self.max_width)
initial_pwm = self._get_pwm_from_pulse_width(iwidth)
# Setup mcu_servo pin
ppins = self.printer.lookup_object('pins')
self.mcu_servo = ppins.setup_pin('pwm', config.get('pin'))
self.mcu_servo.setup_max_duration(0.)
self.mcu_servo.setup_cycle_time(SERVO_SIGNAL_PERIOD)
self.mcu_servo.setup_start_value(initial_pwm, 0.)
# Register commands
servo_name = config.get_name().split()[1] servo_name = config.get_name().split()[1]
gcode = self.printer.lookup_object('gcode') gcode = self.printer.lookup_object('gcode')
gcode.register_mux_command("SET_SERVO", "SERVO", servo_name, gcode.register_mux_command("SET_SERVO", "SERVO", servo_name,
self.cmd_SET_SERVO, self.cmd_SET_SERVO,
desc=self.cmd_SET_SERVO_help) desc=self.cmd_SET_SERVO_help)
# Check to see if an initial angle or pulse width is
# configured and set it as required
self.initial_pwm_value = None
initial_angle = config.getfloat('initial_angle', None,
minval=0., maxval=360.)
if initial_angle is not None:
self.initial_pwm_value = self._get_pwm_from_angle(initial_angle)
else:
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)
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)
def get_status(self, eventtime): def get_status(self, eventtime):
return {'value': self.last_value} return {'value': self.last_value}
def _set_pwm(self, print_time, value): def _set_pwm(self, print_time, value):