|
| 1 | +import type { VSDocument } from './model' |
| 2 | +import type { MBotSensorValues } from './mbot' |
| 3 | +import { ROBOT_STEP_MS, RobotWorld, type Point, type Pose, type RobotWorldSetup } from './robotWorld' |
| 4 | +import { SimulationEngine, type SimNotice, type SubprogramResolver } from './simulation' |
| 5 | + |
| 6 | +/** Ein Bild der Fahrt: wo der mBot2 steht, seine Spur, LEDs und Bildschirm, dazu ein Ton in diesem Schritt. */ |
| 7 | +export interface PreviewFrame { |
| 8 | + pose: Pose |
| 9 | + trail: Point[] |
| 10 | + leds: RobotWorld['leds'] |
| 11 | + screen: string | undefined |
| 12 | + tone?: [number, number] |
| 13 | +} |
| 14 | + |
| 15 | +export interface PreviewResult { |
| 16 | + frames: PreviewFrame[] |
| 17 | + /** Warum die Vorschau aufhört: Ende, Eingabe nötig, Fehler oder zu viele Schritte (Endlosschleife?). */ |
| 18 | + stop: 'end' | 'input' | 'error' | 'limit' |
| 19 | + notice?: SimNotice |
| 20 | + bumps: number |
| 21 | + /** Strecke der Spur in cm. */ |
| 22 | + distance: number |
| 23 | +} |
| 24 | + |
| 25 | +export const maxPreviewSteps = 5000 |
| 26 | + |
| 27 | +/** |
| 28 | + * mBot2-Vorschau beim Bearbeiten: das Programm im Hintergrund ganz durchrechnen, wie in der Simulation |
| 29 | + * (die Welt liefert die Sensorwerte), und jedes Bild der Fahrt merken. |
| 30 | + */ |
| 31 | +export function previewRun(doc: VSDocument, subprograms: SubprogramResolver | undefined, setup: RobotWorldSetup, controls: MBotSensorValues = {}): PreviewResult { |
| 32 | + const engine = new SimulationEngine(doc, subprograms) |
| 33 | + const robot = new RobotWorld(setup) |
| 34 | + const frame = (tone?: [number, number]): PreviewFrame => ({ |
| 35 | + pose: { ...robot.pose }, |
| 36 | + trail: robot.trail.map((p) => ({ ...p })), |
| 37 | + leds: [...robot.leds], |
| 38 | + screen: robot.screen, |
| 39 | + tone, |
| 40 | + }) |
| 41 | + const frames: PreviewFrame[] = [frame()] |
| 42 | + let applied = 0 |
| 43 | + const result = (stop: PreviewResult['stop'], notice?: SimNotice): PreviewResult => ({ |
| 44 | + frames, |
| 45 | + stop, |
| 46 | + notice, |
| 47 | + bumps: robot.bumps, |
| 48 | + distance: Math.round(robot.trail.slice(1).reduce((sum, p, i) => sum + Math.hypot(p.x - robot.trail[i].x, p.y - robot.trail[i].y), 0)), |
| 49 | + }) |
| 50 | + for (let i = 0; i < maxPreviewSteps; i++) { |
| 51 | + const { state } = engine |
| 52 | + if (state.inputRequest) return result('input') |
| 53 | + if (state.isHalted) return result('error', state.notice) |
| 54 | + if (!engine.canStep) return result('end') |
| 55 | + engine.robotSensors = { ...controls, ...robot.sensors() } |
| 56 | + engine.step() |
| 57 | + // jeder neue Befehl ist ein Bild; mit `mbot.drive` fährt er auch ohne Befehl weiter |
| 58 | + for (; applied < state.robotActions.length; applied++) { |
| 59 | + const action = state.robotActions[applied] |
| 60 | + robot.apply(action) |
| 61 | + frames.push(frame(action.method === 'playTone' ? [action.args[0], action.args[1]] : undefined)) |
| 62 | + } |
| 63 | + const moving = robot.wheels.some((p) => p !== 0) |
| 64 | + robot.tick(ROBOT_STEP_MS) |
| 65 | + if (moving) frames.push(frame()) |
| 66 | + } |
| 67 | + return result('limit') |
| 68 | +} |
0 commit comments