Skip to content

Repeated CAN Errors when using PDP 2.0 #18

@PickleFace5

Description

@PickleFace5

Since the PDP 2.0 doesn't feature built-in current logging, we can't do diagnostics with the data. We fixed this issue with the following workaround (thought it can definitely be cleaned up):

def install_safe_power_distribution_logging() -> None:
    """
    Install a Power Distribution logger that does not spam CAN errors when no PDH/PDP
    is present. PyKit's Logger calls LoggedPowerDistribution.getInstance().saveToTable()
    every cycle; if no device exists at the expected CAN ID, that causes repeated
    "CAN: Message not found: Module N" errors.
    """
    module_id = getattr(
        Constants.CanIDs, "POWER_DISTRIBUTION_MODULE_ID", None
    )
    if module_id is not None:
        # PDH/PDP present: use real device but catch CAN errors so one bad cycle doesn't spam
        class _SafeLoggedPowerDistribution(LoggedPowerDistribution):
            def saveToTable(self, table):
                try:
                    super().saveToTable(table)
                except Exception:
                    table.put("Voltage", 0.0)
                    table.put("TotalCurrent", 0.0)
                    table.put("TotalPower", 0.0)
                    table.put("TotalEnergy", 0.0)
                    table.put("Temperature", 0.0)
                    table.put("ChannelCurrentsList", [])
                    table.put("ChannelCurrentsTotal", 0.0)

        LoggedPowerDistribution.instance = _SafeLoggedPowerDistribution(
            moduleId=module_id,
            moduleType=PowerDistribution.ModuleType.kRev,
        )
    else:
        # No PDH: stub that never touches CAN
        class _StubLoggedPowerDistribution(LoggedPowerDistribution):
            def __init__(self) -> None:
                self.moduleId = 0
                self.moduleType = PowerDistribution.ModuleType.kRev
                self.distribution = None

            def saveToTable(self, table):
                table.put("Voltage", 0.0)
                table.put("TotalCurrent", 0.0)
                table.put("TotalPower", 0.0)
                table.put("TotalEnergy", 0.0)
                table.put("Temperature", 0.0)
                table.put("ChannelCurrentsList", [])
                table.put("ChannelCurrentsTotal", 0.0)

        LoggedPowerDistribution.instance = _StubLoggedPowerDistribution()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions