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/power_manager.py
"""Configurable-PD bring-up console. Intentionally keeps motor drive OFF.
Copy firmware/*.py to RP2040 MicroPython and run this file over USB DATA.
Commands: voltage 5 | voltage 9 | voltage 15 | voltage 20. SW4 must be OFF.
This verifies supply supervision; it is NOT qualified commutation firmware.
"""
from machine import Pin, ADC, I2C, WDT
import time, sys, select
from power_policy import valid_motor_contract, adc_millivolts
from pd_profile import verify_runtime, nvm_is_9v
from pd_nvm import NVM
from select_pd_voltage import VoltageSelector
from status_led import StatusLed
speed = Pin(2, Pin.OUT, value=0)
arm = Pin(3, Pin.OUT, value=0)
pd_ok2 = Pin(6, Pin.IN)
led = StatusLed(Pin)
bus = I2C(1, sda=Pin(22), scl=Pin(23), freq=100_000)
wdt = WDT(timeout=2000)
vbus_adc, motor_adc = ADC(26), ADC(27)
def disarm():
arm.off()
speed.off()
def sample():
return dict(vbus_mv=adc_millivolts(vbus_adc.read_u16()),
vmotor_mv=adc_millivolts(motor_adc.read_u16()),
pd_ok2_low=not pd_ok2.value())
selector = VoltageSelector(bus, disarm, sample, time, service=wdt.feed)
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
line = ''
print("PD bring-up: motor held OFF. Set SW4 OFF; type voltage 5/9/15/20")
try:
led.show("waiting")
if not nvm_is_9v(NVM(bus).read()):
raise OSError("Provision and cold-boot verify 9 V default NVM first")
while True:
try:
while poll.poll(0):
ch = sys.stdin.read(1)
if ch in ('\r', '\n'):
if line:
words = line.split()
line = ''
if len(words) != 2 or words[0] != 'voltage' or words[1] not in ('5','9','15','20'):
raise ValueError("Use voltage 5, voltage 9, voltage 15 or voltage 20")
led.show("negotiating")
selector.select(int(words[1])*1000)
print("Selected %d V; motor remains OFF" % (selector.selected_mv//1000))
elif len(line) < 64: line += ch
else:
line = ''
raise ValueError("Command too long")
s = sample()
verify_runtime(bus, selected_mv=selector.selected_mv)
rdo = int.from_bytes(bus.readfrom_mem(0x28, 0x91, 4), 'little')
pe = bus.readfrom_mem(0x28, 0x29, 1)[0]
valid = valid_motor_contract(s['vbus_mv'],rdo,pe,s['pd_ok2_low'],selector.selected_mv)
led.show("pd_ready" if valid else "waiting")
except (OSError, ValueError) as error:
led.show("fault")
print(error)
disarm()
wdt.feed()
time.sleep_ms(100)
except (OSError, ValueError):
led.show("fault")
raise
except KeyboardInterrupt:
led.off()
raise
finally:
disarm()