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/knob_runtime.py

"""Integrate the knob with PD, calibrated ADCs and a verified MCF8329A profile."""
from knob_control import KnobControl, knob_command
from motor_profile import validate_profile, calibrated_sample
from mcf8329 import ProfileCheck
from power_policy import DEFAULT_MV, motor_permitted, stop_reason


class KnobRuntime:
    def __init__(self, hardware, clock, profile=None):
        self.hw, self.clock = hardware, clock
        self.hw.off()
        self.profile = validate_profile(profile) if profile is not None else None
        self.control = KnobControl(self.profile["max_command_u16"] if self.profile else 65535)
        self.checker = ProfileCheck(hardware.motor, self.profile["registers"]) if self.profile else None
        self.verified = False
        self.selected_mv = DEFAULT_MV
        self.knob = 0
        if self.profile:
            self.hw.verify_pd_boot()

    def stop(self, reason="stopped", fault=False):
        # Hardware shutdown comes first, even if later bookkeeping fails.
        self.hw.off()
        self.control.stop(reason, fault)

    def select_voltage(self, millivolts, selector):
        self.stop("voltage change")
        self.verified = False
        self.hw.verify_pd_boot()
        self.selected_mv = selector.select(millivolts)
        return self.selected_mv

    def _sample(self):
        raw = self.hw.sample_raw()
        self.knob = knob_command(raw["knob_raw"])
        s = calibrated_sample(raw, self.profile["calibration"])
        s.update(self.hw.pd_status(self.selected_mv))
        good = motor_permitted(s["vbus_mv"], s["vmotor_mv"], s["input_ma"],
                               s["rdo"], s["pe_state"], s["pd_ok2_low"],
                               True, s["fault_n"], self.selected_mv)
        s["stop_reason"] = None if good else stop_reason(
            s["vbus_mv"], s["vmotor_mv"], s["input_ma"], s["rdo"], s["pe_state"],
            s["pd_ok2_low"], s["fault_n"], self.selected_mv)
        return s, good

    def tick(self, dt_ms):
        start = self.clock.ticks_ms()
        try:
            if self.profile is None:
                self.knob = knob_command(self.hw.sample_raw()["knob_raw"])
                self.stop("commissioned motor profile and calibration required")
                return
            if self.selected_mv not in self.profile["allowed_voltages_mv"]:
                self.stop("voltage not qualified for this motor")
                self.verified = False
                return
            s, healthy = self._sample()
            if healthy:
                if not self.verified:
                    self.hw.off()
                    self.checker.verify_all()
                    self.verified = True
                else:
                    self.checker.verify_next()
                # Do not arm using a snapshot taken before profile bus traffic.
                s, healthy = self._sample()
            if not healthy:
                self.verified = False
            elapsed = self.clock.ticks_diff(self.clock.ticks_ms(), start)
            self.control.update(s["knob_raw"], s["button_down"], healthy,
                                dt_ms + elapsed, s.get("stop_reason") or "supply, current or driver fault")
            self.hw.drive(self.control.armed and self.control.duty > 0,
                          self.control.duty)
        except BaseException:
            self.verified = False
            self.stop("supervision or profile verification failed", fault=True)
            raise

    def status(self):
        return "knob=%d%% command=%d%% armed=%s PD-request=%dV: %s" % (
            self.knob * 100 // 65535, self.control.duty * 100 // 65535,
            self.control.armed, self.selected_mv // 1000, self.control.reason)