imrishabh18/rp2040-motor-controller

This circuit module provides a microcontroller-based NEMA17 stepper motor driver with integrated USB-C Power Delivery, temperature sensing, and safety interlocks.

Version
1.0.16
License
unset
Stars
0

firmware/thermal_policy.py

"""Host-testable shutdown policy; use only through ThermalGuard on the PCB."""
from tmp102 import WARN_C, TRIP_C, RESTART_C


class ThermalPolicy:
    def __init__(self):
        self.enabled = False
        self.latched = True
        self.reason = "startup: explicit arm required"
        self.temperature_c = None
        self.healthy = False

    def trip(self, reason):
        self.enabled = False
        self.latched = True
        self.reason = reason

    def sample(self, temperature, alert_ok=True, sensor_ok=True, driver_fault=False):
        self.temperature_c = temperature
        self.healthy = bool(sensor_ok and temperature is not None
                            and -40 <= temperature <= 125 and alert_ok)
        if not self.healthy:
            self.trip("sensor or hardware temperature fault")
        elif temperature >= TRIP_C:
            self.trip("overtemperature")
        elif driver_fault:
            self.trip("motor driver fault")
        # A cool reading never clears the latch or enables the motor.
        return self.status()

    def arm(self):
        if not self.healthy or self.temperature_c >= RESTART_C:
            return False
        self.latched = False
        self.enabled = True
        self.reason = "enabled"
        return True

    def status(self):
        if self.latched:
            return "STOPPED: " + self.reason
        if self.temperature_c is not None and self.temperature_c >= WARN_C:
            return "WARNING: driver board is hot"
        return "ENABLED" if self.enabled else "STOPPED"