Files
bambuddy/backend/app/services/slot_kprofile.py
T
maziggy c5e0055864 Track which nozzle each AMS feeds through the Filament Track Switch
With a switch fitted, an AMS is not wired to a nozzle any more. It is
plumbed into one of the switch's two inlets and reaches both nozzles
through it, so every unit reports its extruder as "not fixed" (0xE) and
ams_extruder_map comes back empty. Bambuddy had nothing to fall back on
but the AMS unit number, so AMS-A was badged R and AMS-B was badged L
purely because their unit ids are 0 and 1, a third unit got no badge at
all, and every one of those labels was wrong.

The binding needed no new telemetry. BambuStudio reads it out of bits
24-27 of the same AMS info string we already parse for the type and the
extruder id -- 0 is In-B, 1 is In-A -- and it only means anything when a
switch is installed, because without one 0xE really does mean an
uninitialised unit. That gates the read, which forced the switch block to
be parsed before the AMS block: _handle_ams_data runs early in
_process_message and _update_state only much later, so the binding was
lost on every frame that carried both.

The badge letters stay L and R, In-A reading as L, with the inlet named
in full in the tooltip -- the letter is the inlet's position and not a
claim about which nozzle that AMS feeds, since the switch can route
either inlet to either outlet. Both views update live now. The switch
fields were missing from the WebSocket payload, and the frontend
shallow-merges each push over its cached status, so an absent field kept
whatever the last full fetch left behind. The broadcast dedup key had no
term for them either, so "Join IN-B" on the printer screen moved nothing:
the binding is not in the tray component of that key, and not in the AMS
change-hash, which covers tray fields only and must stay that way because
it drives Spoolman sync.

The rest of this is the calibration half, which is where it actually
bites. K-profiles are calibrated per nozzle and the printer numbers its
calibration table per nozzle too, so entry 16 exists on both hotends and
means a different profile on each. A tray holds exactly one index. Move
an AMS to the other inlet and every configured slot in it silently keeps
pointing at the old hotend's table -- measured on the maintainer's H2C, a
black PLA calibrated 0.018 left and 0.020 right stayed on the left
profile after the move, and a manual RFID re-read only re-asserted the
same wrong one. Bambu has not solved this either; their AMS dialog
carries "TODO: fila_switcher broken the connection of ams->extruder"
above the line that decides which nozzle's profiles to offer.

Three copies of "which extruder is this slot on" each ended in else 0,
which on a switch machine filed every profile under the right-hand
nozzle. They now share one resolver that returns None for "unknown",
because unknown and extruder 0 are very different answers on a
dual-nozzle machine and conflating them is what bound a left-nozzle
profile to a slot sitting on the right. The per-slot K value on the
printer card had the same confusion from the other direction: its lookup
was keyed on cali_idx alone, so one nozzle's K silently overwrote the
other's.

Moving an AMS now re-selects each configured slot's counterpart profile
for the nozzle it has arrived on. Only the calibration binding moves, and
only for slots whose spool already has a profile for that nozzle:
configuring a slot is a deliberate preparation step, so a slot we know
nothing about, or a spool calibrated on one hotend only, is left exactly
as the operator set it. Nothing fires on the first sighting of a binding
either, since every reconnect learns them afresh and re-applying there
would overwrite a choice made by hand.

Configure Slot resolves against the slot's own nozzle throughout. Option
identity carries the extruder, so a filament calibrated on both hotends
gives two distinguishable entries instead of two that collapse into
whichever the printer listed first; options name the hotend; matches are
scoped to the nozzle the slot feeds, with the other hotend's profiles
still reachable under Other K profiles; and the slot's active index is
resolved as a pair rather than followed into the wrong table.

Inlet to nozzle is one table, In-A to the left hotend and In-B to the
right, measured rather than assumed -- fila_switch.out cannot be used for
it, reporting [1, 1] unchanged across a 90-second capture, both outlets
claiming the same extruder. The print dialog picks up the same inlet
labelling in its slot dropdown, replacing a left/right hint that never
rendered because it matched snow-encoded values against global tray ids;
decoding it correctly would not have saved it, since the firmware never
reports which inlet is currently paired with which outlet. The dialog
also notes when every filament a print needs sits behind one inlet, which
is legal but slow -- a change between two filaments on the same inlet
retracts the outgoing spool all the way back to its AMS, where a change
across the two only retracts as far as the switch.

