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/knob_control.py
"""Hardware-independent knob/ARM state machine; commands are not measured RPM.
Call every 20 ms with fresh supervision. A late sample, bad supply or fault
disarms. Zero requests gate shutdown immediately; this is not active braking.
"""
FULL_SCALE = 65535
ZERO_RAW = 1966 # 3% counterclockwise deadband; R85 pulls an open wiper low.
BUTTON_MS = 50
READY_MS = 250
MAX_SAMPLE_MS = 100
def knob_command(raw):
if type(raw) is not int or not 0 <= raw <= FULL_SCALE:
raise ValueError("Invalid knob ADC sample")
return max(0, raw - ZERO_RAW) * FULL_SCALE // (FULL_SCALE - ZERO_RAW)
class KnobControl:
def __init__(self, max_command=FULL_SCALE, ramp_per_second=32768):
if type(max_command) is not int or not 0 < max_command <= FULL_SCALE:
raise ValueError("Invalid commissioned command ceiling")
if type(ramp_per_second) is not int or not 0 < ramp_per_second <= FULL_SCALE:
raise ValueError("Invalid command ramp")
self.maximum = max_command
self.ramp = ramp_per_second
self.filtered = 0
self.requested = 0
self.fault_latched = False
self.stop("startup")
def stop(self, reason="disarmed", fault=False):
self.armed = False
self.duty = 0
self.reason = reason
self.fault_latched = self.fault_latched or fault
self.ready_ms = self.zero_ms = 0
self.candidate = None
self.button_ms = 0
self.stable_down = True # A button held during boot/fault cannot arm.
self.released = False
def _press(self, down, dt):
if down != self.candidate:
self.candidate, self.button_ms = down, 0
else:
self.button_ms += dt
if self.button_ms < BUTTON_MS or down == self.stable_down:
return False
self.stable_down = down
if not down:
self.released = True
return False
pressed, self.released = self.released, False
return pressed
def update(self, raw, button_down, healthy, dt_ms, reason="power/fault interlock"):
try:
command = knob_command(raw)
if type(dt_ms) is not int or not 0 < dt_ms <= MAX_SAMPLE_MS:
raise ValueError("Motor supervision sample is late")
except ValueError:
self.stop("invalid or late input", fault=True)
raise
self.requested = command
# Filter jitter during motion. Counterclockwise zero bypasses filtering.
self.filtered = 0 if command == 0 else (3 * self.filtered + command) // 4
if abs(self.filtered - command) < 4:
self.filtered = command # Reach the exact endpoint despite integer rounding.
if not healthy:
self.stop(reason, fault=self.armed)
return 0
self.ready_ms = min(READY_MS, self.ready_ms + dt_ms)
self.zero_ms = min(READY_MS, self.zero_ms + dt_ms) if command == 0 else 0
if self._press(bool(button_down), dt_ms):
if self.armed:
self.stop("disarmed by ARM button")
return 0
if self.ready_ms >= READY_MS and self.zero_ms >= READY_MS:
self.armed = True
self.fault_latched = False
self.reason = "armed at zero"
else:
self.reason = "return knob to zero, then release and press ARM"
if not self.armed or command == 0:
self.duty = 0
else:
target = self.filtered * self.maximum // FULL_SCALE
self.duty = min(target, self.duty + self.ramp * dt_ms // 1000)
self.reason = "speed command active"
return self.duty