MustafaMulla29/rp2040-bldc-motor-controller

Hardware assembly metadata and PCB geometry for a controller board with a snap-off rotary-encoder section, plus four parallel 150 µF/50 V bulk electrolytic capacitors filtering the motor supply.

Version
2.0.40
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone MustafaMulla29/rp2040-bldc-motor-controller  # download the package files into ./rp2040-bldc-motor-controller
tsci add MustafaMulla29/rp2040-bldc-motor-controller    # add it to your project as @tsci/MustafaMulla29.rp2040-bldc-motor-controller

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

firmware/feedback.py

"""Slow, motor-disabled encoder diagnostics; not a high-speed capture driver.

GPIO20=A/H1, GPIO21=B/H2, GPIO7=Z/H3. A/B are consecutive for a future
RP2040 PIO counter. Never use Python polling as the motor's control feedback.
"""

class Quadrature:
    def __init__(self, a, b):
        self.previous = (int(bool(a)) << 1) | int(bool(b))
        self.count = 0
        self.invalid = 0

    def sample(self, a, b):
        state = (int(bool(a)) << 1) | int(bool(b))
        step = (self.previous, state)
        if step in ((0, 1), (1, 3), (3, 2), (2, 0)):
            self.count += 1
        elif step in ((1, 0), (3, 1), (2, 3), (0, 2)):
            self.count -= 1
        elif state != self.previous:
            self.invalid += 1
        self.previous = state
        return self.count


def run():
    import machine
    import time
    # No motor-driver transactions and no motor-enable path in this diagnostic.
    arm = machine.Pin(3, machine.Pin.OUT, value=0)
    speed = machine.Pin(2, machine.Pin.OUT, value=0)
    a = machine.Pin(20, machine.Pin.IN)
    b = machine.Pin(21, machine.Pin.IN)
    z = machine.Pin(7, machine.Pin.IN)
    decoder = Quadrature(a.value(), b.value())
    printed = time.ticks_ms()
    print("Turn the encoder slowly by hand; motor outputs remain disabled.")
    try:
        while True:
            decoder.sample(a.value(), b.value())
            now = time.ticks_ms()
            if time.ticks_diff(now, printed) >= 250:
                print("A=%d B=%d Z=%d count=%d invalid=%d" %
                      (a.value(), b.value(), z.value(), decoder.count, decoder.invalid))
                printed = now
            time.sleep_us(100)
    finally:
        arm.off()
        speed.off()


if __name__ == "__main__":
    run()