Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | 1x 1x 2x 2x 1x 1x 2x 2x 7x 7x 2x 2x 2x 1x 311x 311x 311x 311x 311x 311x 311x 311x 311x 188x 188x 311x 48x 48x 74x 74x 74x 48x 311x 50x 50x 311x 2x 2x 311x 46x 46x 46x 46x 36x 36x 36x 46x 2x 2x 2x 46x 46x 46x 46x 46x 46x 46x 46x 46x 311x 249x 249x 249x 249x 249x 9x 1x 1x 8x 6x 6x 6x 1x 1x 6x 6x 6x 9x 2x 2x 2x 2x 9x 9x 9x 249x 249x 249x 249x 2x 2x 2x 2x 249x 1x 1x 249x 249x 249x 249x 249x 249x 249x 1x 1x 1x 1x 1x 1x 1x 1x 20x 1x 1x 1x 249x 249x 6x 6x 6x 6x 6x 6x 6x 6x 147x 6x 6x 6x 249x 249x 249x 311x 95x 95x 185x 8x 8x 177x 176x 176x 172x 172x 176x 5x 185x 95x 95x 95x 2x 2x 95x 95x 95x 95x 95x 95x 16x 16x 12x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 16x 16x 16x 3x 16x 16x 95x 95x 2x 2x 95x 27x 27x 27x 27x 6x 6x 27x 21x 21x 21x 27x 27x 27x 27x 2x 25x 10x 10x 27x 27x 27x 95x 2x 2x 2x 2x 95x 21x 21x 21x 21x 95x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 13x 13x 13x 1x 1x 12x 12x 12x 12x 12x 12x 36x 12x 12x 12x 36x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 231x 231x 12x 12x 12x 12x 13x 95x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 9x 3x 3x 3x 9x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 63x 63x 3x 3x 3x 3x 3x 95x 95x 311x 3x 3x 3x 3x 3x 22x 22x 22x 3x 3x 3x 3x 311x 311x | import { SimulationPhysicsEngine } from '../physics/engine';
import { VirtualSensorManager } from '../sensors/sensor-manager';
import { MotorPort, resolveMotorSpeedInput } from '../physics/motor-controller';
export class VirtualTimer {
private startTime: number = performance.now();
public reset(): void {
this.startTime = performance.now();
}
public now(): number {
return (performance.now() - this.startTime) / 1000;
}
public get_time_sec(): number {
return (performance.now() - this.startTime) / 1000;
}
public get_time_msec(): number {
return performance.now() - this.startTime;
}
public time(): number {
return Math.round(performance.now() - this.startTime);
}
}
export class VirtualSpikeApi {
private engine: SimulationPhysicsEngine;
private sensors: VirtualSensorManager;
private abortSignal?: AbortSignal;
constructor(engine: SimulationPhysicsEngine, sensors: VirtualSensorManager, abortSignal?: AbortSignal) {
this.engine = engine;
this.sensors = sensors;
this.abortSignal = abortSignal;
}
private checkAborted(): void {
if (this.abortSignal?.aborted) {
throw new Error('Simulation execution stopped by user');
}
}
public async wait(seconds: number): Promise<void> {
const end = Date.now() + seconds * 1000;
while (Date.now() < end) {
this.checkAborted();
await new Promise((r) => setTimeout(r, 16)); // ~60fps yield
}
}
public stopAllMotors(): void {
this.engine.robot?.stopAllMotors();
}
// --- Classes exposed to user scripts ---
public createTimer(): VirtualTimer {
return new VirtualTimer();
}
public createPrimeHub() {
const self = this;
return {
motion_sensor: {
get_yaw_angle: () => {
self.checkAborted();
return self.sensors.getYaw();
},
reset_yaw_angle: () => {
self.checkAborted();
self.sensors.resetYaw();
},
},
light_matrix: {
write: (text: string) => {
console.log(`[SPIKE Hub Matrix]: ${text}`);
},
},
speaker: {
beep: (freq = 440, timeSec = 0.2) => {
console.log(`[SPIKE Speaker]: Beep ${freq}Hz for ${timeSec}s`);
},
},
};
}
public createMotor(port: any) {
const self = this;
const normalizedPort = (typeof port === 'string' ? port.trim().toUpperCase() : port) as MotorPort;
const motor = self.engine.robot?.motors.get(normalizedPort);
let defaultSpeed = 50;
const parseSpeed = (arg1?: any, arg2?: any): number => {
if (typeof arg1 === 'object' && arg1 !== null) {
return resolveMotorSpeedInput(arg1, defaultSpeed);
}
if (typeof arg1 === 'string') {
const s = arg1.trim().toLowerCase();
let sign = 1;
if (s === 'counterclockwise' || s === 'ccw' || s === 'anticlockwise' || s === 'reverse' || s === 'backward') {
sign = -1;
}
const spd = typeof arg2 === 'number' && Number.isFinite(arg2) ? arg2 : defaultSpeed;
return Math.max(-100, Math.min(100, Math.abs(spd) * sign));
}
if (typeof arg1 === 'number' && Number.isFinite(arg1)) {
let sign = 1;
if (typeof arg2 === 'string') {
const s = arg2.trim().toLowerCase();
if (s === 'counterclockwise' || s === 'ccw' || s === 'anticlockwise' || s === 'reverse' || s === 'backward') {
sign = -1;
}
}
return Math.max(-100, Math.min(100, Math.abs(arg1) * sign));
}
if (arg1 !== undefined && arg1 !== null) {
return resolveMotorSpeedInput(arg1, defaultSpeed);
}
if (arg2 !== undefined && arg2 !== null) {
return resolveMotorSpeedInput(arg2, defaultSpeed);
}
return defaultSpeed;
};
return {
port: normalizedPort,
set_default_speed: (speed: number) => {
if (Number.isFinite(speed)) defaultSpeed = speed;
},
start: (arg1?: any, arg2?: any) => {
self.checkAborted();
const spd = parseSpeed(arg1, arg2);
motor?.start(spd);
},
stop: () => {
motor?.stop();
},
get_degrees_counted: () => {
return Math.round(motor?.degrees || 0);
},
set_degrees_counted: (deg: number = 0) => {
if (motor) motor.degrees = deg;
},
get_position: () => {
return Math.round(motor?.degrees || 0);
},
get_speed: () => {
return Math.round(motor?.velocityDegPerSec || 0);
},
was_interrupted: () => false,
was_stalled: () => false,
run_for_degrees: async (degrees: number, arg1?: any, arg2?: any) => {
self.checkAborted();
if (!motor) return;
const spd = parseSpeed(arg1, arg2);
return new Promise<void>((resolve, reject) => {
motor.runForDegrees(degrees, spd, () => {
resolve();
});
const checkInterval = setInterval(() => {
if (self.abortSignal?.aborted) {
clearInterval(checkInterval);
motor.stop();
reject(new Error('Aborted'));
}
}, 30);
});
},
run_for_rotations: async (rotations: number, arg1?: any, arg2?: any) => {
self.checkAborted();
if (!motor) return;
const spd = parseSpeed(arg1, arg2);
return new Promise<void>((resolve, reject) => {
motor.runForDegrees(rotations * 360, spd, () => {
resolve();
});
const checkInterval = setInterval(() => {
if (self.abortSignal?.aborted) {
clearInterval(checkInterval);
motor.stop();
reject(new Error('Aborted'));
}
}, 30);
});
},
run_for_seconds: async (seconds: number, arg1?: any, arg2?: any) => {
self.checkAborted();
if (!motor) return;
const spd = parseSpeed(arg1, arg2);
return new Promise<void>((resolve, reject) => {
motor.runForTime(seconds, spd, () => {
resolve();
});
const checkInterval = setInterval(() => {
if (self.abortSignal?.aborted) {
clearInterval(checkInterval);
motor.stop();
reject(new Error('Aborted'));
}
}, 30);
});
},
run_to_position: async (degrees: number, arg1?: any, arg2?: any) => {
self.checkAborted();
if (!motor) return;
let direction = 'shortest path';
let speed = defaultSpeed;
if (typeof arg1 === 'string') {
direction = arg1.toLowerCase();
if (typeof arg2 === 'number') speed = arg2;
} else if (typeof arg1 === 'number') {
speed = arg1;
} else if (typeof arg1 === 'object' && arg1 !== null) {
if (arg1.direction) direction = String(arg1.direction).toLowerCase();
if (arg1.speed !== undefined) speed = Number(arg1.speed);
}
const currentNorm = ((motor.degrees % 360) + 360) % 360;
const targetNorm = ((degrees % 360) + 360) % 360;
let delta = targetNorm - currentNorm;
if (direction === 'clockwise' || direction === 'cw') {
if (delta <= 0) delta += 360;
} else if (direction === 'counterclockwise' || direction === 'ccw') {
if (delta >= 0) delta -= 360;
} else {
// shortest path
if (delta > 180) delta -= 360;
if (delta < -180) delta += 360;
}
return new Promise<void>((resolve, reject) => {
motor.runForDegrees(delta, speed, () => {
resolve();
});
const checkInterval = setInterval(() => {
if (self.abortSignal?.aborted) {
clearInterval(checkInterval);
motor.stop();
reject(new Error('Aborted'));
}
}, 30);
});
},
};
}
public createMotorPair(leftPort?: any, rightPort?: any) {
const self = this;
const normalizePort = (p: any, defaultPort: MotorPort): MotorPort => {
if (p === undefined || p === null) {
return defaultPort;
}
if (typeof p === 'string') {
const upper = p.trim().toUpperCase();
if (['A', 'B', 'C', 'D', 'E', 'F'].includes(upper)) {
return upper as MotorPort;
}
}
throw new Error(`Invalid port: ${p}`);
};
const normLeft = normalizePort(leftPort, self.engine.robot ? self.engine.robot.driveLeftPort : 'A');
const normRight = normalizePort(rightPort, self.engine.robot ? self.engine.robot.driveRightPort : 'B');
if (normLeft === normRight) {
throw new Error(`Left and right motor ports cannot be the same port: ${normLeft}`);
}
self.engine.robot?.setDrivePorts(normLeft, normRight);
const motorL = self.engine.robot?.motors.get(normLeft) || self.engine.robot?.motors.get('A');
const motorR = self.engine.robot?.motors.get(normRight) || self.engine.robot?.motors.get('B');
let defaultSpeed = 50;
// Wheel physical specs:
// Diameter = 56mm, Circumference = PI * 5.6cm = ~17.5929cm
const wheelCircumferenceCm = 2 * Math.PI * (self.engine.robot.wheelRadius * 100);
const distanceToDegrees = (amount: number, unit: string, speedPercent: number = defaultSpeed) => {
switch ((unit || 'cm').toLowerCase()) {
case 'cm':
return (amount / wheelCircumferenceCm) * 360;
case 'in':
case 'inch':
case 'inches':
return ((amount * 2.54) / wheelCircumferenceCm) * 360;
case 'rotations':
return amount * 360;
case 'seconds':
case 'sec':
case 's': {
const maxSpeed = motorL?.maxSpeedDegPerSec || 1000;
const degPerSec = Math.abs((speedPercent / 100) * maxSpeed);
return amount * degPerSec;
}
case 'degrees':
case 'deg':
default:
return amount;
}
};
return {
set_default_speed: (speed: number) => {
defaultSpeed = speed;
},
start: (arg1: any = 0, arg2?: any) => {
self.checkAborted();
let steering = 0;
let speed = defaultSpeed;
if (typeof arg1 === 'object' && arg1 !== null) {
if (arg1.steering !== undefined) steering = arg1.steering;
if (arg1.speed !== undefined) speed = arg1.speed;
} else if (typeof arg1 === 'number') {
steering = arg1;
if (typeof arg2 === 'number') speed = arg2;
} else if (arg2 !== undefined) {
speed = arg2;
}
// Clamp steering to -100 .. 100
steering = Math.max(-100, Math.min(100, steering));
let leftSpeed = speed;
let rightSpeed = speed;
if (steering > 0) {
rightSpeed = speed * (1 - (2 * steering) / 100);
} else if (steering < 0) {
leftSpeed = speed * (1 - (2 * Math.abs(steering)) / 100);
}
motorL?.start(leftSpeed);
motorR?.start(rightSpeed);
},
start_tank: (leftSpeed: number, rightSpeed: number) => {
self.checkAborted();
motorL?.start(leftSpeed);
motorR?.start(rightSpeed);
},
stop: () => {
motorL?.stop();
motorR?.stop();
self.engine.robot?.brakeChassisAndWheels();
},
move: async (amount: number, unit = 'cm', steeringOrOptions: any = 0, speedArg?: number) => {
self.checkAborted();
let steering = 0;
let spd = defaultSpeed;
if (typeof steeringOrOptions === 'object' && steeringOrOptions !== null) {
if (steeringOrOptions.steering !== undefined) steering = steeringOrOptions.steering;
if (steeringOrOptions.speed !== undefined) spd = steeringOrOptions.speed;
} else if (typeof steeringOrOptions === 'number') {
steering = steeringOrOptions;
if (typeof speedArg === 'number') spd = speedArg;
} else if (speedArg !== undefined) {
spd = speedArg;
}
const targetDeg = distanceToDegrees(amount, unit, spd);
let leftSpeed = spd;
let rightSpeed = spd;
if (steering > 0) {
rightSpeed = spd * (1 - (2 * steering) / 100);
} else if (steering < 0) {
leftSpeed = spd * (1 - (2 * Math.abs(steering)) / 100);
}
const maxSpeed = Math.max(Math.abs(leftSpeed), Math.abs(rightSpeed));
if (maxSpeed < 1e-4 || Math.abs(targetDeg) < 1e-4) {
return;
}
const targetDegL = targetDeg * (Math.abs(leftSpeed) / maxSpeed);
const targetDegR = targetDeg * (Math.abs(rightSpeed) / maxSpeed);
return new Promise<void>((resolve, reject) => {
let leftDone = Math.abs(leftSpeed) < 1e-4;
let rightDone = Math.abs(rightSpeed) < 1e-4;
const checkDone = () => {
if (leftDone && rightDone) {
self.engine.robot?.brakeChassisAndWheels();
resolve();
}
};
if (!leftDone) {
if (motorL) {
motorL.runForDegrees(targetDegL, leftSpeed, () => {
leftDone = true;
checkDone();
});
} else {
leftDone = true;
}
}
if (!rightDone) {
if (motorR) {
motorR.runForDegrees(targetDegR, rightSpeed, () => {
rightDone = true;
checkDone();
});
} else {
rightDone = true;
}
}
checkDone();
const abortCheck = setInterval(() => {
if (self.abortSignal?.aborted) {
clearInterval(abortCheck);
motorL?.stop();
motorR?.stop();
reject(new Error('Aborted'));
}
if (leftDone && rightDone) {
clearInterval(abortCheck);
}
}, 30);
});
},
move_tank: async (
amount: number,
unit = 'degrees',
leftSpeedOrOptions: any = 50,
rightSpeedArg: number = 50
) => {
self.checkAborted();
let leftSpeed = 50;
let rightSpeed = 50;
if (typeof leftSpeedOrOptions === 'object' && leftSpeedOrOptions !== null) {
if (leftSpeedOrOptions.left_speed !== undefined) leftSpeed = leftSpeedOrOptions.left_speed;
else if (leftSpeedOrOptions.leftSpeed !== undefined) leftSpeed = leftSpeedOrOptions.leftSpeed;
if (leftSpeedOrOptions.right_speed !== undefined) rightSpeed = leftSpeedOrOptions.right_speed;
else if (leftSpeedOrOptions.rightSpeed !== undefined) rightSpeed = leftSpeedOrOptions.rightSpeed;
} else if (typeof leftSpeedOrOptions === 'number') {
leftSpeed = leftSpeedOrOptions;
if (typeof rightSpeedArg === 'number') rightSpeed = rightSpeedArg;
}
const maxSpeed = Math.max(Math.abs(leftSpeed), Math.abs(rightSpeed));
const targetDeg = distanceToDegrees(amount, unit, maxSpeed);
if (maxSpeed < 1e-4 || Math.abs(targetDeg) < 1e-4) {
return;
}
const targetDegL = targetDeg * (Math.abs(leftSpeed) / maxSpeed);
const targetDegR = targetDeg * (Math.abs(rightSpeed) / maxSpeed);
return new Promise<void>((resolve, reject) => {
let leftDone = Math.abs(leftSpeed) < 1e-4;
let rightDone = Math.abs(rightSpeed) < 1e-4;
const checkDone = () => {
if (leftDone && rightDone) {
self.engine.robot?.brakeChassisAndWheels();
resolve();
}
};
if (!leftDone) {
if (motorL) {
motorL.runForDegrees(targetDegL, leftSpeed, () => {
leftDone = true;
checkDone();
});
} else {
leftDone = true;
}
}
if (!rightDone) {
if (motorR) {
motorR.runForDegrees(targetDegR, rightSpeed, () => {
rightDone = true;
checkDone();
});
} else {
rightDone = true;
}
}
checkDone();
const abortCheck = setInterval(() => {
if (self.abortSignal?.aborted) {
clearInterval(abortCheck);
motorL?.stop();
motorR?.stop();
reject(new Error('Aborted'));
}
if (leftDone && rightDone) {
clearInterval(abortCheck);
}
}, 30);
});
},
};
}
public createColorSensor(port: any = 'C') {
const self = this;
const normalizedPort: 'C' | 'D' = String(port).toUpperCase() === 'D' ? 'D' : 'C';
return {
port: normalizedPort,
get_reflected_light: () => {
self.checkAborted();
return self.sensors.sampleColorSensor(normalizedPort).reflectedLight;
},
get_color: () => {
self.checkAborted();
return self.sensors.sampleColorSensor(port).color;
},
get_rgb: () => {
self.checkAborted();
return self.sensors.sampleColorSensor(port).rgb;
},
};
}
public createDistanceSensor() {
const self = this;
return {
get_distance_cm: () => {
self.checkAborted();
return self.sensors.sampleDistanceSensor();
},
};
}
}
|