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-controllerIn 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/test_knob_runtime.py
"""Motor settings below are synthetic bus fixtures, NEVER a motor tuning image."""
import copy
import unittest
from mcf8329 import CONFIG_REGISTERS
from motor_profile import validate_profile, calibrated_sample
from knob_runtime import KnobRuntime
def fixture_profile():
regs = {reg: 0 for reg in CONFIG_REGISTERS}
regs.update({0xA4: 1, 0xA6: 0x06000001})
return dict(driver="MCF8329A", motor="SIMULATION ONLY", commissioning_record="TEST FIXTURE ONLY",
allowed_voltages_mv=[9000], max_command_u16=30000,
phase_current_limit_ma=1000, registers=regs,
calibration=dict(reference_mv=3300, vbus_scale_ppm=1000000,
vmotor_scale_ppm=1000000, current_zero_u16=0,
current_uv_per_amp=400000))
def raw_voltage(mv): return round(mv * 65535 * 15 / (3300 * 235))
class Hardware:
def __init__(self):
self.motor = self
self.regs = fixture_profile()["registers"]
self.raw = dict(vbus_raw=raw_voltage(9000), vmotor_raw=raw_voltage(9000),
current_raw=0, knob_raw=0, button_down=False,
fault_n=True, pd_ok2_low=True)
self.pd = dict(rdo=(2 << 28) | (300 << 10) | 300, pe_state=0x18)
self.enabled, self.duty = False, 0
self.reads = []
self.fail_pd = self.fail_mcf = self.bad_boot = False
self.now = 0
self.after_read = lambda: None
def off(self): self.enabled, self.duty = False, 0
def drive(self, enabled, duty): self.enabled, self.duty = enabled, duty
def sample_raw(self): return dict(self.raw)
def ticks_ms(self): return self.now
def ticks_diff(self, a, b): return a-b
def verify_pd_boot(self):
if self.bad_boot: raise OSError("unprovisioned PD")
def pd_status(self, selected_mv):
if self.fail_pd: raise OSError("PD bus lost")
return dict(self.pd)
def read_register(self, reg):
self.reads.append(reg)
if self.fail_mcf: raise OSError("MCF bus lost")
self.after_read()
return self.regs[reg]
class RuntimeTests(unittest.TestCase):
def make(self):
hw = Hardware()
return hw, KnobRuntime(hw, hw, fixture_profile())
def steps(self, hw, rt, count=1, **changes):
hw.raw.update(changes)
for _ in range(count): rt.tick(20)
def start(self, hw, rt):
self.steps(hw, rt, 20, knob_raw=0, button_down=False)
self.steps(hw, rt, 5, button_down=True)
self.steps(hw, rt, 10, button_down=False, knob_raw=50000)
self.assertTrue(hw.enabled)
def test_missing_profile_previews_knob_but_never_enables(self):
hw = Hardware(); rt = KnobRuntime(hw, hw)
self.steps(hw, rt, 30, knob_raw=50000, button_down=True)
self.assertGreater(rt.knob, 0)
self.assertFalse(hw.enabled)
self.assertEqual(hw.duty, 0)
self.assertEqual(hw.reads, [])
def test_valid_profile_arms_zero_stops_and_requires_rearm_after_fault(self):
hw, rt = self.make(); self.start(hw, rt)
self.steps(hw, rt, knob_raw=0)
self.assertFalse(hw.enabled)
self.steps(hw, rt, knob_raw=50000)
self.assertTrue(hw.enabled)
self.steps(hw, rt, fault_n=False)
self.assertFalse(hw.enabled)
self.steps(hw, rt, 50, fault_n=True)
self.assertFalse(hw.enabled)
self.start(hw, rt)
def test_each_power_fault_shuts_down_on_next_sample(self):
for changes in (dict(pd_ok2_low=False), dict(fault_n=False),
dict(vmotor_raw=0), dict(vbus_raw=raw_voltage(5000)),
dict(current_raw=round(2500 * 0.4 / 3300 * 65535))): # above the 2.4 A ceiling at 9 V
hw, rt = self.make(); self.start(hw, rt)
self.steps(hw, rt, **changes)
self.assertFalse(hw.enabled, changes)
self.assertEqual(hw.duty, 0)
self.assertFalse(rt.control.armed)
def test_pd_mcf_exception_and_interrupt_fail_closed(self):
for fail in ("fail_pd", "fail_mcf"):
hw, rt = self.make(); self.start(hw, rt)
setattr(hw, fail, True)
with self.assertRaises(OSError): rt.tick(20)
self.assertFalse(hw.enabled)
hw, rt = self.make(); self.start(hw, rt)
def interrupted(): raise KeyboardInterrupt()
hw.sample_raw = interrupted
with self.assertRaises(KeyboardInterrupt): rt.tick(20)
self.assertFalse(hw.enabled)
def test_profile_mismatch_blocks_arm_and_rechecks_after_power_loss(self):
hw, rt = self.make()
hw.regs[0xAC] = 0x1234
with self.assertRaises(OSError): rt.tick(20)
self.assertFalse(hw.enabled)
hw.regs[0xAC] = 0
self.start(hw, rt)
self.steps(hw, rt, vmotor_raw=0)
hw.reads.clear()
self.steps(hw, rt, vmotor_raw=raw_voltage(9000), knob_raw=0)
self.assertEqual(hw.reads, list(CONFIG_REGISTERS))
self.assertFalse(hw.enabled)
def test_power_loss_during_profile_read_is_resampled_before_arming(self):
hw, rt = self.make()
hw.after_read = lambda: hw.raw.update(pd_ok2_low=False)
self.steps(hw, rt, 30, button_down=True)
self.assertFalse(hw.enabled)
self.assertFalse(rt.verified)
def test_slow_profile_read_trips_timing_and_leaves_pwm_zero(self):
hw, rt = self.make()
def delay(): hw.now += 10
hw.after_read = delay
with self.assertRaises(ValueError): rt.tick(20)
self.assertFalse(hw.enabled)
def test_voltage_selection_disarms_before_selector_and_checks_motor_range(self):
hw, rt = self.make(); self.start(hw, rt)
class Selector:
def select(self, mv):
self_case.assertFalse(hw.enabled)
self_case.assertFalse(rt.control.armed)
return mv
self_case = self
rt.select_voltage(20000, Selector())
self.steps(hw, rt, 30, knob_raw=0, button_down=True)
self.assertFalse(hw.enabled)
self.assertIn("not qualified", rt.control.reason)
def test_invalid_profile_and_unprovisioned_pd_fail_before_drive(self):
mutations = [lambda p: p["registers"].pop(0x80),
lambda p: p.update(driver="MCT8329A"),
lambda p: p.pop("driver"),
lambda p: p["registers"].update({0xA4: 0}),
lambda p: p["registers"].update({0xA4: 2}),
lambda p: p["registers"].update({0xA4: 3}),
lambda p: p["registers"].update({0xA4: 5}), # forced brake
lambda p: p["registers"].update({0xA4: 0x21}),
lambda p: p["registers"].update({0xA8: 0x800}), # sleep
lambda p: p["registers"].update({0xA8: 0x200}), # clock
lambda p: p["registers"].update({0xA8: 0x100}),
lambda p: p["registers"].update({0xA8: 0x10}), # watchdog
lambda p: p["registers"].update({0xAA: 0x200}), # PWM range
lambda p: p["registers"].update({0xAA: 0x10}), # power mode
lambda p: p["registers"].update({0xAA: 0x20}), # current mode
lambda p: p["registers"].update({0xAA: 0x30}), # modulation
lambda p: p["registers"].update({0x94: 0x60000000}),
lambda p: p["registers"].update({0xA6: 0x01000001}),
lambda p: p["registers"].update({0xA6: 0x06000003}),
lambda p: p.update(calibration={}),
lambda p: p.update(allowed_voltages_mv=[])]
for mutate in mutations:
hw = Hardware(); p = fixture_profile(); mutate(p)
with self.assertRaises(ValueError): KnobRuntime(hw, hw, p)
self.assertFalse(hw.enabled)
hw = Hardware(); hw.bad_boot = True
with self.assertRaises(OSError): KnobRuntime(hw, hw, fixture_profile())
self.assertFalse(hw.enabled)
def test_calibration_units_offsets_and_invalid_adc(self):
hw = Hardware(); cal = fixture_profile()["calibration"]
hw.raw["current_raw"] = round(0.5 * 400000 / 3300000 * 65535)
s = calibrated_sample(hw.raw, cal)
self.assertLessEqual(abs(s["input_ma"] - 500), 1)
self.assertLessEqual(abs(s["vbus_mv"] - 9000), 1)
cal["current_zero_u16"] = hw.raw["current_raw"]
self.assertEqual(calibrated_sample(hw.raw, cal)["input_ma"], 0)
hw.raw["current_raw"] = 0
self.assertLess(calibrated_sample(hw.raw, cal)["input_ma"], -20)
hw.raw["vbus_raw"] = 65536
with self.assertRaises(ValueError): calibrated_sample(hw.raw, cal)
def test_mcf_bus_range_must_cover_every_qualified_voltage(self):
profile = fixture_profile()
profile["allowed_voltages_mv"] = [9000, 20000]
profile["registers"][0xA6] = 0x06000000 # 15 V range
with self.assertRaisesRegex(ValueError, "bus-voltage range"):
validate_profile(profile)
for bus_range in (1, 2): # 30/60 V ranges cover 20 V
profile["registers"][0xA6] = 0x06000000 | bus_range
self.assertIs(validate_profile(profile), profile)
def test_legacy_mct_register_fields_cannot_be_relabelled_as_mcf(self):
profile = fixture_profile()
profile["registers"] = {reg:0 for reg in CONFIG_REGISTERS}
profile["registers"].update({0xA6:2, 0xA8:0x06000000, 0xAA:0x2000})
with self.assertRaisesRegex(ValueError, "SPEED input"):
validate_profile(profile)
if __name__ == "__main__": unittest.main()