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

"""Validate commissioning data; a matching image is not proof of motor tuning."""
from mcf8329 import CONFIG_REGISTERS
from power_policy import VOLTAGES_MV, adc_millivolts


def validate_profile(profile):
    if not isinstance(profile, dict):
        raise ValueError("Commissioned motor profile is missing")
    if profile.get("driver") != "MCF8329A":
        raise ValueError("An explicitly commissioned MCF8329A profile is required")
    for key in ("motor", "commissioning_record"):
        if not isinstance(profile.get(key), str) or not profile[key].strip():
            raise ValueError("Missing motor/commissioning record")
    voltages = profile.get("allowed_voltages_mv", ())
    if not voltages or any(type(v) is not int or v not in VOLTAGES_MV for v in voltages):
        raise ValueError("List the motor's qualified voltage selections")
    for key, lo, hi in (("max_command_u16", 1, 65535),
                       ("phase_current_limit_ma", 1, 100000)):
        if type(profile.get(key)) is not int or not lo <= profile[key] <= hi:
            raise ValueError("Missing/invalid " + key)
    regs = profile.get("registers", {})
    if set(regs) != set(CONFIG_REGISTERS):
        raise ValueError("All 24 MCF configuration words are required")
    if any(type(v) is not int or not 0 <= v <= 0xFFFFFFFF for v in regs.values()):
        raise ValueError("MCF register values must be uint32")
    # TI SLLSFQ7 tables 7-24, 7-36..39. Do not reuse MCT field positions.
    # No tuning/EEPROM writes: reject incompatible commissioned configurations.
    if (regs[0xA4] & 3) != 1:
        raise ValueError("MCF SPEED input must use PWM duty mode")
    if ((regs[0xA4] >> 2) & 3) == 1 or regs[0xA4] & (1 << 5):
        raise ValueError("MCF brake must use the low hardware pin or be disabled")
    if regs[0xA8] & 0xF00:
        raise ValueError("Require standby and internal clock")
    if regs[0xA8] & (1 << 4):
        raise ValueError("External driver watchdog servicing is not implemented")
    if regs[0xAA] & (1 << 9):
        raise ValueError("20 kHz command requires the 325 Hz-100 kHz PWM input range")
    if regs[0xAA] & (3 << 4):
        raise ValueError("The speed knob requires MCF closed-loop speed control")
    if regs[0x94] & 0x60000000:
        raise ValueError("Require direct reference mapping, not a custom profiler")
    if ((regs[0xA6] >> 20) & 0x7F) != 0x60:
        raise ValueError("This board application uses MCF address 0x60")
    bus_range = regs[0xA6] & 3
    if bus_range == 3 or max(voltages) > (15000, 30000, 60000)[bus_range]:
        raise ValueError("MCF bus-voltage range does not cover the qualified voltages")
    calibration = profile.get("calibration", {})
    for key, lo, hi in (("reference_mv", 3000, 3600),
                       ("vbus_scale_ppm", 900000, 1100000),
                       ("vmotor_scale_ppm", 900000, 1100000),
                       ("current_zero_u16", 0, 2000),
                       ("current_uv_per_amp", 320000, 480000)):
        if type(calibration.get(key)) is not int or not lo <= calibration[key] <= hi:
            raise ValueError("Missing/invalid measured calibration: " + key)
    return profile


def calibrated_sample(raw, calibration):
    for key in ("vbus_raw", "vmotor_raw", "current_raw", "knob_raw"):
        if type(raw[key]) is not int or not 0 <= raw[key] <= 65535:
            raise ValueError("Invalid ADC sample: " + key)
    result = dict(raw)
    reference = calibration["reference_mv"]
    for name in ("vbus", "vmotor"):
        result[name + "_mv"] = (adc_millivolts(raw[name + "_raw"], reference)
                                * calibration[name + "_scale_ppm"] // 1000000)
    # R13=20 mOhm and INA180A1 gain=20 => nominal 400000 uV/A.
    # Preserve a significant negative/out-of-range reading as a fault.
    current = ((raw["current_raw"] - calibration["current_zero_u16"])
               * reference * 1000000 // (65535 * calibration["current_uv_per_amp"]))
    result["input_ma"] = 0 if -20 <= current < 0 else current
    return result