Last active
August 22, 2018 21:41
-
-
Save machinekoder/4491367e95c4675ec6a818eebcc5d7ee to your computer and use it in GitHub Desktop.
Controlling an RC servo with Machinekit (untested)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Turn commanded position -5 to +5 into 1-2 mS pulse for RC (hobby) servo | |
# gain = 1 mS (range) / 50 mS (PWM period) / 10 (units)= 0.002 | |
# offset = 1.5 mS (value) / 50 mS (PWM period) = 0.03 | |
# Resulting PWM values should be between 0.02 and 0.04, representing a | |
# 1 mS to 2 mS wide pulse | |
def rc_servo(thread, name, range_ms, offset_ms, period_ms, min_step, max_step, in_signal, out_signal): | |
gain = range_ms / pwm_ms / (max_step - min_step) | |
offset = value_ms / pwm_ms | |
min_limit = offset + (gain * min_step) | |
max_limit = offset + (gain * max_step) | |
scale = rt.newinst('scale', 'scale_{}'.format(name)) | |
hal.addf(scale.name, thread) | |
limit = rt.newinst('limit', 'limit_{}'.format(name)) | |
hal.addf(limit.name, thread) | |
scale.pin('gain').set(gain) | |
scale.pin('offset').set(offset) | |
scale.pin('in').link(in_signal) | |
scale.pin('out').link(limit.pin('in')) | |
limit.pin('min').set(0.02) | |
limit.pin('max').set(0.04) | |
limit.pin('out').link(out_signal) | |
z_pos_cmd = hal.Signal('z-pos-cmd', hal.HAL_FLOAT) | |
pins['emcmot.02.pos-cmd'].link(z_pos_cmd) | |
z_pwm_out = hal.Signal('z-pwm-out', hal.HAL_FLOAT) | |
pins['hpg.pwmgen.00.out.00.value'].link(z_pwm_out) | |
rc_servo( | |
thread='servo-thread', | |
name='z', | |
range_ms=1, | |
offset_ms=1.5, | |
period_ms=50, | |
min_step=-5, | |
max_step=5, | |
in_signal=z_pos_cmd, | |
out_signal=z_pwm_out, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment