Add room calibration lock solver

This commit is contained in:
Meta Vens 2026-05-31 21:26:11 +01:00
commit de4b12d84f
4 changed files with 414 additions and 0 deletions

View file

@ -67,6 +67,18 @@ The current status records are camera-device receipts for `/dev/video*` and a
tracking owns that signal. Values are `[present, videoIndex, isPs3Eye]` for
camera-device receipts.
`MimirRoomCalibrationLockSolver` is the first room-lock coordinator. It consumes
the LED string candidate/residual, the camera-rig pose update frame, Move
controller feature-history candidates, and PS3 Eye sparse-feature candidates.
It only reports `Locked` when every witness family is present: multiple cameras
share enough LED indices, the rig solver produced enough pose updates with low
mean ray residual, multiple Move identities have stable history, and multiple
Eyes contribute enough stable feature tracks. Missing families are reported by
name instead of being papered over. `Mimir.BufferSmoke
--room-calibration-lock-smoke` proves a synthetic three-camera LED string, two
Moves, and two Eyes lock at confidence `0.778`, while removing Move history
correctly drops the result to `InsufficientWitnesses`.
## Runtime Profile
Use:

View file

@ -150,6 +150,11 @@ if (args.Any(arg => string.Equals(arg, "--led-spline-calibration-smoke", StringC
return RunLedSplineCalibrationSmoke();
}
if (args.Any(arg => string.Equals(arg, "--room-calibration-lock-smoke", StringComparison.OrdinalIgnoreCase)))
{
return RunRoomCalibrationLockSmoke();
}
if (args.Any(arg => string.Equals(arg, "--led-spline-quality-sweep-smoke", StringComparison.OrdinalIgnoreCase)))
{
return RunLedSplineQualitySweepSmoke();
@ -926,6 +931,130 @@ static int RunLedSplineCalibrationSmoke()
: 1;
}
static int RunRoomCalibrationLockSmoke()
{
var scenePoints = new[]
{
new MimirLedSplineScenePoint(0, new Vector3(-0.20f, 0.30f, 2.20f), 0.95),
new MimirLedSplineScenePoint(1, new Vector3(-0.08f, 0.18f, 2.05f), 0.96),
new MimirLedSplineScenePoint(2, new Vector3(0.04f, 0.02f, 1.95f), 0.97),
new MimirLedSplineScenePoint(3, new Vector3(0.18f, -0.14f, 1.88f), 0.95),
new MimirLedSplineScenePoint(4, new Vector3(0.35f, -0.28f, 1.82f), 0.93),
};
var ps3Eye0Pose = new MimirCameraPoseEstimate("ps3-eye-0", Vector3.Zero, Quaternion.Identity, 1.0);
var ps3Eye1Pose = new MimirCameraPoseEstimate("ps3-eye-1", new Vector3(-0.18f, 0.04f, 0.02f), Quaternion.Identity, 1.0);
var kiyoPose = new MimirCameraPoseEstimate("kiyo-pro-rgb", new Vector3(0.24f, 0.02f, -0.03f), Quaternion.Identity, 1.0);
var curveSolver = new MimirLedSplineCurveSolver();
var ps3Eye0Fit = curveSolver.SolveCameraCurve(
ps3Eye0Pose.SourceId,
"ps3-eye-0:led-spline:lock",
320,
240,
2_000_000_000L,
ProjectLedDetections(scenePoints, ps3Eye0Pose, 320, 240, radiusPixels: 2.4));
var ps3Eye1Fit = curveSolver.SolveCameraCurve(
ps3Eye1Pose.SourceId,
"ps3-eye-1:led-spline:lock",
320,
240,
2_000_000_003L,
ProjectLedDetections(scenePoints, ps3Eye1Pose, 320, 240, radiusPixels: 2.3));
var kiyoFit = curveSolver.SolveCameraCurve(
kiyoPose.SourceId,
"kiyo-pro-rgb:led-spline:lock",
1280,
720,
2_000_000_006L,
ProjectLedDetections(scenePoints, kiyoPose, 1280, 720, radiusPixels: 4.0));
var ledCandidate = new MimirLedSplineFieldCandidate(
CandidateKey: "room-lock:led-string:frame-1",
CalibrationId: "room-calibration-lock",
SplineId: "co-streamer-led-string",
ProducerKey: "mimir-led-spline-detector",
CameraObservations:
[
curveSolver.ToCameraObservation(ps3Eye0Fit, 320, 240, 2_000_000_000L),
curveSolver.ToCameraObservation(ps3Eye1Fit, 320, 240, 2_000_000_003L),
curveSolver.ToCameraObservation(kiyoFit, 1280, 720, 2_000_000_006L),
],
EstimatedLedCount: 5,
HasTemporalCode: true,
HasStableLedIndices: true,
CurveLengthPixels: Math.Max(Math.Max(ps3Eye0Fit.CurveLengthPixels, ps3Eye1Fit.CurveLengthPixels), kiyoFit.CurveLengthPixels),
Confidence: 0.91,
ObservedTimeNs: 2_000_000_006L);
var residual = new MimirVisualCalibrationResidualSolver().EvaluateLedSpline(ledCandidate);
var rigCalibration = new MimirCameraRigCalibrationSolver().FitCameraPositionsFromLedSpline(
ledCandidate,
scenePoints,
[
ps3Eye0Pose with { PositionMeters = new Vector3(0.025f, -0.010f, 0.008f), Confidence = 0.82 },
ps3Eye1Pose with { PositionMeters = new Vector3(-0.120f, 0.030f, 0.000f), Confidence = 0.80 },
kiyoPose with { PositionMeters = new Vector3(0.120f, 0.000f, -0.010f), Confidence = 0.78 },
],
maximumDeltaMeters: 0.50);
var moveCandidate = new MimirFeatureTrackFieldCandidate(
"room-lock:moves:frame-1",
"room-calibration-lock",
"mimir-move-controller-tracker",
[
MoveObservation("ps3-eye-0", "move-a", 320, 240, 2_000_000_000L, 48.0, 120.0, 0.93),
MoveObservation("ps3-eye-1", "move-b", 320, 240, 2_000_000_003L, 210.0, 98.0, 0.91),
],
StableTrackCount: 2,
MeanTrackAgeFrames: 12.0,
MeanSpeedPixelsPerSecond: 34.0,
Confidence: 0.92,
ObservedTimeNs: 2_000_000_006L);
var eyeFeatureCandidate = new MimirFeatureTrackFieldCandidate(
"room-lock:eyes:frame-1",
"room-calibration-lock",
"mimir-sparse-feature-tracker",
[
SparseEyeObservation("ps3-eye-0", 320, 240, 2_000_000_000L, 90),
SparseEyeObservation("ps3-eye-1", 320, 240, 2_000_000_003L, 86),
],
StableTrackCount: 176,
MeanTrackAgeFrames: 9.4,
MeanSpeedPixelsPerSecond: 18.0,
Confidence: 0.88,
ObservedTimeNs: 2_000_000_006L);
var lockFrame = new MimirRoomCalibrationLockSolver().Evaluate(
"room-calibration-lock",
ledCandidate,
residual,
rigCalibration,
[moveCandidate],
[eyeFeatureCandidate]);
var missingMoveFrame = new MimirRoomCalibrationLockSolver().Evaluate(
"room-calibration-lock",
ledCandidate,
residual,
rigCalibration,
[],
[eyeFeatureCandidate]);
Console.WriteLine(
$"room-calibration-lock-smoke status={lockFrame.Status} confidence={lockFrame.Confidence:0.000} ledCameras={lockFrame.LedCameraCount} sharedLeds={lockFrame.SharedLedCount} poseUpdates={lockFrame.PoseUpdateCount} rayResidual={lockFrame.MeanRayDistanceMeters:0.000000} moves={lockFrame.MoveControllerCount} eyeSources={lockFrame.EyeSourceCount} eyeTracks={lockFrame.StableEyeTrackCount} missing={string.Join(",", lockFrame.MissingWitnesses)} negative={missingMoveFrame.Status}");
return lockFrame.HasLock &&
lockFrame.LedCameraCount == 3 &&
lockFrame.SharedLedCount == 5 &&
lockFrame.PoseUpdateCount == 3 &&
lockFrame.MeanRayDistanceMeters < 0.025 &&
lockFrame.MoveControllerCount == 2 &&
lockFrame.StableMoveTrackCount == 2 &&
lockFrame.EyeSourceCount == 2 &&
lockFrame.StableEyeTrackCount == 176 &&
lockFrame.Confidence >= 0.55 &&
lockFrame.MissingWitnesses.Count == 0 &&
missingMoveFrame.Status == MimirRoomCalibrationLockStatus.InsufficientWitnesses &&
missingMoveFrame.MissingWitnesses.Contains("move-controller-history", StringComparer.Ordinal)
? 0
: 1;
}
static int RunLedSplineQualitySweepSmoke()
{
var scenePoints = new[]
@ -1571,6 +1700,99 @@ static int DefaultStride(MimirVideoFrameDescriptor frame) =>
_ => frame.Width,
};
static MimirFeatureTrackCameraObservation MoveObservation(
string sourceId,
string controllerId,
int width,
int height,
long observedTimeNs,
double imageX,
double imageY,
double confidence)
{
var trackId = StablePositiveHash(controllerId);
var track = new MimirFeatureTrackPoint(
trackId,
imageX,
imageY,
PixelToClipX(imageX, width),
PixelToClipY(imageY, height),
VelocityXPerSecond: 12.0,
VelocityYPerSecond: -8.0,
AgeFrames: 12,
Confidence: confidence);
return new MimirFeatureTrackCameraObservation(
$"{sourceId}:move:{controllerId}:{observedTimeNs}",
sourceId,
width,
height,
observedTimeNs,
[track],
FrameCount: 1,
MeanTrackAgeFrames: 12.0,
MeanSpeedPixelsPerSecond: 14.4,
Confidence: confidence);
}
static MimirFeatureTrackCameraObservation SparseEyeObservation(
string sourceId,
int width,
int height,
long observedTimeNs,
int stableTrackCount)
{
var tracks = Enumerable.Range(0, stableTrackCount)
.Select(index =>
{
var x = 12.0 + index % 18 * 16.0;
var y = 14.0 + index / 18 * 18.0;
return new MimirFeatureTrackPoint(
StablePositiveHash($"{sourceId}:{index}"),
x,
y,
PixelToClipX(x, width),
PixelToClipY(y, height),
VelocityXPerSecond: 4.0 + index % 5,
VelocityYPerSecond: 3.0 + index % 7,
AgeFrames: 6 + index % 9,
Confidence: 0.72 + index % 8 * 0.02);
})
.ToArray();
return new MimirFeatureTrackCameraObservation(
$"{sourceId}:sparse-features:{observedTimeNs}",
sourceId,
width,
height,
observedTimeNs,
tracks,
FrameCount: 1,
MeanTrackAgeFrames: tracks.Average(static track => track.AgeFrames),
MeanSpeedPixelsPerSecond: tracks.Average(static track => Math.Sqrt(
track.VelocityXPerSecond * track.VelocityXPerSecond +
track.VelocityYPerSecond * track.VelocityYPerSecond)),
Confidence: tracks.Average(static track => track.Confidence));
}
static double PixelToClipX(double imageX, int width) =>
Math.Clamp(imageX / Math.Max(1.0, width) * 2.0 - 1.0, -1.0, 1.0);
static double PixelToClipY(double imageY, int height) =>
Math.Clamp(1.0 - imageY / Math.Max(1.0, height) * 2.0, -1.0, 1.0);
static int StablePositiveHash(string value)
{
unchecked
{
var hash = 17;
foreach (var ch in value)
{
hash = hash * 31 + ch;
}
return Math.Abs(hash == int.MinValue ? 0 : hash);
}
}
static IReadOnlyList<MimirLedPixelDetection> ProjectLedDetections(
IReadOnlyList<MimirLedSplineScenePoint> scenePoints,
MimirCameraPoseEstimate pose,

View file

@ -0,0 +1,179 @@
namespace Mimir.Runtime.Synchronization;
public enum MimirRoomCalibrationLockStatus
{
Empty,
InsufficientWitnesses,
Provisional,
Locked,
}
public sealed record MimirRoomCalibrationLockThresholds(
int MinimumLedCameraCount = 2,
int MinimumSharedLedCount = 4,
int MinimumPoseUpdateCount = 2,
double MaximumMeanRayDistanceMeters = 0.035,
int MinimumMoveControllerCount = 2,
int MinimumStableMoveTrackCount = 2,
int MinimumEyeSourceCount = 2,
int MinimumStableEyeTrackCount = 80,
double MinimumConfidence = 0.55);
public sealed record MimirRoomCalibrationLockFrame(
string CalibrationId,
MimirRoomCalibrationLockStatus Status,
int LedCameraCount,
int SharedLedCount,
int PoseUpdateCount,
double MeanRayDistanceMeters,
int MoveControllerCount,
int StableMoveTrackCount,
int EyeSourceCount,
int StableEyeTrackCount,
double Confidence,
IReadOnlyList<string> MissingWitnesses,
bool HasLock);
public sealed class MimirRoomCalibrationLockSolver(MimirRoomCalibrationLockThresholds? thresholds = null)
{
private readonly MimirRoomCalibrationLockThresholds thresholds = thresholds ?? new();
public MimirRoomCalibrationLockFrame Evaluate(
string calibrationId,
MimirLedSplineFieldCandidate? ledSpline,
MimirVisualCalibrationResidualFrame? ledResidual,
MimirCameraRigCalibrationFrame? rigCalibration,
IEnumerable<MimirFeatureTrackFieldCandidate> moveControllerCandidates,
IEnumerable<MimirFeatureTrackFieldCandidate> eyeFeatureCandidates)
{
var ledCameraCount = ledSpline?.CameraObservations.Count ?? 0;
var sharedLedCount = ledResidual?.PairResiduals.Count > 0
? ledResidual.PairResiduals.Max(static residual => residual.SharedLedCount)
: 0;
var poseUpdateCount = rigCalibration?.PoseUpdates.Count ?? 0;
var meanRayDistance = rigCalibration?.MeanRayDistanceMeters ?? double.NaN;
var moveCandidates = moveControllerCandidates.ToArray();
var moveControllerCount = moveCandidates
.SelectMany(static candidate => candidate.CameraObservations)
.SelectMany(static observation => observation.Tracks)
.Select(static track => track.TrackId)
.Distinct()
.Count();
var stableMoveTrackCount = moveCandidates.Sum(static candidate => candidate.StableTrackCount);
var eyeCandidates = eyeFeatureCandidates.ToArray();
var eyeSourceCount = eyeCandidates
.SelectMany(static candidate => candidate.CameraObservations)
.Select(static observation => observation.SourceId)
.Distinct(StringComparer.Ordinal)
.Count();
var stableEyeTrackCount = eyeCandidates.Sum(static candidate => candidate.StableTrackCount);
var missing = new List<string>();
if (ledCameraCount < thresholds.MinimumLedCameraCount ||
sharedLedCount < thresholds.MinimumSharedLedCount)
{
missing.Add("led-spline-correspondence");
}
if (poseUpdateCount < thresholds.MinimumPoseUpdateCount ||
!double.IsFinite(meanRayDistance) ||
meanRayDistance > thresholds.MaximumMeanRayDistanceMeters)
{
missing.Add("camera-pose-update");
}
if (moveControllerCount < thresholds.MinimumMoveControllerCount ||
stableMoveTrackCount < thresholds.MinimumStableMoveTrackCount)
{
missing.Add("move-controller-history");
}
if (eyeSourceCount < thresholds.MinimumEyeSourceCount ||
stableEyeTrackCount < thresholds.MinimumStableEyeTrackCount)
{
missing.Add("ps3-eye-feature-field");
}
var confidence = Confidence(
ledSpline,
ledResidual,
rigCalibration,
moveCandidates,
eyeCandidates,
sharedLedCount,
stableEyeTrackCount);
var hasAllWitnesses = missing.Count == 0;
var status = Status(hasAllWitnesses, confidence, ledCameraCount, moveCandidates.Length, eyeCandidates.Length);
return new MimirRoomCalibrationLockFrame(
calibrationId,
status,
ledCameraCount,
sharedLedCount,
poseUpdateCount,
meanRayDistance,
moveControllerCount,
stableMoveTrackCount,
eyeSourceCount,
stableEyeTrackCount,
confidence,
missing,
HasLock: status == MimirRoomCalibrationLockStatus.Locked);
}
private MimirRoomCalibrationLockStatus Status(
bool hasAllWitnesses,
double confidence,
int ledCameraCount,
int moveCandidateCount,
int eyeCandidateCount)
{
if (ledCameraCount == 0 && moveCandidateCount == 0 && eyeCandidateCount == 0)
{
return MimirRoomCalibrationLockStatus.Empty;
}
if (!hasAllWitnesses)
{
return MimirRoomCalibrationLockStatus.InsufficientWitnesses;
}
return confidence >= thresholds.MinimumConfidence
? MimirRoomCalibrationLockStatus.Locked
: MimirRoomCalibrationLockStatus.Provisional;
}
private static double Confidence(
MimirLedSplineFieldCandidate? ledSpline,
MimirVisualCalibrationResidualFrame? ledResidual,
MimirCameraRigCalibrationFrame? rigCalibration,
IReadOnlyList<MimirFeatureTrackFieldCandidate> moveCandidates,
IReadOnlyList<MimirFeatureTrackFieldCandidate> eyeCandidates,
int sharedLedCount,
int stableEyeTrackCount)
{
var ledConfidence = ledSpline is null || ledResidual is null
? 0.0
: Clamp01(ledSpline.Confidence) *
Clamp01(ledResidual.Confidence) *
Clamp01(sharedLedCount / 5.0);
var poseConfidence = rigCalibration is null || !rigCalibration.HasPoseUpdate
? 0.0
: Clamp01(rigCalibration.Confidence) *
(double.IsFinite(rigCalibration.MeanRayDistanceMeters)
? 1.0 / (1.0 + rigCalibration.MeanRayDistanceMeters * 40.0)
: 0.0);
var moveConfidence = moveCandidates.Count == 0
? 0.0
: Clamp01(moveCandidates.Average(static candidate => candidate.Confidence));
var eyeConfidence = eyeCandidates.Count == 0
? 0.0
: Clamp01(eyeCandidates.Average(static candidate => candidate.Confidence)) *
Clamp01(stableEyeTrackCount / 160.0);
return Clamp01(0.40 * ledConfidence + 0.30 * poseConfidence + 0.15 * moveConfidence + 0.15 * eyeConfidence);
}
private static double Clamp01(double value) =>
double.IsFinite(value) ? Math.Clamp(value, 0.0, 1.0) : 0.0;
}

View file

@ -278,3 +278,4 @@
{"ts": "2026-05-31T19:50:49+01:00", "type": "decision", "status": "ok", "note": "Nightwing is now the explicit Eyes/Moves witness authority: it owns local PS3 Eye USB reads, PS Move Bluetooth/HID, LED schedules, timestamps, and compact marker/feature receipts, while Starfire keeps canonical timing/global calibration/program sync and Fensalir keeps dense GPU fusion/surface/reservoir presentation. Encoded in Nightwing distributed witness/profile and verified with --perfect-machine-profile-smoke."}
{"ts": "2026-05-31T20:29:03+01:00", "type": "verification", "status": "ok", "note": "Nightwing now hosts both PS3 Eyes. Linux enumerates them as ov534/gspca V4L2 devices on /dev/video2 and /dev/video3; tools/nightwing_v4l2_cadence_probe.py proved both deliver 320x240 at ~187 fps with zero sequence gaps after explicitly setting VIDIOC_S_PARM timeperframe=1/187. Default streaming remains 30 fps and is not a valid cadence proof."}
{"ts": "2026-05-31T21:07:55+01:00", "type": "implementation", "status": "ok", "note": "Nightwing visible ops now drinks typed witness status through Mimir/Odin instead of a local status sidecar: tools/nightwing_typed_witness_publisher.py sends binary MessagePack mimir.eve_sensor_observation.v1 records to Mimir.EveSensorReceiver, Odin projects them as observation-stream nodes, and the Nightwing Eve TUI renders those accepted CultMesh streams. Live deployment showed fresh nightwing-video0..3 and nightwing-leap-tracking streams on the TUI."}
{"ts": "2026-05-31T21:25:32+01:00", "type": "implementation", "status": "ok", "note": "Added MimirRoomCalibrationLockSolver as the first room-lock coordinator. It consumes LED spline residuals, camera-rig pose updates, Move controller feature histories, and PS3 Eye sparse feature candidates, reports named missing witness families, and only locks when all families are present. --room-calibration-lock-smoke passes with 3 LED cameras, 5 shared LEDs, 3 pose updates, 2 Moves, 2 Eye sources, 176 stable Eye tracks, confidence 0.778; removing Moves drops to InsufficientWitnesses."}