Assigning an AMS to an inlet remains printer-side. BambuStudio can read
that binding and has no command to write it, so there is no wire format
to copy.
2026-08-16 15:59:59 +02:00

133 lines
4.7 KiB
Python

"""Find the stored K-profile for an AMS slot on a particular nozzle.
K-profiles are per-nozzle: Bambuddy already keeps one row per
``(spool, printer, extruder)`` in ``spool_k_profile`` (and the Spoolman mirror
in ``spoolman_k_profile``), each with its own ``cali_idx`` and K value. On the
maintainer's H2C, one black PLA reads 0.018 on the left hotend and 0.020 on the
right, stored as calibration indices 16 and 15.
A tray, by contrast, holds exactly **one** ``cali_idx``. So whenever a slot's
nozzle changes — which on a Filament Track Switch machine happens every time an
AMS is moved between the switch's two inlets — the stored counterpart for the
new nozzle has to be looked up and re-selected. This module is that lookup.
"""
from dataclasses import dataclass
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.models.spool import Spool
from backend.app.models.spool_assignment import SpoolAssignment
from backend.app.models.spool_k_profile import SpoolKProfile
from backend.app.models.spoolman_k_profile import SpoolmanKProfile
from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
@dataclass(frozen=True)
class SlotKProfile:
"""A stored calibration profile, flattened across the two inventory backends."""
cali_idx: int | None
k_value: float | None
name: str | None
extruder: int
# The preset the profile was calibrated under. ``extrusion_cali_sel`` must
# carry this rather than the tray's RFID value, or the firmware mislinks it.
filament_id: str | None
async def find_slot_kprofile_for_extruder(
db: AsyncSession,
printer_id: int,
ams_id: int,
tray_id: int,
extruder: int,
nozzle_diameter: str,
) -> SlotKProfile | None:
"""Stored profile for whatever is in this slot, calibrated for ``extruder``.
Returns None when the slot holds no known spool, or when that spool has no
profile for this nozzle — an operator who calibrated only one side should
keep the binding they set by hand rather than have it swapped for a guess.
Local spools take priority over Spoolman, matching the rest of the
K-profile cascade.
"""
assignment = (
await db.execute(
select(SpoolAssignment).where(
SpoolAssignment.printer_id == printer_id,
SpoolAssignment.ams_id == ams_id,
SpoolAssignment.tray_id == tray_id,
)
)
).scalar_one_or_none()
if assignment is not None:
profile = (
(
await db.execute(
select(SpoolKProfile).where(
SpoolKProfile.spool_id == assignment.spool_id,
SpoolKProfile.printer_id == printer_id,
SpoolKProfile.extruder == extruder,
SpoolKProfile.nozzle_diameter == nozzle_diameter,
)
)
)
.scalars()
.first()
)
if profile is not None:
spool = (await db.execute(select(Spool).where(Spool.id == assignment.spool_id))).scalar_one_or_none()
return SlotKProfile(
cali_idx=profile.cali_idx,
k_value=profile.k_value,
name=profile.name,
extruder=profile.extruder,
filament_id=(spool.slicer_filament if spool else None),
)
# A known spool with no profile for this nozzle is a deliberate stop:
# falling through to Spoolman would answer for a different spool.
return None
sm_assignment = (
await db.execute(
select(SpoolmanSlotAssignment).where(
SpoolmanSlotAssignment.printer_id == printer_id,
SpoolmanSlotAssignment.ams_id == ams_id,
SpoolmanSlotAssignment.tray_id == tray_id,
)
)
).scalar_one_or_none()
if sm_assignment is None:
return None
sm_profile = (
(
await db.execute(
select(SpoolmanKProfile).where(
SpoolmanKProfile.spoolman_spool_id == sm_assignment.spoolman_spool_id,
SpoolmanKProfile.printer_id == printer_id,
SpoolmanKProfile.extruder == extruder,
SpoolmanKProfile.nozzle_diameter == nozzle_diameter,
)
)
)
.scalars()
.first()
)
if sm_profile is None:
return None
# Spoolman rows carry no slicer preset; the caller falls back to the tray's
# own tray_info_idx for filament_id.
return SlotKProfile(
cali_idx=sm_profile.cali_idx,
k_value=sm_profile.k_value,
name=sm_profile.name,
extruder=sm_profile.extruder,
filament_id=None,
)