feat: maximize sorter demo realism and add safe agent MVP
Wire live classifyItem into continuous playback, add jam/E-stop cases, presenter controls (seek/speed/hotkeys), quality modes, demo scripts, audit docs, and a verify-only autonomous agent CLI with hard safety limits. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
25
src/App.tsx
25
src/App.tsx
@@ -22,7 +22,12 @@ import {
|
||||
resumePlayback,
|
||||
stopPlayback,
|
||||
updatePlayback,
|
||||
seekToCase,
|
||||
seekNextCase,
|
||||
seekPrevCase,
|
||||
setPlaybackSpeed,
|
||||
type ContinuousPlaybackState,
|
||||
type PlaybackSpeed,
|
||||
} from './domain/continuousPlayback';
|
||||
|
||||
function scrollToId(id: string) {
|
||||
@@ -236,6 +241,22 @@ function AppContent() {
|
||||
setPlayback(stopPlayback);
|
||||
}, []);
|
||||
|
||||
const handleSeekCase = useCallback((index: number) => {
|
||||
setPlayback((prev) => seekToCase(prev, index));
|
||||
}, []);
|
||||
|
||||
const handleSeekNext = useCallback(() => {
|
||||
setPlayback((prev) => seekNextCase(prev));
|
||||
}, []);
|
||||
|
||||
const handleSeekPrev = useCallback(() => {
|
||||
setPlayback((prev) => seekPrevCase(prev));
|
||||
}, []);
|
||||
|
||||
const handleSetSpeed = useCallback((speed: PlaybackSpeed) => {
|
||||
setPlayback((prev) => setPlaybackSpeed(prev, speed));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Routes>
|
||||
<Route
|
||||
@@ -246,6 +267,10 @@ function AppContent() {
|
||||
onPlay={handleMainPlay}
|
||||
onPause={handleMainPause}
|
||||
onStop={handleMainStop}
|
||||
onSeekCase={handleSeekCase}
|
||||
onSeekNext={handleSeekNext}
|
||||
onSeekPrev={handleSeekPrev}
|
||||
onSetSpeed={handleSetSpeed}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -40,6 +40,8 @@ export default function CVInspectionOverlay({ data, visible }: CVInspectionOverl
|
||||
command,
|
||||
cPriorityApplied,
|
||||
isLowConfidence,
|
||||
classificationReason,
|
||||
classificationLabel,
|
||||
itemTitle,
|
||||
itemDimensions,
|
||||
} = data;
|
||||
@@ -177,6 +179,16 @@ export default function CVInspectionOverlay({ data, visible }: CVInspectionOverl
|
||||
</div>
|
||||
)}
|
||||
|
||||
{classificationReason && (
|
||||
<div className="cv-row cv-reason-row">
|
||||
<span className="cv-label">RULE</span>
|
||||
<span className="cv-value cv-reason">
|
||||
{classificationLabel ? `${classificationLabel} — ` : ''}
|
||||
{classificationReason}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="cv-row cv-command-row">
|
||||
<span className="cv-label">CMD</span>
|
||||
<span
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Suspense, memo, useEffect, useMemo } from 'react';
|
||||
import { getPhysicalItemPose } from '../../domain/physicalItemMotion';
|
||||
import { getModelAsset } from '../../data/modelAssets';
|
||||
import { ITEMS } from '../../data/items';
|
||||
import { resolveItem } from '../../data/resolveItem';
|
||||
import type { PlaylistCase } from '../../domain/demoPlaylist';
|
||||
import { classifyItem } from '../../domain/classifier';
|
||||
import { getRenderedItemDimensions } from '../../domain/physicalLayout';
|
||||
import * as THREE from 'three';
|
||||
import { useLoader } from '@react-three/fiber';
|
||||
@@ -33,9 +34,6 @@ interface RenderProps {
|
||||
metalness: number;
|
||||
}
|
||||
|
||||
// STL geometry is cached globally by useLoader (one geometry per URL, shared
|
||||
// across items). We only render the solid mesh — no per-item wireframe overlay,
|
||||
// which keeps draw calls and allocations low.
|
||||
function STLGeometry({ path, scale, color, accentColor, emissiveIntensity, roughness, metalness }: RenderProps & {
|
||||
path: string;
|
||||
scale: [number, number, number];
|
||||
@@ -60,7 +58,6 @@ function FallbackPrimitive({ type, color, accentColor, emissiveIntensity, roughn
|
||||
type: 'box' | 'cylinder' | 'sphere';
|
||||
w: number; h: number; d: number;
|
||||
}) {
|
||||
// Memoize the geometry once per size so we don't allocate every render.
|
||||
const geometry = useMemo<THREE.BufferGeometry>(() => {
|
||||
if (type === 'cylinder' || type === 'sphere') {
|
||||
const r = Math.max(w, d) / 2;
|
||||
@@ -81,29 +78,32 @@ export const PhysicalPlaybackItem = memo(function PhysicalPlaybackItem({
|
||||
caseData,
|
||||
elapsedMs,
|
||||
slotIndex = 0,
|
||||
jitter,
|
||||
}: {
|
||||
caseData: PlaylistCase;
|
||||
elapsedMs: number;
|
||||
slotIndex?: number;
|
||||
jitter?: { x: number; z: number; yaw: number };
|
||||
}) {
|
||||
const itemId = caseData.itemId.replace('-LC', '');
|
||||
const itemData = useMemo(() => ITEMS.find(i => i.id === itemId) ?? ITEMS[0], [itemId]);
|
||||
const itemData = useMemo(() => resolveItem(caseData.itemId), [caseData.itemId]);
|
||||
const classification = useMemo(() => classifyItem(itemData), [itemData]);
|
||||
const itemId = itemData.id.replace('-LC', '');
|
||||
const asset = getModelAsset(itemId);
|
||||
|
||||
const dims = getRenderedItemDimensions(itemData.dimensionsMm);
|
||||
|
||||
const pose = getPhysicalItemPose({
|
||||
caseId: caseData.id,
|
||||
slotIndex,
|
||||
dimensionsMm: itemData.dimensionsMm,
|
||||
targetCategory: caseData.expectedCategory,
|
||||
targetCategory: classification.category,
|
||||
elapsedMs,
|
||||
faultType: caseData.faultType,
|
||||
jitter,
|
||||
});
|
||||
|
||||
const { position, rotation, phase, surface, isSettled } = pose;
|
||||
const isRouting = phase === 'routing';
|
||||
const useSimplifiedMesh = phase === 'settled' || isSettled;
|
||||
// Contact shadow only while the item rides a flat transport surface.
|
||||
const onTransport = surface === 'main_belt'
|
||||
|| surface === 'inspection_station'
|
||||
|| surface === 'routing_junction'
|
||||
@@ -111,13 +111,14 @@ export const PhysicalPlaybackItem = memo(function PhysicalPlaybackItem({
|
||||
|
||||
if (elapsedMs < 0) return null;
|
||||
|
||||
const routeAccent = COLORS[caseData.expectedCategory] ?? COLORS.sensorAccent;
|
||||
const routeAccent = COLORS[classification.category] ?? COLORS.sensorAccent;
|
||||
const material = ITEM_MATERIALS[itemId] ?? { color: '#d8c3a5', roughness: 0.75 };
|
||||
const bodyColor = useSimplifiedMesh ? (material.color ?? '#b8b2a8') : material.color;
|
||||
const bodyColor = phase === 'fault' ? '#ef4444' : useSimplifiedMesh ? (material.color ?? '#b8b2a8') : material.color;
|
||||
const accentColor = useSimplifiedMesh ? '#94a3b8' : routeAccent;
|
||||
const emissiveIntensity = useSimplifiedMesh ? 0.01 : (isRouting ? 0.12 : 0.03);
|
||||
const emissiveIntensity = useSimplifiedMesh
|
||||
? 0.01
|
||||
: (phase === 'fault' ? 0.25 : isRouting ? 0.12 : 0.03);
|
||||
const metalness = material.metalness ?? 0.05;
|
||||
|
||||
const useSTL = !useSimplifiedMesh && asset?.loaderType === 'stl' && asset?.frontendAssetPath;
|
||||
const stlPath = asset?.frontendAssetPath ?? '';
|
||||
const fallbackType = asset?.fallbackPrimitive ?? 'box';
|
||||
|
||||
@@ -22,7 +22,7 @@ import { ITEMS } from '../../data/items';
|
||||
import type { Category } from '../../domain/types';
|
||||
import { PhysicalPlaybackItem } from './PhysicalPlaybackItem';
|
||||
import { DEMO_PLAYLIST, PLAYLIST_LENGTH } from '../../domain/demoPlaylist';
|
||||
import { CASE_DURATION_MS } from '../../domain/continuousPlayback';
|
||||
import { cumulativePlaylistDurationMs, getPlaylistCaseDurationMs } from '../../domain/continuousPlayback';
|
||||
import {
|
||||
getCameraConfig,
|
||||
smoothCameraTransition,
|
||||
@@ -133,13 +133,14 @@ function CinematicCameraController({
|
||||
// Get item position for camera following
|
||||
const itemPosition = useMemo(() => {
|
||||
if (playback.status === 'idle') return null;
|
||||
const currentItemElapsed = playback.totalElapsedMs - (playback.currentCaseIndex * CASE_DURATION_MS);
|
||||
const pose = getPhysicalItemPose({
|
||||
caseId: playback.currentCase.id,
|
||||
dimensionsMm: { width: 300, depth: 200, height: 200 }, // rough approx for camera target
|
||||
dimensionsMm: { width: 300, depth: 200, height: 200 },
|
||||
targetCategory: playback.targetCategory,
|
||||
elapsedMs: currentItemElapsed,
|
||||
slotIndex: playback.currentCaseIndex
|
||||
elapsedMs: playback.caseElapsedMs,
|
||||
slotIndex: playback.currentCaseIndex,
|
||||
faultType: playback.currentCase.faultType,
|
||||
jitter: playback.positionJitter,
|
||||
});
|
||||
return pose.position;
|
||||
}, [playback]);
|
||||
@@ -1211,8 +1212,9 @@ function ContinuousScene({
|
||||
|
||||
// Compute physical items based on elapsed time. Cap the number of
|
||||
// simultaneously rendered items to keep the scene lightweight.
|
||||
const { totalElapsedMs, currentCase, currentCaseIndex } = playback;
|
||||
const casesSpawned = Math.floor(totalElapsedMs / CASE_DURATION_MS) + 1;
|
||||
// Use cumulative playlist durations (cases may differ: jam / e-stop).
|
||||
const { totalElapsedMs, currentCase, currentCaseIndex, caseElapsedMs, positionJitter } = playback;
|
||||
const casesSpawned = currentCaseIndex + 1;
|
||||
const startIndex = Math.max(0, casesSpawned - MAX_VISIBLE_ITEMS);
|
||||
|
||||
const sceneItems = useMemo(() => {
|
||||
@@ -1220,16 +1222,20 @@ function ContinuousScene({
|
||||
for (let i = startIndex; i < casesSpawned; i++) {
|
||||
const playlistIndex = i % PLAYLIST_LENGTH;
|
||||
const caseData = DEMO_PLAYLIST[playlistIndex];
|
||||
const elapsedMs = totalElapsedMs - (i * CASE_DURATION_MS);
|
||||
const caseStart = cumulativePlaylistDurationMs(i);
|
||||
const elapsedMs = i === currentCaseIndex
|
||||
? caseElapsedMs
|
||||
: getPlaylistCaseDurationMs(caseData) + 1; // settled past end for prior cases
|
||||
items.push({
|
||||
id: `item-${i}-${caseData.id}`,
|
||||
slotIndex: i,
|
||||
caseData,
|
||||
elapsedMs,
|
||||
elapsedMs: Math.max(0, elapsedMs),
|
||||
caseStart,
|
||||
});
|
||||
}
|
||||
return items;
|
||||
}, [totalElapsedMs, casesSpawned, startIndex]);
|
||||
}, [caseElapsedMs, casesSpawned, startIndex, currentCaseIndex]);
|
||||
|
||||
// Get item data for the current case
|
||||
const itemId = currentCase.itemId.replace('-LC', '');
|
||||
@@ -1239,13 +1245,15 @@ function ContinuousScene({
|
||||
const itemScale = Math.max(dims.width, dims.depth, dims.height);
|
||||
|
||||
// Get item position for measurement visualization using physical model
|
||||
const currentItemElapsed = totalElapsedMs - (currentCaseIndex * CASE_DURATION_MS);
|
||||
const currentItemElapsed = caseElapsedMs;
|
||||
const currentPose = getPhysicalItemPose({
|
||||
caseId: currentCase.id,
|
||||
dimensionsMm: itemData.dimensionsMm,
|
||||
targetCategory: category,
|
||||
elapsedMs: currentItemElapsed,
|
||||
slotIndex: currentCaseIndex
|
||||
slotIndex: currentCaseIndex,
|
||||
faultType: currentCase.faultType,
|
||||
jitter: positionJitter,
|
||||
});
|
||||
const itemPos = currentPose.position;
|
||||
const activeRoute = currentPose.activeRoute;
|
||||
@@ -1377,6 +1385,7 @@ function ContinuousScene({
|
||||
caseData={item.caseData}
|
||||
elapsedMs={item.elapsedMs}
|
||||
slotIndex={item.slotIndex}
|
||||
jitter={item.slotIndex === currentCaseIndex ? positionJitter : undefined}
|
||||
/>
|
||||
))}
|
||||
|
||||
|
||||
29
src/data/resolveItem.ts
Normal file
29
src/data/resolveItem.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Resolve playlist / scenario item ids, including low-confidence variants (SKU-*-LC).
|
||||
*/
|
||||
|
||||
import type { Item } from '../domain/types';
|
||||
import { getItem, ITEMS } from './items';
|
||||
|
||||
/** Strip -LC suffix and apply low confidence when present. */
|
||||
export function resolveItem(itemId: string): Item {
|
||||
if (itemId.endsWith('-LC')) {
|
||||
const baseId = itemId.slice(0, -3);
|
||||
const base = getItem(baseId);
|
||||
return {
|
||||
...base,
|
||||
id: itemId,
|
||||
confidence: Math.min(base.confidence, 0.58),
|
||||
};
|
||||
}
|
||||
return getItem(itemId);
|
||||
}
|
||||
|
||||
export function findItemOrFallback(itemId: string): Item {
|
||||
try {
|
||||
return resolveItem(itemId);
|
||||
} catch {
|
||||
const stripped = itemId.replace(/-LC$/, '');
|
||||
return ITEMS.find((i) => i.id === stripped) ?? ITEMS[0];
|
||||
}
|
||||
}
|
||||
@@ -123,6 +123,11 @@ export function getCameraModeForPhase(phase: CasePhase): CameraMode {
|
||||
return 'resultZone';
|
||||
case 'clear_gap':
|
||||
return 'nextItemReset';
|
||||
case 'fault_hold':
|
||||
case 'emergency_hold':
|
||||
return 'routingWide';
|
||||
case 'recover':
|
||||
return 'overview';
|
||||
default:
|
||||
return 'overview';
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
/**
|
||||
* Continuous Playback Engine tests + playlist ↔ classifier consistency.
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
createPlaybackState,
|
||||
@@ -6,48 +10,44 @@ import {
|
||||
resumePlayback,
|
||||
stopPlayback,
|
||||
updatePlayback,
|
||||
seekToCase,
|
||||
setPlaybackSpeed,
|
||||
CASE_DURATION_MS,
|
||||
assertPlaylistClassifierConsistency,
|
||||
type ContinuousPlaybackState,
|
||||
} from './continuousPlayback';
|
||||
import { DEMO_PLAYLIST, PLAYLIST_LENGTH } from './demoPlaylist';
|
||||
import { classifyItem } from './classifier';
|
||||
import { resolveItem } from '../data/resolveItem';
|
||||
|
||||
describe('demoPlaylist', () => {
|
||||
it('has exactly 8 cases', () => {
|
||||
expect(PLAYLIST_LENGTH).toBe(8);
|
||||
expect(DEMO_PLAYLIST.length).toBe(8);
|
||||
it('has classification + safety cases', () => {
|
||||
expect(PLAYLIST_LENGTH).toBe(10);
|
||||
expect(DEMO_PLAYLIST.length).toBe(10);
|
||||
});
|
||||
|
||||
it('has no duplicate case ids', () => {
|
||||
const ids = DEMO_PLAYLIST.map(c => c.id);
|
||||
const uniqueIds = new Set(ids);
|
||||
expect(uniqueIds.size).toBe(ids.length);
|
||||
const ids = DEMO_PLAYLIST.map((c) => c.id);
|
||||
expect(new Set(ids).size).toBe(ids.length);
|
||||
});
|
||||
|
||||
it('has no mixed category (all cases have specific B/C/D)', () => {
|
||||
const validCategories = ['B', 'C', 'D'];
|
||||
DEMO_PLAYLIST.forEach(c => {
|
||||
expect(validCategories).toContain(c.expectedCategory);
|
||||
});
|
||||
it('all non-fault cases match live classifyItem', () => {
|
||||
const mismatches = assertPlaylistClassifierConsistency();
|
||||
expect(mismatches).toEqual([]);
|
||||
});
|
||||
|
||||
it('all commands would be ROUTE_TO_B/C/D', () => {
|
||||
DEMO_PLAYLIST.forEach(c => {
|
||||
const expectedCommand = `ROUTE_TO_${c.expectedCategory}`;
|
||||
expect(['ROUTE_TO_B', 'ROUTE_TO_C', 'ROUTE_TO_D']).toContain(expectedCommand);
|
||||
});
|
||||
});
|
||||
|
||||
it('low_confidence case has warning but final category is B/C/D', () => {
|
||||
const lowConfidence = DEMO_PLAYLIST.find(c => c.id === 'low_confidence');
|
||||
it('low_confidence case has warning but category from rules', () => {
|
||||
const lowConfidence = DEMO_PLAYLIST.find((c) => c.id === 'low_confidence');
|
||||
expect(lowConfidence).toBeDefined();
|
||||
expect(lowConfidence!.warning).toBeDefined();
|
||||
expect(['B', 'C', 'D']).toContain(lowConfidence!.expectedCategory);
|
||||
const result = classifyItem(resolveItem(lowConfidence!.itemId));
|
||||
expect(result.category).toBe('B');
|
||||
expect(result.warnings.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('has correct case order: B, B, C, C, D, D, C, B', () => {
|
||||
const expectedOrder = ['B', 'B', 'C', 'C', 'D', 'D', 'C', 'B'];
|
||||
const actualOrder = DEMO_PLAYLIST.map(c => c.expectedCategory);
|
||||
expect(actualOrder).toEqual(expectedOrder);
|
||||
it('includes jam and emergency_stop safety cases', () => {
|
||||
expect(DEMO_PLAYLIST.some((c) => c.faultType === 'jam')).toBe(true);
|
||||
expect(DEMO_PLAYLIST.some((c) => c.faultType === 'emergency_stop')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -58,112 +58,75 @@ describe('continuousPlayback', () => {
|
||||
expect(state.currentCaseIndex).toBe(0);
|
||||
});
|
||||
|
||||
it('startPlayback sets status to running', () => {
|
||||
const state = createPlaybackState();
|
||||
const running = startPlayback(state);
|
||||
expect(running.status).toBe('running');
|
||||
expect(running.currentCaseIndex).toBe(0);
|
||||
it('startPlayback sets status to running and classifies via classifyItem', () => {
|
||||
const state = startPlayback(createPlaybackState());
|
||||
expect(state.status).toBe('running');
|
||||
expect(state.classification).not.toBeNull();
|
||||
expect(state.targetCategory).toBe(state.classification!.category);
|
||||
});
|
||||
|
||||
it('pausePlayback sets status to paused', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
let state = startPlayback(createPlaybackState());
|
||||
state = pausePlayback(state);
|
||||
expect(state.status).toBe('paused');
|
||||
});
|
||||
|
||||
it('resumePlayback sets status back to running', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
let state = startPlayback(createPlaybackState());
|
||||
state = pausePlayback(state);
|
||||
state = resumePlayback(state);
|
||||
expect(state.status).toBe('running');
|
||||
});
|
||||
|
||||
it('stopPlayback resets to idle', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
state = updatePlayback(state, 1000);
|
||||
let state = startPlayback(createPlaybackState());
|
||||
state = stopPlayback(state);
|
||||
expect(state.status).toBe('idle');
|
||||
expect(state.currentCaseIndex).toBe(0);
|
||||
expect(state.totalElapsedMs).toBe(0);
|
||||
});
|
||||
|
||||
it('advances from case 1 to case 2 after case duration', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
expect(state.currentCaseIndex).toBe(0);
|
||||
|
||||
// Simulate passing entire case duration by updating in steps
|
||||
// Each update processes one phase at a time
|
||||
it('advances to next case after case duration', () => {
|
||||
let state = startPlayback(createPlaybackState());
|
||||
let totalTime = 0;
|
||||
while (totalTime < CASE_DURATION_MS * 1.5 && state.currentCaseIndex === 0) {
|
||||
state = updatePlayback(state, 100);
|
||||
totalTime += 100;
|
||||
}
|
||||
expect(state.currentCaseIndex).toBe(1);
|
||||
expect(state.currentCaseIndex).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it('status becomes finished after case 8 (without loop)', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
state = { ...state, loopMode: false };
|
||||
|
||||
// Simulate passing all 8 cases by updating in steps
|
||||
let totalTime = 0;
|
||||
const maxTime = CASE_DURATION_MS * 10; // Safety limit
|
||||
while (state.status === 'running' && totalTime < maxTime) {
|
||||
it('seekToCase jumps to jam case and sets FAULT command later', () => {
|
||||
const jamIndex = DEMO_PLAYLIST.findIndex((c) => c.faultType === 'jam');
|
||||
let state = seekToCase(createPlaybackState(), jamIndex);
|
||||
expect(state.currentCase.faultType).toBe('jam');
|
||||
for (let i = 0; i < 80; i++) {
|
||||
state = updatePlayback(state, 100);
|
||||
totalTime += 100;
|
||||
if (state.command === 'FAULT') break;
|
||||
}
|
||||
expect(state.command).toBe('FAULT');
|
||||
});
|
||||
|
||||
it('setPlaybackSpeed changes multiplier', () => {
|
||||
let state = startPlayback(createPlaybackState());
|
||||
state = setPlaybackSpeed(state, 2);
|
||||
expect(state.speed).toBe(2);
|
||||
});
|
||||
|
||||
it('records classification events in journal', () => {
|
||||
let state = startPlayback(createPlaybackState());
|
||||
for (let i = 0; i < 60; i++) {
|
||||
state = updatePlayback(state, 100);
|
||||
}
|
||||
expect(state.events.some((e) => e.type === 'classification' || e.type === 'system')).toBe(true);
|
||||
});
|
||||
|
||||
it('completes playlist', () => {
|
||||
let state = startPlayback(createPlaybackState());
|
||||
const maxTime = CASE_DURATION_MS * 20;
|
||||
let elapsed = 0;
|
||||
while (state.status === 'running' && elapsed < maxTime) {
|
||||
state = updatePlayback(state, 200);
|
||||
elapsed += 200;
|
||||
}
|
||||
|
||||
expect(state.status).toBe('finished');
|
||||
});
|
||||
|
||||
it('loops back to case 1 after case 8 with loopMode', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
state = { ...state, loopMode: true };
|
||||
|
||||
// Simulate passing all 8 cases in steps
|
||||
let casesPassed = 0;
|
||||
let totalTime = 0;
|
||||
const maxTime = CASE_DURATION_MS * 10;
|
||||
|
||||
while (casesPassed < 8 && totalTime < maxTime) {
|
||||
const prevIndex = state.currentCaseIndex;
|
||||
state = updatePlayback(state, 100);
|
||||
totalTime += 100;
|
||||
if (state.currentCaseIndex !== prevIndex || (prevIndex === 7 && state.currentCaseIndex === 0)) {
|
||||
casesPassed++;
|
||||
}
|
||||
}
|
||||
|
||||
expect(state.status).toBe('running');
|
||||
expect(state.currentCaseIndex).toBe(0);
|
||||
});
|
||||
|
||||
it('updates command during routing phase', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
|
||||
// Advance to routing phase (roughly halfway through case)
|
||||
state = updatePlayback(state, CASE_DURATION_MS * 0.7);
|
||||
|
||||
// Command should be ROUTE_TO_* based on category
|
||||
expect(state.command).toMatch(/^ROUTE_TO_[BCD]$|^MOVING_TO_CAMERA$|^DETECTING$|^MEASURING$|^CLASSIFYING$|^RETURN_HOME$/);
|
||||
});
|
||||
|
||||
it('does not update when paused', () => {
|
||||
let state = createPlaybackState();
|
||||
state = startPlayback(state);
|
||||
state = updatePlayback(state, 1000);
|
||||
const elapsed1 = state.totalElapsedMs;
|
||||
|
||||
state = pausePlayback(state);
|
||||
state = updatePlayback(state, 1000);
|
||||
|
||||
expect(state.totalElapsedMs).toBe(elapsed1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
/**
|
||||
* Continuous Playback Engine — manages 8-case auto-demo on main page.
|
||||
* Controls playlist progression, timing, and phase transitions.
|
||||
* Continuous Playback Engine — manages auto-demo on main page.
|
||||
* Classification is always driven by classifyItem (live rule engine).
|
||||
* Supports speed control, case seek, seeded variability, and event journal.
|
||||
*/
|
||||
|
||||
import type { Category } from './types';
|
||||
import type { Category, ClassificationResult, EventLogEntry } from './types';
|
||||
import { DEMO_PLAYLIST, type PlaylistCase, PLAYLIST_LENGTH } from './demoPlaylist';
|
||||
import { classifyItem } from './classifier';
|
||||
import { resolveItem } from '../data/resolveItem';
|
||||
import { createSeededRng, DEFAULT_DEMO_SEED, seededOffset } from './seededRng';
|
||||
|
||||
/** Playback status for the continuous demo. */
|
||||
export type PlaybackStatus = 'idle' | 'running' | 'paused' | 'finished';
|
||||
|
||||
/** Phase within a single case timeline. */
|
||||
export type CasePhase =
|
||||
export type CasePhase =
|
||||
| 'spawn'
|
||||
| 'move_to_detection'
|
||||
| 'detection'
|
||||
@@ -19,7 +23,10 @@ export type CasePhase =
|
||||
| 'command_sent'
|
||||
| 'routing'
|
||||
| 'exit'
|
||||
| 'clear_gap';
|
||||
| 'clear_gap'
|
||||
| 'fault_hold'
|
||||
| 'emergency_hold'
|
||||
| 'recover';
|
||||
|
||||
/** Phase configuration with duration based on conveyor speed (1 m/s). */
|
||||
export interface PhaseConfig {
|
||||
@@ -29,25 +36,48 @@ export interface PhaseConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* Timeline phases for a single item.
|
||||
* Timeline phases for a single normal item.
|
||||
* Distances are in meters, speed is 1 m/s, so duration = distance * 1000 ms.
|
||||
*/
|
||||
export const CASE_PHASES: PhaseConfig[] = [
|
||||
{ phase: 'spawn', durationMs: 300, label: 'Spawn at A' },
|
||||
{ phase: 'move_to_detection', durationMs: 2500, label: 'Moving to camera' }, // 2.5m at 1m/s
|
||||
{ phase: 'move_to_detection', durationMs: 2500, label: 'Moving to camera' },
|
||||
{ phase: 'detection', durationMs: 600, label: 'CV Detection' },
|
||||
{ phase: 'measurement', durationMs: 1000, label: 'Laser measurement' }, // 1.0m at 1m/s
|
||||
{ phase: 'classification', durationMs: 1000, label: 'Classification' }, // 1.0m at 1m/s
|
||||
{ phase: 'command_sent', durationMs: 1000, label: 'Command sent' }, // 1.0m at 1m/s
|
||||
{ phase: 'routing', durationMs: 2500, label: 'Routing to zone' }, // 2.5m B-route at 1m/s
|
||||
{ phase: 'measurement', durationMs: 1000, label: 'Laser measurement' },
|
||||
{ phase: 'classification', durationMs: 1000, label: 'Classification' },
|
||||
{ phase: 'command_sent', durationMs: 1000, label: 'Command sent' },
|
||||
{ phase: 'routing', durationMs: 2500, label: 'Routing to zone' },
|
||||
{ phase: 'exit', durationMs: 400, label: 'Exit to zone' },
|
||||
{ phase: 'clear_gap', durationMs: 500, label: 'Clear gap' },
|
||||
];
|
||||
|
||||
/** Total duration of one case in ms. */
|
||||
/** Safety timeline: jam near gate then recover. */
|
||||
export const JAM_CASE_PHASES: PhaseConfig[] = [
|
||||
{ phase: 'spawn', durationMs: 300, label: 'Spawn at A' },
|
||||
{ phase: 'move_to_detection', durationMs: 2000, label: 'Moving to camera' },
|
||||
{ phase: 'detection', durationMs: 500, label: 'CV Detection' },
|
||||
{ phase: 'measurement', durationMs: 800, label: 'Laser measurement' },
|
||||
{ phase: 'classification', durationMs: 600, label: 'Classification' },
|
||||
{ phase: 'fault_hold', durationMs: 2800, label: 'JAM / FAULT' },
|
||||
{ phase: 'recover', durationMs: 1200, label: 'Recovery' },
|
||||
{ phase: 'clear_gap', durationMs: 400, label: 'Clear gap' },
|
||||
];
|
||||
|
||||
/** Safety timeline: emergency stop. */
|
||||
export const ESTOP_CASE_PHASES: PhaseConfig[] = [
|
||||
{ phase: 'spawn', durationMs: 300, label: 'Spawn at A' },
|
||||
{ phase: 'move_to_detection', durationMs: 1800, label: 'Moving to camera' },
|
||||
{ phase: 'detection', durationMs: 400, label: 'CV Detection' },
|
||||
{ phase: 'emergency_hold', durationMs: 3000, label: 'EMERGENCY STOP' },
|
||||
{ phase: 'recover', durationMs: 1500, label: 'System reset' },
|
||||
{ phase: 'clear_gap', durationMs: 400, label: 'Clear gap' },
|
||||
];
|
||||
|
||||
/** Total duration of one normal case in ms. */
|
||||
export const CASE_DURATION_MS = CASE_PHASES.reduce((sum, p) => sum + p.durationMs, 0);
|
||||
|
||||
/** Continuous playback state. */
|
||||
export type PlaybackSpeed = 0.5 | 1 | 1.5 | 2;
|
||||
|
||||
export interface ContinuousPlaybackState {
|
||||
status: PlaybackStatus;
|
||||
currentCaseIndex: number;
|
||||
@@ -59,13 +89,104 @@ export interface ContinuousPlaybackState {
|
||||
totalElapsedMs: number;
|
||||
loopMode: boolean;
|
||||
targetCategory: Category | null;
|
||||
classification: ClassificationResult | null;
|
||||
command: string;
|
||||
warning: string | null;
|
||||
/** Playback speed multiplier */
|
||||
speed: PlaybackSpeed;
|
||||
/** Seed for reproducible variability */
|
||||
seed: number;
|
||||
/** Deterministic position jitter (mm-scale visual offsets stored as meters) */
|
||||
positionJitter: { x: number; z: number; yaw: number };
|
||||
/** Bounded event journal for proof / engineering HUD */
|
||||
events: EventLogEntry[];
|
||||
}
|
||||
|
||||
function phasesForCase(playlistCase: PlaylistCase): PhaseConfig[] {
|
||||
if (playlistCase.faultType === 'jam') return JAM_CASE_PHASES;
|
||||
if (playlistCase.faultType === 'emergency_stop') return ESTOP_CASE_PHASES;
|
||||
return CASE_PHASES;
|
||||
}
|
||||
|
||||
export function getPlaylistCaseDurationMs(playlistCase: PlaylistCase): number {
|
||||
return phasesForCase(playlistCase).reduce((sum, p) => sum + p.durationMs, 0);
|
||||
}
|
||||
|
||||
/** Cumulative playlist duration before case index (supports wrap for loops). */
|
||||
export function cumulativePlaylistDurationMs(caseIndex: number): number {
|
||||
let sum = 0;
|
||||
for (let i = 0; i < caseIndex; i++) {
|
||||
sum += getPlaylistCaseDurationMs(DEMO_PLAYLIST[i % PLAYLIST_LENGTH]);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
function caseDurationMs(playlistCase: PlaylistCase): number {
|
||||
return getPlaylistCaseDurationMs(playlistCase);
|
||||
}
|
||||
|
||||
function classifyCase(playlistCase: PlaylistCase): ClassificationResult {
|
||||
const item = resolveItem(playlistCase.itemId);
|
||||
return classifyItem(item);
|
||||
}
|
||||
|
||||
function buildJitter(seed: number, caseIndex: number): { x: number; z: number; yaw: number } {
|
||||
const rng = createSeededRng(seed + caseIndex * 9973);
|
||||
return {
|
||||
x: seededOffset(rng, 0.012),
|
||||
z: seededOffset(rng, 0.008),
|
||||
yaw: seededOffset(rng, 0.04),
|
||||
};
|
||||
}
|
||||
|
||||
let eventCounter = 0;
|
||||
|
||||
function pushEvent(
|
||||
events: EventLogEntry[],
|
||||
timestampMs: number,
|
||||
entry: Omit<EventLogEntry, 'id' | 'timestampMs'>,
|
||||
): EventLogEntry[] {
|
||||
eventCounter += 1;
|
||||
const next: EventLogEntry = {
|
||||
id: `pb-evt-${eventCounter}`,
|
||||
timestampMs,
|
||||
...entry,
|
||||
};
|
||||
return [next, ...events].slice(0, 40);
|
||||
}
|
||||
|
||||
function initCaseFields(playlistCase: PlaylistCase, seed: number, caseIndex: number, events: EventLogEntry[], simTime: number) {
|
||||
const classification = classifyCase(playlistCase);
|
||||
const warnings = [
|
||||
...(playlistCase.warning ? [playlistCase.warning] : []),
|
||||
...classification.warnings,
|
||||
];
|
||||
return {
|
||||
currentCase: playlistCase,
|
||||
currentCaseIndex: caseIndex,
|
||||
currentPhaseIndex: 0,
|
||||
currentPhase: 'spawn' as CasePhase,
|
||||
phaseElapsedMs: 0,
|
||||
caseElapsedMs: 0,
|
||||
targetCategory: classification.category,
|
||||
classification,
|
||||
command: 'IDLE',
|
||||
warning: warnings[0] ?? null,
|
||||
positionJitter: buildJitter(seed, caseIndex),
|
||||
events: pushEvent(events, simTime, {
|
||||
itemId: playlistCase.itemId,
|
||||
type: 'system',
|
||||
message: `Case start: ${playlistCase.title}`,
|
||||
category: classification.category,
|
||||
status: 'info',
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/** Create initial playback state. */
|
||||
export function createPlaybackState(): ContinuousPlaybackState {
|
||||
export function createPlaybackState(seed: number = DEFAULT_DEMO_SEED): ContinuousPlaybackState {
|
||||
const firstCase = DEMO_PLAYLIST[0];
|
||||
const classification = classifyCase(firstCase);
|
||||
return {
|
||||
status: 'idle',
|
||||
currentCaseIndex: 0,
|
||||
@@ -77,100 +198,100 @@ export function createPlaybackState(): ContinuousPlaybackState {
|
||||
totalElapsedMs: 0,
|
||||
loopMode: false,
|
||||
targetCategory: null,
|
||||
classification: null,
|
||||
command: 'IDLE',
|
||||
warning: null,
|
||||
speed: 1,
|
||||
seed,
|
||||
positionJitter: { x: 0, z: 0, yaw: 0 },
|
||||
events: [],
|
||||
};
|
||||
}
|
||||
|
||||
/** Start playback from the beginning. */
|
||||
export function startPlayback(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
const firstCase = DEMO_PLAYLIST[0];
|
||||
const fields = initCaseFields(firstCase, state.seed, 0, [], 0);
|
||||
return {
|
||||
...state,
|
||||
status: 'running',
|
||||
currentCaseIndex: 0,
|
||||
currentCase: firstCase,
|
||||
currentPhaseIndex: 0,
|
||||
currentPhase: 'spawn',
|
||||
phaseElapsedMs: 0,
|
||||
caseElapsedMs: 0,
|
||||
totalElapsedMs: 0,
|
||||
targetCategory: firstCase.expectedCategory,
|
||||
command: 'IDLE',
|
||||
warning: firstCase.warning ?? null,
|
||||
speed: state.speed,
|
||||
seed: state.seed,
|
||||
loopMode: state.loopMode,
|
||||
...fields,
|
||||
};
|
||||
}
|
||||
|
||||
/** Pause playback. */
|
||||
export function pausePlayback(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
if (state.status !== 'running') return state;
|
||||
return { ...state, status: 'paused' };
|
||||
}
|
||||
|
||||
/** Resume playback. */
|
||||
export function resumePlayback(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
if (state.status !== 'paused') return state;
|
||||
return { ...state, status: 'running' };
|
||||
}
|
||||
|
||||
/** Stop and reset playback. */
|
||||
export function stopPlayback(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
return createPlaybackState();
|
||||
return createPlaybackState(state.seed);
|
||||
}
|
||||
|
||||
/** Toggle loop mode. */
|
||||
export function toggleLoopMode(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
return { ...state, loopMode: !state.loopMode };
|
||||
}
|
||||
|
||||
/** Advance to the next case. */
|
||||
function advanceToNextCase(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
const nextIndex = state.currentCaseIndex + 1;
|
||||
|
||||
// Check if we've completed all cases
|
||||
if (nextIndex >= PLAYLIST_LENGTH) {
|
||||
if (state.loopMode) {
|
||||
// Loop back to first case
|
||||
const firstCase = DEMO_PLAYLIST[0];
|
||||
return {
|
||||
...state,
|
||||
currentCaseIndex: 0,
|
||||
currentCase: firstCase,
|
||||
currentPhaseIndex: 0,
|
||||
currentPhase: 'spawn',
|
||||
phaseElapsedMs: 0,
|
||||
caseElapsedMs: 0,
|
||||
targetCategory: firstCase.expectedCategory,
|
||||
command: 'IDLE',
|
||||
warning: firstCase.warning ?? null,
|
||||
};
|
||||
} else {
|
||||
// Finished all cases
|
||||
return {
|
||||
...state,
|
||||
status: 'finished',
|
||||
command: 'COMPLETE',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Advance to next case
|
||||
const nextCase = DEMO_PLAYLIST[nextIndex];
|
||||
export function setPlaybackSpeed(state: ContinuousPlaybackState, speed: PlaybackSpeed): ContinuousPlaybackState {
|
||||
return { ...state, speed };
|
||||
}
|
||||
|
||||
/** Jump to a playlist case index (keeps running/paused status). */
|
||||
export function seekToCase(state: ContinuousPlaybackState, caseIndex: number): ContinuousPlaybackState {
|
||||
const idx = ((caseIndex % PLAYLIST_LENGTH) + PLAYLIST_LENGTH) % PLAYLIST_LENGTH;
|
||||
const nextCase = DEMO_PLAYLIST[idx];
|
||||
const fields = initCaseFields(nextCase, state.seed, idx, state.events, state.totalElapsedMs);
|
||||
const status = state.status === 'idle' || state.status === 'finished' ? 'running' : state.status;
|
||||
return {
|
||||
...state,
|
||||
currentCaseIndex: nextIndex,
|
||||
currentCase: nextCase,
|
||||
currentPhaseIndex: 0,
|
||||
currentPhase: 'spawn',
|
||||
phaseElapsedMs: 0,
|
||||
caseElapsedMs: 0,
|
||||
targetCategory: nextCase.expectedCategory,
|
||||
command: 'IDLE',
|
||||
warning: nextCase.warning ?? null,
|
||||
status,
|
||||
...fields,
|
||||
};
|
||||
}
|
||||
|
||||
/** Get command for current phase. */
|
||||
export function seekNextCase(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
return seekToCase(state, state.currentCaseIndex + 1);
|
||||
}
|
||||
|
||||
export function seekPrevCase(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
return seekToCase(state, state.currentCaseIndex - 1);
|
||||
}
|
||||
|
||||
function advanceToNextCase(state: ContinuousPlaybackState): ContinuousPlaybackState {
|
||||
const nextIndex = state.currentCaseIndex + 1;
|
||||
|
||||
if (nextIndex >= PLAYLIST_LENGTH) {
|
||||
if (state.loopMode) {
|
||||
const firstCase = DEMO_PLAYLIST[0];
|
||||
const fields = initCaseFields(firstCase, state.seed, 0, state.events, state.totalElapsedMs);
|
||||
return { ...state, ...fields };
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
status: 'finished',
|
||||
command: 'COMPLETE',
|
||||
events: pushEvent(state.events, state.totalElapsedMs, {
|
||||
type: 'system',
|
||||
message: 'Playlist complete',
|
||||
status: 'success',
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
const nextCase = DEMO_PLAYLIST[nextIndex];
|
||||
const fields = initCaseFields(nextCase, state.seed, nextIndex, state.events, state.totalElapsedMs);
|
||||
return { ...state, ...fields };
|
||||
}
|
||||
|
||||
function getCommandForPhase(phase: CasePhase, category: Category): string {
|
||||
switch (phase) {
|
||||
case 'spawn':
|
||||
@@ -186,6 +307,12 @@ function getCommandForPhase(phase: CasePhase, category: Category): string {
|
||||
case 'routing':
|
||||
case 'exit':
|
||||
return `ROUTE_TO_${category}`;
|
||||
case 'fault_hold':
|
||||
return 'FAULT';
|
||||
case 'emergency_hold':
|
||||
return 'EMERGENCY_STOP';
|
||||
case 'recover':
|
||||
return 'RECOVERING';
|
||||
case 'clear_gap':
|
||||
return 'RETURN_HOME';
|
||||
default:
|
||||
@@ -193,7 +320,62 @@ function getCommandForPhase(phase: CasePhase, category: Category): string {
|
||||
}
|
||||
}
|
||||
|
||||
/** Update playback state with elapsed time. */
|
||||
function maybeLogPhaseTransition(
|
||||
state: ContinuousPlaybackState,
|
||||
phase: CasePhase,
|
||||
category: Category,
|
||||
): EventLogEntry[] {
|
||||
let events = state.events;
|
||||
if (phase === 'classification' && state.classification) {
|
||||
events = pushEvent(events, state.totalElapsedMs, {
|
||||
itemId: state.currentCase.itemId,
|
||||
type: 'classification',
|
||||
message: `${state.classification.label}: ${state.classification.reason}`,
|
||||
category: state.classification.category,
|
||||
command: 'CLASSIFY_RULE_BASED',
|
||||
status: state.classification.warnings.length ? 'warning' : 'success',
|
||||
});
|
||||
}
|
||||
if (phase === 'command_sent') {
|
||||
events = pushEvent(events, state.totalElapsedMs, {
|
||||
itemId: state.currentCase.itemId,
|
||||
type: 'routing',
|
||||
message: `Command ROUTE_TO_${category}`,
|
||||
category,
|
||||
command: `ROUTE_TO_${category}`,
|
||||
status: 'success',
|
||||
});
|
||||
}
|
||||
if (phase === 'fault_hold') {
|
||||
events = pushEvent(events, state.totalElapsedMs, {
|
||||
itemId: state.currentCase.itemId,
|
||||
type: 'fault',
|
||||
message: 'Jam detected at stop-gate — conveyor halted',
|
||||
command: 'FAULT',
|
||||
status: 'error',
|
||||
});
|
||||
}
|
||||
if (phase === 'emergency_hold') {
|
||||
events = pushEvent(events, state.totalElapsedMs, {
|
||||
itemId: state.currentCase.itemId,
|
||||
type: 'fault',
|
||||
message: 'Emergency stop engaged — all motion frozen',
|
||||
command: 'EMERGENCY_STOP',
|
||||
status: 'error',
|
||||
});
|
||||
}
|
||||
if (phase === 'recover') {
|
||||
events = pushEvent(events, state.totalElapsedMs, {
|
||||
type: 'system',
|
||||
message: 'Recovery sequence started',
|
||||
command: 'RECOVER',
|
||||
status: 'warning',
|
||||
});
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
/** Update playback state with elapsed wall-clock time (scaled by speed). */
|
||||
export function updatePlayback(
|
||||
state: ContinuousPlaybackState,
|
||||
deltaMs: number,
|
||||
@@ -202,29 +384,30 @@ export function updatePlayback(
|
||||
return state;
|
||||
}
|
||||
|
||||
const scaledDelta = deltaMs * state.speed;
|
||||
let newState = { ...state };
|
||||
newState.phaseElapsedMs += deltaMs;
|
||||
newState.caseElapsedMs += deltaMs;
|
||||
newState.totalElapsedMs += deltaMs;
|
||||
newState.phaseElapsedMs += scaledDelta;
|
||||
newState.caseElapsedMs += scaledDelta;
|
||||
newState.totalElapsedMs += scaledDelta;
|
||||
|
||||
const phases = phasesForCase(newState.currentCase);
|
||||
const currentPhaseConfig = phases[newState.currentPhaseIndex];
|
||||
|
||||
// Check if current phase is complete
|
||||
const currentPhaseConfig = CASE_PHASES[newState.currentPhaseIndex];
|
||||
if (newState.phaseElapsedMs >= currentPhaseConfig.durationMs) {
|
||||
// Advance to next phase
|
||||
const nextPhaseIndex = newState.currentPhaseIndex + 1;
|
||||
|
||||
if (nextPhaseIndex >= CASE_PHASES.length) {
|
||||
// Case complete, advance to next case
|
||||
|
||||
if (nextPhaseIndex >= phases.length) {
|
||||
newState = advanceToNextCase(newState);
|
||||
} else {
|
||||
// Move to next phase
|
||||
newState.currentPhaseIndex = nextPhaseIndex;
|
||||
newState.currentPhase = CASE_PHASES[nextPhaseIndex].phase;
|
||||
newState.currentPhase = phases[nextPhaseIndex].phase;
|
||||
newState.phaseElapsedMs = 0;
|
||||
if (newState.targetCategory) {
|
||||
newState.events = maybeLogPhaseTransition(newState, newState.currentPhase, newState.targetCategory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update command based on phase
|
||||
if (newState.status === 'running' && newState.targetCategory) {
|
||||
newState.command = getCommandForPhase(newState.currentPhase, newState.targetCategory);
|
||||
}
|
||||
@@ -232,35 +415,55 @@ export function updatePlayback(
|
||||
return newState;
|
||||
}
|
||||
|
||||
/** Get progress within current case (0 to 1). */
|
||||
export function getCaseProgress(state: ContinuousPlaybackState): number {
|
||||
return Math.min(state.caseElapsedMs / CASE_DURATION_MS, 1);
|
||||
export function getCasePhases(state: ContinuousPlaybackState): PhaseConfig[] {
|
||||
return phasesForCase(state.currentCase);
|
||||
}
|
||||
|
||||
export function getCaseDurationMs(state: ContinuousPlaybackState): number {
|
||||
return caseDurationMs(state.currentCase);
|
||||
}
|
||||
|
||||
export function getCaseProgress(state: ContinuousPlaybackState): number {
|
||||
return Math.min(state.caseElapsedMs / getCaseDurationMs(state), 1);
|
||||
}
|
||||
|
||||
/** Get progress within current phase (0 to 1). */
|
||||
export function getPhaseProgress(state: ContinuousPlaybackState): number {
|
||||
const phaseConfig = CASE_PHASES[state.currentPhaseIndex];
|
||||
const phases = getCasePhases(state);
|
||||
const phaseConfig = phases[state.currentPhaseIndex];
|
||||
return Math.min(state.phaseElapsedMs / phaseConfig.durationMs, 1);
|
||||
}
|
||||
|
||||
/** Get current phase configuration. */
|
||||
export function getCurrentPhaseConfig(state: ContinuousPlaybackState): PhaseConfig {
|
||||
return CASE_PHASES[state.currentPhaseIndex];
|
||||
return getCasePhases(state)[state.currentPhaseIndex];
|
||||
}
|
||||
|
||||
/** Check if detection is active. */
|
||||
export function isDetectionActive(state: ContinuousPlaybackState): boolean {
|
||||
return state.currentPhase === 'detection' || state.currentPhase === 'measurement';
|
||||
}
|
||||
|
||||
/** Check if routing is active. */
|
||||
export function isRoutingActive(state: ContinuousPlaybackState): boolean {
|
||||
return state.currentPhase === 'routing' || state.currentPhase === 'exit';
|
||||
}
|
||||
|
||||
/** Get total playlist progress (0 to 1). */
|
||||
export function isFaultActive(state: ContinuousPlaybackState): boolean {
|
||||
return state.currentPhase === 'fault_hold' || state.currentPhase === 'emergency_hold';
|
||||
}
|
||||
|
||||
export function getTotalProgress(state: ContinuousPlaybackState): number {
|
||||
const completedCases = state.currentCaseIndex;
|
||||
const currentCaseProgress = getCaseProgress(state);
|
||||
return (completedCases + currentCaseProgress) / PLAYLIST_LENGTH;
|
||||
}
|
||||
|
||||
/** Assert playlist expectedCategory matches live classifier (for tests). */
|
||||
export function assertPlaylistClassifierConsistency(): Array<{ id: string; expected: Category; actual: Category }> {
|
||||
const mismatches: Array<{ id: string; expected: Category; actual: Category }> = [];
|
||||
for (const c of DEMO_PLAYLIST) {
|
||||
if (c.faultType) continue;
|
||||
const result = classifyCase(c);
|
||||
if (result.category !== c.expectedCategory) {
|
||||
mismatches.push({ id: c.id, expected: c.expectedCategory, actual: result.category });
|
||||
}
|
||||
}
|
||||
return mismatches;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
/**
|
||||
* Demo Playlist — 8 showcase cases for the main page auto-demo.
|
||||
* Each case demonstrates a specific classification scenario.
|
||||
* Demo Playlist — showcase cases for the main page auto-demo.
|
||||
* Includes classification cases + jam / emergency safety demos.
|
||||
*/
|
||||
|
||||
import type { Category, ScenarioId } from './types';
|
||||
|
||||
export type FaultType = 'jam' | 'emergency_stop';
|
||||
|
||||
export interface PlaylistCase {
|
||||
id: string;
|
||||
title: string;
|
||||
@@ -13,11 +15,13 @@ export interface PlaylistCase {
|
||||
scenarioId: ScenarioId;
|
||||
expectedCategory: Category;
|
||||
warning?: string;
|
||||
/** Optional safety fault sequence (no routing). */
|
||||
faultType?: FaultType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 8 showcase cases for continuous demo playback.
|
||||
* Order is designed for storytelling: B → C → D → edge cases.
|
||||
* Showcase playlist: B → C → D → edge cases → safety.
|
||||
* Order is designed for storytelling for jury / investors.
|
||||
*/
|
||||
export const DEMO_PLAYLIST: PlaylistCase[] = [
|
||||
{
|
||||
@@ -85,6 +89,26 @@ export const DEMO_PLAYLIST: PlaylistCase[] = [
|
||||
expectedCategory: 'B',
|
||||
warning: 'Low confidence is a warning, not a 4th category. Rules still apply.',
|
||||
},
|
||||
{
|
||||
id: 'jam_fault',
|
||||
title: 'Заклинивание у gate',
|
||||
description: 'Jam → FAULT, конвейер остановлен, затем recovery',
|
||||
itemId: 'SKU-004',
|
||||
scenarioId: 'jam',
|
||||
expectedCategory: 'C',
|
||||
faultType: 'jam',
|
||||
warning: 'FAULT: item jammed at stop-gate',
|
||||
},
|
||||
{
|
||||
id: 'emergency_stop',
|
||||
title: 'Аварийная остановка',
|
||||
description: 'E-STOP — все приводы заморожены, затем reset',
|
||||
itemId: 'SKU-001',
|
||||
scenarioId: 'emergency_stop',
|
||||
expectedCategory: 'B',
|
||||
faultType: 'emergency_stop',
|
||||
warning: 'EMERGENCY STOP engaged',
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,6 +40,9 @@ const PHASE_LABELS: Record<CasePhase, string> = {
|
||||
routing: 'Routing to zone',
|
||||
exit: 'Exiting',
|
||||
clear_gap: 'Clear',
|
||||
fault_hold: 'FAULT / Jam',
|
||||
emergency_hold: 'EMERGENCY STOP',
|
||||
recover: 'Recovering',
|
||||
};
|
||||
|
||||
const VISIBLE_PHASES: CasePhase[] = [
|
||||
@@ -48,6 +51,8 @@ const VISIBLE_PHASES: CasePhase[] = [
|
||||
'classification',
|
||||
'command_sent',
|
||||
'routing',
|
||||
'fault_hold',
|
||||
'emergency_hold',
|
||||
];
|
||||
|
||||
function normalizeShape(shape: string, roundness: number): DetectedShape {
|
||||
|
||||
@@ -1,28 +1,24 @@
|
||||
/**
|
||||
* Measurement System — engineering logic for item measurement.
|
||||
*
|
||||
*
|
||||
* Measurement architecture:
|
||||
* 1. Stepper motor: measures length along conveyor axis (step counting)
|
||||
* 2. Laser rangefinder: measures height from above
|
||||
* 3. Stereo camera: measures width and determines shape/roundness
|
||||
* 4. PLC controller: synchronizes and makes classification decision
|
||||
* 4. PLC controller: synchronizes and makes classification decision via classifyItem
|
||||
*/
|
||||
|
||||
import type { ContinuousPlaybackState, CasePhase } from './continuousPlayback';
|
||||
import type { Category } from './types';
|
||||
import { ITEMS } from '../data/items';
|
||||
import { classifyItem, DIMENSION_LIMITS } from './classifier';
|
||||
import { resolveItem } from '../data/resolveItem';
|
||||
import {
|
||||
MM_PER_STEP,
|
||||
LASER_HEIGHT_M,
|
||||
BELT_TOP_Y,
|
||||
CONVEYOR_SPEED_MPS,
|
||||
} from './physicalLayout';
|
||||
|
||||
// =========================================================
|
||||
// Types
|
||||
// =========================================================
|
||||
|
||||
export type MeasurementStage =
|
||||
export type MeasurementStage =
|
||||
| 'idle'
|
||||
| 'leading_edge_detected'
|
||||
| 'step_counting'
|
||||
@@ -32,28 +28,19 @@ export type MeasurementStage =
|
||||
| 'command_sent';
|
||||
|
||||
export interface MeasurementData {
|
||||
/** Current measurement stage */
|
||||
stage: MeasurementStage;
|
||||
|
||||
/** Stepper motor data */
|
||||
stepCount: number;
|
||||
mmPerStep: number;
|
||||
measuredLengthMm: number;
|
||||
pulseActive: boolean;
|
||||
|
||||
/** Laser rangefinder data */
|
||||
laserDistanceMm: number;
|
||||
laserMountHeightMm: number;
|
||||
measuredHeightMm: number;
|
||||
laserBeamActive: boolean;
|
||||
|
||||
/** Stereo camera data */
|
||||
measuredWidthMm: number;
|
||||
roundnessK: number;
|
||||
stereoActive: boolean;
|
||||
pointCloudActive: boolean;
|
||||
|
||||
/** Classification */
|
||||
confidence: number;
|
||||
dimensionsPass: boolean;
|
||||
shapeResult: 'box' | 'round' | 'irregular';
|
||||
@@ -61,24 +48,13 @@ export interface MeasurementData {
|
||||
command: string;
|
||||
cPriorityApplied: boolean;
|
||||
isLowConfidence: boolean;
|
||||
|
||||
/** Item info */
|
||||
/** Live classifier reason (proof of algorithm). */
|
||||
classificationReason: string | null;
|
||||
classificationLabel: string | null;
|
||||
itemTitle: string;
|
||||
itemDimensions: { width: number; depth: number; height: number };
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// Dimension limits (OZON Track 3 spec)
|
||||
// =========================================================
|
||||
const MAX_DIMENSION_MM = {
|
||||
width: 450,
|
||||
depth: 320,
|
||||
height: 320,
|
||||
};
|
||||
|
||||
// =========================================================
|
||||
// Stage mapping from playback phase
|
||||
// =========================================================
|
||||
function getStageFromPhase(phase: CasePhase): MeasurementStage {
|
||||
switch (phase) {
|
||||
case 'spawn':
|
||||
@@ -93,6 +69,9 @@ function getStageFromPhase(phase: CasePhase): MeasurementStage {
|
||||
case 'command_sent':
|
||||
case 'routing':
|
||||
case 'exit':
|
||||
case 'fault_hold':
|
||||
case 'emergency_hold':
|
||||
case 'recover':
|
||||
return 'command_sent';
|
||||
case 'clear_gap':
|
||||
return 'idle';
|
||||
@@ -101,99 +80,72 @@ function getStageFromPhase(phase: CasePhase): MeasurementStage {
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// Check dimension compliance
|
||||
// =========================================================
|
||||
function checkDimensionsPass(dims: { width: number; depth: number; height: number }): boolean {
|
||||
return (
|
||||
dims.width <= MAX_DIMENSION_MM.width &&
|
||||
dims.depth <= MAX_DIMENSION_MM.depth &&
|
||||
dims.height <= MAX_DIMENSION_MM.height
|
||||
);
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// Determine shape from roundness
|
||||
// =========================================================
|
||||
function getShapeResult(roundnessK: number): 'box' | 'round' | 'irregular' {
|
||||
if (roundnessK >= 0.7) return 'round';
|
||||
if (roundnessK >= DIMENSION_LIMITS.roundnessThreshold) return 'round';
|
||||
if (roundnessK >= 0.3) return 'box';
|
||||
return 'irregular';
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// Main measurement data getter
|
||||
// =========================================================
|
||||
export function getMeasurementData(playback: ContinuousPlaybackState): MeasurementData {
|
||||
const currentCase = playback.currentCase;
|
||||
const phase = playback.currentPhase;
|
||||
const stage = getStageFromPhase(phase);
|
||||
|
||||
// Get item data
|
||||
const itemId = currentCase.itemId.replace('-LC', '');
|
||||
const itemData = ITEMS.find(i => i.id === itemId) ?? ITEMS[0];
|
||||
|
||||
const itemData = resolveItem(currentCase.itemId);
|
||||
const dims = itemData.dimensionsMm;
|
||||
|
||||
// Calculate step count from item length (depth = length along conveyor)
|
||||
|
||||
const lengthMm = dims.depth;
|
||||
const stepCount = Math.round(lengthMm / MM_PER_STEP);
|
||||
|
||||
// Laser measurement: distance from laser to top of item
|
||||
|
||||
const laserMountHeightMm = LASER_HEIGHT_M * 1000;
|
||||
const itemTopHeightMm = BELT_TOP_Y * 1000 + dims.height;
|
||||
const laserDistanceMm = laserMountHeightMm - itemTopHeightMm;
|
||||
|
||||
// Derived measurements
|
||||
|
||||
const measuredLengthMm = stepCount * MM_PER_STEP;
|
||||
const measuredHeightMm = laserMountHeightMm - laserDistanceMm;
|
||||
const measuredWidthMm = dims.width;
|
||||
const roundnessK = itemData.roundness;
|
||||
|
||||
// Classification
|
||||
const dimensionsPass = checkDimensionsPass(dims);
|
||||
|
||||
const classification = classifyItem(itemData);
|
||||
const dimensionsPass = classification.dimensionsPass;
|
||||
const shapeResult = getShapeResult(roundnessK);
|
||||
const confidence = itemData.confidence ?? 0.95;
|
||||
const isLowConfidence = confidence < 0.8;
|
||||
|
||||
// Determine category
|
||||
const isLowConfidence = confidence < 0.65;
|
||||
|
||||
const deciding =
|
||||
phase !== 'spawn' &&
|
||||
phase !== 'move_to_detection' &&
|
||||
phase !== 'clear_gap';
|
||||
|
||||
let finalCategory: Category | null = null;
|
||||
let cPriorityApplied = false;
|
||||
|
||||
if (phase !== 'spawn' && phase !== 'move_to_detection') {
|
||||
if (!dimensionsPass) {
|
||||
// Dimensions fail → C (overrides roundness)
|
||||
finalCategory = 'C';
|
||||
if (roundnessK >= 0.7) {
|
||||
cPriorityApplied = true;
|
||||
}
|
||||
} else if (roundnessK >= 0.7) {
|
||||
// Round shape → D
|
||||
finalCategory = 'D';
|
||||
} else {
|
||||
// Normal → B
|
||||
finalCategory = 'B';
|
||||
}
|
||||
let classificationReason: string | null = null;
|
||||
let classificationLabel: string | null = null;
|
||||
|
||||
if (deciding) {
|
||||
// Prefer live result already computed on playback; fall back to classifyItem.
|
||||
finalCategory = playback.classification?.category ?? classification.category;
|
||||
classificationReason = playback.classification?.reason ?? classification.reason;
|
||||
classificationLabel = playback.classification?.label ?? classification.label;
|
||||
cPriorityApplied =
|
||||
finalCategory === 'C' && roundnessK >= DIMENSION_LIMITS.roundnessThreshold;
|
||||
}
|
||||
|
||||
// Override with expected category if available (for consistent demo)
|
||||
if (currentCase.expectedCategory && phase !== 'spawn' && phase !== 'move_to_detection') {
|
||||
finalCategory = currentCase.expectedCategory;
|
||||
// Check if C-priority case
|
||||
if (finalCategory === 'C' && roundnessK >= 0.7) {
|
||||
cPriorityApplied = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Command
|
||||
const command = finalCategory ? `ROUTE_TO_${finalCategory}` : 'STANDBY';
|
||||
|
||||
// Active states for visualization
|
||||
|
||||
const command =
|
||||
phase === 'fault_hold'
|
||||
? 'FAULT'
|
||||
: phase === 'emergency_hold'
|
||||
? 'EMERGENCY_STOP'
|
||||
: finalCategory
|
||||
? `ROUTE_TO_${finalCategory}`
|
||||
: 'STANDBY';
|
||||
|
||||
const isActivePhase = ['detection', 'measurement', 'classification'].includes(phase);
|
||||
const pulseActive = stage === 'step_counting' || stage === 'leading_edge_detected';
|
||||
const laserBeamActive = isActivePhase;
|
||||
const stereoActive = isActivePhase;
|
||||
const pointCloudActive = phase === 'measurement' || phase === 'classification';
|
||||
|
||||
|
||||
return {
|
||||
stage,
|
||||
stepCount,
|
||||
@@ -215,32 +167,44 @@ export function getMeasurementData(playback: ContinuousPlaybackState): Measureme
|
||||
command,
|
||||
cPriorityApplied,
|
||||
isLowConfidence,
|
||||
classificationReason,
|
||||
classificationLabel,
|
||||
itemTitle: currentCase.title,
|
||||
itemDimensions: dims,
|
||||
};
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// Stage labels for UI
|
||||
// =========================================================
|
||||
export function getStageLabel(stage: MeasurementStage): string {
|
||||
switch (stage) {
|
||||
case 'idle': return 'Standby';
|
||||
case 'leading_edge_detected': return 'Edge Detected';
|
||||
case 'step_counting': return 'Counting Steps';
|
||||
case 'laser_height': return 'Laser Height';
|
||||
case 'stereo_width_shape': return 'Stereo Analysis';
|
||||
case 'decision_ready': return 'Decision Ready';
|
||||
case 'command_sent': return 'Command Sent';
|
||||
default: return 'Unknown';
|
||||
case 'idle':
|
||||
return 'Standby';
|
||||
case 'leading_edge_detected':
|
||||
return 'Edge Detected';
|
||||
case 'step_counting':
|
||||
return 'Counting Steps';
|
||||
case 'laser_height':
|
||||
return 'Laser Height';
|
||||
case 'stereo_width_shape':
|
||||
return 'Stereo Analysis';
|
||||
case 'decision_ready':
|
||||
return 'Decision Ready';
|
||||
case 'command_sent':
|
||||
return 'Command Sent';
|
||||
default:
|
||||
return 'Unknown';
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================
|
||||
// Check if measurement visualization should be shown
|
||||
// =========================================================
|
||||
export function shouldShowMeasurement(phase: CasePhase): boolean {
|
||||
return ['detection', 'measurement', 'classification', 'command_sent', 'routing'].includes(phase);
|
||||
return [
|
||||
'detection',
|
||||
'measurement',
|
||||
'classification',
|
||||
'command_sent',
|
||||
'routing',
|
||||
'fault_hold',
|
||||
'emergency_hold',
|
||||
].includes(phase);
|
||||
}
|
||||
|
||||
export function shouldShowLaserBeam(phase: CasePhase): boolean {
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
*/
|
||||
|
||||
import type { Category, DimensionsMm } from './types';
|
||||
import { CASE_PHASES } from './continuousPlayback';
|
||||
import type { FaultType } from './demoPlaylist';
|
||||
import { CASE_PHASES, JAM_CASE_PHASES, ESTOP_CASE_PHASES } from './continuousPlayback';
|
||||
import {
|
||||
SURFACES,
|
||||
lerp3,
|
||||
@@ -17,7 +18,7 @@ import {
|
||||
} from './conveyorNetwork';
|
||||
|
||||
export type SurfaceType = SurfaceName;
|
||||
export type MotionPhase = 'feed' | 'inspection' | 'decision' | 'routing' | 'settled';
|
||||
export type MotionPhase = 'feed' | 'inspection' | 'decision' | 'routing' | 'settled' | 'fault' | 'recover';
|
||||
|
||||
export interface PhysicalItemPose {
|
||||
position: Vec3;
|
||||
@@ -34,13 +35,16 @@ export interface PoseInput {
|
||||
dimensionsMm: DimensionsMm;
|
||||
targetCategory: Category | null;
|
||||
elapsedMs: number;
|
||||
faultType?: FaultType;
|
||||
/** Optional seeded visual jitter (meters / radians). */
|
||||
jitter?: { x: number; z: number; yaw: number };
|
||||
}
|
||||
|
||||
/** Cumulative phase start times reconstructed once from CASE_PHASES. */
|
||||
function phaseStarts() {
|
||||
/** Cumulative phase start times from a phase table. */
|
||||
function phaseStartsFrom(phases: typeof CASE_PHASES) {
|
||||
let cumulative = 0;
|
||||
const starts: Record<string, number> = {};
|
||||
for (const p of CASE_PHASES) {
|
||||
for (const p of phases) {
|
||||
starts[p.phase] = cumulative;
|
||||
cumulative += p.durationMs;
|
||||
}
|
||||
@@ -51,7 +55,6 @@ function phaseStarts() {
|
||||
function poseOnSurface(name: SurfaceName, t: number, itemHeightM: number): { pos: Vec3; rotY: number } {
|
||||
const s = SURFACES[name];
|
||||
const p = lerp3(s.start, s.end, t);
|
||||
// Item bottom sits on the surface: center = surfaceTop + half height.
|
||||
return {
|
||||
pos: [p[0], p[1] + itemHeightM / 2, p[2]],
|
||||
rotY: surfaceHeading(name),
|
||||
@@ -69,9 +72,9 @@ function settledSlot(name: SurfaceName, slotIndex: number, itemHeightM: number):
|
||||
const cz = (b.minZ + b.maxZ) / 2;
|
||||
const spanX = b.maxX - b.minX;
|
||||
const spanZ = b.maxZ - b.minZ;
|
||||
const col = slotIndex % 3; // 0,1,2
|
||||
const row = Math.floor(slotIndex / 3) % 2; // 0,1
|
||||
const offsetX = (col - 1) * (spanX / 3.5); // stays well within bounds
|
||||
const col = slotIndex % 3;
|
||||
const row = Math.floor(slotIndex / 3) % 2;
|
||||
const offsetX = (col - 1) * (spanX / 3.5);
|
||||
const offsetZ = (row - 0.5) * (spanZ / 3.0);
|
||||
return {
|
||||
pos: [cx + offsetX, s.surfaceY + itemHeightM / 2, cz + offsetZ],
|
||||
@@ -79,12 +82,85 @@ function settledSlot(name: SurfaceName, slotIndex: number, itemHeightM: number):
|
||||
};
|
||||
}
|
||||
|
||||
function applyJitter(pos: Vec3, rotY: number, jitter?: PoseInput['jitter']): { pos: Vec3; rotY: number } {
|
||||
if (!jitter) return { pos, rotY };
|
||||
return {
|
||||
pos: [pos[0] + jitter.x, pos[1], pos[2] + jitter.z],
|
||||
rotY: rotY + jitter.yaw,
|
||||
};
|
||||
}
|
||||
|
||||
/** Jam / E-stop motion: freeze at junction, then recover (no settle into bin). */
|
||||
function getFaultPose(input: PoseInput): PhysicalItemPose {
|
||||
const { dimensionsMm, elapsedMs, faultType, jitter } = input;
|
||||
const itemHeightM = dimensionsMm.height / 1000;
|
||||
const phases = faultType === 'emergency_stop' ? ESTOP_CASE_PHASES : JAM_CASE_PHASES;
|
||||
const { starts, total } = phaseStartsFrom(phases);
|
||||
|
||||
const feedStart = starts['move_to_detection'] ?? 0;
|
||||
const inspectStart = starts['detection'] ?? feedStart;
|
||||
const measureStart = starts['measurement'] ?? inspectStart;
|
||||
const classifyStart = starts['classification'] ?? measureStart;
|
||||
const faultStart =
|
||||
starts['fault_hold'] ?? starts['emergency_hold'] ?? classifyStart;
|
||||
const recoverStart = starts['recover'] ?? faultStart;
|
||||
const clearStart = starts['clear_gap'] ?? recoverStart;
|
||||
|
||||
let pos: Vec3;
|
||||
let rotY = 0;
|
||||
let surface: SurfaceType = 'main_belt';
|
||||
let phase: MotionPhase = 'feed';
|
||||
|
||||
if (elapsedMs <= feedStart) {
|
||||
const r = poseOnSurface('main_belt', 0, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'main_belt'; phase = 'feed';
|
||||
} else if (elapsedMs <= inspectStart) {
|
||||
const t = (elapsedMs - feedStart) / Math.max(inspectStart - feedStart, 1);
|
||||
const r = poseOnSurface('main_belt', t, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'main_belt'; phase = 'feed';
|
||||
} else if (elapsedMs <= faultStart) {
|
||||
// Hold under inspection / approach gate
|
||||
const r = poseOnSurface('inspection_station', 0.4, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'inspection_station'; phase = 'inspection';
|
||||
} else if (elapsedMs <= recoverStart) {
|
||||
// Freeze at routing junction (jam / e-stop visual)
|
||||
const r = poseOnSurface('routing_junction', 0.35, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'routing_junction'; phase = 'fault';
|
||||
} else if (elapsedMs <= clearStart) {
|
||||
// Slight reverse toward belt for recovery narrative
|
||||
const t = (elapsedMs - recoverStart) / Math.max(clearStart - recoverStart, 1);
|
||||
const r = poseOnSurface('routing_junction', 0.35 * (1 - t * 0.5), itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'routing_junction'; phase = 'recover';
|
||||
} else {
|
||||
const r = poseOnSurface('main_belt', 0.15, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'main_belt'; phase = 'recover';
|
||||
}
|
||||
|
||||
const j = applyJitter(pos, rotY, jitter);
|
||||
if (!Number.isFinite(j.pos[0]) || !Number.isFinite(j.pos[1]) || !Number.isFinite(j.pos[2])) {
|
||||
j.pos = [SURFACES.main_belt.start[0], SURFACES.main_belt.surfaceY + itemHeightM / 2, 0];
|
||||
}
|
||||
|
||||
return {
|
||||
position: j.pos,
|
||||
rotation: [0, j.rotY, 0],
|
||||
surface,
|
||||
phase,
|
||||
isSettled: elapsedMs >= total && false,
|
||||
activeRoute: (input.targetCategory as 'B' | 'C' | 'D') || 'B',
|
||||
};
|
||||
}
|
||||
|
||||
export function getPhysicalItemPose(input: PoseInput): PhysicalItemPose {
|
||||
const { dimensionsMm, targetCategory, elapsedMs, slotIndex = 0 } = input;
|
||||
if (input.faultType) {
|
||||
return getFaultPose(input);
|
||||
}
|
||||
|
||||
const { dimensionsMm, targetCategory, elapsedMs, slotIndex = 0, jitter } = input;
|
||||
const category: 'B' | 'C' | 'D' = (targetCategory as 'B' | 'C' | 'D') || 'B';
|
||||
const itemHeightM = dimensionsMm.height / 1000;
|
||||
|
||||
const { starts, total } = phaseStarts();
|
||||
const { starts, total } = phaseStartsFrom(CASE_PHASES);
|
||||
const feedStart = starts['move_to_detection'];
|
||||
const inspectStart = starts['detection'];
|
||||
const junctionStart = starts['measurement'];
|
||||
@@ -98,25 +174,20 @@ export function getPhysicalItemPose(input: PoseInput): PhysicalItemPose {
|
||||
let isSettled = false;
|
||||
|
||||
if (elapsedMs <= feedStart) {
|
||||
// Spawn dwell at belt entry (A).
|
||||
const r = poseOnSurface('main_belt', 0, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'main_belt'; phase = 'feed';
|
||||
} else if (elapsedMs <= inspectStart) {
|
||||
// Feed along the main belt A → inspection.
|
||||
const t = (elapsedMs - feedStart) / (inspectStart - feedStart);
|
||||
const r = poseOnSurface('main_belt', t, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'main_belt'; phase = 'feed';
|
||||
} else if (elapsedMs <= junctionStart) {
|
||||
// Dwell under the inspection station.
|
||||
const r = poseOnSurface('inspection_station', 0, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'inspection_station'; phase = 'inspection';
|
||||
} else if (elapsedMs <= routingStart) {
|
||||
// Travel to the routing junction.
|
||||
const t = (elapsedMs - junctionStart) / (routingStart - junctionStart);
|
||||
const r = poseOnSurface('routing_junction', t, itemHeightM);
|
||||
pos = r.pos; rotY = r.rotY; surface = 'routing_junction'; phase = 'decision';
|
||||
} else if (category === 'B') {
|
||||
// B: short transfer spur → drop chute → settle inside floor bin.
|
||||
const exitStart = starts['exit'];
|
||||
const travelSpan = exitStart - routingStart;
|
||||
const t = (elapsedMs - routingStart) / travelSpan;
|
||||
@@ -132,7 +203,6 @@ export function getPhysicalItemPose(input: PoseInput): PhysicalItemPose {
|
||||
isSettled = elapsedMs >= total;
|
||||
}
|
||||
} else {
|
||||
// C / D: slide down the chute, then settle on the cage floor.
|
||||
const chuteName: SurfaceName = category === 'C' ? 'chute_c' : 'chute_d';
|
||||
const cageName: SurfaceName = category === 'C' ? 'c_cage_floor' : 'd_cage_floor';
|
||||
const travelSpan = clearStart - routingStart;
|
||||
@@ -147,14 +217,14 @@ export function getPhysicalItemPose(input: PoseInput): PhysicalItemPose {
|
||||
}
|
||||
}
|
||||
|
||||
// Guarantee no NaN/Infinity escapes.
|
||||
if (!Number.isFinite(pos[0]) || !Number.isFinite(pos[1]) || !Number.isFinite(pos[2])) {
|
||||
pos = [SURFACES.main_belt.start[0], SURFACES.main_belt.surfaceY + itemHeightM / 2, 0];
|
||||
const j = applyJitter(pos, rotY, jitter);
|
||||
if (!Number.isFinite(j.pos[0]) || !Number.isFinite(j.pos[1]) || !Number.isFinite(j.pos[2])) {
|
||||
j.pos = [SURFACES.main_belt.start[0], SURFACES.main_belt.surfaceY + itemHeightM / 2, 0];
|
||||
}
|
||||
|
||||
return {
|
||||
position: pos,
|
||||
rotation: [0, rotY, 0],
|
||||
position: j.pos,
|
||||
rotation: [0, j.rotY, 0],
|
||||
surface,
|
||||
phase,
|
||||
isSettled,
|
||||
|
||||
@@ -121,8 +121,9 @@ describe('Playlist Items Physical Constraints', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('playlist has 8 cases', () => {
|
||||
expect(DEMO_PLAYLIST).toHaveLength(8);
|
||||
it('playlist has classification and safety cases', () => {
|
||||
expect(DEMO_PLAYLIST.length).toBeGreaterThanOrEqual(8);
|
||||
expect(DEMO_PLAYLIST).toHaveLength(10);
|
||||
});
|
||||
|
||||
it('low confidence case still has B/C/D category', () => {
|
||||
|
||||
30
src/domain/qualityMode.test.ts
Normal file
30
src/domain/qualityMode.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Quality mode unit tests.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { detectQualityMode, getQualitySettings, adaptQuality } from './qualityMode';
|
||||
|
||||
describe('qualityMode', () => {
|
||||
it('selects low on narrow viewports', () => {
|
||||
expect(detectQualityMode(600)).toBe('low');
|
||||
});
|
||||
|
||||
it('selects demo on wide viewports with healthy FPS', () => {
|
||||
expect(detectQualityMode(1440, 60)).toBe('demo');
|
||||
});
|
||||
|
||||
it('downgrades when FPS collapses', () => {
|
||||
expect(detectQualityMode(1440, 20)).toBe('low');
|
||||
});
|
||||
|
||||
it('demo preset keeps effects off for stable FPS', () => {
|
||||
const s = getQualitySettings('demo');
|
||||
expect(s.effectsEnabled).toBe(false);
|
||||
expect(s.shadows).toBe(false);
|
||||
expect(s.targetFps).toBe(60);
|
||||
});
|
||||
|
||||
it('adaptQuality steps down under load', () => {
|
||||
expect(adaptQuality('demo', 20)).toBe('high');
|
||||
});
|
||||
});
|
||||
85
src/domain/qualityMode.ts
Normal file
85
src/domain/qualityMode.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Render quality modes adapted to device capability and demo needs.
|
||||
* Demo/Ultra targets stable 60 FPS when hardware allows; falls back gracefully.
|
||||
*/
|
||||
|
||||
export type QualityMode = 'low' | 'medium' | 'high' | 'demo';
|
||||
|
||||
export interface QualitySettings {
|
||||
mode: QualityMode;
|
||||
dprMax: number;
|
||||
antialias: boolean;
|
||||
shadows: boolean;
|
||||
maxVisibleItems: number;
|
||||
effectsEnabled: boolean;
|
||||
rollerDetail: 'none' | 'sparse' | 'full';
|
||||
targetFps: 30 | 60;
|
||||
}
|
||||
|
||||
const PRESETS: Record<QualityMode, QualitySettings> = {
|
||||
low: {
|
||||
mode: 'low',
|
||||
dprMax: 1,
|
||||
antialias: false,
|
||||
shadows: false,
|
||||
maxVisibleItems: 3,
|
||||
effectsEnabled: false,
|
||||
rollerDetail: 'none',
|
||||
targetFps: 30,
|
||||
},
|
||||
medium: {
|
||||
mode: 'medium',
|
||||
dprMax: 1.25,
|
||||
antialias: false,
|
||||
shadows: false,
|
||||
maxVisibleItems: 5,
|
||||
effectsEnabled: false,
|
||||
rollerDetail: 'sparse',
|
||||
targetFps: 30,
|
||||
},
|
||||
high: {
|
||||
mode: 'high',
|
||||
dprMax: 1.5,
|
||||
antialias: true,
|
||||
shadows: false,
|
||||
maxVisibleItems: 6,
|
||||
effectsEnabled: false,
|
||||
rollerDetail: 'full',
|
||||
targetFps: 60,
|
||||
},
|
||||
demo: {
|
||||
mode: 'demo',
|
||||
dprMax: 1.5,
|
||||
antialias: true,
|
||||
shadows: false,
|
||||
maxVisibleItems: 6,
|
||||
effectsEnabled: false,
|
||||
rollerDetail: 'full',
|
||||
targetFps: 60,
|
||||
},
|
||||
};
|
||||
|
||||
/** Auto-select quality from viewport width and optional FPS sample. */
|
||||
export function detectQualityMode(width: number, recentFps?: number): QualityMode {
|
||||
if (width < 768) return 'low';
|
||||
if (width < 1100) return 'medium';
|
||||
if (recentFps !== undefined && recentFps < 28) return 'low';
|
||||
if (recentFps !== undefined && recentFps < 45) return 'medium';
|
||||
return 'demo';
|
||||
}
|
||||
|
||||
export function getQualitySettings(mode: QualityMode): QualitySettings {
|
||||
return PRESETS[mode];
|
||||
}
|
||||
|
||||
/** Soft downgrade when FPS drops below target. */
|
||||
export function adaptQuality(current: QualityMode, fps: number): QualityMode {
|
||||
const order: QualityMode[] = ['low', 'medium', 'high', 'demo'];
|
||||
const idx = order.indexOf(current);
|
||||
const target = getQualitySettings(current).targetFps;
|
||||
if (fps < target * 0.7 && idx > 0) return order[idx - 1];
|
||||
if (fps > target * 0.95 && idx < order.length - 1 && current !== 'demo') {
|
||||
return order[Math.min(idx + 1, order.length - 1)];
|
||||
}
|
||||
return current;
|
||||
}
|
||||
20
src/domain/seededRng.ts
Normal file
20
src/domain/seededRng.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Deterministic PRNG (Mulberry32) for reproducible demo variability.
|
||||
*/
|
||||
|
||||
export function createSeededRng(seed: number): () => number {
|
||||
let t = seed >>> 0;
|
||||
return () => {
|
||||
t += 0x6d2b79f5;
|
||||
let r = Math.imul(t ^ (t >>> 15), 1 | t);
|
||||
r ^= r + Math.imul(r ^ (r >>> 7), 61 | r);
|
||||
return ((r ^ (r >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
/** Small signed offset in [-amplitude, +amplitude]. */
|
||||
export function seededOffset(rng: () => number, amplitude: number): number {
|
||||
return (rng() * 2 - 1) * amplitude;
|
||||
}
|
||||
|
||||
export const DEFAULT_DEMO_SEED = 20260715;
|
||||
54
src/domain/simulation.test.ts
Normal file
54
src/domain/simulation.test.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createSimulation, stepSimulationToNextState, setRunning, stepSimulation } from './simulation';
|
||||
import { SCENARIOS } from '../data/scenarios';
|
||||
|
||||
function scenario(id: string) {
|
||||
const s = SCENARIOS.find((x) => x.id === id);
|
||||
if (!s) throw new Error(`missing scenario ${id}`);
|
||||
return s;
|
||||
}
|
||||
|
||||
describe('simulation state machine', () => {
|
||||
it('classifies and routes normal item to B/C/D via classifyItem', () => {
|
||||
let state = createSimulation(scenario('normal_flow'));
|
||||
state = stepSimulationToNextState(state);
|
||||
expect(state.machineState).toBe('MOVING_TO_CAMERA');
|
||||
expect(state.currentItem).toBeDefined();
|
||||
expect(['B', 'C', 'D']).toContain(state.currentItem!.classification.category);
|
||||
});
|
||||
|
||||
it('oversized scenario routes to C', () => {
|
||||
let state = createSimulation(scenario('oversized_item'));
|
||||
state = stepSimulationToNextState(state);
|
||||
expect(state.currentItem!.classification.category).toBe('C');
|
||||
});
|
||||
|
||||
it('c_priority chooses C over D', () => {
|
||||
let state = createSimulation(scenario('c_priority'));
|
||||
state = stepSimulationToNextState(state);
|
||||
expect(state.currentItem!.classification.category).toBe('C');
|
||||
expect(state.currentItem!.item.roundness).toBeGreaterThanOrEqual(0.7);
|
||||
});
|
||||
|
||||
it('jam scenario enters FAULT', () => {
|
||||
let state = setRunning(createSimulation(scenario('jam')), true);
|
||||
state = setRunning(stepSimulationToNextState(state), true);
|
||||
for (let i = 0; i < 120; i++) {
|
||||
state = stepSimulation(setRunning(state, true), 100);
|
||||
if (state.machineState === 'FAULT') break;
|
||||
}
|
||||
expect(state.machineState).toBe('FAULT');
|
||||
expect(state.systemStatus).toBe('FAULT');
|
||||
});
|
||||
|
||||
it('emergency_stop freezes system', () => {
|
||||
let state = setRunning(createSimulation(scenario('emergency_stop')), true);
|
||||
state = stepSimulationToNextState(state);
|
||||
for (let i = 0; i < 40; i++) {
|
||||
state = stepSimulation(state, 100);
|
||||
if (state.machineState === 'EMERGENCY_STOP') break;
|
||||
}
|
||||
expect(state.machineState).toBe('EMERGENCY_STOP');
|
||||
expect(state.systemStatus).toBe('EMERGENCY_STOP');
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,14 @@
|
||||
import { lazy, Suspense, useEffect, useState, useMemo } from 'react';
|
||||
import { lazy, Suspense, useEffect, useState, useMemo, useCallback } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import ThreeFallback from '../components/ThreeD/ThreeFallback';
|
||||
import ThreeErrorBoundary from '../components/ThreeD/ThreeErrorBoundary';
|
||||
import { prefer3DByDefault, useWebGLSupport } from '../components/ThreeD/useWebGL';
|
||||
import { DEMO_PLAYLIST, PLAYLIST_LENGTH } from '../domain/demoPlaylist';
|
||||
import type { ContinuousPlaybackState } from '../domain/continuousPlayback';
|
||||
import { getCaseProgress, getCurrentPhaseConfig } from '../domain/continuousPlayback';
|
||||
import type { ContinuousPlaybackState, PlaybackSpeed } from '../domain/continuousPlayback';
|
||||
import { getCaseProgress, getCurrentPhaseConfig, isFaultActive } from '../domain/continuousPlayback';
|
||||
import { getMeasurementData, shouldShowMeasurement } from '../domain/measurementSystem';
|
||||
import { getViewportType, type ViewportType } from '../domain/cinematicCamera';
|
||||
import { detectQualityMode } from '../domain/qualityMode';
|
||||
import { resolveItem } from '../data/resolveItem';
|
||||
import CVInspectionOverlay from '../components/CVInspectionOverlay';
|
||||
|
||||
const SorterDigitalTwinContinuous = lazy(() => import('../components/ThreeD/SorterDigitalTwinContinuous'));
|
||||
@@ -17,18 +18,30 @@ interface MainPageProps {
|
||||
onPlay: () => void;
|
||||
onPause: () => void;
|
||||
onStop: () => void;
|
||||
onSeekCase: (index: number) => void;
|
||||
onSeekNext: () => void;
|
||||
onSeekPrev: () => void;
|
||||
onSetSpeed: (speed: PlaybackSpeed) => void;
|
||||
}
|
||||
|
||||
const SPEEDS: PlaybackSpeed[] = [0.5, 1, 1.5, 2];
|
||||
|
||||
export default function MainPage({
|
||||
playback,
|
||||
onPlay,
|
||||
onPause,
|
||||
onStop,
|
||||
onSeekCase,
|
||||
onSeekNext,
|
||||
onSeekPrev,
|
||||
onSetSpeed,
|
||||
}: MainPageProps) {
|
||||
const webgl = useWebGLSupport();
|
||||
const [width, setWidth] = useState(() => (typeof window === 'undefined' ? 1200 : window.innerWidth));
|
||||
const [contextLost, setContextLost] = useState(false);
|
||||
const [autoCameraEnabled, setAutoCameraEnabled] = useState(true);
|
||||
const [presentationMode, setPresentationMode] = useState(false);
|
||||
const [showEventLog, setShowEventLog] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const onResize = () => setWidth(window.innerWidth);
|
||||
@@ -36,34 +49,98 @@ export default function MainPage({
|
||||
return () => window.removeEventListener('resize', onResize);
|
||||
}, []);
|
||||
|
||||
// Hotkeys for live demo resilience
|
||||
useEffect(() => {
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) return;
|
||||
switch (e.key.toLowerCase()) {
|
||||
case ' ':
|
||||
e.preventDefault();
|
||||
if (playback.status === 'running') onPause();
|
||||
else onPlay();
|
||||
break;
|
||||
case 'r':
|
||||
onStop();
|
||||
onPlay();
|
||||
break;
|
||||
case 'n':
|
||||
case 'arrowright':
|
||||
onSeekNext();
|
||||
break;
|
||||
case 'b':
|
||||
case 'arrowleft':
|
||||
onSeekPrev();
|
||||
break;
|
||||
case 'f':
|
||||
if (!document.fullscreenElement) {
|
||||
document.documentElement.requestFullscreen?.().catch(() => undefined);
|
||||
} else {
|
||||
document.exitFullscreen?.().catch(() => undefined);
|
||||
}
|
||||
break;
|
||||
case 'p':
|
||||
setPresentationMode((v) => !v);
|
||||
break;
|
||||
case 'e':
|
||||
setShowEventLog((v) => !v);
|
||||
break;
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
case '0': {
|
||||
const idx = e.key === '0' ? 9 : Number(e.key) - 1;
|
||||
if (idx < PLAYLIST_LENGTH) onSeekCase(idx);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
window.addEventListener('keydown', onKey);
|
||||
return () => window.removeEventListener('keydown', onKey);
|
||||
}, [playback.status, onPlay, onPause, onStop, onSeekNext, onSeekPrev, onSeekCase]);
|
||||
|
||||
const show3D = prefer3DByDefault(width, webgl && !contextLost);
|
||||
const simplified = width < 900;
|
||||
const qualityMode = detectQualityMode(width);
|
||||
const simplified = width < 900 || qualityMode === 'low';
|
||||
const viewportType: ViewportType = getViewportType(width);
|
||||
|
||||
const isRunning = playback.status === 'running';
|
||||
const isPaused = playback.status === 'paused';
|
||||
const isFinished = playback.status === 'finished';
|
||||
const faultActive = isFaultActive(playback);
|
||||
|
||||
const currentCase = playback.currentCase;
|
||||
const category = playback.targetCategory;
|
||||
const command = playback.command;
|
||||
const phaseConfig = getCurrentPhaseConfig(playback);
|
||||
const caseProgress = getCaseProgress(playback);
|
||||
|
||||
|
||||
const measurementData = useMemo(() => getMeasurementData(playback), [playback]);
|
||||
const showMeasurement = shouldShowMeasurement(playback.currentPhase) && (isRunning || isPaused) && !isFinished;
|
||||
const showMeasurement =
|
||||
!presentationMode &&
|
||||
shouldShowMeasurement(playback.currentPhase) &&
|
||||
(isRunning || isPaused) &&
|
||||
!isFinished;
|
||||
|
||||
const handlePlayPause = () => {
|
||||
if (isRunning) {
|
||||
onPause();
|
||||
} else {
|
||||
onPlay();
|
||||
}
|
||||
if (isRunning) onPause();
|
||||
else onPlay();
|
||||
};
|
||||
|
||||
const enterFullscreen = useCallback(() => {
|
||||
document.documentElement.requestFullscreen?.().catch(() => undefined);
|
||||
setPresentationMode(true);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="main-page">
|
||||
{/* Full-screen 3D Demo */}
|
||||
<div className={`main-page ${presentationMode ? 'presentation-mode' : ''} ${faultActive ? 'fault-active' : ''}`}>
|
||||
<div className="main-demo-viewport">
|
||||
{show3D ? (
|
||||
<ThreeErrorBoundary
|
||||
@@ -93,70 +170,86 @@ export default function MainPage({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Measurement System Overlay - right center */}
|
||||
{width >= 768 && (
|
||||
<CVInspectionOverlay data={measurementData} visible={showMeasurement} />
|
||||
)}
|
||||
|
||||
{/* Minimal HUD - top right */}
|
||||
<div className="main-hud">
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Item</span>
|
||||
<span className="hud-value">{currentCase.title}</span>
|
||||
</div>
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Status</span>
|
||||
<span className={`hud-value status-${playback.status}`}>
|
||||
{isFinished ? 'FINISHED' : phaseConfig.label}
|
||||
</span>
|
||||
</div>
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Category</span>
|
||||
<span className={`hud-value ${category ? `category-${category}` : ''}`}>
|
||||
{category ?? '—'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Command</span>
|
||||
<span className="hud-value">{command}</span>
|
||||
</div>
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Speed</span>
|
||||
<span className="hud-value">1.0 m/s</span>
|
||||
</div>
|
||||
{playback.warning && (
|
||||
<div className="hud-row hud-warning">
|
||||
<span className="hud-value warning-text">⚠ {playback.warning}</span>
|
||||
{!presentationMode && (
|
||||
<div className="main-hud">
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Item</span>
|
||||
<span className="hud-value">{currentCase.title}</span>
|
||||
</div>
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Status</span>
|
||||
<span className={`hud-value status-${playback.status} ${faultActive ? 'status-fault' : ''}`}>
|
||||
{isFinished ? 'FINISHED' : phaseConfig.label}
|
||||
</span>
|
||||
</div>
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Category</span>
|
||||
<span className={`hud-value ${category ? `category-${category}` : ''}`}>
|
||||
{category ?? '—'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Command</span>
|
||||
<span className="hud-value">{command}</span>
|
||||
</div>
|
||||
{playback.classification && (
|
||||
<div className="hud-row hud-row-small">
|
||||
<span className="hud-value proof-text">
|
||||
{playback.classification.dimensionsPass ? 'DIM✓' : 'DIM✗'} · K=
|
||||
{resolveItem(currentCase.itemId).roundness.toFixed(2)} · {playback.classification.reason}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Speed</span>
|
||||
<span className="hud-value">{playback.speed.toFixed(1)}× · 1.0 m/s</span>
|
||||
</div>
|
||||
{playback.warning && (
|
||||
<div className="hud-row hud-warning">
|
||||
<span className="hud-value warning-text">⚠ {playback.warning}</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="hud-divider" />
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Case</span>
|
||||
<span className="hud-value">{playback.currentCaseIndex + 1}/{PLAYLIST_LENGTH}</span>
|
||||
</div>
|
||||
<div className="hud-row hud-row-small">
|
||||
<span className="hud-value">{currentCase.description}</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="hud-divider" />
|
||||
<div className="hud-row">
|
||||
<span className="hud-label">Case</span>
|
||||
<span className="hud-value">{playback.currentCaseIndex + 1}/{PLAYLIST_LENGTH}</span>
|
||||
</div>
|
||||
<div className="hud-row hud-row-small">
|
||||
<span className="hud-value">{currentCase.description}</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Case progress bar */}
|
||||
<div className="main-case-progress">
|
||||
<div className="case-progress-bar" style={{ width: `${caseProgress * 100}%` }} />
|
||||
<div
|
||||
className={`case-progress-bar ${faultActive ? 'fault' : ''}`}
|
||||
style={{ width: `${caseProgress * 100}%` }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Playlist progress dots */}
|
||||
<div className="main-progress">
|
||||
{DEMO_PLAYLIST.map((c, idx) => (
|
||||
<div
|
||||
key={c.id}
|
||||
className={`progress-dot ${idx === playback.currentCaseIndex ? 'active' : ''} ${idx < playback.currentCaseIndex ? 'done' : ''}`}
|
||||
title={`${idx + 1}. ${c.title} → ${c.expectedCategory}`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
{!presentationMode && (
|
||||
<div className="main-progress">
|
||||
{DEMO_PLAYLIST.map((c, idx) => (
|
||||
<button
|
||||
key={c.id}
|
||||
type="button"
|
||||
className={`progress-dot ${idx === playback.currentCaseIndex ? 'active' : ''} ${idx < playback.currentCaseIndex ? 'done' : ''} ${c.faultType ? 'fault-dot' : ''}`}
|
||||
title={`${idx + 1}. ${c.title} → ${c.faultType ?? c.expectedCategory}`}
|
||||
onClick={() => onSeekCase(idx)}
|
||||
aria-label={`Jump to case ${idx + 1}`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Play/Pause Button - center bottom */}
|
||||
<div className="main-controls">
|
||||
<button type="button" className="ctrl-nav" onClick={onSeekPrev} aria-label="Previous case" title="Prev (← / B)">
|
||||
⏮
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="play-button"
|
||||
@@ -172,20 +265,37 @@ export default function MainPage({
|
||||
{isRunning ? 'Pause' : isPaused ? 'Resume' : isFinished ? 'Replay' : 'Play Demo'}
|
||||
</span>
|
||||
</button>
|
||||
<button type="button" className="ctrl-nav" onClick={onSeekNext} aria-label="Next case" title="Next (→ / N)">
|
||||
⏭
|
||||
</button>
|
||||
|
||||
{(isRunning || isPaused) && (
|
||||
<button type="button" className="stop-button" onClick={onStop} aria-label="Stop demo">
|
||||
<button type="button" className="stop-button" onClick={onStop} aria-label="Stop demo" title="Reset (R)">
|
||||
⏹
|
||||
</button>
|
||||
)}
|
||||
|
||||
{!presentationMode && (
|
||||
<div className="speed-controls" role="group" aria-label="Playback speed">
|
||||
{SPEEDS.map((s) => (
|
||||
<button
|
||||
key={s}
|
||||
type="button"
|
||||
className={`speed-btn ${playback.speed === s ? 'active' : ''}`}
|
||||
onClick={() => onSetSpeed(s)}
|
||||
>
|
||||
{s}×
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Finished overlay */}
|
||||
{isFinished && (
|
||||
<div className="main-finished-overlay">
|
||||
<div className="finished-content">
|
||||
<h2>Demo Complete</h2>
|
||||
<p>All 8 cases demonstrated successfully</p>
|
||||
<p>All {PLAYLIST_LENGTH} cases demonstrated — including safety scenarios</p>
|
||||
<button type="button" className="btn-primary" onClick={onPlay}>
|
||||
Replay Demo
|
||||
</button>
|
||||
@@ -193,9 +303,22 @@ export default function MainPage({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Auto Camera Toggle - bottom left */}
|
||||
{show3D && (
|
||||
<button
|
||||
{showEventLog && !presentationMode && (
|
||||
<div className="main-event-log" aria-live="polite">
|
||||
<div className="event-log-header">Event journal</div>
|
||||
<ul>
|
||||
{playback.events.slice(0, 8).map((ev) => (
|
||||
<li key={ev.id} className={`evt-${ev.status}`}>
|
||||
<span className="evt-time">{Math.round(ev.timestampMs / 1000)}s</span>
|
||||
<span className="evt-msg">{ev.message}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{show3D && !presentationMode && (
|
||||
<button
|
||||
type="button"
|
||||
className="auto-camera-toggle"
|
||||
onClick={() => setAutoCameraEnabled(!autoCameraEnabled)}
|
||||
@@ -206,10 +329,23 @@ export default function MainPage({
|
||||
</button>
|
||||
)}
|
||||
|
||||
{/* Details link - bottom right */}
|
||||
<Link to="/details" className="details-link">
|
||||
Details →
|
||||
</Link>
|
||||
{!presentationMode && (
|
||||
<>
|
||||
<button type="button" className="presentation-toggle" onClick={enterFullscreen} title="Presentation (P / F)">
|
||||
Present
|
||||
</button>
|
||||
<Link to="/details" className="details-link">
|
||||
Details →
|
||||
</Link>
|
||||
<div className="hotkey-hint">Space play · N/B seek · 1–0 jump · R reset · P present · E log · F fullscreen</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{presentationMode && (
|
||||
<button type="button" className="presentation-exit" onClick={() => setPresentationMode(false)}>
|
||||
Exit presentation (P)
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
159
src/styles.css
159
src/styles.css
@@ -2832,3 +2832,162 @@ button:disabled {
|
||||
max-height: calc(100vh - 320px);
|
||||
}
|
||||
}
|
||||
|
||||
/* ——— Maximum demo realism: controls, presentation, events ——— */
|
||||
.ctrl-nav {
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
border-radius: 50%;
|
||||
border: 1px solid rgba(148, 163, 184, 0.35);
|
||||
background: rgba(5, 9, 16, 0.7);
|
||||
color: #e2e8f0;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.ctrl-nav:hover { border-color: #38bdf8; color: #38bdf8; }
|
||||
|
||||
.speed-controls {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
margin-left: 8px;
|
||||
padding: 4px;
|
||||
border-radius: 999px;
|
||||
background: rgba(5, 9, 16, 0.65);
|
||||
border: 1px solid rgba(148, 163, 184, 0.25);
|
||||
}
|
||||
.speed-btn {
|
||||
min-width: 40px;
|
||||
height: 32px;
|
||||
border: none;
|
||||
border-radius: 999px;
|
||||
background: transparent;
|
||||
color: #94a3b8;
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
}
|
||||
.speed-btn.active {
|
||||
background: rgba(56, 189, 248, 0.25);
|
||||
color: #7dd3fc;
|
||||
}
|
||||
|
||||
.progress-dot {
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
padding: 0;
|
||||
}
|
||||
.progress-dot.fault-dot {
|
||||
background: rgba(239, 68, 68, 0.55);
|
||||
}
|
||||
.progress-dot.fault-dot.active {
|
||||
background: #ef4444;
|
||||
box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.45);
|
||||
}
|
||||
|
||||
.case-progress-bar.fault {
|
||||
background: linear-gradient(90deg, #f97316, #ef4444);
|
||||
}
|
||||
|
||||
.main-page.fault-active .main-hud {
|
||||
border-color: rgba(239, 68, 68, 0.45);
|
||||
}
|
||||
|
||||
.hud-value.status-fault,
|
||||
.hud-value.proof-text {
|
||||
color: #fda4af;
|
||||
}
|
||||
.hud-value.proof-text {
|
||||
color: #cbd5e1;
|
||||
font-size: 11px;
|
||||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.presentation-toggle,
|
||||
.presentation-exit {
|
||||
position: absolute;
|
||||
z-index: 12;
|
||||
padding: 10px 14px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid rgba(56, 189, 248, 0.35);
|
||||
background: rgba(5, 9, 16, 0.75);
|
||||
color: #7dd3fc;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
}
|
||||
.presentation-toggle { bottom: 16px; right: 140px; }
|
||||
.presentation-exit { top: 16px; right: 16px; }
|
||||
|
||||
.hotkey-hint {
|
||||
position: absolute;
|
||||
bottom: 8px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
z-index: 9;
|
||||
font-size: 11px;
|
||||
color: rgba(148, 163, 184, 0.75);
|
||||
pointer-events: none;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.main-event-log {
|
||||
position: absolute;
|
||||
left: 16px;
|
||||
bottom: 100px;
|
||||
z-index: 12;
|
||||
width: min(360px, 42vw);
|
||||
max-height: 240px;
|
||||
overflow: auto;
|
||||
padding: 12px;
|
||||
border-radius: 12px;
|
||||
background: rgba(5, 9, 16, 0.88);
|
||||
border: 1px solid rgba(148, 163, 184, 0.25);
|
||||
backdrop-filter: blur(10px);
|
||||
color: #e2e8f0;
|
||||
font-size: 12px;
|
||||
}
|
||||
.event-log-header {
|
||||
font-weight: 800;
|
||||
margin-bottom: 8px;
|
||||
color: #7dd3fc;
|
||||
letter-spacing: 0.04em;
|
||||
text-transform: uppercase;
|
||||
font-size: 11px;
|
||||
}
|
||||
.main-event-log ul {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
.main-event-log li {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
padding: 4px 0;
|
||||
border-bottom: 1px solid rgba(148, 163, 184, 0.12);
|
||||
}
|
||||
.evt-time { color: #64748b; min-width: 28px; }
|
||||
.evt-error .evt-msg { color: #fda4af; }
|
||||
.evt-warning .evt-msg { color: #fdba74; }
|
||||
.evt-success .evt-msg { color: #86efac; }
|
||||
|
||||
.cv-reason-row .cv-reason {
|
||||
font-size: 11px;
|
||||
line-height: 1.3;
|
||||
color: #cbd5e1;
|
||||
white-space: normal;
|
||||
}
|
||||
|
||||
.main-page.presentation-mode .main-hud,
|
||||
.main-page.presentation-mode .main-progress,
|
||||
.main-page.presentation-mode .auto-camera-toggle,
|
||||
.main-page.presentation-mode .details-link,
|
||||
.main-page.presentation-mode .hotkey-hint,
|
||||
.main-page.presentation-mode .presentation-toggle,
|
||||
.main-page.presentation-mode .speed-controls {
|
||||
display: none;
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
.speed-controls { display: none; }
|
||||
.hotkey-hint { display: none; }
|
||||
.presentation-toggle { right: 16px; bottom: 72px; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user