I did some code up to read the encoders.
from pimoroni_i2c import PimoroniI2C
from breakout_encoder import BreakoutEncoder
from pimoroni import RGBLED
led = RGBLED(18, 19, 20)
led.set_rgb(50, 0, 0)
steps_per_rev = 90
i2c = PimoroniI2C(sda=(4), scl=(5))
enc_L = BreakoutEncoder(i2c,0xe)
enc_R = BreakoutEncoder(i2c,0xf)
enc_L.set_brightness(1.0)
enc_R.set_brightness(1.0)
# enc.set_direction(BreakoutEncoder.DIRECTION_CCW) # Uncomment this to flip the direction
# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v): # noqa: RET503
if s == 0.0:
return v, v, v
i = int(h * 6.0)
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
count_L = 0
count_R = 0
def count_L_changed(count):
print("Count L: ",count_L, " Count R: ",count_R)
h = ((count_L % steps_per_rev) * 360.0) / steps_per_rev # Convert the count to a colour hue
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
enc_L.set_led(r, g, b)
count_L = 0
count_L_changed(count_L)
enc_L.clear_interrupt_flag()
def count_R_changed(count):
print("Count L: ",count_L, " Count R: ",count_R)
h = ((count_R % steps_per_rev) * 360.0) / steps_per_rev # Convert the count to a colour hue
r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
enc_R.set_led(r, g, b)
count_R = 0
count_R_changed(count_R)
enc_R.clear_interrupt_flag()
while True:
if enc_L.get_interrupt_flag():
count_L = enc_L.read()
enc_L.clear_interrupt_flag()
'''
while count_L < 0:
count_L += steps_per_rev
'''
count_L_changed(count_L)
if enc_R.get_interrupt_flag():
count_R = enc_R.read()
enc_R.clear_interrupt_flag()
'''
while count_R < 0:
count_R += steps_per_rev
'''
count_R_changed(count_L)
That gets me this
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
Count L: 0 Count R: 0
Count L: 0 Count R: 0
Count L: 1 Count R: 0
Count L: 2 Count R: 0
Count L: 3 Count R: 0
Count L: 2 Count R: 0
Count L: 1 Count R: 0
Count L: 1 Count R: 1
Count L: 1 Count R: 2
Count L: 1 Count R: 3
Count L: 1 Count R: 2
Count L: 1 Count R: 1
Count L: 1 Count R: 0
Count L: 1 Count R: -1
Count L: 1 Count R: -2
Count L: 0 Count R: -2
Count L: -1 Count R: -2
0 is the starting centered position, - one way, + the other. I’ll likely limit them to +90 and -90. 180 degree servos.
I also have code to test run the servos.
import time
import math
from pimoroni_i2c import PimoroniI2C
from breakout_ioexpander import BreakoutIOExpander
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
ioe_servo_pin = 1 #pan
#ioe_servo_pin = 2 #tilt
# Settings to produce a 50Hz output from the 24MHz clock.
# 24,000,000 Hz / 8 = 3,000,000 Hz
# 3,000,000 Hz / 60,000 Period = 50 Hz
divider = 8
period = 60000
cycle_time = 5.0
servo_range = 2000.0 # Between 1000 and 2000us (1-2ms)
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
ioe = BreakoutIOExpander(i2c, address=0x18)
ioe.set_pwm_period(period)
ioe.set_pwm_control(divider)
ioe.set_mode(ioe_servo_pin, BreakoutIOExpander.PIN_PWM)
t = 0
while True:
s = math.sin((t * math.pi * 2.0) / cycle_time) / 2.0
servo_us = 1500.0 + (s * servo_range)
duty_per_microsecond = period / (20 * 1000) # Default is 3 LSB per microsecond
duty_cycle = round(servo_us * duty_per_microsecond)
print("Cycle Time: ", t % cycle_time, ", Pulse: ", servo_us, ", Duty Cycle: ", duty_cycle, sep="")
ioe.output(ioe_servo_pin, duty_cycle)
time.sleep(0.02)
t += 0.02
I just need to decifer what the servo code is actually doing / how it does it?
The servo cycles back and forth from full left to full right when I run it.