Skip to content

Commit ad49ecb

Browse files
Add Debug Script to simulator menu
- New 'Debug Script' option in Challenge Simulator dropdown (above Challenge 0) - Mirrors project/main.py hardware test code - Supports string challenge IDs (e.g., ?challenge=debug)
1 parent e69703d commit ad49ecb

File tree

3 files changed

+108
-8
lines changed

3 files changed

+108
-8
lines changed

app/js/app.js

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,13 @@ function init() {
7474
// Load challenge from URL parameter or default to 0
7575
const urlParams = new URLSearchParams(window.location.search);
7676
const challengeParam = urlParams.get("challenge");
77+
// Support both numeric and string challenge IDs (e.g., "debug")
7778
const initialChallenge =
78-
challengeParam !== null ? parseInt(challengeParam) : 0;
79+
challengeParam !== null
80+
? isNaN(parseInt(challengeParam))
81+
? challengeParam
82+
: parseInt(challengeParam)
83+
: 0;
7984
loadChallenge(initialChallenge);
8085

8186
// Hide loading overlay
@@ -265,7 +270,11 @@ function setupEventListeners() {
265270
stopExecution();
266271
}
267272
App.hasRun = false;
268-
const challengeId = parseInt(e.currentTarget.dataset.challenge);
273+
const rawChallenge = e.currentTarget.dataset.challenge;
274+
// Support both numeric and string challenge IDs
275+
const challengeId = isNaN(parseInt(rawChallenge))
276+
? rawChallenge
277+
: parseInt(rawChallenge);
269278

270279
// Update URL to reflect the new challenge
271280
const newUrl = `simulator.html?challenge=${challengeId}`;
@@ -297,7 +306,13 @@ function setupEventListeners() {
297306
window.addEventListener("popstate", (e) => {
298307
const urlParams = new URLSearchParams(window.location.search);
299308
const challengeParam = urlParams.get("challenge");
300-
const challengeId = challengeParam !== null ? parseInt(challengeParam) : 0;
309+
// Support both numeric and string challenge IDs
310+
const challengeId =
311+
challengeParam !== null
312+
? isNaN(parseInt(challengeParam))
313+
? challengeParam
314+
: parseInt(challengeParam)
315+
: 0;
301316

302317
if (App.isRunning) {
303318
stopExecution();
@@ -413,11 +428,13 @@ function loadChallenge(challengeId) {
413428
// Update dropdown text
414429
const dropdownItems = document.querySelectorAll("[data-challenge]");
415430
dropdownItems.forEach((item) => {
416-
item.classList.toggle(
417-
"active",
418-
parseInt(item.dataset.challenge) === challengeId
419-
);
420-
if (parseInt(item.dataset.challenge) === challengeId) {
431+
// Compare as strings to support both numeric and string IDs
432+
const itemChallenge = item.dataset.challenge;
433+
const isActive =
434+
itemChallenge === String(challengeId) ||
435+
parseInt(itemChallenge) === challengeId;
436+
item.classList.toggle("active", isActive);
437+
if (isActive) {
421438
App.elements.challengeDropdown.innerHTML = item.innerHTML;
422439
}
423440
});
@@ -1834,6 +1851,57 @@ function hideLoading() {
18341851
*/
18351852
function getStarterCodes() {
18361853
return {
1854+
debug: `# Debug Script: Hardware Sanity Test
1855+
# Runs a sequence of movements and distance readings
1856+
# This mirrors project/main.py for testing on real hardware
1857+
1858+
from aidriver import AIDriver, hold_state
1859+
import aidriver
1860+
1861+
aidriver.DEBUG_AIDRIVER = True
1862+
1863+
print("Initialising AIDriver hardware test...")
1864+
my_robot = AIDriver()
1865+
1866+
print("Starting tests in 3 seconds. Ensure clear space around the robot.")
1867+
hold_state(3)
1868+
1869+
# Test 1: Drive Forward
1870+
print("Test 1: drive_forward")
1871+
my_robot.drive_forward(200, 200)
1872+
hold_state(2)
1873+
my_robot.brake()
1874+
hold_state(1)
1875+
1876+
# Test 2: Drive Backward
1877+
print("Test 2: drive_backward")
1878+
my_robot.drive_backward(200, 200)
1879+
hold_state(2)
1880+
my_robot.brake()
1881+
hold_state(1)
1882+
1883+
# Test 3: Rotate Right
1884+
print("Test 3: rotate_right")
1885+
my_robot.rotate_right(200)
1886+
hold_state(2)
1887+
my_robot.brake()
1888+
hold_state(1)
1889+
1890+
# Test 4: Rotate Left
1891+
print("Test 4: rotate_left")
1892+
my_robot.rotate_left(200)
1893+
hold_state(2)
1894+
my_robot.brake()
1895+
hold_state(1)
1896+
1897+
# Test 5: Ultrasonic Sensor
1898+
print("Test 5: ultrasonic distance readings")
1899+
for i in range(5):
1900+
distance = my_robot.read_distance()
1901+
hold_state(0.5)
1902+
1903+
print("All hardware tests completed.")
1904+
`,
18371905
0: `# Challenge 0: Fix the Code
18381906
# This code has errors - can you find and fix them?
18391907

app/js/challenges.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ const Challenges = (function () {
1818
* Challenge definitions
1919
*/
2020
const definitions = {
21+
// Debug Script: Hardware test from project/main.py
22+
debug: {
23+
id: "debug",
24+
title: "Debug Script",
25+
subtitle: "Hardware Test",
26+
difficulty: DIFFICULTY.BEGINNER,
27+
description: "Run the hardware debug script (project/main.py) to test all robot functions.",
28+
goal: "Verify motors, rotation, and ultrasonic sensor are working correctly.",
29+
hints: [
30+
"This script tests all hardware functions",
31+
"Watch the robot drive forward, backward, rotate, and read distances",
32+
"Check the debug output for sensor readings",
33+
],
34+
startPosition: { x: 1000, y: 1800, heading: 0 },
35+
successCriteria: {
36+
type: "run_without_error",
37+
minDistance: 100,
38+
},
39+
path: null,
40+
obstacles: [],
41+
},
42+
2143
// Challenge 0: Fix the Code
2244
0: {
2345
id: 0,

app/simulator.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@
126126
<i class="bi bi-play-circle me-1"></i>Challenge Simulator
127127
</a>
128128
<ul class="dropdown-menu" aria-labelledby="challengeDropdown">
129+
<li>
130+
<a
131+
class="dropdown-item"
132+
href="simulator.html?challenge=debug"
133+
data-challenge="debug"
134+
>
135+
<i class="bi bi-bug me-2"></i>Debug Script
136+
</a>
137+
</li>
138+
<li><hr class="dropdown-divider" /></li>
129139
<li>
130140
<a
131141
class="dropdown-item"

0 commit comments

Comments
 (0)